Skip to content

Commit 1de5104

Browse files
committed
[RemoveDIs][NFC] Add DPLabel class [2/3]
1 parent 4a23ab4 commit 1de5104

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ class raw_ostream;
7979
/// deleteRecord
8080
/// clone
8181
/// isIdenticalToWhenDefined
82-
/// isEquivalentTo
8382
/// both print methods
8483
class DbgRecord : public ilist_node<DbgRecord> {
8584
public:
8685
/// Marker that this DbgRecord is linked into.
8786
DPMarker *Marker = nullptr;
8887
/// Subclass discriminator.
89-
enum Kind : uint8_t { ValueKind };
88+
enum Kind : uint8_t { ValueKind, LabelKind };
9089

9190
protected:
9291
DebugLoc DbgLoc;
@@ -104,9 +103,11 @@ class DbgRecord : public ilist_node<DbgRecord> {
104103
void print(raw_ostream &O, bool IsForDebug = false) const;
105104
void print(raw_ostream &O, ModuleSlotTracker &MST, bool IsForDebug) const;
106105
bool isIdenticalToWhenDefined(const DbgRecord &R) const;
107-
bool isEquivalentTo(const DbgRecord &R) const;
108106
///@}
109107

108+
/// Same as isIdenticalToWhenDefined but checks DebugLoc too.
109+
bool isEquivalentTo(const DbgRecord &R) const;
110+
110111
Kind getRecordKind() const { return RecordKind; }
111112

112113
void setMarker(DPMarker *M) { Marker = M; }
@@ -156,6 +157,31 @@ class DbgRecord : public ilist_node<DbgRecord> {
156157
~DbgRecord() = default;
157158
};
158159

160+
/// Records a position in IR for a source label (DILabel). Corresponds to the
161+
/// llvm.dbg.label intrinsic.
162+
/// FIXME: Rename DbgLabelRecord when DPValue is renamed to DbgVariableRecord.
163+
class DPLabel : public DbgRecord {
164+
DILabel *Label;
165+
166+
public:
167+
DPLabel(DILabel *Label, DebugLoc DL)
168+
: DbgRecord(LabelKind, DL), Label(Label) {
169+
assert(Label && "unexpected nullptr");
170+
}
171+
172+
DPLabel *clone() const;
173+
void print(raw_ostream &O, bool IsForDebug = false) const;
174+
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
175+
176+
void setLabel(DILabel *NewLabel) { Label = NewLabel; }
177+
DILabel *getLabel() const { return Label; }
178+
179+
/// Support type inquiry through isa, cast, and dyn_cast.
180+
static bool classof(const DbgRecord *E) {
181+
return E->getRecordKind() == LabelKind;
182+
}
183+
};
184+
159185
/// Record of a variable value-assignment, aka a non instruction representation
160186
/// of the dbg.value intrinsic.
161187
///

llvm/lib/IR/AsmWriter.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ static const Module *getModuleFromDPI(const DPMarker *Marker) {
292292
return M ? M->getParent() : nullptr;
293293
}
294294

295-
static const Module *getModuleFromDPI(const DPValue *DPV) {
296-
return DPV->getMarker() ? getModuleFromDPI(DPV->getMarker()) : nullptr;
295+
static const Module *getModuleFromDPI(const DbgRecord *DR) {
296+
return DR->getMarker() ? getModuleFromDPI(DR->getMarker()) : nullptr;
297297
}
298298

299299
static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
@@ -2676,6 +2676,7 @@ class AssemblyWriter {
26762676
void printInstruction(const Instruction &I);
26772677
void printDPMarker(const DPMarker &DPI);
26782678
void printDPValue(const DPValue &DPI);
2679+
void printDPLabel(const DPLabel &DPL);
26792680
void printDbgRecord(const DbgRecord &DPI);
26802681

26812682
void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle);
@@ -4622,6 +4623,16 @@ void AssemblyWriter::printDPValue(const DPValue &Value) {
46224623
Out << " }";
46234624
}
46244625

4626+
void AssemblyWriter::printDPLabel(const DPLabel &Label) {
4627+
// There's no formal representation of a DPLabel -- print purely as
4628+
// a debugging aid.
4629+
Out << " DPLabel { ";
4630+
auto WriterCtx = getContext();
4631+
WriteAsOperandInternal(Out, Label.getLabel(), WriterCtx, true);
4632+
Out << " marker @" << Label.getMarker();
4633+
Out << " }";
4634+
}
4635+
46254636
void AssemblyWriter::printMetadataAttachments(
46264637
const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
46274638
StringRef Separator) {
@@ -4885,6 +4896,12 @@ void DPMarker::print(raw_ostream &ROS, ModuleSlotTracker &MST,
48854896
W.printDPMarker(*this);
48864897
}
48874898

4899+
void DPLabel::print(raw_ostream &ROS, bool IsForDebug) const {
4900+
4901+
ModuleSlotTracker MST(getModuleFromDPI(this), true);
4902+
print(ROS, MST, IsForDebug);
4903+
}
4904+
48884905
void DPValue::print(raw_ostream &ROS, ModuleSlotTracker &MST,
48894906
bool IsForDebug) const {
48904907
// There's no formal representation of a DPValue -- print purely as a
@@ -4904,6 +4921,24 @@ void DPValue::print(raw_ostream &ROS, ModuleSlotTracker &MST,
49044921
W.printDPValue(*this);
49054922
}
49064923

4924+
void DPLabel::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4925+
bool IsForDebug) const {
4926+
// There's no formal representation of a DbgLabelRecord -- print purely as
4927+
// a debugging aid.
4928+
formatted_raw_ostream OS(ROS);
4929+
SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4930+
SlotTracker &SlotTable =
4931+
MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4932+
auto incorporateFunction = [&](const Function *F) {
4933+
if (F)
4934+
MST.incorporateFunction(*F);
4935+
};
4936+
incorporateFunction(Marker->getParent() ? Marker->getParent()->getParent()
4937+
: nullptr);
4938+
AssemblyWriter W(OS, SlotTable, getModuleFromDPI(this), nullptr, IsForDebug);
4939+
W.printDPLabel(*this);
4940+
}
4941+
49074942
void Value::print(raw_ostream &ROS, bool IsForDebug) const {
49084943
bool ShouldInitializeAllMetadata = false;
49094944
if (auto *I = dyn_cast<Instruction>(this))

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void DbgRecord::deleteRecord() {
6464
case ValueKind:
6565
delete cast<DPValue>(this);
6666
return;
67+
case LabelKind:
68+
delete cast<DPLabel>(this);
69+
return;
6770
}
6871
llvm_unreachable("unsupported DbgRecord kind");
6972
}
@@ -73,6 +76,9 @@ void DbgRecord::print(raw_ostream &O, bool IsForDebug) const {
7376
case ValueKind:
7477
cast<DPValue>(this)->print(O, IsForDebug);
7578
return;
79+
case LabelKind:
80+
cast<DPLabel>(this)->print(O, IsForDebug);
81+
return;
7682
};
7783
llvm_unreachable("unsupported DbgRecord kind");
7884
}
@@ -83,6 +89,9 @@ void DbgRecord::print(raw_ostream &O, ModuleSlotTracker &MST,
8389
case ValueKind:
8490
cast<DPValue>(this)->print(O, MST, IsForDebug);
8591
return;
92+
case LabelKind:
93+
cast<DPLabel>(this)->print(O, MST, IsForDebug);
94+
return;
8695
};
8796
llvm_unreachable("unsupported DbgRecord kind");
8897
}
@@ -93,18 +102,14 @@ bool DbgRecord::isIdenticalToWhenDefined(const DbgRecord &R) const {
93102
switch (RecordKind) {
94103
case ValueKind:
95104
return cast<DPValue>(this)->isIdenticalToWhenDefined(*cast<DPValue>(&R));
105+
case LabelKind:
106+
return cast<DPLabel>(this)->getLabel() == cast<DPLabel>(R).getLabel();
96107
};
97108
llvm_unreachable("unsupported DbgRecord kind");
98109
}
99110

100111
bool DbgRecord::isEquivalentTo(const DbgRecord &R) const {
101-
if (RecordKind != R.RecordKind)
102-
return false;
103-
switch (RecordKind) {
104-
case ValueKind:
105-
return cast<DPValue>(this)->isEquivalentTo(*cast<DPValue>(&R));
106-
};
107-
llvm_unreachable("unsupported DbgRecord kind");
112+
return getDebugLoc() == R.getDebugLoc() && isIdenticalToWhenDefined(R);
108113
}
109114

110115
DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
@@ -307,12 +312,16 @@ DbgRecord *DbgRecord::clone() const {
307312
switch (RecordKind) {
308313
case ValueKind:
309314
return cast<DPValue>(this)->clone();
315+
case LabelKind:
316+
return cast<DPLabel>(this)->clone();
310317
};
311318
llvm_unreachable("unsupported DbgRecord kind");
312319
}
313320

314321
DPValue *DPValue::clone() const { return new DPValue(*this); }
315322

323+
DPLabel *DPLabel::clone() const { return new DPLabel(Label, getDebugLoc()); }
324+
316325
DbgVariableIntrinsic *
317326
DPValue::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const {
318327
[[maybe_unused]] DICompileUnit *Unit =

0 commit comments

Comments
 (0)