@@ -377,15 +377,12 @@ bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
377377// /
378378// / Possible improvements:
379379// / - Check fully overlapping fragments and not only identical fragments.
380- static bool
381- DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan (BasicBlock *BB) {
380+ static bool removeRedundantDbgInstrsUsingBackwardScan (BasicBlock *BB) {
382381 SmallVector<DbgVariableRecord *, 8 > ToBeRemoved;
383382 SmallDenseSet<DebugVariable> VariableSet;
384383 for (auto &I : reverse (*BB)) {
385- for (DbgVariableRecord &DR :
384+ for (DbgVariableRecord &DVR :
386385 reverse (filterDbgVars (I.getDbgRecordRange ()))) {
387- DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
388-
389386 DebugVariable Key (DVR.getVariable (), DVR.getExpression (),
390387 DVR.getDebugLoc ()->getInlinedAt ());
391388 auto R = VariableSet.insert (Key);
@@ -416,10 +413,6 @@ DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
416413 return !ToBeRemoved.empty ();
417414}
418415
419- static bool removeRedundantDbgInstrsUsingBackwardScan (BasicBlock *BB) {
420- return DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan (BB);
421- }
422-
423416// / Remove redundant dbg.value instructions using a forward scan. This can
424417// / remove a dbg.value instruction that is redundant due to indicating that a
425418// / variable has the same value as already being indicated by an earlier
@@ -439,14 +432,14 @@ static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
439432// /
440433// / Possible improvements:
441434// / - Keep track of non-overlapping fragments.
442- static bool
443- DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
444- SmallVector<DbgVariableRecord *, 8 > ToBeRemoved;
435+ static bool removeRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
436+ bool RemovedAny = false ;
445437 SmallDenseMap<DebugVariable,
446438 std::pair<SmallVector<Value *, 4 >, DIExpression *>, 4 >
447439 VariableMap;
448440 for (auto &I : *BB) {
449- for (DbgVariableRecord &DVR : filterDbgVars (I.getDbgRecordRange ())) {
441+ for (DbgVariableRecord &DVR :
442+ make_early_inc_range (filterDbgVars (I.getDbgRecordRange ()))) {
450443 if (DVR.getType () == DbgVariableRecord::LocationType::Declare)
451444 continue ;
452445 DebugVariable Key (DVR.getVariable (), std::nullopt ,
@@ -472,55 +465,12 @@ DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
472465 if (!IsDbgValueKind)
473466 continue ;
474467 // Found an identical mapping. Remember the instruction for later removal.
475- ToBeRemoved.push_back (&DVR);
476- }
477- }
478-
479- for (auto *DVR : ToBeRemoved)
480- DVR->eraseFromParent ();
481-
482- return !ToBeRemoved.empty ();
483- }
484-
485- static bool
486- DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock (BasicBlock *BB) {
487- assert (BB->isEntryBlock () && " expected entry block" );
488- SmallVector<DbgVariableRecord *, 8 > ToBeRemoved;
489- DenseSet<DebugVariable> SeenDefForAggregate;
490- // Returns the DebugVariable for DVI with no fragment info.
491- auto GetAggregateVariable = [](const DbgVariableRecord &DVR) {
492- return DebugVariable (DVR.getVariable (), std::nullopt ,
493- DVR.getDebugLoc ().getInlinedAt ());
494- };
495-
496- // Remove undef dbg.assign intrinsics that are encountered before
497- // any non-undef intrinsics from the entry block.
498- for (auto &I : *BB) {
499- for (DbgVariableRecord &DVR : filterDbgVars (I.getDbgRecordRange ())) {
500- if (!DVR.isDbgValue () && !DVR.isDbgAssign ())
501- continue ;
502- bool IsDbgValueKind =
503- (DVR.isDbgValue () || at::getAssignmentInsts (&DVR).empty ());
504- DebugVariable Aggregate = GetAggregateVariable (DVR);
505- if (!SeenDefForAggregate.contains (Aggregate)) {
506- bool IsKill = DVR.isKillLocation () && IsDbgValueKind;
507- if (!IsKill) {
508- SeenDefForAggregate.insert (Aggregate);
509- } else if (DVR.isDbgAssign ()) {
510- ToBeRemoved.push_back (&DVR);
511- }
512- }
468+ DVR.eraseFromParent ();
469+ RemovedAny = true ;
513470 }
514471 }
515472
516- for (DbgVariableRecord *DVR : ToBeRemoved)
517- DVR->eraseFromParent ();
518-
519- return !ToBeRemoved.empty ();
520- }
521-
522- static bool removeRedundantDbgInstrsUsingForwardScan (BasicBlock *BB) {
523- return DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan (BB);
473+ return RemovedAny;
524474}
525475
526476// / Remove redundant undef dbg.assign intrinsic from an entry block using a
@@ -543,7 +493,34 @@ static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
543493// / Possible improvements:
544494// / - Keep track of non-overlapping fragments.
545495static bool removeUndefDbgAssignsFromEntryBlock (BasicBlock *BB) {
546- return DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock (BB);
496+ assert (BB->isEntryBlock () && " expected entry block" );
497+ bool RemovedAny = false ;
498+ DenseSet<DebugVariableAggregate> SeenDefForAggregate;
499+
500+ // Remove undef dbg.assign intrinsics that are encountered before
501+ // any non-undef intrinsics from the entry block.
502+ for (auto &I : *BB) {
503+ for (DbgVariableRecord &DVR :
504+ make_early_inc_range (filterDbgVars (I.getDbgRecordRange ()))) {
505+ if (!DVR.isDbgValue () && !DVR.isDbgAssign ())
506+ continue ;
507+ bool IsDbgValueKind =
508+ (DVR.isDbgValue () || at::getAssignmentInsts (&DVR).empty ());
509+
510+ DebugVariableAggregate Aggregate (&DVR);
511+ if (!SeenDefForAggregate.contains (Aggregate)) {
512+ bool IsKill = DVR.isKillLocation () && IsDbgValueKind;
513+ if (!IsKill) {
514+ SeenDefForAggregate.insert (Aggregate);
515+ } else if (DVR.isDbgAssign ()) {
516+ DVR.eraseFromParent ();
517+ RemovedAny = true ;
518+ }
519+ }
520+ }
521+ }
522+
523+ return RemovedAny;
547524}
548525
549526bool llvm::RemoveRedundantDbgInstrs (BasicBlock *BB) {
0 commit comments