Skip to content

Commit 3356818

Browse files
committed
Reapply [RemoveDIs] Enable DPLabels conversion [3b/3] (#82639)
Enables conversion between llvm.dbg.label and DPLabel.
1 parent 6067711 commit 3356818

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class BasicBlock;
6262
class MDNode;
6363
class Module;
6464
class DbgVariableIntrinsic;
65+
class DbgInfoIntrinsic;
66+
class DbgLabelInst;
6567
class DIAssignID;
6668
class DPMarker;
6769
class DPValue;
@@ -80,6 +82,7 @@ class raw_ostream;
8082
/// clone
8183
/// isIdenticalToWhenDefined
8284
/// both print methods
85+
/// createDebugIntrinsic
8386
class DbgRecord : public ilist_node<DbgRecord> {
8487
public:
8588
/// Marker that this DbgRecord is linked into.
@@ -103,6 +106,11 @@ class DbgRecord : public ilist_node<DbgRecord> {
103106
void print(raw_ostream &O, bool IsForDebug = false) const;
104107
void print(raw_ostream &O, ModuleSlotTracker &MST, bool IsForDebug) const;
105108
bool isIdenticalToWhenDefined(const DbgRecord &R) const;
109+
/// Convert this DbgRecord back into an appropriate llvm.dbg.* intrinsic.
110+
/// \p InsertBefore Optional position to insert this intrinsic.
111+
/// \returns A new llvm.dbg.* intrinsic representiung this DbgRecord.
112+
DbgInfoIntrinsic *createDebugIntrinsic(Module *M,
113+
Instruction *InsertBefore) const;
106114
///@}
107115

108116
/// Same as isIdenticalToWhenDefined but checks DebugLoc too.
@@ -177,6 +185,8 @@ class DPLabel : public DbgRecord {
177185
DPLabel *clone() const;
178186
void print(raw_ostream &O, bool IsForDebug = false) const;
179187
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
188+
DbgLabelInst *createDebugIntrinsic(Module *M,
189+
Instruction *InsertBefore) const;
180190

181191
void setLabel(DILabel *NewLabel) { Label = NewLabel; }
182192
DILabel *getLabel() const { return Label; }

llvm/lib/IR/BasicBlock.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ void BasicBlock::convertToNewDbgValues() {
8181
continue;
8282
}
8383

84+
if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(&I)) {
85+
DPVals.push_back(new DPLabel(DLI->getLabel(), DLI->getDebugLoc()));
86+
DLI->eraseFromParent();
87+
continue;
88+
}
89+
8490
if (DPVals.empty())
8591
continue;
8692

@@ -107,16 +113,12 @@ void BasicBlock::convertFromNewDbgValues() {
107113
continue;
108114

109115
DPMarker &Marker = *Inst.DbgMarker;
110-
for (DbgRecord &DR : Marker.getDbgValueRange()) {
111-
if (auto *DPV = dyn_cast<DPValue>(&DR))
112-
InstList.insert(Inst.getIterator(),
113-
DPV->createDebugIntrinsic(getModule(), nullptr));
114-
else
115-
llvm_unreachable("unsupported DbgRecord kind");
116-
}
116+
for (DbgRecord &DR : Marker.getDbgValueRange())
117+
InstList.insert(Inst.getIterator(),
118+
DR.createDebugIntrinsic(getModule(), nullptr));
117119

118120
Marker.eraseFromParent();
119-
};
121+
}
120122

121123
// Assume no trailing DPValues: we could technically create them at the end
122124
// of the block, after a terminator, but this would be non-cannonical and

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ bool DbgRecord::isEquivalentTo(const DbgRecord &R) const {
112112
return getDebugLoc() == R.getDebugLoc() && isIdenticalToWhenDefined(R);
113113
}
114114

115+
DbgInfoIntrinsic *
116+
DbgRecord::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const {
117+
switch (RecordKind) {
118+
case ValueKind:
119+
return cast<DPValue>(this)->createDebugIntrinsic(M, InsertBefore);
120+
case LabelKind:
121+
return cast<DPLabel>(this)->createDebugIntrinsic(M, InsertBefore);
122+
};
123+
llvm_unreachable("unsupported DbgRecord kind");
124+
}
125+
115126
DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
116127
DIExpression *Expr, const DILocation *DI) {
117128
return new DPValue(ValueAsMetadata::get(Location), DV, Expr, DI,
@@ -377,6 +388,20 @@ DPValue::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const {
377388
return DVI;
378389
}
379390

391+
DbgLabelInst *DPLabel::createDebugIntrinsic(Module *M,
392+
Instruction *InsertBefore) const {
393+
auto *LabelFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_label);
394+
Value *Args[] = {
395+
MetadataAsValue::get(getDebugLoc()->getContext(), getLabel())};
396+
DbgLabelInst *DbgLabel = cast<DbgLabelInst>(
397+
CallInst::Create(LabelFn->getFunctionType(), LabelFn, Args));
398+
DbgLabel->setTailCall();
399+
DbgLabel->setDebugLoc(getDebugLoc());
400+
if (InsertBefore)
401+
DbgLabel->insertBefore(InsertBefore);
402+
return DbgLabel;
403+
}
404+
380405
Value *DPValue::getAddress() const {
381406
auto *MD = getRawAddress();
382407
if (auto *V = dyn_cast<ValueAsMetadata>(MD))

0 commit comments

Comments
 (0)