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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
Andrew Trick | cda51d4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfoImpl.h" |
Andrew Trick | 78b40c3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopIterator.h" |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame^] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Metadata.h" |
Misha Brukman | 81804b4 | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CFG.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6de9942 | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | 55b7ef5 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Andrew Trick | cda51d4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 33 | // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops. |
| 34 | template class llvm::LoopBase<BasicBlock, Loop>; |
| 35 | template class llvm::LoopInfoBase<BasicBlock, Loop>; |
| 36 | |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 37 | // Always verify loopinfo if expensive checking is enabled. |
| 38 | #ifdef XDEBUG |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 39 | static bool VerifyLoopInfo = true; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 40 | #else |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 41 | static bool VerifyLoopInfo = false; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 42 | #endif |
| 43 | static cl::opt<bool,true> |
| 44 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
| 45 | cl::desc("Verify loop info (time consuming)")); |
| 46 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 47 | char LoopInfo::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 48 | INITIALIZE_PASS_BEGIN(LoopInfo, "loops", "Natural Loop Information", true, true) |
| 49 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 50 | INITIALIZE_PASS_END(LoopInfo, "loops", "Natural Loop Information", true, true) |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 51 | |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 52 | // Loop identifier metadata name. |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 53 | static const char *const LoopMDName = "llvm.loop"; |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 54 | |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 56 | // Loop implementation |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 57 | // |
Misha Brukman | 3845be2 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 58 | |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 59 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 60 | /// |
| 61 | bool Loop::isLoopInvariant(Value *V) const { |
| 62 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 63 | return !contains(I); |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 64 | return true; // All non-instructions are loop invariant |
| 65 | } |
| 66 | |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 67 | /// hasLoopInvariantOperands - Return true if all the operands of the |
Andrew Trick | f898cbd | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 68 | /// specified instruction are loop invariant. |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 69 | bool Loop::hasLoopInvariantOperands(Instruction *I) const { |
| 70 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 71 | if (!isLoopInvariant(I->getOperand(i))) |
| 72 | return false; |
Andrew Trick | f898cbd | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 73 | |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 74 | return true; |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// makeLoopInvariant - If the given value is an instruciton inside of the |
| 78 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 79 | /// Return true if the value after any hoisting is loop invariant. This |
| 80 | /// function can be used as a slightly more aggressive replacement for |
| 81 | /// isLoopInvariant. |
| 82 | /// |
| 83 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 84 | /// If null, the terminator of the loop preheader is used. |
| 85 | /// |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 86 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, |
| 87 | Instruction *InsertPt) const { |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 88 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 89 | return makeLoopInvariant(I, Changed, InsertPt); |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 90 | return true; // All non-instructions are loop-invariant. |
| 91 | } |
| 92 | |
| 93 | /// makeLoopInvariant - If the given instruction is inside of the |
| 94 | /// loop and it can be hoisted, do so to make it trivially loop-invariant. |
| 95 | /// Return true if the instruction after any hoisting is loop invariant. This |
| 96 | /// function can be used as a slightly more aggressive replacement for |
| 97 | /// isLoopInvariant. |
| 98 | /// |
| 99 | /// If InsertPt is specified, it is the point to hoist instructions to. |
| 100 | /// If null, the terminator of the loop preheader is used. |
| 101 | /// |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 102 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, |
| 103 | Instruction *InsertPt) const { |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 104 | // Test if the value is already loop-invariant. |
| 105 | if (isLoopInvariant(I)) |
| 106 | return true; |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 107 | if (!isSafeToSpeculativelyExecute(I)) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 108 | return false; |
Eli Friedman | b8f6a4f | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 109 | if (I->mayReadFromMemory()) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 110 | return false; |
Bill Wendling | a9ee09f | 2011-08-17 20:36:44 +0000 | [diff] [blame] | 111 | // The landingpad instruction is immobile. |
| 112 | if (isa<LandingPadInst>(I)) |
| 113 | return false; |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 114 | // Determine the insertion point, unless one was given. |
| 115 | if (!InsertPt) { |
| 116 | BasicBlock *Preheader = getLoopPreheader(); |
| 117 | // Without a preheader, hoisting is not feasible. |
| 118 | if (!Preheader) |
| 119 | return false; |
| 120 | InsertPt = Preheader->getTerminator(); |
| 121 | } |
| 122 | // Don't hoist instructions with loop-variant operands. |
| 123 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 124 | if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt)) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 125 | return false; |
Andrew Trick | f898cbd | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 127 | // Hoist. |
| 128 | I->moveBefore(InsertPt); |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 129 | Changed = true; |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 133 | /// getCanonicalInductionVariable - Check to see if the loop has a canonical |
| 134 | /// induction variable: an integer recurrence that starts at 0 and increments |
| 135 | /// by one each time through the loop. If so, return the phi node that |
| 136 | /// corresponds to it. |
| 137 | /// |
| 138 | /// The IndVarSimplify pass transforms loops to have a canonical induction |
| 139 | /// variable. |
| 140 | /// |
| 141 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 142 | BasicBlock *H = getHeader(); |
| 143 | |
| 144 | BasicBlock *Incoming = 0, *Backedge = 0; |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 145 | pred_iterator PI = pred_begin(H); |
| 146 | assert(PI != pred_end(H) && |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 147 | "Loop must have at least one backedge!"); |
| 148 | Backedge = *PI++; |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 149 | if (PI == pred_end(H)) return 0; // dead loop |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 150 | Incoming = *PI++; |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 151 | if (PI != pred_end(H)) return 0; // multiple backedges? |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 152 | |
| 153 | if (contains(Incoming)) { |
| 154 | if (contains(Backedge)) |
| 155 | return 0; |
| 156 | std::swap(Incoming, Backedge); |
| 157 | } else if (!contains(Backedge)) |
| 158 | return 0; |
| 159 | |
| 160 | // Loop over all of the PHI nodes, looking for a canonical indvar. |
| 161 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
| 162 | PHINode *PN = cast<PHINode>(I); |
| 163 | if (ConstantInt *CI = |
| 164 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) |
| 165 | if (CI->isNullValue()) |
| 166 | if (Instruction *Inc = |
| 167 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) |
| 168 | if (Inc->getOpcode() == Instruction::Add && |
| 169 | Inc->getOperand(0) == PN) |
| 170 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
| 171 | if (CI->equalsInt(1)) |
| 172 | return PN; |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 177 | /// isLCSSAForm - Return true if the Loop is in LCSSA form |
Dan Gohman | 2734ebd | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 178 | bool Loop::isLCSSAForm(DominatorTree &DT) const { |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 179 | for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) { |
Dan Gohman | 5196e41 | 2009-11-09 18:19:43 +0000 | [diff] [blame] | 180 | BasicBlock *BB = *BI; |
| 181 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 182 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 183 | ++UI) { |
Gabor Greif | 2732561 | 2010-07-09 14:28:41 +0000 | [diff] [blame] | 184 | User *U = *UI; |
| 185 | BasicBlock *UserBB = cast<Instruction>(U)->getParent(); |
| 186 | if (PHINode *P = dyn_cast<PHINode>(U)) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 187 | UserBB = P->getIncomingBlock(UI); |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 188 | |
Dan Gohman | 93452ce | 2010-03-09 01:53:33 +0000 | [diff] [blame] | 189 | // Check the current block, as a fast-path, before checking whether |
| 190 | // the use is anywhere in the loop. Most values are used in the same |
| 191 | // block they are defined in. Also, blocks not reachable from the |
| 192 | // entry are special; uses in them don't need to go through PHIs. |
| 193 | if (UserBB != BB && |
Wan Xiaofei | be640b2 | 2013-10-26 03:08:02 +0000 | [diff] [blame] | 194 | !contains(UserBB) && |
Dan Gohman | 2734ebd | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 195 | DT.isReachableFromEntry(UserBB)) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
Dan Gohman | 1511f70 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 202 | |
| 203 | /// isLoopSimplifyForm - Return true if the Loop is in the form that |
| 204 | /// the LoopSimplify form transforms loops to, which is sometimes called |
| 205 | /// normal form. |
| 206 | bool Loop::isLoopSimplifyForm() const { |
Dan Gohman | e3a1706 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 207 | // Normal-form loops have a preheader, a single backedge, and all of their |
| 208 | // exits have all their predecessors inside the loop. |
| 209 | return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); |
| 210 | } |
| 211 | |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 212 | /// isSafeToClone - Return true if the loop body is safe to clone in practice. |
| 213 | /// Routines that reform the loop CFG and split edges often fail on indirectbr. |
| 214 | bool Loop::isSafeToClone() const { |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 215 | // Return false if any loop blocks contain indirectbrs, or there are any calls |
| 216 | // to noduplicate functions. |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 217 | for (Loop::block_iterator I = block_begin(), E = block_end(); I != E; ++I) { |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 218 | if (isa<IndirectBrInst>((*I)->getTerminator())) |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 219 | return false; |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 220 | |
| 221 | if (const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator())) |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 222 | if (II->hasFnAttr(Attribute::NoDuplicate)) |
| 223 | return false; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 224 | |
| 225 | for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); BI != BE; ++BI) { |
| 226 | if (const CallInst *CI = dyn_cast<CallInst>(BI)) { |
| 227 | if (CI->hasFnAttr(Attribute::NoDuplicate)) |
| 228 | return false; |
| 229 | } |
| 230 | } |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 235 | MDNode *Loop::getLoopID() const { |
| 236 | MDNode *LoopID = 0; |
| 237 | if (isLoopSimplifyForm()) { |
| 238 | LoopID = getLoopLatch()->getTerminator()->getMetadata(LoopMDName); |
| 239 | } else { |
| 240 | // Go through each predecessor of the loop header and check the |
| 241 | // terminator for the metadata. |
| 242 | BasicBlock *H = getHeader(); |
| 243 | for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) { |
| 244 | TerminatorInst *TI = (*I)->getTerminator(); |
| 245 | MDNode *MD = 0; |
| 246 | |
| 247 | // Check if this terminator branches to the loop header. |
| 248 | for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) { |
| 249 | if (TI->getSuccessor(i) == H) { |
| 250 | MD = TI->getMetadata(LoopMDName); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | if (!MD) |
| 255 | return 0; |
| 256 | |
| 257 | if (!LoopID) |
| 258 | LoopID = MD; |
| 259 | else if (MD != LoopID) |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | if (!LoopID || LoopID->getNumOperands() == 0 || |
| 264 | LoopID->getOperand(0) != LoopID) |
| 265 | return 0; |
| 266 | return LoopID; |
| 267 | } |
| 268 | |
| 269 | void Loop::setLoopID(MDNode *LoopID) const { |
| 270 | assert(LoopID && "Loop ID should not be null"); |
| 271 | assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand"); |
| 272 | assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself"); |
| 273 | |
| 274 | if (isLoopSimplifyForm()) { |
| 275 | getLoopLatch()->getTerminator()->setMetadata(LoopMDName, LoopID); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | BasicBlock *H = getHeader(); |
| 280 | for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) { |
| 281 | TerminatorInst *TI = (*I)->getTerminator(); |
| 282 | for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) { |
| 283 | if (TI->getSuccessor(i) == H) |
| 284 | TI->setMetadata(LoopMDName, LoopID); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 289 | bool Loop::isAnnotatedParallel() const { |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 290 | MDNode *desiredLoopIdMetadata = getLoopID(); |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 291 | |
| 292 | if (!desiredLoopIdMetadata) |
| 293 | return false; |
| 294 | |
| 295 | // The loop branch contains the parallel loop metadata. In order to ensure |
| 296 | // that any parallel-loop-unaware optimization pass hasn't added loop-carried |
| 297 | // dependencies (thus converted the loop back to a sequential loop), check |
| 298 | // that all the memory instructions in the loop contain parallelism metadata |
| 299 | // that point to the same unique "loop id metadata" the loop branch does. |
| 300 | for (block_iterator BB = block_begin(), BE = block_end(); BB != BE; ++BB) { |
| 301 | for (BasicBlock::iterator II = (*BB)->begin(), EE = (*BB)->end(); |
| 302 | II != EE; II++) { |
| 303 | |
| 304 | if (!II->mayReadOrWriteMemory()) |
| 305 | continue; |
| 306 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 307 | // The memory instruction can refer to the loop identifier metadata |
| 308 | // directly or indirectly through another list metadata (in case of |
| 309 | // nested parallel loops). The loop identifier metadata refers to |
| 310 | // itself so we can check both cases with the same routine. |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 311 | MDNode *loopIdMD = II->getMetadata("llvm.mem.parallel_loop_access"); |
| 312 | |
| 313 | if (!loopIdMD) |
| 314 | return false; |
| 315 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 316 | bool loopIdMDFound = false; |
| 317 | for (unsigned i = 0, e = loopIdMD->getNumOperands(); i < e; ++i) { |
| 318 | if (loopIdMD->getOperand(i) == desiredLoopIdMetadata) { |
| 319 | loopIdMDFound = true; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (!loopIdMDFound) |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | |
Dan Gohman | e3a1706 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 332 | /// hasDedicatedExits - Return true if no exit block for the loop |
| 333 | /// has a predecessor that is outside the loop. |
| 334 | bool Loop::hasDedicatedExits() const { |
Dan Gohman | 1511f70 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 335 | // Each predecessor of each exit block of a normal loop is contained |
| 336 | // within the loop. |
| 337 | SmallVector<BasicBlock *, 4> ExitBlocks; |
| 338 | getExitBlocks(ExitBlocks); |
| 339 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 340 | for (pred_iterator PI = pred_begin(ExitBlocks[i]), |
| 341 | PE = pred_end(ExitBlocks[i]); PI != PE; ++PI) |
Wan Xiaofei | be640b2 | 2013-10-26 03:08:02 +0000 | [diff] [blame] | 342 | if (!contains(*PI)) |
Dan Gohman | 1511f70 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 343 | return false; |
| 344 | // All the requirements are met. |
| 345 | return true; |
| 346 | } |
| 347 | |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 348 | /// getUniqueExitBlocks - Return all unique successor blocks of this loop. |
| 349 | /// 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] | 350 | /// This assumes that loop exits are in canonical form. |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 351 | /// |
| 352 | void |
| 353 | Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const { |
Dan Gohman | 84ba039 | 2009-12-11 20:05:23 +0000 | [diff] [blame] | 354 | assert(hasDedicatedExits() && |
| 355 | "getUniqueExitBlocks assumes the loop has canonical form exits!"); |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 356 | |
Dan Gohman | ed8f320 | 2009-09-03 20:36:13 +0000 | [diff] [blame] | 357 | SmallVector<BasicBlock *, 32> switchExitBlocks; |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 358 | |
| 359 | for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) { |
| 360 | |
| 361 | BasicBlock *current = *BI; |
| 362 | switchExitBlocks.clear(); |
| 363 | |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 364 | 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] | 365 | // If block is inside the loop then it is not a exit block. |
Wan Xiaofei | be640b2 | 2013-10-26 03:08:02 +0000 | [diff] [blame] | 366 | if (contains(*I)) |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 367 | continue; |
| 368 | |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 369 | pred_iterator PI = pred_begin(*I); |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 370 | BasicBlock *firstPred = *PI; |
| 371 | |
| 372 | // If current basic block is this exit block's first predecessor |
| 373 | // then only insert exit block in to the output ExitBlocks vector. |
| 374 | // This ensures that same exit block is not inserted twice into |
| 375 | // ExitBlocks vector. |
| 376 | if (current != firstPred) |
| 377 | continue; |
| 378 | |
| 379 | // If a terminator has more then two successors, for example SwitchInst, |
| 380 | // then it is possible that there are multiple edges from current block |
| 381 | // to one exit block. |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 382 | if (std::distance(succ_begin(current), succ_end(current)) <= 2) { |
Dan Gohman | 3a0ce3e | 2009-09-03 16:10:48 +0000 | [diff] [blame] | 383 | ExitBlocks.push_back(*I); |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | // In case of multiple edges from current block to exit block, collect |
| 388 | // only one edge in ExitBlocks. Use switchExitBlocks to keep track of |
| 389 | // duplicate edges. |
| 390 | if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I) |
| 391 | == switchExitBlocks.end()) { |
| 392 | switchExitBlocks.push_back(*I); |
| 393 | ExitBlocks.push_back(*I); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one |
| 400 | /// block, return that block. Otherwise return null. |
| 401 | BasicBlock *Loop::getUniqueExitBlock() const { |
| 402 | SmallVector<BasicBlock *, 8> UniqueExitBlocks; |
| 403 | getUniqueExitBlocks(UniqueExitBlocks); |
| 404 | if (UniqueExitBlocks.size() == 1) |
| 405 | return UniqueExitBlocks[0]; |
| 406 | return 0; |
| 407 | } |
| 408 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 409 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 410 | void Loop::dump() const { |
| 411 | print(dbgs()); |
| 412 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 413 | #endif |
Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 416 | // UnloopUpdater implementation |
| 417 | // |
| 418 | |
Benjamin Kramer | 4938edb | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 419 | namespace { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 420 | /// Find the new parent loop for all blocks within the "unloop" whose last |
| 421 | /// backedges has just been removed. |
| 422 | class UnloopUpdater { |
| 423 | Loop *Unloop; |
| 424 | LoopInfo *LI; |
| 425 | |
| 426 | LoopBlocksDFS DFS; |
| 427 | |
| 428 | // Map unloop's immediate subloops to their nearest reachable parents. Nested |
| 429 | // loops within these subloops will not change parents. However, an immediate |
| 430 | // subloop's new parent will be the nearest loop reachable from either its own |
| 431 | // exits *or* any of its nested loop's exits. |
| 432 | DenseMap<Loop*, Loop*> SubloopParents; |
| 433 | |
| 434 | // Flag the presence of an irreducible backedge whose destination is a block |
| 435 | // directly contained by the original unloop. |
| 436 | bool FoundIB; |
| 437 | |
| 438 | public: |
| 439 | UnloopUpdater(Loop *UL, LoopInfo *LInfo) : |
| 440 | Unloop(UL), LI(LInfo), DFS(UL), FoundIB(false) {} |
| 441 | |
| 442 | void updateBlockParents(); |
| 443 | |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 444 | void removeBlocksFromAncestors(); |
| 445 | |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 446 | void updateSubloopParents(); |
| 447 | |
| 448 | protected: |
| 449 | Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop); |
| 450 | }; |
Benjamin Kramer | 4938edb | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 451 | } // end anonymous namespace |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 452 | |
| 453 | /// updateBlockParents - Update the parent loop for all blocks that are directly |
| 454 | /// contained within the original "unloop". |
| 455 | void UnloopUpdater::updateBlockParents() { |
| 456 | if (Unloop->getNumBlocks()) { |
| 457 | // Perform a post order CFG traversal of all blocks within this loop, |
| 458 | // propagating the nearest loop from sucessors to predecessors. |
| 459 | LoopBlocksTraversal Traversal(DFS, LI); |
| 460 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 461 | POE = Traversal.end(); POI != POE; ++POI) { |
| 462 | |
| 463 | Loop *L = LI->getLoopFor(*POI); |
| 464 | Loop *NL = getNearestLoop(*POI, L); |
| 465 | |
| 466 | if (NL != L) { |
| 467 | // For reducible loops, NL is now an ancestor of Unloop. |
| 468 | assert((NL != Unloop && (!NL || NL->contains(Unloop))) && |
| 469 | "uninitialized successor"); |
| 470 | LI->changeLoopFor(*POI, NL); |
| 471 | } |
| 472 | else { |
| 473 | // Or the current block is part of a subloop, in which case its parent |
| 474 | // is unchanged. |
| 475 | assert((FoundIB || Unloop->contains(L)) && "uninitialized successor"); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | // Each irreducible loop within the unloop induces a round of iteration using |
| 480 | // the DFS result cached by Traversal. |
| 481 | bool Changed = FoundIB; |
| 482 | for (unsigned NIters = 0; Changed; ++NIters) { |
| 483 | assert(NIters < Unloop->getNumBlocks() && "runaway iterative algorithm"); |
| 484 | |
| 485 | // Iterate over the postorder list of blocks, propagating the nearest loop |
| 486 | // from successors to predecessors as before. |
| 487 | Changed = false; |
| 488 | for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), |
| 489 | POE = DFS.endPostorder(); POI != POE; ++POI) { |
| 490 | |
| 491 | Loop *L = LI->getLoopFor(*POI); |
| 492 | Loop *NL = getNearestLoop(*POI, L); |
| 493 | if (NL != L) { |
| 494 | assert(NL != Unloop && (!NL || NL->contains(Unloop)) && |
| 495 | "uninitialized successor"); |
| 496 | LI->changeLoopFor(*POI, NL); |
| 497 | Changed = true; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 503 | /// removeBlocksFromAncestors - Remove unloop's blocks from all ancestors below |
| 504 | /// their new parents. |
| 505 | void UnloopUpdater::removeBlocksFromAncestors() { |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 506 | // Remove all unloop's blocks (including those in nested subloops) from |
| 507 | // ancestors below the new parent loop. |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 508 | for (Loop::block_iterator BI = Unloop->block_begin(), |
| 509 | BE = Unloop->block_end(); BI != BE; ++BI) { |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 510 | Loop *OuterParent = LI->getLoopFor(*BI); |
| 511 | if (Unloop->contains(OuterParent)) { |
| 512 | while (OuterParent->getParentLoop() != Unloop) |
| 513 | OuterParent = OuterParent->getParentLoop(); |
| 514 | OuterParent = SubloopParents[OuterParent]; |
| 515 | } |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 516 | // Remove blocks from former Ancestors except Unloop itself which will be |
| 517 | // deleted. |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 518 | for (Loop *OldParent = Unloop->getParentLoop(); OldParent != OuterParent; |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 519 | OldParent = OldParent->getParentLoop()) { |
| 520 | assert(OldParent && "new loop is not an ancestor of the original"); |
| 521 | OldParent->removeBlockFromLoop(*BI); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 526 | /// updateSubloopParents - Update the parent loop for all subloops directly |
| 527 | /// nested within unloop. |
| 528 | void UnloopUpdater::updateSubloopParents() { |
| 529 | while (!Unloop->empty()) { |
Andrew Trick | 266ab10 | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 530 | Loop *Subloop = *llvm::prior(Unloop->end()); |
| 531 | Unloop->removeChildLoop(llvm::prior(Unloop->end())); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 532 | |
| 533 | assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); |
Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 534 | if (Loop *Parent = SubloopParents[Subloop]) |
| 535 | Parent->addChildLoop(Subloop); |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 536 | else |
| 537 | LI->addTopLevelLoop(Subloop); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | /// getNearestLoop - Return the nearest parent loop among this block's |
| 542 | /// successors. If a successor is a subloop header, consider its parent to be |
| 543 | /// the nearest parent of the subloop's exits. |
| 544 | /// |
| 545 | /// For subloop blocks, simply update SubloopParents and return NULL. |
| 546 | Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { |
| 547 | |
Andrew Trick | 266ab10 | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 548 | // Initially for blocks directly contained by Unloop, NearLoop == Unloop and |
| 549 | // is considered uninitialized. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 550 | Loop *NearLoop = BBLoop; |
| 551 | |
| 552 | Loop *Subloop = 0; |
| 553 | if (NearLoop != Unloop && Unloop->contains(NearLoop)) { |
| 554 | Subloop = NearLoop; |
| 555 | // Find the subloop ancestor that is directly contained within Unloop. |
| 556 | while (Subloop->getParentLoop() != Unloop) { |
| 557 | Subloop = Subloop->getParentLoop(); |
| 558 | assert(Subloop && "subloop is not an ancestor of the original loop"); |
| 559 | } |
| 560 | // Get the current nearest parent of the Subloop exits, initially Unloop. |
Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 561 | NearLoop = |
| 562 | SubloopParents.insert(std::make_pair(Subloop, Unloop)).first->second; |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | succ_iterator I = succ_begin(BB), E = succ_end(BB); |
| 566 | if (I == E) { |
| 567 | assert(!Subloop && "subloop blocks must have a successor"); |
| 568 | NearLoop = 0; // unloop blocks may now exit the function. |
| 569 | } |
| 570 | for (; I != E; ++I) { |
| 571 | if (*I == BB) |
| 572 | continue; // self loops are uninteresting |
| 573 | |
| 574 | Loop *L = LI->getLoopFor(*I); |
| 575 | if (L == Unloop) { |
| 576 | // This successor has not been processed. This path must lead to an |
| 577 | // irreducible backedge. |
| 578 | assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB"); |
| 579 | FoundIB = true; |
| 580 | } |
| 581 | if (L != Unloop && Unloop->contains(L)) { |
| 582 | // Successor is in a subloop. |
| 583 | if (Subloop) |
| 584 | continue; // Branching within subloops. Ignore it. |
| 585 | |
| 586 | // BB branches from the original into a subloop header. |
| 587 | assert(L->getParentLoop() == Unloop && "cannot skip into nested loops"); |
| 588 | |
| 589 | // Get the current nearest parent of the Subloop's exits. |
| 590 | L = SubloopParents[L]; |
| 591 | // L could be Unloop if the only exit was an irreducible backedge. |
| 592 | } |
| 593 | if (L == Unloop) { |
| 594 | continue; |
| 595 | } |
| 596 | // Handle critical edges from Unloop into a sibling loop. |
| 597 | if (L && !L->contains(Unloop)) { |
| 598 | L = L->getParentLoop(); |
| 599 | } |
| 600 | // Remember the nearest parent loop among successors or subloop exits. |
| 601 | if (NearLoop == Unloop || !NearLoop || NearLoop->contains(L)) |
| 602 | NearLoop = L; |
| 603 | } |
| 604 | if (Subloop) { |
| 605 | SubloopParents[Subloop] = NearLoop; |
| 606 | return BBLoop; |
| 607 | } |
| 608 | return NearLoop; |
| 609 | } |
| 610 | |
| 611 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 612 | // LoopInfo implementation |
| 613 | // |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 614 | bool LoopInfo::runOnFunction(Function &) { |
| 615 | releaseMemory(); |
Andrew Trick | fb2ba3e | 2012-06-26 04:11:38 +0000 | [diff] [blame] | 616 | LI.Analyze(getAnalysis<DominatorTree>().getBase()); |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 617 | return false; |
| 618 | } |
| 619 | |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 620 | /// updateUnloop - The last backedge has been removed from a loop--now the |
| 621 | /// "unloop". Find a new parent for the blocks contained within unloop and |
Andrew Trick | 266ab10 | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 622 | /// update the loop tree. We don't necessarily have valid dominators at this |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 623 | /// point, but LoopInfo is still valid except for the removal of this loop. |
| 624 | /// |
| 625 | /// Note that Unloop may now be an empty loop. Calling Loop::getHeader without |
| 626 | /// checking first is illegal. |
| 627 | void LoopInfo::updateUnloop(Loop *Unloop) { |
| 628 | |
| 629 | // First handle the special case of no parent loop to simplify the algorithm. |
| 630 | if (!Unloop->getParentLoop()) { |
| 631 | // Since BBLoop had no parent, Unloop blocks are no longer in a loop. |
| 632 | for (Loop::block_iterator I = Unloop->block_begin(), |
| 633 | E = Unloop->block_end(); I != E; ++I) { |
| 634 | |
| 635 | // Don't reparent blocks in subloops. |
| 636 | if (getLoopFor(*I) != Unloop) |
| 637 | continue; |
| 638 | |
| 639 | // Blocks no longer have a parent but are still referenced by Unloop until |
| 640 | // the Unloop object is deleted. |
| 641 | LI.changeLoopFor(*I, 0); |
| 642 | } |
| 643 | |
| 644 | // Remove the loop from the top-level LoopInfo object. |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 645 | for (LoopInfo::iterator I = LI.begin();; ++I) { |
| 646 | assert(I != LI.end() && "Couldn't find loop"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 647 | if (*I == Unloop) { |
| 648 | LI.removeLoop(I); |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // Move all of the subloops to the top-level. |
| 654 | while (!Unloop->empty()) |
Andrew Trick | 266ab10 | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 655 | LI.addTopLevelLoop(Unloop->removeChildLoop(llvm::prior(Unloop->end()))); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 656 | |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | // Update the parent loop for all blocks within the loop. Blocks within |
| 661 | // subloops will not change parents. |
| 662 | UnloopUpdater Updater(Unloop, this); |
| 663 | Updater.updateBlockParents(); |
| 664 | |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 665 | // Remove blocks from former ancestor loops. |
| 666 | Updater.removeBlocksFromAncestors(); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 667 | |
| 668 | // Add direct subloops as children in their new parent loop. |
| 669 | Updater.updateSubloopParents(); |
| 670 | |
| 671 | // Remove unloop from its parent loop. |
| 672 | Loop *ParentLoop = Unloop->getParentLoop(); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 673 | for (Loop::iterator I = ParentLoop->begin();; ++I) { |
| 674 | assert(I != ParentLoop->end() && "Couldn't find loop"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 675 | if (*I == Unloop) { |
| 676 | ParentLoop->removeChildLoop(I); |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 682 | void LoopInfo::verifyAnalysis() const { |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 683 | // LoopInfo is a FunctionPass, but verifying every loop in the function |
| 684 | // each time verifyAnalysis is called is very expensive. The |
| 685 | // -verify-loop-info option can enable this. In order to perform some |
| 686 | // checking by default, LoopPass has been taught to call verifyLoop |
| 687 | // manually during loop pass sequences. |
| 688 | |
| 689 | if (!VerifyLoopInfo) return; |
| 690 | |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 691 | DenseSet<const Loop*> Loops; |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 692 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 693 | assert(!(*I)->getParentLoop() && "Top-level loop has a parent!"); |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 694 | (*I)->verifyLoopNest(&Loops); |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 695 | } |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 696 | |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 697 | // Verify that blocks are mapped to valid loops. |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 698 | for (DenseMap<BasicBlock*, Loop*>::const_iterator I = LI.BBMap.begin(), |
| 699 | E = LI.BBMap.end(); I != E; ++I) { |
| 700 | assert(Loops.count(I->second) && "orphaned loop"); |
| 701 | assert(I->second->contains(I->first) && "orphaned block"); |
| 702 | } |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 705 | void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 706 | AU.setPreservesAll(); |
Devang Patel | aee309e | 2007-06-08 00:17:13 +0000 | [diff] [blame] | 707 | AU.addRequired<DominatorTree>(); |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 708 | } |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 710 | void LoopInfo::print(raw_ostream &OS, const Module*) const { |
| 711 | LI.print(OS); |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Andrew Trick | 78b40c3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 714 | //===----------------------------------------------------------------------===// |
| 715 | // LoopBlocksDFS implementation |
| 716 | // |
| 717 | |
| 718 | /// Traverse the loop blocks and store the DFS result. |
| 719 | /// Useful for clients that just want the final DFS result and don't need to |
| 720 | /// visit blocks during the initial traversal. |
| 721 | void LoopBlocksDFS::perform(LoopInfo *LI) { |
| 722 | LoopBlocksTraversal Traversal(*this, LI); |
| 723 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
| 724 | POE = Traversal.end(); POI != POE; ++POI) ; |
| 725 | } |