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