Skip to content

Commit dae90c3

Browse files
Add code to handle #dbg_values in SROA.
This patch adds a bunch of functions that will be used to properly handle debug information for #dbg_values in the SROA pass.
1 parent f6c1e65 commit dae90c3

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Module;
4343
TinyPtrVector<DbgDeclareInst *> findDbgDeclares(Value *V);
4444
/// As above, for DVRDeclares.
4545
TinyPtrVector<DbgVariableRecord *> findDVRDeclares(Value *V);
46+
/// As above, for DVRValues.
47+
TinyPtrVector<DbgVariableRecord *> findDVRValues(Value *V);
4648

4749
/// Finds the llvm.dbg.value intrinsics describing a value.
4850
void findDbgValues(

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
426426
/// Does this describe the address of a local variable. True for dbg.addr
427427
/// and dbg.declare, but not dbg.value, which describes its value.
428428
bool isAddressOfVariable() const { return Type == LocationType::Declare; }
429+
430+
/// Does this describe the value of a local variable. False for dbg.declare,
431+
/// but True for dbg.value, which describes its value.
432+
bool isValueOfVariable() const { return Type == LocationType::Value; }
433+
429434
LocationType getType() const { return Type; }
430435

431436
void setKillLocation();

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ class DbgVariableIntrinsic : public DbgInfoIntrinsic {
341341
return getIntrinsicID() == Intrinsic::dbg_declare;
342342
}
343343

344+
/// Does this describe the value of a local variable. True for dbg.value,
345+
/// but False for dbg.declare, which describes its address, and False for
346+
/// dbg.assign, which describes a combination of the variable's value and
347+
/// address.
348+
bool isValueOfVariable() const {
349+
return getIntrinsicID() == Intrinsic::dbg_value;
350+
}
351+
344352
void setKillLocation() {
345353
// TODO: When/if we remove duplicate values from DIArgLists, we don't need
346354
// this set anymore.

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ CallInst *changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr);
259259
/// Dbg Intrinsic utilities
260260
///
261261

262+
/// Creates and inserts a dbg_value record intrinsic before a store
263+
/// that has an associated llvm.dbg.value intrinsic.
264+
void InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
265+
DIBuilder &Builder);
266+
267+
/// Creates and inserts a llvm.dbg.value intrinsic before a store
268+
/// that has an associated llvm.dbg.value intrinsic.
269+
void InsertDebugValueAtStoreLoc(DbgVariableIntrinsic *DII, StoreInst *SI,
270+
DIBuilder &Builder);
271+
262272
/// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
263273
/// that has an associated llvm.dbg.declare intrinsic.
264274
void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,

llvm/lib/IR/DebugInfo.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) {
8080
return Declares;
8181
}
8282

83+
TinyPtrVector<DbgVariableRecord *> llvm::findDVRValues(Value *V) {
84+
// This function is hot. Check whether the value has any metadata to avoid a
85+
// DenseMap lookup.
86+
if (!V->isUsedByMetadata())
87+
return {};
88+
auto *L = LocalAsMetadata::getIfExists(V);
89+
if (!L)
90+
return {};
91+
92+
TinyPtrVector<DbgVariableRecord *> Values;
93+
for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers())
94+
if (DVR->getType() == DbgVariableRecord::LocationType::Value)
95+
Values.push_back(DVR);
96+
97+
return Values;
98+
}
99+
83100
template <typename IntrinsicT, bool DbgAssignAndValuesOnly>
84101
static void
85102
findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,19 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
17311731
SI->getIterator());
17321732
}
17331733

1734+
void llvm::InsertDebugValueAtStoreLoc(DbgVariableIntrinsic *DII, StoreInst *SI,
1735+
DIBuilder &Builder) {
1736+
auto *DIVar = DII->getVariable();
1737+
assert(DIVar && "Missing variable");
1738+
auto *DIExpr = DII->getExpression();
1739+
Value *DV = SI->getValueOperand();
1740+
1741+
DebugLoc NewLoc = getDebugValueLoc(DII);
1742+
1743+
insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1744+
SI->getIterator());
1745+
}
1746+
17341747
/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
17351748
/// that has an associated llvm.dbg.declare intrinsic.
17361749
void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
@@ -1805,6 +1818,19 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
18051818
SI->getParent()->insertDbgRecordBefore(NewDVR, SI->getIterator());
18061819
}
18071820

1821+
void llvm::InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
1822+
DIBuilder &Builder) {
1823+
auto *DIVar = DVR->getVariable();
1824+
assert(DIVar && "Missing variable");
1825+
auto *DIExpr = DVR->getExpression();
1826+
Value *DV = SI->getValueOperand();
1827+
1828+
DebugLoc NewLoc = getDebugValueLoc(DVR);
1829+
1830+
insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1831+
SI->getIterator());
1832+
}
1833+
18081834
/// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
18091835
/// llvm.dbg.declare intrinsic.
18101836
void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,

0 commit comments

Comments
 (0)