Skip to content

Commit adc4faf

Browse files
committed
[IndVarSimplify] Teach IndVarSimplify to preserve MemorySSA.
1 parent c4144ca commit adc4faf

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "llvm/ADT/iterator_range.h"
3939
#include "llvm/Analysis/LoopInfo.h"
4040
#include "llvm/Analysis/LoopPass.h"
41+
#include "llvm/Analysis/MemorySSA.h"
42+
#include "llvm/Analysis/MemorySSAUpdater.h"
4143
#include "llvm/Analysis/ScalarEvolution.h"
4244
#include "llvm/Analysis/ScalarEvolutionExpander.h"
4345
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
@@ -138,6 +140,8 @@ class IndVarSimplify {
138140
const DataLayout &DL;
139141
TargetLibraryInfo *TLI;
140142
const TargetTransformInfo *TTI;
143+
MemorySSA *MSSA;
144+
std::unique_ptr<MemorySSAUpdater> MSSAU;
141145

142146
SmallVector<WeakTrackingVH, 16> DeadInsts;
143147

@@ -162,8 +166,11 @@ class IndVarSimplify {
162166
public:
163167
IndVarSimplify(LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
164168
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+
}
167174

168175
bool run(Loop *L);
169176
};
@@ -418,11 +425,11 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
418425
// new comparison.
419426
NewCompare->takeName(Compare);
420427
Compare->replaceAllUsesWith(NewCompare);
421-
RecursivelyDeleteTriviallyDeadInstructions(Compare, TLI);
428+
RecursivelyDeleteTriviallyDeadInstructions(Compare, TLI, MSSAU.get());
422429

423430
// Delete the old floating point increment.
424431
Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
425-
RecursivelyDeleteTriviallyDeadInstructions(Incr, TLI);
432+
RecursivelyDeleteTriviallyDeadInstructions(Incr, TLI, MSSAU.get());
426433

427434
// If the FP induction variable still has uses, this is because something else
428435
// in the loop uses its value. In order to canonicalize the induction
@@ -435,7 +442,7 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
435442
Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
436443
&*PN->getParent()->getFirstInsertionPt());
437444
PN->replaceAllUsesWith(Conv);
438-
RecursivelyDeleteTriviallyDeadInstructions(PN, TLI);
445+
RecursivelyDeleteTriviallyDeadInstructions(PN, TLI, MSSAU.get());
439446
}
440447
return true;
441448
}
@@ -2823,12 +2830,14 @@ PreservedAnalyses IndVarSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
28232830
Function *F = L.getHeader()->getParent();
28242831
const DataLayout &DL = F->getParent()->getDataLayout();
28252832

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);
28272834
if (!IVS.run(&L))
28282835
return PreservedAnalyses::all();
28292836

28302837
auto PA = getLoopPassPreservedAnalyses();
28312838
PA.preserveSet<CFGAnalyses>();
2839+
if (AR.MSSA)
2840+
PA.preserve<MemorySSAAnalysis>();
28322841
return PA;
28332842
}
28342843

@@ -2853,13 +2862,18 @@ struct IndVarSimplifyLegacyPass : public LoopPass {
28532862
auto *TTIP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
28542863
auto *TTI = TTIP ? &TTIP->getTTI(*L->getHeader()->getParent()) : nullptr;
28552864
const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
2865+
auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
2866+
MemorySSA *MSSA = nullptr;
2867+
if (MSSAAnalysis)
2868+
MSSA = &MSSAAnalysis->getMSSA();
28562869

2857-
IndVarSimplify IVS(LI, SE, DT, DL, TLI, TTI);
2870+
IndVarSimplify IVS(LI, SE, DT, DL, TLI, TTI, MSSA);
28582871
return IVS.run(L);
28592872
}
28602873

28612874
void getAnalysisUsage(AnalysisUsage &AU) const override {
28622875
AU.setPreservesCFG();
2876+
AU.addPreserved<MemorySSAWrapperPass>();
28632877
getLoopAnalysisUsage(AU);
28642878
}
28652879
};

0 commit comments

Comments
 (0)