Skip to content

Commit 3ef98bc

Browse files
committed
[DebugInfo][RemoveDIs] Support maintaining DPValues in CodeGenPrepare (#73660)
CodeGenPrepare needs to support the maintenence of DPValues, the non-instruction replacement for dbg.value intrinsics. This means there are a few functions we need to duplicate or replicate the functionality of: * fixupDbgValue for setting users of sunk addr GEPs, * The remains of placeDbgValues needs a DPValue implementation for sinking * Rollback of RAUWs needs to update DPValues * Rollback of instruction removal needs supporting (see github #73350) * A few places where we have to use iterators rather than instructions. There are three places where we have to use the setHeadBit call on iterators to indicate which portion of debug-info records we're about to splice around. This is because CodeGenPrepare, unlike other optimisation passes, is very much concerned with which block an operation occurs in and where in the block instructions are because it's preparing things to be in a format that's good for SelectionDAG. There isn't a large amount of test coverage for debuginfo behaviours in this pass, hence I've added some more.
1 parent 73d9f5f commit 3ef98bc

File tree

8 files changed

+218
-72
lines changed

8 files changed

+218
-72
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 151 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class CodeGenPrepare : public FunctionPass {
459459
bool optimizeExtractElementInst(Instruction *Inst);
460460
bool dupRetToEnableTailCallOpts(BasicBlock *BB, ModifyDT &ModifiedDT);
461461
bool fixupDbgValue(Instruction *I);
462+
bool fixupDPValue(DPValue &I);
462463
bool placeDbgValues(Function &F);
463464
bool placePseudoProbes(Function &F);
464465
bool canFormExtLd(const SmallVectorImpl<Instruction *> &MovedExts,
@@ -1753,8 +1754,8 @@ static bool sinkCmpExpression(CmpInst *Cmp, const TargetLowering &TLI) {
17531754
BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
17541755
assert(InsertPt != UserBB->end());
17551756
InsertedCmp = CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(),
1756-
Cmp->getOperand(0), Cmp->getOperand(1), "",
1757-
&*InsertPt);
1757+
Cmp->getOperand(0), Cmp->getOperand(1), "");
1758+
InsertedCmp->insertBefore(*UserBB, InsertPt);
17581759
// Propagate the debug info.
17591760
InsertedCmp->setDebugLoc(Cmp->getDebugLoc());
17601761
}
@@ -2066,10 +2067,13 @@ SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
20662067
// Sink the trunc
20672068
BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt();
20682069
TruncInsertPt++;
2070+
// It will go ahead of any debug-info.
2071+
TruncInsertPt.setHeadBit(true);
20692072
assert(TruncInsertPt != TruncUserBB->end());
20702073

20712074
InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift,
2072-
TruncI->getType(), "", &*TruncInsertPt);
2075+
TruncI->getType(), "");
2076+
InsertedTrunc->insertBefore(*TruncUserBB, TruncInsertPt);
20732077
InsertedTrunc->setDebugLoc(TruncI->getDebugLoc());
20742078

20752079
MadeChange = true;
@@ -2234,7 +2238,9 @@ static bool despeculateCountZeros(IntrinsicInst *CountZeros,
22342238
// Create another block after the count zero intrinsic. A PHI will be added
22352239
// in this block to select the result of the intrinsic or the bit-width
22362240
// constant if the input to the intrinsic is zero.
2237-
BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(CountZeros));
2241+
BasicBlock::iterator SplitPt = std::next(BasicBlock::iterator(CountZeros));
2242+
// Any debug-info after CountZeros should not be included.
2243+
SplitPt.setHeadBit(true);
22382244
BasicBlock *EndBlock = CallBlock->splitBasicBlock(SplitPt, "cond.end");
22392245
if (IsHugeFunc)
22402246
FreshBBs.insert(EndBlock);
@@ -2829,34 +2835,44 @@ class TypePromotionTransaction {
28292835
Instruction *PrevInst;
28302836
BasicBlock *BB;
28312837
} Point;
2838+
std::optional<DPValue::self_iterator> BeforeDPValue = std::nullopt;
28322839

28332840
/// Remember whether or not the instruction had a previous instruction.
28342841
bool HasPrevInstruction;
28352842

28362843
public:
28372844
/// Record the position of \p Inst.
28382845
InsertionHandler(Instruction *Inst) {
2839-
BasicBlock::iterator It = Inst->getIterator();
2840-
HasPrevInstruction = (It != (Inst->getParent()->begin()));
2841-
if (HasPrevInstruction)
2842-
Point.PrevInst = &*--It;
2843-
else
2844-
Point.BB = Inst->getParent();
2846+
HasPrevInstruction = (Inst != &*(Inst->getParent()->begin()));
2847+
BasicBlock *BB = Inst->getParent();
2848+
2849+
// Record where we would have to re-insert the instruction in the sequence
2850+
// of DPValues, if we ended up reinserting.
2851+
if (BB->IsNewDbgInfoFormat)
2852+
BeforeDPValue = Inst->getDbgReinsertionPosition();
2853+
2854+
if (HasPrevInstruction) {
2855+
Point.PrevInst = &*std::prev(Inst->getIterator());
2856+
} else {
2857+
Point.BB = BB;
2858+
}
28452859
}
28462860

28472861
/// Insert \p Inst at the recorded position.
28482862
void insert(Instruction *Inst) {
28492863
if (HasPrevInstruction) {
28502864
if (Inst->getParent())
28512865
Inst->removeFromParent();
2852-
Inst->insertAfter(Point.PrevInst);
2866+
Inst->insertAfter(&*Point.PrevInst);
28532867
} else {
2854-
Instruction *Position = &*Point.BB->getFirstInsertionPt();
2868+
BasicBlock::iterator Position = Point.BB->getFirstInsertionPt();
28552869
if (Inst->getParent())
2856-
Inst->moveBefore(Position);
2870+
Inst->moveBefore(*Point.BB, Position);
28572871
else
2858-
Inst->insertBefore(Position);
2872+
Inst->insertBefore(*Point.BB, Position);
28592873
}
2874+
2875+
Inst->getParent()->reinsertInstInDPValues(Inst, BeforeDPValue);
28602876
}
28612877
};
28622878

@@ -3059,6 +3075,8 @@ class TypePromotionTransaction {
30593075
SmallVector<InstructionAndIdx, 4> OriginalUses;
30603076
/// Keep track of the debug users.
30613077
SmallVector<DbgValueInst *, 1> DbgValues;
3078+
/// And non-instruction debug-users too.
3079+
SmallVector<DPValue *, 1> DPValues;
30623080

30633081
/// Keep track of the new value so that we can undo it by replacing
30643082
/// instances of the new value with the original value.
@@ -3079,7 +3097,7 @@ class TypePromotionTransaction {
30793097
}
30803098
// Record the debug uses separately. They are not in the instruction's
30813099
// use list, but they are replaced by RAUW.
3082-
findDbgValues(DbgValues, Inst);
3100+
findDbgValues(DbgValues, Inst, &DPValues);
30833101

30843102
// Now, we can replace the uses.
30853103
Inst->replaceAllUsesWith(New);
@@ -3096,6 +3114,10 @@ class TypePromotionTransaction {
30963114
// correctness and utility of debug value instructions.
30973115
for (auto *DVI : DbgValues)
30983116
DVI->replaceVariableLocationOp(New, Inst);
3117+
// Similar story with DPValues, the non-instruction representation of
3118+
// dbg.values.
3119+
for (DPValue *DPV : DPValues) // tested by transaction-test I'm adding
3120+
DPV->replaceVariableLocationOp(New, Inst);
30993121
}
31003122
};
31013123

@@ -6749,7 +6771,7 @@ bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) {
67496771
!TLI->isLoadExtLegal(ISD::ZEXTLOAD, LoadResultVT, TruncVT))
67506772
return false;
67516773

6752-
IRBuilder<> Builder(Load->getNextNode());
6774+
IRBuilder<> Builder(Load->getNextNonDebugInstruction());
67536775
auto *NewAnd = cast<Instruction>(
67546776
Builder.CreateAnd(Load, ConstantInt::get(Ctx, DemandBits)));
67556777
// Mark this instruction as "inserted by CGP", so that other
@@ -7005,7 +7027,9 @@ bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
70057027
// Split the select block, according to how many (if any) values go on each
70067028
// side.
70077029
BasicBlock *StartBlock = SI->getParent();
7008-
BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(LastSI));
7030+
BasicBlock::iterator SplitPt = std::next(BasicBlock::iterator(LastSI));
7031+
// We should split before any debug-info.
7032+
SplitPt.setHeadBit(true);
70097033

70107034
IRBuilder<> IB(SI);
70117035
auto *CondFr = IB.CreateFreeze(SI->getCondition(), SI->getName() + ".frozen");
@@ -7017,18 +7041,18 @@ bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
70177041
BranchInst *FalseBranch = nullptr;
70187042
if (TrueInstrs.size() == 0) {
70197043
FalseBranch = cast<BranchInst>(SplitBlockAndInsertIfElse(
7020-
CondFr, &*SplitPt, false, nullptr, nullptr, LI));
7044+
CondFr, SplitPt, false, nullptr, nullptr, LI));
70217045
FalseBlock = FalseBranch->getParent();
70227046
EndBlock = cast<BasicBlock>(FalseBranch->getOperand(0));
70237047
} else if (FalseInstrs.size() == 0) {
70247048
TrueBranch = cast<BranchInst>(SplitBlockAndInsertIfThen(
7025-
CondFr, &*SplitPt, false, nullptr, nullptr, LI));
7049+
CondFr, SplitPt, false, nullptr, nullptr, LI));
70267050
TrueBlock = TrueBranch->getParent();
70277051
EndBlock = cast<BasicBlock>(TrueBranch->getOperand(0));
70287052
} else {
70297053
Instruction *ThenTerm = nullptr;
70307054
Instruction *ElseTerm = nullptr;
7031-
SplitBlockAndInsertIfThenElse(CondFr, &*SplitPt, &ThenTerm, &ElseTerm,
7055+
SplitBlockAndInsertIfThenElse(CondFr, SplitPt, &ThenTerm, &ElseTerm,
70327056
nullptr, nullptr, LI);
70337057
TrueBranch = cast<BranchInst>(ThenTerm);
70347058
FalseBranch = cast<BranchInst>(ElseTerm);
@@ -8095,10 +8119,14 @@ static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI,
80958119
}
80968120

80978121
bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8122+
bool AnyChange = false;
8123+
for (DPValue &DPV : I->getDbgValueRange())
8124+
AnyChange |= fixupDPValue(DPV);
8125+
80988126
// Bail out if we inserted the instruction to prevent optimizations from
80998127
// stepping on each other's toes.
81008128
if (InsertedInsts.count(I))
8101-
return false;
8129+
return AnyChange;
81028130

81038131
// TODO: Move into the switch on opcode below here.
81048132
if (PHINode *P = dyn_cast<PHINode>(I)) {
@@ -8112,7 +8140,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
81128140
++NumPHIsElim;
81138141
return true;
81148142
}
8115-
return false;
8143+
return AnyChange;
81168144
}
81178145

81188146
if (CastInst *CI = dyn_cast<CastInst>(I)) {
@@ -8123,7 +8151,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
81238151
// the address of globals out of a loop). If this is the case, we don't
81248152
// want to forward-subst the cast.
81258153
if (isa<Constant>(CI->getOperand(0)))
8126-
return false;
8154+
return AnyChange;
81278155

81288156
if (OptimizeNoopCopyExpression(CI, *TLI, *DL))
81298157
return true;
@@ -8149,7 +8177,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
81498177
return MadeChange | optimizeExtUses(I);
81508178
}
81518179
}
8152-
return false;
8180+
return AnyChange;
81538181
}
81548182

81558183
if (auto *Cmp = dyn_cast<CmpInst>(I))
@@ -8244,7 +8272,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
82448272
return true;
82458273
}
82468274
}
8247-
return false;
8275+
return AnyChange;
82488276
}
82498277

82508278
if (tryToSinkFreeOperands(I))
@@ -8269,7 +8297,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
82698297
return optimizeBranch(cast<BranchInst>(I), *TLI, FreshBBs, IsHugeFunc);
82708298
}
82718299

8272-
return false;
8300+
return AnyChange;
82738301
}
82748302

82758303
/// Given an OR instruction, check to see if this is a bitreverse
@@ -8359,6 +8387,49 @@ bool CodeGenPrepare::fixupDbgValue(Instruction *I) {
83598387
return AnyChange;
83608388
}
83618389

8390+
// FIXME: should updating debug-info really cause the "changed" flag to fire,
8391+
// which can cause a function to be reprocessed?
8392+
bool CodeGenPrepare::fixupDPValue(DPValue &DPV) {
8393+
if (DPV.Type != DPValue::LocationType::Value)
8394+
return false;
8395+
8396+
// Does this DPValue refer to a sunk address calculation?
8397+
bool AnyChange = false;
8398+
SmallDenseSet<Value *> LocationOps(DPV.location_ops().begin(),
8399+
DPV.location_ops().end());
8400+
for (Value *Location : LocationOps) {
8401+
WeakTrackingVH SunkAddrVH = SunkAddrs[Location];
8402+
Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr;
8403+
if (SunkAddr) {
8404+
// Point dbg.value at locally computed address, which should give the best
8405+
// opportunity to be accurately lowered. This update may change the type
8406+
// of pointer being referred to; however this makes no difference to
8407+
// debugging information, and we can't generate bitcasts that may affect
8408+
// codegen.
8409+
DPV.replaceVariableLocationOp(Location, SunkAddr);
8410+
AnyChange = true;
8411+
}
8412+
}
8413+
return AnyChange;
8414+
}
8415+
8416+
static void DbgInserterHelper(DbgValueInst *DVI, Instruction *VI) {
8417+
DVI->removeFromParent();
8418+
if (isa<PHINode>(VI))
8419+
DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt());
8420+
else
8421+
DVI->insertAfter(VI);
8422+
}
8423+
8424+
static void DbgInserterHelper(DPValue *DPV, Instruction *VI) {
8425+
DPV->removeFromParent();
8426+
BasicBlock *VIBB = VI->getParent();
8427+
if (isa<PHINode>(VI))
8428+
VIBB->insertDPValueBefore(DPV, VIBB->getFirstInsertionPt());
8429+
else
8430+
VIBB->insertDPValueAfter(DPV, VI);
8431+
}
8432+
83628433
// A llvm.dbg.value may be using a value before its definition, due to
83638434
// optimizations in this pass and others. Scan for such dbg.values, and rescue
83648435
// them by moving the dbg.value to immediately after the value definition.
@@ -8368,59 +8439,69 @@ bool CodeGenPrepare::placeDbgValues(Function &F) {
83688439
bool MadeChange = false;
83698440
DominatorTree DT(F);
83708441

8371-
for (BasicBlock &BB : F) {
8372-
for (Instruction &Insn : llvm::make_early_inc_range(BB)) {
8373-
DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn);
8374-
if (!DVI)
8442+
auto DbgProcessor = [&](auto *DbgItem, Instruction *Position) {
8443+
SmallVector<Instruction *, 4> VIs;
8444+
for (Value *V : DbgItem->location_ops())
8445+
if (Instruction *VI = dyn_cast_or_null<Instruction>(V))
8446+
VIs.push_back(VI);
8447+
8448+
// This item may depend on multiple instructions, complicating any
8449+
// potential sink. This block takes the defensive approach, opting to
8450+
// "undef" the item if it has more than one instruction and any of them do
8451+
// not dominate iem.
8452+
for (Instruction *VI : VIs) {
8453+
if (VI->isTerminator())
83758454
continue;
83768455

8377-
SmallVector<Instruction *, 4> VIs;
8378-
for (Value *V : DVI->getValues())
8379-
if (Instruction *VI = dyn_cast_or_null<Instruction>(V))
8380-
VIs.push_back(VI);
8381-
8382-
// This DVI may depend on multiple instructions, complicating any
8383-
// potential sink. This block takes the defensive approach, opting to
8384-
// "undef" the DVI if it has more than one instruction and any of them do
8385-
// not dominate DVI.
8386-
for (Instruction *VI : VIs) {
8387-
if (VI->isTerminator())
8388-
continue;
8456+
// If VI is a phi in a block with an EHPad terminator, we can't insert
8457+
// after it.
8458+
if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad())
8459+
continue;
83898460

8390-
// If VI is a phi in a block with an EHPad terminator, we can't insert
8391-
// after it.
8392-
if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad())
8393-
continue;
8461+
// If the defining instruction dominates the dbg.value, we do not need
8462+
// to move the dbg.value.
8463+
if (DT.dominates(VI, Position))
8464+
continue;
83948465

8395-
// If the defining instruction dominates the dbg.value, we do not need
8396-
// to move the dbg.value.
8397-
if (DT.dominates(VI, DVI))
8398-
continue;
8466+
// If we depend on multiple instructions and any of them doesn't
8467+
// dominate this DVI, we probably can't salvage it: moving it to
8468+
// after any of the instructions could cause us to lose the others.
8469+
if (VIs.size() > 1) {
8470+
LLVM_DEBUG(
8471+
dbgs()
8472+
<< "Unable to find valid location for Debug Value, undefing:\n"
8473+
<< *DbgItem);
8474+
DbgItem->setKillLocation();
8475+
break;
8476+
}
83998477

8400-
// If we depend on multiple instructions and any of them doesn't
8401-
// dominate this DVI, we probably can't salvage it: moving it to
8402-
// after any of the instructions could cause us to lose the others.
8403-
if (VIs.size() > 1) {
8404-
LLVM_DEBUG(
8405-
dbgs()
8406-
<< "Unable to find valid location for Debug Value, undefing:\n"
8407-
<< *DVI);
8408-
DVI->setKillLocation();
8409-
break;
8410-
}
8478+
LLVM_DEBUG(dbgs() << "Moving Debug Value before :\n"
8479+
<< *DbgItem << ' ' << *VI);
8480+
DbgInserterHelper(DbgItem, VI);
8481+
MadeChange = true;
8482+
++NumDbgValueMoved;
8483+
}
8484+
};
84118485

8412-
LLVM_DEBUG(dbgs() << "Moving Debug Value before :\n"
8413-
<< *DVI << ' ' << *VI);
8414-
DVI->removeFromParent();
8415-
if (isa<PHINode>(VI))
8416-
DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt());
8417-
else
8418-
DVI->insertAfter(VI);
8419-
MadeChange = true;
8420-
++NumDbgValueMoved;
8486+
for (BasicBlock &BB : F) {
8487+
for (Instruction &Insn : llvm::make_early_inc_range(BB)) {
8488+
// Process dbg.value intrinsics.
8489+
DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn);
8490+
if (DVI) {
8491+
DbgProcessor(DVI, DVI);
8492+
continue;
8493+
}
8494+
8495+
// If this isn't a dbg.value, process any attached DPValue records
8496+
// attached to this instruction.
8497+
for (DPValue &DPV : llvm::make_early_inc_range(Insn.getDbgValueRange())) {
8498+
if (DPV.Type != DPValue::LocationType::Value)
8499+
continue;
8500+
DbgProcessor(&DPV, &Insn);
84218501
}
84228502
}
84238503
}
8504+
84248505
return MadeChange;
84258506
}
84268507

0 commit comments

Comments
 (0)