Skip to content

Commit a7674f1

Browse files
committed
Add DPAssign variant of DPValues
1 parent 3ac9fe6 commit a7674f1

File tree

5 files changed

+263
-67
lines changed

5 files changed

+263
-67
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class BasicBlock;
6161
class MDNode;
6262
class Module;
6363
class DbgVariableIntrinsic;
64+
class DIAssignID;
6465
class DPMarker;
6566
class DPValue;
6667
class raw_ostream;
@@ -83,6 +84,7 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
8384
DILocalVariable *Variable;
8485
DIExpression *Expression;
8586
DebugLoc DbgLoc;
87+
DIExpression *AddressExpression;
8688

8789
public:
8890
void deleteInstr();
@@ -102,6 +104,7 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
102104
enum class LocationType {
103105
Declare,
104106
Value,
107+
Assign,
105108

106109
End, ///< Marks the end of the concrete types.
107110
Any, ///< To indicate all LocationTypes in searches.
@@ -122,6 +125,22 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
122125
/// assigning \p Location to the DV / Expr / DI variable.
123126
DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,
124127
const DILocation *DI, LocationType Type = LocationType::Value);
128+
DPValue(Metadata *Value, DILocalVariable *Variable, DIExpression *Expression,
129+
DIAssignID *AssignID, Metadata *Address,
130+
DIExpression *AddressExpression, const DILocation *DI);
131+
132+
static DPValue *createDPVAssign(Metadata *Value, DILocalVariable *Variable,
133+
DIExpression *Expression,
134+
DIAssignID *AssignID, Metadata *Address,
135+
DIExpression *AddressExpression,
136+
const DILocation *DI,
137+
Instruction *InsertBefore = nullptr);
138+
static DPValue *createLinkedDPVAssign(Instruction *LinkedInstr, Value *Val,
139+
DILocalVariable *Variable,
140+
DIExpression *Expression,
141+
Value *Address,
142+
DIExpression *AddressExpression,
143+
const DILocation *DI);
125144

126145
static DPValue *createDPValue(Value *Location, DILocalVariable *DV,
127146
DIExpression *Expr, const DILocation *DI);
@@ -213,7 +232,7 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
213232

214233
/// Does this describe the address of a local variable. True for dbg.addr
215234
/// and dbg.declare, but not dbg.value, which describes its value.
216-
bool isAddressOfVariable() const { return Type != LocationType::Value; }
235+
bool isAddressOfVariable() const { return Type == LocationType::Declare; }
217236
LocationType getType() const { return Type; }
218237

219238
DebugLoc getDebugLoc() const { return DbgLoc; }
@@ -226,7 +245,11 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
226245

227246
DIExpression *getExpression() const { return Expression; }
228247

229-
Metadata *getRawLocation() const { return DebugValue; }
248+
/// Returns the metadata operand for the first location description. i.e.,
249+
/// dbg intrinsic dbg.value,declare operand and dbg.assign 1st location
250+
/// operand (the "value componenet"). Note the operand (singular) may be
251+
/// a DIArgList which is a list of values.
252+
Metadata *getRawLocation() const { return DebugValues[0]; }
230253

231254
Value *getValue(unsigned OpIdx = 0) const {
232255
return getVariableLocationOp(OpIdx);
@@ -240,7 +263,7 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
240263
(isa<ValueAsMetadata>(NewLocation) || isa<DIArgList>(NewLocation) ||
241264
isa<MDNode>(NewLocation)) &&
242265
"Location for a DPValue must be either ValueAsMetadata or DIArgList");
243-
resetDebugValue(NewLocation);
266+
resetDebugValue(0, NewLocation);
244267
}
245268

246269
/// Get the size (in bits) of the variable, or fragment of the variable that
@@ -260,16 +283,38 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
260283
Other.Expression);
261284
}
262285

286+
/// @name DbgAssign Methods
287+
/// @{
288+
bool isDbgAssign() const { return getType() == LocationType::Assign; }
289+
290+
Value *getAddress() const;
291+
Metadata *getRawAddress() const {
292+
return isDbgAssign() ? DebugValues[1] : DebugValues[0];
293+
}
294+
Metadata *getRawAssignID() const { return DebugValues[2]; }
295+
DIAssignID *getAssignID() const;
296+
DIExpression *getAddressExpression() const { return AddressExpression; }
297+
void setAddressExpression(DIExpression *NewExpr) {
298+
AddressExpression = NewExpr;
299+
}
300+
void setAssignId(DIAssignID *New);
301+
void setAddress(Value *V) { resetDebugValue(1, ValueAsMetadata::get(V)); }
302+
/// Kill the address component.
303+
void setKillAddress();
304+
/// Check whether this kills the address component. This doesn't take into
305+
/// account the position of the intrinsic, therefore a returned value of false
306+
/// does not guarentee the address is a valid location for the variable at the
307+
/// intrinsic's position in IR.
308+
bool isKillAddress() const;
309+
310+
/// @}
311+
263312
DPValue *clone() const;
264313
/// Convert this DPValue back into a dbg.value intrinsic.
265314
/// \p InsertBefore Optional position to insert this intrinsic.
266315
/// \returns A new dbg.value intrinsic representiung this DPValue.
267316
DbgVariableIntrinsic *createDebugIntrinsic(Module *M,
268317
Instruction *InsertBefore) const;
269-
/// Handle changes to the location of the Value(s) that we refer to happening
270-
/// "under our feet".
271-
void handleChangedLocation(Metadata *NewLocation);
272-
273318
void setMarker(DPMarker *M) { Marker = M; }
274319

275320
DPMarker *getMarker() { return Marker; }

llvm/include/llvm/IR/Metadata.h

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -211,67 +211,81 @@ class MetadataAsValue : public Value {
211211
/// lookup and callback handling.
212212
class DebugValueUser {
213213
protected:
214-
Metadata *DebugValue;
214+
// Capacity to store 3 debug values.
215+
// TODO: Not all DebugValueUser instances need all 3 elements, if we
216+
// restructure the DPValue class then we can template parameterize this array
217+
// size.
218+
std::array<Metadata *, 3> DebugValues;
219+
220+
ArrayRef<Metadata *> getDebugValues() const { return DebugValues; }
215221

216222
public:
217223
DPValue *getUser();
218224
const DPValue *getUser() const;
219-
void handleChangedValue(Metadata *NewDebugValue);
225+
void handleChangedValue(void *Old, Metadata *NewDebugValue);
220226
DebugValueUser() = default;
221-
explicit DebugValueUser(Metadata *DebugValue) : DebugValue(DebugValue) {
222-
trackDebugValue();
227+
explicit DebugValueUser(std::array<Metadata *, 3> DebugValues)
228+
: DebugValues(DebugValues) {
229+
trackDebugValues();
223230
}
224-
225-
DebugValueUser(DebugValueUser &&X) : DebugValue(X.DebugValue) {
226-
retrackDebugValue(X);
231+
DebugValueUser(DebugValueUser &&X) {
232+
DebugValues = X.DebugValues;
233+
retrackDebugValues(X);
227234
}
228-
DebugValueUser(const DebugValueUser &X) : DebugValue(X.DebugValue) {
229-
trackDebugValue();
235+
DebugValueUser(const DebugValueUser &X) {
236+
DebugValues = X.DebugValues;
237+
trackDebugValues();
230238
}
231239

232240
DebugValueUser &operator=(DebugValueUser &&X) {
233241
if (&X == this)
234242
return *this;
235243

236-
untrackDebugValue();
237-
DebugValue = X.DebugValue;
238-
retrackDebugValue(X);
244+
untrackDebugValues();
245+
DebugValues = X.DebugValues;
246+
retrackDebugValues(X);
239247
return *this;
240248
}
241249

242250
DebugValueUser &operator=(const DebugValueUser &X) {
243251
if (&X == this)
244252
return *this;
245253

246-
untrackDebugValue();
247-
DebugValue = X.DebugValue;
248-
trackDebugValue();
254+
untrackDebugValues();
255+
DebugValues = X.DebugValues;
256+
trackDebugValues();
249257
return *this;
250258
}
251259

252-
~DebugValueUser() { untrackDebugValue(); }
260+
~DebugValueUser() { untrackDebugValues(); }
253261

254-
void resetDebugValue() {
255-
untrackDebugValue();
256-
DebugValue = nullptr;
262+
void resetDebugValues() {
263+
untrackDebugValues();
264+
DebugValues.fill(nullptr);
257265
}
258-
void resetDebugValue(Metadata *DebugValue) {
259-
untrackDebugValue();
260-
this->DebugValue = DebugValue;
261-
trackDebugValue();
266+
267+
void resetDebugValue(size_t Idx, Metadata *DebugValue) {
268+
assert(Idx < 3 && "Invalid debug value index.");
269+
untrackDebugValue(Idx);
270+
DebugValues[Idx] = DebugValue;
271+
trackDebugValue(Idx);
262272
}
263273

264274
bool operator==(const DebugValueUser &X) const {
265-
return DebugValue == X.DebugValue;
275+
return DebugValues == X.DebugValues;
266276
}
267277
bool operator!=(const DebugValueUser &X) const {
268-
return DebugValue != X.DebugValue;
278+
return DebugValues != X.DebugValues;
269279
}
270280

271281
private:
272-
void trackDebugValue();
273-
void untrackDebugValue();
274-
void retrackDebugValue(DebugValueUser &X);
282+
void trackDebugValue(size_t Idx);
283+
void trackDebugValues();
284+
285+
void untrackDebugValue(size_t Idx);
286+
void untrackDebugValues();
287+
288+
void retrackDebugValues(DebugValueUser &X);
275289
};
276290

277291
/// API for tracking metadata references through RAUW and deletion.

llvm/lib/IR/AsmWriter.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,9 @@ void SlotTracker::processFunctionMetadata(const Function &F) {
11401140
void SlotTracker::processDPValueMetadata(const DPValue &DPV) {
11411141
CreateMetadataSlot(DPV.getVariable());
11421142
CreateMetadataSlot(DPV.getDebugLoc());
1143+
if (DPV.isDbgAssign()) {
1144+
CreateMetadataSlot(DPV.getAssignID());
1145+
}
11431146
}
11441147

11451148
void SlotTracker::processInstructionMetadata(const Instruction &I) {
@@ -4571,14 +4574,37 @@ void AssemblyWriter::printDPMarker(const DPMarker &Marker) {
45714574
void AssemblyWriter::printDPValue(const DPValue &Value) {
45724575
// There's no formal representation of a DPValue -- print purely as a
45734576
// debugging aid.
4574-
Out << " DPValue { ";
4577+
Out << " DPValue ";
4578+
4579+
switch (Value.getType()) {
4580+
case DPValue::LocationType::Value:
4581+
Out << "value";
4582+
break;
4583+
case DPValue::LocationType::Declare:
4584+
Out << "declare";
4585+
break;
4586+
case DPValue::LocationType::Assign:
4587+
Out << "assign";
4588+
break;
4589+
default:
4590+
llvm_unreachable("Tried to print a DPValue with an invalid LocationType!");
4591+
}
4592+
Out << " { ";
45754593
auto WriterCtx = getContext();
45764594
WriteAsOperandInternal(Out, Value.getRawLocation(), WriterCtx, true);
45774595
Out << ", ";
45784596
WriteAsOperandInternal(Out, Value.getVariable(), WriterCtx, true);
45794597
Out << ", ";
45804598
WriteAsOperandInternal(Out, Value.getExpression(), WriterCtx, true);
45814599
Out << ", ";
4600+
if (Value.isDbgAssign()) {
4601+
WriteAsOperandInternal(Out, Value.getAssignID(), WriterCtx, true);
4602+
Out << ", ";
4603+
WriteAsOperandInternal(Out, Value.getRawAddress(), WriterCtx, true);
4604+
Out << ", ";
4605+
WriteAsOperandInternal(Out, Value.getAddressExpression(), WriterCtx, true);
4606+
Out << ", ";
4607+
}
45824608
WriteAsOperandInternal(Out, Value.getDebugLoc().get(), WriterCtx, true);
45834609
Out << " marker @" << Value.getMarker();
45844610
Out << " }";

0 commit comments

Comments
 (0)