llvm.org GIT mirror llvm / ca28eb1
[PM] Convert LoopInstSimplify Pass to new PM Summary: Convert LoopInstSimplify to new PM. Unfortunately there is no exisiting unittest for this pass. Reviewers: davidxl, silvas Subscribers: silvas, llvm-commits, mzolotukhin Differential Revision: https://reviews.llvm.org/D22280 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275576 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 4 years ago
6 changed file(s) with 121 addition(s) and 62 deletion(s). Raw diff Collapse all Expand all
180180 void initializeLoopExtractorPass(PassRegistry&);
181181 void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry&);
182182 void initializeLoopInfoWrapperPassPass(PassRegistry&);
183 void initializeLoopInstSimplifyPass(PassRegistry&);
183 void initializeLoopInstSimplifyLegacyPassPass(PassRegistry&);
184184 void initializeLoopInterchangePass(PassRegistry &);
185185 void initializeLoopLoadEliminationPass(PassRegistry&);
186186 void initializeLoopPassPass(PassRegistry&);
0 //===- LoopInstSimplify.h - Loop Inst Simplify Pass -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass performs lightweight instruction simplification on loop bodies.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPINSTSIMPLIFY_H
14 #define LLVM_TRANSFORMS_SCALAR_LOOPINSTSIMPLIFY_H
15
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/PassManager.h"
18
19 namespace llvm {
20
21 /// Performs Loop Inst Simplify Pass.
22 class LoopInstSimplifyPass : public PassInfoMixin {
23 public:
24 PreservedAnalyses run(Loop &L, AnalysisManager &AM);
25 };
26 } // end namespace llvm
27
28 #endif // LLVM_TRANSFORMS_SCALAR_LOOPINSTSIMPLIFY_H
9090 #include "llvm/Transforms/Scalar/LICM.h"
9191 #include "llvm/Transforms/Scalar/LoopDeletion.h"
9292 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
93 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
9394 #include "llvm/Transforms/Scalar/LoopRotation.h"
9495 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
9596 #include "llvm/Transforms/Scalar/LowerAtomic.h"
189189 LOOP_PASS("invalidate", InvalidateAllAnalysesPass())
190190 LOOP_PASS("licm", LICMPass())
191191 LOOP_PASS("loop-idiom", LoopIdiomRecognizePass())
192 LOOP_PASS("loop-instsimplify", LoopInstSimplifyPass())
192193 LOOP_PASS("rotate", LoopRotatePass())
193194 LOOP_PASS("no-op-loop", NoOpLoopPass())
194195 LOOP_PASS("print", PrintLoopPass(dbgs()))
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
1414 #include "llvm/ADT/STLExtras.h"
1515 #include "llvm/ADT/Statistic.h"
1616 #include "llvm/Analysis/AssumptionCache.h"
1717 #include "llvm/Analysis/InstructionSimplify.h"
1818 #include "llvm/Analysis/LoopInfo.h"
1919 #include "llvm/Analysis/LoopPass.h"
20 #include "llvm/Analysis/LoopPassManager.h"
2021 #include "llvm/Analysis/ScalarEvolution.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
2123 #include "llvm/IR/DataLayout.h"
2224 #include "llvm/IR/Dominators.h"
2325 #include "llvm/IR/Instructions.h"
2426 #include "llvm/Support/Debug.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
27 #include "llvm/Transforms/Scalar.h"
2628 #include "llvm/Transforms/Utils/Local.h"
2729 #include "llvm/Transforms/Utils/LoopUtils.h"
2830 using namespace llvm;
3133
3234 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
3335
34 namespace {
35 class LoopInstSimplify : public LoopPass {
36 public:
37 static char ID; // Pass ID, replacement for typeid
38 LoopInstSimplify() : LoopPass(ID) {
39 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
40 }
41
42 bool runOnLoop(Loop*, LPPassManager&) override;
43
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.addRequired();
46 AU.addRequired();
47 AU.setPreservesCFG();
48 getLoopAnalysisUsage(AU);
49 }
50 };
51 }
52
53 char LoopInstSimplify::ID = 0;
54 INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
55 "Simplify instructions in loops", false, false)
56 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
57 INITIALIZE_PASS_DEPENDENCY(LoopPass)
58 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
59 INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
60 "Simplify instructions in loops", false, false)
61
62 Pass *llvm::createLoopInstSimplifyPass() {
63 return new LoopInstSimplify();
64 }
65
66 bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
67 if (skipLoop(L))
68 return false;
69
70 DominatorTreeWrapperPass *DTWP =
71 getAnalysisIfAvailable();
72 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
73 LoopInfo *LI = &getAnalysis().getLoopInfo();
74 const TargetLibraryInfo *TLI =
75 &getAnalysis().getTLI();
76 auto &AC = getAnalysis().getAssumptionCache(
77 *L->getHeader()->getParent());
78
79 SmallVector ExitBlocks;
36 static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
37 AssumptionCache *AC,
38 const TargetLibraryInfo *TLI) {
39 SmallVector ExitBlocks;
8040 L->getUniqueExitBlocks(ExitBlocks);
8141 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
8242
83 SmallPtrSet*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
43 SmallPtrSet *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
8444
8545 // The bit we are stealing from the pointer represents whether this basic
8646 // block is the header of a subloop, in which case we only process its phis.
87 typedef PointerIntPair*, 1> WorklistItem;
47 typedef PointerIntPair *, 1> WorklistItem;
8848 SmallVector VisitStack;
89 SmallPtrSet*, 32> Visited;
49 SmallPtrSet *, 32> Visited;
9050
9151 bool Changed = false;
9252 bool LocalChanged;
11676
11777 // Don't bother simplifying unused instructions.
11878 if (!I->use_empty()) {
119 Value *V = SimplifyInstruction(I, DL, TLI, DT, &AC);
79 Value *V = SimplifyInstruction(I, DL, TLI, DT, AC);
12080 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
12181 // Mark all uses for resimplification next time round the loop.
12282 for (User *U : I->users())
13191 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
13292 // instruction, so simply incrementing the iterator does not work.
13393 // When instructions get deleted re-iterate instead.
134 BI = BB->begin(); BE = BB->end();
94 BI = BB->begin();
95 BE = BB->end();
13596 LocalChanged = true;
13697 }
13798
140101 }
141102
142103 // Add all successors to the worklist, except for loop exit blocks and the
143 // bodies of subloops. We visit the headers of loops so that we can process
144 // their phis, but we contract the rest of the subloop body and only follow
104 // bodies of subloops. We visit the headers of loops so that we can
105 // process
106 // their phis, but we contract the rest of the subloop body and only
107 // follow
145108 // edges leading back to the original loop.
146109 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
147110 ++SI) {
150113 continue;
151114
152115 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
153 if (SuccLoop && SuccLoop->getHeader() == SuccBB
154 && L->contains(SuccLoop)) {
116 if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
117 L->contains(SuccLoop)) {
155118 VisitStack.push_back(WorklistItem(SuccBB, true));
156119
157 SmallVector*, 8> SubLoopExitBlocks;
120 SmallVector *, 8> SubLoopExitBlocks;
158121 SuccLoop->getExitBlocks(SubLoopExitBlocks);
159122
160123 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
166129 continue;
167130 }
168131
169 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
170 ExitBlocks.end(), SuccBB);
132 bool IsExitBlock =
133 std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
171134 if (IsExitBlock)
172135 continue;
173136
185148
186149 return Changed;
187150 }
151
152 namespace {
153 class LoopInstSimplifyLegacyPass : public LoopPass {
154 public:
155 static char ID; // Pass ID, replacement for typeid
156 LoopInstSimplifyLegacyPass() : LoopPass(ID) {
157 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
158 }
159
160 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
161 if (skipLoop(L))
162 return false;
163 DominatorTreeWrapperPass *DTWP =
164 getAnalysisIfAvailable();
165 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
166 LoopInfo *LI = &getAnalysis().getLoopInfo();
167 AssumptionCache *AC =
168 &getAnalysis().getAssumptionCache(
169 *L->getHeader()->getParent());
170 const TargetLibraryInfo *TLI =
171 &getAnalysis().getTLI();
172
173 return SimplifyLoopInst(L, DT, LI, AC, TLI);
174 }
175
176 void getAnalysisUsage(AnalysisUsage &AU) const override {
177 AU.addRequired();
178 AU.addRequired();
179 AU.setPreservesCFG();
180 getLoopAnalysisUsage(AU);
181 }
182 };
183 }
184
185 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L,
186 AnalysisManager &AM) {
187 const auto &FAM =
188 AM.getResult(L).getManager();
189 Function *F = L.getHeader()->getParent();
190
191 // Use getCachedResult because Loop pass cannot trigger a function analysis.
192 auto *DT = FAM.getCachedResult(*F);
193 auto *LI = FAM.getCachedResult(*F);
194 auto *AC = FAM.getCachedResult(*F);
195 const auto *TLI = FAM.getCachedResult(*F);
196 assert((LI && AC && TLI) && "Analyses for Loop Inst Simplify not available");
197
198 if (!SimplifyLoopInst(&L, DT, LI, AC, TLI))
199 return PreservedAnalyses::all();
200
201 return getLoopPassPreservedAnalyses();
202 }
203
204 char LoopInstSimplifyLegacyPass::ID = 0;
205 INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
206 "Simplify instructions in loops", false, false)
207 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
208 INITIALIZE_PASS_DEPENDENCY(LoopPass)
209 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
210 INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
211 "Simplify instructions in loops", false, false)
212
213 Pass *llvm::createLoopInstSimplifyPass() {
214 return new LoopInstSimplifyLegacyPass();
215 }
5252 initializeLoopDataPrefetchPass(Registry);
5353 initializeLoopDeletionLegacyPassPass(Registry);
5454 initializeLoopAccessLegacyAnalysisPass(Registry);
55 initializeLoopInstSimplifyPass(Registry);
55 initializeLoopInstSimplifyLegacyPassPass(Registry);
5656 initializeLoopInterchangePass(Registry);
5757 initializeLoopRotateLegacyPassPass(Registry);
5858 initializeLoopStrengthReducePass(Registry);