Skip to content

Commit 7696d36

Browse files
[Transforms] Debug values are not remapped when cloning. (#87747)
When cloning instructions from one basic block to another, the debug values are not remapped, in the same was as the normal instructions.
1 parent c4c9d4f commit 7696d36

File tree

8 files changed

+101
-26
lines changed

8 files changed

+101
-26
lines changed

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ class DbgVariableIntrinsic : public DbgInfoIntrinsic {
311311

312312
Value *getVariableLocationOp(unsigned OpIdx) const;
313313

314-
void replaceVariableLocationOp(Value *OldValue, Value *NewValue);
314+
void replaceVariableLocationOp(Value *OldValue, Value *NewValue,
315+
bool AllowEmpty = false);
315316
void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
316317
/// Adding a new location operand will always result in this intrinsic using
317318
/// an ArgList, and must always be accompanied by a new expression that uses

llvm/include/llvm/Transforms/Scalar/JumpThreading.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Analysis/BranchProbabilityInfo.h"
2323
#include "llvm/Analysis/DomTreeUpdater.h"
2424
#include "llvm/IR/ValueHandle.h"
25+
#include "llvm/Transforms/Utils/ValueMapper.h"
2526
#include <optional>
2627
#include <utility>
2728

@@ -114,11 +115,10 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
114115
bool processBlock(BasicBlock *BB);
115116
bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB);
116117
void updateSSA(BasicBlock *BB, BasicBlock *NewBB,
117-
DenseMap<Instruction *, Value *> &ValueMapping);
118-
DenseMap<Instruction *, Value *> cloneInstructions(BasicBlock::iterator BI,
119-
BasicBlock::iterator BE,
120-
BasicBlock *NewBB,
121-
BasicBlock *PredBB);
118+
ValueToValueMapTy &ValueMapping);
119+
void cloneInstructions(ValueToValueMapTy &ValueMapping,
120+
BasicBlock::iterator BI, BasicBlock::iterator BE,
121+
BasicBlock *NewBB, BasicBlock *PredBB);
122122
bool tryThreadEdge(BasicBlock *BB,
123123
const SmallVectorImpl<BasicBlock *> &PredBBs,
124124
BasicBlock *SuccBB);

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/IR/Dominators.h"
1919
#include "llvm/Support/CommandLine.h"
2020
#include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
21+
#include "llvm/Transforms/Utils/ValueMapper.h"
2122
#include <cstdint>
2223

2324
namespace llvm {
@@ -490,6 +491,10 @@ void hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
490491
DIExpression *getExpressionForConstant(DIBuilder &DIB, const Constant &C,
491492
Type &Ty);
492493

494+
/// Remap the operands of the debug records attached to \p Inst, and the
495+
/// operands of \p Inst itself if it's a debug intrinsic.
496+
void remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst);
497+
493498
//===----------------------------------------------------------------------===//
494499
// Intrinsic pattern matching
495500
//

llvm/lib/IR/IntrinsicInst.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ static ValueAsMetadata *getAsMetadata(Value *V) {
119119
}
120120

121121
void DbgVariableIntrinsic::replaceVariableLocationOp(Value *OldValue,
122-
Value *NewValue) {
122+
Value *NewValue,
123+
bool AllowEmpty) {
123124
// If OldValue is used as the address part of a dbg.assign intrinsic replace
124125
// it with NewValue and return true.
125126
auto ReplaceDbgAssignAddress = [this, OldValue, NewValue]() -> bool {
@@ -136,6 +137,8 @@ void DbgVariableIntrinsic::replaceVariableLocationOp(Value *OldValue,
136137
auto Locations = location_ops();
137138
auto OldIt = find(Locations, OldValue);
138139
if (OldIt == Locations.end()) {
140+
if (AllowEmpty || DbgAssignAddrReplaced)
141+
return;
139142
assert(DbgAssignAddrReplaced &&
140143
"OldValue must be dbg.assign addr if unused in DIArgList");
141144
return;

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,15 +1876,15 @@ bool JumpThreadingPass::processBranchOnXOR(BinaryOperator *BO) {
18761876
static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
18771877
BasicBlock *OldPred,
18781878
BasicBlock *NewPred,
1879-
DenseMap<Instruction*, Value*> &ValueMap) {
1879+
ValueToValueMapTy &ValueMap) {
18801880
for (PHINode &PN : PHIBB->phis()) {
18811881
// Ok, we have a PHI node. Figure out what the incoming value was for the
18821882
// DestBlock.
18831883
Value *IV = PN.getIncomingValueForBlock(OldPred);
18841884

18851885
// Remap the value if necessary.
18861886
if (Instruction *Inst = dyn_cast<Instruction>(IV)) {
1887-
DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst);
1887+
ValueToValueMapTy::iterator I = ValueMap.find(Inst);
18881888
if (I != ValueMap.end())
18891889
IV = I->second;
18901890
}
@@ -1945,9 +1945,8 @@ bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) {
19451945

19461946
/// Update the SSA form. NewBB contains instructions that are copied from BB.
19471947
/// ValueMapping maps old values in BB to new ones in NewBB.
1948-
void JumpThreadingPass::updateSSA(
1949-
BasicBlock *BB, BasicBlock *NewBB,
1950-
DenseMap<Instruction *, Value *> &ValueMapping) {
1948+
void JumpThreadingPass::updateSSA(BasicBlock *BB, BasicBlock *NewBB,
1949+
ValueToValueMapTy &ValueMapping) {
19511950
// If there were values defined in BB that are used outside the block, then we
19521951
// now have to update all uses of the value to use either the original value,
19531952
// the cloned value, or some PHI derived value. This can require arbitrary
@@ -2008,14 +2007,15 @@ void JumpThreadingPass::updateSSA(
20082007
/// Clone instructions in range [BI, BE) to NewBB. For PHI nodes, we only clone
20092008
/// arguments that come from PredBB. Return the map from the variables in the
20102009
/// source basic block to the variables in the newly created basic block.
2011-
DenseMap<Instruction *, Value *>
2012-
JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
2013-
BasicBlock::iterator BE, BasicBlock *NewBB,
2014-
BasicBlock *PredBB) {
2010+
2011+
void JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
2012+
BasicBlock::iterator BI,
2013+
BasicBlock::iterator BE,
2014+
BasicBlock *NewBB,
2015+
BasicBlock *PredBB) {
20152016
// We are going to have to map operands from the source basic block to the new
20162017
// copy of the block 'NewBB'. If there are PHI nodes in the source basic
20172018
// block, evaluate them to account for entry from PredBB.
2018-
DenseMap<Instruction *, Value *> ValueMapping;
20192019

20202020
// Retargets llvm.dbg.value to any renamed variables.
20212021
auto RetargetDbgValueIfPossible = [&](Instruction *NewInst) -> bool {
@@ -2103,7 +2103,7 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
21032103
// Remap operands to patch up intra-block references.
21042104
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
21052105
if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2106-
DenseMap<Instruction *, Value *>::iterator I = ValueMapping.find(Inst);
2106+
ValueToValueMapTy::iterator I = ValueMapping.find(Inst);
21072107
if (I != ValueMapping.end())
21082108
New->setOperand(i, I->second);
21092109
}
@@ -2120,7 +2120,7 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
21202120
RetargetDbgVariableRecordIfPossible(&DVR);
21212121
}
21222122

2123-
return ValueMapping;
2123+
return;
21242124
}
21252125

21262126
/// Attempt to thread through two successive basic blocks.
@@ -2295,8 +2295,9 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB,
22952295
// We are going to have to map operands from the original BB block to the new
22962296
// copy of the block 'NewBB'. If there are PHI nodes in PredBB, evaluate them
22972297
// to account for entry from PredPredBB.
2298-
DenseMap<Instruction *, Value *> ValueMapping =
2299-
cloneInstructions(PredBB->begin(), PredBB->end(), NewBB, PredPredBB);
2298+
ValueToValueMapTy ValueMapping;
2299+
cloneInstructions(ValueMapping, PredBB->begin(), PredBB->end(), NewBB,
2300+
PredPredBB);
23002301

23012302
// Copy the edge probabilities from PredBB to NewBB.
23022303
if (BPI)
@@ -2419,8 +2420,9 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB,
24192420
}
24202421

24212422
// Copy all the instructions from BB to NewBB except the terminator.
2422-
DenseMap<Instruction *, Value *> ValueMapping =
2423-
cloneInstructions(BB->begin(), std::prev(BB->end()), NewBB, PredBB);
2423+
ValueToValueMapTy ValueMapping;
2424+
cloneInstructions(ValueMapping, BB->begin(), std::prev(BB->end()), NewBB,
2425+
PredBB);
24242426

24252427
// We didn't copy the terminator from BB over to NewBB, because there is now
24262428
// an unconditional jump to SuccBB. Insert the unconditional jump.
@@ -2675,7 +2677,7 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
26752677

26762678
// We are going to have to map operands from the original BB block into the
26772679
// PredBB block. Evaluate PHI nodes in BB.
2678-
DenseMap<Instruction*, Value*> ValueMapping;
2680+
ValueToValueMapTy ValueMapping;
26792681

26802682
BasicBlock::iterator BI = BB->begin();
26812683
for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
@@ -2689,11 +2691,14 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
26892691
// Remap operands to patch up intra-block references.
26902692
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
26912693
if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2692-
DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
2694+
ValueToValueMapTy::iterator I = ValueMapping.find(Inst);
26932695
if (I != ValueMapping.end())
26942696
New->setOperand(i, I->second);
26952697
}
26962698

2699+
// Remap debug variable operands.
2700+
remapDebugVariable(ValueMapping, New);
2701+
26972702
// If this instruction can be simplified after the operands are updated,
26982703
// just use the simplified value instead. This frequently happens due to
26992704
// phi translation.

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,9 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
11311131
if (I != ValueMapping.end())
11321132
New->setOperand(i, I->second);
11331133
}
1134+
1135+
// Remap debug variable operands.
1136+
remapDebugVariable(ValueMapping, New);
11341137
}
11351138

11361139
return NewBB;

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,6 +3685,30 @@ DIExpression *llvm::getExpressionForConstant(DIBuilder &DIB, const Constant &C,
36853685
return nullptr;
36863686
}
36873687

3688+
void llvm::remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst) {
3689+
auto RemapDebugOperands = [&Mapping](auto *DV, auto Set) {
3690+
for (auto *Op : Set) {
3691+
auto I = Mapping.find(Op);
3692+
if (I != Mapping.end())
3693+
DV->replaceVariableLocationOp(Op, I->second, /*AllowEmpty=*/true);
3694+
}
3695+
};
3696+
auto RemapAssignAddress = [&Mapping](auto *DA) {
3697+
auto I = Mapping.find(DA->getAddress());
3698+
if (I != Mapping.end())
3699+
DA->setAddress(I->second);
3700+
};
3701+
if (auto DVI = dyn_cast<DbgVariableIntrinsic>(Inst))
3702+
RemapDebugOperands(DVI, DVI->location_ops());
3703+
if (auto DAI = dyn_cast<DbgAssignIntrinsic>(Inst))
3704+
RemapAssignAddress(DAI);
3705+
for (DbgVariableRecord &DVR : filterDbgVars(Inst->getDbgRecordRange())) {
3706+
RemapDebugOperands(&DVR, DVR.location_ops());
3707+
if (DVR.isDbgAssign())
3708+
RemapAssignAddress(&DVR);
3709+
}
3710+
}
3711+
36883712
namespace {
36893713

36903714
/// A potential constituent of a bitreverse or bswap expression. See

llvm/test/Transforms/CallSiteSplitting/callsite-split-debug.ll

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S -passes=callsite-splitting -o - < %s | FileCheck %s
1+
; RUN: opt -S -passes=callsite-splitting -o - < %s | FileCheck %s --check-prefixes=CHECK,CHECK-DEBUG
22
; RUN: opt -S -strip-debug -passes=callsite-splitting -o - < %s | FileCheck %s
33

44
define internal i16 @bar(i16 %p1, i16 %p2) {
@@ -8,6 +8,9 @@ define internal i16 @bar(i16 %p1, i16 %p2) {
88

99
define i16 @foo(i16 %in) {
1010
bb0:
11+
%a = alloca i16, align 4, !DIAssignID !12
12+
call void @llvm.dbg.assign(metadata i1 undef, metadata !11, metadata !DIExpression(), metadata !12, metadata ptr %a, metadata !DIExpression()), !dbg !8
13+
store i16 7, ptr %a, align 4, !DIAssignID !13
1114
br label %bb1
1215

1316
bb1:
@@ -20,13 +23,21 @@ bb2:
2023
CallsiteBB:
2124
%1 = phi i16 [ 0, %bb1 ], [ 1, %bb2 ]
2225
%c = phi i16 [ 2, %bb1 ], [ 3, %bb2 ]
26+
%p = phi ptr [ %a, %bb1 ], [ %a, %bb2 ]
27+
call void @llvm.dbg.value(metadata i16 %1, metadata !7, metadata !DIExpression()), !dbg !8
2328
call void @llvm.dbg.value(metadata i16 %c, metadata !7, metadata !DIExpression()), !dbg !8
29+
call void @llvm.dbg.value(metadata !DIArgList(i16 %1, i16 %c), metadata !7, metadata !DIExpression()), !dbg !8
30+
call void @llvm.dbg.value(metadata !DIArgList(i16 %c, i16 %c), metadata !7, metadata !DIExpression()), !dbg !8
31+
call void @llvm.dbg.assign(metadata i16 %1, metadata !11, metadata !DIExpression(), metadata !13, metadata ptr %a, metadata !DIExpression()), !dbg !8
32+
call void @llvm.dbg.assign(metadata i16 %c, metadata !11, metadata !DIExpression(), metadata !13, metadata ptr %a, metadata !DIExpression()), !dbg !8
33+
call void @llvm.dbg.assign(metadata i16 %1, metadata !11, metadata !DIExpression(), metadata !13, metadata ptr %p, metadata !DIExpression()), !dbg !8
2434
%2 = call i16 @bar(i16 %1, i16 5)
2535
ret i16 %2
2636
}
2737

2838
; Function Attrs: nounwind readnone speculatable
2939
declare void @llvm.dbg.value(metadata, metadata, metadata) #0
40+
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
3041

3142
attributes #0 = { nounwind readnone speculatable }
3243

@@ -43,14 +54,37 @@ attributes #0 = { nounwind readnone speculatable }
4354
!6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 4, unit: !0)
4455
!7 = !DILocalVariable(name: "c", scope: !6, line: 5, type: !5)
4556
!8 = !DILocation(line: 5, column: 7, scope: !6)
57+
!11 = !DILocalVariable(name: "a", scope: !6, line: 6, type: !5)
58+
!12 = distinct !DIAssignID()
59+
!13 = distinct !DIAssignID()
4660

4761
; The optimization should trigger even in the presence of the dbg.value in
4862
; CallSiteBB.
4963

5064
; CHECK-LABEL: @foo
5165
; CHECK-LABEL: bb1.split:
66+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata i16 0, metadata ![[DBG_1:[0-9]+]], {{.*}}
67+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata i16 2, metadata ![[DBG_1]], {{.*}}
68+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata !DIArgList(i16 0, i16 2), {{.*}}
69+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata !DIArgList(i16 2, i16 2), {{.*}}
70+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 0, metadata ![[DBG_2:[0-9]+]], {{.*}}
71+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 2, metadata ![[DBG_2]], {{.*}}
72+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 0, metadata ![[DBG_2]], metadata !DIExpression(), metadata ![[ID_1:[0-9]+]], metadata ptr %a, {{.*}}
5273
; CHECK: [[TMP1:%[0-9]+]] = call i16 @bar(i16 0, i16 5)
74+
5375
; CHECK-LABEL: bb2.split:
76+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata i16 1, metadata ![[DBG_1]], {{.*}}
77+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata i16 3, metadata ![[DBG_1]], {{.*}}
78+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata !DIArgList(i16 1, i16 3), {{.*}}
79+
; CHECK-DEBUG: call void @llvm.dbg.value(metadata !DIArgList(i16 3, i16 3), {{.*}}
80+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 1, metadata ![[DBG_2]], {{.*}}
81+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 3, metadata ![[DBG_2]], {{.*}}
82+
; CHECK-DEBUG: call void @llvm.dbg.assign(metadata i16 1, metadata ![[DBG_2]], metadata !DIExpression(), metadata ![[ID_1:[0-9]+]], metadata ptr %a, {{.*}}
5483
; CHECK: [[TMP2:%[0-9]+]] = call i16 @bar(i16 1, i16 5)
84+
5585
; CHECK-LABEL: CallsiteBB
5686
; CHECK: %phi.call = phi i16 [ [[TMP2]], %bb2.split ], [ [[TMP1]], %bb1.split
87+
88+
; CHECK-DEBUG-DAG: ![[DBG_1]] = !DILocalVariable(name: "c"{{.*}})
89+
; CHECK-DEBUG-DAG: ![[DBG_2]] = !DILocalVariable(name: "a"{{.*}})
90+
; CHECK-DEBUG-DAG: ![[ID_1]] = distinct !DIAssignID()

0 commit comments

Comments
 (0)