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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6de9942 | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file defines the LoopInfo class that is used to identify natural loops |
| 10 | // and determine the loop depth of various nodes of the CFG. Note that the |
| 11 | // loops identified may actually be several natural loops that share the same |
| 12 | // header node... not just a single natural loop. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | 81804b4 | 2004-01-30 17:26:24 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DepthFirstIterator.h" |
Sanjoy Das | 09613b1 | 2017-09-20 02:31:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ScopeExit.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Whitney Tsang | 2d0896c | 2019-06-05 20:42:47 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/IVDescriptors.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" |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/MemorySSA.h" |
| 24 | #include "llvm/Analysis/MemorySSAUpdater.h" |
Whitney Tsang | 2d0896c | 2019-06-05 20:42:47 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ValueTracking.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 27 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 28 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Constants.h" |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Dominators.h" |
Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instructions.h" |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 34 | #include "llvm/IR/LLVMContext.h" |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 36 | #include "llvm/IR/PassManager.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 6de9942 | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Chris Lattner | 55b7ef5 | 2004-04-12 20:26:17 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Andrew Trick | cda51d4 | 2012-06-20 03:42:09 +0000 | [diff] [blame] | 43 | // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops. |
| 44 | template class llvm::LoopBase<BasicBlock, Loop>; |
| 45 | template class llvm::LoopInfoBase<BasicBlock, Loop>; |
| 46 | |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 47 | // Always verify loopinfo if expensive checking is enabled. |
Filipe Cabecinhas | 0da9937 | 2016-04-29 15:22:48 +0000 | [diff] [blame] | 48 | #ifdef EXPENSIVE_CHECKS |
Serge Pavlov | 69b3ff9 | 2017-01-24 05:52:07 +0000 | [diff] [blame] | 49 | bool llvm::VerifyLoopInfo = true; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 50 | #else |
Serge Pavlov | 69b3ff9 | 2017-01-24 05:52:07 +0000 | [diff] [blame] | 51 | bool llvm::VerifyLoopInfo = false; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 52 | #endif |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 53 | static cl::opt<bool, true> |
| 54 | VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 55 | cl::Hidden, cl::desc("Verify loop info (time consuming)")); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 56 | |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 58 | // Loop implementation |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 59 | // |
Misha Brukman | 3845be2 | 2002-10-11 05:31:10 +0000 | [diff] [blame] | 60 | |
Pete Cooper | 016daa6 | 2015-05-13 01:12:06 +0000 | [diff] [blame] | 61 | bool Loop::isLoopInvariant(const Value *V) const { |
| 62 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | da24b9a | 2010-09-06 01:05:37 +0000 | [diff] [blame] | 63 | return !contains(I); |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 64 | return true; // All non-instructions are loop invariant |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Pete Cooper | 016daa6 | 2015-05-13 01:12:06 +0000 | [diff] [blame] | 67 | bool Loop::hasLoopInvariantOperands(const Instruction *I) const { |
Pete Cooper | a264dc0 | 2015-05-13 22:19:13 +0000 | [diff] [blame] | 68 | return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); }); |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 71 | bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt, |
| 72 | MemorySSAUpdater *MSSAU) const { |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 73 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 74 | return makeLoopInvariant(I, Changed, InsertPt, MSSAU); |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 75 | return true; // All non-instructions are loop-invariant. |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 78 | bool Loop::makeLoopInvariant(Instruction *I, bool &Changed, |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 79 | Instruction *InsertPt, |
| 80 | MemorySSAUpdater *MSSAU) const { |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 81 | // Test if the value is already loop-invariant. |
| 82 | if (isLoopInvariant(I)) |
| 83 | return true; |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 84 | if (!isSafeToSpeculativelyExecute(I)) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 85 | return false; |
Eli Friedman | b8f6a4f | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 86 | if (I->mayReadFromMemory()) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 87 | return false; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 88 | // EH block instructions are immobile. |
| 89 | if (I->isEHPad()) |
Bill Wendling | a9ee09f | 2011-08-17 20:36:44 +0000 | [diff] [blame] | 90 | return false; |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 91 | // Determine the insertion point, unless one was given. |
| 92 | if (!InsertPt) { |
| 93 | BasicBlock *Preheader = getLoopPreheader(); |
| 94 | // Without a preheader, hoisting is not feasible. |
| 95 | if (!Preheader) |
| 96 | return false; |
| 97 | InsertPt = Preheader->getTerminator(); |
| 98 | } |
| 99 | // Don't hoist instructions with loop-variant operands. |
Sanjay Patel | 960e534 | 2016-01-15 00:08:10 +0000 | [diff] [blame] | 100 | for (Value *Operand : I->operands()) |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 101 | if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU)) |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 102 | return false; |
Andrew Trick | f898cbd | 2011-08-03 23:45:50 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 104 | // Hoist. |
| 105 | I->moveBefore(InsertPt); |
Alina Sbirlea | f31eba6 | 2019-05-08 17:05:36 +0000 | [diff] [blame] | 106 | if (MSSAU) |
| 107 | if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I)) |
| 108 | MSSAU->moveToPlace(MUD, InsertPt->getParent(), MemorySSA::End); |
Igor Laevsky | 7310c68 | 2015-11-18 14:50:18 +0000 | [diff] [blame] | 109 | |
| 110 | // There is possibility of hoisting this instruction above some arbitrary |
| 111 | // condition. Any metadata defined on it can be control dependent on this |
| 112 | // condition. Conservatively strip it here so that we don't give any wrong |
| 113 | // information to the optimizer. |
| 114 | I->dropUnknownNonDebugMetadata(); |
| 115 | |
Dan Gohman | c43e479 | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 116 | Changed = true; |
Dan Gohman | 6f6d864 | 2009-07-14 01:06:29 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 120 | bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming, |
| 121 | BasicBlock *&Backedge) const { |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 122 | BasicBlock *H = getHeader(); |
| 123 | |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 124 | Incoming = nullptr; |
| 125 | Backedge = nullptr; |
Dan Gohman | acafc61 | 2010-07-23 21:25:16 +0000 | [diff] [blame] | 126 | pred_iterator PI = pred_begin(H); |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 127 | assert(PI != pred_end(H) && "Loop must have at least one backedge!"); |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 128 | Backedge = *PI++; |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 129 | if (PI == pred_end(H)) |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 130 | return false; // dead loop |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 131 | Incoming = *PI++; |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 132 | if (PI != pred_end(H)) |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 133 | return false; // multiple backedges? |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 134 | |
| 135 | if (contains(Incoming)) { |
| 136 | if (contains(Backedge)) |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 137 | return false; |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 138 | std::swap(Incoming, Backedge); |
| 139 | } else if (!contains(Backedge)) |
Mircea Trofin | b27d0fd | 2019-03-29 17:39:17 +0000 | [diff] [blame] | 140 | return false; |
| 141 | |
| 142 | assert(Incoming && Backedge && "expected non-null incoming and backedges"); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | PHINode *Loop::getCanonicalInductionVariable() const { |
| 147 | BasicBlock *H = getHeader(); |
| 148 | |
| 149 | BasicBlock *Incoming = nullptr, *Backedge = nullptr; |
| 150 | if (!getIncomingAndBackEdge(Incoming, Backedge)) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 151 | return nullptr; |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 152 | |
| 153 | // Loop over all of the PHI nodes, looking for a canonical indvar. |
| 154 | for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { |
| 155 | PHINode *PN = cast<PHINode>(I); |
| 156 | if (ConstantInt *CI = |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 157 | dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 158 | if (CI->isZero()) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 159 | if (Instruction *Inc = |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 160 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge))) |
| 161 | if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 162 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1))) |
Craig Topper | ca2c876 | 2017-07-06 18:39:49 +0000 | [diff] [blame] | 163 | if (CI->isOne()) |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 164 | return PN; |
| 165 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 166 | return nullptr; |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Whitney Tsang | 2d0896c | 2019-06-05 20:42:47 +0000 | [diff] [blame] | 169 | /// Get the latch condition instruction. |
| 170 | static ICmpInst *getLatchCmpInst(const Loop &L) { |
| 171 | if (BasicBlock *Latch = L.getLoopLatch()) |
| 172 | if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator())) |
| 173 | if (BI->isConditional()) |
| 174 | return dyn_cast<ICmpInst>(BI->getCondition()); |
| 175 | |
| 176 | return nullptr; |
| 177 | } |
| 178 | |
| 179 | /// Return the final value of the loop induction variable if found. |
| 180 | static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar, |
| 181 | const Instruction &StepInst) { |
| 182 | ICmpInst *LatchCmpInst = getLatchCmpInst(L); |
| 183 | if (!LatchCmpInst) |
| 184 | return nullptr; |
| 185 | |
| 186 | Value *Op0 = LatchCmpInst->getOperand(0); |
| 187 | Value *Op1 = LatchCmpInst->getOperand(1); |
| 188 | if (Op0 == &IndVar || Op0 == &StepInst) |
| 189 | return Op1; |
| 190 | |
| 191 | if (Op1 == &IndVar || Op1 == &StepInst) |
| 192 | return Op0; |
| 193 | |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | Optional<Loop::LoopBounds> Loop::LoopBounds::getBounds(const Loop &L, |
| 198 | PHINode &IndVar, |
| 199 | ScalarEvolution &SE) { |
| 200 | InductionDescriptor IndDesc; |
| 201 | if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc)) |
| 202 | return None; |
| 203 | |
| 204 | Value *InitialIVValue = IndDesc.getStartValue(); |
| 205 | Instruction *StepInst = IndDesc.getInductionBinOp(); |
| 206 | if (!InitialIVValue || !StepInst) |
| 207 | return None; |
| 208 | |
| 209 | const SCEV *Step = IndDesc.getStep(); |
| 210 | Value *StepInstOp1 = StepInst->getOperand(1); |
| 211 | Value *StepInstOp0 = StepInst->getOperand(0); |
| 212 | Value *StepValue = nullptr; |
| 213 | if (SE.getSCEV(StepInstOp1) == Step) |
| 214 | StepValue = StepInstOp1; |
| 215 | else if (SE.getSCEV(StepInstOp0) == Step) |
| 216 | StepValue = StepInstOp0; |
| 217 | |
| 218 | Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst); |
| 219 | if (!FinalIVValue) |
| 220 | return None; |
| 221 | |
| 222 | return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue, |
| 223 | SE); |
| 224 | } |
| 225 | |
| 226 | using Direction = Loop::LoopBounds::Direction; |
| 227 | |
| 228 | ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const { |
| 229 | BasicBlock *Latch = L.getLoopLatch(); |
| 230 | assert(Latch && "Expecting valid latch"); |
| 231 | |
| 232 | BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()); |
| 233 | assert(BI && BI->isConditional() && "Expecting conditional latch branch"); |
| 234 | |
| 235 | ICmpInst *LatchCmpInst = dyn_cast<ICmpInst>(BI->getCondition()); |
| 236 | assert(LatchCmpInst && |
| 237 | "Expecting the latch compare instruction to be a CmpInst"); |
| 238 | |
| 239 | // Need to inverse the predicate when first successor is not the loop |
| 240 | // header |
| 241 | ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader()) |
| 242 | ? LatchCmpInst->getPredicate() |
| 243 | : LatchCmpInst->getInversePredicate(); |
| 244 | |
| 245 | if (LatchCmpInst->getOperand(0) == &getFinalIVValue()) |
| 246 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 247 | |
| 248 | // Need to flip strictness of the predicate when the latch compare instruction |
| 249 | // is not using StepInst |
| 250 | if (LatchCmpInst->getOperand(0) == &getStepInst() || |
| 251 | LatchCmpInst->getOperand(1) == &getStepInst()) |
| 252 | return Pred; |
| 253 | |
| 254 | // Cannot flip strictness of NE and EQ |
| 255 | if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ) |
| 256 | return ICmpInst::getFlippedStrictnessPredicate(Pred); |
| 257 | |
| 258 | Direction D = getDirection(); |
| 259 | if (D == Direction::Increasing) |
| 260 | return ICmpInst::ICMP_SLT; |
| 261 | |
| 262 | if (D == Direction::Decreasing) |
| 263 | return ICmpInst::ICMP_SGT; |
| 264 | |
| 265 | // If cannot determine the direction, then unable to find the canonical |
| 266 | // predicate |
| 267 | return ICmpInst::BAD_ICMP_PREDICATE; |
| 268 | } |
| 269 | |
| 270 | Direction Loop::LoopBounds::getDirection() const { |
| 271 | if (const SCEVAddRecExpr *StepAddRecExpr = |
| 272 | dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&getStepInst()))) |
| 273 | if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) { |
| 274 | if (SE.isKnownPositive(StepRecur)) |
| 275 | return Direction::Increasing; |
| 276 | if (SE.isKnownNegative(StepRecur)) |
| 277 | return Direction::Decreasing; |
| 278 | } |
| 279 | |
| 280 | return Direction::Unknown; |
| 281 | } |
| 282 | |
| 283 | Optional<Loop::LoopBounds> Loop::getBounds(ScalarEvolution &SE) const { |
| 284 | if (PHINode *IndVar = getInductionVariable(SE)) |
| 285 | return LoopBounds::getBounds(*this, *IndVar, SE); |
| 286 | |
| 287 | return None; |
| 288 | } |
| 289 | |
| 290 | PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const { |
| 291 | if (!isLoopSimplifyForm()) |
| 292 | return nullptr; |
| 293 | |
| 294 | BasicBlock *Header = getHeader(); |
| 295 | assert(Header && "Expected a valid loop header"); |
| 296 | ICmpInst *CmpInst = getLatchCmpInst(*this); |
| 297 | if (!CmpInst) |
| 298 | return nullptr; |
| 299 | |
| 300 | Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0)); |
| 301 | Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1)); |
| 302 | |
| 303 | for (PHINode &IndVar : Header->phis()) { |
| 304 | InductionDescriptor IndDesc; |
| 305 | if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc)) |
| 306 | continue; |
| 307 | |
| 308 | Instruction *StepInst = IndDesc.getInductionBinOp(); |
| 309 | |
| 310 | // case 1: |
| 311 | // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}] |
| 312 | // StepInst = IndVar + step |
| 313 | // cmp = StepInst < FinalValue |
| 314 | if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1) |
| 315 | return &IndVar; |
| 316 | |
| 317 | // case 2: |
| 318 | // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}] |
| 319 | // StepInst = IndVar + step |
| 320 | // cmp = IndVar < FinalValue |
| 321 | if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1) |
| 322 | return &IndVar; |
| 323 | } |
| 324 | |
| 325 | return nullptr; |
| 326 | } |
| 327 | |
| 328 | bool Loop::getInductionDescriptor(ScalarEvolution &SE, |
| 329 | InductionDescriptor &IndDesc) const { |
| 330 | if (PHINode *IndVar = getInductionVariable(SE)) |
| 331 | return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc); |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar, |
| 337 | ScalarEvolution &SE) const { |
| 338 | // Located in the loop header |
| 339 | BasicBlock *Header = getHeader(); |
| 340 | if (AuxIndVar.getParent() != Header) |
| 341 | return false; |
| 342 | |
| 343 | // No uses outside of the loop |
| 344 | for (User *U : AuxIndVar.users()) |
| 345 | if (const Instruction *I = dyn_cast<Instruction>(U)) |
| 346 | if (!contains(I)) |
| 347 | return false; |
| 348 | |
| 349 | InductionDescriptor IndDesc; |
| 350 | if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc)) |
| 351 | return false; |
| 352 | |
| 353 | // The step instruction opcode should be add or sub. |
| 354 | if (IndDesc.getInductionOpcode() != Instruction::Add && |
| 355 | IndDesc.getInductionOpcode() != Instruction::Sub) |
| 356 | return false; |
| 357 | |
| 358 | // Incremented by a loop invariant step for each loop iteration |
| 359 | return SE.isLoopInvariant(IndDesc.getStep(), this); |
| 360 | } |
| 361 | |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 362 | BranchInst *Loop::getLoopGuardBranch() const { |
Whitney Tsang | dcb75bf | 2019-10-06 16:39:43 +0000 | [diff] [blame] | 363 | if (!isLoopSimplifyForm()) |
| 364 | return nullptr; |
| 365 | |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 366 | BasicBlock *Preheader = getLoopPreheader(); |
Whitney Tsang | dcb75bf | 2019-10-06 16:39:43 +0000 | [diff] [blame] | 367 | BasicBlock *Latch = getLoopLatch(); |
| 368 | assert(Preheader && Latch && |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 369 | "Expecting a loop with valid preheader and latch"); |
Whitney Tsang | dcb75bf | 2019-10-06 16:39:43 +0000 | [diff] [blame] | 370 | |
| 371 | // Loop should be in rotate form. |
| 372 | if (!isLoopExiting(Latch)) |
| 373 | return nullptr; |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 374 | |
Whitney Tsang | 9c5fbcf | 2019-09-26 20:20:42 +0000 | [diff] [blame] | 375 | // Disallow loops with more than one unique exit block, as we do not verify |
| 376 | // that GuardOtherSucc post dominates all exit blocks. |
| 377 | BasicBlock *ExitFromLatch = getUniqueExitBlock(); |
| 378 | if (!ExitFromLatch) |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 379 | return nullptr; |
| 380 | |
Whitney Tsang | 8ee361e | 2019-07-25 16:13:18 +0000 | [diff] [blame] | 381 | BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor(); |
| 382 | if (!ExitFromLatchSucc) |
| 383 | return nullptr; |
| 384 | |
| 385 | BasicBlock *GuardBB = Preheader->getUniquePredecessor(); |
| 386 | if (!GuardBB) |
| 387 | return nullptr; |
| 388 | |
| 389 | assert(GuardBB->getTerminator() && "Expecting valid guard terminator"); |
| 390 | |
| 391 | BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator()); |
| 392 | if (!GuardBI || GuardBI->isUnconditional()) |
| 393 | return nullptr; |
| 394 | |
| 395 | BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader) |
| 396 | ? GuardBI->getSuccessor(1) |
| 397 | : GuardBI->getSuccessor(0); |
| 398 | return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr; |
| 399 | } |
| 400 | |
Whitney Tsang | 2d0896c | 2019-06-05 20:42:47 +0000 | [diff] [blame] | 401 | bool Loop::isCanonical(ScalarEvolution &SE) const { |
| 402 | InductionDescriptor IndDesc; |
| 403 | if (!getInductionDescriptor(SE, IndDesc)) |
| 404 | return false; |
| 405 | |
| 406 | ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue()); |
| 407 | if (!Init || !Init->isZero()) |
| 408 | return false; |
| 409 | |
| 410 | if (IndDesc.getInductionOpcode() != Instruction::Add) |
| 411 | return false; |
| 412 | |
| 413 | ConstantInt *Step = IndDesc.getConstIntStepValue(); |
| 414 | if (!Step || !Step->isOne()) |
| 415 | return false; |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 420 | // Check that 'BB' doesn't have any uses outside of the 'L' |
| 421 | static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB, |
| 422 | DominatorTree &DT) { |
| 423 | for (const Instruction &I : BB) { |
| 424 | // Tokens can't be used in PHI nodes and live-out tokens prevent loop |
| 425 | // optimizations, so for the purposes of considered LCSSA form, we |
| 426 | // can ignore them. |
| 427 | if (I.getType()->isTokenTy()) |
| 428 | continue; |
Andrew Kaylor | 123048d | 2015-12-18 18:12:35 +0000 | [diff] [blame] | 429 | |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 430 | for (const Use &U : I.uses()) { |
| 431 | const Instruction *UI = cast<Instruction>(U.getUser()); |
| 432 | const BasicBlock *UserBB = UI->getParent(); |
| 433 | if (const PHINode *P = dyn_cast<PHINode>(UI)) |
| 434 | UserBB = P->getIncomingBlock(U); |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 435 | |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 436 | // Check the current block, as a fast-path, before checking whether |
| 437 | // the use is anywhere in the loop. Most values are used in the same |
| 438 | // block they are defined in. Also, blocks not reachable from the |
| 439 | // entry are special; uses in them don't need to go through PHIs. |
| 440 | if (UserBB != &BB && !L.contains(UserBB) && |
| 441 | DT.isReachableFromEntry(UserBB)) |
| 442 | return false; |
Andrew Kaylor | 123048d | 2015-12-18 18:12:35 +0000 | [diff] [blame] | 443 | } |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 444 | } |
Dan Gohman | 80a9942 | 2009-07-13 22:02:44 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
Dan Gohman | 1511f70 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 447 | |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 448 | bool Loop::isLCSSAForm(DominatorTree &DT) const { |
| 449 | // For each block we check that it doesn't have any uses outside of this loop. |
| 450 | return all_of(this->blocks(), [&](const BasicBlock *BB) { |
| 451 | return isBlockInLCSSAForm(*this, *BB, DT); |
| 452 | }); |
| 453 | } |
Sanjoy Das | 683bf07 | 2015-12-08 00:13:21 +0000 | [diff] [blame] | 454 | |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 455 | bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const { |
Davide Italiano | 362cc7b | 2017-01-08 22:22:09 +0000 | [diff] [blame] | 456 | // For each block we check that it doesn't have any uses outside of its |
| 457 | // innermost loop. This process will transitively guarantee that the current |
| 458 | // loop and all of the nested loops are in LCSSA form. |
Igor Laevsky | 04423cf | 2016-10-11 13:37:22 +0000 | [diff] [blame] | 459 | return all_of(this->blocks(), [&](const BasicBlock *BB) { |
| 460 | return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT); |
| 461 | }); |
Sanjoy Das | 683bf07 | 2015-12-08 00:13:21 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Dan Gohman | 1511f70 | 2009-07-16 16:16:23 +0000 | [diff] [blame] | 464 | bool Loop::isLoopSimplifyForm() const { |
Dan Gohman | e3a1706 | 2009-11-05 19:21:41 +0000 | [diff] [blame] | 465 | // Normal-form loops have a preheader, a single backedge, and all of their |
| 466 | // exits have all their predecessors inside the loop. |
| 467 | return getLoopPreheader() && getLoopLatch() && hasDedicatedExits(); |
| 468 | } |
| 469 | |
Sanjay Patel | 784b5e3 | 2016-01-14 23:23:04 +0000 | [diff] [blame] | 470 | // 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] | 471 | bool Loop::isSafeToClone() const { |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 472 | // Return false if any loop blocks contain indirectbrs, or there are any calls |
| 473 | // to noduplicate functions. |
Nick Desaulniers | c4f245b | 2019-07-15 21:16:29 +0000 | [diff] [blame] | 474 | // FIXME: it should be ok to clone CallBrInst's if we correctly update the |
| 475 | // operand list to reflect the newly cloned labels. |
Sanjay Patel | 960e534 | 2016-01-15 00:08:10 +0000 | [diff] [blame] | 476 | for (BasicBlock *BB : this->blocks()) { |
Nick Desaulniers | c4f245b | 2019-07-15 21:16:29 +0000 | [diff] [blame] | 477 | if (isa<IndirectBrInst>(BB->getTerminator()) || |
| 478 | isa<CallBrInst>(BB->getTerminator())) |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 479 | return false; |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 480 | |
David Majnemer | 3d90bb7 | 2016-05-03 03:57:40 +0000 | [diff] [blame] | 481 | for (Instruction &I : *BB) |
| 482 | if (auto CS = CallSite(&I)) |
| 483 | if (CS.cannotDuplicate()) |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 484 | return false; |
Andrew Trick | 4442bfe | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 485 | } |
| 486 | return true; |
| 487 | } |
| 488 | |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 489 | MDNode *Loop::getLoopID() const { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 490 | MDNode *LoopID = nullptr; |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 491 | |
Michael Kruse | 534c87d | 2018-09-17 18:40:29 +0000 | [diff] [blame] | 492 | // Go through the latch blocks and check the terminator for the metadata. |
| 493 | SmallVector<BasicBlock *, 4> LatchesBlocks; |
| 494 | getLoopLatches(LatchesBlocks); |
| 495 | for (BasicBlock *BB : LatchesBlocks) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 496 | Instruction *TI = BB->getTerminator(); |
Michael Kruse | 534c87d | 2018-09-17 18:40:29 +0000 | [diff] [blame] | 497 | MDNode *MD = TI->getMetadata(LLVMContext::MD_loop); |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 498 | |
Michael Kruse | 534c87d | 2018-09-17 18:40:29 +0000 | [diff] [blame] | 499 | if (!MD) |
| 500 | return nullptr; |
| 501 | |
| 502 | if (!LoopID) |
| 503 | LoopID = MD; |
| 504 | else if (MD != LoopID) |
| 505 | return nullptr; |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 506 | } |
| 507 | if (!LoopID || LoopID->getNumOperands() == 0 || |
| 508 | LoopID->getOperand(0) != LoopID) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 509 | return nullptr; |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 510 | return LoopID; |
| 511 | } |
| 512 | |
| 513 | void Loop::setLoopID(MDNode *LoopID) const { |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 514 | assert((!LoopID || LoopID->getNumOperands() > 0) && |
| 515 | "Loop ID needs at least one operand"); |
| 516 | assert((!LoopID || LoopID->getOperand(0) == LoopID) && |
| 517 | "Loop ID should refer to itself"); |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 518 | |
Keno Fischer | d8f856d | 2019-05-01 14:39:11 +0000 | [diff] [blame] | 519 | SmallVector<BasicBlock *, 4> LoopLatches; |
| 520 | getLoopLatches(LoopLatches); |
| 521 | for (BasicBlock *BB : LoopLatches) |
| 522 | BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); |
Paul Redmond | 5fdf836 | 2013-05-28 20:00:34 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Hongbin Zheng | 73f6504 | 2017-10-15 07:31:02 +0000 | [diff] [blame] | 525 | void Loop::setLoopAlreadyUnrolled() { |
Hongbin Zheng | 73f6504 | 2017-10-15 07:31:02 +0000 | [diff] [blame] | 526 | LLVMContext &Context = getHeader()->getContext(); |
Hongbin Zheng | 73f6504 | 2017-10-15 07:31:02 +0000 | [diff] [blame] | 527 | |
Michael Kruse | 77a614a | 2019-02-11 19:45:44 +0000 | [diff] [blame] | 528 | MDNode *DisableUnrollMD = |
| 529 | MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable")); |
| 530 | MDNode *LoopID = getLoopID(); |
| 531 | MDNode *NewLoopID = makePostTransformationMetadata( |
| 532 | Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD}); |
Hongbin Zheng | 73f6504 | 2017-10-15 07:31:02 +0000 | [diff] [blame] | 533 | setLoopID(NewLoopID); |
| 534 | } |
| 535 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 536 | bool Loop::isAnnotatedParallel() const { |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 537 | MDNode *DesiredLoopIdMetadata = getLoopID(); |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 538 | |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 539 | if (!DesiredLoopIdMetadata) |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 540 | return false; |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 541 | |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 542 | MDNode *ParallelAccesses = |
| 543 | findOptionMDForLoop(this, "llvm.loop.parallel_accesses"); |
| 544 | SmallPtrSet<MDNode *, 4> |
| 545 | ParallelAccessGroups; // For scalable 'contains' check. |
| 546 | if (ParallelAccesses) { |
| 547 | for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) { |
| 548 | MDNode *AccGroup = cast<MDNode>(MD.get()); |
| 549 | assert(isValidAsAccessGroup(AccGroup) && |
| 550 | "List item must be an access group"); |
| 551 | ParallelAccessGroups.insert(AccGroup); |
| 552 | } |
| 553 | } |
| 554 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 555 | // The loop branch contains the parallel loop metadata. In order to ensure |
| 556 | // that any parallel-loop-unaware optimization pass hasn't added loop-carried |
| 557 | // dependencies (thus converted the loop back to a sequential loop), check |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 558 | // that all the memory instructions in the loop belong to an access group that |
| 559 | // is parallel to this loop. |
Sanjay Patel | 960e534 | 2016-01-15 00:08:10 +0000 | [diff] [blame] | 560 | for (BasicBlock *BB : this->blocks()) { |
| 561 | for (Instruction &I : *BB) { |
| 562 | if (!I.mayReadOrWriteMemory()) |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 563 | continue; |
| 564 | |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 565 | if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) { |
| 566 | auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool { |
| 567 | if (AG->getNumOperands() == 0) { |
| 568 | assert(isValidAsAccessGroup(AG) && "Item must be an access group"); |
| 569 | return ParallelAccessGroups.count(AG); |
| 570 | } |
| 571 | |
| 572 | for (const MDOperand &AccessListItem : AG->operands()) { |
| 573 | MDNode *AccGroup = cast<MDNode>(AccessListItem.get()); |
| 574 | assert(isValidAsAccessGroup(AccGroup) && |
| 575 | "List item must be an access group"); |
| 576 | if (ParallelAccessGroups.count(AccGroup)) |
| 577 | return true; |
| 578 | } |
| 579 | return false; |
| 580 | }; |
| 581 | |
| 582 | if (ContainsAccessGroup(AccessGroup)) |
| 583 | continue; |
| 584 | } |
| 585 | |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 586 | // The memory instruction can refer to the loop identifier metadata |
| 587 | // directly or indirectly through another list metadata (in case of |
| 588 | // nested parallel loops). The loop identifier metadata refers to |
| 589 | // itself so we can check both cases with the same routine. |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 590 | MDNode *LoopIdMD = |
Sanjay Patel | 960e534 | 2016-01-15 00:08:10 +0000 | [diff] [blame] | 591 | I.getMetadata(LLVMContext::MD_mem_parallel_loop_access); |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 592 | |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 593 | if (!LoopIdMD) |
Jakub Staszak | 9dca4b3 | 2013-11-13 20:18:38 +0000 | [diff] [blame] | 594 | return false; |
| 595 | |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 596 | bool LoopIdMDFound = false; |
| 597 | for (const MDOperand &MDOp : LoopIdMD->operands()) { |
| 598 | if (MDOp == DesiredLoopIdMetadata) { |
| 599 | LoopIdMDFound = true; |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
Sanjay Patel | 6435c6e | 2016-01-17 23:18:05 +0000 | [diff] [blame] | 604 | if (!LoopIdMDFound) |
Pekka Jaaskelainen | 0d23725 | 2013-02-13 18:08:57 +0000 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | } |
| 608 | return true; |
| 609 | } |
| 610 | |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 611 | DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); } |
Amara Emerson | 0b40201 | 2016-11-08 11:18:59 +0000 | [diff] [blame] | 612 | |
| 613 | Loop::LocRange Loop::getLocRange() const { |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 614 | // 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] | 615 | if (MDNode *LoopID = getLoopID()) { |
| 616 | DebugLoc Start; |
| 617 | // We use the first DebugLoc in the header as the start location of the loop |
| 618 | // and if there is a second DebugLoc in the header we use it as end location |
| 619 | // of the loop. |
| 620 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 621 | if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) { |
| 622 | if (!Start) |
| 623 | Start = DebugLoc(L); |
| 624 | else |
| 625 | return LocRange(Start, DebugLoc(L)); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | if (Start) |
| 630 | return LocRange(Start); |
| 631 | } |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 632 | |
| 633 | // Try the pre-header first. |
| 634 | if (BasicBlock *PHeadBB = getLoopPreheader()) |
| 635 | if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc()) |
Amara Emerson | 0b40201 | 2016-11-08 11:18:59 +0000 | [diff] [blame] | 636 | return LocRange(DL); |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 637 | |
| 638 | // If we have no pre-header or there are no instructions with debug |
| 639 | // info in it, try the header. |
| 640 | if (BasicBlock *HeadBB = getHeader()) |
Amara Emerson | 0b40201 | 2016-11-08 11:18:59 +0000 | [diff] [blame] | 641 | return LocRange(HeadBB->getTerminator()->getDebugLoc()); |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 642 | |
Amara Emerson | 0b40201 | 2016-11-08 11:18:59 +0000 | [diff] [blame] | 643 | return LocRange(); |
Hal Finkel | 2f68868 | 2016-05-25 21:42:37 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 646 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 647 | LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); } |
Sebastian Pop | 18c964d | 2016-07-27 05:02:17 +0000 | [diff] [blame] | 648 | |
| 649 | LLVM_DUMP_METHOD void Loop::dumpVerbose() const { |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 650 | print(dbgs(), /*Depth=*/0, /*Verbose=*/true); |
Sebastian Pop | 18c964d | 2016-07-27 05:02:17 +0000 | [diff] [blame] | 651 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 652 | #endif |
Dan Gohman | c3f2137 | 2010-01-05 21:08:02 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 654 | //===----------------------------------------------------------------------===// |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 655 | // UnloopUpdater implementation |
| 656 | // |
| 657 | |
Benjamin Kramer | 4938edb | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 658 | namespace { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 659 | /// Find the new parent loop for all blocks within the "unloop" whose last |
| 660 | /// backedges has just been removed. |
| 661 | class UnloopUpdater { |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 662 | Loop &Unloop; |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 663 | LoopInfo *LI; |
| 664 | |
| 665 | LoopBlocksDFS DFS; |
| 666 | |
| 667 | // Map unloop's immediate subloops to their nearest reachable parents. Nested |
| 668 | // loops within these subloops will not change parents. However, an immediate |
| 669 | // subloop's new parent will be the nearest loop reachable from either its own |
| 670 | // exits *or* any of its nested loop's exits. |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 671 | DenseMap<Loop *, Loop *> SubloopParents; |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 672 | |
| 673 | // Flag the presence of an irreducible backedge whose destination is a block |
| 674 | // directly contained by the original unloop. |
| 675 | bool FoundIB; |
| 676 | |
| 677 | public: |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 678 | UnloopUpdater(Loop *UL, LoopInfo *LInfo) |
| 679 | : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {} |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 680 | |
| 681 | void updateBlockParents(); |
| 682 | |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 683 | void removeBlocksFromAncestors(); |
| 684 | |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 685 | void updateSubloopParents(); |
| 686 | |
| 687 | protected: |
| 688 | Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop); |
| 689 | }; |
Benjamin Kramer | 4938edb | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 690 | } // end anonymous namespace |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 691 | |
Sanjay Patel | 784b5e3 | 2016-01-14 23:23:04 +0000 | [diff] [blame] | 692 | /// Update the parent loop for all blocks that are directly contained within the |
| 693 | /// original "unloop". |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 694 | void UnloopUpdater::updateBlockParents() { |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 695 | if (Unloop.getNumBlocks()) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 696 | // Perform a post order CFG traversal of all blocks within this loop, |
Hiroshi Inoue | 713b5ba | 2017-07-09 05:54:44 +0000 | [diff] [blame] | 697 | // propagating the nearest loop from successors to predecessors. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 698 | LoopBlocksTraversal Traversal(DFS, LI); |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 699 | for (BasicBlock *POI : Traversal) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 700 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 701 | Loop *L = LI->getLoopFor(POI); |
| 702 | Loop *NL = getNearestLoop(POI, L); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 703 | |
| 704 | if (NL != L) { |
| 705 | // For reducible loops, NL is now an ancestor of Unloop. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 706 | assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) && |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 707 | "uninitialized successor"); |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 708 | LI->changeLoopFor(POI, NL); |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 709 | } else { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 710 | // Or the current block is part of a subloop, in which case its parent |
| 711 | // is unchanged. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 712 | assert((FoundIB || Unloop.contains(L)) && "uninitialized successor"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | } |
| 716 | // Each irreducible loop within the unloop induces a round of iteration using |
| 717 | // the DFS result cached by Traversal. |
| 718 | bool Changed = FoundIB; |
| 719 | for (unsigned NIters = 0; Changed; ++NIters) { |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 720 | assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 721 | |
| 722 | // Iterate over the postorder list of blocks, propagating the nearest loop |
| 723 | // from successors to predecessors as before. |
| 724 | Changed = false; |
| 725 | for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(), |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 726 | POE = DFS.endPostorder(); |
| 727 | POI != POE; ++POI) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 728 | |
| 729 | Loop *L = LI->getLoopFor(*POI); |
| 730 | Loop *NL = getNearestLoop(*POI, L); |
| 731 | if (NL != L) { |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 732 | assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) && |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 733 | "uninitialized successor"); |
| 734 | LI->changeLoopFor(*POI, NL); |
| 735 | Changed = true; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
Sanjay Patel | 784b5e3 | 2016-01-14 23:23:04 +0000 | [diff] [blame] | 741 | /// Remove unloop's blocks from all ancestors below their new parents. |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 742 | void UnloopUpdater::removeBlocksFromAncestors() { |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 743 | // Remove all unloop's blocks (including those in nested subloops) from |
| 744 | // ancestors below the new parent loop. |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 745 | for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end(); |
| 746 | BI != BE; ++BI) { |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 747 | Loop *OuterParent = LI->getLoopFor(*BI); |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 748 | if (Unloop.contains(OuterParent)) { |
| 749 | while (OuterParent->getParentLoop() != &Unloop) |
Andrew Trick | 6b4d578 | 2011-11-18 03:42:41 +0000 | [diff] [blame] | 750 | OuterParent = OuterParent->getParentLoop(); |
| 751 | OuterParent = SubloopParents[OuterParent]; |
| 752 | } |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 753 | // Remove blocks from former Ancestors except Unloop itself which will be |
| 754 | // deleted. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 755 | for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent; |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 756 | OldParent = OldParent->getParentLoop()) { |
| 757 | assert(OldParent && "new loop is not an ancestor of the original"); |
| 758 | OldParent->removeBlockFromLoop(*BI); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Sanjay Patel | 784b5e3 | 2016-01-14 23:23:04 +0000 | [diff] [blame] | 763 | /// Update the parent loop for all subloops directly nested within unloop. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 764 | void UnloopUpdater::updateSubloopParents() { |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 765 | while (!Unloop.empty()) { |
| 766 | Loop *Subloop = *std::prev(Unloop.end()); |
| 767 | Unloop.removeChildLoop(std::prev(Unloop.end())); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 768 | |
| 769 | assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop"); |
Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 770 | if (Loop *Parent = SubloopParents[Subloop]) |
| 771 | Parent->addChildLoop(Subloop); |
Andrew Trick | 147d9cd | 2011-08-26 03:06:34 +0000 | [diff] [blame] | 772 | else |
| 773 | LI->addTopLevelLoop(Subloop); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Sanjay Patel | 784b5e3 | 2016-01-14 23:23:04 +0000 | [diff] [blame] | 777 | /// Return the nearest parent loop among this block's successors. If a successor |
| 778 | /// is a subloop header, consider its parent to be the nearest parent of the |
| 779 | /// subloop's exits. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 780 | /// |
| 781 | /// For subloop blocks, simply update SubloopParents and return NULL. |
| 782 | Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) { |
| 783 | |
Andrew Trick | 266ab10 | 2011-08-11 17:54:58 +0000 | [diff] [blame] | 784 | // Initially for blocks directly contained by Unloop, NearLoop == Unloop and |
| 785 | // is considered uninitialized. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 786 | Loop *NearLoop = BBLoop; |
| 787 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 788 | Loop *Subloop = nullptr; |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 789 | if (NearLoop != &Unloop && Unloop.contains(NearLoop)) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 790 | Subloop = NearLoop; |
| 791 | // Find the subloop ancestor that is directly contained within Unloop. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 792 | while (Subloop->getParentLoop() != &Unloop) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 793 | Subloop = Subloop->getParentLoop(); |
| 794 | assert(Subloop && "subloop is not an ancestor of the original loop"); |
| 795 | } |
| 796 | // Get the current nearest parent of the Subloop exits, initially Unloop. |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 797 | NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second; |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | succ_iterator I = succ_begin(BB), E = succ_end(BB); |
| 801 | if (I == E) { |
| 802 | assert(!Subloop && "subloop blocks must have a successor"); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 803 | NearLoop = nullptr; // unloop blocks may now exit the function. |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 804 | } |
| 805 | for (; I != E; ++I) { |
| 806 | if (*I == BB) |
| 807 | continue; // self loops are uninteresting |
| 808 | |
| 809 | Loop *L = LI->getLoopFor(*I); |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 810 | if (L == &Unloop) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 811 | // This successor has not been processed. This path must lead to an |
| 812 | // irreducible backedge. |
| 813 | assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB"); |
| 814 | FoundIB = true; |
| 815 | } |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 816 | if (L != &Unloop && Unloop.contains(L)) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 817 | // Successor is in a subloop. |
| 818 | if (Subloop) |
| 819 | continue; // Branching within subloops. Ignore it. |
| 820 | |
| 821 | // BB branches from the original into a subloop header. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 822 | assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 823 | |
| 824 | // Get the current nearest parent of the Subloop's exits. |
| 825 | L = SubloopParents[L]; |
| 826 | // L could be Unloop if the only exit was an irreducible backedge. |
| 827 | } |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 828 | if (L == &Unloop) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 829 | continue; |
| 830 | } |
| 831 | // Handle critical edges from Unloop into a sibling loop. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 832 | if (L && !L->contains(&Unloop)) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 833 | L = L->getParentLoop(); |
| 834 | } |
| 835 | // Remember the nearest parent loop among successors or subloop exits. |
Silviu Baranga | 24dbd2e | 2016-05-13 14:54:50 +0000 | [diff] [blame] | 836 | if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L)) |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 837 | NearLoop = L; |
| 838 | } |
| 839 | if (Subloop) { |
| 840 | SubloopParents[Subloop] = NearLoop; |
| 841 | return BBLoop; |
| 842 | } |
| 843 | return NearLoop; |
| 844 | } |
| 845 | |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 846 | LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); } |
Cong Hou | 9b4f6b2 | 2015-07-16 23:23:35 +0000 | [diff] [blame] | 847 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 848 | bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 849 | FunctionAnalysisManager::Invalidator &) { |
| 850 | // Check whether the analysis, all analyses on functions, or the function's |
| 851 | // CFG have been preserved. |
| 852 | auto PAC = PA.getChecker<LoopAnalysis>(); |
| 853 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || |
| 854 | PAC.preservedSet<CFGAnalyses>()); |
| 855 | } |
| 856 | |
Sanjoy Das | 388b012 | 2017-09-22 01:47:41 +0000 | [diff] [blame] | 857 | void LoopInfo::erase(Loop *Unloop) { |
Sanjoy Das | 76ab232 | 2017-09-19 23:19:00 +0000 | [diff] [blame] | 858 | assert(!Unloop->isInvalid() && "Loop has already been erased!"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 859 | |
Sanjoy Das | def1729 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 860 | auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); }); |
Sanjoy Das | 09613b1 | 2017-09-20 02:31:57 +0000 | [diff] [blame] | 861 | |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 862 | // First handle the special case of no parent loop to simplify the algorithm. |
| 863 | if (!Unloop->getParentLoop()) { |
| 864 | // Since BBLoop had no parent, Unloop blocks are no longer in a loop. |
| 865 | for (Loop::block_iterator I = Unloop->block_begin(), |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 866 | E = Unloop->block_end(); |
| 867 | I != E; ++I) { |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 868 | |
| 869 | // Don't reparent blocks in subloops. |
| 870 | if (getLoopFor(*I) != Unloop) |
| 871 | continue; |
| 872 | |
| 873 | // Blocks no longer have a parent but are still referenced by Unloop until |
| 874 | // the Unloop object is deleted. |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 875 | changeLoopFor(*I, nullptr); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | // Remove the loop from the top-level LoopInfo object. |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 879 | for (iterator I = begin();; ++I) { |
| 880 | assert(I != end() && "Couldn't find loop"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 881 | if (*I == Unloop) { |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 882 | removeLoop(I); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 883 | break; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // Move all of the subloops to the top-level. |
| 888 | while (!Unloop->empty()) |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 889 | addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end()))); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 890 | |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | // Update the parent loop for all blocks within the loop. Blocks within |
| 895 | // subloops will not change parents. |
| 896 | UnloopUpdater Updater(Unloop, this); |
| 897 | Updater.updateBlockParents(); |
| 898 | |
Andrew Trick | c12c30a | 2011-08-11 20:27:32 +0000 | [diff] [blame] | 899 | // Remove blocks from former ancestor loops. |
| 900 | Updater.removeBlocksFromAncestors(); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 901 | |
| 902 | // Add direct subloops as children in their new parent loop. |
| 903 | Updater.updateSubloopParents(); |
| 904 | |
| 905 | // Remove unloop from its parent loop. |
| 906 | Loop *ParentLoop = Unloop->getParentLoop(); |
Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 907 | for (Loop::iterator I = ParentLoop->begin();; ++I) { |
| 908 | assert(I != ParentLoop->end() && "Couldn't find loop"); |
Andrew Trick | d3530b9 | 2011-08-10 23:22:57 +0000 | [diff] [blame] | 909 | if (*I == Unloop) { |
| 910 | ParentLoop->removeChildLoop(I); |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 916 | AnalysisKey LoopAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 917 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 918 | LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 919 | // FIXME: Currently we create a LoopInfo from scratch for every function. |
| 920 | // This may prove to be too wasteful due to deallocating and re-allocating |
| 921 | // memory each time for the underlying map and vector datastructures. At some |
| 922 | // point it may prove worthwhile to use a freelist and recycle LoopInfo |
| 923 | // objects. I don't want to add that kind of complexity until the scope of |
| 924 | // the problem is better understood. |
| 925 | LoopInfo LI; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 926 | LI.analyze(AM.getResult<DominatorTreeAnalysis>(F)); |
Richard Trieu | 6ae3796 | 2015-04-30 23:07:00 +0000 | [diff] [blame] | 927 | return LI; |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | PreservedAnalyses LoopPrinterPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 931 | FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 932 | AM.getResult<LoopAnalysis>(F).print(OS); |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 933 | return PreservedAnalyses::all(); |
| 934 | } |
| 935 | |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 936 | void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) { |
Fedor Sergeev | 3b459c3 | 2017-12-01 18:33:58 +0000 | [diff] [blame] | 937 | |
| 938 | if (forcePrintModuleIR()) { |
| 939 | // handling -print-module-scope |
| 940 | OS << Banner << " (loop: "; |
| 941 | L.getHeader()->printAsOperand(OS, false); |
| 942 | OS << ")\n"; |
| 943 | |
| 944 | // printing whole module |
| 945 | OS << *L.getHeader()->getModule(); |
| 946 | return; |
| 947 | } |
| 948 | |
Justin Bogner | c2b98f0 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 949 | OS << Banner; |
Fedor Sergeev | 61975b4 | 2017-11-22 20:59:53 +0000 | [diff] [blame] | 950 | |
| 951 | auto *PreHeader = L.getLoopPreheader(); |
| 952 | if (PreHeader) { |
| 953 | OS << "\n; Preheader:"; |
| 954 | PreHeader->print(OS); |
| 955 | OS << "\n; Loop:"; |
| 956 | } |
| 957 | |
Justin Bogner | c2b98f0 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 958 | for (auto *Block : L.blocks()) |
| 959 | if (Block) |
| 960 | Block->print(OS); |
| 961 | else |
| 962 | OS << "Printing <null> block"; |
Fedor Sergeev | 61975b4 | 2017-11-22 20:59:53 +0000 | [diff] [blame] | 963 | |
| 964 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 965 | L.getExitBlocks(ExitBlocks); |
| 966 | if (!ExitBlocks.empty()) { |
| 967 | OS << "\n; Exit blocks"; |
| 968 | for (auto *Block : ExitBlocks) |
| 969 | if (Block) |
| 970 | Block->print(OS); |
| 971 | else |
| 972 | OS << "Printing <null> block"; |
| 973 | } |
Justin Bogner | c2b98f0 | 2015-11-04 22:24:08 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 976 | MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) { |
| 977 | // No loop metadata node, no loop properties. |
| 978 | if (!LoopID) |
| 979 | return nullptr; |
| 980 | |
| 981 | // First operand should refer to the metadata node itself, for legacy reasons. |
| 982 | assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); |
| 983 | assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); |
| 984 | |
| 985 | // Iterate over the metdata node operands and look for MDString metadata. |
| 986 | for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { |
| 987 | MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); |
| 988 | if (!MD || MD->getNumOperands() < 1) |
| 989 | continue; |
| 990 | MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 991 | if (!S) |
| 992 | continue; |
| 993 | // Return the operand node if MDString holds expected metadata. |
| 994 | if (Name.equals(S->getString())) |
| 995 | return MD; |
| 996 | } |
| 997 | |
| 998 | // Loop property not found. |
| 999 | return nullptr; |
| 1000 | } |
| 1001 | |
| 1002 | MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) { |
| 1003 | return findOptionMDForLoopID(TheLoop->getLoopID(), Name); |
| 1004 | } |
| 1005 | |
| 1006 | bool llvm::isValidAsAccessGroup(MDNode *Node) { |
| 1007 | return Node->getNumOperands() == 0 && Node->isDistinct(); |
| 1008 | } |
| 1009 | |
Michael Kruse | 77a614a | 2019-02-11 19:45:44 +0000 | [diff] [blame] | 1010 | MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context, |
| 1011 | MDNode *OrigLoopID, |
| 1012 | ArrayRef<StringRef> RemovePrefixes, |
| 1013 | ArrayRef<MDNode *> AddAttrs) { |
| 1014 | // First remove any existing loop metadata related to this transformation. |
| 1015 | SmallVector<Metadata *, 4> MDs; |
| 1016 | |
| 1017 | // Reserve first location for self reference to the LoopID metadata node. |
| 1018 | TempMDTuple TempNode = MDNode::getTemporary(Context, None); |
| 1019 | MDs.push_back(TempNode.get()); |
| 1020 | |
| 1021 | // Remove metadata for the transformation that has been applied or that became |
| 1022 | // outdated. |
| 1023 | if (OrigLoopID) { |
| 1024 | for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) { |
| 1025 | bool IsVectorMetadata = false; |
| 1026 | Metadata *Op = OrigLoopID->getOperand(i); |
| 1027 | if (MDNode *MD = dyn_cast<MDNode>(Op)) { |
| 1028 | const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); |
| 1029 | if (S) |
| 1030 | IsVectorMetadata = |
| 1031 | llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool { |
| 1032 | return S->getString().startswith(Prefix); |
| 1033 | }); |
| 1034 | } |
| 1035 | if (!IsVectorMetadata) |
| 1036 | MDs.push_back(Op); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // Add metadata to avoid reapplying a transformation, such as |
| 1041 | // llvm.loop.unroll.disable and llvm.loop.isvectorized. |
| 1042 | MDs.append(AddAttrs.begin(), AddAttrs.end()); |
| 1043 | |
| 1044 | MDNode *NewLoopID = MDNode::getDistinct(Context, MDs); |
| 1045 | // Replace the temporary node with a self-reference. |
| 1046 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 1047 | return NewLoopID; |
| 1048 | } |
| 1049 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1050 | //===----------------------------------------------------------------------===// |
| 1051 | // LoopInfo implementation |
| 1052 | // |
| 1053 | |
| 1054 | char LoopInfoWrapperPass::ID = 0; |
| 1055 | INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information", |
| 1056 | true, true) |
| 1057 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1058 | INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information", |
| 1059 | true, true) |
| 1060 | |
| 1061 | bool LoopInfoWrapperPass::runOnFunction(Function &) { |
| 1062 | releaseMemory(); |
Cong Hou | d2c1d91 | 2015-07-16 18:23:57 +0000 | [diff] [blame] | 1063 | LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree()); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | void LoopInfoWrapperPass::verifyAnalysis() const { |
| 1068 | // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the |
| 1069 | // function each time verifyAnalysis is called is very expensive. The |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 1070 | // -verify-loop-info option can enable this. In order to perform some |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1071 | // checking by default, LoopPass has been taught to call verifyLoop manually |
| 1072 | // during loop pass sequences. |
Michael Zolotukhin | e0b2d97 | 2016-08-31 19:26:19 +0000 | [diff] [blame] | 1073 | if (VerifyLoopInfo) { |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 1074 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1075 | LI.verify(DT); |
Michael Zolotukhin | e0b2d97 | 2016-08-31 19:26:19 +0000 | [diff] [blame] | 1076 | } |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1079 | void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 1080 | AU.setPreservesAll(); |
Evgeniy Stepanov | 6abd78c | 2019-07-17 23:31:59 +0000 | [diff] [blame] | 1081 | AU.addRequiredTransitive<DominatorTreeWrapperPass>(); |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 1082 | } |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 1083 | |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1084 | void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 1085 | LI.print(OS); |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Sean Silva | e3c18a5 | 2016-07-19 23:54:23 +0000 | [diff] [blame] | 1088 | PreservedAnalyses LoopVerifierPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 1089 | FunctionAnalysisManager &AM) { |
Sean Silva | e3c18a5 | 2016-07-19 23:54:23 +0000 | [diff] [blame] | 1090 | LoopInfo &LI = AM.getResult<LoopAnalysis>(F); |
Michael Zolotukhin | e0b2d97 | 2016-08-31 19:26:19 +0000 | [diff] [blame] | 1091 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 1092 | LI.verify(DT); |
Sean Silva | e3c18a5 | 2016-07-19 23:54:23 +0000 | [diff] [blame] | 1093 | return PreservedAnalyses::all(); |
| 1094 | } |
| 1095 | |
Andrew Trick | 78b40c3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 1096 | //===----------------------------------------------------------------------===// |
| 1097 | // LoopBlocksDFS implementation |
| 1098 | // |
| 1099 | |
| 1100 | /// Traverse the loop blocks and store the DFS result. |
| 1101 | /// Useful for clients that just want the final DFS result and don't need to |
| 1102 | /// visit blocks during the initial traversal. |
| 1103 | void LoopBlocksDFS::perform(LoopInfo *LI) { |
| 1104 | LoopBlocksTraversal Traversal(*this, LI); |
| 1105 | for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(), |
Sanjoy Das | 66a004a | 2017-09-20 01:12:09 +0000 | [diff] [blame] | 1106 | POE = Traversal.end(); |
| 1107 | POI != POE; ++POI) |
| 1108 | ; |
Andrew Trick | 78b40c3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 1109 | } |