llvm.org GIT mirror llvm / fa907d3
[PM] Port Dead Loop Deletion Pass to the new PM Summary: Port Dead Loop Deletion Pass to the new pass manager. Reviewers: silvas, davide Subscribers: llvm-commits, sanjoy, mcrosier Differential Revision: https://reviews.llvm.org/D21483 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275453 91177308-0d34-0410-b5e6-96231b3b80d8 Jun Bum Lim 3 years ago
7 changed file(s) with 115 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
174174 void initializeLocalStackSlotPassPass(PassRegistry&);
175175 void initializeLoopAccessLegacyAnalysisPass(PassRegistry&);
176176 void initializeLoopDataPrefetchPass(PassRegistry&);
177 void initializeLoopDeletionPass(PassRegistry&);
177 void initializeLoopDeletionLegacyPassPass(PassRegistry&);
178178 void initializeLoopDistributePass(PassRegistry&);
179179 void initializeLoopExtractorPass(PassRegistry&);
180180 void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry&);
0 //===- LoopDeletion.h - Loop Deletion -------------------------------------===//
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 file provides the interface for the Loop Deletion Pass.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPDELETION_H
14 #define LLVM_TRANSFORMS_SCALAR_LOOPDELETION_H
15
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/ScalarEvolution.h"
18 #include "llvm/IR/PassManager.h"
19
20 namespace llvm {
21
22 class LoopDeletionPass : public PassInfoMixin {
23 public:
24 LoopDeletionPass() {}
25 PreservedAnalyses run(Loop &L, AnalysisManager &AM);
26 bool runImpl(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
27 LoopInfo &loopInfo);
28
29 private:
30 bool isLoopDead(Loop *L, ScalarEvolution &SE,
31 SmallVectorImpl &exitingBlocks,
32 SmallVectorImpl &exitBlocks, bool &Changed,
33 BasicBlock *Preheader);
34 };
35 }
36
37 #endif // LLVM_TRANSFORMS_SCALAR_LOOPDELETION_H
8888 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
8989 #include "llvm/Transforms/Scalar/JumpThreading.h"
9090 #include "llvm/Transforms/Scalar/LICM.h"
91 #include "llvm/Transforms/Scalar/LoopDeletion.h"
9192 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
9293 #include "llvm/Transforms/Scalar/LoopRotation.h"
9394 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
191191 LOOP_PASS("rotate", LoopRotatePass())
192192 LOOP_PASS("no-op-loop", NoOpLoopPass())
193193 LOOP_PASS("print", PrintLoopPass(dbgs()))
194 LOOP_PASS("loop-deletion", LoopDeletionPass())
194195 LOOP_PASS("simplify-cfg", LoopSimplifyCFGPass())
195196 LOOP_PASS("indvars", IndVarSimplifyPass())
196197 LOOP_PASS("print-access-info", LoopAccessInfoPrinterPass(dbgs()))
1313 //
1414 //===----------------------------------------------------------------------===//
1515
16 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Transforms/Scalar/LoopDeletion.h"
1717 #include "llvm/ADT/SmallVector.h"
1818 #include "llvm/ADT/Statistic.h"
1919 #include "llvm/Analysis/GlobalsModRef.h"
2020 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/ScalarEvolution.h"
21 #include "llvm/Analysis/LoopPassManager.h"
2222 #include "llvm/IR/Dominators.h"
23 #include "llvm/Transforms/Scalar.h"
2324 #include "llvm/Transforms/Utils/LoopUtils.h"
2425 using namespace llvm;
2526
2627 #define DEBUG_TYPE "loop-delete"
2728
2829 STATISTIC(NumDeleted, "Number of loops deleted");
29
30 namespace {
31 class LoopDeletion : public LoopPass {
32 public:
33 static char ID; // Pass ID, replacement for typeid
34 LoopDeletion() : LoopPass(ID) {
35 initializeLoopDeletionPass(*PassRegistry::getPassRegistry());
36 }
37
38 // Possibly eliminate loop L if it is dead.
39 bool runOnLoop(Loop *L, LPPassManager &) override;
40
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 getLoopAnalysisUsage(AU);
43 }
44
45 private:
46 bool isLoopDead(Loop *L, ScalarEvolution &SE,
47 SmallVectorImpl &exitingBlocks,
48 SmallVectorImpl &exitBlocks, bool &Changed,
49 BasicBlock *Preheader);
50 };
51 }
52
53 char LoopDeletion::ID = 0;
54 INITIALIZE_PASS_BEGIN(LoopDeletion, "loop-deletion",
55 "Delete dead loops", false, false)
56 INITIALIZE_PASS_DEPENDENCY(LoopPass)
57 INITIALIZE_PASS_END(LoopDeletion, "loop-deletion",
58 "Delete dead loops", false, false)
59
60 Pass *llvm::createLoopDeletionPass() {
61 return new LoopDeletion();
62 }
6330
6431 /// isLoopDead - Determined if a loop is dead. This assumes that we've already
6532 /// checked for unique exit and exiting blocks, and that the code is in LCSSA
6633 /// form.
67 bool LoopDeletion::isLoopDead(Loop *L, ScalarEvolution &SE,
68 SmallVectorImpl &exitingBlocks,
69 SmallVectorImpl &exitBlocks,
70 bool &Changed, BasicBlock *Preheader) {
34 bool LoopDeletionPass::isLoopDead(Loop *L, ScalarEvolution &SE,
35 SmallVectorImpl &exitingBlocks,
36 SmallVectorImpl &exitBlocks,
37 bool &Changed, BasicBlock *Preheader) {
7138 BasicBlock *exitBlock = exitBlocks[0];
7239
7340 // Make sure that all PHI entries coming from the loop are loop invariant.
12390 return true;
12491 }
12592
126 /// runOnLoop - Remove dead loops, by which we mean loops that do not impact the
127 /// observable behavior of the program other than finite running time. Note
128 /// we do ensure that this never remove a loop that might be infinite, as doing
129 /// so could change the halting/non-halting nature of a program.
130 /// NOTE: This entire process relies pretty heavily on LoopSimplify and LCSSA
131 /// in order to make various safety checks work.
132 bool LoopDeletion::runOnLoop(Loop *L, LPPassManager &) {
133 if (skipLoop(L))
134 return false;
135
136 DominatorTree &DT = getAnalysis().getDomTree();
93 /// Remove dead loops, by which we mean loops that do not impact the observable
94 /// behavior of the program other than finite running time. Note we do ensure
95 /// that this never remove a loop that might be infinite, as doing so could
96 /// change the halting/non-halting nature of a program. NOTE: This entire
97 /// process relies pretty heavily on LoopSimplify and LCSSA in order to make
98 /// various safety checks work.
99 bool LoopDeletionPass::runImpl(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
100 LoopInfo &loopInfo) {
137101 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
138102
139103 // We can only remove the loop if there is a preheader that we can
151115 if (L->begin() != L->end())
152116 return false;
153117
154 SmallVector*, 4> exitingBlocks;
118 SmallVector *, 4> exitingBlocks;
155119 L->getExitingBlocks(exitingBlocks);
156120
157 SmallVector*, 4> exitBlocks;
121 SmallVector *, 4> exitBlocks;
158122 L->getUniqueExitBlocks(exitBlocks);
159123
160124 // We require that the loop only have a single exit block. Otherwise, we'd
163127 // a loop invariant manner.
164128 if (exitBlocks.size() != 1)
165129 return false;
166
167 ScalarEvolution &SE = getAnalysis().getSE();
168130
169131 // Finally, we have to check that the loop really is dead.
170132 bool Changed = false;
237199
238200 // Finally, the blocks from loopinfo. This has to happen late because
239201 // otherwise our loop iterators won't work.
240 LoopInfo &loopInfo = getAnalysis().getLoopInfo();
241 SmallPtrSet blocks;
202
203 SmallPtrSet blocks;
242204 blocks.insert(L->block_begin(), L->block_end());
243205 for (BasicBlock *BB : blocks)
244206 loopInfo.removeBlock(BB);
251213
252214 return Changed;
253215 }
216
217 PreservedAnalyses LoopDeletionPass::run(Loop &L, AnalysisManager &AM) {
218 auto &FAM = AM.getResult(L).getManager();
219 Function *F = L.getHeader()->getParent();
220
221 auto &DT = *FAM.getCachedResult(*F);
222 auto &SE = *FAM.getCachedResult(*F);
223 auto &LI = *FAM.getCachedResult(*F);
224
225 bool Changed = runImpl(&L, DT, SE, LI);
226 if (!Changed)
227 return PreservedAnalyses::all();
228
229 return getLoopPassPreservedAnalyses();
230 }
231
232 namespace {
233 class LoopDeletionLegacyPass : public LoopPass {
234 public:
235 static char ID; // Pass ID, replacement for typeid
236 LoopDeletionLegacyPass() : LoopPass(ID) {
237 initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry());
238 }
239
240 // Possibly eliminate loop L if it is dead.
241 bool runOnLoop(Loop *L, LPPassManager &) override;
242
243 void getAnalysisUsage(AnalysisUsage &AU) const override {
244 getLoopAnalysisUsage(AU);
245 }
246 };
247 }
248
249 char LoopDeletionLegacyPass::ID = 0;
250 INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion",
251 "Delete dead loops", false, false)
252 INITIALIZE_PASS_DEPENDENCY(LoopPass)
253 INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion",
254 "Delete dead loops", false, false)
255
256 Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); }
257
258 bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) {
259 if (skipLoop(L))
260 return false;
261
262 DominatorTree &DT = getAnalysis().getDomTree();
263 ScalarEvolution &SE = getAnalysis().getSE();
264 LoopInfo &loopInfo = getAnalysis().getLoopInfo();
265
266 LoopDeletionPass Impl;
267 return Impl.runImpl(L, DT, SE, loopInfo);
268 }
4949 initializeJumpThreadingPass(Registry);
5050 initializeLegacyLICMPassPass(Registry);
5151 initializeLoopDataPrefetchPass(Registry);
52 initializeLoopDeletionPass(Registry);
52 initializeLoopDeletionLegacyPassPass(Registry);
5353 initializeLoopAccessLegacyAnalysisPass(Registry);
5454 initializeLoopInstSimplifyPass(Registry);
5555 initializeLoopInterchangePass(Registry);
0 ; RUN: opt < %s -loop-deletion -S | FileCheck %s
1 ; RUN: opt < %s -passes='require,loop(loop-deletion)' -S | FileCheck %s
12
23 ; ScalarEvolution can prove the loop iteration is finite, even though
34 ; it can't represent the exact trip count as an expression. That's