| Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===// | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 6de9942 | 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 | 81804b4 | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" | 
| Chris Lattner | d9dc425 | 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 | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 21 | #include "llvm/Assembly/Writer.h" | 
| Misha Brukman | 81804b4 | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" | 
| Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" | 
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DepthFirstIterator.h" | 
| Chris Lattner | 16cf9a7 | 2007-03-04 04:06:39 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" | 
| Chris Lattner | 6de9942 | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 27 | #include <algorithm> | 
| Chris Lattner | 55b7ef5 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 28 | using namespace llvm; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 |  | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 30 | // Always verify loopinfo if expensive checking is enabled. | 
|  | 31 | #ifdef XDEBUG | 
| Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 32 | static bool VerifyLoopInfo = true; | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 33 | #else | 
| Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 34 | static bool VerifyLoopInfo = false; | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 35 | #endif | 
|  | 36 | static cl::opt<bool,true> | 
|  | 37 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), | 
|  | 38 | cl::desc("Verify loop info (time consuming)")); | 
|  | 39 |  | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 40 | char LoopInfo::ID = 0; | 
| Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 41 | INITIALIZE_PASS(LoopInfo, "loops", "Natural Loop Information", true, true); | 
| Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 44 | // Loop implementation | 
| Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 45 | // | 
| Misha Brukman | 3845be2 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 46 |  | 
| Dan Gohman | 80a9942 | 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)) | 
| Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 51 | return !contains(I); | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 52 | return true;  // All non-instructions are loop invariant | 
|  | 53 | } | 
|  | 54 |  | 
| Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 55 | /// hasLoopInvariantOperands - Return true if all the operands of the | 
|  | 56 | /// specified instruction are loop invariant. | 
|  | 57 | bool Loop::hasLoopInvariantOperands(Instruction *I) const { | 
|  | 58 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) | 
|  | 59 | if (!isLoopInvariant(I->getOperand(i))) | 
|  | 60 | return false; | 
|  | 61 |  | 
|  | 62 | return true; | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | /// makeLoopInvariant - If the given value is an instruciton inside of the | 
|  | 66 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. | 
|  | 67 | /// Return true if the value after any hoisting is loop invariant. This | 
|  | 68 | /// function can be used as a slightly more aggressive replacement for | 
|  | 69 | /// isLoopInvariant. | 
|  | 70 | /// | 
|  | 71 | /// If InsertPt is specified, it is the point to hoist instructions to. | 
|  | 72 | /// If null, the terminator of the loop preheader is used. | 
|  | 73 | /// | 
| Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 74 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, | 
|  | 75 | Instruction *InsertPt) const { | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 76 | if (Instruction *I = dyn_cast<Instruction>(V)) | 
| Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 77 | return makeLoopInvariant(I, Changed, InsertPt); | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 78 | return true;  // All non-instructions are loop-invariant. | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | /// makeLoopInvariant - If the given instruction is inside of the | 
|  | 82 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. | 
|  | 83 | /// Return true if the instruction after any hoisting is loop invariant. This | 
|  | 84 | /// function can be used as a slightly more aggressive replacement for | 
|  | 85 | /// isLoopInvariant. | 
|  | 86 | /// | 
|  | 87 | /// If InsertPt is specified, it is the point to hoist instructions to. | 
|  | 88 | /// If null, the terminator of the loop preheader is used. | 
|  | 89 | /// | 
| Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 90 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, | 
|  | 91 | Instruction *InsertPt) const { | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 92 | // Test if the value is already loop-invariant. | 
|  | 93 | if (isLoopInvariant(I)) | 
|  | 94 | return true; | 
| Eli Friedman | b8f6a4f | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 95 | if (!I->isSafeToSpeculativelyExecute()) | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 96 | return false; | 
| Eli Friedman | b8f6a4f | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 97 | if (I->mayReadFromMemory()) | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 98 | return false; | 
|  | 99 | // Determine the insertion point, unless one was given. | 
|  | 100 | if (!InsertPt) { | 
|  | 101 | BasicBlock *Preheader = getLoopPreheader(); | 
|  | 102 | // Without a preheader, hoisting is not feasible. | 
|  | 103 | if (!Preheader) | 
|  | 104 | return false; | 
|  | 105 | InsertPt = Preheader->getTerminator(); | 
|  | 106 | } | 
|  | 107 | // Don't hoist instructions with loop-variant operands. | 
|  | 108 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) | 
| Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 109 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 110 | return false; | 
| Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 111 |  | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 112 | // Hoist. | 
|  | 113 | I->moveBefore(InsertPt); | 
| Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 114 | Changed = true; | 
| Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 115 | return true; | 
|  | 116 | } | 
|  | 117 |  | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 118 | /// getCanonicalInductionVariable - Check to see if the loop has a canonical | 
|  | 119 | /// induction variable: an integer recurrence that starts at 0 and increments | 
|  | 120 | /// by one each time through the loop.  If so, return the phi node that | 
|  | 121 | /// corresponds to it. | 
|  | 122 | /// | 
|  | 123 | /// The IndVarSimplify pass transforms loops to have a canonical induction | 
|  | 124 | /// variable. | 
|  | 125 | /// | 
|  | 126 | PHINode *Loop::getCanonicalInductionVariable() const { | 
|  | 127 | BasicBlock *H = getHeader(); | 
|  | 128 |  | 
|  | 129 | BasicBlock *Incoming = 0, *Backedge = 0; | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 130 | pred_iterator PI = pred_begin(H); | 
|  | 131 | assert(PI != pred_end(H) && | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 132 | "Loop must have at least one backedge!"); | 
|  | 133 | Backedge = *PI++; | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 134 | if (PI == pred_end(H)) return 0;  // dead loop | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 135 | Incoming = *PI++; | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 136 | if (PI != pred_end(H)) return 0;  // multiple backedges? | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 137 |  | 
|  | 138 | if (contains(Incoming)) { | 
|  | 139 | if (contains(Backedge)) | 
|  | 140 | return 0; | 
|  | 141 | std::swap(Incoming, Backedge); | 
|  | 142 | } else if (!contains(Backedge)) | 
|  | 143 | return 0; | 
|  | 144 |  | 
|  | 145 | // Loop over all of the PHI nodes, looking for a canonical indvar. | 
|  | 146 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { | 
|  | 147 | PHINode *PN = cast<PHINode>(I); | 
|  | 148 | if (ConstantInt *CI = | 
|  | 149 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) | 
|  | 150 | if (CI->isNullValue()) | 
|  | 151 | if (Instruction *Inc = | 
|  | 152 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) | 
|  | 153 | if (Inc->getOpcode() == Instruction::Add && | 
|  | 154 | Inc->getOperand(0) == PN) | 
|  | 155 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) | 
|  | 156 | if (CI->equalsInt(1)) | 
|  | 157 | return PN; | 
|  | 158 | } | 
|  | 159 | return 0; | 
|  | 160 | } | 
|  | 161 |  | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 162 | /// getTripCount - Return a loop-invariant LLVM value indicating the number of | 
|  | 163 | /// times the loop will be executed.  Note that this means that the backedge | 
|  | 164 | /// of the loop executes N-1 times.  If the trip-count cannot be determined, | 
|  | 165 | /// this returns null. | 
|  | 166 | /// | 
|  | 167 | /// The IndVarSimplify pass transforms loops to have a form that this | 
|  | 168 | /// function easily understands. | 
|  | 169 | /// | 
|  | 170 | Value *Loop::getTripCount() const { | 
|  | 171 | // Canonical loops will end with a 'cmp ne I, V', where I is the incremented | 
|  | 172 | // canonical induction variable and V is the trip count of the loop. | 
| Dan Gohman | 7038bd5 | 2010-07-23 21:34:51 +0000 | [diff] [blame] | 173 | PHINode *IV = getCanonicalInductionVariable(); | 
|  | 174 | if (IV == 0 || IV->getNumIncomingValues() != 2) return 0; | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 175 |  | 
| Dan Gohman | 7038bd5 | 2010-07-23 21:34:51 +0000 | [diff] [blame] | 176 | bool P0InLoop = contains(IV->getIncomingBlock(0)); | 
|  | 177 | Value *Inc = IV->getIncomingValue(!P0InLoop); | 
|  | 178 | BasicBlock *BackedgeBlock = IV->getIncomingBlock(!P0InLoop); | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 179 |  | 
|  | 180 | if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) | 
|  | 181 | if (BI->isConditional()) { | 
|  | 182 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { | 
|  | 183 | if (ICI->getOperand(0) == Inc) { | 
|  | 184 | if (BI->getSuccessor(0) == getHeader()) { | 
|  | 185 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | 
|  | 186 | return ICI->getOperand(1); | 
|  | 187 | } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { | 
|  | 188 | return ICI->getOperand(1); | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | return 0; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | /// getSmallConstantTripCount - Returns the trip count of this loop as a | 
|  | 198 | /// normal unsigned value, if possible. Returns 0 if the trip count is unknown | 
|  | 199 | /// of not constant. Will also return 0 if the trip count is very large | 
|  | 200 | /// (>= 2^32) | 
|  | 201 | unsigned Loop::getSmallConstantTripCount() const { | 
|  | 202 | Value* TripCount = this->getTripCount(); | 
|  | 203 | if (TripCount) { | 
|  | 204 | if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) { | 
|  | 205 | // Guard against huge trip counts. | 
|  | 206 | if (TripCountC->getValue().getActiveBits() <= 32) { | 
|  | 207 | return (unsigned)TripCountC->getZExtValue(); | 
|  | 208 | } | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 | return 0; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | /// getSmallConstantTripMultiple - Returns the largest constant divisor of the | 
|  | 215 | /// trip count of this loop as a normal unsigned value, if possible. This | 
|  | 216 | /// means that the actual trip count is always a multiple of the returned | 
|  | 217 | /// value (don't forget the trip count could very well be zero as well!). | 
|  | 218 | /// | 
|  | 219 | /// Returns 1 if the trip count is unknown or not guaranteed to be the | 
|  | 220 | /// multiple of a constant (which is also the case if the trip count is simply | 
|  | 221 | /// constant, use getSmallConstantTripCount for that case), Will also return 1 | 
|  | 222 | /// if the trip count is very large (>= 2^32). | 
|  | 223 | unsigned Loop::getSmallConstantTripMultiple() const { | 
|  | 224 | Value* TripCount = this->getTripCount(); | 
|  | 225 | // This will hold the ConstantInt result, if any | 
|  | 226 | ConstantInt *Result = NULL; | 
|  | 227 | if (TripCount) { | 
|  | 228 | // See if the trip count is constant itself | 
|  | 229 | Result = dyn_cast<ConstantInt>(TripCount); | 
|  | 230 | // if not, see if it is a multiplication | 
|  | 231 | if (!Result) | 
|  | 232 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) { | 
|  | 233 | switch (BO->getOpcode()) { | 
|  | 234 | case BinaryOperator::Mul: | 
|  | 235 | Result = dyn_cast<ConstantInt>(BO->getOperand(1)); | 
|  | 236 | break; | 
| Dan Gohman | 62167b9 | 2009-11-20 01:09:34 +0000 | [diff] [blame] | 237 | case BinaryOperator::Shl: | 
|  | 238 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) | 
|  | 239 | if (CI->getValue().getActiveBits() <= 5) | 
|  | 240 | return 1u << CI->getZExtValue(); | 
|  | 241 | break; | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 242 | default: | 
|  | 243 | break; | 
|  | 244 | } | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 | // Guard against huge trip counts. | 
|  | 248 | if (Result && Result->getValue().getActiveBits() <= 32) { | 
|  | 249 | return (unsigned)Result->getZExtValue(); | 
|  | 250 | } else { | 
|  | 251 | return 1; | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | /// isLCSSAForm - Return true if the Loop is in LCSSA form | 
| Dan Gohman | 2734ebd | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 256 | bool Loop::isLCSSAForm(DominatorTree &DT) const { | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 257 | // Sort the blocks vector so that we can use binary search to do quick | 
|  | 258 | // lookups. | 
| Gabor Greif | 2732561 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 259 | SmallPtrSet<BasicBlock*, 16> LoopBBs(block_begin(), block_end()); | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 260 |  | 
|  | 261 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { | 
| Dan Gohman | 5196e41 | 2009-11-09 18:19:43 +0000 | [diff] [blame] | 262 | BasicBlock *BB = *BI; | 
|  | 263 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 264 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; | 
|  | 265 | ++UI) { | 
| Gabor Greif | 2732561 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 266 | User *U = *UI; | 
|  | 267 | BasicBlock *UserBB = cast<Instruction>(U)->getParent(); | 
|  | 268 | if (PHINode *P = dyn_cast<PHINode>(U)) | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 269 | UserBB = P->getIncomingBlock(UI); | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 270 |  | 
| Dan Gohman | 93452ce | 2010-03-09 01:53:33 +0000 | [diff] [blame] | 271 | // Check the current block, as a fast-path, before checking whether | 
|  | 272 | // the use is anywhere in the loop.  Most values are used in the same | 
|  | 273 | // block they are defined in.  Also, blocks not reachable from the | 
|  | 274 | // entry are special; uses in them don't need to go through PHIs. | 
|  | 275 | if (UserBB != BB && | 
|  | 276 | !LoopBBs.count(UserBB) && | 
| Dan Gohman | 2734ebd | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 277 | DT.isReachableFromEntry(UserBB)) | 
| Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 278 | return false; | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | return true; | 
|  | 283 | } | 
| Dan Gohman | 1511f70 | 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 | e3a1706 | 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 | 83f5c83 | 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 | 1511f70 | 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 | 83f5c83 | 2009-10-20 20:41:13 +0000 | [diff] [blame] | 307 | if (!LoopBBs.count(*PI)) | 
| Dan Gohman | 1511f70 | 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 | 3a0ce3e | 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. | 
| Dan Gohman | 84ba039 | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 315 | /// This assumes that loop exits are in canonical form. | 
| Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 316 | /// | 
|  | 317 | void | 
|  | 318 | Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { | 
| Dan Gohman | 84ba039 | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 319 | assert(hasDedicatedExits() && | 
|  | 320 | "getUniqueExitBlocks assumes the loop has canonical form exits!"); | 
| Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 321 |  | 
| Dan Gohman | 3a0ce3e | 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 | ed8f320 | 2009-09-03 20:36:13 +0000 | [diff] [blame] | 327 | SmallVector<BasicBlock *, 32> switchExitBlocks; | 
| Dan Gohman | 3a0ce3e | 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 |  | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 334 | for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) { | 
| Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 335 | // If block is inside the loop then it is not a exit block. | 
|  | 336 | if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) | 
|  | 337 | continue; | 
|  | 338 |  | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 339 | pred_iterator PI = pred_begin(*I); | 
| Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 340 | BasicBlock *firstPred = *PI; | 
|  | 341 |  | 
|  | 342 | // If current basic block is this exit block's first predecessor | 
|  | 343 | // then only insert exit block in to the output ExitBlocks vector. | 
|  | 344 | // This ensures that same exit block is not inserted twice into | 
|  | 345 | // ExitBlocks vector. | 
|  | 346 | if (current != firstPred) | 
|  | 347 | continue; | 
|  | 348 |  | 
|  | 349 | // If a terminator has more then two successors, for example SwitchInst, | 
|  | 350 | // then it is possible that there are multiple edges from current block | 
|  | 351 | // to one exit block. | 
| Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 352 | if (std::distance(succ_begin(current), succ_end(current)) <= 2) { | 
| Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 353 | ExitBlocks.push_back(*I); | 
|  | 354 | continue; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | // In case of multiple edges from current block to exit block, collect | 
|  | 358 | // only one edge in ExitBlocks. Use switchExitBlocks to keep track of | 
|  | 359 | // duplicate edges. | 
|  | 360 | if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) | 
|  | 361 | == switchExitBlocks.end()) { | 
|  | 362 | switchExitBlocks.push_back(*I); | 
|  | 363 | ExitBlocks.push_back(*I); | 
|  | 364 | } | 
|  | 365 | } | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one | 
|  | 370 | /// block, return that block. Otherwise return null. | 
|  | 371 | BasicBlock *Loop::getUniqueExitBlock() const { | 
|  | 372 | SmallVector<BasicBlock *, 8> UniqueExitBlocks; | 
|  | 373 | getUniqueExitBlocks(UniqueExitBlocks); | 
|  | 374 | if (UniqueExitBlocks.size() == 1) | 
|  | 375 | return UniqueExitBlocks[0]; | 
|  | 376 | return 0; | 
|  | 377 | } | 
|  | 378 |  | 
| Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 379 | void Loop::dump() const { | 
|  | 380 | print(dbgs()); | 
|  | 381 | } | 
|  | 382 |  | 
| Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 383 | //===----------------------------------------------------------------------===// | 
|  | 384 | // LoopInfo implementation | 
|  | 385 | // | 
| Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 386 | bool LoopInfo::runOnFunction(Function &) { | 
|  | 387 | releaseMemory(); | 
| Dan Gohman | 4f16a29 | 2009-06-27 21:22:48 +0000 | [diff] [blame] | 388 | LI.Calculate(getAnalysis<DominatorTree>().getBase());    // Update | 
| Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 389 | return false; | 
|  | 390 | } | 
|  | 391 |  | 
| Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 392 | void LoopInfo::verifyAnalysis() const { | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 393 | // LoopInfo is a FunctionPass, but verifying every loop in the function | 
|  | 394 | // each time verifyAnalysis is called is very expensive. The | 
|  | 395 | // -verify-loop-info option can enable this. In order to perform some | 
|  | 396 | // checking by default, LoopPass has been taught to call verifyLoop | 
|  | 397 | // manually during loop pass sequences. | 
|  | 398 |  | 
|  | 399 | if (!VerifyLoopInfo) return; | 
|  | 400 |  | 
| Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 401 | for (iterator I = begin(), E = end(); I != E; ++I) { | 
|  | 402 | assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); | 
|  | 403 | (*I)->verifyLoopNest(); | 
|  | 404 | } | 
| Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 405 |  | 
|  | 406 | // TODO: check BBMap consistency. | 
| Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
| Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 409 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { | 
| Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 410 | AU.setPreservesAll(); | 
| Devang Patel | aee309e | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 411 | AU.addRequired<DominatorTree>(); | 
| Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 412 | } | 
| Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 413 |  | 
| Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 414 | void LoopInfo::print(raw_ostream &OS, const Module*) const { | 
|  | 415 | LI.print(OS); | 
| Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 416 | } | 
|  | 417 |  |