@@ -405,10 +405,17 @@ static bool DPValuesRemoveRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
405
405
// If the same variable fragment is described more than once it is enough
406
406
// to keep the last one (i.e. the first found since we for reverse
407
407
// iteration).
408
- // FIXME: add assignment tracking support (see parallel implementation
409
- // below).
410
- if (!R.second )
411
- ToBeRemoved.push_back (&DPV);
408
+ if (R.second )
409
+ continue ;
410
+
411
+ if (DPV.isDbgAssign ()) {
412
+ // Don't delete dbg.assign intrinsics that are linked to instructions.
413
+ if (!at::getAssignmentInsts (&DPV).empty ())
414
+ continue ;
415
+ // Unlinked dbg.assign intrinsics can be treated like dbg.values.
416
+ }
417
+
418
+ ToBeRemoved.push_back (&DPV);
412
419
continue ;
413
420
}
414
421
// Sequence with consecutive dbg.value instrs ended. Clear the map to
@@ -495,14 +502,25 @@ static bool DPValuesRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
495
502
DebugVariable Key (DPV.getVariable (), std::nullopt,
496
503
DPV.getDebugLoc ()->getInlinedAt ());
497
504
auto VMI = VariableMap.find (Key);
505
+ // A dbg.assign with no linked instructions can be treated like a
506
+ // dbg.value (i.e. can be deleted).
507
+ bool IsDbgValueKind =
508
+ (!DPV.isDbgAssign () || at::getAssignmentInsts (&DPV).empty ());
509
+
498
510
// Update the map if we found a new value/expression describing the
499
511
// variable, or if the variable wasn't mapped already.
500
512
SmallVector<Value *, 4 > Values (DPV.location_ops ());
501
513
if (VMI == VariableMap.end () || VMI->second .first != Values ||
502
514
VMI->second .second != DPV.getExpression ()) {
503
- VariableMap[Key] = {Values, DPV.getExpression ()};
515
+ if (IsDbgValueKind)
516
+ VariableMap[Key] = {Values, DPV.getExpression ()};
517
+ else
518
+ VariableMap[Key] = {Values, nullptr };
504
519
continue ;
505
520
}
521
+ // Don't delete dbg.assign intrinsics that are linked to instructions.
522
+ if (!IsDbgValueKind)
523
+ continue ;
506
524
// Found an identical mapping. Remember the instruction for later removal.
507
525
ToBeRemoved.push_back (&DPV);
508
526
}
@@ -514,6 +532,42 @@ static bool DPValuesRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
514
532
return !ToBeRemoved.empty ();
515
533
}
516
534
535
+ static bool DPValuesRemoveUndefDbgAssignsFromEntryBlock (BasicBlock *BB) {
536
+ assert (BB->isEntryBlock () && " expected entry block" );
537
+ SmallVector<DPValue *, 8 > ToBeRemoved;
538
+ DenseSet<DebugVariable> SeenDefForAggregate;
539
+ // Returns the DebugVariable for DVI with no fragment info.
540
+ auto GetAggregateVariable = [](const DPValue &DPV) {
541
+ return DebugVariable (DPV.getVariable (), std::nullopt,
542
+ DPV.getDebugLoc ().getInlinedAt ());
543
+ };
544
+
545
+ // Remove undef dbg.assign intrinsics that are encountered before
546
+ // any non-undef intrinsics from the entry block.
547
+ for (auto &I : *BB) {
548
+ for (DPValue &DPV : I.getDbgValueRange ()) {
549
+ if (!DPV.isDbgValue () && !DPV.isDbgAssign ())
550
+ continue ;
551
+ bool IsDbgValueKind =
552
+ (DPV.isDbgValue () || at::getAssignmentInsts (&DPV).empty ());
553
+ DebugVariable Aggregate = GetAggregateVariable (DPV);
554
+ if (!SeenDefForAggregate.contains (Aggregate)) {
555
+ bool IsKill = DPV.isKillLocation () && IsDbgValueKind;
556
+ if (!IsKill) {
557
+ SeenDefForAggregate.insert (Aggregate);
558
+ } else if (DPV.isDbgAssign ()) {
559
+ ToBeRemoved.push_back (&DPV);
560
+ }
561
+ }
562
+ }
563
+ }
564
+
565
+ for (DPValue *DPV : ToBeRemoved)
566
+ DPV->eraseFromParent ();
567
+
568
+ return !ToBeRemoved.empty ();
569
+ }
570
+
517
571
static bool removeRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
518
572
if (BB->IsNewDbgInfoFormat )
519
573
return DPValuesRemoveRedundantDbgInstrsUsingForwardScan (BB);
@@ -578,7 +632,10 @@ static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
578
632
// / then (only) the instruction marked with (*) can be removed.
579
633
// / Possible improvements:
580
634
// / - Keep track of non-overlapping fragments.
581
- static bool remomveUndefDbgAssignsFromEntryBlock (BasicBlock *BB) {
635
+ static bool removeUndefDbgAssignsFromEntryBlock (BasicBlock *BB) {
636
+ if (BB->IsNewDbgInfoFormat )
637
+ return DPValuesRemoveUndefDbgAssignsFromEntryBlock (BB);
638
+
582
639
assert (BB->isEntryBlock () && " expected entry block" );
583
640
SmallVector<DbgAssignIntrinsic *, 8 > ToBeRemoved;
584
641
DenseSet<DebugVariable> SeenDefForAggregate;
@@ -629,7 +686,7 @@ bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
629
686
MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan (BB);
630
687
if (BB->isEntryBlock () &&
631
688
isAssignmentTrackingEnabled (*BB->getParent ()->getParent ()))
632
- MadeChanges |= remomveUndefDbgAssignsFromEntryBlock (BB);
689
+ MadeChanges |= removeUndefDbgAssignsFromEntryBlock (BB);
633
690
MadeChanges |= removeRedundantDbgInstrsUsingForwardScan (BB);
634
691
635
692
if (MadeChanges)
0 commit comments