@@ -382,7 +382,39 @@ bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
382
382
// / - Check fully overlapping fragments and not only identical fragments.
383
383
// / - Support dbg.declare. dbg.label, and possibly other meta instructions being
384
384
// / part of the sequence of consecutive instructions.
385
+ static bool DPValuesRemoveRedundantDbgInstrsUsingBackwardScan (BasicBlock *BB) {
386
+ SmallVector<DPValue *, 8 > ToBeRemoved;
387
+ SmallDenseSet<DebugVariable> VariableSet;
388
+ for (auto &I : reverse (*BB)) {
389
+ for (DPValue &DPV : reverse (I.getDbgValueRange ())) {
390
+ DebugVariable Key (DPV.getVariable (), DPV.getExpression (),
391
+ DPV.getDebugLoc ()->getInlinedAt ());
392
+ auto R = VariableSet.insert (Key);
393
+ // If the same variable fragment is described more than once it is enough
394
+ // to keep the last one (i.e. the first found since we for reverse
395
+ // iteration).
396
+ // FIXME: add assignment tracking support (see parallel implementation
397
+ // below).
398
+ if (!R.second )
399
+ ToBeRemoved.push_back (&DPV);
400
+ continue ;
401
+ }
402
+ // Sequence with consecutive dbg.value instrs ended. Clear the map to
403
+ // restart identifying redundant instructions if case we find another
404
+ // dbg.value sequence.
405
+ VariableSet.clear ();
406
+ }
407
+
408
+ for (auto &DPV : ToBeRemoved)
409
+ DPV->eraseFromParent ();
410
+
411
+ return !ToBeRemoved.empty ();
412
+ }
413
+
385
414
static bool removeRedundantDbgInstrsUsingBackwardScan (BasicBlock *BB) {
415
+ if (BB->IsNewDbgInfoFormat )
416
+ return DPValuesRemoveRedundantDbgInstrsUsingBackwardScan (BB);
417
+
386
418
SmallVector<DbgValueInst *, 8 > ToBeRemoved;
387
419
SmallDenseSet<DebugVariable> VariableSet;
388
420
for (auto &I : reverse (*BB)) {
@@ -440,7 +472,38 @@ static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
440
472
// /
441
473
// / Possible improvements:
442
474
// / - Keep track of non-overlapping fragments.
475
+ static bool DPValuesRemoveRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
476
+ SmallVector<DPValue *, 8 > ToBeRemoved;
477
+ DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4 >, DIExpression *>>
478
+ VariableMap;
479
+ for (auto &I : *BB) {
480
+ for (DPValue &DPV : I.getDbgValueRange ()) {
481
+ DebugVariable Key (DPV.getVariable (), std::nullopt,
482
+ DPV.getDebugLoc ()->getInlinedAt ());
483
+ auto VMI = VariableMap.find (Key);
484
+ // Update the map if we found a new value/expression describing the
485
+ // variable, or if the variable wasn't mapped already.
486
+ SmallVector<Value *, 4 > Values (DPV.location_ops ());
487
+ if (VMI == VariableMap.end () || VMI->second .first != Values ||
488
+ VMI->second .second != DPV.getExpression ()) {
489
+ VariableMap[Key] = {Values, DPV.getExpression ()};
490
+ continue ;
491
+ }
492
+ // Found an identical mapping. Remember the instruction for later removal.
493
+ ToBeRemoved.push_back (&DPV);
494
+ }
495
+ }
496
+
497
+ for (auto *DPV : ToBeRemoved)
498
+ DPV->eraseFromParent ();
499
+
500
+ return !ToBeRemoved.empty ();
501
+ }
502
+
443
503
static bool removeRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
504
+ if (BB->IsNewDbgInfoFormat )
505
+ return DPValuesRemoveRedundantDbgInstrsUsingForwardScan (BB);
506
+
444
507
SmallVector<DbgValueInst *, 8 > ToBeRemoved;
445
508
DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4 >, DIExpression *>>
446
509
VariableMap;
0 commit comments