blob: eca5ffe3ad1042a09b7f3ba33a6d6aa282ec5dec [file] [log] [blame]
Owen Anderson2306a1e2008-04-29 20:06:54 +00001//===- LoopDeletion.cpp - Dead Loop Deletion Pass ---------------===//
Owen Anderson94ad7022008-04-29 00:38:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksen829046b2008-05-08 17:46:35 +000010// This file implements the Dead Loop Deletion Pass. This pass is responsible
11// for eliminating loops with non-infinite computable trip counts that have no
12// side effects or volatile instructions, and do not contribute to the
13// computation of the function's return value.
Owen Anderson94ad7022008-04-29 00:38:34 +000014//
15//===----------------------------------------------------------------------===//
16
Jun Bum Limc837af32016-07-14 18:28:29 +000017#include "llvm/Transforms/Scalar/LoopDeletion.h"
Owen Anderson94ad7022008-04-29 00:38:34 +000018#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/Statistic.h"
James Molloyefbba722015-09-10 10:22:12 +000020#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Analysis/LoopPass.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Dominators.h"
Anna Thomas53c8d952017-05-03 11:47:11 +000023#include "llvm/IR/PatternMatch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +000024#include "llvm/Transforms/Scalar.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000025#include "llvm/Transforms/Scalar/LoopPassManager.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000026#include "llvm/Transforms/Utils/LoopUtils.h"
Owen Anderson94ad7022008-04-29 00:38:34 +000027using namespace llvm;
28
Chandler Carruth964daaa2014-04-22 02:55:47 +000029#define DEBUG_TYPE "loop-delete"
30
Owen Anderson94ad7022008-04-29 00:38:34 +000031STATISTIC(NumDeleted, "Number of loops deleted");
32
Anna Thomas53c8d952017-05-03 11:47:11 +000033/// This function deletes dead loops. The caller of this function needs to
Anna Thomase7cb6332017-06-25 21:13:58 +000034/// guarantee that the loop is infact dead. Here we handle two kinds of dead
Anna Thomas53c8d952017-05-03 11:47:11 +000035/// loop. The first kind (\p isLoopDead) is where only invariant values from
36/// within the loop are used outside of it. The second kind (\p
37/// isLoopNeverExecuted) is where the loop is provably never executed. We can
Anna Thomase7cb6332017-06-25 21:13:58 +000038/// always remove never executed loops since they will not cause any difference
39/// to program behaviour.
Anna Thomas53c8d952017-05-03 11:47:11 +000040///
41/// This also updates the relevant analysis information in \p DT, \p SE, and \p
42/// LI. It also updates the loop PM if an updater struct is provided.
43// TODO: This function will be used by loop-simplifyCFG as well. So, move this
44// to LoopUtils.cpp
45static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
Anna Thomase7cb6332017-06-25 21:13:58 +000046 LoopInfo &LI, LPMUpdater *Updater = nullptr);
Chandler Carruth26169f002017-01-17 22:07:26 +000047/// Determines if a loop is dead.
48///
49/// This assumes that we've already checked for unique exit and exiting blocks,
50/// and that the code is in LCSSA form.
51static bool isLoopDead(Loop *L, ScalarEvolution &SE,
52 SmallVectorImpl<BasicBlock *> &ExitingBlocks,
53 BasicBlock *ExitBlock, bool &Changed,
54 BasicBlock *Preheader) {
Owen Anderson94ad7022008-04-29 00:38:34 +000055 // Make sure that all PHI entries coming from the loop are loop invariant.
Owen Andersone6746002008-04-29 20:59:33 +000056 // Because the code is in LCSSA form, any values used outside of the loop
57 // must pass through a PHI in the exit block, meaning that this check is
58 // sufficient to guarantee that no loop-variant values are used outside
59 // of the loop.
Chandler Carruth04a73872017-01-17 21:51:39 +000060 BasicBlock::iterator BI = ExitBlock->begin();
Sanjoy Das905fc272016-05-03 17:50:02 +000061 bool AllEntriesInvariant = true;
62 bool AllOutgoingValuesSame = true;
Jakub Staszakbc421ef2013-03-18 23:31:30 +000063 while (PHINode *P = dyn_cast<PHINode>(BI)) {
Chandler Carruth04a73872017-01-17 21:51:39 +000064 Value *incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
Cameron Zwarich82630852011-02-22 22:25:39 +000065
66 // Make sure all exiting blocks produce the same incoming value for the exit
67 // block. If there are different incoming values for different exiting
68 // blocks, then it is impossible to statically determine which value should
69 // be used.
Sanjoy Das7e7a5a02016-05-03 17:50:06 +000070 AllOutgoingValuesSame =
Chandler Carruth04a73872017-01-17 21:51:39 +000071 all_of(makeArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) {
Sanjoy Das7e7a5a02016-05-03 17:50:06 +000072 return incoming == P->getIncomingValueForBlock(BB);
73 });
Nadav Rotem465834c2012-07-24 10:51:42 +000074
Sanjoy Das905fc272016-05-03 17:50:02 +000075 if (!AllOutgoingValuesSame)
76 break;
77
Jakub Staszakbc421ef2013-03-18 23:31:30 +000078 if (Instruction *I = dyn_cast<Instruction>(incoming))
Sanjoy Das905fc272016-05-03 17:50:02 +000079 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) {
80 AllEntriesInvariant = false;
81 break;
82 }
Cameron Zwarich82630852011-02-22 22:25:39 +000083
Dan Gohmand2d1ae12010-06-22 15:08:57 +000084 ++BI;
Owen Anderson94ad7022008-04-29 00:38:34 +000085 }
Nadav Rotem465834c2012-07-24 10:51:42 +000086
Sanjoy Das905fc272016-05-03 17:50:02 +000087 if (Changed)
88 SE.forgetLoopDispositions(L);
89
90 if (!AllEntriesInvariant || !AllOutgoingValuesSame)
91 return false;
92
Owen Anderson94ad7022008-04-29 00:38:34 +000093 // Make sure that no instructions in the block have potential side-effects.
Owen Andersone6746002008-04-29 20:59:33 +000094 // This includes instructions that could write to memory, and loads that are
Xin Tongdf4dff32017-02-23 23:47:10 +000095 // marked volatile.
Davide Italiano49a0aac2017-02-26 07:08:20 +000096 for (auto &I : L->blocks())
97 if (any_of(*I, [](Instruction &I) { return I.mayHaveSideEffects(); }))
98 return false;
Owen Anderson94ad7022008-04-29 00:38:34 +000099 return true;
100}
101
Anna Thomas53c8d952017-05-03 11:47:11 +0000102/// This function returns true if there is no viable path from the
103/// entry block to the header of \p L. Right now, it only does
104/// a local search to save compile time.
105static bool isLoopNeverExecuted(Loop *L) {
106 using namespace PatternMatch;
107
108 auto *Preheader = L->getLoopPreheader();
109 // TODO: We can relax this constraint, since we just need a loop
110 // predecessor.
111 assert(Preheader && "Needs preheader!");
112
113 if (Preheader == &Preheader->getParent()->getEntryBlock())
114 return false;
115 // All predecessors of the preheader should have a constant conditional
116 // branch, with the loop's preheader as not-taken.
117 for (auto *Pred: predecessors(Preheader)) {
118 BasicBlock *Taken, *NotTaken;
119 ConstantInt *Cond;
120 if (!match(Pred->getTerminator(),
121 m_Br(m_ConstantInt(Cond), Taken, NotTaken)))
122 return false;
123 if (!Cond->getZExtValue())
124 std::swap(Taken, NotTaken);
125 if (Taken == Preheader)
126 return false;
127 }
128 assert(!pred_empty(Preheader) &&
129 "Preheader should have predecessors at this point!");
130 // All the predecessors have the loop preheader as not-taken target.
131 return true;
132}
133
Chandler Carruth26169f002017-01-17 22:07:26 +0000134/// Remove a loop if it is dead.
135///
136/// A loop is considered dead if it does not impact the observable behavior of
137/// the program other than finite running time. This never removes a loop that
Anna Thomas53c8d952017-05-03 11:47:11 +0000138/// might be infinite (unless it is never executed), as doing so could change
139/// the halting/non-halting nature of a program.
Chandler Carruth26169f002017-01-17 22:07:26 +0000140///
141/// This entire process relies pretty heavily on LoopSimplify form and LCSSA in
142/// order to make various safety checks work.
143///
Chandler Carruth027340f2017-02-11 00:09:30 +0000144/// \returns true if any changes were made. This may mutate the loop even if it
145/// is unable to delete it due to hoisting trivially loop invariant
146/// instructions out of the loop.
Chandler Carruth26169f002017-01-17 22:07:26 +0000147static bool deleteLoopIfDead(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
Chandler Carruth027340f2017-02-11 00:09:30 +0000148 LoopInfo &LI, LPMUpdater *Updater = nullptr) {
Sanjoy Das979a11d2016-02-21 17:11:59 +0000149 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
150
Anna Thomas90f69ab2017-07-04 14:05:19 +0000151 // We can only remove the loop if there is a preheader that we can branch from
152 // after removing it. Also, if LoopSimplify form is not available, stay out
153 // of trouble.
Chandler Carruthaa885c92017-01-17 22:19:56 +0000154 BasicBlock *Preheader = L->getLoopPreheader();
Anna Thomas90f69ab2017-07-04 14:05:19 +0000155 if (!Preheader || !L->hasDedicatedExits()) {
156 DEBUG(dbgs()
157 << "Deletion requires Loop with preheader and dedicated exits.\n");
Owen Anderson94ad7022008-04-29 00:38:34 +0000158 return false;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000159 }
Owen Anderson94ad7022008-04-29 00:38:34 +0000160 // We can't remove loops that contain subloops. If the subloops were dead,
161 // they would already have been removed in earlier executions of this pass.
Anna Thomas90f69ab2017-07-04 14:05:19 +0000162 if (L->begin() != L->end()) {
163 DEBUG(dbgs() << "Loop contains subloops.\n");
Owen Anderson94ad7022008-04-29 00:38:34 +0000164 return false;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000165 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000166
Anna Thomas53c8d952017-05-03 11:47:11 +0000167
168 BasicBlock *ExitBlock = L->getUniqueExitBlock();
169
170 if (ExitBlock && isLoopNeverExecuted(L)) {
Anna Thomas90f69ab2017-07-04 14:05:19 +0000171 DEBUG(dbgs() << "Loop is proven to never execute, delete it!");
Anna Thomase7cb6332017-06-25 21:13:58 +0000172 // Set incoming value to undef for phi nodes in the exit block.
173 BasicBlock::iterator BI = ExitBlock->begin();
174 while (PHINode *P = dyn_cast<PHINode>(BI)) {
175 for (unsigned i = 0; i < P->getNumIncomingValues(); i++)
176 P->setIncomingValue(i, UndefValue::get(P->getType()));
177 BI++;
178 }
179 deleteDeadLoop(L, DT, SE, LI, Updater);
Anna Thomas53c8d952017-05-03 11:47:11 +0000180 ++NumDeleted;
181 return true;
182 }
183
184 // The remaining checks below are for a loop being dead because all statements
185 // in the loop are invariant.
Chandler Carruth04a73872017-01-17 21:51:39 +0000186 SmallVector<BasicBlock *, 4> ExitingBlocks;
187 L->getExitingBlocks(ExitingBlocks);
Nadav Rotem465834c2012-07-24 10:51:42 +0000188
Owen Andersonad5f2112008-05-16 04:32:45 +0000189 // We require that the loop only have a single exit block. Otherwise, we'd
190 // be in the situation of needing to be able to solve statically which exit
191 // block will be branched to, or trying to preserve the branching logic in
192 // a loop invariant manner.
Anna Thomas90f69ab2017-07-04 14:05:19 +0000193 if (!ExitBlock) {
194 DEBUG(dbgs() << "Deletion requires single exit block\n");
Owen Andersone6746002008-04-29 20:59:33 +0000195 return false;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000196 }
Owen Anderson94ad7022008-04-29 00:38:34 +0000197 // Finally, we have to check that the loop really is dead.
Chandler Carruth027340f2017-02-11 00:09:30 +0000198 bool Changed = false;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000199 if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) {
200 DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n");
Chandler Carruth027340f2017-02-11 00:09:30 +0000201 return Changed;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000202 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000203
Owen Andersonad5f2112008-05-16 04:32:45 +0000204 // Don't remove loops for which we can't solve the trip count.
205 // They could be infinite, in which case we'd be changing program behavior.
Dan Gohman41d00ac2009-10-23 17:10:01 +0000206 const SCEV *S = SE.getMaxBackedgeTakenCount(L);
Anna Thomas90f69ab2017-07-04 14:05:19 +0000207 if (isa<SCEVCouldNotCompute>(S)) {
208 DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount.\n");
Chandler Carruth027340f2017-02-11 00:09:30 +0000209 return Changed;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000210 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000211
Anna Thomas90f69ab2017-07-04 14:05:19 +0000212 DEBUG(dbgs() << "Loop is invariant, delete it!");
Anna Thomase7cb6332017-06-25 21:13:58 +0000213 deleteDeadLoop(L, DT, SE, LI, Updater);
Anna Thomas53c8d952017-05-03 11:47:11 +0000214 ++NumDeleted;
215
216 return true;
217}
218
219static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
Anna Thomase7cb6332017-06-25 21:13:58 +0000220 LoopInfo &LI, LPMUpdater *Updater) {
Anna Thomas53c8d952017-05-03 11:47:11 +0000221 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
222 auto *Preheader = L->getLoopPreheader();
223 assert(Preheader && "Preheader should exist!");
224
Owen Andersone6746002008-04-29 20:59:33 +0000225 // Now that we know the removal is safe, remove the loop by changing the
Nadav Rotem465834c2012-07-24 10:51:42 +0000226 // branch from the preheader to go to the single exit block.
Chandler Carruthbb7e4b42017-01-17 22:00:52 +0000227 //
Owen Anderson586216e2008-04-29 00:45:15 +0000228 // Because we're deleting a large chunk of code at once, the sequence in which
Chandler Carruthbd551e92017-01-17 22:09:28 +0000229 // we remove things is very important to avoid invalidation issues.
Dan Gohman7bb31732009-07-08 19:14:29 +0000230
Chandler Carruth027340f2017-02-11 00:09:30 +0000231 // If we have an LPM updater, tell it about the loop being removed.
232 if (Updater)
233 Updater->markLoopAsDeleted(*L);
234
Dan Gohman7bb31732009-07-08 19:14:29 +0000235 // Tell ScalarEvolution that the loop is deleted. Do this before
236 // deleting the loop so that ScalarEvolution can look at the loop
237 // to determine what it needs to clean up.
Dan Gohman880c92a2009-10-31 15:04:55 +0000238 SE.forgetLoop(L);
Dan Gohman7bb31732009-07-08 19:14:29 +0000239
Anna Thomas53c8d952017-05-03 11:47:11 +0000240 auto *ExitBlock = L->getUniqueExitBlock();
241 assert(ExitBlock && "Should have a unique exit block!");
Anna Thomas72c90c82017-06-22 20:20:56 +0000242 assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
243
Jakub Kuderskid8699132017-08-02 18:17:52 +0000244 auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
245 assert(OldBr && "Preheader must end with a branch");
246 assert(OldBr->isUnconditional() && "Preheader must have a single successor");
247 // Connect the preheader to the exit block. Keep the old edge to the header
248 // around to perform the dominator tree update in two separate steps
249 // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
250 // preheader -> header.
251 //
252 //
253 // 0. Preheader 1. Preheader 2. Preheader
254 // | | | |
255 // V | V |
256 // Header <--\ | Header <--\ | Header <--\
257 // | | | | | | | | | | |
258 // | V | | | V | | | V |
259 // | Body --/ | | Body --/ | | Body --/
260 // V V V V V
261 // Exit Exit Exit
262 //
263 // By doing this is two separate steps we can perform the dominator tree
264 // update without using the batch update API.
265 //
Anna Thomas53c8d952017-05-03 11:47:11 +0000266 // Even when the loop is never executed, we cannot remove the edge from the
267 // source block to the exit block. Consider the case where the unexecuted loop
268 // branches back to an outer loop. If we deleted the loop and removed the edge
269 // coming to this inner loop, this will break the outer loop structure (by
270 // deleting the backedge of the outer loop). If the outer loop is indeed a
271 // non-loop, it will be deleted in a future iteration of loop deletion pass.
Jakub Kuderskid8699132017-08-02 18:17:52 +0000272 IRBuilder<> Builder(OldBr);
273 Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
274 // Remove the old branch. The conditional branch becomes a new terminator.
275 OldBr->eraseFromParent();
276
277 // Update the dominator tree by informing it about the new edge from the
278 // preheader to the exit.
279 DT.insertEdge(Preheader, ExitBlock);
Anna Thomas53c8d952017-05-03 11:47:11 +0000280
Anna Thomas53c8d952017-05-03 11:47:11 +0000281 // Rewrite phis in the exit block to get their inputs from the Preheader
282 // instead of the exiting block.
Chandler Carruth04a73872017-01-17 21:51:39 +0000283 BasicBlock::iterator BI = ExitBlock->begin();
Jakub Staszakbc421ef2013-03-18 23:31:30 +0000284 while (PHINode *P = dyn_cast<PHINode>(BI)) {
Anna Thomas72c90c82017-06-22 20:20:56 +0000285 // Set the zero'th element of Phi to be from the preheader and remove all
286 // other incoming values. Given the loop has dedicated exits, all other
287 // incoming values must be from the exiting blocks.
288 int PredIndex = 0;
Anna Thomas72c90c82017-06-22 20:20:56 +0000289 P->setIncomingBlock(PredIndex, Preheader);
290 // Removes all incoming values from all other exiting blocks (including
291 // duplicate values from an exiting block).
292 // Nuke all entries except the zero'th entry which is the preheader entry.
293 // NOTE! We need to remove Incoming Values in the reverse order as done
294 // below, to keep the indices valid for deletion (removeIncomingValues
295 // updates getNumIncomingValues and shifts all values down into the operand
296 // being deleted).
297 for (unsigned i = 0, e = P->getNumIncomingValues() - 1; i != e; ++i)
298 P->removeIncomingValue(e-i, false);
299
300 assert((P->getNumIncomingValues() == 1 &&
301 P->getIncomingBlock(PredIndex) == Preheader) &&
302 "Should have exactly one value and that's from the preheader!");
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000303 ++BI;
Owen Anderson94ad7022008-04-29 00:38:34 +0000304 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000305
Jakub Kuderskid8699132017-08-02 18:17:52 +0000306 // Disconnect the loop body by branching directly to its exit.
307 Builder.SetInsertPoint(Preheader->getTerminator());
308 Builder.CreateBr(ExitBlock);
309 // Remove the old branch.
310 Preheader->getTerminator()->eraseFromParent();
Nadav Rotem465834c2012-07-24 10:51:42 +0000311
Jakub Kuderskid8699132017-08-02 18:17:52 +0000312 // Inform the dominator tree about the removed edge.
313 DT.deleteEdge(Preheader, L->getHeader());
Dan Gohmane6698842009-02-24 01:21:53 +0000314
Jakub Kuderskid8699132017-08-02 18:17:52 +0000315 // Remove the block from the reference counting scheme, so that we can
316 // delete it freely later.
317 for (auto *Block : L->blocks())
318 Block->dropAllReferences();
Nadav Rotem465834c2012-07-24 10:51:42 +0000319
Owen Anderson586216e2008-04-29 00:45:15 +0000320 // Erase the instructions and the blocks without having to worry
321 // about ordering because we already dropped the references.
Owen Andersone6746002008-04-29 20:59:33 +0000322 // NOTE: This iteration is safe because erasing the block does not remove its
323 // entry from the loop's block list. We do that in the next section.
Owen Anderson94ad7022008-04-29 00:38:34 +0000324 for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
Owen Andersonf4aece52008-05-29 08:15:48 +0000325 LI != LE; ++LI)
Owen Anderson94ad7022008-04-29 00:38:34 +0000326 (*LI)->eraseFromParent();
Dan Gohmane5914112009-02-23 17:10:29 +0000327
Owen Anderson586216e2008-04-29 00:45:15 +0000328 // Finally, the blocks from loopinfo. This has to happen late because
329 // otherwise our loop iterators won't work.
Jun Bum Limc837af32016-07-14 18:28:29 +0000330
331 SmallPtrSet<BasicBlock *, 8> blocks;
Owen Anderson94ad7022008-04-29 00:38:34 +0000332 blocks.insert(L->block_begin(), L->block_end());
Craig Topper46276792014-08-24 23:23:06 +0000333 for (BasicBlock *BB : blocks)
Chandler Carruth04a73872017-01-17 21:51:39 +0000334 LI.removeBlock(BB);
Nadav Rotem465834c2012-07-24 10:51:42 +0000335
Justin Bogner883a3ea2015-12-16 18:40:20 +0000336 // The last step is to update LoopInfo now that we've eliminated this loop.
Sanjoy Das388b0122017-09-22 01:47:41 +0000337 LI.erase(L);
Owen Anderson94ad7022008-04-29 00:38:34 +0000338}
Jun Bum Limc837af32016-07-14 18:28:29 +0000339
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000340PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM,
341 LoopStandardAnalysisResults &AR,
Chandler Carruthd50c5fb2017-01-18 02:41:26 +0000342 LPMUpdater &Updater) {
Anna Thomas90f69ab2017-07-04 14:05:19 +0000343
344 DEBUG(dbgs() << "Analyzing Loop for deletion: ");
345 DEBUG(L.dump());
Chandler Carruth027340f2017-02-11 00:09:30 +0000346 if (!deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI, &Updater))
Jun Bum Limc837af32016-07-14 18:28:29 +0000347 return PreservedAnalyses::all();
348
349 return getLoopPassPreservedAnalyses();
350}
351
352namespace {
353class LoopDeletionLegacyPass : public LoopPass {
354public:
355 static char ID; // Pass ID, replacement for typeid
356 LoopDeletionLegacyPass() : LoopPass(ID) {
357 initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry());
358 }
359
360 // Possibly eliminate loop L if it is dead.
361 bool runOnLoop(Loop *L, LPPassManager &) override;
362
363 void getAnalysisUsage(AnalysisUsage &AU) const override {
364 getLoopAnalysisUsage(AU);
365 }
366};
367}
368
369char LoopDeletionLegacyPass::ID = 0;
370INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion",
371 "Delete dead loops", false, false)
372INITIALIZE_PASS_DEPENDENCY(LoopPass)
373INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion",
374 "Delete dead loops", false, false)
375
376Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); }
377
378bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) {
379 if (skipLoop(L))
380 return false;
Jun Bum Limc837af32016-07-14 18:28:29 +0000381 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
382 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth04a73872017-01-17 21:51:39 +0000383 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Jun Bum Limc837af32016-07-14 18:28:29 +0000384
Anna Thomasada4ddc2017-07-04 17:00:03 +0000385 DEBUG(dbgs() << "Analyzing Loop for deletion: ");
386 DEBUG(L->dump());
Chandler Carruth027340f2017-02-11 00:09:30 +0000387 return deleteLoopIfDead(L, DT, SE, LI);
Jun Bum Limc837af32016-07-14 18:28:29 +0000388}