38
38
#include " llvm/ADT/iterator_range.h"
39
39
#include " llvm/Analysis/LoopInfo.h"
40
40
#include " llvm/Analysis/LoopPass.h"
41
+ #include " llvm/Analysis/MemorySSA.h"
42
+ #include " llvm/Analysis/MemorySSAUpdater.h"
41
43
#include " llvm/Analysis/ScalarEvolution.h"
42
44
#include " llvm/Analysis/ScalarEvolutionExpander.h"
43
45
#include " llvm/Analysis/ScalarEvolutionExpressions.h"
@@ -138,6 +140,8 @@ class IndVarSimplify {
138
140
const DataLayout &DL;
139
141
TargetLibraryInfo *TLI;
140
142
const TargetTransformInfo *TTI;
143
+ MemorySSA *MSSA;
144
+ std::unique_ptr<MemorySSAUpdater> MSSAU;
141
145
142
146
SmallVector<WeakTrackingVH, 16 > DeadInsts;
143
147
@@ -162,8 +166,11 @@ class IndVarSimplify {
162
166
public:
163
167
IndVarSimplify (LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
164
168
const DataLayout &DL, TargetLibraryInfo *TLI,
165
- TargetTransformInfo *TTI)
166
- : LI(LI), SE(SE), DT(DT), DL(DL), TLI(TLI), TTI(TTI) {}
169
+ TargetTransformInfo *TTI, MemorySSA *MSSA)
170
+ : LI(LI), SE(SE), DT(DT), DL(DL), TLI(TLI), TTI(TTI), MSSA(MSSA) {
171
+ if (MSSA)
172
+ MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
173
+ }
167
174
168
175
bool run (Loop *L);
169
176
};
@@ -418,11 +425,11 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
418
425
// new comparison.
419
426
NewCompare->takeName (Compare);
420
427
Compare->replaceAllUsesWith (NewCompare);
421
- RecursivelyDeleteTriviallyDeadInstructions (Compare, TLI);
428
+ RecursivelyDeleteTriviallyDeadInstructions (Compare, TLI, MSSAU. get () );
422
429
423
430
// Delete the old floating point increment.
424
431
Incr->replaceAllUsesWith (UndefValue::get (Incr->getType ()));
425
- RecursivelyDeleteTriviallyDeadInstructions (Incr, TLI);
432
+ RecursivelyDeleteTriviallyDeadInstructions (Incr, TLI, MSSAU. get () );
426
433
427
434
// If the FP induction variable still has uses, this is because something else
428
435
// in the loop uses its value. In order to canonicalize the induction
@@ -435,7 +442,7 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
435
442
Value *Conv = new SIToFPInst (NewPHI, PN->getType (), " indvar.conv" ,
436
443
&*PN->getParent ()->getFirstInsertionPt ());
437
444
PN->replaceAllUsesWith (Conv);
438
- RecursivelyDeleteTriviallyDeadInstructions (PN, TLI);
445
+ RecursivelyDeleteTriviallyDeadInstructions (PN, TLI, MSSAU. get () );
439
446
}
440
447
return true ;
441
448
}
@@ -2823,12 +2830,14 @@ PreservedAnalyses IndVarSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
2823
2830
Function *F = L.getHeader ()->getParent ();
2824
2831
const DataLayout &DL = F->getParent ()->getDataLayout ();
2825
2832
2826
- IndVarSimplify IVS (&AR.LI , &AR.SE , &AR.DT , DL, &AR.TLI , &AR.TTI );
2833
+ IndVarSimplify IVS (&AR.LI , &AR.SE , &AR.DT , DL, &AR.TLI , &AR.TTI , AR. MSSA );
2827
2834
if (!IVS.run (&L))
2828
2835
return PreservedAnalyses::all ();
2829
2836
2830
2837
auto PA = getLoopPassPreservedAnalyses ();
2831
2838
PA.preserveSet <CFGAnalyses>();
2839
+ if (AR.MSSA )
2840
+ PA.preserve <MemorySSAAnalysis>();
2832
2841
return PA;
2833
2842
}
2834
2843
@@ -2853,13 +2862,18 @@ struct IndVarSimplifyLegacyPass : public LoopPass {
2853
2862
auto *TTIP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
2854
2863
auto *TTI = TTIP ? &TTIP->getTTI (*L->getHeader ()->getParent ()) : nullptr ;
2855
2864
const DataLayout &DL = L->getHeader ()->getModule ()->getDataLayout ();
2865
+ auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
2866
+ MemorySSA *MSSA = nullptr ;
2867
+ if (MSSAAnalysis)
2868
+ MSSA = &MSSAAnalysis->getMSSA ();
2856
2869
2857
- IndVarSimplify IVS (LI, SE, DT, DL, TLI, TTI);
2870
+ IndVarSimplify IVS (LI, SE, DT, DL, TLI, TTI, MSSA );
2858
2871
return IVS.run (L);
2859
2872
}
2860
2873
2861
2874
void getAnalysisUsage (AnalysisUsage &AU) const override {
2862
2875
AU.setPreservesCFG ();
2876
+ AU.addPreserved <MemorySSAWrapperPass>();
2863
2877
getLoopAnalysisUsage (AU);
2864
2878
}
2865
2879
};
0 commit comments