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