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