Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 1 | //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements a simple loop unroller. It works best when loops have |
| 11 | // been canonicalized by the -indvars pass, allowing it to determine the trip |
| 12 | // counts of loops easily. |
| 13 | // |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 14 | // This pass will multi-block loops only if they contain no non-unrolled |
| 15 | // subloops. The process of unrolling can produce extraneous basic blocks |
| 16 | // linked with unconditional branches. This will be corrected in the future. |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #define DEBUG_TYPE "loop-unroll" |
| 21 | #include "llvm/Transforms/Scalar.h" |
| 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Analysis/LoopInfo.h" |
| 26 | #include "llvm/Transforms/Utils/Cloning.h" |
| 27 | #include "llvm/Transforms/Utils/Local.h" |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 64613ea | 2004-11-22 17:18:36 +0000 | [diff] [blame] | 33 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 34 | #include <cstdio> |
Chris Lattner | 5864aeb | 2004-04-19 03:01:23 +0000 | [diff] [blame] | 35 | #include <set> |
Reid Spencer | 17e6e44 | 2004-10-18 14:38:48 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 37 | #include <iostream> |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
| 40 | namespace { |
| 41 | Statistic<> NumUnrolled("loop-unroll", "Number of loops completely unrolled"); |
| 42 | |
| 43 | cl::opt<unsigned> |
Chris Lattner | e3a5586 | 2004-04-18 18:06:14 +0000 | [diff] [blame] | 44 | UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden, |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 45 | cl::desc("The cut-off point for loop unrolling")); |
| 46 | |
| 47 | class LoopUnroll : public FunctionPass { |
| 48 | LoopInfo *LI; // The current loop information |
| 49 | public: |
| 50 | virtual bool runOnFunction(Function &F); |
| 51 | bool visitLoop(Loop *L); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 52 | BasicBlock* FoldBlockIntoPredecessor(BasicBlock* BB); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 53 | |
| 54 | /// This transformation requires natural loop information & requires that |
| 55 | /// loop preheaders be inserted into the CFG... |
| 56 | /// |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 58 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 59 | AU.addRequiredID(LCSSAID); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 60 | AU.addRequired<LoopInfo>(); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 61 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 62 | AU.addPreserved<LoopInfo>(); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 65 | RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops"); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | FunctionPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); } |
| 69 | |
| 70 | bool LoopUnroll::runOnFunction(Function &F) { |
| 71 | bool Changed = false; |
| 72 | LI = &getAnalysis<LoopInfo>(); |
| 73 | |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 74 | // Transform all the top-level loops. Copy the loop list so that the child |
| 75 | // can update the loop tree if it needs to delete the loop. |
| 76 | std::vector<Loop*> SubLoops(LI->begin(), LI->end()); |
| 77 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 78 | Changed |= visitLoop(SubLoops[i]); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 79 | |
| 80 | return Changed; |
| 81 | } |
| 82 | |
| 83 | /// ApproximateLoopSize - Approximate the size of the loop after it has been |
| 84 | /// unrolled. |
| 85 | static unsigned ApproximateLoopSize(const Loop *L) { |
| 86 | unsigned Size = 0; |
| 87 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) { |
| 88 | BasicBlock *BB = L->getBlocks()[i]; |
| 89 | Instruction *Term = BB->getTerminator(); |
| 90 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 91 | if (isa<PHINode>(I) && BB == L->getHeader()) { |
| 92 | // Ignore PHI nodes in the header. |
| 93 | } else if (I->hasOneUse() && I->use_back() == Term) { |
| 94 | // Ignore instructions only used by the loop terminator. |
Chris Lattner | 64613ea | 2004-11-22 17:18:36 +0000 | [diff] [blame] | 95 | } else if (DbgInfoIntrinsic *DbgI = dyn_cast<DbgInfoIntrinsic>(I)) { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 96 | // Ignore debug instructions |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 97 | } else { |
| 98 | ++Size; |
| 99 | } |
| 100 | |
| 101 | // TODO: Ignore expressions derived from PHI and constants if inval of phi |
| 102 | // is a constant, or if operation is associative. This will get induction |
| 103 | // variables. |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return Size; |
| 108 | } |
| 109 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 110 | // RemapInstruction - Convert the instruction operands from referencing the |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 111 | // current values into those specified by ValueMap. |
| 112 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 113 | static inline void RemapInstruction(Instruction *I, |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 114 | std::map<const Value *, Value*> &ValueMap) { |
| 115 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 116 | Value *Op = I->getOperand(op); |
| 117 | std::map<const Value *, Value*>::iterator It = ValueMap.find(Op); |
| 118 | if (It != ValueMap.end()) Op = It->second; |
| 119 | I->setOperand(op, Op); |
| 120 | } |
| 121 | } |
| 122 | |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 123 | // FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it |
| 124 | // only has one predecessor, and that predecessor only has one successor. |
| 125 | // Returns the new combined block. |
| 126 | BasicBlock* LoopUnroll::FoldBlockIntoPredecessor(BasicBlock* BB) { |
| 127 | // Merge basic blocks into their predecessor if there is only one distinct |
| 128 | // pred, and if there is only one distinct successor of the predecessor, and |
| 129 | // if there are no PHI nodes. |
| 130 | // |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 131 | BasicBlock *OnlyPred = BB->getSinglePredecessor(); |
| 132 | if (!OnlyPred) return 0; |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 133 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 134 | if (OnlyPred->getTerminator()->getNumSuccessors() != 1) |
| 135 | return 0; |
| 136 | |
| 137 | DEBUG(std::cerr << "Merging: " << *BB << "into: " << *OnlyPred); |
| 138 | TerminatorInst *Term = OnlyPred->getTerminator(); |
| 139 | |
| 140 | // Resolve any PHI nodes at the start of the block. They are all |
| 141 | // guaranteed to have exactly one entry if they exist, unless there are |
| 142 | // multiple duplicate (but guaranteed to be equal) entries for the |
| 143 | // incoming edges. This occurs when there are multiple edges from |
| 144 | // OnlyPred to OnlySucc. |
| 145 | // |
| 146 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 147 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 148 | BB->getInstList().pop_front(); // Delete the phi node... |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 151 | // Delete the unconditional branch from the predecessor... |
| 152 | OnlyPred->getInstList().pop_back(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 153 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 154 | // Move all definitions in the successor to the predecessor... |
| 155 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 156 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 157 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 158 | // source... |
| 159 | BB->replaceAllUsesWith(OnlyPred); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 160 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 161 | std::string OldName = BB->getName(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 162 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 163 | // Erase basic block from the function... |
| 164 | LI->removeBlock(BB); |
| 165 | BB->eraseFromParent(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 166 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 167 | // Inherit predecessors name if it exists... |
| 168 | if (!OldName.empty() && !OnlyPred->hasName()) |
| 169 | OnlyPred->setName(OldName); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 170 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 171 | return OnlyPred; |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 174 | bool LoopUnroll::visitLoop(Loop *L) { |
| 175 | bool Changed = false; |
| 176 | |
| 177 | // Recurse through all subloops before we process this loop. Copy the loop |
| 178 | // list so that the child can update the loop tree if it needs to delete the |
| 179 | // loop. |
| 180 | std::vector<Loop*> SubLoops(L->begin(), L->end()); |
| 181 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 182 | Changed |= visitLoop(SubLoops[i]); |
| 183 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 184 | BasicBlock* Header = L->getHeader(); |
| 185 | BasicBlock* LatchBlock = L->getLoopLatch(); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 186 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 187 | BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 188 | if (BI == 0) return Changed; // Must end in a conditional branch |
| 189 | |
| 190 | ConstantInt *TripCountC = dyn_cast_or_null<ConstantInt>(L->getTripCount()); |
| 191 | if (!TripCountC) return Changed; // Must have constant trip count! |
| 192 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 193 | uint64_t TripCountFull = TripCountC->getZExtValue(); |
| 194 | if (TripCountFull != TripCountC->getZExtValue() || TripCountFull == 0) |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 195 | return Changed; // More than 2^32 iterations??? |
| 196 | |
| 197 | unsigned LoopSize = ApproximateLoopSize(L); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 198 | DEBUG(std::cerr << "Loop Unroll: F[" << Header->getParent()->getName() |
| 199 | << "] Loop %" << Header->getName() << " Loop Size = " |
| 200 | << LoopSize << " Trip Count = " << TripCountFull << " - "); |
Chris Lattner | d4bc564 | 2005-01-08 19:37:20 +0000 | [diff] [blame] | 201 | uint64_t Size = (uint64_t)LoopSize*TripCountFull; |
Chris Lattner | 82fec4e | 2004-05-13 20:43:31 +0000 | [diff] [blame] | 202 | if (Size > UnrollThreshold) { |
| 203 | DEBUG(std::cerr << "TOO LARGE: " << Size << ">" << UnrollThreshold << "\n"); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 204 | return Changed; |
| 205 | } |
| 206 | DEBUG(std::cerr << "UNROLLING!\n"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 207 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 208 | std::vector<BasicBlock*> LoopBlocks = L->getBlocks(); |
| 209 | |
Chris Lattner | d4bc564 | 2005-01-08 19:37:20 +0000 | [diff] [blame] | 210 | unsigned TripCount = (unsigned)TripCountFull; |
| 211 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 212 | BasicBlock *LoopExit = BI->getSuccessor(L->contains(BI->getSuccessor(0))); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 213 | |
| 214 | // For the first iteration of the loop, we should use the precloned values for |
| 215 | // PHI nodes. Insert associations now. |
| 216 | std::map<const Value*, Value*> LastValueMap; |
| 217 | std::vector<PHINode*> OrigPHINode; |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 218 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 219 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 220 | OrigPHINode.push_back(PN); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 221 | if (Instruction *I = |
| 222 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock))) |
| 223 | if (L->contains(I->getParent())) |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 224 | LastValueMap[I] = I; |
| 225 | } |
| 226 | |
| 227 | // Remove the exit branch from the loop |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 228 | LatchBlock->getInstList().erase(BI); |
| 229 | |
| 230 | std::vector<BasicBlock*> Headers; |
| 231 | std::vector<BasicBlock*> Latches; |
| 232 | Headers.push_back(Header); |
| 233 | Latches.push_back(LatchBlock); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 234 | |
| 235 | assert(TripCount != 0 && "Trip count of 0 is impossible!"); |
| 236 | for (unsigned It = 1; It != TripCount; ++It) { |
| 237 | char SuffixBuffer[100]; |
| 238 | sprintf(SuffixBuffer, ".%d", It); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 239 | |
| 240 | std::vector<BasicBlock*> NewBlocks; |
| 241 | |
| 242 | for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(), |
| 243 | E = LoopBlocks.end(); BB != E; ++BB) { |
| 244 | std::map<const Value*, Value*> ValueMap; |
| 245 | BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer); |
| 246 | Header->getParent()->getBasicBlockList().push_back(New); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 247 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 248 | // Loop over all of the PHI nodes in the block, changing them to use the |
| 249 | // incoming values from the previous block. |
| 250 | if (*BB == Header) |
| 251 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 252 | PHINode *NewPHI = cast<PHINode>(ValueMap[OrigPHINode[i]]); |
| 253 | Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); |
| 254 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) |
| 255 | if (It > 1 && L->contains(InValI->getParent())) |
| 256 | InVal = LastValueMap[InValI]; |
| 257 | ValueMap[OrigPHINode[i]] = InVal; |
| 258 | New->getInstList().erase(NewPHI); |
| 259 | } |
| 260 | |
| 261 | // Update our running map of newest clones |
| 262 | LastValueMap[*BB] = New; |
| 263 | for (std::map<const Value*, Value*>::iterator VI = ValueMap.begin(), |
| 264 | VE = ValueMap.end(); VI != VE; ++VI) |
| 265 | LastValueMap[VI->first] = VI->second; |
| 266 | |
| 267 | L->addBasicBlockToLoop(New, *LI); |
| 268 | |
| 269 | // Add phi entries for newly created values to all exit blocks except |
| 270 | // the successor of the latch block. The successor of the exit block will |
| 271 | // be updated specially after unrolling all the way. |
| 272 | if (*BB != LatchBlock) |
| 273 | for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end(); |
| 274 | UI != UE; ++UI) { |
| 275 | Instruction* UseInst = cast<Instruction>(*UI); |
| 276 | if (isa<PHINode>(UseInst) && !L->contains(UseInst->getParent())) { |
| 277 | PHINode* phi = cast<PHINode>(UseInst); |
| 278 | Value* Incoming = phi->getIncomingValueForBlock(*BB); |
| 279 | if (isa<Instruction>(Incoming)) |
| 280 | Incoming = LastValueMap[Incoming]; |
| 281 | |
| 282 | phi->addIncoming(Incoming, New); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Keep track of new headers and latches as we create them, so that |
| 287 | // we can insert the proper branches later. |
| 288 | if (*BB == Header) |
| 289 | Headers.push_back(New); |
| 290 | if (*BB == LatchBlock) |
| 291 | Latches.push_back(New); |
| 292 | |
| 293 | NewBlocks.push_back(New); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 294 | } |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 295 | |
| 296 | // Remap all instructions in the most recent iteration |
| 297 | for (unsigned i = 0; i < NewBlocks.size(); ++i) |
| 298 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 299 | E = NewBlocks[i]->end(); I != E; ++I) |
Chris Lattner | 998f44f | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 300 | RemapInstruction(I, LastValueMap); |
Chris Lattner | 998f44f | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 302 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 303 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 304 | |
| 305 | // Update PHI nodes that reference the final latch block |
| 306 | if (TripCount > 1) { |
| 307 | std::set<PHINode*> Users; |
| 308 | for (Value::use_iterator UI = LatchBlock->use_begin(), |
| 309 | UE = LatchBlock->use_end(); UI != UE; ++UI) |
| 310 | if (PHINode* phi = dyn_cast<PHINode>(*UI)) |
| 311 | Users.insert(phi); |
| 312 | |
| 313 | for (std::set<PHINode*>::iterator SI = Users.begin(), SE = Users.end(); |
| 314 | SI != SE; ++SI) { |
| 315 | Value* InVal = (*SI)->getIncomingValueForBlock(LatchBlock); |
| 316 | if (isa<Instruction>(InVal)) |
| 317 | InVal = LastValueMap[InVal]; |
| 318 | (*SI)->removeIncomingValue(LatchBlock, false); |
Owen Anderson | 2035725 | 2006-08-25 22:13:55 +0000 | [diff] [blame] | 319 | if (InVal) |
| 320 | (*SI)->addIncoming(InVal, cast<BasicBlock>(LastValueMap[LatchBlock])); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 323 | |
| 324 | // Now loop over the PHI nodes in the original block, setting them to their |
| 325 | // incoming values. |
| 326 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 327 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 328 | PHINode *PN = OrigPHINode[i]; |
| 329 | PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 330 | Header->getInstList().erase(PN); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Insert the branches that link the different iterations together |
| 334 | for (unsigned i = 0; i < Latches.size()-1; ++i) { |
| 335 | new BranchInst(Headers[i+1], Latches[i]); |
| 336 | if(BasicBlock* Fold = FoldBlockIntoPredecessor(Headers[i+1])) { |
| 337 | std::replace(Latches.begin(), Latches.end(), Headers[i+1], Fold); |
| 338 | std::replace(Headers.begin(), Headers.end(), Headers[i+1], Fold); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Finally, add an unconditional branch to the block to continue into the exit |
| 343 | // block. |
| 344 | new BranchInst(LoopExit, Latches[Latches.size()-1]); |
| 345 | FoldBlockIntoPredecessor(LoopExit); |
| 346 | |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 347 | // At this point, the code is well formed. We now do a quick sweep over the |
| 348 | // inserted code, doing constant propagation and dead code elimination as we |
| 349 | // go. |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 350 | const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); |
| 351 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 352 | BBE = NewLoopBlocks.end(); BB != BBE; ++BB) |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 353 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) { |
| 354 | Instruction *Inst = I++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 355 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 356 | if (isInstructionTriviallyDead(Inst)) |
| 357 | (*BB)->getInstList().erase(Inst); |
| 358 | else if (Constant *C = ConstantFoldInstruction(Inst)) { |
| 359 | Inst->replaceAllUsesWith(C); |
| 360 | (*BB)->getInstList().erase(Inst); |
| 361 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 364 | // Update the loop information for this loop. |
| 365 | Loop *Parent = L->getParentLoop(); |
| 366 | |
| 367 | // Move all of the basic blocks in the loop into the parent loop. |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 368 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
| 369 | E = NewLoopBlocks.end(); BB != E; ++BB) |
| 370 | LI->changeLoopFor(*BB, Parent); |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 371 | |
| 372 | // Remove the loop from the parent. |
| 373 | if (Parent) |
| 374 | delete Parent->removeChildLoop(std::find(Parent->begin(), Parent->end(),L)); |
| 375 | else |
| 376 | delete LI->removeLoop(std::find(LI->begin(), LI->end(), L)); |
| 377 | |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 378 | ++NumUnrolled; |
| 379 | return true; |
| 380 | } |