blob: cbb4281b10f7dfe43a2df71164fd1b2a760d158d [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,
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +000046 LoopInfo &LI);
47
48enum class LoopDeletionResult {
49 Unmodified,
50 Modified,
51 Deleted,
52};
53
Chandler Carruth26169f002017-01-17 22:07:26 +000054/// Determines if a loop is dead.
55///
56/// This assumes that we've already checked for unique exit and exiting blocks,
57/// and that the code is in LCSSA form.
58static bool isLoopDead(Loop *L, ScalarEvolution &SE,
59 SmallVectorImpl<BasicBlock *> &ExitingBlocks,
60 BasicBlock *ExitBlock, bool &Changed,
61 BasicBlock *Preheader) {
Owen Anderson94ad7022008-04-29 00:38:34 +000062 // Make sure that all PHI entries coming from the loop are loop invariant.
Owen Andersone6746002008-04-29 20:59:33 +000063 // Because the code is in LCSSA form, any values used outside of the loop
64 // must pass through a PHI in the exit block, meaning that this check is
65 // sufficient to guarantee that no loop-variant values are used outside
66 // of the loop.
Chandler Carruth04a73872017-01-17 21:51:39 +000067 BasicBlock::iterator BI = ExitBlock->begin();
Sanjoy Das905fc272016-05-03 17:50:02 +000068 bool AllEntriesInvariant = true;
69 bool AllOutgoingValuesSame = true;
Jakub Staszakbc421ef2013-03-18 23:31:30 +000070 while (PHINode *P = dyn_cast<PHINode>(BI)) {
Chandler Carruth04a73872017-01-17 21:51:39 +000071 Value *incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
Cameron Zwarich82630852011-02-22 22:25:39 +000072
73 // Make sure all exiting blocks produce the same incoming value for the exit
74 // block. If there are different incoming values for different exiting
75 // blocks, then it is impossible to statically determine which value should
76 // be used.
Sanjoy Das7e7a5a02016-05-03 17:50:06 +000077 AllOutgoingValuesSame =
Chandler Carruth04a73872017-01-17 21:51:39 +000078 all_of(makeArrayRef(ExitingBlocks).slice(1), [&](BasicBlock *BB) {
Sanjoy Das7e7a5a02016-05-03 17:50:06 +000079 return incoming == P->getIncomingValueForBlock(BB);
80 });
Nadav Rotem465834c2012-07-24 10:51:42 +000081
Sanjoy Das905fc272016-05-03 17:50:02 +000082 if (!AllOutgoingValuesSame)
83 break;
84
Jakub Staszakbc421ef2013-03-18 23:31:30 +000085 if (Instruction *I = dyn_cast<Instruction>(incoming))
Sanjoy Das905fc272016-05-03 17:50:02 +000086 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) {
87 AllEntriesInvariant = false;
88 break;
89 }
Cameron Zwarich82630852011-02-22 22:25:39 +000090
Dan Gohmand2d1ae12010-06-22 15:08:57 +000091 ++BI;
Owen Anderson94ad7022008-04-29 00:38:34 +000092 }
Nadav Rotem465834c2012-07-24 10:51:42 +000093
Sanjoy Das905fc272016-05-03 17:50:02 +000094 if (Changed)
95 SE.forgetLoopDispositions(L);
96
97 if (!AllEntriesInvariant || !AllOutgoingValuesSame)
98 return false;
99
Owen Anderson94ad7022008-04-29 00:38:34 +0000100 // Make sure that no instructions in the block have potential side-effects.
Owen Andersone6746002008-04-29 20:59:33 +0000101 // This includes instructions that could write to memory, and loads that are
Xin Tongdf4dff32017-02-23 23:47:10 +0000102 // marked volatile.
Davide Italiano49a0aac2017-02-26 07:08:20 +0000103 for (auto &I : L->blocks())
104 if (any_of(*I, [](Instruction &I) { return I.mayHaveSideEffects(); }))
105 return false;
Owen Anderson94ad7022008-04-29 00:38:34 +0000106 return true;
107}
108
Anna Thomas53c8d952017-05-03 11:47:11 +0000109/// This function returns true if there is no viable path from the
110/// entry block to the header of \p L. Right now, it only does
111/// a local search to save compile time.
112static bool isLoopNeverExecuted(Loop *L) {
113 using namespace PatternMatch;
114
115 auto *Preheader = L->getLoopPreheader();
116 // TODO: We can relax this constraint, since we just need a loop
117 // predecessor.
118 assert(Preheader && "Needs preheader!");
119
120 if (Preheader == &Preheader->getParent()->getEntryBlock())
121 return false;
122 // All predecessors of the preheader should have a constant conditional
123 // branch, with the loop's preheader as not-taken.
124 for (auto *Pred: predecessors(Preheader)) {
125 BasicBlock *Taken, *NotTaken;
126 ConstantInt *Cond;
127 if (!match(Pred->getTerminator(),
128 m_Br(m_ConstantInt(Cond), Taken, NotTaken)))
129 return false;
130 if (!Cond->getZExtValue())
131 std::swap(Taken, NotTaken);
132 if (Taken == Preheader)
133 return false;
134 }
135 assert(!pred_empty(Preheader) &&
136 "Preheader should have predecessors at this point!");
137 // All the predecessors have the loop preheader as not-taken target.
138 return true;
139}
140
Chandler Carruth26169f002017-01-17 22:07:26 +0000141/// Remove a loop if it is dead.
142///
143/// A loop is considered dead if it does not impact the observable behavior of
144/// the program other than finite running time. This never removes a loop that
Anna Thomas53c8d952017-05-03 11:47:11 +0000145/// might be infinite (unless it is never executed), as doing so could change
146/// the halting/non-halting nature of a program.
Chandler Carruth26169f002017-01-17 22:07:26 +0000147///
148/// This entire process relies pretty heavily on LoopSimplify form and LCSSA in
149/// order to make various safety checks work.
150///
Chandler Carruth027340f2017-02-11 00:09:30 +0000151/// \returns true if any changes were made. This may mutate the loop even if it
152/// is unable to delete it due to hoisting trivially loop invariant
153/// instructions out of the loop.
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000154static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
155 ScalarEvolution &SE, LoopInfo &LI) {
Sanjoy Das979a11d2016-02-21 17:11:59 +0000156 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
157
Anna Thomas90f69ab2017-07-04 14:05:19 +0000158 // We can only remove the loop if there is a preheader that we can branch from
159 // after removing it. Also, if LoopSimplify form is not available, stay out
160 // of trouble.
Chandler Carruthaa885c92017-01-17 22:19:56 +0000161 BasicBlock *Preheader = L->getLoopPreheader();
Anna Thomas90f69ab2017-07-04 14:05:19 +0000162 if (!Preheader || !L->hasDedicatedExits()) {
163 DEBUG(dbgs()
164 << "Deletion requires Loop with preheader and dedicated exits.\n");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000165 return LoopDeletionResult::Unmodified;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000166 }
Owen Anderson94ad7022008-04-29 00:38:34 +0000167 // We can't remove loops that contain subloops. If the subloops were dead,
168 // they would already have been removed in earlier executions of this pass.
Anna Thomas90f69ab2017-07-04 14:05:19 +0000169 if (L->begin() != L->end()) {
170 DEBUG(dbgs() << "Loop contains subloops.\n");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000171 return LoopDeletionResult::Unmodified;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000172 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000173
Anna Thomas53c8d952017-05-03 11:47:11 +0000174
175 BasicBlock *ExitBlock = L->getUniqueExitBlock();
176
177 if (ExitBlock && isLoopNeverExecuted(L)) {
Anna Thomas90f69ab2017-07-04 14:05:19 +0000178 DEBUG(dbgs() << "Loop is proven to never execute, delete it!");
Anna Thomase7cb6332017-06-25 21:13:58 +0000179 // Set incoming value to undef for phi nodes in the exit block.
180 BasicBlock::iterator BI = ExitBlock->begin();
181 while (PHINode *P = dyn_cast<PHINode>(BI)) {
182 for (unsigned i = 0; i < P->getNumIncomingValues(); i++)
183 P->setIncomingValue(i, UndefValue::get(P->getType()));
184 BI++;
185 }
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000186 deleteDeadLoop(L, DT, SE, LI);
Anna Thomas53c8d952017-05-03 11:47:11 +0000187 ++NumDeleted;
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000188 return LoopDeletionResult::Deleted;
Anna Thomas53c8d952017-05-03 11:47:11 +0000189 }
190
191 // The remaining checks below are for a loop being dead because all statements
192 // in the loop are invariant.
Chandler Carruth04a73872017-01-17 21:51:39 +0000193 SmallVector<BasicBlock *, 4> ExitingBlocks;
194 L->getExitingBlocks(ExitingBlocks);
Nadav Rotem465834c2012-07-24 10:51:42 +0000195
Owen Andersonad5f2112008-05-16 04:32:45 +0000196 // We require that the loop only have a single exit block. Otherwise, we'd
197 // be in the situation of needing to be able to solve statically which exit
198 // block will be branched to, or trying to preserve the branching logic in
199 // a loop invariant manner.
Anna Thomas90f69ab2017-07-04 14:05:19 +0000200 if (!ExitBlock) {
201 DEBUG(dbgs() << "Deletion requires single exit block\n");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000202 return LoopDeletionResult::Unmodified;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000203 }
Owen Anderson94ad7022008-04-29 00:38:34 +0000204 // Finally, we have to check that the loop really is dead.
Chandler Carruth027340f2017-02-11 00:09:30 +0000205 bool Changed = false;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000206 if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader)) {
207 DEBUG(dbgs() << "Loop is not invariant, cannot delete.\n");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000208 return Changed ? LoopDeletionResult::Modified
209 : LoopDeletionResult::Unmodified;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000210 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000211
Owen Andersonad5f2112008-05-16 04:32:45 +0000212 // Don't remove loops for which we can't solve the trip count.
213 // They could be infinite, in which case we'd be changing program behavior.
Dan Gohman41d00ac2009-10-23 17:10:01 +0000214 const SCEV *S = SE.getMaxBackedgeTakenCount(L);
Anna Thomas90f69ab2017-07-04 14:05:19 +0000215 if (isa<SCEVCouldNotCompute>(S)) {
216 DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount.\n");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000217 return Changed ? LoopDeletionResult::Modified
218 : LoopDeletionResult::Unmodified;
Anna Thomas90f69ab2017-07-04 14:05:19 +0000219 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000220
Anna Thomas90f69ab2017-07-04 14:05:19 +0000221 DEBUG(dbgs() << "Loop is invariant, delete it!");
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000222 deleteDeadLoop(L, DT, SE, LI);
Anna Thomas53c8d952017-05-03 11:47:11 +0000223 ++NumDeleted;
224
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000225 return LoopDeletionResult::Deleted;
Anna Thomas53c8d952017-05-03 11:47:11 +0000226}
227
228static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000229 LoopInfo &LI) {
Anna Thomas53c8d952017-05-03 11:47:11 +0000230 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
231 auto *Preheader = L->getLoopPreheader();
232 assert(Preheader && "Preheader should exist!");
233
Owen Andersone6746002008-04-29 20:59:33 +0000234 // Now that we know the removal is safe, remove the loop by changing the
Nadav Rotem465834c2012-07-24 10:51:42 +0000235 // branch from the preheader to go to the single exit block.
Chandler Carruthbb7e4b42017-01-17 22:00:52 +0000236 //
Owen Anderson586216e2008-04-29 00:45:15 +0000237 // Because we're deleting a large chunk of code at once, the sequence in which
Chandler Carruthbd551e92017-01-17 22:09:28 +0000238 // we remove things is very important to avoid invalidation issues.
Dan Gohman7bb31732009-07-08 19:14:29 +0000239
240 // Tell ScalarEvolution that the loop is deleted. Do this before
241 // deleting the loop so that ScalarEvolution can look at the loop
242 // to determine what it needs to clean up.
Dan Gohman880c92a2009-10-31 15:04:55 +0000243 SE.forgetLoop(L);
Dan Gohman7bb31732009-07-08 19:14:29 +0000244
Anna Thomas53c8d952017-05-03 11:47:11 +0000245 auto *ExitBlock = L->getUniqueExitBlock();
246 assert(ExitBlock && "Should have a unique exit block!");
Anna Thomas72c90c82017-06-22 20:20:56 +0000247 assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
248
Jakub Kuderskid8699132017-08-02 18:17:52 +0000249 auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
250 assert(OldBr && "Preheader must end with a branch");
251 assert(OldBr->isUnconditional() && "Preheader must have a single successor");
252 // Connect the preheader to the exit block. Keep the old edge to the header
253 // around to perform the dominator tree update in two separate steps
254 // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
255 // preheader -> header.
256 //
257 //
258 // 0. Preheader 1. Preheader 2. Preheader
259 // | | | |
260 // V | V |
261 // Header <--\ | Header <--\ | Header <--\
262 // | | | | | | | | | | |
263 // | V | | | V | | | V |
264 // | Body --/ | | Body --/ | | Body --/
265 // V V V V V
266 // Exit Exit Exit
267 //
268 // By doing this is two separate steps we can perform the dominator tree
269 // update without using the batch update API.
270 //
Anna Thomas53c8d952017-05-03 11:47:11 +0000271 // Even when the loop is never executed, we cannot remove the edge from the
272 // source block to the exit block. Consider the case where the unexecuted loop
273 // branches back to an outer loop. If we deleted the loop and removed the edge
274 // coming to this inner loop, this will break the outer loop structure (by
275 // deleting the backedge of the outer loop). If the outer loop is indeed a
276 // non-loop, it will be deleted in a future iteration of loop deletion pass.
Jakub Kuderskid8699132017-08-02 18:17:52 +0000277 IRBuilder<> Builder(OldBr);
278 Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
279 // Remove the old branch. The conditional branch becomes a new terminator.
280 OldBr->eraseFromParent();
281
282 // Update the dominator tree by informing it about the new edge from the
283 // preheader to the exit.
284 DT.insertEdge(Preheader, ExitBlock);
Anna Thomas53c8d952017-05-03 11:47:11 +0000285
Anna Thomas53c8d952017-05-03 11:47:11 +0000286 // Rewrite phis in the exit block to get their inputs from the Preheader
287 // instead of the exiting block.
Chandler Carruth04a73872017-01-17 21:51:39 +0000288 BasicBlock::iterator BI = ExitBlock->begin();
Jakub Staszakbc421ef2013-03-18 23:31:30 +0000289 while (PHINode *P = dyn_cast<PHINode>(BI)) {
Anna Thomas72c90c82017-06-22 20:20:56 +0000290 // Set the zero'th element of Phi to be from the preheader and remove all
291 // other incoming values. Given the loop has dedicated exits, all other
292 // incoming values must be from the exiting blocks.
293 int PredIndex = 0;
Anna Thomas72c90c82017-06-22 20:20:56 +0000294 P->setIncomingBlock(PredIndex, Preheader);
295 // Removes all incoming values from all other exiting blocks (including
296 // duplicate values from an exiting block).
297 // Nuke all entries except the zero'th entry which is the preheader entry.
298 // NOTE! We need to remove Incoming Values in the reverse order as done
299 // below, to keep the indices valid for deletion (removeIncomingValues
300 // updates getNumIncomingValues and shifts all values down into the operand
301 // being deleted).
302 for (unsigned i = 0, e = P->getNumIncomingValues() - 1; i != e; ++i)
303 P->removeIncomingValue(e-i, false);
304
305 assert((P->getNumIncomingValues() == 1 &&
306 P->getIncomingBlock(PredIndex) == Preheader) &&
307 "Should have exactly one value and that's from the preheader!");
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000308 ++BI;
Owen Anderson94ad7022008-04-29 00:38:34 +0000309 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000310
Jakub Kuderskid8699132017-08-02 18:17:52 +0000311 // Disconnect the loop body by branching directly to its exit.
312 Builder.SetInsertPoint(Preheader->getTerminator());
313 Builder.CreateBr(ExitBlock);
314 // Remove the old branch.
315 Preheader->getTerminator()->eraseFromParent();
Nadav Rotem465834c2012-07-24 10:51:42 +0000316
Jakub Kuderskid8699132017-08-02 18:17:52 +0000317 // Inform the dominator tree about the removed edge.
318 DT.deleteEdge(Preheader, L->getHeader());
Dan Gohmane6698842009-02-24 01:21:53 +0000319
Jakub Kuderskid8699132017-08-02 18:17:52 +0000320 // Remove the block from the reference counting scheme, so that we can
321 // delete it freely later.
322 for (auto *Block : L->blocks())
323 Block->dropAllReferences();
Nadav Rotem465834c2012-07-24 10:51:42 +0000324
Owen Anderson586216e2008-04-29 00:45:15 +0000325 // Erase the instructions and the blocks without having to worry
326 // about ordering because we already dropped the references.
Owen Andersone6746002008-04-29 20:59:33 +0000327 // NOTE: This iteration is safe because erasing the block does not remove its
328 // entry from the loop's block list. We do that in the next section.
Owen Anderson94ad7022008-04-29 00:38:34 +0000329 for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
Owen Andersonf4aece52008-05-29 08:15:48 +0000330 LI != LE; ++LI)
Owen Anderson94ad7022008-04-29 00:38:34 +0000331 (*LI)->eraseFromParent();
Dan Gohmane5914112009-02-23 17:10:29 +0000332
Owen Anderson586216e2008-04-29 00:45:15 +0000333 // Finally, the blocks from loopinfo. This has to happen late because
334 // otherwise our loop iterators won't work.
Jun Bum Limc837af32016-07-14 18:28:29 +0000335
336 SmallPtrSet<BasicBlock *, 8> blocks;
Owen Anderson94ad7022008-04-29 00:38:34 +0000337 blocks.insert(L->block_begin(), L->block_end());
Craig Topper46276792014-08-24 23:23:06 +0000338 for (BasicBlock *BB : blocks)
Chandler Carruth04a73872017-01-17 21:51:39 +0000339 LI.removeBlock(BB);
Nadav Rotem465834c2012-07-24 10:51:42 +0000340
Justin Bogner883a3ea2015-12-16 18:40:20 +0000341 // The last step is to update LoopInfo now that we've eliminated this loop.
Sanjoy Das388b0122017-09-22 01:47:41 +0000342 LI.erase(L);
Owen Anderson94ad7022008-04-29 00:38:34 +0000343}
Jun Bum Limc837af32016-07-14 18:28:29 +0000344
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000345PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM,
346 LoopStandardAnalysisResults &AR,
Chandler Carruthd50c5fb2017-01-18 02:41:26 +0000347 LPMUpdater &Updater) {
Anna Thomas90f69ab2017-07-04 14:05:19 +0000348
349 DEBUG(dbgs() << "Analyzing Loop for deletion: ");
350 DEBUG(L.dump());
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000351 auto Result = deleteLoopIfDead(&L, AR.DT, AR.SE, AR.LI);
352 if (Result == LoopDeletionResult::Unmodified)
Jun Bum Limc837af32016-07-14 18:28:29 +0000353 return PreservedAnalyses::all();
354
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000355 if (Result == LoopDeletionResult::Deleted)
356 Updater.markLoopAsDeleted(L);
357
Jun Bum Limc837af32016-07-14 18:28:29 +0000358 return getLoopPassPreservedAnalyses();
359}
360
361namespace {
362class LoopDeletionLegacyPass : public LoopPass {
363public:
364 static char ID; // Pass ID, replacement for typeid
365 LoopDeletionLegacyPass() : LoopPass(ID) {
366 initializeLoopDeletionLegacyPassPass(*PassRegistry::getPassRegistry());
367 }
368
369 // Possibly eliminate loop L if it is dead.
370 bool runOnLoop(Loop *L, LPPassManager &) override;
371
372 void getAnalysisUsage(AnalysisUsage &AU) const override {
373 getLoopAnalysisUsage(AU);
374 }
375};
376}
377
378char LoopDeletionLegacyPass::ID = 0;
379INITIALIZE_PASS_BEGIN(LoopDeletionLegacyPass, "loop-deletion",
380 "Delete dead loops", false, false)
381INITIALIZE_PASS_DEPENDENCY(LoopPass)
382INITIALIZE_PASS_END(LoopDeletionLegacyPass, "loop-deletion",
383 "Delete dead loops", false, false)
384
385Pass *llvm::createLoopDeletionPass() { return new LoopDeletionLegacyPass(); }
386
387bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) {
388 if (skipLoop(L))
389 return false;
Jun Bum Limc837af32016-07-14 18:28:29 +0000390 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
391 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth04a73872017-01-17 21:51:39 +0000392 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Jun Bum Limc837af32016-07-14 18:28:29 +0000393
Anna Thomasada4ddc2017-07-04 17:00:03 +0000394 DEBUG(dbgs() << "Analyzing Loop for deletion: ");
395 DEBUG(L->dump());
Sanjoy Das8e8c1bc2017-09-27 21:45:21 +0000396 return deleteLoopIfDead(L, DT, SE, LI) != LoopDeletionResult::Unmodified;
Jun Bum Limc837af32016-07-14 18:28:29 +0000397}