Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 9 | // |
| 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 Brukman | 10d208d | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 92020fa | 2004-04-15 15:16:02 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Analysis/Dominators.h" |
Andrew Trick | cbf24b4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopInfoImpl.h" |
Andrew Trick | 2d31ae3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopIterator.h" |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 24 | #include "llvm/Assembly/Writer.h" |
Misha Brukman | 10d208d | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DepthFirstIterator.h" |
Chris Lattner | b1f5d8b | 2007-03-04 04:06:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | 46758a8 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Andrew Trick | cbf24b4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 33 | // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops. |
| 34 | template class llvm::LoopBase<BasicBlock, Loop>; |
| 35 | template class llvm::LoopInfoBase<BasicBlock, Loop>; |
| 36 | |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 37 | // Always verify loopinfo if expensive checking is enabled. |
| 38 | #ifdef XDEBUG |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 39 | static bool VerifyLoopInfo = true; |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 40 | #else |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 41 | static bool VerifyLoopInfo = false; |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 42 | #endif |
| 43 | static cl::opt<bool,true> |
| 44 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
| 45 | cl::desc("Verify loop info (time consuming)")); |
| 46 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 47 | char LoopInfo::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 48 | INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) |
| 49 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 50 | INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 51 | |
| 52 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 53 | // Loop implementation |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 54 | // |
Misha Brukman | 6b290a5 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 55 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 56 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 57 | /// |
| 58 | bool Loop::isLoopInvariant(Value *V) const { |
| 59 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 60 | return !contains(I); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 61 | return true; // All non-instructions are loop invariant |
| 62 | } |
| 63 | |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 64 | /// hasLoopInvariantOperands - Return true if all the operands of the |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 65 | /// specified instruction are loop invariant. |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 66 | bool Loop::hasLoopInvariantOperands(Instruction *I) const { |
| 67 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 68 | if (!isLoopInvariant(I->getOperand(i))) |
| 69 | return false; |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 70 | |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 71 | return true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// makeLoopInvariant - If the given value is an instruciton inside of the |
| 75 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 76 | /// Return true if the value after any hoisting is loop invariant. This |
| 77 | /// function can be used as a slightly more aggressive replacement for |
| 78 | /// isLoopInvariant. |
| 79 | /// |
| 80 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 81 | /// If null, the terminator of the loop preheader is used. |
| 82 | /// |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 83 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, |
| 84 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 85 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 86 | return makeLoopInvariant(I, Changed, InsertPt); |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 87 | return true; // All non-instructions are loop-invariant. |
| 88 | } |
| 89 | |
| 90 | /// makeLoopInvariant - If the given instruction is inside of the |
| 91 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 92 | /// Return true if the instruction after any hoisting is loop invariant. This |
| 93 | /// function can be used as a slightly more aggressive replacement for |
| 94 | /// isLoopInvariant. |
| 95 | /// |
| 96 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 97 | /// If null, the terminator of the loop preheader is used. |
| 98 | /// |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 99 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, |
| 100 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 101 | // Test if the value is already loop-invariant. |
| 102 | if (isLoopInvariant(I)) |
| 103 | return true; |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 104 | if (!isSafeToSpeculativelyExecute(I)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 105 | return false; |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 106 | if (I->mayReadFromMemory()) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 107 | return false; |
Bill Wendling | c9b2a98 | 2011-08-17 20:36:44 +0000 | [diff] [blame] | 108 | // The landingpad instruction is immobile. |
| 109 | if (isa<LandingPadInst>(I)) |
| 110 | return false; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 111 | // Determine the insertion point, unless one was given. |
| 112 | if (!InsertPt) { |
| 113 | BasicBlock *Preheader = getLoopPreheader(); |
| 114 | // Without a preheader, hoisting is not feasible. |
| 115 | if (!Preheader) |
| 116 | return false; |
| 117 | InsertPt = Preheader->getTerminator(); |
| 118 | } |
| 119 | // Don't hoist instructions with loop-variant operands. |
| 120 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 121 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 122 | return false; |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 123 | |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 124 | // Hoist. |
| 125 | I->moveBefore(InsertPt); |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 126 | Changed = true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 130 | /// getCanonicalInductionVariable - Check to see if the loop has a canonical |
| 131 | /// induction variable: an integer recurrence that starts at 0 and increments |
| 132 | /// by one each time through the loop. If so, return the phi node that |
| 133 | /// corresponds to it. |
| 134 | /// |
| 135 | /// The IndVarSimplify pass transforms loops to have a canonical induction |
| 136 | /// variable. |
| 137 | /// |
| 138 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 139 | BasicBlock *H = getHeader(); |
| 140 | |
| 141 | BasicBlock *Incoming = 0, *Backedge = 0; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 142 | pred_iterator PI = pred_begin(H); |
| 143 | assert(PI != pred_end(H) && |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 144 | "Loop must have at least one backedge!"); |
| 145 | Backedge = *PI++; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 146 | if (PI == pred_end(H)) return 0; // dead loop |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 147 | Incoming = *PI++; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 148 | if (PI != pred_end(H)) return 0; // multiple backedges? |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 149 | |
| 150 | if (contains(Incoming)) { |
| 151 | if (contains(Backedge)) |
| 152 | return 0; |
| 153 | std::swap(Incoming, Backedge); |
| 154 | } else if (!contains(Backedge)) |
| 155 | return 0; |
| 156 | |
| 157 | // Loop over all of the PHI nodes, looking for a canonical indvar. |
| 158 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
| 159 | PHINode *PN = cast<PHINode>(I); |
| 160 | if (ConstantInt *CI = |
| 161 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) |
| 162 | if (CI->isNullValue()) |
| 163 | if (Instruction *Inc = |
| 164 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) |
| 165 | if (Inc->getOpcode() == Instruction::Add && |
| 166 | Inc->getOperand(0) == PN) |
| 167 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
| 168 | if (CI->equalsInt(1)) |
| 169 | return PN; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 174 | /// isLCSSAForm - Return true if the Loop is in LCSSA form |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 175 | bool Loop::isLCSSAForm(DominatorTree &DT) const { |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 176 | // Sort the blocks vector so that we can use binary search to do quick |
| 177 | // lookups. |
Gabor Greif | 5891ac8 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 178 | SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end()); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 179 | |
| 180 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
Dan Gohman | 81d893c | 2009-11-09 18:19:43 +0000 | [diff] [blame] | 181 | BasicBlock *BB = *BI; |
| 182 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 183 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 184 | ++UI) { |
Gabor Greif | 5891ac8 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 185 | User *U = *UI; |
| 186 | BasicBlock *UserBB = cast<Instruction>(U)->getParent(); |
| 187 | if (PHINode *P = dyn_cast<PHINode>(U)) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 188 | UserBB = P->getIncomingBlock(UI); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 189 | |
Dan Gohman | cbac7f1 | 2010-03-09 01:53:33 +0000 | [diff] [blame] | 190 | // Check the current block, as a fast-path, before checking whether |
| 191 | // the use is anywhere in the loop. Most values are used in the same |
| 192 | // block they are defined in. Also, blocks not reachable from the |
| 193 | // entry are special; uses in them don't need to go through PHIs. |
| 194 | if (UserBB != BB && |
| 195 | !LoopBBs.count(UserBB) && |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 196 | DT.isReachableFromEntry(UserBB)) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 203 | |
| 204 | /// isLoopSimplifyForm - Return true if the Loop is in the form that |
| 205 | /// the LoopSimplify form transforms loops to, which is sometimes called |
| 206 | /// normal form. |
| 207 | bool Loop::isLoopSimplifyForm() const { |
Dan Gohman | f17e951 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 208 | // Normal-form loops have a preheader, a single backedge, and all of their |
| 209 | // exits have all their predecessors inside the loop. |
| 210 | return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); |
| 211 | } |
| 212 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 213 | /// isSafeToClone - Return true if the loop body is safe to clone in practice. |
| 214 | /// Routines that reform the loop CFG and split edges often fail on indirectbr. |
| 215 | bool Loop::isSafeToClone() const { |
| 216 | // Return false if any loop blocks contain indirectbrs. |
| 217 | for (Loop::block_iterator I = block_begin(), E = block_end(); I != E; ++I) { |
| 218 | if (isa<IndirectBrInst>((*I)->getTerminator())) |
| 219 | return false; |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
Dan Gohman | f17e951 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 224 | /// hasDedicatedExits - Return true if no exit block for the loop |
| 225 | /// has a predecessor that is outside the loop. |
| 226 | bool Loop::hasDedicatedExits() const { |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 227 | // Sort the blocks vector so that we can use binary search to do quick |
| 228 | // lookups. |
| 229 | SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 230 | // Each predecessor of each exit block of a normal loop is contained |
| 231 | // within the loop. |
| 232 | SmallVector<BasicBlock *, 4> ExitBlocks; |
| 233 | getExitBlocks(ExitBlocks); |
| 234 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 235 | for (pred_iterator PI = pred_begin(ExitBlocks[i]), |
| 236 | PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 237 | if (!LoopBBs.count(*PI)) |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 238 | return false; |
| 239 | // All the requirements are met. |
| 240 | return true; |
| 241 | } |
| 242 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 243 | /// getUniqueExitBlocks - Return all unique successor blocks of this loop. |
| 244 | /// These are the blocks _outside of the current loop_ which are branched to. |
Dan Gohman | 050959c | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 245 | /// This assumes that loop exits are in canonical form. |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 246 | /// |
| 247 | void |
| 248 | Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { |
Dan Gohman | 050959c | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 249 | assert(hasDedicatedExits() && |
| 250 | "getUniqueExitBlocks assumes the loop has canonical form exits!"); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 251 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 252 | // Sort the blocks vector so that we can use binary search to do quick |
| 253 | // lookups. |
| 254 | SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end()); |
| 255 | std::sort(LoopBBs.begin(), LoopBBs.end()); |
| 256 | |
Dan Gohman | 058db92 | 2009-09-03 20:36:13 +0000 | [diff] [blame] | 257 | SmallVector<BasicBlock *, 32> switchExitBlocks; |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 258 | |
| 259 | for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { |
| 260 | |
| 261 | BasicBlock *current = *BI; |
| 262 | switchExitBlocks.clear(); |
| 263 | |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 264 | for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) { |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 265 | // If block is inside the loop then it is not a exit block. |
| 266 | if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) |
| 267 | continue; |
| 268 | |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 269 | pred_iterator PI = pred_begin(*I); |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 270 | BasicBlock *firstPred = *PI; |
| 271 | |
| 272 | // If current basic block is this exit block's first predecessor |
| 273 | // then only insert exit block in to the output ExitBlocks vector. |
| 274 | // This ensures that same exit block is not inserted twice into |
| 275 | // ExitBlocks vector. |
| 276 | if (current != firstPred) |
| 277 | continue; |
| 278 | |
| 279 | // If a terminator has more then two successors, for example SwitchInst, |
| 280 | // then it is possible that there are multiple edges from current block |
| 281 | // to one exit block. |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 282 | if (std::distance(succ_begin(current), succ_end(current)) <= 2) { |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 283 | ExitBlocks.push_back(*I); |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | // In case of multiple edges from current block to exit block, collect |
| 288 | // only one edge in ExitBlocks. Use switchExitBlocks to keep track of |
| 289 | // duplicate edges. |
| 290 | if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) |
| 291 | == switchExitBlocks.end()) { |
| 292 | switchExitBlocks.push_back(*I); |
| 293 | ExitBlocks.push_back(*I); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one |
| 300 | /// block, return that block. Otherwise return null. |
| 301 | BasicBlock *Loop::getUniqueExitBlock() const { |
| 302 | SmallVector<BasicBlock *, 8> UniqueExitBlocks; |
| 303 | getUniqueExitBlocks(UniqueExitBlocks); |
| 304 | if (UniqueExitBlocks.size() == 1) |
| 305 | return UniqueExitBlocks[0]; |
| 306 | return 0; |
| 307 | } |
| 308 | |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 309 | void Loop::dump() const { |
| 310 | print(dbgs()); |
| 311 | } |
| 312 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 313 | //===----------------------------------------------------------------------===// |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 314 | // UnloopUpdater implementation |
| 315 | // |
| 316 | |
Benjamin Kramer | a67f14b | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 317 | namespace { |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 318 | /// Find the new parent loop for all blocks within the "unloop" whose last |
| 319 | /// backedges has just been removed. |
| 320 | class UnloopUpdater { |
| 321 | Loop *Unloop; |
| 322 | LoopInfo *LI; |
| 323 | |
| 324 | LoopBlocksDFS DFS; |
| 325 | |
| 326 | // Map unloop's immediate subloops to their nearest reachable parents. Nested |
| 327 | // loops within these subloops will not change parents. However, an immediate |
| 328 | // subloop's new parent will be the nearest loop reachable from either its own |
| 329 | // exits *or* any of its nested loop's exits. |
| 330 | DenseMap<Loop*, Loop*> SubloopParents; |
| 331 | |
| 332 | // Flag the presence of an irreducible backedge whose destination is a block |
| 333 | // directly contained by the original unloop. |
| 334 | bool FoundIB; |
| 335 | |
| 336 | public: |
| 337 | UnloopUpdater(Loop *UL, LoopInfo *LInfo) : |
| 338 | Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {} |
| 339 | |
| 340 | void updateBlockParents(); |
| 341 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 342 | void removeBlocksFromAncestors(); |
| 343 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 344 | void updateSubloopParents(); |
| 345 | |
| 346 | protected: |
| 347 | Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop); |
| 348 | }; |
Benjamin Kramer | a67f14b | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 349 | } // end anonymous namespace |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 350 | |
| 351 | /// updateBlockParents - Update the parent loop for all blocks that are directly |
| 352 | /// contained within the original "unloop". |
| 353 | void UnloopUpdater::updateBlockParents() { |
| 354 | if (Unloop->getNumBlocks()) { |
| 355 | // Perform a post order CFG traversal of all blocks within this loop, |
| 356 | // propagating the nearest loop from sucessors to predecessors. |
| 357 | LoopBlocksTraversal Traversal(DFS, LI); |
| 358 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 359 | POE = Traversal.end(); POI != POE; ++POI) { |
| 360 | |
| 361 | Loop *L = LI->getLoopFor(*POI); |
| 362 | Loop *NL = getNearestLoop(*POI, L); |
| 363 | |
| 364 | if (NL != L) { |
| 365 | // For reducible loops, NL is now an ancestor of Unloop. |
| 366 | assert((NL != Unloop && (!NL || NL->contains(Unloop))) && |
| 367 | "uninitialized successor"); |
| 368 | LI->changeLoopFor(*POI, NL); |
| 369 | } |
| 370 | else { |
| 371 | // Or the current block is part of a subloop, in which case its parent |
| 372 | // is unchanged. |
| 373 | assert((FoundIB || Unloop->contains(L)) && "uninitialized successor"); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | // Each irreducible loop within the unloop induces a round of iteration using |
| 378 | // the DFS result cached by Traversal. |
| 379 | bool Changed = FoundIB; |
| 380 | for (unsigned NIters = 0; Changed; ++NIters) { |
| 381 | assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm"); |
| 382 | |
| 383 | // Iterate over the postorder list of blocks, propagating the nearest loop |
| 384 | // from successors to predecessors as before. |
| 385 | Changed = false; |
| 386 | for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), |
| 387 | POE = DFS.endPostorder(); POI != POE; ++POI) { |
| 388 | |
| 389 | Loop *L = LI->getLoopFor(*POI); |
| 390 | Loop *NL = getNearestLoop(*POI, L); |
| 391 | if (NL != L) { |
| 392 | assert(NL != Unloop && (!NL || NL->contains(Unloop)) && |
| 393 | "uninitialized successor"); |
| 394 | LI->changeLoopFor(*POI, NL); |
| 395 | Changed = true; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 401 | /// removeBlocksFromAncestors - Remove unloop's blocks from all ancestors below |
| 402 | /// their new parents. |
| 403 | void UnloopUpdater::removeBlocksFromAncestors() { |
Andrew Trick | 5865a8d | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 404 | // Remove all unloop's blocks (including those in nested subloops) from |
| 405 | // ancestors below the new parent loop. |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 406 | for (Loop::block_iterator BI = Unloop->block_begin(), |
| 407 | BE = Unloop->block_end(); BI != BE; ++BI) { |
Andrew Trick | 5865a8d | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 408 | Loop *OuterParent = LI->getLoopFor(*BI); |
| 409 | if (Unloop->contains(OuterParent)) { |
| 410 | while (OuterParent->getParentLoop() != Unloop) |
| 411 | OuterParent = OuterParent->getParentLoop(); |
| 412 | OuterParent = SubloopParents[OuterParent]; |
| 413 | } |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 414 | // Remove blocks from former Ancestors except Unloop itself which will be |
| 415 | // deleted. |
Andrew Trick | 5865a8d | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 416 | for (Loop *OldParent = Unloop->getParentLoop(); OldParent != OuterParent; |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 417 | OldParent = OldParent->getParentLoop()) { |
| 418 | assert(OldParent && "new loop is not an ancestor of the original"); |
| 419 | OldParent->removeBlockFromLoop(*BI); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 424 | /// updateSubloopParents - Update the parent loop for all subloops directly |
| 425 | /// nested within unloop. |
| 426 | void UnloopUpdater::updateSubloopParents() { |
| 427 | while (!Unloop->empty()) { |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 428 | Loop *Subloop = *llvm::prior(Unloop->end()); |
| 429 | Unloop->removeChildLoop(llvm::prior(Unloop->end())); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 430 | |
| 431 | assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); |
| 432 | if (SubloopParents[Subloop]) |
| 433 | SubloopParents[Subloop]->addChildLoop(Subloop); |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 434 | else |
| 435 | LI->addTopLevelLoop(Subloop); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | /// getNearestLoop - Return the nearest parent loop among this block's |
| 440 | /// successors. If a successor is a subloop header, consider its parent to be |
| 441 | /// the nearest parent of the subloop's exits. |
| 442 | /// |
| 443 | /// For subloop blocks, simply update SubloopParents and return NULL. |
| 444 | Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { |
| 445 | |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 446 | // Initially for blocks directly contained by Unloop, NearLoop == Unloop and |
| 447 | // is considered uninitialized. |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 448 | Loop *NearLoop = BBLoop; |
| 449 | |
| 450 | Loop *Subloop = 0; |
| 451 | if (NearLoop != Unloop && Unloop->contains(NearLoop)) { |
| 452 | Subloop = NearLoop; |
| 453 | // Find the subloop ancestor that is directly contained within Unloop. |
| 454 | while (Subloop->getParentLoop() != Unloop) { |
| 455 | Subloop = Subloop->getParentLoop(); |
| 456 | assert(Subloop && "subloop is not an ancestor of the original loop"); |
| 457 | } |
| 458 | // Get the current nearest parent of the Subloop exits, initially Unloop. |
| 459 | if (!SubloopParents.count(Subloop)) |
| 460 | SubloopParents[Subloop] = Unloop; |
| 461 | NearLoop = SubloopParents[Subloop]; |
| 462 | } |
| 463 | |
| 464 | succ_iterator I = succ_begin(BB), E = succ_end(BB); |
| 465 | if (I == E) { |
| 466 | assert(!Subloop && "subloop blocks must have a successor"); |
| 467 | NearLoop = 0; // unloop blocks may now exit the function. |
| 468 | } |
| 469 | for (; I != E; ++I) { |
| 470 | if (*I == BB) |
| 471 | continue; // self loops are uninteresting |
| 472 | |
| 473 | Loop *L = LI->getLoopFor(*I); |
| 474 | if (L == Unloop) { |
| 475 | // This successor has not been processed. This path must lead to an |
| 476 | // irreducible backedge. |
| 477 | assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB"); |
| 478 | FoundIB = true; |
| 479 | } |
| 480 | if (L != Unloop && Unloop->contains(L)) { |
| 481 | // Successor is in a subloop. |
| 482 | if (Subloop) |
| 483 | continue; // Branching within subloops. Ignore it. |
| 484 | |
| 485 | // BB branches from the original into a subloop header. |
| 486 | assert(L->getParentLoop() == Unloop && "cannot skip into nested loops"); |
| 487 | |
| 488 | // Get the current nearest parent of the Subloop's exits. |
| 489 | L = SubloopParents[L]; |
| 490 | // L could be Unloop if the only exit was an irreducible backedge. |
| 491 | } |
| 492 | if (L == Unloop) { |
| 493 | continue; |
| 494 | } |
| 495 | // Handle critical edges from Unloop into a sibling loop. |
| 496 | if (L && !L->contains(Unloop)) { |
| 497 | L = L->getParentLoop(); |
| 498 | } |
| 499 | // Remember the nearest parent loop among successors or subloop exits. |
| 500 | if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L)) |
| 501 | NearLoop = L; |
| 502 | } |
| 503 | if (Subloop) { |
| 504 | SubloopParents[Subloop] = NearLoop; |
| 505 | return BBLoop; |
| 506 | } |
| 507 | return NearLoop; |
| 508 | } |
| 509 | |
| 510 | //===----------------------------------------------------------------------===// |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 511 | // LoopInfo implementation |
| 512 | // |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 513 | bool LoopInfo::runOnFunction(Function &) { |
| 514 | releaseMemory(); |
Andrew Trick | c9b1e25 | 2012-06-26 04:11:38 +0000 | [diff] [blame] | 515 | LI.Analyze(getAnalysis<DominatorTree>().getBase()); |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 519 | /// updateUnloop - The last backedge has been removed from a loop--now the |
| 520 | /// "unloop". Find a new parent for the blocks contained within unloop and |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 521 | /// update the loop tree. We don't necessarily have valid dominators at this |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 522 | /// point, but LoopInfo is still valid except for the removal of this loop. |
| 523 | /// |
| 524 | /// Note that Unloop may now be an empty loop. Calling Loop::getHeader without |
| 525 | /// checking first is illegal. |
| 526 | void LoopInfo::updateUnloop(Loop *Unloop) { |
| 527 | |
| 528 | // First handle the special case of no parent loop to simplify the algorithm. |
| 529 | if (!Unloop->getParentLoop()) { |
| 530 | // Since BBLoop had no parent, Unloop blocks are no longer in a loop. |
| 531 | for (Loop::block_iterator I = Unloop->block_begin(), |
| 532 | E = Unloop->block_end(); I != E; ++I) { |
| 533 | |
| 534 | // Don't reparent blocks in subloops. |
| 535 | if (getLoopFor(*I) != Unloop) |
| 536 | continue; |
| 537 | |
| 538 | // Blocks no longer have a parent but are still referenced by Unloop until |
| 539 | // the Unloop object is deleted. |
| 540 | LI.changeLoopFor(*I, 0); |
| 541 | } |
| 542 | |
| 543 | // Remove the loop from the top-level LoopInfo object. |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 544 | for (LoopInfo::iterator I = LI.begin();; ++I) { |
| 545 | assert(I != LI.end() && "Couldn't find loop"); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 546 | if (*I == Unloop) { |
| 547 | LI.removeLoop(I); |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // Move all of the subloops to the top-level. |
| 553 | while (!Unloop->empty()) |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 554 | LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end()))); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 555 | |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | // Update the parent loop for all blocks within the loop. Blocks within |
| 560 | // subloops will not change parents. |
| 561 | UnloopUpdater Updater(Unloop, this); |
| 562 | Updater.updateBlockParents(); |
| 563 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 564 | // Remove blocks from former ancestor loops. |
| 565 | Updater.removeBlocksFromAncestors(); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 566 | |
| 567 | // Add direct subloops as children in their new parent loop. |
| 568 | Updater.updateSubloopParents(); |
| 569 | |
| 570 | // Remove unloop from its parent loop. |
| 571 | Loop *ParentLoop = Unloop->getParentLoop(); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 572 | for (Loop::iterator I = ParentLoop->begin();; ++I) { |
| 573 | assert(I != ParentLoop->end() && "Couldn't find loop"); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 574 | if (*I == Unloop) { |
| 575 | ParentLoop->removeChildLoop(I); |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 581 | void LoopInfo::verifyAnalysis() const { |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 582 | // LoopInfo is a FunctionPass, but verifying every loop in the function |
| 583 | // each time verifyAnalysis is called is very expensive. The |
| 584 | // -verify-loop-info option can enable this. In order to perform some |
| 585 | // checking by default, LoopPass has been taught to call verifyLoop |
| 586 | // manually during loop pass sequences. |
| 587 | |
| 588 | if (!VerifyLoopInfo) return; |
| 589 | |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 590 | DenseSet<const Loop*> Loops; |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 591 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 592 | assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 593 | (*I)->verifyLoopNest(&Loops); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 594 | } |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 595 | |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 596 | // Verify that blocks are mapped to valid loops. |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 597 | for (DenseMap<BasicBlock*, Loop*>::const_iterator I = LI.BBMap.begin(), |
| 598 | E = LI.BBMap.end(); I != E; ++I) { |
| 599 | assert(Loops.count(I->second) && "orphaned loop"); |
| 600 | assert(I->second->contains(I->first) && "orphaned block"); |
| 601 | } |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 604 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 605 | AU.setPreservesAll(); |
Devang Patel | 53c279b | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 606 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 609 | void LoopInfo::print(raw_ostream &OS, const Module*) const { |
| 610 | LI.print(OS); |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Andrew Trick | 2d31ae3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 613 | //===----------------------------------------------------------------------===// |
| 614 | // LoopBlocksDFS implementation |
| 615 | // |
| 616 | |
| 617 | /// Traverse the loop blocks and store the DFS result. |
| 618 | /// Useful for clients that just want the final DFS result and don't need to |
| 619 | /// visit blocks during the initial traversal. |
| 620 | void LoopBlocksDFS::perform(LoopInfo *LI) { |
| 621 | LoopBlocksTraversal Traversal(*this, LI); |
| 622 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 623 | POE = Traversal.end(); POI != POE; ++POI) ; |
| 624 | } |