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" |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 21 | #include "llvm/Assembly/Writer.h" |
Misha Brukman | 10d208d | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DepthFirstIterator.h" |
Chris Lattner | b1f5d8b | 2007-03-04 04:06:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 46758a8 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 29 | // Always verify loopinfo if expensive checking is enabled. |
| 30 | #ifdef XDEBUG |
| 31 | bool VerifyLoopInfo = true; |
| 32 | #else |
| 33 | bool VerifyLoopInfo = false; |
| 34 | #endif |
| 35 | static cl::opt<bool,true> |
| 36 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
| 37 | cl::desc("Verify loop info (time consuming)")); |
| 38 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 39 | char LoopInfo::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 40 | static RegisterPass<LoopInfo> |
Dan Gohman | 7e54404 | 2009-05-01 21:58:05 +0000 | [diff] [blame] | 41 | X("loops", "Natural Loop Information", true, true); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 42 | |
| 43 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 44 | // Loop implementation |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 45 | // |
Misha Brukman | 6b290a5 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 46 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 47 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 48 | /// |
| 49 | bool Loop::isLoopInvariant(Value *V) const { |
| 50 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 51 | return isLoopInvariant(I); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 52 | return true; // All non-instructions are loop invariant |
| 53 | } |
| 54 | |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 55 | /// isLoopInvariant - Return true if the specified instruction is |
| 56 | /// loop-invariant. |
| 57 | /// |
| 58 | bool Loop::isLoopInvariant(Instruction *I) const { |
| 59 | return !contains(I->getParent()); |
| 60 | } |
| 61 | |
| 62 | /// makeLoopInvariant - If the given value is an instruciton inside of the |
| 63 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 64 | /// Return true if the value after any hoisting is loop invariant. This |
| 65 | /// function can be used as a slightly more aggressive replacement for |
| 66 | /// isLoopInvariant. |
| 67 | /// |
| 68 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 69 | /// If null, the terminator of the loop preheader is used. |
| 70 | /// |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 71 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, |
| 72 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 73 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 74 | return makeLoopInvariant(I, Changed, InsertPt); |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 75 | return true; // All non-instructions are loop-invariant. |
| 76 | } |
| 77 | |
| 78 | /// makeLoopInvariant - If the given instruction is inside of the |
| 79 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 80 | /// Return true if the instruction after any hoisting is loop invariant. This |
| 81 | /// function can be used as a slightly more aggressive replacement for |
| 82 | /// isLoopInvariant. |
| 83 | /// |
| 84 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 85 | /// If null, the terminator of the loop preheader is used. |
| 86 | /// |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 87 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, |
| 88 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 89 | // Test if the value is already loop-invariant. |
| 90 | if (isLoopInvariant(I)) |
| 91 | return true; |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 92 | if (!I->isSafeToSpeculativelyExecute()) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 93 | return false; |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 94 | if (I->mayReadFromMemory()) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 95 | return false; |
| 96 | // Determine the insertion point, unless one was given. |
| 97 | if (!InsertPt) { |
| 98 | BasicBlock *Preheader = getLoopPreheader(); |
| 99 | // Without a preheader, hoisting is not feasible. |
| 100 | if (!Preheader) |
| 101 | return false; |
| 102 | InsertPt = Preheader->getTerminator(); |
| 103 | } |
| 104 | // Don't hoist instructions with loop-variant operands. |
| 105 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 106 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 107 | return false; |
| 108 | // Hoist. |
| 109 | I->moveBefore(InsertPt); |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 110 | Changed = true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 114 | /// getCanonicalInductionVariable - Check to see if the loop has a canonical |
| 115 | /// induction variable: an integer recurrence that starts at 0 and increments |
| 116 | /// by one each time through the loop. If so, return the phi node that |
| 117 | /// corresponds to it. |
| 118 | /// |
| 119 | /// The IndVarSimplify pass transforms loops to have a canonical induction |
| 120 | /// variable. |
| 121 | /// |
| 122 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 123 | BasicBlock *H = getHeader(); |
| 124 | |
| 125 | BasicBlock *Incoming = 0, *Backedge = 0; |
| 126 | typedef GraphTraits<Inverse<BasicBlock*> > InvBlockTraits; |
| 127 | InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(H); |
| 128 | assert(PI != InvBlockTraits::child_end(H) && |
| 129 | "Loop must have at least one backedge!"); |
| 130 | Backedge = *PI++; |
| 131 | if (PI == InvBlockTraits::child_end(H)) return 0; // dead loop |
| 132 | Incoming = *PI++; |
| 133 | if (PI != InvBlockTraits::child_end(H)) return 0; // multiple backedges? |
| 134 | |
| 135 | if (contains(Incoming)) { |
| 136 | if (contains(Backedge)) |
| 137 | return 0; |
| 138 | std::swap(Incoming, Backedge); |
| 139 | } else if (!contains(Backedge)) |
| 140 | return 0; |
| 141 | |
| 142 | // Loop over all of the PHI nodes, looking for a canonical indvar. |
| 143 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
| 144 | PHINode *PN = cast<PHINode>(I); |
| 145 | if (ConstantInt *CI = |
| 146 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) |
| 147 | if (CI->isNullValue()) |
| 148 | if (Instruction *Inc = |
| 149 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) |
| 150 | if (Inc->getOpcode() == Instruction::Add && |
| 151 | Inc->getOperand(0) == PN) |
| 152 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
| 153 | if (CI->equalsInt(1)) |
| 154 | return PN; |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds |
| 160 | /// the canonical induction variable value for the "next" iteration of the |
| 161 | /// loop. This always succeeds if getCanonicalInductionVariable succeeds. |
| 162 | /// |
| 163 | Instruction *Loop::getCanonicalInductionVariableIncrement() const { |
| 164 | if (PHINode *PN = getCanonicalInductionVariable()) { |
| 165 | bool P1InLoop = contains(PN->getIncomingBlock(1)); |
| 166 | return cast<Instruction>(PN->getIncomingValue(P1InLoop)); |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /// getTripCount - Return a loop-invariant LLVM value indicating the number of |
| 172 | /// times the loop will be executed. Note that this means that the backedge |
| 173 | /// of the loop executes N-1 times. If the trip-count cannot be determined, |
| 174 | /// this returns null. |
| 175 | /// |
| 176 | /// The IndVarSimplify pass transforms loops to have a form that this |
| 177 | /// function easily understands. |
| 178 | /// |
| 179 | Value *Loop::getTripCount() const { |
| 180 | // Canonical loops will end with a 'cmp ne I, V', where I is the incremented |
| 181 | // canonical induction variable and V is the trip count of the loop. |
| 182 | Instruction *Inc = getCanonicalInductionVariableIncrement(); |
| 183 | if (Inc == 0) return 0; |
| 184 | PHINode *IV = cast<PHINode>(Inc->getOperand(0)); |
| 185 | |
| 186 | BasicBlock *BackedgeBlock = |
| 187 | IV->getIncomingBlock(contains(IV->getIncomingBlock(1))); |
| 188 | |
| 189 | if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) |
| 190 | if (BI->isConditional()) { |
| 191 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { |
| 192 | if (ICI->getOperand(0) == Inc) { |
| 193 | if (BI->getSuccessor(0) == getHeader()) { |
| 194 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 195 | return ICI->getOperand(1); |
| 196 | } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { |
| 197 | return ICI->getOperand(1); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /// getSmallConstantTripCount - Returns the trip count of this loop as a |
| 207 | /// normal unsigned value, if possible. Returns 0 if the trip count is unknown |
| 208 | /// of not constant. Will also return 0 if the trip count is very large |
| 209 | /// (>= 2^32) |
| 210 | unsigned Loop::getSmallConstantTripCount() const { |
| 211 | Value* TripCount = this->getTripCount(); |
| 212 | if (TripCount) { |
| 213 | if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) { |
| 214 | // Guard against huge trip counts. |
| 215 | if (TripCountC->getValue().getActiveBits() <= 32) { |
| 216 | return (unsigned)TripCountC->getZExtValue(); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /// getSmallConstantTripMultiple - Returns the largest constant divisor of the |
| 224 | /// trip count of this loop as a normal unsigned value, if possible. This |
| 225 | /// means that the actual trip count is always a multiple of the returned |
| 226 | /// value (don't forget the trip count could very well be zero as well!). |
| 227 | /// |
| 228 | /// Returns 1 if the trip count is unknown or not guaranteed to be the |
| 229 | /// multiple of a constant (which is also the case if the trip count is simply |
| 230 | /// constant, use getSmallConstantTripCount for that case), Will also return 1 |
| 231 | /// if the trip count is very large (>= 2^32). |
| 232 | unsigned Loop::getSmallConstantTripMultiple() const { |
| 233 | Value* TripCount = this->getTripCount(); |
| 234 | // This will hold the ConstantInt result, if any |
| 235 | ConstantInt *Result = NULL; |
| 236 | if (TripCount) { |
| 237 | // See if the trip count is constant itself |
| 238 | Result = dyn_cast<ConstantInt>(TripCount); |
| 239 | // if not, see if it is a multiplication |
| 240 | if (!Result) |
| 241 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) { |
| 242 | switch (BO->getOpcode()) { |
| 243 | case BinaryOperator::Mul: |
| 244 | Result = dyn_cast<ConstantInt>(BO->getOperand(1)); |
| 245 | break; |
| 246 | default: |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | // Guard against huge trip counts. |
| 252 | if (Result && Result->getValue().getActiveBits() <= 32) { |
| 253 | return (unsigned)Result->getZExtValue(); |
| 254 | } else { |
| 255 | return 1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /// isLCSSAForm - Return true if the Loop is in LCSSA form |
| 260 | bool Loop::isLCSSAForm() const { |
| 261 | // Sort the blocks vector so that we can use binary search to do quick |
| 262 | // lookups. |
| 263 | SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); |
| 264 | |
| 265 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
| 266 | BasicBlock *BB = *BI; |
| 267 | for (BasicBlock ::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
| 268 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 269 | ++UI) { |
| 270 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
| 271 | if (PHINode *P = dyn_cast<PHINode>(*UI)) { |
| 272 | UserBB = P->getIncomingBlock(UI); |
| 273 | } |
| 274 | |
| 275 | // Check the current block, as a fast-path. Most values are used in |
| 276 | // the same block they are defined in. |
| 277 | if (UserBB != BB && !LoopBBs.count(UserBB)) |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 284 | |
| 285 | /// isLoopSimplifyForm - Return true if the Loop is in the form that |
| 286 | /// the LoopSimplify form transforms loops to, which is sometimes called |
| 287 | /// normal form. |
| 288 | bool Loop::isLoopSimplifyForm() const { |
Dan Gohman | f17e951 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 289 | // Normal-form loops have a preheader, a single backedge, and all of their |
| 290 | // exits have all their predecessors inside the loop. |
| 291 | return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); |
| 292 | } |
| 293 | |
| 294 | /// hasDedicatedExits - Return true if no exit block for the loop |
| 295 | /// has a predecessor that is outside the loop. |
| 296 | bool Loop::hasDedicatedExits() const { |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 297 | // Sort the blocks vector so that we can use binary search to do quick |
| 298 | // lookups. |
| 299 | SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 300 | // Each predecessor of each exit block of a normal loop is contained |
| 301 | // within the loop. |
| 302 | SmallVector<BasicBlock *, 4> ExitBlocks; |
| 303 | getExitBlocks(ExitBlocks); |
| 304 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 305 | for (pred_iterator PI = pred_begin(ExitBlocks[i]), |
| 306 | PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) |
Dan Gohman | eed9e5b | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 307 | if (!LoopBBs.count(*PI)) |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 308 | return false; |
| 309 | // All the requirements are met. |
| 310 | return true; |
| 311 | } |
| 312 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 313 | /// getUniqueExitBlocks - Return all unique successor blocks of this loop. |
| 314 | /// These are the blocks _outside of the current loop_ which are branched to. |
| 315 | /// This assumes that loop is in canonical form. |
| 316 | /// |
| 317 | void |
| 318 | Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 319 | assert(isLoopSimplifyForm() && |
| 320 | "getUniqueExitBlocks assumes the loop is in canonical form!"); |
| 321 | |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 322 | // Sort the blocks vector so that we can use binary search to do quick |
| 323 | // lookups. |
| 324 | SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end()); |
| 325 | std::sort(LoopBBs.begin(), LoopBBs.end()); |
| 326 | |
Dan Gohman | 058db92 | 2009-09-03 20:36:13 +0000 | [diff] [blame] | 327 | SmallVector<BasicBlock *, 32> switchExitBlocks; |
Dan Gohman | f0608d8 | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 328 | |
| 329 | for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { |
| 330 | |
| 331 | BasicBlock *current = *BI; |
| 332 | switchExitBlocks.clear(); |
| 333 | |
| 334 | typedef GraphTraits<BasicBlock *> BlockTraits; |
| 335 | typedef GraphTraits<Inverse<BasicBlock *> > InvBlockTraits; |
| 336 | for (BlockTraits::ChildIteratorType I = |
| 337 | BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI); |
| 338 | I != E; ++I) { |
| 339 | // If block is inside the loop then it is not a exit block. |
| 340 | if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) |
| 341 | continue; |
| 342 | |
| 343 | InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(*I); |
| 344 | BasicBlock *firstPred = *PI; |
| 345 | |
| 346 | // If current basic block is this exit block's first predecessor |
| 347 | // then only insert exit block in to the output ExitBlocks vector. |
| 348 | // This ensures that same exit block is not inserted twice into |
| 349 | // ExitBlocks vector. |
| 350 | if (current != firstPred) |
| 351 | continue; |
| 352 | |
| 353 | // If a terminator has more then two successors, for example SwitchInst, |
| 354 | // then it is possible that there are multiple edges from current block |
| 355 | // to one exit block. |
| 356 | if (std::distance(BlockTraits::child_begin(current), |
| 357 | BlockTraits::child_end(current)) <= 2) { |
| 358 | ExitBlocks.push_back(*I); |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | // In case of multiple edges from current block to exit block, collect |
| 363 | // only one edge in ExitBlocks. Use switchExitBlocks to keep track of |
| 364 | // duplicate edges. |
| 365 | if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) |
| 366 | == switchExitBlocks.end()) { |
| 367 | switchExitBlocks.push_back(*I); |
| 368 | ExitBlocks.push_back(*I); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one |
| 375 | /// block, return that block. Otherwise return null. |
| 376 | BasicBlock *Loop::getUniqueExitBlock() const { |
| 377 | SmallVector<BasicBlock *, 8> UniqueExitBlocks; |
| 378 | getUniqueExitBlocks(UniqueExitBlocks); |
| 379 | if (UniqueExitBlocks.size() == 1) |
| 380 | return UniqueExitBlocks[0]; |
| 381 | return 0; |
| 382 | } |
| 383 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 384 | //===----------------------------------------------------------------------===// |
| 385 | // LoopInfo implementation |
| 386 | // |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 387 | bool LoopInfo::runOnFunction(Function &) { |
| 388 | releaseMemory(); |
Dan Gohman | 9d59d9f | 2009-06-27 21:22:48 +0000 | [diff] [blame] | 389 | LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 393 | void LoopInfo::verifyAnalysis() const { |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 394 | // LoopInfo is a FunctionPass, but verifying every loop in the function |
| 395 | // each time verifyAnalysis is called is very expensive. The |
| 396 | // -verify-loop-info option can enable this. In order to perform some |
| 397 | // checking by default, LoopPass has been taught to call verifyLoop |
| 398 | // manually during loop pass sequences. |
| 399 | |
| 400 | if (!VerifyLoopInfo) return; |
| 401 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 402 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 403 | assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); |
| 404 | (*I)->verifyLoopNest(); |
| 405 | } |
Dan Gohman | 9450b0e | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 406 | |
| 407 | // TODO: check BBMap consistency. |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 410 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 411 | AU.setPreservesAll(); |
Devang Patel | 53c279b | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 412 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 415 | void LoopInfo::print(raw_ostream &OS, const Module*) const { |
| 416 | LI.print(OS); |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 417 | } |
| 418 | |