Skip to content

Commit 7d89437

Browse files
committed
[Assignment Tracking][NFC] Use RawLocationWrapper in VarLocInfo [2/x]
Use RawLocationWrapper rather than a Value to represent the location operand(s) so that it's possible to represent multiple location operands. AssignmentTrackingAnalysis still converts variadic debug intrinsics to kill locations so this patch is NFC. Reviewed By: StephenTozer Differential Revision: https://reviews.llvm.org/D145911
1 parent af9e522 commit 7d89437

File tree

5 files changed

+79
-44
lines changed

5 files changed

+79
-44
lines changed

llvm/include/llvm/CodeGen/AssignmentTrackingAnalysis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "llvm/IR/DebugInfoMetadata.h"
55
#include "llvm/IR/DebugLoc.h"
6+
#include "llvm/IR/IntrinsicInst.h"
67
#include "llvm/Pass.h"
78

89
namespace llvm {
@@ -21,7 +22,7 @@ struct VarLocInfo {
2122
llvm::VariableID VariableID;
2223
DIExpression *Expr = nullptr;
2324
DebugLoc DL;
24-
Value *V = nullptr; // TODO: Needs to be value_s_ for variadic expressions.
25+
RawLocationWrapper Values = RawLocationWrapper();
2526
};
2627

2728
/// Data structure describing the variable locations in a function. Used as the

llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,23 @@ class FunctionVarLocsBuilder {
105105

106106
/// Add a def for a variable that is valid for its lifetime.
107107
void addSingleLocVar(DebugVariable Var, DIExpression *Expr, DebugLoc DL,
108-
Value *V) {
108+
RawLocationWrapper R) {
109109
VarLocInfo VarLoc;
110110
VarLoc.VariableID = insertVariable(Var);
111111
VarLoc.Expr = Expr;
112112
VarLoc.DL = DL;
113-
VarLoc.V = V;
113+
VarLoc.Values = R;
114114
SingleLocVars.emplace_back(VarLoc);
115115
}
116116

117117
/// Add a def to the wedge of defs just before /p Before.
118118
void addVarLoc(Instruction *Before, DebugVariable Var, DIExpression *Expr,
119-
DebugLoc DL, Value *V) {
119+
DebugLoc DL, RawLocationWrapper R) {
120120
VarLocInfo VarLoc;
121121
VarLoc.VariableID = insertVariable(Var);
122122
VarLoc.Expr = Expr;
123123
VarLoc.DL = DL;
124-
VarLoc.V = V;
124+
VarLoc.Values = R;
125125
VarLocsBeforeInst[Before].emplace_back(VarLoc);
126126
}
127127
};
@@ -148,7 +148,11 @@ void FunctionVarLocs::print(raw_ostream &OS, const Function &Fn) const {
148148

149149
auto PrintLoc = [&OS](const VarLocInfo &Loc) {
150150
OS << "DEF Var=[" << (unsigned)Loc.VariableID << "]"
151-
<< " Expr=" << *Loc.Expr << " V=" << *Loc.V << "\n";
151+
<< " Expr=" << *Loc.Expr << " Values=(";
152+
for (auto *Op : Loc.Values.location_ops()) {
153+
errs() << Op->getName() << " ";
154+
}
155+
errs() << ")\n";
152156
};
153157

154158
// Print the single location variables.
@@ -317,7 +321,7 @@ class MemLocFragmentFill {
317321

318322
/// IDs for memory location base addresses in maps. Use 0 to indicate that
319323
/// there's no memory location.
320-
UniqueVector<Value *> Bases;
324+
UniqueVector<RawLocationWrapper> Bases;
321325
UniqueVector<DebugAggregate> Aggregates;
322326
DenseMap<const BasicBlock *, VarFragMap> LiveIn;
323327
DenseMap<const BasicBlock *, VarFragMap> LiveOut;
@@ -370,7 +374,7 @@ class MemLocFragmentFill {
370374
/// Return a string for the value that \p BaseID represents.
371375
std::string toString(unsigned BaseID) {
372376
if (BaseID)
373-
return Bases[BaseID]->getName().str();
377+
return Bases[BaseID].getVariableLocationOp(0)->getName().str();
374378
else
375379
return "None";
376380
}
@@ -603,7 +607,7 @@ class MemLocFragmentFill {
603607
const auto DerefOffsetInBytes = getDerefOffsetInBytes(DIExpr);
604608
const unsigned Base =
605609
DerefOffsetInBytes && *DerefOffsetInBytes * 8 == StartBit
606-
? Bases.insert(VarLoc.V)
610+
? Bases.insert(VarLoc.Values)
607611
: 0;
608612
LLVM_DEBUG(dbgs() << "DEF " << DbgVar.getVariable()->getName() << " ["
609613
<< StartBit << ", " << EndBit << "): " << toString(Base)
@@ -1244,10 +1248,11 @@ void AssignmentTrackingLowering::emitDbgValue(
12441248
const DbgVariableIntrinsic *Source, Instruction *After) {
12451249

12461250
DILocation *DL = Source->getDebugLoc();
1247-
auto Emit = [this, Source, After, DL](Value *Val, DIExpression *Expr) {
1251+
auto Emit = [this, Source, After, DL](Metadata *Val, DIExpression *Expr) {
12481252
assert(Expr);
12491253
if (!Val)
1250-
Val = PoisonValue::get(Type::getInt1Ty(Source->getContext()));
1254+
Val = ValueAsMetadata::get(
1255+
PoisonValue::get(Type::getInt1Ty(Source->getContext())));
12511256

12521257
// Find a suitable insert point.
12531258
Instruction *InsertBefore = After->getNextNode();
@@ -1257,7 +1262,7 @@ void AssignmentTrackingLowering::emitDbgValue(
12571262
VarLocInfo VarLoc;
12581263
VarLoc.VariableID = static_cast<VariableID>(Var);
12591264
VarLoc.Expr = Expr;
1260-
VarLoc.V = Val;
1265+
VarLoc.Values = RawLocationWrapper(Val);
12611266
VarLoc.DL = DL;
12621267
// Insert it into the map for later.
12631268
InsertBeforeMap[InsertBefore].push_back(VarLoc);
@@ -1286,7 +1291,7 @@ void AssignmentTrackingLowering::emitDbgValue(
12861291
// The address-expression has an implicit deref, add it now.
12871292
std::tie(Val, Expr) =
12881293
walkToAllocaAndPrependOffsetDeref(Layout, Val, Expr);
1289-
Emit(Val, Expr);
1294+
Emit(ValueAsMetadata::get(Val), Expr);
12901295
return;
12911296
}
12921297
}
@@ -1295,7 +1300,7 @@ void AssignmentTrackingLowering::emitDbgValue(
12951300
/// Get the value component, converting to Undef if it is variadic.
12961301
Value *Val =
12971302
Source->hasArgList() ? nullptr : Source->getVariableLocationOp(0);
1298-
Emit(Val, Source->getExpression());
1303+
Emit(ValueAsMetadata::get(Val), Source->getExpression());
12991304
return;
13001305
}
13011306

@@ -1373,7 +1378,8 @@ void AssignmentTrackingLowering::processUntaggedInstruction(
13731378
VarLocInfo VarLoc;
13741379
VarLoc.VariableID = static_cast<VariableID>(Var);
13751380
VarLoc.Expr = DIE;
1376-
VarLoc.V = const_cast<AllocaInst *>(Info.Base);
1381+
VarLoc.Values = RawLocationWrapper(
1382+
ValueAsMetadata::get(const_cast<AllocaInst *>(Info.Base)));
13771383
VarLoc.DL = DILoc;
13781384
// 3. Insert it into the map for later.
13791385
InsertBeforeMap[InsertBefore].push_back(VarLoc);
@@ -1856,7 +1862,8 @@ static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares(
18561862
for (auto &I : BB) {
18571863
if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) {
18581864
FnVarLocs->addSingleLocVar(DebugVariable(DDI), DDI->getExpression(),
1859-
DDI->getDebugLoc(), DDI->getAddress());
1865+
DDI->getDebugLoc(),
1866+
DDI->getWrappedLocation());
18601867
} else if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
18611868
DebugVariable DV = DebugVariable(DII);
18621869
DebugAggregate DA = {DV.getVariable(), DV.getInlinedAt()};
@@ -2068,14 +2075,15 @@ bool AssignmentTrackingLowering::run(FunctionVarLocsBuilder *FnVarLocsBuilder) {
20682075
//
20692076
// Unless we've already done so, create the single location def now.
20702077
if (AlwaysStackHomed.insert(Aggr).second) {
2071-
assert(isa<AllocaInst>(VarLoc.V));
2078+
assert(!VarLoc.Values.hasArgList() &&
2079+
isa<AllocaInst>(VarLoc.Values.getVariableLocationOp(0)));
20722080
// TODO: When more complex cases are handled VarLoc.Expr should be
20732081
// built appropriately rather than always using an empty DIExpression.
20742082
// The assert below is a reminder.
20752083
assert(Simple);
20762084
VarLoc.Expr = DIExpression::get(Fn.getContext(), std::nullopt);
20772085
DebugVariable Var = FnVarLocs->getVariable(VarLoc.VariableID);
2078-
FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.V);
2086+
FnVarLocs->addSingleLocVar(Var, VarLoc.Expr, VarLoc.DL, VarLoc.Values);
20792087
InsertedAnyIntrinsics = true;
20802088
}
20812089
}
@@ -2119,13 +2127,16 @@ bool AssignmentTrackingLowering::emitPromotedVarLocs(
21192127
if (VarsWithStackSlot->contains(getAggregate(DVI)))
21202128
continue;
21212129
// Wrapper to get a single value (or undef) from DVI.
2122-
auto GetValue = [DVI]() -> Value * {
2130+
auto GetValue = [DVI]() -> RawLocationWrapper {
21232131
// We can't handle variadic DIExpressions yet so treat those as
21242132
// kill locations.
2133+
Value *V;
21252134
if (DVI->isKillLocation() || DVI->getValue() == nullptr ||
21262135
DVI->hasArgList())
2127-
return PoisonValue::get(Type::getInt32Ty(DVI->getContext()));
2128-
return DVI->getValue();
2136+
V = PoisonValue::get(Type::getInt32Ty(DVI->getContext()));
2137+
else
2138+
V = DVI->getVariableLocationOp(0);
2139+
return RawLocationWrapper(ValueAsMetadata::get(V));
21292140
};
21302141
Instruction *InsertBefore = I.getNextNode();
21312142
assert(InsertBefore && "Unexpected: debug intrinsics after a terminator");
@@ -2213,7 +2224,8 @@ static bool
22132224
removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
22142225
FunctionVarLocsBuilder &FnVarLocs) {
22152226
bool Changed = false;
2216-
DenseMap<DebugVariable, std::pair<Value *, DIExpression *>> VariableMap;
2227+
DenseMap<DebugVariable, std::pair<RawLocationWrapper, DIExpression *>>
2228+
VariableMap;
22172229

22182230
// Scan over the entire block, not just over the instructions mapped by
22192231
// FnVarLocs, because wedges in FnVarLocs may only be seperated by debug
@@ -2238,9 +2250,9 @@ removeRedundantDbgLocsUsingForwardScan(const BasicBlock *BB,
22382250

22392251
// Update the map if we found a new value/expression describing the
22402252
// variable, or if the variable wasn't mapped already.
2241-
if (VMI == VariableMap.end() || VMI->second.first != Loc.V ||
2253+
if (VMI == VariableMap.end() || VMI->second.first != Loc.Values ||
22422254
VMI->second.second != Loc.Expr) {
2243-
VariableMap[Key] = {Loc.V, Loc.Expr};
2255+
VariableMap[Key] = {Loc.Values, Loc.Expr};
22442256
NewDefs.push_back(Loc);
22452257
continue;
22462258
}
@@ -2320,7 +2332,7 @@ removeUndefDbgLocsFromEntryBlock(const BasicBlock *BB,
23202332

23212333
// Remove undef entries that are encountered before any non-undef
23222334
// intrinsics from the entry block.
2323-
if (isa<UndefValue>(Loc.V) && !HasDefinedBits(Aggr, Var)) {
2335+
if (Loc.Values.isKillLocation(Loc.Expr) && !HasDefinedBits(Aggr, Var)) {
23242336
// Did not insert this Loc, which is the same as removing it.
23252337
NumDefsRemoved++;
23262338
ChangedThisWedge = true;

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,9 @@ void SelectionDAGBuilder::visit(const Instruction &I) {
11451145
It != End; ++It) {
11461146
auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
11471147
dropDanglingDebugInfo(Var, It->Expr);
1148-
if (!handleDebugValue(It->V, Var, It->Expr, It->DL, SDNodeOrder,
1149-
/*IsVariadic=*/false))
1148+
SmallVector<Value *> Values(It->Values.location_ops());
1149+
if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1150+
It->Values.hasArgList()))
11501151
addDanglingDebugInfo(It, SDNodeOrder);
11511152
}
11521153
}
@@ -1206,27 +1207,46 @@ void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
12061207
}
12071208
}
12081209

1210+
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG,
1211+
DILocalVariable *Variable,
1212+
DebugLoc DL, unsigned Order,
1213+
RawLocationWrapper Values,
1214+
DIExpression *Expression) {
1215+
if (!Values.hasArgList())
1216+
return false;
1217+
// For variadic dbg_values we will now insert an undef.
1218+
// FIXME: We can potentially recover these!
1219+
SmallVector<SDDbgOperand, 2> Locs;
1220+
for (const Value *V : Values.location_ops()) {
1221+
auto *Undef = UndefValue::get(V->getType());
1222+
Locs.push_back(SDDbgOperand::fromConst(Undef));
1223+
}
1224+
SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1225+
/*IsIndirect=*/false, DL, Order,
1226+
/*IsVariadic=*/true);
1227+
DAG.AddDbgValue(SDV, /*isParameter=*/false);
1228+
return true;
1229+
}
1230+
12091231
void SelectionDAGBuilder::addDanglingDebugInfo(const VarLocInfo *VarLoc,
12101232
unsigned Order) {
1211-
DanglingDebugInfoMap[VarLoc->V].emplace_back(VarLoc, Order);
1233+
if (!handleDanglingVariadicDebugInfo(
1234+
DAG,
1235+
const_cast<DILocalVariable *>(DAG.getFunctionVarLocs()
1236+
->getVariable(VarLoc->VariableID)
1237+
.getVariable()),
1238+
VarLoc->DL, Order, VarLoc->Values, VarLoc->Expr)) {
1239+
DanglingDebugInfoMap[VarLoc->Values.getVariableLocationOp(0)].emplace_back(
1240+
VarLoc, Order);
1241+
}
12121242
}
12131243

12141244
void SelectionDAGBuilder::addDanglingDebugInfo(const DbgValueInst *DI,
12151245
unsigned Order) {
12161246
// We treat variadic dbg_values differently at this stage.
1217-
if (DI->hasArgList()) {
1218-
// For variadic dbg_values we will now insert an undef.
1219-
// FIXME: We can potentially recover these!
1220-
SmallVector<SDDbgOperand, 2> Locs;
1221-
for (const Value *V : DI->getValues()) {
1222-
auto Undef = UndefValue::get(V->getType());
1223-
Locs.push_back(SDDbgOperand::fromConst(Undef));
1224-
}
1225-
SDDbgValue *SDV = DAG.getDbgValueList(
1226-
DI->getVariable(), DI->getExpression(), Locs, {},
1227-
/*IsIndirect=*/false, DI->getDebugLoc(), Order, /*IsVariadic=*/true);
1228-
DAG.AddDbgValue(SDV, /*isParameter=*/false);
1229-
} else {
1247+
if (!handleDanglingVariadicDebugInfo(
1248+
DAG, DI->getVariable(), DI->getDebugLoc(), Order,
1249+
DI->getWrappedLocation(), DI->getExpression())) {
12301250
// TODO: Dangling debug info will eventually either be resolved or produce
12311251
// an Undef DBG_VALUE. However in the resolution case, a gap may appear
12321252
// between the original dbg.value location and its resolved DBG_VALUE,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class SelectionDAGBuilder {
131131
Value *getVariableLocationOp(unsigned Idx) const {
132132
assert(Idx == 0 && "Dangling variadic debug values not supported yet");
133133
if (Info.is<VarLocTy>())
134-
return Info.get<VarLocTy>()->V;
134+
return Info.get<VarLocTy>()->Values.getVariableLocationOp(Idx);
135135
return Info.get<DbgValTy>()->getVariableLocationOp(Idx);
136136
}
137137
DebugLoc getDebugLoc() const {

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,9 +1368,11 @@ static void processSingleLocVars(FunctionLoweringInfo &FuncInfo,
13681368
FunctionVarLocs const *FnVarLocs) {
13691369
for (auto It = FnVarLocs->single_locs_begin(),
13701370
End = FnVarLocs->single_locs_end();
1371-
It != End; ++It)
1372-
processDbgDeclare(FuncInfo, It->V, It->Expr,
1371+
It != End; ++It) {
1372+
assert(!It->Values.hasArgList() && "Single loc variadic ops not supported");
1373+
processDbgDeclare(FuncInfo, It->Values.getVariableLocationOp(0), It->Expr,
13731374
FnVarLocs->getDILocalVariable(It->VariableID), It->DL);
1375+
}
13741376
}
13751377

13761378
void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {

0 commit comments

Comments
 (0)