Skip to content

Commit 3041198

Browse files
authored
[DebugInfo][RemoveDIs][NFC] Split findDbgDeclares into two functions (#77478)
This patch follows on from comments on #73498, implementing the proposed split of findDbgDeclares into two separate functions for DbgDeclareInsts and DPVDeclares, which return containers rather than taking containers by reference.
1 parent c6dfb62 commit 3041198

File tree

6 files changed

+60
-48
lines changed

6 files changed

+60
-48
lines changed

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class Module;
4040

4141
/// Finds dbg.declare intrinsics declaring local variables as living in the
4242
/// memory that 'V' points to.
43-
void findDbgDeclares(SmallVectorImpl<DbgDeclareInst *> &DbgUsers, Value *V,
44-
SmallVectorImpl<DPValue *> *DPValues = nullptr);
43+
TinyPtrVector<DbgDeclareInst *> findDbgDeclares(Value *V);
44+
/// As above, for DPVDeclares.
45+
TinyPtrVector<DPValue *> findDPVDeclares(Value *V);
4546

4647
/// Finds the llvm.dbg.value intrinsics describing a value.
4748
void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,

llvm/lib/IR/DebugInfo.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,42 @@ using namespace llvm;
4444
using namespace llvm::at;
4545
using namespace llvm::dwarf;
4646

47+
TinyPtrVector<DbgDeclareInst *> llvm::findDbgDeclares(Value *V) {
48+
// This function is hot. Check whether the value has any metadata to avoid a
49+
// DenseMap lookup.
50+
if (!V->isUsedByMetadata())
51+
return {};
52+
auto *L = LocalAsMetadata::getIfExists(V);
53+
if (!L)
54+
return {};
55+
auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
56+
if (!MDV)
57+
return {};
58+
59+
TinyPtrVector<DbgDeclareInst *> Declares;
60+
for (User *U : MDV->users())
61+
if (auto *DDI = dyn_cast<DbgDeclareInst>(U))
62+
Declares.push_back(DDI);
63+
64+
return Declares;
65+
}
66+
TinyPtrVector<DPValue *> llvm::findDPVDeclares(Value *V) {
67+
// This function is hot. Check whether the value has any metadata to avoid a
68+
// DenseMap lookup.
69+
if (!V->isUsedByMetadata())
70+
return {};
71+
auto *L = LocalAsMetadata::getIfExists(V);
72+
if (!L)
73+
return {};
74+
75+
TinyPtrVector<DPValue *> Declares;
76+
for (DPValue *DPV : L->getAllDPValueUsers())
77+
if (DPV->getType() == DPValue::LocationType::Declare)
78+
Declares.push_back(DPV);
79+
80+
return Declares;
81+
}
82+
4783
template <typename IntrinsicT,
4884
DPValue::LocationType Type = DPValue::LocationType::Any>
4985
static void findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
@@ -97,12 +133,6 @@ static void findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result, Value *V,
97133
}
98134
}
99135

100-
void llvm::findDbgDeclares(SmallVectorImpl<DbgDeclareInst *> &DbgUsers,
101-
Value *V, SmallVectorImpl<DPValue *> *DPValues) {
102-
findDbgIntrinsics<DbgDeclareInst, DPValue::LocationType::Declare>(DbgUsers, V,
103-
DPValues);
104-
}
105-
106136
void llvm::findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,
107137
Value *V, SmallVectorImpl<DPValue *> *DPValues) {
108138
findDbgIntrinsics<DbgValueInst, DPValue::LocationType::Value>(DbgValues, V,

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -963,18 +963,15 @@ static void cacheDIVar(FrameDataInfo &FrameData,
963963
if (DIVarCache.contains(V))
964964
continue;
965965

966-
SmallVector<DbgDeclareInst *, 1> DDIs;
967-
SmallVector<DPValue *, 1> DPVs;
968-
findDbgDeclares(DDIs, V, &DPVs);
969-
auto CacheIt = [&DIVarCache, V](auto &Container) {
966+
auto CacheIt = [&DIVarCache, V](auto Container) {
970967
auto *I = llvm::find_if(Container, [](auto *DDI) {
971968
return DDI->getExpression()->getNumElements() == 0;
972969
});
973970
if (I != Container.end())
974971
DIVarCache.insert({V, (*I)->getVariable()});
975972
};
976-
CacheIt(DDIs);
977-
CacheIt(DPVs);
973+
CacheIt(findDbgDeclares(V));
974+
CacheIt(findDPVDeclares(V));
978975
}
979976
}
980977

@@ -1125,9 +1122,8 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
11251122
assert(PromiseAlloca &&
11261123
"Coroutine with switch ABI should own Promise alloca");
11271124

1128-
SmallVector<DbgDeclareInst *, 1> DIs;
1129-
SmallVector<DPValue *, 1> DPVs;
1130-
findDbgDeclares(DIs, PromiseAlloca, &DPVs);
1125+
TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(PromiseAlloca);
1126+
TinyPtrVector<DPValue *> DPVs = findDPVDeclares(PromiseAlloca);
11311127

11321128
DILocalVariable *PromiseDIVariable = nullptr;
11331129
DILocation *DILoc = nullptr;
@@ -1865,9 +1861,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
18651861
FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP,
18661862
SpillAlignment, E.first->getName() + Twine(".reload"));
18671863

1868-
SmallVector<DbgDeclareInst *, 1> DIs;
1869-
SmallVector<DPValue *, 1> DPVs;
1870-
findDbgDeclares(DIs, Def, &DPVs);
1864+
TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(Def);
1865+
TinyPtrVector<DPValue *> DPVs = findDPVDeclares(Def);
18711866
// Try best to find dbg.declare. If the spill is a temp, there may not
18721867
// be a direct dbg.declare. Walk up the load chain to find one from an
18731868
// alias.
@@ -1881,9 +1876,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
18811876
CurDef = LdInst->getPointerOperand();
18821877
if (!isa<AllocaInst, LoadInst>(CurDef))
18831878
break;
1884-
DIs.clear();
1885-
DPVs.clear();
1886-
findDbgDeclares(DIs, CurDef, &DPVs);
1879+
DIs = findDbgDeclares(CurDef);
1880+
DPVs = findDPVDeclares(CurDef);
18871881
}
18881882
}
18891883

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5021,9 +5021,6 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
50215021

50225022
// Remove any existing intrinsics on the new alloca describing
50235023
// the variable fragment.
5024-
SmallVector<DbgDeclareInst *, 1> FragDbgDeclares;
5025-
SmallVector<DPValue *, 1> FragDPVs;
5026-
findDbgDeclares(FragDbgDeclares, Fragment.Alloca, &FragDPVs);
50275024
auto RemoveOne = [DbgVariable](auto *OldDII) {
50285025
auto SameVariableFragment = [](const auto *LHS, const auto *RHS) {
50295026
return LHS->getVariable() == RHS->getVariable() &&
@@ -5033,20 +5030,17 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
50335030
if (SameVariableFragment(OldDII, DbgVariable))
50345031
OldDII->eraseFromParent();
50355032
};
5036-
for_each(FragDbgDeclares, RemoveOne);
5037-
for_each(FragDPVs, RemoveOne);
5033+
for_each(findDbgDeclares(Fragment.Alloca), RemoveOne);
5034+
for_each(findDPVDeclares(Fragment.Alloca), RemoveOne);
50385035

50395036
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, FragmentExpr, &AI);
50405037
}
50415038
};
50425039

50435040
// Migrate debug information from the old alloca to the new alloca(s)
50445041
// and the individual partitions.
5045-
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
5046-
SmallVector<DPValue *, 1> DPValues;
5047-
findDbgDeclares(DbgDeclares, &AI, &DPValues);
5048-
for_each(DbgDeclares, MigrateOne);
5049-
for_each(DPValues, MigrateOne);
5042+
for_each(findDbgDeclares(&AI), MigrateOne);
5043+
for_each(findDPVDeclares(&AI), MigrateOne);
50505044
for_each(at::getAssignmentMarkers(&AI), MigrateOne);
50515045

50525046
return Changed;
@@ -5169,12 +5163,9 @@ bool SROA::deleteDeadInstructions(
51695163
// not be able to find it.
51705164
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
51715165
DeletedAllocas.insert(AI);
5172-
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
5173-
SmallVector<DPValue *, 1> DPValues;
5174-
findDbgDeclares(DbgDeclares, AI, &DPValues);
5175-
for (DbgDeclareInst *OldDII : DbgDeclares)
5166+
for (DbgDeclareInst *OldDII : findDbgDeclares(AI))
51765167
OldDII->eraseFromParent();
5177-
for (DPValue *OldDII : DPValues)
5168+
for (DPValue *OldDII : findDPVDeclares(AI))
51785169
OldDII->eraseFromParent();
51795170
}
51805171

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,9 +2130,8 @@ void llvm::insertDebugValuesForPHIs(BasicBlock *BB,
21302130
bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
21312131
DIBuilder &Builder, uint8_t DIExprFlags,
21322132
int Offset) {
2133-
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
2134-
SmallVector<DPValue *, 1> DPValues;
2135-
findDbgDeclares(DbgDeclares, Address, &DPValues);
2133+
TinyPtrVector<DbgDeclareInst *> DbgDeclares = findDbgDeclares(Address);
2134+
TinyPtrVector<DPValue *> DPVDeclares = findDPVDeclares(Address);
21362135

21372136
auto ReplaceOne = [&](auto *DII) {
21382137
assert(DII->getVariable() && "Missing variable");
@@ -2143,9 +2142,9 @@ bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
21432142
};
21442143

21452144
for_each(DbgDeclares, ReplaceOne);
2146-
for_each(DPValues, ReplaceOne);
2145+
for_each(DPVDeclares, ReplaceOne);
21472146

2148-
return !DbgDeclares.empty() || !DPValues.empty();
2147+
return !DbgDeclares.empty() || !DPVDeclares.empty();
21492148
}
21502149

21512150
static void updateOneDbgValueForAlloca(const DebugLoc &Loc,

llvm/lib/Transforms/Utils/MemoryOpRemark.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@ void MemoryOpRemark::visitVariable(const Value *V,
321321
bool FoundDI = false;
322322
// Try to get an llvm.dbg.declare, which has a DILocalVariable giving us the
323323
// real debug info name and size of the variable.
324-
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
325-
SmallVector<DPValue *, 1> DPValues;
326-
findDbgDeclares(DbgDeclares, const_cast<Value *>(V), &DPValues);
327324
auto FindDI = [&](const auto *DVI) {
328325
if (DILocalVariable *DILV = DVI->getVariable()) {
329326
std::optional<uint64_t> DISize = getSizeInBytes(DILV->getSizeInBits());
@@ -334,8 +331,8 @@ void MemoryOpRemark::visitVariable(const Value *V,
334331
}
335332
}
336333
};
337-
for_each(DbgDeclares, FindDI);
338-
for_each(DPValues, FindDI);
334+
for_each(findDbgDeclares(const_cast<Value *>(V)), FindDI);
335+
for_each(findDPVDeclares(const_cast<Value *>(V)), FindDI);
339336

340337
if (FoundDI) {
341338
assert(!Result.empty());

0 commit comments

Comments
 (0)