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" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Streams.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 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 29 | char LoopInfo::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 30 | static RegisterPass<LoopInfo> |
Dan Gohman | 7e54404 | 2009-05-01 21:58:05 +0000 | [diff] [blame] | 31 | X("loops", "Natural Loop Information", true, true); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 34 | // Loop implementation |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 35 | // |
Misha Brukman | 6b290a5 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 36 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 37 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 38 | /// |
| 39 | bool Loop::isLoopInvariant(Value *V) const { |
| 40 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 41 | return isLoopInvariant(I); |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 42 | return true; // All non-instructions are loop invariant |
| 43 | } |
| 44 | |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 45 | /// isLoopInvariant - Return true if the specified instruction is |
| 46 | /// loop-invariant. |
| 47 | /// |
| 48 | bool Loop::isLoopInvariant(Instruction *I) const { |
| 49 | return !contains(I->getParent()); |
| 50 | } |
| 51 | |
| 52 | /// makeLoopInvariant - If the given value is an instruciton inside of the |
| 53 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 54 | /// Return true if the value after any hoisting is loop invariant. This |
| 55 | /// function can be used as a slightly more aggressive replacement for |
| 56 | /// isLoopInvariant. |
| 57 | /// |
| 58 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 59 | /// If null, the terminator of the loop preheader is used. |
| 60 | /// |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 61 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, |
| 62 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 63 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 64 | return makeLoopInvariant(I, Changed, InsertPt); |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 65 | return true; // All non-instructions are loop-invariant. |
| 66 | } |
| 67 | |
| 68 | /// makeLoopInvariant - If the given instruction is inside of the |
| 69 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 70 | /// Return true if the instruction 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(Instruction *I, bool &Changed, |
| 78 | Instruction *InsertPt) const { |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 79 | // Test if the value is already loop-invariant. |
| 80 | if (isLoopInvariant(I)) |
| 81 | return true; |
| 82 | // Don't hoist instructions with side-effects. |
| 83 | if (I->isTrapping()) |
| 84 | return false; |
| 85 | // Don't hoist PHI nodes. |
| 86 | if (isa<PHINode>(I)) |
| 87 | return false; |
| 88 | // Don't hoist allocation instructions. |
| 89 | if (isa<AllocationInst>(I)) |
| 90 | return false; |
| 91 | // Determine the insertion point, unless one was given. |
| 92 | if (!InsertPt) { |
| 93 | BasicBlock *Preheader = getLoopPreheader(); |
| 94 | // Without a preheader, hoisting is not feasible. |
| 95 | if (!Preheader) |
| 96 | return false; |
| 97 | InsertPt = Preheader->getTerminator(); |
| 98 | } |
| 99 | // Don't hoist instructions with loop-variant operands. |
| 100 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 101 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 102 | return false; |
| 103 | // Hoist. |
| 104 | I->moveBefore(InsertPt); |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 105 | Changed = true; |
Dan Gohman | a342026 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Dan Gohman | 16a2c92 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 109 | /// getCanonicalInductionVariable - Check to see if the loop has a canonical |
| 110 | /// induction variable: an integer recurrence that starts at 0 and increments |
| 111 | /// by one each time through the loop. If so, return the phi node that |
| 112 | /// corresponds to it. |
| 113 | /// |
| 114 | /// The IndVarSimplify pass transforms loops to have a canonical induction |
| 115 | /// variable. |
| 116 | /// |
| 117 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 118 | BasicBlock *H = getHeader(); |
| 119 | |
| 120 | BasicBlock *Incoming = 0, *Backedge = 0; |
| 121 | typedef GraphTraits<Inverse<BasicBlock*> > InvBlockTraits; |
| 122 | InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(H); |
| 123 | assert(PI != InvBlockTraits::child_end(H) && |
| 124 | "Loop must have at least one backedge!"); |
| 125 | Backedge = *PI++; |
| 126 | if (PI == InvBlockTraits::child_end(H)) return 0; // dead loop |
| 127 | Incoming = *PI++; |
| 128 | if (PI != InvBlockTraits::child_end(H)) return 0; // multiple backedges? |
| 129 | |
| 130 | if (contains(Incoming)) { |
| 131 | if (contains(Backedge)) |
| 132 | return 0; |
| 133 | std::swap(Incoming, Backedge); |
| 134 | } else if (!contains(Backedge)) |
| 135 | return 0; |
| 136 | |
| 137 | // Loop over all of the PHI nodes, looking for a canonical indvar. |
| 138 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
| 139 | PHINode *PN = cast<PHINode>(I); |
| 140 | if (ConstantInt *CI = |
| 141 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) |
| 142 | if (CI->isNullValue()) |
| 143 | if (Instruction *Inc = |
| 144 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) |
| 145 | if (Inc->getOpcode() == Instruction::Add && |
| 146 | Inc->getOperand(0) == PN) |
| 147 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
| 148 | if (CI->equalsInt(1)) |
| 149 | return PN; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds |
| 155 | /// the canonical induction variable value for the "next" iteration of the |
| 156 | /// loop. This always succeeds if getCanonicalInductionVariable succeeds. |
| 157 | /// |
| 158 | Instruction *Loop::getCanonicalInductionVariableIncrement() const { |
| 159 | if (PHINode *PN = getCanonicalInductionVariable()) { |
| 160 | bool P1InLoop = contains(PN->getIncomingBlock(1)); |
| 161 | return cast<Instruction>(PN->getIncomingValue(P1InLoop)); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /// getTripCount - Return a loop-invariant LLVM value indicating the number of |
| 167 | /// times the loop will be executed. Note that this means that the backedge |
| 168 | /// of the loop executes N-1 times. If the trip-count cannot be determined, |
| 169 | /// this returns null. |
| 170 | /// |
| 171 | /// The IndVarSimplify pass transforms loops to have a form that this |
| 172 | /// function easily understands. |
| 173 | /// |
| 174 | Value *Loop::getTripCount() const { |
| 175 | // Canonical loops will end with a 'cmp ne I, V', where I is the incremented |
| 176 | // canonical induction variable and V is the trip count of the loop. |
| 177 | Instruction *Inc = getCanonicalInductionVariableIncrement(); |
| 178 | if (Inc == 0) return 0; |
| 179 | PHINode *IV = cast<PHINode>(Inc->getOperand(0)); |
| 180 | |
| 181 | BasicBlock *BackedgeBlock = |
| 182 | IV->getIncomingBlock(contains(IV->getIncomingBlock(1))); |
| 183 | |
| 184 | if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator())) |
| 185 | if (BI->isConditional()) { |
| 186 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { |
| 187 | if (ICI->getOperand(0) == Inc) { |
| 188 | if (BI->getSuccessor(0) == getHeader()) { |
| 189 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) |
| 190 | return ICI->getOperand(1); |
| 191 | } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) { |
| 192 | return ICI->getOperand(1); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /// getSmallConstantTripCount - Returns the trip count of this loop as a |
| 202 | /// normal unsigned value, if possible. Returns 0 if the trip count is unknown |
| 203 | /// of not constant. Will also return 0 if the trip count is very large |
| 204 | /// (>= 2^32) |
| 205 | unsigned Loop::getSmallConstantTripCount() const { |
| 206 | Value* TripCount = this->getTripCount(); |
| 207 | if (TripCount) { |
| 208 | if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) { |
| 209 | // Guard against huge trip counts. |
| 210 | if (TripCountC->getValue().getActiveBits() <= 32) { |
| 211 | return (unsigned)TripCountC->getZExtValue(); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /// getSmallConstantTripMultiple - Returns the largest constant divisor of the |
| 219 | /// trip count of this loop as a normal unsigned value, if possible. This |
| 220 | /// means that the actual trip count is always a multiple of the returned |
| 221 | /// value (don't forget the trip count could very well be zero as well!). |
| 222 | /// |
| 223 | /// Returns 1 if the trip count is unknown or not guaranteed to be the |
| 224 | /// multiple of a constant (which is also the case if the trip count is simply |
| 225 | /// constant, use getSmallConstantTripCount for that case), Will also return 1 |
| 226 | /// if the trip count is very large (>= 2^32). |
| 227 | unsigned Loop::getSmallConstantTripMultiple() const { |
| 228 | Value* TripCount = this->getTripCount(); |
| 229 | // This will hold the ConstantInt result, if any |
| 230 | ConstantInt *Result = NULL; |
| 231 | if (TripCount) { |
| 232 | // See if the trip count is constant itself |
| 233 | Result = dyn_cast<ConstantInt>(TripCount); |
| 234 | // if not, see if it is a multiplication |
| 235 | if (!Result) |
| 236 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) { |
| 237 | switch (BO->getOpcode()) { |
| 238 | case BinaryOperator::Mul: |
| 239 | Result = dyn_cast<ConstantInt>(BO->getOperand(1)); |
| 240 | break; |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | // Guard against huge trip counts. |
| 247 | if (Result && Result->getValue().getActiveBits() <= 32) { |
| 248 | return (unsigned)Result->getZExtValue(); |
| 249 | } else { |
| 250 | return 1; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /// isLCSSAForm - Return true if the Loop is in LCSSA form |
| 255 | bool Loop::isLCSSAForm() const { |
| 256 | // Sort the blocks vector so that we can use binary search to do quick |
| 257 | // lookups. |
| 258 | SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end()); |
| 259 | |
| 260 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
| 261 | BasicBlock *BB = *BI; |
| 262 | for (BasicBlock ::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
| 263 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 264 | ++UI) { |
| 265 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
| 266 | if (PHINode *P = dyn_cast<PHINode>(*UI)) { |
| 267 | UserBB = P->getIncomingBlock(UI); |
| 268 | } |
| 269 | |
| 270 | // Check the current block, as a fast-path. Most values are used in |
| 271 | // the same block they are defined in. |
| 272 | if (UserBB != BB && !LoopBBs.count(UserBB)) |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
Dan Gohman | 9377386 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 279 | |
| 280 | /// isLoopSimplifyForm - Return true if the Loop is in the form that |
| 281 | /// the LoopSimplify form transforms loops to, which is sometimes called |
| 282 | /// normal form. |
| 283 | bool Loop::isLoopSimplifyForm() const { |
| 284 | // Normal-form loops have a preheader. |
| 285 | if (!getLoopPreheader()) |
| 286 | return false; |
| 287 | // Normal-form loops have a single backedge. |
| 288 | if (!getLoopLatch()) |
| 289 | return false; |
| 290 | // Each predecessor of each exit block of a normal loop is contained |
| 291 | // within the loop. |
| 292 | SmallVector<BasicBlock *, 4> ExitBlocks; |
| 293 | getExitBlocks(ExitBlocks); |
| 294 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 295 | for (pred_iterator PI = pred_begin(ExitBlocks[i]), |
| 296 | PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) |
| 297 | if (!contains(*PI)) |
| 298 | return false; |
| 299 | // All the requirements are met. |
| 300 | return true; |
| 301 | } |
| 302 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 303 | //===----------------------------------------------------------------------===// |
| 304 | // LoopInfo implementation |
| 305 | // |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 306 | bool LoopInfo::runOnFunction(Function &) { |
| 307 | releaseMemory(); |
Dan Gohman | 9d59d9f | 2009-06-27 21:22:48 +0000 | [diff] [blame] | 308 | LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 312 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 313 | AU.setPreservesAll(); |
Devang Patel | 53c279b | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 314 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 315 | } |