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