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