@@ -1816,8 +1816,12 @@ void at::RAUW(DIAssignID *Old, DIAssignID *New) {
1816
1816
1817
1817
void at::deleteAll (Function *F) {
1818
1818
SmallVector<DbgAssignIntrinsic *, 12 > ToDelete;
1819
+ SmallVector<DPValue *, 12 > DPToDelete;
1819
1820
for (BasicBlock &BB : *F) {
1820
1821
for (Instruction &I : BB) {
1822
+ for (auto &DPV : I.getDbgValueRange ())
1823
+ if (DPV.isDbgAssign ())
1824
+ DPToDelete.push_back (&DPV);
1821
1825
if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
1822
1826
ToDelete.push_back (DAI);
1823
1827
else
@@ -1826,6 +1830,8 @@ void at::deleteAll(Function *F) {
1826
1830
}
1827
1831
for (auto *DAI : ToDelete)
1828
1832
DAI->eraseFromParent ();
1833
+ for (auto *DPV : DPToDelete)
1834
+ DPV->eraseFromParent ();
1829
1835
}
1830
1836
1831
1837
// / Get the FragmentInfo for the variable if it exists, otherwise return a
@@ -2056,9 +2062,9 @@ std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL,
2056
2062
}
2057
2063
2058
2064
// / Returns nullptr if the assignment shouldn't be attributed to this variable.
2059
- static CallInst * emitDbgAssign (AssignmentInfo Info, Value *Val, Value *Dest,
2060
- Instruction &StoreLikeInst ,
2061
- const VarRecord &VarRec, DIBuilder &DIB) {
2065
+ static void emitDbgAssign (AssignmentInfo Info, Value *Val, Value *Dest,
2066
+ Instruction &StoreLikeInst, const VarRecord &VarRec ,
2067
+ DIBuilder &DIB) {
2062
2068
auto *ID = StoreLikeInst.getMetadata (LLVMContext::MD_DIAssignID);
2063
2069
assert (ID && " Store instruction must have DIAssignID metadata" );
2064
2070
(void )ID;
@@ -2082,7 +2088,7 @@ static CallInst *emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
2082
2088
2083
2089
// Discard stores to bits outside this variable.
2084
2090
if (FragStartBit >= FragEndBit)
2085
- return nullptr ;
2091
+ return ;
2086
2092
2087
2093
StoreToWholeVariable = FragStartBit <= VarStartBit && FragEndBit >= *Size;
2088
2094
}
@@ -2097,8 +2103,17 @@ static CallInst *emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
2097
2103
}
2098
2104
DIExpression *AddrExpr =
2099
2105
DIExpression::get (StoreLikeInst.getContext (), std::nullopt);
2100
- return DIB.insertDbgAssign (&StoreLikeInst, Val, VarRec.Var , Expr, Dest,
2101
- AddrExpr, VarRec.DL );
2106
+ if (StoreLikeInst.getParent ()->IsNewDbgInfoFormat ) {
2107
+ auto *Assign = DPValue::createLinkedDPVAssign (
2108
+ &StoreLikeInst, Val, VarRec.Var , Expr, Dest, AddrExpr, VarRec.DL );
2109
+ (void )Assign;
2110
+ LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
2111
+ return ;
2112
+ }
2113
+ auto *Assign = DIB.insertDbgAssign (&StoreLikeInst, Val, VarRec.Var , Expr,
2114
+ Dest, AddrExpr, VarRec.DL );
2115
+ (void )Assign;
2116
+ LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
2102
2117
}
2103
2118
2104
2119
#undef DEBUG_TYPE // Silence redefinition warning (from ConstantsContext.h).
@@ -2185,12 +2200,8 @@ void at::trackAssignments(Function::iterator Start, Function::iterator End,
2185
2200
I.setMetadata (LLVMContext::MD_DIAssignID, ID);
2186
2201
}
2187
2202
2188
- for (const VarRecord &R : LocalIt->second ) {
2189
- auto *Assign =
2190
- emitDbgAssign (*Info, ValueComponent, DestComponent, I, R, DIB);
2191
- (void )Assign;
2192
- LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
2193
- }
2203
+ for (const VarRecord &R : LocalIt->second )
2204
+ emitDbgAssign (*Info, ValueComponent, DestComponent, I, R, DIB);
2194
2205
}
2195
2206
}
2196
2207
}
@@ -2206,32 +2217,38 @@ bool AssignmentTrackingPass::runOnFunction(Function &F) {
2206
2217
// storage" is limited to Allocas). We'll use this to find dbg.declares to
2207
2218
// delete after running `trackAssignments`.
2208
2219
DenseMap<const AllocaInst *, SmallPtrSet<DbgDeclareInst *, 2 >> DbgDeclares;
2220
+ DenseMap<const AllocaInst *, SmallPtrSet<DPValue *, 2 >> DPVDeclares;
2209
2221
// Create another similar map of {storage : variables} that we'll pass to
2210
2222
// trackAssignments.
2211
2223
StorageToVarsMap Vars;
2224
+ auto ProcessDeclare = [&](auto *Declare, auto &DeclareList) {
2225
+ // FIXME: trackAssignments doesn't let you specify any modifiers to the
2226
+ // variable (e.g. fragment) or location (e.g. offset), so we have to
2227
+ // leave dbg.declares with non-empty expressions in place.
2228
+ if (Declare->getExpression ()->getNumElements () != 0 )
2229
+ return ;
2230
+ if (!Declare->getAddress ())
2231
+ return ;
2232
+ if (AllocaInst *Alloca =
2233
+ dyn_cast<AllocaInst>(Declare->getAddress ()->stripPointerCasts ())) {
2234
+ // FIXME: Skip VLAs for now (let these variables use dbg.declares).
2235
+ if (!Alloca->isStaticAlloca ())
2236
+ return ;
2237
+ // Similarly, skip scalable vectors (use dbg.declares instead).
2238
+ if (auto Sz = Alloca->getAllocationSize (*DL); Sz && Sz->isScalable ())
2239
+ return ;
2240
+ DeclareList[Alloca].insert (Declare);
2241
+ Vars[Alloca].insert (VarRecord (Declare));
2242
+ }
2243
+ };
2212
2244
for (auto &BB : F) {
2213
2245
for (auto &I : BB) {
2214
- DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(&I);
2215
- if (!DDI)
2216
- continue ;
2217
- // FIXME: trackAssignments doesn't let you specify any modifiers to the
2218
- // variable (e.g. fragment) or location (e.g. offset), so we have to
2219
- // leave dbg.declares with non-empty expressions in place.
2220
- if (DDI->getExpression ()->getNumElements () != 0 )
2221
- continue ;
2222
- if (!DDI->getAddress ())
2223
- continue ;
2224
- if (AllocaInst *Alloca =
2225
- dyn_cast<AllocaInst>(DDI->getAddress ()->stripPointerCasts ())) {
2226
- // FIXME: Skip VLAs for now (let these variables use dbg.declares).
2227
- if (!Alloca->isStaticAlloca ())
2228
- continue ;
2229
- // Similarly, skip scalable vectors (use dbg.declares instead).
2230
- if (auto Sz = Alloca->getAllocationSize (*DL); Sz && Sz->isScalable ())
2231
- continue ;
2232
- DbgDeclares[Alloca].insert (DDI);
2233
- Vars[Alloca].insert (VarRecord (DDI));
2246
+ for (auto &DPV : I.getDbgValueRange ()) {
2247
+ if (DPV.isDbgDeclare ())
2248
+ ProcessDeclare (&DPV, DPVDeclares);
2234
2249
}
2250
+ if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(&I))
2251
+ ProcessDeclare (DDI, DbgDeclares);
2235
2252
}
2236
2253
}
2237
2254
@@ -2247,35 +2264,30 @@ bool AssignmentTrackingPass::runOnFunction(Function &F) {
2247
2264
trackAssignments (F.begin (), F.end (), Vars, *DL);
2248
2265
2249
2266
// Delete dbg.declares for variables now tracked with assignment tracking.
2250
- for (auto &P : DbgDeclares) {
2251
- const AllocaInst *Alloca = P.first ;
2252
- auto Markers = at::getAssignmentMarkers (Alloca);
2253
- SmallVector<DPValue *> DPMarkers = at::getDPVAssignmentMarkers (Alloca);
2267
+ auto DeleteSubsumedDeclare = [&](const auto &Markers, auto &Declares) {
2254
2268
(void )Markers;
2255
- (void )DPMarkers;
2256
- for (DbgDeclareInst *DDI : P.second ) {
2257
- // Assert that the alloca that DDI uses is now linked to a dbg.assign
2269
+ for (auto *Declare : Declares) {
2270
+ // Assert that the alloca that Declare uses is now linked to a dbg.assign
2258
2271
// describing the same variable (i.e. check that this dbg.declare has
2259
2272
// been replaced by a dbg.assign). Use DebugVariableAggregate to Discard
2260
2273
// the fragment part because trackAssignments may alter the
2261
2274
// fragment. e.g. if the alloca is smaller than the variable, then
2262
2275
// trackAssignments will create an alloca-sized fragment for the
2263
2276
// dbg.assign.
2264
- assert (llvm::any_of (Markers,
2265
- [DDI](DbgAssignIntrinsic *DAI) {
2266
- return DebugVariableAggregate (DAI) ==
2267
- DebugVariableAggregate (DDI);
2268
- }) ||
2269
- llvm::any_of (DPMarkers, [DDI](DPValue *DPV) {
2270
- return DebugVariableAggregate (DPV) ==
2271
- DebugVariableAggregate (DDI);
2272
- }));
2273
- // Delete DDI because the variable location is now tracked using
2277
+ assert (llvm::any_of (Markers, [Declare](auto *Assign) {
2278
+ return DebugVariableAggregate (Assign) ==
2279
+ DebugVariableAggregate (Declare);
2280
+ }));
2281
+ // Delete Declare because the variable location is now tracked using
2274
2282
// assignment tracking.
2275
- DDI ->eraseFromParent ();
2283
+ Declare ->eraseFromParent ();
2276
2284
Changed = true ;
2277
2285
}
2278
- }
2286
+ };
2287
+ for (auto &P : DbgDeclares)
2288
+ DeleteSubsumedDeclare (at::getAssignmentMarkers (P.first ), P.second );
2289
+ for (auto &P : DPVDeclares)
2290
+ DeleteSubsumedDeclare (at::getDPVAssignmentMarkers (P.first ), P.second );
2279
2291
return Changed;
2280
2292
}
2281
2293
0 commit comments