@@ -262,8 +262,7 @@ bool DebugInfoFinder::addScope(DIScope *Scope) {
262
262
}
263
263
264
264
static MDNode *updateLoopMetadataDebugLocationsImpl (
265
- MDNode *OrigLoopID,
266
- function_ref<DILocation *(const DILocation &)> Updater) {
265
+ MDNode *OrigLoopID, function_ref<Metadata *(Metadata *)> Updater) {
267
266
assert (OrigLoopID && OrigLoopID->getNumOperands () > 0 &&
268
267
" Loop ID needs at least one operand" );
269
268
assert (OrigLoopID && OrigLoopID->getOperand (0 ).get () == OrigLoopID &&
@@ -274,11 +273,10 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
274
273
275
274
for (unsigned i = 1 ; i < OrigLoopID->getNumOperands (); ++i) {
276
275
Metadata *MD = OrigLoopID->getOperand (i);
277
- if (DILocation *DL = dyn_cast<DILocation>(MD)) {
278
- if (DILocation *NewDL = Updater (*DL))
279
- MDs.push_back (NewDL);
280
- } else
281
- MDs.push_back (MD);
276
+ if (!MD)
277
+ MDs.push_back (nullptr );
278
+ else if (Metadata *NewMD = Updater (MD))
279
+ MDs.push_back (NewMD);
282
280
}
283
281
284
282
MDNode *NewLoopID = MDNode::getDistinct (OrigLoopID->getContext (), MDs);
@@ -288,34 +286,72 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
288
286
}
289
287
290
288
void llvm::updateLoopMetadataDebugLocations (
291
- Instruction &I, function_ref<DILocation *(const DILocation & )> Updater) {
289
+ Instruction &I, function_ref<Metadata *(Metadata * )> Updater) {
292
290
MDNode *OrigLoopID = I.getMetadata (LLVMContext::MD_loop);
293
291
if (!OrigLoopID)
294
292
return ;
295
293
MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl (OrigLoopID, Updater);
296
294
I.setMetadata (LLVMContext::MD_loop, NewLoopID);
297
295
}
298
296
297
+ // / Return true if a node is a DILocation or if a DILocation is
298
+ // / indirectly referenced by one of the node's children.
299
+ static bool isDILocationReachable (SmallPtrSetImpl<Metadata *> &Visited,
300
+ SmallPtrSetImpl<Metadata *> &Reachable,
301
+ Metadata *MD) {
302
+ MDNode *N = dyn_cast_or_null<MDNode>(MD);
303
+ if (!N)
304
+ return false ;
305
+ if (Reachable.count (N) || isa<DILocation>(N))
306
+ return true ;
307
+ if (!Visited.insert (N).second )
308
+ return false ;
309
+ for (auto &OpIt : N->operands ()) {
310
+ Metadata *Op = OpIt.get ();
311
+ if (isDILocationReachable (Visited, Reachable, Op)) {
312
+ Reachable.insert (N);
313
+ return true ;
314
+ }
315
+ }
316
+ return false ;
317
+ }
318
+
299
319
static MDNode *stripDebugLocFromLoopID (MDNode *N) {
300
320
assert (!N->operands ().empty () && " Missing self reference?" );
321
+ SmallPtrSet<Metadata *, 8 > Visited, DILocationReachable;
322
+ // If we already visited N, there is nothing to do.
323
+ if (!Visited.insert (N).second )
324
+ return N;
301
325
302
- // if there is no debug location, we do not have to rewrite this MDNode.
303
- if (std::none_of (N->op_begin () + 1 , N->op_end (), [](const MDOperand &Op) {
304
- return isa<DILocation>(Op.get ());
305
- }))
326
+ // If there is no debug location, we do not have to rewrite this
327
+ // MDNode. This loop also initializes DILocationReachable, later
328
+ // needed by updateLoopMetadataDebugLocationsImpl; the use of
329
+ // count_if avoids an early exit.
330
+ if (!std::count_if (N->op_begin () + 1 , N->op_end (),
331
+ [&Visited, &DILocationReachable](const MDOperand &Op) {
332
+ return isa<DILocation>(Op.get ()) ||
333
+ isDILocationReachable (
334
+ Visited, DILocationReachable, Op.get ());
335
+ }))
306
336
return N;
307
337
308
338
// If there is only the debug location without any actual loop metadata, we
309
339
// can remove the metadata.
310
- if (std::none_of (N->op_begin () + 1 , N->op_end (), [](const MDOperand &Op) {
311
- return !isa<DILocation>(Op.get ());
312
- }))
340
+ if (std::all_of (
341
+ N->op_begin () + 1 , N->op_end (),
342
+ [&Visited, &DILocationReachable](const MDOperand &Op) {
343
+ return isa<DILocation>(Op.get ()) ||
344
+ isDILocationReachable (Visited, DILocationReachable,
345
+ Op.get ());
346
+ }))
313
347
return nullptr ;
314
348
315
- auto dropDebugLoc = [](const DILocation &) -> DILocation * {
316
- return nullptr ;
317
- };
318
- return updateLoopMetadataDebugLocationsImpl (N, dropDebugLoc);
349
+ return updateLoopMetadataDebugLocationsImpl (
350
+ N, [&DILocationReachable](Metadata *MD) -> Metadata * {
351
+ if (isa<DILocation>(MD) || DILocationReachable.count (MD))
352
+ return nullptr ;
353
+ return MD;
354
+ });
319
355
}
320
356
321
357
bool llvm::stripDebugInfo (Function &F) {
@@ -325,7 +361,7 @@ bool llvm::stripDebugInfo(Function &F) {
325
361
F.setSubprogram (nullptr );
326
362
}
327
363
328
- DenseMap<MDNode*, MDNode*> LoopIDsMap;
364
+ DenseMap<MDNode *, MDNode *> LoopIDsMap;
329
365
for (BasicBlock &BB : F) {
330
366
for (auto II = BB.begin (), End = BB.end (); II != End;) {
331
367
Instruction &I = *II++; // We may delete the instruction, increment now.
@@ -658,8 +694,10 @@ bool llvm::stripNonLineTableDebugInfo(Module &M) {
658
694
I.setDebugLoc (remapDebugLoc (I.getDebugLoc ()));
659
695
660
696
// Remap DILocations in llvm.loop attachments.
661
- updateLoopMetadataDebugLocations (I, [&](const DILocation &Loc) {
662
- return remapDebugLoc (&Loc).get ();
697
+ updateLoopMetadataDebugLocations (I, [&](Metadata *MD) -> Metadata * {
698
+ if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
699
+ return remapDebugLoc (Loc).get ();
700
+ return MD;
663
701
});
664
702
665
703
// Strip heapallocsite attachments, they point into the DIType system.
0 commit comments