Skip to content

Commit dc11e03

Browse files
Add code to handle llvm.dbg.values in SROA.
This patch adds a bunch of functions that will be used to properly handle debug information for llvm.dbg.values in the SROA pass.
1 parent f4681be commit dc11e03

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-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.addr
431+
/// and dbg.declare, but not True 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ 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 not dbg.declare, which describes its address, or dbg.assign, which
346+
/// describes a combination of the variable's value and address.
347+
bool isValueOfVariable() const {
348+
return getIntrinsicID() == Intrinsic::dbg_value;
349+
}
350+
344351
void setKillLocation() {
345352
// TODO: When/if we remove duplicate values from DIArgLists, we don't need
346353
// this set anymore.

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

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

262+
/// Moves an llvm.dbg.value intrinsic before a store to an alloca'd value
263+
/// that has an associated llvm.dbg.value intrinsic.
264+
void MoveDebugValueToStoreLoc(DbgVariableRecord *DVR, StoreInst *SI,
265+
DIBuilder &Builder);
266+
void MoveDebugValueToStoreLoc(DbgVariableIntrinsic *DII, StoreInst *SI,
267+
DIBuilder &Builder);
268+
262269
/// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
263270
/// that has an associated llvm.dbg.declare intrinsic.
264271
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,21 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
17311731
SI->getIterator());
17321732
}
17331733

1734+
/// Moves an llvm.dbg.value intrinsic before a store to an alloca'd value
1735+
/// that has an associated llvm.dbg.value intrinsic.
1736+
void llvm::MoveDebugValueToStoreLoc(DbgVariableIntrinsic *DII, StoreInst *SI,
1737+
DIBuilder &Builder) {
1738+
auto *DIVar = DII->getVariable();
1739+
assert(DIVar && "Missing variable");
1740+
auto *DIExpr = DII->getExpression();
1741+
Value *DV = SI->getValueOperand();
1742+
1743+
DebugLoc NewLoc = getDebugValueLoc(DII);
1744+
1745+
insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc,
1746+
SI->getIterator());
1747+
}
1748+
17341749
/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
17351750
/// that has an associated llvm.dbg.declare intrinsic.
17361751
void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
@@ -1805,6 +1820,19 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR,
18051820
SI->getParent()->insertDbgRecordBefore(NewDVR, SI->getIterator());
18061821
}
18071822

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

0 commit comments

Comments
 (0)