Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 1 | //===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements some loop unrolling utilities. It does not define any |
| 11 | // actual pass or policy, but provides a single function to perform loop |
| 12 | // unrolling. |
| 13 | // |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 14 | // The process of unrolling can produce extraneous basic blocks linked with |
| 15 | // unconditional branches. This will be corrected in the future. |
Chris Lattner | b298db7 | 2011-01-11 08:00:40 +0000 | [diff] [blame] | 16 | // |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #define DEBUG_TYPE "loop-unroll" |
| 20 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Duncan Sands | b6133d1 | 2010-11-23 20:26:33 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopIterator.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/LoopPass.h" |
Dan Gohman | 572365e | 2010-07-26 18:02:06 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame^] | 26 | #include "llvm/IR/BasicBlock.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Cloning.h" |
| 31 | #include "llvm/Transforms/Utils/Local.h" |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/SimplifyIndVar.h" |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 35 | // TODO: Should these be here or in LoopUnroll? |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 36 | STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled"); |
Chris Lattner | b298db7 | 2011-01-11 08:00:40 +0000 | [diff] [blame] | 37 | STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 38 | |
| 39 | /// RemapInstruction - Convert the instruction operands from referencing the |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 40 | /// current values into those specified by VMap. |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 41 | static inline void RemapInstruction(Instruction *I, |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 42 | ValueToValueMapTy &VMap) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 43 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 44 | Value *Op = I->getOperand(op); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 45 | ValueToValueMapTy::iterator It = VMap.find(Op); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 46 | if (It != VMap.end()) |
Dan Gohman | b56c966 | 2009-10-31 14:46:50 +0000 | [diff] [blame] | 47 | I->setOperand(op, It->second); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 48 | } |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 49 | |
| 50 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 51 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 52 | ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i)); |
| 53 | if (It != VMap.end()) |
| 54 | PN->setIncomingBlock(i, cast<BasicBlock>(It->second)); |
| 55 | } |
| 56 | } |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 59 | /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it |
| 60 | /// only has one predecessor, and that predecessor only has one successor. |
| 61 | /// The LoopInfo Analysis that is passed will be kept consistent. |
| 62 | /// Returns the new combined block. |
Andrew Trick | 1009c32 | 2011-08-03 18:32:11 +0000 | [diff] [blame] | 63 | static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI, |
| 64 | LPPassManager *LPM) { |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 65 | // Merge basic blocks into their predecessor if there is only one distinct |
| 66 | // pred, and if there is only one distinct successor of the predecessor, and |
| 67 | // if there are no PHI nodes. |
| 68 | BasicBlock *OnlyPred = BB->getSinglePredecessor(); |
| 69 | if (!OnlyPred) return 0; |
| 70 | |
| 71 | if (OnlyPred->getTerminator()->getNumSuccessors() != 1) |
| 72 | return 0; |
| 73 | |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 74 | DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 75 | |
| 76 | // Resolve any PHI nodes at the start of the block. They are all |
| 77 | // guaranteed to have exactly one entry if they exist, unless there are |
| 78 | // multiple duplicate (but guaranteed to be equal) entries for the |
| 79 | // incoming edges. This occurs when there are multiple edges from |
| 80 | // OnlyPred to OnlySucc. |
| 81 | FoldSingleEntryPHINodes(BB); |
| 82 | |
| 83 | // Delete the unconditional branch from the predecessor... |
| 84 | OnlyPred->getInstList().pop_back(); |
| 85 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 86 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 87 | // source... |
| 88 | BB->replaceAllUsesWith(OnlyPred); |
| 89 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 90 | // Move all definitions in the successor to the predecessor... |
| 91 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
| 92 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 93 | std::string OldName = BB->getName(); |
| 94 | |
| 95 | // Erase basic block from the function... |
Andrew Trick | 1009c32 | 2011-08-03 18:32:11 +0000 | [diff] [blame] | 96 | |
| 97 | // ScalarEvolution holds references to loop exit blocks. |
Andrew Trick | 1247376 | 2012-06-05 17:51:05 +0000 | [diff] [blame] | 98 | if (LPM) { |
| 99 | if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) { |
| 100 | if (Loop *L = LI->getLoopFor(BB)) |
| 101 | SE->forgetLoop(L); |
| 102 | } |
Andrew Trick | 1009c32 | 2011-08-03 18:32:11 +0000 | [diff] [blame] | 103 | } |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 104 | LI->removeBlock(BB); |
| 105 | BB->eraseFromParent(); |
| 106 | |
| 107 | // Inherit predecessor's name if it exists... |
| 108 | if (!OldName.empty() && !OnlyPred->hasName()) |
| 109 | OnlyPred->setName(OldName); |
| 110 | |
| 111 | return OnlyPred; |
| 112 | } |
| 113 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 114 | /// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true |
Chris Lattner | f5ebfb0 | 2011-02-18 04:25:21 +0000 | [diff] [blame] | 115 | /// if unrolling was successful, or false if the loop was unmodified. Unrolling |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 116 | /// can only fail when the loop's latch block is not terminated by a conditional |
| 117 | /// branch instruction. However, if the trip count (and multiple) are not known, |
| 118 | /// loop unrolling will mostly produce more code that is no faster. |
| 119 | /// |
Andrew Trick | 478849e | 2011-07-25 22:17:47 +0000 | [diff] [blame] | 120 | /// TripCount is generally defined as the number of times the loop header |
| 121 | /// executes. UnrollLoop relaxes the definition to permit early exits: here |
| 122 | /// TripCount is the iteration on which control exits LatchBlock if no early |
| 123 | /// exits were taken. Note that UnrollLoop assumes that the loop counter test |
| 124 | /// terminates LatchBlock in order to remove unnecesssary instances of the |
| 125 | /// test. In other words, control may exit the loop prior to TripCount |
| 126 | /// iterations via an early branch, but control may not exit the loop from the |
| 127 | /// LatchBlock's terminator prior to TripCount iterations. |
| 128 | /// |
| 129 | /// Similarly, TripMultiple divides the number of times that the LatchBlock may |
| 130 | /// execute without exiting the loop. |
| 131 | /// |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 132 | /// The LoopInfo Analysis that is passed will be kept consistent. |
| 133 | /// |
| 134 | /// If a LoopPassManager is passed in, and the loop is fully removed, it will be |
| 135 | /// removed from the LoopPassManager as well. LPM can also be NULL. |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 136 | /// |
| 137 | /// This utility preserves LoopInfo. If DominatorTree or ScalarEvolution are |
Andrew Trick | 7cb3dcb | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 138 | /// available it must also preserve those analyses. |
Andrew Trick | 2045ce1 | 2011-07-23 00:33:05 +0000 | [diff] [blame] | 139 | bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 140 | bool AllowRuntime, unsigned TripMultiple, |
| 141 | LoopInfo *LI, LPPassManager *LPM) { |
Dan Gohman | 692ad8d | 2009-11-05 19:44:06 +0000 | [diff] [blame] | 142 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 143 | if (!Preheader) { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 144 | DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n"); |
Dan Gohman | 692ad8d | 2009-11-05 19:44:06 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 148 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Dan Gohman | 692ad8d | 2009-11-05 19:44:06 +0000 | [diff] [blame] | 149 | if (!LatchBlock) { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 150 | DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n"); |
Dan Gohman | 692ad8d | 2009-11-05 19:44:06 +0000 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 154 | // Loops with indirectbr cannot be cloned. |
| 155 | if (!L->isSafeToClone()) { |
| 156 | DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n"); |
| 157 | return false; |
| 158 | } |
| 159 | |
Dan Gohman | 692ad8d | 2009-11-05 19:44:06 +0000 | [diff] [blame] | 160 | BasicBlock *Header = L->getHeader(); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 161 | BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 162 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 163 | if (!BI || BI->isUnconditional()) { |
| 164 | // The loop-rotate pass can be helpful to avoid this in many cases. |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 165 | DEBUG(dbgs() << |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 166 | " Can't unroll; loop not terminated by a conditional branch.\n"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 169 | |
Chris Lattner | f5ebfb0 | 2011-02-18 04:25:21 +0000 | [diff] [blame] | 170 | if (Header->hasAddressTaken()) { |
| 171 | // The loop-rotate pass can be helpful to avoid this in many cases. |
| 172 | DEBUG(dbgs() << |
| 173 | " Won't unroll loop: address of header block is taken.\n"); |
| 174 | return false; |
| 175 | } |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 176 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 177 | if (TripCount != 0) |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 178 | DEBUG(dbgs() << " Trip Count = " << TripCount << "\n"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 179 | if (TripMultiple != 1) |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 180 | DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 181 | |
| 182 | // Effectively "DCE" unrolled iterations that are beyond the tripcount |
| 183 | // and will never be executed. |
| 184 | if (TripCount != 0 && Count > TripCount) |
| 185 | Count = TripCount; |
| 186 | |
Andrew Trick | 1da2827 | 2011-12-16 02:03:48 +0000 | [diff] [blame] | 187 | // Don't enter the unroll code if there is nothing to do. This way we don't |
| 188 | // need to support "partial unrolling by 1". |
| 189 | if (TripCount == 0 && Count < 2) |
| 190 | return false; |
| 191 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 192 | assert(Count > 0); |
| 193 | assert(TripMultiple > 0); |
| 194 | assert(TripCount == 0 || TripCount % TripMultiple == 0); |
| 195 | |
| 196 | // Are we eliminating the loop control altogether? |
| 197 | bool CompletelyUnroll = Count == TripCount; |
| 198 | |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 199 | // We assume a run-time trip count if the compiler cannot |
| 200 | // figure out the loop trip count and the unroll-runtime |
| 201 | // flag is specified. |
| 202 | bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime); |
| 203 | |
| 204 | if (RuntimeTripCount && !UnrollRuntimeLoopProlog(L, Count, LI, LPM)) |
| 205 | return false; |
| 206 | |
| 207 | // Notify ScalarEvolution that the loop will be substantially changed, |
| 208 | // if not outright eliminated. |
Andrew Trick | b78d83d | 2012-05-08 02:52:09 +0000 | [diff] [blame] | 209 | if (LPM) { |
| 210 | ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>(); |
| 211 | if (SE) |
| 212 | SE->forgetLoop(L); |
| 213 | } |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 214 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 215 | // If we know the trip count, we know the multiple... |
| 216 | unsigned BreakoutTrip = 0; |
| 217 | if (TripCount != 0) { |
| 218 | BreakoutTrip = TripCount % Count; |
| 219 | TripMultiple = 0; |
| 220 | } else { |
| 221 | // Figure out what multiple to use. |
| 222 | BreakoutTrip = TripMultiple = |
| 223 | (unsigned)GreatestCommonDivisor64(Count, TripMultiple); |
| 224 | } |
| 225 | |
| 226 | if (CompletelyUnroll) { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 227 | DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 228 | << " with trip count " << TripCount << "!\n"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 229 | } else { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 230 | DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 231 | << " by " << Count); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 232 | if (TripMultiple == 0 || BreakoutTrip != TripMultiple) { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 233 | DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 234 | } else if (TripMultiple != 1) { |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 235 | DEBUG(dbgs() << " with " << TripMultiple << " trips per branch"); |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 236 | } else if (RuntimeTripCount) { |
| 237 | DEBUG(dbgs() << " with run-time trip count"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 238 | } |
David Greene | a9ad9c2 | 2010-01-05 01:26:41 +0000 | [diff] [blame] | 239 | DEBUG(dbgs() << "!\n"); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | std::vector<BasicBlock*> LoopBlocks = L->getBlocks(); |
| 243 | |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 244 | bool ContinueOnTrue = L->contains(BI->getSuccessor(0)); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 245 | BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue); |
| 246 | |
| 247 | // For the first iteration of the loop, we should use the precloned values for |
| 248 | // PHI nodes. Insert associations now. |
Devang Patel | 3943084 | 2010-04-20 22:24:18 +0000 | [diff] [blame] | 249 | ValueToValueMapTy LastValueMap; |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 250 | std::vector<PHINode*> OrigPHINode; |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 251 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
Andrew Trick | 70d0ca9 | 2011-08-09 03:11:29 +0000 | [diff] [blame] | 252 | OrigPHINode.push_back(cast<PHINode>(I)); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | std::vector<BasicBlock*> Headers; |
| 256 | std::vector<BasicBlock*> Latches; |
| 257 | Headers.push_back(Header); |
| 258 | Latches.push_back(LatchBlock); |
| 259 | |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 260 | // The current on-the-fly SSA update requires blocks to be processed in |
| 261 | // reverse postorder so that LastValueMap contains the correct value at each |
| 262 | // exit. |
| 263 | LoopBlocksDFS DFS(L); |
Andrew Trick | 2d31ae3 | 2011-08-10 01:59:05 +0000 | [diff] [blame] | 264 | DFS.perform(LI); |
| 265 | |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 266 | // Stash the DFS iterators before adding blocks to the loop. |
| 267 | LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO(); |
| 268 | LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO(); |
| 269 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 270 | for (unsigned It = 1; It != Count; ++It) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 271 | std::vector<BasicBlock*> NewBlocks; |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 272 | |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 273 | for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) { |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 274 | ValueToValueMapTy VMap; |
| 275 | BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It)); |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 276 | Header->getParent()->getBasicBlockList().push_back(New); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 277 | |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 278 | // Loop over all of the PHI nodes in the block, changing them to use the |
| 279 | // incoming values from the previous block. |
| 280 | if (*BB == Header) |
| 281 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 282 | PHINode *NewPHI = cast<PHINode>(VMap[OrigPHINode[i]]); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 283 | Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); |
| 284 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 285 | if (It > 1 && L->contains(InValI)) |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 286 | InVal = LastValueMap[InValI]; |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 287 | VMap[OrigPHINode[i]] = InVal; |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 288 | New->getInstList().erase(NewPHI); |
| 289 | } |
| 290 | |
| 291 | // Update our running map of newest clones |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 292 | LastValueMap[*BB] = New; |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 293 | for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end(); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 294 | VI != VE; ++VI) |
| 295 | LastValueMap[VI->first] = VI->second; |
| 296 | |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 297 | L->addBasicBlockToLoop(New, LI->getBase()); |
| 298 | |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 299 | // Add phi entries for newly created values to all exit blocks. |
| 300 | for (succ_iterator SI = succ_begin(*BB), SE = succ_end(*BB); |
| 301 | SI != SE; ++SI) { |
| 302 | if (L->contains(*SI)) |
| 303 | continue; |
| 304 | for (BasicBlock::iterator BBI = (*SI)->begin(); |
| 305 | PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) { |
| 306 | Value *Incoming = phi->getIncomingValueForBlock(*BB); |
| 307 | ValueToValueMapTy::iterator It = LastValueMap.find(Incoming); |
| 308 | if (It != LastValueMap.end()) |
| 309 | Incoming = It->second; |
| 310 | phi->addIncoming(Incoming, New); |
| 311 | } |
| 312 | } |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 313 | // Keep track of new headers and latches as we create them, so that |
| 314 | // we can insert the proper branches later. |
| 315 | if (*BB == Header) |
| 316 | Headers.push_back(New); |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 317 | if (*BB == LatchBlock) |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 318 | Latches.push_back(New); |
| 319 | |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 320 | NewBlocks.push_back(New); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 321 | } |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 322 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 323 | // Remap all instructions in the most recent iteration |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 324 | for (unsigned i = 0; i < NewBlocks.size(); ++i) |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 325 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 326 | E = NewBlocks[i]->end(); I != E; ++I) |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 327 | ::RemapInstruction(I, LastValueMap); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 328 | } |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 329 | |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 330 | // Loop over the PHI nodes in the original block, setting incoming values. |
| 331 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 332 | PHINode *PN = OrigPHINode[i]; |
| 333 | if (CompletelyUnroll) { |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 334 | PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); |
| 335 | Header->getInstList().erase(PN); |
| 336 | } |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 337 | else if (Count > 1) { |
| 338 | Value *InVal = PN->removeIncomingValue(LatchBlock, false); |
| 339 | // If this value was defined in the loop, take the value defined by the |
| 340 | // last iteration of the loop. |
| 341 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) { |
| 342 | if (L->contains(InValI)) |
| 343 | InVal = LastValueMap[InVal]; |
| 344 | } |
| 345 | assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch"); |
| 346 | PN->addIncoming(InVal, Latches.back()); |
| 347 | } |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 348 | } |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 349 | |
| 350 | // Now that all the basic blocks for the unrolled iterations are in place, |
| 351 | // set up the branches to connect them. |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 352 | for (unsigned i = 0, e = Latches.size(); i != e; ++i) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 353 | // The original branch was replicated in each unrolled iteration. |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 354 | BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 355 | |
| 356 | // The branch destination. |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 357 | unsigned j = (i + 1) % e; |
| 358 | BasicBlock *Dest = Headers[j]; |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 359 | bool NeedConditional = true; |
| 360 | |
Andrew Trick | 5d73448 | 2011-12-09 06:19:40 +0000 | [diff] [blame] | 361 | if (RuntimeTripCount && j != 0) { |
| 362 | NeedConditional = false; |
| 363 | } |
| 364 | |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 365 | // For a complete unroll, make the last iteration end with a branch |
| 366 | // to the exit block. |
| 367 | if (CompletelyUnroll && j == 0) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 368 | Dest = LoopExit; |
| 369 | NeedConditional = false; |
| 370 | } |
| 371 | |
| 372 | // If we know the trip count or a multiple of it, we can safely use an |
| 373 | // unconditional branch for some iterations. |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 374 | if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 375 | NeedConditional = false; |
| 376 | } |
| 377 | |
| 378 | if (NeedConditional) { |
| 379 | // Update the conditional branch's successor for the following |
| 380 | // iteration. |
| 381 | Term->setSuccessor(!ContinueOnTrue, Dest); |
| 382 | } else { |
Andrew Trick | b1eede1 | 2011-08-10 00:28:10 +0000 | [diff] [blame] | 383 | // Remove phi operands at this loop exit |
| 384 | if (Dest != LoopExit) { |
| 385 | BasicBlock *BB = Latches[i]; |
| 386 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 387 | SI != SE; ++SI) { |
| 388 | if (*SI == Headers[i]) |
| 389 | continue; |
| 390 | for (BasicBlock::iterator BBI = (*SI)->begin(); |
| 391 | PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) { |
| 392 | Phi->removeIncomingValue(BB, false); |
| 393 | } |
| 394 | } |
| 395 | } |
Jay Foad | 8f9ffbd | 2011-01-07 20:25:56 +0000 | [diff] [blame] | 396 | // Replace the conditional branch with an unconditional one. |
| 397 | BranchInst::Create(Dest, Term); |
| 398 | Term->eraseFromParent(); |
Jay Foad | cd35e09 | 2011-06-21 10:33:19 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 402 | // Merge adjacent basic blocks, if possible. |
| 403 | for (unsigned i = 0, e = Latches.size(); i != e; ++i) { |
| 404 | BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); |
| 405 | if (Term->isUnconditional()) { |
| 406 | BasicBlock *Dest = Term->getSuccessor(0); |
Andrew Trick | 1009c32 | 2011-08-03 18:32:11 +0000 | [diff] [blame] | 407 | if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM)) |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 408 | std::replace(Latches.begin(), Latches.end(), Dest, Fold); |
| 409 | } |
| 410 | } |
Andrew Trick | ba03377 | 2011-07-23 00:29:16 +0000 | [diff] [blame] | 411 | |
Andrew Trick | b78d83d | 2012-05-08 02:52:09 +0000 | [diff] [blame] | 412 | if (LPM) { |
| 413 | // FIXME: Reconstruct dom info, because it is not preserved properly. |
| 414 | // Incrementally updating domtree after loop unrolling would be easy. |
| 415 | if (DominatorTree *DT = LPM->getAnalysisIfAvailable<DominatorTree>()) |
| 416 | DT->runOnFunction(*L->getHeader()->getParent()); |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 417 | |
Andrew Trick | b78d83d | 2012-05-08 02:52:09 +0000 | [diff] [blame] | 418 | // Simplify any new induction variables in the partially unrolled loop. |
| 419 | ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>(); |
| 420 | if (SE && !CompletelyUnroll) { |
| 421 | SmallVector<WeakVH, 16> DeadInsts; |
| 422 | simplifyLoopIVs(L, SE, LPM, DeadInsts); |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 423 | |
Andrew Trick | b78d83d | 2012-05-08 02:52:09 +0000 | [diff] [blame] | 424 | // Aggressively clean up dead instructions that simplifyLoopIVs already |
| 425 | // identified. Any remaining should be cleaned up below. |
| 426 | while (!DeadInsts.empty()) |
| 427 | if (Instruction *Inst = |
| 428 | dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val())) |
| 429 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 430 | } |
Andrew Trick | 39f4029 | 2011-08-10 04:29:49 +0000 | [diff] [blame] | 431 | } |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 432 | // At this point, the code is well formed. We now do a quick sweep over the |
| 433 | // inserted code, doing constant propagation and dead code elimination as we |
| 434 | // go. |
| 435 | const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); |
| 436 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
| 437 | BBE = NewLoopBlocks.end(); BB != BBE; ++BB) |
| 438 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) { |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 439 | Instruction *Inst = I++; |
| 440 | |
| 441 | if (isInstructionTriviallyDead(Inst)) |
Dan Gohman | 8dbe7f8 | 2008-06-24 20:44:42 +0000 | [diff] [blame] | 442 | (*BB)->getInstList().erase(Inst); |
Duncan Sands | b6133d1 | 2010-11-23 20:26:33 +0000 | [diff] [blame] | 443 | else if (Value *V = SimplifyInstruction(Inst)) |
| 444 | if (LI->replacementPreservesLCSSAForm(Inst, V)) { |
| 445 | Inst->replaceAllUsesWith(V); |
| 446 | (*BB)->getInstList().erase(Inst); |
| 447 | } |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 448 | } |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 449 | |
| 450 | NumCompletelyUnrolled += CompletelyUnroll; |
| 451 | ++NumUnrolled; |
| 452 | // Remove the loop from the LoopPassManager if it's completely removed. |
| 453 | if (CompletelyUnroll && LPM != NULL) |
| 454 | LPM->deleteLoopFromQueue(L); |
| 455 | |
Dan Gohman | 45b3197 | 2008-05-14 00:24:14 +0000 | [diff] [blame] | 456 | return true; |
| 457 | } |