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 | 2d31ae3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopIterator.h" |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 22 | #include "llvm/Assembly/Writer.h" |
Misha Brukman | 10d208d | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DepthFirstIterator.h" |
Chris Lattner | b1f5d8b | 2007-03-04 04:06:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 46758a8 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 31 | // Always verify loopinfo if expensive checking is enabled. |
| 32 | #ifdef XDEBUG |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 33 | static bool VerifyLoopInfo = true; |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 34 | #else |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 35 | static bool VerifyLoopInfo = false; |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 36 | #endif |
| 37 | static cl::opt<bool,true> |
| 38 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
| 39 | cl::desc("Verify loop info (time consuming)")); |
| 40 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 41 | char LoopInfo::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 42 | INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) |
| 43 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 44 | INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 45 | |
| 46 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 47 | // Loop implementation |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 48 | // |
Misha Brukman | 6b290a5 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 49 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 50 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 51 | /// |
| 52 | bool Loop::isLoopInvariant(Value *V) const { |
| 53 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 54 | return !contains(I); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 55 | return true; // All non-instructions are loop invariant |
| 56 | } |
| 57 | |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 58 | /// hasLoopInvariantOperands - Return true if all the operands of the |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 59 | /// specified instruction are loop invariant. |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 60 | bool 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 Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 64 | |
Chris Lattner | adc7991 | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 65 | return true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 66 | } |
| 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 Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 77 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, |
| 78 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 79 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 80 | return makeLoopInvariant(I, Changed, InsertPt); |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 81 | 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 Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 93 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, |
| 94 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 95 | // Test if the value is already loop-invariant. |
| 96 | if (isLoopInvariant(I)) |
| 97 | return true; |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 98 | if (!I->isSafeToSpeculativelyExecute()) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 99 | return false; |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 100 | if (I->mayReadFromMemory()) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 101 | return false; |
Bill Wendling | c9b2a98 | 2011-08-17 20:36:44 +0000 | [diff] [blame] | 102 | // The landingpad instruction is immobile. |
| 103 | if (isa<LandingPadInst>(I)) |
| 104 | return false; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 105 | // 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 Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 115 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 116 | return false; |
Andrew Trick | 882bcc6 | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 117 | |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 118 | // Hoist. |
| 119 | I->moveBefore(InsertPt); |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 120 | Changed = true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 124 | /// 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 | /// |
| 132 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 133 | BasicBlock *H = getHeader(); |
| 134 | |
| 135 | BasicBlock *Incoming = 0, *Backedge = 0; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 136 | pred_iterator PI = pred_begin(H); |
| 137 | assert(PI != pred_end(H) && |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 138 | "Loop must have at least one backedge!"); |
| 139 | Backedge = *PI++; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 140 | if (PI == pred_end(H)) return 0; // dead loop |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 141 | Incoming = *PI++; |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 142 | if (PI != pred_end(H)) return 0; // multiple backedges? |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 143 | |
| 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 Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 168 | /// getTripCount - Return a loop-invariant LLVM value indicating the number of |
| 169 | /// times the loop will be executed. Note that this means that the backedge |
| 170 | /// of the loop executes N-1 times. If the trip-count cannot be determined, |
| 171 | /// this returns null. |
| 172 | /// |
| 173 | /// The IndVarSimplify pass transforms loops to have a form that this |
| 174 | /// function easily understands. |
| 175 | /// |
| 176 | Value *Loop::getTripCount() const { |
| 177 | // Canonical loops will end with a 'cmp ne I, V', where I is the incremented |
| 178 | // canonical induction variable and V is the trip count of the loop. |
Dan Gohman | f8336a7 | 2010-07-23 21:34:51 +0000 | [diff] [blame] | 179 | PHINode *IV = getCanonicalInductionVariable(); |
| 180 | if (IV == 0 || IV->getNumIncomingValues() != 2) return 0; |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 181 | |
Dan Gohman | f8336a7 | 2010-07-23 21:34:51 +0000 | [diff] [blame] | 182 | bool P0InLoop = contains(IV->getIncomingBlock(0)); |
| 183 | Value *Inc = IV->getIncomingValue(!P0InLoop); |
| 184 | BasicBlock *BackedgeBlock = IV->getIncomingBlock(!P0InLoop); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 185 | |
| 186 | if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) |
| 187 | if (BI->isConditional()) { |
| 188 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { |
| 189 | if (ICI->getOperand(0) == Inc) { |
| 190 | if (BI->getSuccessor(0) == getHeader()) { |
| 191 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 192 | return ICI->getOperand(1); |
| 193 | } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { |
| 194 | return ICI->getOperand(1); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /// getSmallConstantTripCount - Returns the trip count of this loop as a |
| 204 | /// normal unsigned value, if possible. Returns 0 if the trip count is unknown |
Duncan Sands | 768b87c | 2010-11-13 12:16:27 +0000 | [diff] [blame] | 205 | /// or not constant. Will also return 0 if the trip count is very large |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 206 | /// (>= 2^32) |
| 207 | unsigned Loop::getSmallConstantTripCount() const { |
| 208 | Value* TripCount = this->getTripCount(); |
| 209 | if (TripCount) { |
| 210 | if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) { |
| 211 | // Guard against huge trip counts. |
| 212 | if (TripCountC->getValue().getActiveBits() <= 32) { |
| 213 | return (unsigned)TripCountC->getZExtValue(); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /// getSmallConstantTripMultiple - Returns the largest constant divisor of the |
| 221 | /// trip count of this loop as a normal unsigned value, if possible. This |
| 222 | /// means that the actual trip count is always a multiple of the returned |
| 223 | /// value (don't forget the trip count could very well be zero as well!). |
| 224 | /// |
| 225 | /// Returns 1 if the trip count is unknown or not guaranteed to be the |
| 226 | /// multiple of a constant (which is also the case if the trip count is simply |
| 227 | /// constant, use getSmallConstantTripCount for that case), Will also return 1 |
| 228 | /// if the trip count is very large (>= 2^32). |
| 229 | unsigned Loop::getSmallConstantTripMultiple() const { |
| 230 | Value* TripCount = this->getTripCount(); |
| 231 | // This will hold the ConstantInt result, if any |
| 232 | ConstantInt *Result = NULL; |
| 233 | if (TripCount) { |
| 234 | // See if the trip count is constant itself |
| 235 | Result = dyn_cast<ConstantInt>(TripCount); |
| 236 | // if not, see if it is a multiplication |
| 237 | if (!Result) |
| 238 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) { |
| 239 | switch (BO->getOpcode()) { |
| 240 | case BinaryOperator::Mul: |
| 241 | Result = dyn_cast<ConstantInt>(BO->getOperand(1)); |
| 242 | break; |
Dan Gohman | ac14665 | 2009-11-20 01:09:34 +0000 | [diff] [blame] | 243 | case BinaryOperator::Shl: |
| 244 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 245 | if (CI->getValue().getActiveBits() <= 5) |
| 246 | return 1u << CI->getZExtValue(); |
| 247 | break; |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | // Guard against huge trip counts. |
| 254 | if (Result && Result->getValue().getActiveBits() <= 32) { |
| 255 | return (unsigned)Result->getZExtValue(); |
| 256 | } else { |
| 257 | return 1; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /// isLCSSAForm - Return true if the Loop is in LCSSA form |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 262 | bool Loop::isLCSSAForm(DominatorTree &DT) const { |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 263 | // Sort the blocks vector so that we can use binary search to do quick |
| 264 | // lookups. |
Gabor Greif | 5891ac8 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 265 | SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end()); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 266 | |
| 267 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
Dan Gohman | 81d893c | 2009-11-09 18:19:43 +0000 | [diff] [blame] | 268 | BasicBlock *BB = *BI; |
| 269 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 270 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 271 | ++UI) { |
Gabor Greif | 5891ac8 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 272 | User *U = *UI; |
| 273 | BasicBlock *UserBB = cast<Instruction>(U)->getParent(); |
| 274 | if (PHINode *P = dyn_cast<PHINode>(U)) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 275 | UserBB = P->getIncomingBlock(UI); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 276 | |
Dan Gohman | cbac7f1 | 2010-03-09 01:53:33 +0000 | [diff] [blame] | 277 | // Check the current block, as a fast-path, before checking whether |
| 278 | // the use is anywhere in the loop. Most values are used in the same |
| 279 | // block they are defined in. Also, blocks not reachable from the |
| 280 | // entry are special; uses in them don't need to go through PHIs. |
| 281 | if (UserBB != BB && |
| 282 | !LoopBBs.count(UserBB) && |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 283 | DT.isReachableFromEntry(UserBB)) |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 290 | |
| 291 | /// isLoopSimplifyForm - Return true if the Loop is in the form that |
| 292 | /// the LoopSimplify form transforms loops to, which is sometimes called |
| 293 | /// normal form. |
| 294 | bool Loop::isLoopSimplifyForm() const { |
Dan Gohman | f17e951 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 295 | // Normal-form loops have a preheader, a single backedge, and all of their |
| 296 | // exits have all their predecessors inside the loop. |
| 297 | return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); |
| 298 | } |
| 299 | |
| 300 | /// hasDedicatedExits - Return true if no exit block for the loop |
| 301 | /// has a predecessor that is outside the loop. |
| 302 | bool Loop::hasDedicatedExits() const { |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 303 | // Sort the blocks vector so that we can use binary search to do quick |
| 304 | // lookups. |
| 305 | SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 306 | // Each predecessor of each exit block of a normal loop is contained |
| 307 | // within the loop. |
| 308 | SmallVector<BasicBlock *, 4> ExitBlocks; |
| 309 | getExitBlocks(ExitBlocks); |
| 310 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 311 | for (pred_iterator PI = pred_begin(ExitBlocks[i]), |
| 312 | PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 313 | if (!LoopBBs.count(*PI)) |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 314 | return false; |
| 315 | // All the requirements are met. |
| 316 | return true; |
| 317 | } |
| 318 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 319 | /// getUniqueExitBlocks - Return all unique successor blocks of this loop. |
| 320 | /// 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] | 321 | /// This assumes that loop exits are in canonical form. |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 322 | /// |
| 323 | void |
| 324 | Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { |
Dan Gohman | 050959c | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 325 | assert(hasDedicatedExits() && |
| 326 | "getUniqueExitBlocks assumes the loop has canonical form exits!"); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 327 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 328 | // Sort the blocks vector so that we can use binary search to do quick |
| 329 | // lookups. |
| 330 | SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end()); |
| 331 | std::sort(LoopBBs.begin(), LoopBBs.end()); |
| 332 | |
Dan Gohman | 058db92 | 2009-09-03 20:36:13 +0000 | [diff] [blame] | 333 | SmallVector<BasicBlock *, 32> switchExitBlocks; |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 334 | |
| 335 | for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { |
| 336 | |
| 337 | BasicBlock *current = *BI; |
| 338 | switchExitBlocks.clear(); |
| 339 | |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 340 | 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] | 341 | // If block is inside the loop then it is not a exit block. |
| 342 | if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) |
| 343 | continue; |
| 344 | |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 345 | pred_iterator PI = pred_begin(*I); |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 346 | BasicBlock *firstPred = *PI; |
| 347 | |
| 348 | // If current basic block is this exit block's first predecessor |
| 349 | // then only insert exit block in to the output ExitBlocks vector. |
| 350 | // This ensures that same exit block is not inserted twice into |
| 351 | // ExitBlocks vector. |
| 352 | if (current != firstPred) |
| 353 | continue; |
| 354 | |
| 355 | // If a terminator has more then two successors, for example SwitchInst, |
| 356 | // then it is possible that there are multiple edges from current block |
| 357 | // to one exit block. |
Dan Gohman | 63137d5 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 358 | if (std::distance(succ_begin(current), succ_end(current)) <= 2) { |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 359 | ExitBlocks.push_back(*I); |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | // In case of multiple edges from current block to exit block, collect |
| 364 | // only one edge in ExitBlocks. Use switchExitBlocks to keep track of |
| 365 | // duplicate edges. |
| 366 | if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) |
| 367 | == switchExitBlocks.end()) { |
| 368 | switchExitBlocks.push_back(*I); |
| 369 | ExitBlocks.push_back(*I); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one |
| 376 | /// block, return that block. Otherwise return null. |
| 377 | BasicBlock *Loop::getUniqueExitBlock() const { |
| 378 | SmallVector<BasicBlock *, 8> UniqueExitBlocks; |
| 379 | getUniqueExitBlocks(UniqueExitBlocks); |
| 380 | if (UniqueExitBlocks.size() == 1) |
| 381 | return UniqueExitBlocks[0]; |
| 382 | return 0; |
| 383 | } |
| 384 | |
Dan Gohman | dda30cd | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 385 | void Loop::dump() const { |
| 386 | print(dbgs()); |
| 387 | } |
| 388 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 389 | //===----------------------------------------------------------------------===// |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 390 | // UnloopUpdater implementation |
| 391 | // |
| 392 | |
Benjamin Kramer | a67f14b | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 393 | namespace { |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 394 | /// Find the new parent loop for all blocks within the "unloop" whose last |
| 395 | /// backedges has just been removed. |
| 396 | class UnloopUpdater { |
| 397 | Loop *Unloop; |
| 398 | LoopInfo *LI; |
| 399 | |
| 400 | LoopBlocksDFS DFS; |
| 401 | |
| 402 | // Map unloop's immediate subloops to their nearest reachable parents. Nested |
| 403 | // loops within these subloops will not change parents. However, an immediate |
| 404 | // subloop's new parent will be the nearest loop reachable from either its own |
| 405 | // exits *or* any of its nested loop's exits. |
| 406 | DenseMap<Loop*, Loop*> SubloopParents; |
| 407 | |
| 408 | // Flag the presence of an irreducible backedge whose destination is a block |
| 409 | // directly contained by the original unloop. |
| 410 | bool FoundIB; |
| 411 | |
| 412 | public: |
| 413 | UnloopUpdater(Loop *UL, LoopInfo *LInfo) : |
| 414 | Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {} |
| 415 | |
| 416 | void updateBlockParents(); |
| 417 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 418 | void removeBlocksFromAncestors(); |
| 419 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 420 | void updateSubloopParents(); |
| 421 | |
| 422 | protected: |
| 423 | Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop); |
| 424 | }; |
Benjamin Kramer | a67f14b | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 425 | } // end anonymous namespace |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 426 | |
| 427 | /// updateBlockParents - Update the parent loop for all blocks that are directly |
| 428 | /// contained within the original "unloop". |
| 429 | void UnloopUpdater::updateBlockParents() { |
| 430 | if (Unloop->getNumBlocks()) { |
| 431 | // Perform a post order CFG traversal of all blocks within this loop, |
| 432 | // propagating the nearest loop from sucessors to predecessors. |
| 433 | LoopBlocksTraversal Traversal(DFS, LI); |
| 434 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 435 | POE = Traversal.end(); POI != POE; ++POI) { |
| 436 | |
| 437 | Loop *L = LI->getLoopFor(*POI); |
| 438 | Loop *NL = getNearestLoop(*POI, L); |
| 439 | |
| 440 | if (NL != L) { |
| 441 | // For reducible loops, NL is now an ancestor of Unloop. |
| 442 | assert((NL != Unloop && (!NL || NL->contains(Unloop))) && |
| 443 | "uninitialized successor"); |
| 444 | LI->changeLoopFor(*POI, NL); |
| 445 | } |
| 446 | else { |
| 447 | // Or the current block is part of a subloop, in which case its parent |
| 448 | // is unchanged. |
| 449 | assert((FoundIB || Unloop->contains(L)) && "uninitialized successor"); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | // Each irreducible loop within the unloop induces a round of iteration using |
| 454 | // the DFS result cached by Traversal. |
| 455 | bool Changed = FoundIB; |
| 456 | for (unsigned NIters = 0; Changed; ++NIters) { |
| 457 | assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm"); |
| 458 | |
| 459 | // Iterate over the postorder list of blocks, propagating the nearest loop |
| 460 | // from successors to predecessors as before. |
| 461 | Changed = false; |
| 462 | for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), |
| 463 | POE = DFS.endPostorder(); POI != POE; ++POI) { |
| 464 | |
| 465 | Loop *L = LI->getLoopFor(*POI); |
| 466 | Loop *NL = getNearestLoop(*POI, L); |
| 467 | if (NL != L) { |
| 468 | assert(NL != Unloop && (!NL || NL->contains(Unloop)) && |
| 469 | "uninitialized successor"); |
| 470 | LI->changeLoopFor(*POI, NL); |
| 471 | Changed = true; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 477 | /// removeBlocksFromAncestors - Remove unloop's blocks from all ancestors below |
| 478 | /// their new parents. |
| 479 | void UnloopUpdater::removeBlocksFromAncestors() { |
| 480 | // Remove unloop's blocks from all ancestors below their new parents. |
| 481 | for (Loop::block_iterator BI = Unloop->block_begin(), |
| 482 | BE = Unloop->block_end(); BI != BE; ++BI) { |
| 483 | Loop *NewParent = LI->getLoopFor(*BI); |
| 484 | // If this block is an immediate subloop, remove all blocks (including |
| 485 | // nested subloops) from ancestors below the new parent loop. |
| 486 | // Otherwise, if this block is in a nested subloop, skip it. |
| 487 | if (SubloopParents.count(NewParent)) |
| 488 | NewParent = SubloopParents[NewParent]; |
| 489 | else if (Unloop->contains(NewParent)) |
| 490 | continue; |
| 491 | |
| 492 | // Remove blocks from former Ancestors except Unloop itself which will be |
| 493 | // deleted. |
| 494 | for (Loop *OldParent = Unloop->getParentLoop(); OldParent != NewParent; |
| 495 | OldParent = OldParent->getParentLoop()) { |
| 496 | assert(OldParent && "new loop is not an ancestor of the original"); |
| 497 | OldParent->removeBlockFromLoop(*BI); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 502 | /// updateSubloopParents - Update the parent loop for all subloops directly |
| 503 | /// nested within unloop. |
| 504 | void UnloopUpdater::updateSubloopParents() { |
| 505 | while (!Unloop->empty()) { |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 506 | Loop *Subloop = *llvm::prior(Unloop->end()); |
| 507 | Unloop->removeChildLoop(llvm::prior(Unloop->end())); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 508 | |
| 509 | assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); |
| 510 | if (SubloopParents[Subloop]) |
| 511 | SubloopParents[Subloop]->addChildLoop(Subloop); |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame^] | 512 | else |
| 513 | LI->addTopLevelLoop(Subloop); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | /// getNearestLoop - Return the nearest parent loop among this block's |
| 518 | /// successors. If a successor is a subloop header, consider its parent to be |
| 519 | /// the nearest parent of the subloop's exits. |
| 520 | /// |
| 521 | /// For subloop blocks, simply update SubloopParents and return NULL. |
| 522 | Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { |
| 523 | |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 524 | // Initially for blocks directly contained by Unloop, NearLoop == Unloop and |
| 525 | // is considered uninitialized. |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 526 | Loop *NearLoop = BBLoop; |
| 527 | |
| 528 | Loop *Subloop = 0; |
| 529 | if (NearLoop != Unloop && Unloop->contains(NearLoop)) { |
| 530 | Subloop = NearLoop; |
| 531 | // Find the subloop ancestor that is directly contained within Unloop. |
| 532 | while (Subloop->getParentLoop() != Unloop) { |
| 533 | Subloop = Subloop->getParentLoop(); |
| 534 | assert(Subloop && "subloop is not an ancestor of the original loop"); |
| 535 | } |
| 536 | // Get the current nearest parent of the Subloop exits, initially Unloop. |
| 537 | if (!SubloopParents.count(Subloop)) |
| 538 | SubloopParents[Subloop] = Unloop; |
| 539 | NearLoop = SubloopParents[Subloop]; |
| 540 | } |
| 541 | |
| 542 | succ_iterator I = succ_begin(BB), E = succ_end(BB); |
| 543 | if (I == E) { |
| 544 | assert(!Subloop && "subloop blocks must have a successor"); |
| 545 | NearLoop = 0; // unloop blocks may now exit the function. |
| 546 | } |
| 547 | for (; I != E; ++I) { |
| 548 | if (*I == BB) |
| 549 | continue; // self loops are uninteresting |
| 550 | |
| 551 | Loop *L = LI->getLoopFor(*I); |
| 552 | if (L == Unloop) { |
| 553 | // This successor has not been processed. This path must lead to an |
| 554 | // irreducible backedge. |
| 555 | assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB"); |
| 556 | FoundIB = true; |
| 557 | } |
| 558 | if (L != Unloop && Unloop->contains(L)) { |
| 559 | // Successor is in a subloop. |
| 560 | if (Subloop) |
| 561 | continue; // Branching within subloops. Ignore it. |
| 562 | |
| 563 | // BB branches from the original into a subloop header. |
| 564 | assert(L->getParentLoop() == Unloop && "cannot skip into nested loops"); |
| 565 | |
| 566 | // Get the current nearest parent of the Subloop's exits. |
| 567 | L = SubloopParents[L]; |
| 568 | // L could be Unloop if the only exit was an irreducible backedge. |
| 569 | } |
| 570 | if (L == Unloop) { |
| 571 | continue; |
| 572 | } |
| 573 | // Handle critical edges from Unloop into a sibling loop. |
| 574 | if (L && !L->contains(Unloop)) { |
| 575 | L = L->getParentLoop(); |
| 576 | } |
| 577 | // Remember the nearest parent loop among successors or subloop exits. |
| 578 | if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L)) |
| 579 | NearLoop = L; |
| 580 | } |
| 581 | if (Subloop) { |
| 582 | SubloopParents[Subloop] = NearLoop; |
| 583 | return BBLoop; |
| 584 | } |
| 585 | return NearLoop; |
| 586 | } |
| 587 | |
| 588 | //===----------------------------------------------------------------------===// |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 589 | // LoopInfo implementation |
| 590 | // |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 591 | bool LoopInfo::runOnFunction(Function &) { |
| 592 | releaseMemory(); |
Dan Gohman | 9d59d9f | 2009-06-27 21:22:48 +0000 | [diff] [blame] | 593 | LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 594 | return false; |
| 595 | } |
| 596 | |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 597 | /// updateUnloop - The last backedge has been removed from a loop--now the |
| 598 | /// "unloop". Find a new parent for the blocks contained within unloop and |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 599 | /// 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] | 600 | /// point, but LoopInfo is still valid except for the removal of this loop. |
| 601 | /// |
| 602 | /// Note that Unloop may now be an empty loop. Calling Loop::getHeader without |
| 603 | /// checking first is illegal. |
| 604 | void LoopInfo::updateUnloop(Loop *Unloop) { |
| 605 | |
| 606 | // First handle the special case of no parent loop to simplify the algorithm. |
| 607 | if (!Unloop->getParentLoop()) { |
| 608 | // Since BBLoop had no parent, Unloop blocks are no longer in a loop. |
| 609 | for (Loop::block_iterator I = Unloop->block_begin(), |
| 610 | E = Unloop->block_end(); I != E; ++I) { |
| 611 | |
| 612 | // Don't reparent blocks in subloops. |
| 613 | if (getLoopFor(*I) != Unloop) |
| 614 | continue; |
| 615 | |
| 616 | // Blocks no longer have a parent but are still referenced by Unloop until |
| 617 | // the Unloop object is deleted. |
| 618 | LI.changeLoopFor(*I, 0); |
| 619 | } |
| 620 | |
| 621 | // Remove the loop from the top-level LoopInfo object. |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 622 | for (LoopInfo::iterator I = LI.begin();; ++I) { |
| 623 | assert(I != LI.end() && "Couldn't find loop"); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 624 | if (*I == Unloop) { |
| 625 | LI.removeLoop(I); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Move all of the subloops to the top-level. |
| 631 | while (!Unloop->empty()) |
Andrew Trick | 5c1ff1f | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 632 | LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end()))); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 633 | |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | // Update the parent loop for all blocks within the loop. Blocks within |
| 638 | // subloops will not change parents. |
| 639 | UnloopUpdater Updater(Unloop, this); |
| 640 | Updater.updateBlockParents(); |
| 641 | |
Andrew Trick | c12d9b9 | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 642 | // Remove blocks from former ancestor loops. |
| 643 | Updater.removeBlocksFromAncestors(); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 644 | |
| 645 | // Add direct subloops as children in their new parent loop. |
| 646 | Updater.updateSubloopParents(); |
| 647 | |
| 648 | // Remove unloop from its parent loop. |
| 649 | Loop *ParentLoop = Unloop->getParentLoop(); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 650 | for (Loop::iterator I = ParentLoop->begin();; ++I) { |
| 651 | assert(I != ParentLoop->end() && "Couldn't find loop"); |
Andrew Trick | fb62b8d | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 652 | if (*I == Unloop) { |
| 653 | ParentLoop->removeChildLoop(I); |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 659 | void LoopInfo::verifyAnalysis() const { |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 660 | // LoopInfo is a FunctionPass, but verifying every loop in the function |
| 661 | // each time verifyAnalysis is called is very expensive. The |
| 662 | // -verify-loop-info option can enable this. In order to perform some |
| 663 | // checking by default, LoopPass has been taught to call verifyLoop |
| 664 | // manually during loop pass sequences. |
| 665 | |
| 666 | if (!VerifyLoopInfo) return; |
| 667 | |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame^] | 668 | DenseSet<const Loop*> Loops; |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 669 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 670 | assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame^] | 671 | (*I)->verifyLoopNest(&Loops); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 672 | } |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 673 | |
Andrew Trick | 5434c1e | 2011-08-26 03:06:34 +0000 | [diff] [blame^] | 674 | // Verify that blocks are mapped to valid loops. |
| 675 | // |
| 676 | // FIXME: With an up-to-date DFS (see LoopIterator.h) and DominatorTree, we |
| 677 | // could also verify that the blocks are still in the correct loops. |
| 678 | for (DenseMap<BasicBlock*, Loop*>::const_iterator I = LI.BBMap.begin(), |
| 679 | E = LI.BBMap.end(); I != E; ++I) { |
| 680 | assert(Loops.count(I->second) && "orphaned loop"); |
| 681 | assert(I->second->contains(I->first) && "orphaned block"); |
| 682 | } |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 685 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 686 | AU.setPreservesAll(); |
Devang Patel | 53c279b | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 687 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 690 | void LoopInfo::print(raw_ostream &OS, const Module*) const { |
| 691 | LI.print(OS); |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Andrew Trick | 2d31ae3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 694 | //===----------------------------------------------------------------------===// |
| 695 | // LoopBlocksDFS implementation |
| 696 | // |
| 697 | |
| 698 | /// Traverse the loop blocks and store the DFS result. |
| 699 | /// Useful for clients that just want the final DFS result and don't need to |
| 700 | /// visit blocks during the initial traversal. |
| 701 | void LoopBlocksDFS::perform(LoopInfo *LI) { |
| 702 | LoopBlocksTraversal Traversal(*this, LI); |
| 703 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 704 | POE = Traversal.end(); POI != POE; ++POI) ; |
| 705 | } |