@@ -876,6 +876,12 @@ void GVNPass::printPipeline(
876
876
OS << ' >' ;
877
877
}
878
878
879
+ void GVNPass::salvageAndRemoveInstruction (Instruction *I) {
880
+ salvageKnowledge (I, AC);
881
+ salvageDebugInfo (*I);
882
+ removeInstruction (I);
883
+ }
884
+
879
885
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
880
886
LLVM_DUMP_METHOD void GVNPass::dump (DenseMap<uint32_t , Value *> &Map) const {
881
887
errs () << " {\n " ;
@@ -1555,7 +1561,6 @@ void GVNPass::eliminatePartiallyRedundantLoad(
1555
1561
replaceValuesPerBlockEntry (ValuesPerBlock, OldLoad, NewLoad);
1556
1562
if (uint32_t ValNo = VN.lookup (OldLoad, false ))
1557
1563
LeaderTable.erase (ValNo, OldLoad, OldLoad->getParent ());
1558
- VN.erase (OldLoad);
1559
1564
removeInstruction (OldLoad);
1560
1565
}
1561
1566
}
@@ -1572,11 +1577,11 @@ void GVNPass::eliminatePartiallyRedundantLoad(
1572
1577
I->setDebugLoc (Load->getDebugLoc ());
1573
1578
if (V->getType ()->isPtrOrPtrVectorTy ())
1574
1579
MD->invalidateCachedPointerInfo (V);
1575
- markInstructionForDeletion (Load);
1576
1580
ORE->emit ([&]() {
1577
1581
return OptimizationRemark (DEBUG_TYPE, " LoadPRE" , Load)
1578
1582
<< " load eliminated by PRE" ;
1579
1583
});
1584
+ salvageAndRemoveInstruction (Load);
1580
1585
}
1581
1586
1582
1587
bool GVNPass::PerformLoadPRE (LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
@@ -1795,7 +1800,7 @@ bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1795
1800
// Erase instructions generated by the failed PHI translation before
1796
1801
// trying to number them. PHI translation might insert instructions
1797
1802
// in basic blocks other than the current one, and we delete them
1798
- // directly, as markInstructionForDeletion only allows removing from the
1803
+ // directly, as salvageAndRemoveInstruction only allows removing from the
1799
1804
// current basic block.
1800
1805
NewInsts.pop_back_val ()->eraseFromParent ();
1801
1806
}
@@ -1994,9 +1999,9 @@ bool GVNPass::processNonLocalLoad(LoadInst *Load) {
1994
1999
I->setDebugLoc (Load->getDebugLoc ());
1995
2000
if (V->getType ()->isPtrOrPtrVectorTy ())
1996
2001
MD->invalidateCachedPointerInfo (V);
1997
- markInstructionForDeletion (Load);
1998
2002
++NumGVNLoad;
1999
2003
reportLoadElim (Load, V, ORE);
2004
+ salvageAndRemoveInstruction (Load);
2000
2005
return true ;
2001
2006
}
2002
2007
@@ -2064,7 +2069,7 @@ bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
2064
2069
}
2065
2070
}
2066
2071
if (isAssumeWithEmptyBundle (*IntrinsicI)) {
2067
- markInstructionForDeletion (IntrinsicI);
2072
+ salvageAndRemoveInstruction (IntrinsicI);
2068
2073
return true ;
2069
2074
}
2070
2075
return false ;
@@ -2175,7 +2180,7 @@ bool GVNPass::processLoad(LoadInst *L) {
2175
2180
return false ;
2176
2181
2177
2182
if (L->use_empty ()) {
2178
- markInstructionForDeletion (L);
2183
+ salvageAndRemoveInstruction (L);
2179
2184
return true ;
2180
2185
}
2181
2186
@@ -2205,11 +2210,11 @@ bool GVNPass::processLoad(LoadInst *L) {
2205
2210
// MaterializeAdjustedValue is responsible for combining metadata.
2206
2211
ICF->removeUsersOf (L);
2207
2212
L->replaceAllUsesWith (AvailableValue);
2208
- markInstructionForDeletion (L);
2209
2213
if (MSSAU)
2210
2214
MSSAU->removeMemoryAccess (L);
2211
2215
++NumGVNLoad;
2212
2216
reportLoadElim (L, AvailableValue, ORE);
2217
+ salvageAndRemoveInstruction (L);
2213
2218
// Tell MDA to reexamine the reused pointer since we might have more
2214
2219
// information after forwarding it.
2215
2220
if (MD && AvailableValue->getType ()->isPtrOrPtrVectorTy ())
@@ -2601,7 +2606,7 @@ bool GVNPass::processInstruction(Instruction *I) {
2601
2606
Changed = true ;
2602
2607
}
2603
2608
if (isInstructionTriviallyDead (I, TLI)) {
2604
- markInstructionForDeletion (I);
2609
+ salvageAndRemoveInstruction (I);
2605
2610
Changed = true ;
2606
2611
}
2607
2612
if (Changed) {
@@ -2718,7 +2723,7 @@ bool GVNPass::processInstruction(Instruction *I) {
2718
2723
patchAndReplaceAllUsesWith (I, Repl);
2719
2724
if (MD && Repl->getType ()->isPtrOrPtrVectorTy ())
2720
2725
MD->invalidateCachedPointerInfo (Repl);
2721
- markInstructionForDeletion (I);
2726
+ salvageAndRemoveInstruction (I);
2722
2727
return true ;
2723
2728
}
2724
2729
@@ -2794,10 +2799,6 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2794
2799
}
2795
2800
2796
2801
bool GVNPass::processBlock (BasicBlock *BB) {
2797
- // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2798
- // (and incrementing BI before processing an instruction).
2799
- assert (InstrsToErase.empty () &&
2800
- " We expect InstrsToErase to be empty across iterations" );
2801
2802
if (DeadBlocks.count (BB))
2802
2803
return false ;
2803
2804
@@ -2812,44 +2813,13 @@ bool GVNPass::processBlock(BasicBlock *BB) {
2812
2813
SmallPtrSet<PHINode *, 8 > PHINodesToRemove;
2813
2814
ChangedFunction |= EliminateDuplicatePHINodes (BB, PHINodesToRemove);
2814
2815
for (PHINode *PN : PHINodesToRemove) {
2815
- VN.erase (PN);
2816
2816
removeInstruction (PN);
2817
2817
}
2818
-
2819
- for (BasicBlock::iterator BI = BB->begin (), BE = BB->end ();
2820
- BI != BE;) {
2818
+ for (Instruction &Inst : make_early_inc_range (*BB)) {
2821
2819
if (!ReplaceOperandsWithMap.empty ())
2822
- ChangedFunction |= replaceOperandsForInBlockEquality (&*BI);
2823
- ChangedFunction |= processInstruction (&*BI);
2824
-
2825
- if (InstrsToErase.empty ()) {
2826
- ++BI;
2827
- continue ;
2828
- }
2829
-
2830
- // If we need some instructions deleted, do it now.
2831
- NumGVNInstr += InstrsToErase.size ();
2832
-
2833
- // Avoid iterator invalidation.
2834
- bool AtStart = BI == BB->begin ();
2835
- if (!AtStart)
2836
- --BI;
2837
-
2838
- for (auto *I : InstrsToErase) {
2839
- assert (I->getParent () == BB && " Removing instruction from wrong block?" );
2840
- LLVM_DEBUG (dbgs () << " GVN removed: " << *I << ' \n ' );
2841
- salvageKnowledge (I, AC);
2842
- salvageDebugInfo (*I);
2843
- removeInstruction (I);
2844
- }
2845
- InstrsToErase.clear ();
2846
-
2847
- if (AtStart)
2848
- BI = BB->begin ();
2849
- else
2850
- ++BI;
2820
+ ChangedFunction |= replaceOperandsForInBlockEquality (&Inst);
2821
+ ChangedFunction |= processInstruction (&Inst);
2851
2822
}
2852
-
2853
2823
return ChangedFunction;
2854
2824
}
2855
2825
@@ -3055,7 +3025,6 @@ bool GVNPass::performScalarPRE(Instruction *CurInst) {
3055
3025
CurInst->replaceAllUsesWith (Phi);
3056
3026
if (MD && Phi->getType ()->isPtrOrPtrVectorTy ())
3057
3027
MD->invalidateCachedPointerInfo (Phi);
3058
- VN.erase (CurInst);
3059
3028
LeaderTable.erase (ValNo, CurInst, CurrentBlock);
3060
3029
3061
3030
LLVM_DEBUG (dbgs () << " GVN PRE removed: " << *CurInst << ' \n ' );
@@ -3155,6 +3124,7 @@ void GVNPass::cleanupGlobalSets() {
3155
3124
}
3156
3125
3157
3126
void GVNPass::removeInstruction (Instruction *I) {
3127
+ VN.erase (I);
3158
3128
if (MD) MD->removeInstruction (I);
3159
3129
if (MSSAU)
3160
3130
MSSAU->removeMemoryAccess (I);
0 commit comments