Skip to content

Commit 35e621f

Browse files
committed
[NFC][AsmPrinter] Expose std::variant-ness of DbgVariable
Differential Revision: https://reviews.llvm.org/D158677
1 parent 414ceff commit 35e621f

File tree

6 files changed

+86
-151
lines changed

6 files changed

+86
-151
lines changed

llvm/lib/CodeGen/AsmPrinter/DebugLocStream.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,5 @@ void DebugLocStream::finalizeEntry() {
4040
DebugLocStream::ListBuilder::~ListBuilder() {
4141
if (!Locs.finalizeList(Asm))
4242
return;
43-
V.initializeDbgValue(&MI);
44-
V.setDebugLocListIndex(ListIndex);
45-
if (TagOffset)
46-
V.setDebugLocListTagOffset(*TagOffset);
43+
V.emplace<Loc::Multi>(ListIndex, TagOffset);
4744
}

llvm/lib/CodeGen/AsmPrinter/DebugLocStream.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,13 @@ class DebugLocStream::ListBuilder {
156156
DebugLocStream &Locs;
157157
AsmPrinter &Asm;
158158
DbgVariable &V;
159-
const MachineInstr &MI;
160159
size_t ListIndex;
161160
std::optional<uint8_t> TagOffset;
162161

163162
public:
164163
ListBuilder(DebugLocStream &Locs, DwarfCompileUnit &CU, AsmPrinter &Asm,
165-
DbgVariable &V, const MachineInstr &MI)
166-
: Locs(Locs), Asm(Asm), V(V), MI(MI), ListIndex(Locs.startList(&CU)),
164+
DbgVariable &V)
165+
: Locs(Locs), Asm(Asm), V(V), ListIndex(Locs.startList(&CU)),
167166
TagOffset(std::nullopt) {}
168167

169168
void setTagOffset(uint8_t TO) {

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -772,24 +772,25 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
772772

773773
// Add variable address.
774774

775-
unsigned Index = DV.getDebugLocListIndex();
776-
if (Index != ~0U) {
777-
addLocationList(*VariableDie, dwarf::DW_AT_location, Index);
778-
auto TagOffset = DV.getDebugLocListTagOffset();
775+
if (auto *Multi = std::get_if<Loc::Multi>(&DV)) {
776+
addLocationList(*VariableDie, dwarf::DW_AT_location,
777+
Multi->getDebugLocListIndex());
778+
auto TagOffset = Multi->getDebugLocListTagOffset();
779779
if (TagOffset)
780780
addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
781781
*TagOffset);
782782
return VariableDie;
783783
}
784784

785785
// Check if variable has a single location description.
786-
if (auto *DVal = DV.getValueLoc()) {
786+
if (const auto *Single = std::get_if<Loc::Single>(&DV)) {
787+
const DbgValueLoc *DVal = &Single->getValueLoc();
787788
if (!DVal->isVariadic()) {
788789
const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
789790
if (Entry->isLocation()) {
790791
addVariableAddress(DV, *VariableDie, Entry->getLoc());
791792
} else if (Entry->isInt()) {
792-
auto *Expr = DV.getSingleExpression();
793+
auto *Expr = Single->getExpr();
793794
if (Expr && Expr->getNumElements()) {
794795
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
795796
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
@@ -823,7 +824,7 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
823824
return Entry.isLocation() && !Entry.getLoc().getReg();
824825
}))
825826
return VariableDie;
826-
const DIExpression *Expr = DV.getSingleExpression();
827+
const DIExpression *Expr = Single->getExpr();
827828
assert(Expr && "Variadic Debug Value must have an Expression.");
828829
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
829830
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
@@ -882,11 +883,11 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
882883
return VariableDie;
883884
}
884885

885-
if (const std::set<EntryValueInfo> *EntryValues = DV.getEntryValues()) {
886+
if (auto *EntryValue = std::get_if<Loc::EntryValue>(&DV)) {
886887
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
887888
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
888889
// Emit each expression as: EntryValue(Register) <other ops> <Fragment>.
889-
for (auto [Register, Expr] : *EntryValues) {
890+
for (auto [Register, Expr] : EntryValue->EntryValues) {
890891
DwarfExpr.addFragmentOffset(&Expr);
891892
DIExpressionCursor Cursor(Expr.getElements());
892893
DwarfExpr.beginEntryValueExpression(Cursor);
@@ -899,13 +900,13 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
899900
}
900901

901902
// .. else use frame index.
902-
if (!DV.hasFrameIndexExprs())
903+
if (!DV.holds<Loc::MMI>())
903904
return VariableDie;
904905

905906
std::optional<unsigned> NVPTXAddressSpace;
906907
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
907908
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
908-
for (const auto &Fragment : DV.getFrameIndexExprs()) {
909+
for (const auto &Fragment : DV.get<Loc::MMI>().getFrameIndexExprs()) {
909910
Register FrameReg;
910911
const DIExpression *Expr = Fragment.Expr;
911912
const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
@@ -1530,8 +1531,9 @@ void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty,
15301531

15311532
void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
15321533
MachineLocation Location) {
1533-
if (DV.hasComplexAddress())
1534-
addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
1534+
auto *Single = std::get_if<Loc::Single>(&DV);
1535+
if (Single && Single->getExpr())
1536+
addComplexAddress(Single->getExpr(), Die, dwarf::DW_AT_location, Location);
15351537
else
15361538
addAddress(Die, dwarf::DW_AT_location, Location);
15371539
}
@@ -1562,12 +1564,11 @@ void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
15621564
/// DWARF information necessary to find the actual variable given the extra
15631565
/// address information encoded in the DbgVariable, starting from the starting
15641566
/// location. Add the DWARF information to the die.
1565-
void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
1567+
void DwarfCompileUnit::addComplexAddress(const DIExpression *DIExpr, DIE &Die,
15661568
dwarf::Attribute Attribute,
15671569
const MachineLocation &Location) {
15681570
DIELoc *Loc = new (DIEValueAllocator) DIELoc;
15691571
DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1570-
const DIExpression *DIExpr = DV.getSingleExpression();
15711572
DwarfExpr.addFragmentOffset(DIExpr);
15721573
DwarfExpr.setLocation(Location, DIExpr);
15731574

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class DwarfCompileUnit final : public DwarfUnit {
341341
/// DWARF information necessary to find the actual variable (navigating the
342342
/// extra location information encoded in the type) based on the starting
343343
/// location. Add the DWARF information to the die.
344-
void addComplexAddress(const DbgVariable &DV, DIE &Die,
344+
void addComplexAddress(const DIExpression *DIExpr, DIE &Die,
345345
dwarf::Attribute Attribute,
346346
const MachineLocation &Location);
347347

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -257,44 +257,23 @@ static DbgValueLoc getDebugLocValue(const MachineInstr *MI) {
257257
return DbgValueLoc(Expr, DbgValueLocEntries, IsVariadic);
258258
}
259259

260-
/// Initialize from the MMI table.
261-
void DbgVariable::initializeMMI(const DIExpression *E, int FI) {
262-
assert(holds<std::monostate>() && "Already initialized");
263-
assert((!E || E->isValid()) && "Expected valid expression");
264-
assert(FI != std::numeric_limits<int>::max() && "Expected valid index");
265-
emplace<MMILoc>().FrameIndexExprs.push_back({FI, E});
260+
Loc::Single::Single(DbgValueLoc ValueLoc)
261+
: ValueLoc(std::make_unique<DbgValueLoc>(ValueLoc)),
262+
Expr(ValueLoc.getExpression()) {
263+
if (!Expr->getNumElements())
264+
Expr = nullptr;
266265
}
267266

268-
void DbgVariable::initializeDbgValue(DbgValueLoc Value) {
269-
assert(holds<std::monostate>() && "Already initialized");
270-
assert(!Value.getExpression()->isFragment() && "Fragments not supported");
271-
emplace<SingleLoc>(Value);
272-
}
273-
274-
void DbgVariable::initializeDbgValue(const MachineInstr *DbgValue) {
275-
assert(holds<std::monostate>() && "Already initialized");
276-
assert(getVariable() == DbgValue->getDebugVariable() && "Wrong variable");
277-
assert(getInlinedAt() == DbgValue->getDebugLoc()->getInlinedAt() &&
278-
"Wrong inlined-at");
279-
emplace<SingleLoc>(getDebugLocValue(DbgValue));
280-
}
281-
282-
void DbgVariable::initializeEntryValue(MCRegister Reg,
283-
const DIExpression &Expr) {
284-
assert(holds<std::monostate>() && "Already initialized?");
285-
emplace<EntryValueLoc>(Reg, Expr);
286-
}
287-
288-
ArrayRef<FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
289-
auto &FrameIndexExprs = get<MMILoc>().FrameIndexExprs;
267+
Loc::Single::Single(const MachineInstr *DbgValue)
268+
: Single(getDebugLocValue(DbgValue)) {}
290269

270+
ArrayRef<FrameIndexExpr> Loc::MMI::getFrameIndexExprs() const {
291271
if (FrameIndexExprs.size() == 1)
292272
return FrameIndexExprs;
293273

294-
assert(llvm::all_of(FrameIndexExprs,
295-
[](const FrameIndexExpr &A) {
296-
return A.Expr->isFragment();
297-
}) &&
274+
assert(llvm::all_of(
275+
FrameIndexExprs,
276+
[](const FrameIndexExpr &A) { return A.Expr->isFragment(); }) &&
298277
"multiple FI expressions without DW_OP_LLVM_fragment");
299278
llvm::sort(FrameIndexExprs,
300279
[](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
@@ -305,9 +284,7 @@ ArrayRef<FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
305284
return FrameIndexExprs;
306285
}
307286

308-
void DbgVariable::addMMIEntry(const DIExpression *Expr, int FI) {
309-
auto &FrameIndexExprs = get<MMILoc>().FrameIndexExprs;
310-
287+
void Loc::MMI::addFrameIndexExpr(const DIExpression *Expr, int FI) {
311288
// FIXME: This logic should not be necessary anymore, as we now have proper
312289
// deduplication. However, without it, we currently run into the assertion
313290
// below, which means that we are likely dealing with broken input, i.e. two
@@ -1570,19 +1547,20 @@ void DwarfDebug::collectVariableInfoFromMFTable(
15701547
ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode());
15711548

15721549
if (DbgVariable *DbgVar = MFVars.lookup(Var)) {
1573-
if (DbgVar->hasFrameIndexExprs())
1574-
DbgVar->addMMIEntry(VI.Expr, VI.getStackSlot());
1550+
if (auto *MMI = std::get_if<Loc::MMI>(DbgVar))
1551+
MMI->addFrameIndexExpr(VI.Expr, VI.getStackSlot());
15751552
else
1576-
DbgVar->addEntryValueExpr(VI.getEntryValueRegister(), *VI.Expr);
1553+
DbgVar->get<Loc::EntryValue>().addExpr(VI.getEntryValueRegister(),
1554+
*VI.Expr);
15771555
continue;
15781556
}
15791557

15801558
auto RegVar = std::make_unique<DbgVariable>(
15811559
cast<DILocalVariable>(Var.first), Var.second);
15821560
if (VI.inStackSlot())
1583-
RegVar->initializeMMI(VI.Expr, VI.getStackSlot());
1561+
RegVar->emplace<Loc::MMI>(VI.Expr, VI.getStackSlot());
15841562
else
1585-
RegVar->initializeEntryValue(VI.getEntryValueRegister(), *VI.Expr);
1563+
RegVar->emplace<Loc::EntryValue>(VI.getEntryValueRegister(), *VI.Expr);
15861564
LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName()
15871565
<< "\n");
15881566
InfoHolder.addScopeVariable(Scope, RegVar.get());
@@ -1921,7 +1899,7 @@ void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
19211899
const auto *End =
19221900
SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr;
19231901
if (validThroughout(LScopes, MInsn, End, getInstOrdering())) {
1924-
RegVar->initializeDbgValue(MInsn);
1902+
RegVar->emplace<Loc::Single>(MInsn);
19251903
continue;
19261904
}
19271905
}
@@ -1931,7 +1909,7 @@ void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
19311909
continue;
19321910

19331911
// Handle multiple DBG_VALUE instructions describing one variable.
1934-
DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1912+
DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar);
19351913

19361914
// Build the location list for this variable.
19371915
SmallVector<DebugLocEntry, 8> Entries;
@@ -1941,7 +1919,7 @@ void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
19411919
// that is valid throughout the variable's scope. If so, produce single
19421920
// value location.
19431921
if (isValidSingleLocation) {
1944-
RegVar->initializeDbgValue(Entries[0].getValues()[0]);
1922+
RegVar->emplace<Loc::Single>(Entries[0].getValues()[0]);
19451923
continue;
19461924
}
19471925

0 commit comments

Comments
 (0)