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