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