blob: eb355537f77afe229b47d1dccc31ed66f70fa3e3 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner0bbe58f2001-11-26 18:41:20 +00009//
10// This file defines the LoopInfo class that is used to identify natural loops
11// and determine the loop depth of various nodes of the CFG. Note that the
12// loops identified may actually be several natural loops that share the same
13// header node... not just a single natural loop.
14//
15//===----------------------------------------------------------------------===//
16
Misha Brukman10d208d2004-01-30 17:26:24 +000017#include "llvm/Analysis/LoopInfo.h"
Chris Lattner92020fa2004-04-15 15:16:02 +000018#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
20#include "llvm/Analysis/Dominators.h"
Andrew Trick2d31ae32011-08-10 01:59:05 +000021#include "llvm/Analysis/LoopIterator.h"
Chris Lattnera59cbb22002-07-27 01:12:17 +000022#include "llvm/Assembly/Writer.h"
Misha Brukman10d208d2004-01-30 17:26:24 +000023#include "llvm/Support/CFG.h"
Dan Gohman9450b0e2009-09-28 00:27:48 +000024#include "llvm/Support/CommandLine.h"
Dan Gohmandda30cd2010-01-05 21:08:02 +000025#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/DepthFirstIterator.h"
Chris Lattnerb1f5d8b2007-03-04 04:06:39 +000027#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000028#include <algorithm>
Chris Lattner46758a82004-04-12 20:26:17 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Dan Gohman9450b0e2009-09-28 00:27:48 +000031// Always verify loopinfo if expensive checking is enabled.
32#ifdef XDEBUG
Dan Gohmanb3579832010-04-15 17:08:50 +000033static bool VerifyLoopInfo = true;
Dan Gohman9450b0e2009-09-28 00:27:48 +000034#else
Dan Gohmanb3579832010-04-15 17:08:50 +000035static bool VerifyLoopInfo = false;
Dan Gohman9450b0e2009-09-28 00:27:48 +000036#endif
37static cl::opt<bool,true>
38VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
39 cl::desc("Verify loop info (time consuming)"));
40
Devang Patel19974732007-05-03 01:11:54 +000041char LoopInfo::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000042INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true)
43INITIALIZE_PASS_DEPENDENCY(DominatorTree)
44INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true)
Chris Lattner93193f82002-01-31 00:42:27 +000045
46//===----------------------------------------------------------------------===//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000047// Loop implementation
Chris Lattner93193f82002-01-31 00:42:27 +000048//
Misha Brukman6b290a52002-10-11 05:31:10 +000049
Dan Gohman16a2c922009-07-13 22:02:44 +000050/// isLoopInvariant - Return true if the specified value is loop invariant
51///
52bool Loop::isLoopInvariant(Value *V) const {
53 if (Instruction *I = dyn_cast<Instruction>(V))
Chris Lattneradc79912010-09-06 01:05:37 +000054 return !contains(I);
Dan Gohman16a2c922009-07-13 22:02:44 +000055 return true; // All non-instructions are loop invariant
56}
57
Chris Lattneradc79912010-09-06 01:05:37 +000058/// hasLoopInvariantOperands - Return true if all the operands of the
Andrew Trick882bcc62011-08-03 23:45:50 +000059/// specified instruction are loop invariant.
Chris Lattneradc79912010-09-06 01:05:37 +000060bool Loop::hasLoopInvariantOperands(Instruction *I) const {
61 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
62 if (!isLoopInvariant(I->getOperand(i)))
63 return false;
Andrew Trick882bcc62011-08-03 23:45:50 +000064
Chris Lattneradc79912010-09-06 01:05:37 +000065 return true;
Dan Gohmana3420262009-07-14 01:06:29 +000066}
67
68/// makeLoopInvariant - If the given value is an instruciton inside of the
69/// loop and it can be hoisted, do so to make it trivially loop-invariant.
70/// Return true if the value after any hoisting is loop invariant. This
71/// function can be used as a slightly more aggressive replacement for
72/// isLoopInvariant.
73///
74/// If InsertPt is specified, it is the point to hoist instructions to.
75/// If null, the terminator of the loop preheader is used.
76///
Dan Gohmanbdc017e2009-07-15 01:25:43 +000077bool Loop::makeLoopInvariant(Value *V, bool &Changed,
78 Instruction *InsertPt) const {
Dan Gohmana3420262009-07-14 01:06:29 +000079 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmanbdc017e2009-07-15 01:25:43 +000080 return makeLoopInvariant(I, Changed, InsertPt);
Dan Gohmana3420262009-07-14 01:06:29 +000081 return true; // All non-instructions are loop-invariant.
82}
83
84/// makeLoopInvariant - If the given instruction is inside of the
85/// loop and it can be hoisted, do so to make it trivially loop-invariant.
86/// Return true if the instruction after any hoisting is loop invariant. This
87/// function can be used as a slightly more aggressive replacement for
88/// isLoopInvariant.
89///
90/// If InsertPt is specified, it is the point to hoist instructions to.
91/// If null, the terminator of the loop preheader is used.
92///
Dan Gohmanbdc017e2009-07-15 01:25:43 +000093bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
94 Instruction *InsertPt) const {
Dan Gohmana3420262009-07-14 01:06:29 +000095 // Test if the value is already loop-invariant.
96 if (isLoopInvariant(I))
97 return true;
Eli Friedman0b79a772009-07-17 04:28:42 +000098 if (!I->isSafeToSpeculativelyExecute())
Dan Gohmana3420262009-07-14 01:06:29 +000099 return false;
Eli Friedman0b79a772009-07-17 04:28:42 +0000100 if (I->mayReadFromMemory())
Dan Gohmana3420262009-07-14 01:06:29 +0000101 return false;
Bill Wendlingc9b2a982011-08-17 20:36:44 +0000102 // The landingpad instruction is immobile.
103 if (isa<LandingPadInst>(I))
104 return false;
Dan Gohmana3420262009-07-14 01:06:29 +0000105 // Determine the insertion point, unless one was given.
106 if (!InsertPt) {
107 BasicBlock *Preheader = getLoopPreheader();
108 // Without a preheader, hoisting is not feasible.
109 if (!Preheader)
110 return false;
111 InsertPt = Preheader->getTerminator();
112 }
113 // Don't hoist instructions with loop-variant operands.
114 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000115 if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt))
Dan Gohmana3420262009-07-14 01:06:29 +0000116 return false;
Andrew Trick882bcc62011-08-03 23:45:50 +0000117
Dan Gohmana3420262009-07-14 01:06:29 +0000118 // Hoist.
119 I->moveBefore(InsertPt);
Dan Gohmanbdc017e2009-07-15 01:25:43 +0000120 Changed = true;
Dan Gohmana3420262009-07-14 01:06:29 +0000121 return true;
122}
123
Dan Gohman16a2c922009-07-13 22:02:44 +0000124/// getCanonicalInductionVariable - Check to see if the loop has a canonical
125/// induction variable: an integer recurrence that starts at 0 and increments
126/// by one each time through the loop. If so, return the phi node that
127/// corresponds to it.
128///
129/// The IndVarSimplify pass transforms loops to have a canonical induction
130/// variable.
131///
132PHINode *Loop::getCanonicalInductionVariable() const {
133 BasicBlock *H = getHeader();
134
135 BasicBlock *Incoming = 0, *Backedge = 0;
Dan Gohman63137d52010-07-23 21:25:16 +0000136 pred_iterator PI = pred_begin(H);
137 assert(PI != pred_end(H) &&
Dan Gohman16a2c922009-07-13 22:02:44 +0000138 "Loop must have at least one backedge!");
139 Backedge = *PI++;
Dan Gohman63137d52010-07-23 21:25:16 +0000140 if (PI == pred_end(H)) return 0; // dead loop
Dan Gohman16a2c922009-07-13 22:02:44 +0000141 Incoming = *PI++;
Dan Gohman63137d52010-07-23 21:25:16 +0000142 if (PI != pred_end(H)) return 0; // multiple backedges?
Dan Gohman16a2c922009-07-13 22:02:44 +0000143
144 if (contains(Incoming)) {
145 if (contains(Backedge))
146 return 0;
147 std::swap(Incoming, Backedge);
148 } else if (!contains(Backedge))
149 return 0;
150
151 // Loop over all of the PHI nodes, looking for a canonical indvar.
152 for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
153 PHINode *PN = cast<PHINode>(I);
154 if (ConstantInt *CI =
155 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
156 if (CI->isNullValue())
157 if (Instruction *Inc =
158 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
159 if (Inc->getOpcode() == Instruction::Add &&
160 Inc->getOperand(0) == PN)
161 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
162 if (CI->equalsInt(1))
163 return PN;
164 }
165 return 0;
166}
167
Dan Gohman16a2c922009-07-13 22:02:44 +0000168/// isLCSSAForm - Return true if the Loop is in LCSSA form
Dan Gohmanbbf81d82010-03-10 19:38:49 +0000169bool Loop::isLCSSAForm(DominatorTree &DT) const {
Dan Gohman16a2c922009-07-13 22:02:44 +0000170 // Sort the blocks vector so that we can use binary search to do quick
171 // lookups.
Gabor Greif5891ac82010-07-09 14:28:41 +0000172 SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end());
Dan Gohman16a2c922009-07-13 22:02:44 +0000173
174 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
Dan Gohman81d893c2009-11-09 18:19:43 +0000175 BasicBlock *BB = *BI;
176 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I)
Dan Gohman16a2c922009-07-13 22:02:44 +0000177 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
178 ++UI) {
Gabor Greif5891ac82010-07-09 14:28:41 +0000179 User *U = *UI;
180 BasicBlock *UserBB = cast<Instruction>(U)->getParent();
181 if (PHINode *P = dyn_cast<PHINode>(U))
Dan Gohman16a2c922009-07-13 22:02:44 +0000182 UserBB = P->getIncomingBlock(UI);
Dan Gohman16a2c922009-07-13 22:02:44 +0000183
Dan Gohmancbac7f12010-03-09 01:53:33 +0000184 // Check the current block, as a fast-path, before checking whether
185 // the use is anywhere in the loop. Most values are used in the same
186 // block they are defined in. Also, blocks not reachable from the
187 // entry are special; uses in them don't need to go through PHIs.
188 if (UserBB != BB &&
189 !LoopBBs.count(UserBB) &&
Dan Gohmanbbf81d82010-03-10 19:38:49 +0000190 DT.isReachableFromEntry(UserBB))
Dan Gohman16a2c922009-07-13 22:02:44 +0000191 return false;
192 }
193 }
194
195 return true;
196}
Dan Gohman93773862009-07-16 16:16:23 +0000197
198/// isLoopSimplifyForm - Return true if the Loop is in the form that
199/// the LoopSimplify form transforms loops to, which is sometimes called
200/// normal form.
201bool Loop::isLoopSimplifyForm() const {
Dan Gohmanf17e9512009-11-05 19:21:41 +0000202 // Normal-form loops have a preheader, a single backedge, and all of their
203 // exits have all their predecessors inside the loop.
204 return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
205}
206
207/// hasDedicatedExits - Return true if no exit block for the loop
208/// has a predecessor that is outside the loop.
209bool Loop::hasDedicatedExits() const {
Dan Gohmaneed9e5b2009-10-20 20:41:13 +0000210 // Sort the blocks vector so that we can use binary search to do quick
211 // lookups.
212 SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end());
Dan Gohman93773862009-07-16 16:16:23 +0000213 // Each predecessor of each exit block of a normal loop is contained
214 // within the loop.
215 SmallVector<BasicBlock *, 4> ExitBlocks;
216 getExitBlocks(ExitBlocks);
217 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
218 for (pred_iterator PI = pred_begin(ExitBlocks[i]),
219 PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
Dan Gohmaneed9e5b2009-10-20 20:41:13 +0000220 if (!LoopBBs.count(*PI))
Dan Gohman93773862009-07-16 16:16:23 +0000221 return false;
222 // All the requirements are met.
223 return true;
224}
225
Dan Gohmanf0608d82009-09-03 16:10:48 +0000226/// getUniqueExitBlocks - Return all unique successor blocks of this loop.
227/// These are the blocks _outside of the current loop_ which are branched to.
Dan Gohman050959c2009-12-11 20:05:23 +0000228/// This assumes that loop exits are in canonical form.
Dan Gohmanf0608d82009-09-03 16:10:48 +0000229///
230void
231Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
Dan Gohman050959c2009-12-11 20:05:23 +0000232 assert(hasDedicatedExits() &&
233 "getUniqueExitBlocks assumes the loop has canonical form exits!");
Dan Gohman5c89b522009-09-08 15:45:00 +0000234
Dan Gohmanf0608d82009-09-03 16:10:48 +0000235 // Sort the blocks vector so that we can use binary search to do quick
236 // lookups.
237 SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end());
238 std::sort(LoopBBs.begin(), LoopBBs.end());
239
Dan Gohman058db922009-09-03 20:36:13 +0000240 SmallVector<BasicBlock *, 32> switchExitBlocks;
Dan Gohmanf0608d82009-09-03 16:10:48 +0000241
242 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
243
244 BasicBlock *current = *BI;
245 switchExitBlocks.clear();
246
Dan Gohman63137d52010-07-23 21:25:16 +0000247 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
Dan Gohmanf0608d82009-09-03 16:10:48 +0000248 // If block is inside the loop then it is not a exit block.
249 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
250 continue;
251
Dan Gohman63137d52010-07-23 21:25:16 +0000252 pred_iterator PI = pred_begin(*I);
Dan Gohmanf0608d82009-09-03 16:10:48 +0000253 BasicBlock *firstPred = *PI;
254
255 // If current basic block is this exit block's first predecessor
256 // then only insert exit block in to the output ExitBlocks vector.
257 // This ensures that same exit block is not inserted twice into
258 // ExitBlocks vector.
259 if (current != firstPred)
260 continue;
261
262 // If a terminator has more then two successors, for example SwitchInst,
263 // then it is possible that there are multiple edges from current block
264 // to one exit block.
Dan Gohman63137d52010-07-23 21:25:16 +0000265 if (std::distance(succ_begin(current), succ_end(current)) <= 2) {
Dan Gohmanf0608d82009-09-03 16:10:48 +0000266 ExitBlocks.push_back(*I);
267 continue;
268 }
269
270 // In case of multiple edges from current block to exit block, collect
271 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
272 // duplicate edges.
273 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
274 == switchExitBlocks.end()) {
275 switchExitBlocks.push_back(*I);
276 ExitBlocks.push_back(*I);
277 }
278 }
279 }
280}
281
282/// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
283/// block, return that block. Otherwise return null.
284BasicBlock *Loop::getUniqueExitBlock() const {
285 SmallVector<BasicBlock *, 8> UniqueExitBlocks;
286 getUniqueExitBlocks(UniqueExitBlocks);
287 if (UniqueExitBlocks.size() == 1)
288 return UniqueExitBlocks[0];
289 return 0;
290}
291
Dan Gohmandda30cd2010-01-05 21:08:02 +0000292void Loop::dump() const {
293 print(dbgs());
294}
295
Chris Lattnera59cbb22002-07-27 01:12:17 +0000296//===----------------------------------------------------------------------===//
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000297// UnloopUpdater implementation
298//
299
Benjamin Kramera67f14b2011-08-19 01:42:18 +0000300namespace {
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000301/// Find the new parent loop for all blocks within the "unloop" whose last
302/// backedges has just been removed.
303class UnloopUpdater {
304 Loop *Unloop;
305 LoopInfo *LI;
306
307 LoopBlocksDFS DFS;
308
309 // Map unloop's immediate subloops to their nearest reachable parents. Nested
310 // loops within these subloops will not change parents. However, an immediate
311 // subloop's new parent will be the nearest loop reachable from either its own
312 // exits *or* any of its nested loop's exits.
313 DenseMap<Loop*, Loop*> SubloopParents;
314
315 // Flag the presence of an irreducible backedge whose destination is a block
316 // directly contained by the original unloop.
317 bool FoundIB;
318
319public:
320 UnloopUpdater(Loop *UL, LoopInfo *LInfo) :
321 Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {}
322
323 void updateBlockParents();
324
Andrew Trickc12d9b92011-08-11 20:27:32 +0000325 void removeBlocksFromAncestors();
326
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000327 void updateSubloopParents();
328
329protected:
330 Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
331};
Benjamin Kramera67f14b2011-08-19 01:42:18 +0000332} // end anonymous namespace
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000333
334/// updateBlockParents - Update the parent loop for all blocks that are directly
335/// contained within the original "unloop".
336void UnloopUpdater::updateBlockParents() {
337 if (Unloop->getNumBlocks()) {
338 // Perform a post order CFG traversal of all blocks within this loop,
339 // propagating the nearest loop from sucessors to predecessors.
340 LoopBlocksTraversal Traversal(DFS, LI);
341 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
342 POE = Traversal.end(); POI != POE; ++POI) {
343
344 Loop *L = LI->getLoopFor(*POI);
345 Loop *NL = getNearestLoop(*POI, L);
346
347 if (NL != L) {
348 // For reducible loops, NL is now an ancestor of Unloop.
349 assert((NL != Unloop && (!NL || NL->contains(Unloop))) &&
350 "uninitialized successor");
351 LI->changeLoopFor(*POI, NL);
352 }
353 else {
354 // Or the current block is part of a subloop, in which case its parent
355 // is unchanged.
356 assert((FoundIB || Unloop->contains(L)) && "uninitialized successor");
357 }
358 }
359 }
360 // Each irreducible loop within the unloop induces a round of iteration using
361 // the DFS result cached by Traversal.
362 bool Changed = FoundIB;
363 for (unsigned NIters = 0; Changed; ++NIters) {
364 assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm");
365
366 // Iterate over the postorder list of blocks, propagating the nearest loop
367 // from successors to predecessors as before.
368 Changed = false;
369 for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
370 POE = DFS.endPostorder(); POI != POE; ++POI) {
371
372 Loop *L = LI->getLoopFor(*POI);
373 Loop *NL = getNearestLoop(*POI, L);
374 if (NL != L) {
375 assert(NL != Unloop && (!NL || NL->contains(Unloop)) &&
376 "uninitialized successor");
377 LI->changeLoopFor(*POI, NL);
378 Changed = true;
379 }
380 }
381 }
382}
383
Andrew Trickc12d9b92011-08-11 20:27:32 +0000384/// removeBlocksFromAncestors - Remove unloop's blocks from all ancestors below
385/// their new parents.
386void UnloopUpdater::removeBlocksFromAncestors() {
Andrew Trick5865a8d2011-11-18 03:42:41 +0000387 // Remove all unloop's blocks (including those in nested subloops) from
388 // ancestors below the new parent loop.
Andrew Trickc12d9b92011-08-11 20:27:32 +0000389 for (Loop::block_iterator BI = Unloop->block_begin(),
390 BE = Unloop->block_end(); BI != BE; ++BI) {
Andrew Trick5865a8d2011-11-18 03:42:41 +0000391 Loop *OuterParent = LI->getLoopFor(*BI);
392 if (Unloop->contains(OuterParent)) {
393 while (OuterParent->getParentLoop() != Unloop)
394 OuterParent = OuterParent->getParentLoop();
395 OuterParent = SubloopParents[OuterParent];
396 }
Andrew Trickc12d9b92011-08-11 20:27:32 +0000397 // Remove blocks from former Ancestors except Unloop itself which will be
398 // deleted.
Andrew Trick5865a8d2011-11-18 03:42:41 +0000399 for (Loop *OldParent = Unloop->getParentLoop(); OldParent != OuterParent;
Andrew Trickc12d9b92011-08-11 20:27:32 +0000400 OldParent = OldParent->getParentLoop()) {
401 assert(OldParent && "new loop is not an ancestor of the original");
402 OldParent->removeBlockFromLoop(*BI);
403 }
404 }
405}
406
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000407/// updateSubloopParents - Update the parent loop for all subloops directly
408/// nested within unloop.
409void UnloopUpdater::updateSubloopParents() {
410 while (!Unloop->empty()) {
Andrew Trick5c1ff1f2011-08-11 17:54:58 +0000411 Loop *Subloop = *llvm::prior(Unloop->end());
412 Unloop->removeChildLoop(llvm::prior(Unloop->end()));
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000413
414 assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
415 if (SubloopParents[Subloop])
416 SubloopParents[Subloop]->addChildLoop(Subloop);
Andrew Trick5434c1e2011-08-26 03:06:34 +0000417 else
418 LI->addTopLevelLoop(Subloop);
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000419 }
420}
421
422/// getNearestLoop - Return the nearest parent loop among this block's
423/// successors. If a successor is a subloop header, consider its parent to be
424/// the nearest parent of the subloop's exits.
425///
426/// For subloop blocks, simply update SubloopParents and return NULL.
427Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
428
Andrew Trick5c1ff1f2011-08-11 17:54:58 +0000429 // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
430 // is considered uninitialized.
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000431 Loop *NearLoop = BBLoop;
432
433 Loop *Subloop = 0;
434 if (NearLoop != Unloop && Unloop->contains(NearLoop)) {
435 Subloop = NearLoop;
436 // Find the subloop ancestor that is directly contained within Unloop.
437 while (Subloop->getParentLoop() != Unloop) {
438 Subloop = Subloop->getParentLoop();
439 assert(Subloop && "subloop is not an ancestor of the original loop");
440 }
441 // Get the current nearest parent of the Subloop exits, initially Unloop.
442 if (!SubloopParents.count(Subloop))
443 SubloopParents[Subloop] = Unloop;
444 NearLoop = SubloopParents[Subloop];
445 }
446
447 succ_iterator I = succ_begin(BB), E = succ_end(BB);
448 if (I == E) {
449 assert(!Subloop && "subloop blocks must have a successor");
450 NearLoop = 0; // unloop blocks may now exit the function.
451 }
452 for (; I != E; ++I) {
453 if (*I == BB)
454 continue; // self loops are uninteresting
455
456 Loop *L = LI->getLoopFor(*I);
457 if (L == Unloop) {
458 // This successor has not been processed. This path must lead to an
459 // irreducible backedge.
460 assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
461 FoundIB = true;
462 }
463 if (L != Unloop && Unloop->contains(L)) {
464 // Successor is in a subloop.
465 if (Subloop)
466 continue; // Branching within subloops. Ignore it.
467
468 // BB branches from the original into a subloop header.
469 assert(L->getParentLoop() == Unloop && "cannot skip into nested loops");
470
471 // Get the current nearest parent of the Subloop's exits.
472 L = SubloopParents[L];
473 // L could be Unloop if the only exit was an irreducible backedge.
474 }
475 if (L == Unloop) {
476 continue;
477 }
478 // Handle critical edges from Unloop into a sibling loop.
479 if (L && !L->contains(Unloop)) {
480 L = L->getParentLoop();
481 }
482 // Remember the nearest parent loop among successors or subloop exits.
483 if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L))
484 NearLoop = L;
485 }
486 if (Subloop) {
487 SubloopParents[Subloop] = NearLoop;
488 return BBLoop;
489 }
490 return NearLoop;
491}
492
493//===----------------------------------------------------------------------===//
Chris Lattnera59cbb22002-07-27 01:12:17 +0000494// LoopInfo implementation
495//
Chris Lattnera59cbb22002-07-27 01:12:17 +0000496bool LoopInfo::runOnFunction(Function &) {
497 releaseMemory();
Dan Gohman9d59d9f2009-06-27 21:22:48 +0000498 LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update
Chris Lattnera59cbb22002-07-27 01:12:17 +0000499 return false;
500}
501
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000502/// updateUnloop - The last backedge has been removed from a loop--now the
503/// "unloop". Find a new parent for the blocks contained within unloop and
Andrew Trick5c1ff1f2011-08-11 17:54:58 +0000504/// update the loop tree. We don't necessarily have valid dominators at this
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000505/// point, but LoopInfo is still valid except for the removal of this loop.
506///
507/// Note that Unloop may now be an empty loop. Calling Loop::getHeader without
508/// checking first is illegal.
509void LoopInfo::updateUnloop(Loop *Unloop) {
510
511 // First handle the special case of no parent loop to simplify the algorithm.
512 if (!Unloop->getParentLoop()) {
513 // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
514 for (Loop::block_iterator I = Unloop->block_begin(),
515 E = Unloop->block_end(); I != E; ++I) {
516
517 // Don't reparent blocks in subloops.
518 if (getLoopFor(*I) != Unloop)
519 continue;
520
521 // Blocks no longer have a parent but are still referenced by Unloop until
522 // the Unloop object is deleted.
523 LI.changeLoopFor(*I, 0);
524 }
525
526 // Remove the loop from the top-level LoopInfo object.
Duncan Sands1f6a3292011-08-12 14:54:45 +0000527 for (LoopInfo::iterator I = LI.begin();; ++I) {
528 assert(I != LI.end() && "Couldn't find loop");
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000529 if (*I == Unloop) {
530 LI.removeLoop(I);
531 break;
532 }
533 }
534
535 // Move all of the subloops to the top-level.
536 while (!Unloop->empty())
Andrew Trick5c1ff1f2011-08-11 17:54:58 +0000537 LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end())));
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000538
539 return;
540 }
541
542 // Update the parent loop for all blocks within the loop. Blocks within
543 // subloops will not change parents.
544 UnloopUpdater Updater(Unloop, this);
545 Updater.updateBlockParents();
546
Andrew Trickc12d9b92011-08-11 20:27:32 +0000547 // Remove blocks from former ancestor loops.
548 Updater.removeBlocksFromAncestors();
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000549
550 // Add direct subloops as children in their new parent loop.
551 Updater.updateSubloopParents();
552
553 // Remove unloop from its parent loop.
554 Loop *ParentLoop = Unloop->getParentLoop();
Duncan Sands1f6a3292011-08-12 14:54:45 +0000555 for (Loop::iterator I = ParentLoop->begin();; ++I) {
556 assert(I != ParentLoop->end() && "Couldn't find loop");
Andrew Trickfb62b8d2011-08-10 23:22:57 +0000557 if (*I == Unloop) {
558 ParentLoop->removeChildLoop(I);
559 break;
560 }
561 }
562}
563
Dan Gohman5c89b522009-09-08 15:45:00 +0000564void LoopInfo::verifyAnalysis() const {
Dan Gohman9450b0e2009-09-28 00:27:48 +0000565 // LoopInfo is a FunctionPass, but verifying every loop in the function
566 // each time verifyAnalysis is called is very expensive. The
567 // -verify-loop-info option can enable this. In order to perform some
568 // checking by default, LoopPass has been taught to call verifyLoop
569 // manually during loop pass sequences.
570
571 if (!VerifyLoopInfo) return;
572
Andrew Trick5434c1e2011-08-26 03:06:34 +0000573 DenseSet<const Loop*> Loops;
Dan Gohman5c89b522009-09-08 15:45:00 +0000574 for (iterator I = begin(), E = end(); I != E; ++I) {
575 assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
Andrew Trick5434c1e2011-08-26 03:06:34 +0000576 (*I)->verifyLoopNest(&Loops);
Dan Gohman5c89b522009-09-08 15:45:00 +0000577 }
Dan Gohman9450b0e2009-09-28 00:27:48 +0000578
Andrew Trick5434c1e2011-08-26 03:06:34 +0000579 // Verify that blocks are mapped to valid loops.
580 //
581 // FIXME: With an up-to-date DFS (see LoopIterator.h) and DominatorTree, we
582 // could also verify that the blocks are still in the correct loops.
583 for (DenseMap<BasicBlock*, Loop*>::const_iterator I = LI.BBMap.begin(),
584 E = LI.BBMap.end(); I != E; ++I) {
585 assert(Loops.count(I->second) && "orphaned loop");
586 assert(I->second->contains(I->first) && "orphaned block");
587 }
Dan Gohman5c89b522009-09-08 15:45:00 +0000588}
589
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000590void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000591 AU.setPreservesAll();
Devang Patel53c279b2007-06-08 00:17:13 +0000592 AU.addRequired<DominatorTree>();
Chris Lattner93193f82002-01-31 00:42:27 +0000593}
Chris Lattner791102f2009-08-23 05:17:37 +0000594
Chris Lattner45cfe542009-08-23 06:03:38 +0000595void LoopInfo::print(raw_ostream &OS, const Module*) const {
596 LI.print(OS);
Chris Lattner791102f2009-08-23 05:17:37 +0000597}
598
Andrew Trick2d31ae32011-08-10 01:59:05 +0000599//===----------------------------------------------------------------------===//
600// LoopBlocksDFS implementation
601//
602
603/// Traverse the loop blocks and store the DFS result.
604/// Useful for clients that just want the final DFS result and don't need to
605/// visit blocks during the initial traversal.
606void LoopBlocksDFS::perform(LoopInfo *LI) {
607 LoopBlocksTraversal Traversal(*this, LI);
608 for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
609 POE = Traversal.end(); POI != POE; ++POI) ;
610}