Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 1 | //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===// |
| 2 | // |
| 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. |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Loop Rotation Pass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/CodeMetrics.h" |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 28 | #include "llvm/Transforms/Utils/Local.h" |
| 29 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 30 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "loop-rotate" |
| 34 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 35 | #define MAX_HEADER_SIZE 16 |
| 36 | |
| 37 | STATISTIC(NumRotated, "Number of loops rotated"); |
| 38 | namespace { |
| 39 | |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 40 | class LoopRotate : public LoopPass { |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 41 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 42 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 43 | LoopRotate() : LoopPass(ID) { |
| 44 | initializeLoopRotatePass(*PassRegistry::getPassRegistry()); |
| 45 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 46 | |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 47 | // LCSSA form makes instruction renaming easier. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 48 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 49 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Dan Gohman | efd7f9c | 2010-07-16 17:58:45 +0000 | [diff] [blame] | 50 | AU.addRequired<LoopInfo>(); |
| 51 | AU.addPreserved<LoopInfo>(); |
Devang Patel | a42c314 | 2008-02-15 01:24:49 +0000 | [diff] [blame] | 52 | AU.addRequiredID(LoopSimplifyID); |
| 53 | AU.addPreservedID(LoopSimplifyID); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 54 | AU.addRequiredID(LCSSAID); |
| 55 | AU.addPreservedID(LCSSAID); |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 56 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 57 | AU.addRequired<TargetTransformInfo>(); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 60 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 61 | bool simplifyLoopLatch(Loop *L); |
| 62 | bool rotateLoop(Loop *L, bool SimplifiedLatch); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 63 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 64 | private: |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 65 | LoopInfo *LI; |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 66 | const TargetTransformInfo *TTI; |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 67 | }; |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 68 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 69 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 70 | char LoopRotate::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 71 | INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 72 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 74 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 75 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 76 | INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | 7f39e2d | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 78 | Pass *llvm::createLoopRotatePass() { return new LoopRotate(); } |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 79 | |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 80 | /// Rotate Loop L as many times as possible. Return true if |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 81 | /// the loop is rotated at least once. |
Chris Lattner | 385f2ec | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 82 | bool LoopRotate::runOnLoop(Loop *L, LPPassManager &LPM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 83 | if (skipOptnoneFunction(L)) |
| 84 | return false; |
| 85 | |
Alexey Bataev | b97f9e8 | 2014-04-15 09:37:30 +0000 | [diff] [blame] | 86 | // Save the loop metadata. |
| 87 | MDNode *LoopMD = L->getLoopID(); |
| 88 | |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 89 | LI = &getAnalysis<LoopInfo>(); |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 90 | TTI = &getAnalysis<TargetTransformInfo>(); |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 91 | |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 92 | // Simplify the loop latch before attempting to rotate the header |
| 93 | // upward. Rotation may not be needed if the loop tail can be folded into the |
| 94 | // loop exit. |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 95 | bool SimplifiedLatch = simplifyLoopLatch(L); |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 96 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 97 | // One loop can be rotated multiple times. |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 98 | bool MadeChange = false; |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 99 | while (rotateLoop(L, SimplifiedLatch)) { |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 100 | MadeChange = true; |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 101 | SimplifiedLatch = false; |
| 102 | } |
Alexey Bataev | b97f9e8 | 2014-04-15 09:37:30 +0000 | [diff] [blame] | 103 | |
| 104 | // Restore the loop metadata. |
| 105 | // NB! We presume LoopRotation DOESN'T ADD its own metadata. |
| 106 | if ((MadeChange || SimplifiedLatch) && LoopMD) |
| 107 | L->setLoopID(LoopMD); |
| 108 | |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 109 | return MadeChange; |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 112 | /// RewriteUsesOfClonedInstructions - We just cloned the instructions from the |
| 113 | /// old header into the preheader. If there were uses of the values produced by |
| 114 | /// these instruction that were outside of the loop, we have to insert PHI nodes |
| 115 | /// to merge the two values. Do this now. |
| 116 | static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader, |
| 117 | BasicBlock *OrigPreheader, |
| 118 | ValueToValueMapTy &ValueMap) { |
| 119 | // Remove PHI node entries that are no longer live. |
| 120 | BasicBlock::iterator I, E = OrigHeader->end(); |
| 121 | for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 122 | PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreheader)); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 124 | // Now fix up users of the instructions in OrigHeader, inserting PHI nodes |
| 125 | // as necessary. |
| 126 | SSAUpdater SSA; |
| 127 | for (I = OrigHeader->begin(); I != E; ++I) { |
| 128 | Value *OrigHeaderVal = I; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 130 | // If there are no uses of the value (e.g. because it returns void), there |
| 131 | // is nothing to rewrite. |
| 132 | if (OrigHeaderVal->use_empty()) |
| 133 | continue; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 135 | Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal]; |
| 136 | |
| 137 | // The value now exits in two versions: the initial value in the preheader |
| 138 | // and the loop "next" value in the original header. |
| 139 | SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName()); |
| 140 | SSA.AddAvailableValue(OrigHeader, OrigHeaderVal); |
| 141 | SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 143 | // Visit each use of the OrigHeader instruction. |
| 144 | for (Value::use_iterator UI = OrigHeaderVal->use_begin(), |
| 145 | UE = OrigHeaderVal->use_end(); UI != UE; ) { |
| 146 | // Grab the use before incrementing the iterator. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 147 | Use &U = *UI; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 149 | // Increment the iterator before removing the use from the list. |
| 150 | ++UI; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 152 | // SSAUpdater can't handle a non-PHI use in the same block as an |
| 153 | // earlier def. We can easily handle those cases manually. |
| 154 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 155 | if (!isa<PHINode>(UserInst)) { |
| 156 | BasicBlock *UserBB = UserInst->getParent(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 158 | // The original users in the OrigHeader are already using the |
| 159 | // original definitions. |
| 160 | if (UserBB == OrigHeader) |
| 161 | continue; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 163 | // Users in the OrigPreHeader need to use the value to which the |
| 164 | // original definitions are mapped. |
| 165 | if (UserBB == OrigPreheader) { |
| 166 | U = OrigPreHeaderVal; |
| 167 | continue; |
| 168 | } |
| 169 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 171 | // Anything else can be handled by SSAUpdater. |
| 172 | SSA.RewriteUse(U); |
| 173 | } |
| 174 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 176 | |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 177 | /// Determine whether the instructions in this range my be safely and cheaply |
| 178 | /// speculated. This is not an important enough situation to develop complex |
| 179 | /// heuristics. We handle a single arithmetic instruction along with any type |
| 180 | /// conversions. |
| 181 | static bool shouldSpeculateInstrs(BasicBlock::iterator Begin, |
| 182 | BasicBlock::iterator End) { |
| 183 | bool seenIncrement = false; |
| 184 | for (BasicBlock::iterator I = Begin; I != End; ++I) { |
| 185 | |
| 186 | if (!isSafeToSpeculativelyExecute(I)) |
| 187 | return false; |
| 188 | |
| 189 | if (isa<DbgInfoIntrinsic>(I)) |
| 190 | continue; |
| 191 | |
| 192 | switch (I->getOpcode()) { |
| 193 | default: |
| 194 | return false; |
| 195 | case Instruction::GetElementPtr: |
| 196 | // GEPs are cheap if all indices are constant. |
| 197 | if (!cast<GEPOperator>(I)->hasAllConstantIndices()) |
| 198 | return false; |
| 199 | // fall-thru to increment case |
| 200 | case Instruction::Add: |
| 201 | case Instruction::Sub: |
| 202 | case Instruction::And: |
| 203 | case Instruction::Or: |
| 204 | case Instruction::Xor: |
| 205 | case Instruction::Shl: |
| 206 | case Instruction::LShr: |
| 207 | case Instruction::AShr: |
| 208 | if (seenIncrement) |
| 209 | return false; |
| 210 | seenIncrement = true; |
| 211 | break; |
| 212 | case Instruction::Trunc: |
| 213 | case Instruction::ZExt: |
| 214 | case Instruction::SExt: |
| 215 | // ignore type conversions |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /// Fold the loop tail into the loop exit by speculating the loop tail |
| 223 | /// instructions. Typically, this is a single post-increment. In the case of a |
| 224 | /// simple 2-block loop, hoisting the increment can be much better than |
| 225 | /// duplicating the entire loop header. In the cast of loops with early exits, |
| 226 | /// rotation will not work anyway, but simplifyLoopLatch will put the loop in |
| 227 | /// canonical form so downstream passes can handle it. |
| 228 | /// |
| 229 | /// I don't believe this invalidates SCEV. |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 230 | bool LoopRotate::simplifyLoopLatch(Loop *L) { |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 231 | BasicBlock *Latch = L->getLoopLatch(); |
| 232 | if (!Latch || Latch->hasAddressTaken()) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 233 | return false; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 234 | |
| 235 | BranchInst *Jmp = dyn_cast<BranchInst>(Latch->getTerminator()); |
| 236 | if (!Jmp || !Jmp->isUnconditional()) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 237 | return false; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 238 | |
| 239 | BasicBlock *LastExit = Latch->getSinglePredecessor(); |
| 240 | if (!LastExit || !L->isLoopExiting(LastExit)) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 241 | return false; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 242 | |
| 243 | BranchInst *BI = dyn_cast<BranchInst>(LastExit->getTerminator()); |
| 244 | if (!BI) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 245 | return false; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 246 | |
| 247 | if (!shouldSpeculateInstrs(Latch->begin(), Jmp)) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 248 | return false; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 249 | |
| 250 | DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into " |
| 251 | << LastExit->getName() << "\n"); |
| 252 | |
| 253 | // Hoist the instructions from Latch into LastExit. |
| 254 | LastExit->getInstList().splice(BI, Latch->getInstList(), Latch->begin(), Jmp); |
| 255 | |
| 256 | unsigned FallThruPath = BI->getSuccessor(0) == Latch ? 0 : 1; |
| 257 | BasicBlock *Header = Jmp->getSuccessor(0); |
| 258 | assert(Header == L->getHeader() && "expected a backward branch"); |
| 259 | |
| 260 | // Remove Latch from the CFG so that LastExit becomes the new Latch. |
| 261 | BI->setSuccessor(FallThruPath, Header); |
| 262 | Latch->replaceSuccessorsPhiUsesWith(LastExit); |
| 263 | Jmp->eraseFromParent(); |
| 264 | |
| 265 | // Nuke the Latch block. |
| 266 | assert(Latch->empty() && "unable to evacuate Latch"); |
| 267 | LI->removeBlock(Latch); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 268 | if (DominatorTreeWrapperPass *DTWP = |
| 269 | getAnalysisIfAvailable<DominatorTreeWrapperPass>()) |
| 270 | DTWP->getDomTree().eraseNode(Latch); |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 271 | Latch->eraseFromParent(); |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 272 | return true; |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Dan Gohman | b5650eb | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 275 | /// Rotate loop LP. Return true if the loop is rotated. |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 276 | /// |
| 277 | /// \param SimplifiedLatch is true if the latch was just folded into the final |
| 278 | /// loop exit. In this case we may want to rotate even though the new latch is |
| 279 | /// now an exiting branch. This rotation would have happened had the latch not |
| 280 | /// been simplified. However, if SimplifiedLatch is false, then we avoid |
| 281 | /// rotating loops in which the latch exits to avoid excessive or endless |
| 282 | /// rotation. LoopRotate should be repeatable and converge to a canonical |
| 283 | /// form. This property is satisfied because simplifying the loop latch can only |
| 284 | /// happen once across multiple invocations of the LoopRotate pass. |
| 285 | bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) { |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 286 | // If the loop has only one block then there is not much to rotate. |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 287 | if (L->getBlocks().size() == 1) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 288 | return false; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 290 | BasicBlock *OrigHeader = L->getHeader(); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 291 | BasicBlock *OrigLatch = L->getLoopLatch(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 293 | BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame^] | 294 | if (!BI || BI->isUnconditional()) |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 295 | return false; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 296 | |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 297 | // If the loop header is not one of the loop exiting blocks then |
| 298 | // either this loop is already rotated or it is not |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 299 | // suitable for loop rotation transformations. |
Dan Gohman | 8f4078b | 2009-10-24 23:34:26 +0000 | [diff] [blame] | 300 | if (!L->isLoopExiting(OrigHeader)) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 301 | return false; |
| 302 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 303 | // If the loop latch already contains a branch that leaves the loop then the |
| 304 | // loop is already rotated. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame^] | 305 | if (!OrigLatch) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 306 | return false; |
| 307 | |
| 308 | // Rotate if either the loop latch does *not* exit the loop, or if the loop |
| 309 | // latch was just simplified. |
| 310 | if (L->isLoopExiting(OrigLatch) && !SimplifiedLatch) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 311 | return false; |
| 312 | |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 313 | // Check size of original header and reject loop if it is very big or we can't |
| 314 | // duplicate blocks inside it. |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 315 | { |
| 316 | CodeMetrics Metrics; |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 317 | Metrics.analyzeBasicBlock(OrigHeader, *TTI); |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 318 | if (Metrics.notDuplicatable) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 319 | DEBUG(dbgs() << "LoopRotation: NOT rotating - contains non-duplicatable" |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 320 | << " instructions: "; L->dump()); |
| 321 | return false; |
| 322 | } |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 323 | if (Metrics.NumInsts > MAX_HEADER_SIZE) |
| 324 | return false; |
Devang Patel | bab43b4 | 2009-03-06 03:51:30 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 327 | // Now, this loop is suitable for rotation. |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 328 | BasicBlock *OrigPreheader = L->getLoopPreheader(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 88974f4 | 2011-04-09 07:25:58 +0000 | [diff] [blame] | 330 | // If the loop could not be converted to canonical form, it must have an |
| 331 | // indirectbr in it, just give up. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame^] | 332 | if (!OrigPreheader) |
Chris Lattner | 88974f4 | 2011-04-09 07:25:58 +0000 | [diff] [blame] | 333 | return false; |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 334 | |
Dan Gohman | fc20b67 | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 335 | // Anything ScalarEvolution may know about this loop or the PHI nodes |
| 336 | // in its header will soon be invalidated. |
| 337 | if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>()) |
Dan Gohman | 880c92a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 338 | SE->forgetLoop(L); |
Dan Gohman | fc20b67 | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 339 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 340 | DEBUG(dbgs() << "LoopRotation: rotating "; L->dump()); |
| 341 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 342 | // Find new Loop header. NewHeader is a Header's one and only successor |
Chris Lattner | 57cb472 | 2009-01-26 01:57:01 +0000 | [diff] [blame] | 343 | // that is inside loop. Header's other successor is outside the |
| 344 | // loop. Otherwise loop is not suitable for rotation. |
Chris Lattner | 385f2ec | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 345 | BasicBlock *Exit = BI->getSuccessor(0); |
| 346 | BasicBlock *NewHeader = BI->getSuccessor(1); |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 347 | if (L->contains(Exit)) |
| 348 | std::swap(Exit, NewHeader); |
Chris Lattner | d67aaa6 | 2009-01-26 01:38:24 +0000 | [diff] [blame] | 349 | assert(NewHeader && "Unable to determine new loop header"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 350 | assert(L->contains(NewHeader) && !L->contains(Exit) && |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 351 | "Unable to determine loop header and exit blocks"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 352 | |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 353 | // This code assumes that the new header has exactly one predecessor. |
| 354 | // Remove any single-entry PHI nodes in it. |
Chris Lattner | 7b6647c | 2009-01-26 02:11:30 +0000 | [diff] [blame] | 355 | assert(NewHeader->getSinglePredecessor() && |
| 356 | "New header doesn't have one pred!"); |
| 357 | FoldSingleEntryPHINodes(NewHeader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 358 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 359 | // Begin by walking OrigHeader and populating ValueMap with an entry for |
| 360 | // each Instruction. |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 361 | BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end(); |
Chris Lattner | 2b3f20e | 2011-01-08 07:21:31 +0000 | [diff] [blame] | 362 | ValueToValueMapTy ValueMap; |
Devang Patel | b9af574 | 2007-04-09 19:04:21 +0000 | [diff] [blame] | 363 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 364 | // For PHI nodes, the value available in OldPreHeader is just the |
| 365 | // incoming value from OldPreHeader. |
| 366 | for (; PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Jay Foad | 372ad64 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 367 | ValueMap[PN] = PN->getIncomingValueForBlock(OrigPreheader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 368 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 369 | // For the rest of the instructions, either hoist to the OrigPreheader if |
| 370 | // possible or create a clone in the OldPreHeader if not. |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 371 | TerminatorInst *LoopEntryBranch = OrigPreheader->getTerminator(); |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 372 | while (I != E) { |
| 373 | Instruction *Inst = I++; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 374 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 375 | // If the instruction's operands are invariant and it doesn't read or write |
| 376 | // memory, then it is safe to hoist. Doing this doesn't change the order of |
| 377 | // execution in the preheader, but does prevent the instruction from |
| 378 | // executing in each iteration of the loop. This means it is safe to hoist |
| 379 | // something that might trap, but isn't safe to hoist something that reads |
| 380 | // memory (without proving that the loop doesn't write). |
| 381 | if (L->hasLoopInvariantOperands(Inst) && |
| 382 | !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() && |
Eli Friedman | c458885 | 2012-02-16 00:41:10 +0000 | [diff] [blame] | 383 | !isa<TerminatorInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst) && |
| 384 | !isa<AllocaInst>(Inst)) { |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 385 | Inst->moveBefore(LoopEntryBranch); |
| 386 | continue; |
| 387 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 388 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 389 | // Otherwise, create a duplicate of the instruction. |
| 390 | Instruction *C = Inst->clone(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 392 | // Eagerly remap the operands of the instruction. |
| 393 | RemapInstruction(C, ValueMap, |
| 394 | RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 396 | // With the operands remapped, see if the instruction constant folds or is |
| 397 | // otherwise simplifyable. This commonly occurs because the entry from PHI |
| 398 | // nodes allows icmps and other instructions to fold. |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 399 | Value *V = SimplifyInstruction(C); |
| 400 | if (V && LI->replacementPreservesLCSSAForm(C, V)) { |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 401 | // If so, then delete the temporary instruction and stick the folded value |
| 402 | // in the map. |
| 403 | delete C; |
| 404 | ValueMap[Inst] = V; |
| 405 | } else { |
| 406 | // Otherwise, stick the new instruction into the new block! |
| 407 | C->setName(Inst->getName()); |
| 408 | C->insertBefore(LoopEntryBranch); |
| 409 | ValueMap[Inst] = C; |
| 410 | } |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 413 | // Along with all the other instructions, we just cloned OrigHeader's |
| 414 | // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's |
| 415 | // successors by duplicating their incoming values for OrigHeader. |
| 416 | TerminatorInst *TI = OrigHeader->getTerminator(); |
| 417 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 418 | for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin(); |
| 419 | PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 420 | PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 421 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 422 | // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove |
| 423 | // OrigPreHeader's old terminator (the original branch into the loop), and |
| 424 | // remove the corresponding incoming values from the PHI nodes in OrigHeader. |
| 425 | LoopEntryBranch->eraseFromParent(); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 427 | // If there were any uses of instructions in the duplicated block outside the |
| 428 | // loop, update them, inserting PHI nodes as required |
| 429 | RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 430 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 431 | // NewHeader is now the header of the loop. |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 432 | L->moveToHeader(NewHeader); |
Chris Lattner | 2615130 | 2011-01-08 19:10:28 +0000 | [diff] [blame] | 433 | assert(L->getHeader() == NewHeader && "Latch block is our new header"); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 434 | |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 436 | // At this point, we've finished our major CFG changes. As part of cloning |
| 437 | // the loop into the preheader we've simplified instructions and the |
| 438 | // duplicated conditional branch may now be branching on a constant. If it is |
| 439 | // branching on a constant and if that constant means that we enter the loop, |
| 440 | // then we fold away the cond branch to an uncond branch. This simplifies the |
| 441 | // loop in cases important for nested loops, and it also means we don't have |
| 442 | // to split as many edges. |
| 443 | BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator()); |
| 444 | assert(PHBI->isConditional() && "Should be clone of BI condbr!"); |
| 445 | if (!isa<ConstantInt>(PHBI->getCondition()) || |
| 446 | PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) |
| 447 | != NewHeader) { |
| 448 | // The conditional branch can't be folded, handle the general case. |
| 449 | // Update DominatorTree to reflect the CFG change we just made. Then split |
| 450 | // edges as necessary to preserve LoopSimplify form. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 451 | if (DominatorTreeWrapperPass *DTWP = |
| 452 | getAnalysisIfAvailable<DominatorTreeWrapperPass>()) { |
| 453 | DominatorTree &DT = DTWP->getDomTree(); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 454 | // Everything that was dominated by the old loop header is now dominated |
| 455 | // by the original loop preheader. Conceptually the header was merged |
| 456 | // into the preheader, even though we reuse the actual block as a new |
| 457 | // loop latch. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 458 | DomTreeNode *OrigHeaderNode = DT.getNode(OrigHeader); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 459 | SmallVector<DomTreeNode *, 8> HeaderChildren(OrigHeaderNode->begin(), |
| 460 | OrigHeaderNode->end()); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 461 | DomTreeNode *OrigPreheaderNode = DT.getNode(OrigPreheader); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 462 | for (unsigned I = 0, E = HeaderChildren.size(); I != E; ++I) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 463 | DT.changeImmediateDominator(HeaderChildren[I], OrigPreheaderNode); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 464 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 465 | assert(DT.getNode(Exit)->getIDom() == OrigPreheaderNode); |
| 466 | assert(DT.getNode(NewHeader)->getIDom() == OrigPreheaderNode); |
Benjamin Kramer | 3be6a48 | 2012-09-01 12:04:51 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 468 | // Update OrigHeader to be dominated by the new header block. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 469 | DT.changeImmediateDominator(OrigHeader, OrigLatch); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 470 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 472 | // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 473 | // thus is not a preheader anymore. |
| 474 | // Split the edge to form a real preheader. |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 475 | BasicBlock *NewPH = SplitCriticalEdge(OrigPreheader, NewHeader, this); |
| 476 | NewPH->setName(NewHeader->getName() + ".lr.ph"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 477 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 478 | // Preserve canonical loop form, which means that 'Exit' should have only |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 479 | // one predecessor. Note that Exit could be an exit block for multiple |
| 480 | // nested loops, causing both of the edges to now be critical and need to |
| 481 | // be split. |
| 482 | SmallVector<BasicBlock *, 4> ExitPreds(pred_begin(Exit), pred_end(Exit)); |
| 483 | bool SplitLatchEdge = false; |
| 484 | for (SmallVectorImpl<BasicBlock *>::iterator PI = ExitPreds.begin(), |
| 485 | PE = ExitPreds.end(); |
| 486 | PI != PE; ++PI) { |
| 487 | // We only need to split loop exit edges. |
| 488 | Loop *PredLoop = LI->getLoopFor(*PI); |
| 489 | if (!PredLoop || PredLoop->contains(Exit)) |
| 490 | continue; |
| 491 | SplitLatchEdge |= L->getLoopLatch() == *PI; |
| 492 | BasicBlock *ExitSplit = SplitCriticalEdge(*PI, Exit, this); |
| 493 | ExitSplit->moveBefore(Exit); |
| 494 | } |
| 495 | assert(SplitLatchEdge && |
| 496 | "Despite splitting all preds, failed to split latch exit?"); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 497 | } else { |
| 498 | // We can fold the conditional branch in the preheader, this makes things |
| 499 | // simpler. The first step is to remove the extra edge to the Exit block. |
| 500 | Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/); |
Devang Patel | c1f7c1d | 2011-04-29 20:38:55 +0000 | [diff] [blame] | 501 | BranchInst *NewBI = BranchInst::Create(NewHeader, PHBI); |
| 502 | NewBI->setDebugLoc(PHBI->getDebugLoc()); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 503 | PHBI->eraseFromParent(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 505 | // With our CFG finalized, update DomTree if it is available. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 506 | if (DominatorTreeWrapperPass *DTWP = |
| 507 | getAnalysisIfAvailable<DominatorTreeWrapperPass>()) { |
| 508 | DominatorTree &DT = DTWP->getDomTree(); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 509 | // Update OrigHeader to be dominated by the new header block. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 510 | DT.changeImmediateDominator(NewHeader, OrigPreheader); |
| 511 | DT.changeImmediateDominator(OrigHeader, OrigLatch); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 512 | |
| 513 | // Brute force incremental dominator tree update. Call |
| 514 | // findNearestCommonDominator on all CFG predecessors of each child of the |
| 515 | // original header. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 516 | DomTreeNode *OrigHeaderNode = DT.getNode(OrigHeader); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 517 | SmallVector<DomTreeNode *, 8> HeaderChildren(OrigHeaderNode->begin(), |
| 518 | OrigHeaderNode->end()); |
| 519 | bool Changed; |
| 520 | do { |
| 521 | Changed = false; |
| 522 | for (unsigned I = 0, E = HeaderChildren.size(); I != E; ++I) { |
| 523 | DomTreeNode *Node = HeaderChildren[I]; |
| 524 | BasicBlock *BB = Node->getBlock(); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 525 | |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 526 | pred_iterator PI = pred_begin(BB); |
| 527 | BasicBlock *NearestDom = *PI; |
| 528 | for (pred_iterator PE = pred_end(BB); PI != PE; ++PI) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 529 | NearestDom = DT.findNearestCommonDominator(NearestDom, *PI); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 530 | |
| 531 | // Remember if this changes the DomTree. |
| 532 | if (Node->getIDom()->getBlock() != NearestDom) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 533 | DT.changeImmediateDominator(BB, NearestDom); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 534 | Changed = true; |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 535 | } |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 538 | // If the dominator changed, this may have an effect on other |
| 539 | // predecessors, continue until we reach a fixpoint. |
| 540 | } while (Changed); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 541 | } |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 542 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 544 | assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation"); |
Chris Lattner | 063dca0 | 2011-01-08 18:52:51 +0000 | [diff] [blame] | 545 | assert(L->getLoopLatch() && "Invalid loop latch after loop rotation"); |
Chris Lattner | fee37c5 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 63fe78d | 2011-01-11 07:47:59 +0000 | [diff] [blame] | 547 | // Now that the CFG and DomTree are in a consistent state again, try to merge |
| 548 | // the OrigHeader block into OrigLatch. This will succeed if they are |
| 549 | // connected by an unconditional branch. This is just a cleanup so the |
| 550 | // emitted code isn't too gross in this common case. |
| 551 | MergeBlockIntoPredecessor(OrigHeader, this); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 552 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 553 | DEBUG(dbgs() << "LoopRotation: into "; L->dump()); |
| 554 | |
Chris Lattner | fee37c5 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 555 | ++NumRotated; |
| 556 | return true; |
Devang Patel | 8541978 | 2007-04-09 20:19:46 +0000 | [diff] [blame] | 557 | } |