Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 1 | //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 946b255 | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 946b255 | 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 | e001d81 | 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 | 946b255 | 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" |
Chris Lattner | 024f4ab | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/LoopInfo.h" |
| 27 | #include "llvm/Transforms/Utils/Cloning.h" |
| 28 | #include "llvm/Transforms/Utils/Local.h" |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" |
Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 6d048a0 | 2004-11-22 17:18:36 +0000 | [diff] [blame] | 36 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 37 | #include <cstdio> |
Reid Spencer | ce07833 | 2004-10-18 14:38:48 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 41 | STATISTIC(NumUnrolled, "Number of loops completely unrolled"); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 43 | namespace { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 44 | cl::opt<unsigned> |
Chris Lattner | d152502 | 2004-04-18 18:06:14 +0000 | [diff] [blame] | 45 | UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden, |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 46 | cl::desc("The cut-off point for loop unrolling")); |
| 47 | |
Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 48 | class VISIBILITY_HIDDEN LoopUnroll : public FunctionPass { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 49 | LoopInfo *LI; // The current loop information |
| 50 | public: |
| 51 | virtual bool runOnFunction(Function &F); |
| 52 | bool visitLoop(Loop *L); |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 53 | BasicBlock* FoldBlockIntoPredecessor(BasicBlock* BB); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 54 | |
| 55 | /// This transformation requires natural loop information & requires that |
| 56 | /// loop preheaders be inserted into the CFG... |
| 57 | /// |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 59 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 60 | AU.addRequiredID(LCSSAID); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 61 | AU.addRequired<LoopInfo>(); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 62 | AU.addPreservedID(LCSSAID); |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 63 | AU.addPreserved<LoopInfo>(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 64 | } |
| 65 | }; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 66 | RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops"); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | FunctionPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); } |
| 70 | |
| 71 | bool LoopUnroll::runOnFunction(Function &F) { |
| 72 | bool Changed = false; |
| 73 | LI = &getAnalysis<LoopInfo>(); |
| 74 | |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 75 | // Transform all the top-level loops. Copy the loop list so that the child |
| 76 | // can update the loop tree if it needs to delete the loop. |
| 77 | std::vector<Loop*> SubLoops(LI->begin(), LI->end()); |
| 78 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 79 | Changed |= visitLoop(SubLoops[i]); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 80 | |
| 81 | return Changed; |
| 82 | } |
| 83 | |
| 84 | /// ApproximateLoopSize - Approximate the size of the loop after it has been |
| 85 | /// unrolled. |
| 86 | static unsigned ApproximateLoopSize(const Loop *L) { |
| 87 | unsigned Size = 0; |
| 88 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) { |
| 89 | BasicBlock *BB = L->getBlocks()[i]; |
| 90 | Instruction *Term = BB->getTerminator(); |
| 91 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 92 | if (isa<PHINode>(I) && BB == L->getHeader()) { |
| 93 | // Ignore PHI nodes in the header. |
| 94 | } else if (I->hasOneUse() && I->use_back() == Term) { |
| 95 | // Ignore instructions only used by the loop terminator. |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 96 | } else if (isa<DbgInfoIntrinsic>(I)) { |
Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 97 | // Ignore debug instructions |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 98 | } else { |
| 99 | ++Size; |
| 100 | } |
| 101 | |
| 102 | // TODO: Ignore expressions derived from PHI and constants if inval of phi |
| 103 | // is a constant, or if operation is associative. This will get induction |
| 104 | // variables. |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return Size; |
| 109 | } |
| 110 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 111 | // RemapInstruction - Convert the instruction operands from referencing the |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 112 | // current values into those specified by ValueMap. |
| 113 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 114 | static inline void RemapInstruction(Instruction *I, |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 115 | DenseMap<const Value *, Value*> &ValueMap) { |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 116 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 117 | Value *Op = I->getOperand(op); |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 118 | DenseMap<const Value *, Value*>::iterator It = ValueMap.find(Op); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 119 | if (It != ValueMap.end()) Op = It->second; |
| 120 | I->setOperand(op, Op); |
| 121 | } |
| 122 | } |
| 123 | |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 124 | // FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it |
| 125 | // only has one predecessor, and that predecessor only has one successor. |
| 126 | // Returns the new combined block. |
| 127 | BasicBlock* LoopUnroll::FoldBlockIntoPredecessor(BasicBlock* BB) { |
| 128 | // Merge basic blocks into their predecessor if there is only one distinct |
| 129 | // pred, and if there is only one distinct successor of the predecessor, and |
| 130 | // if there are no PHI nodes. |
| 131 | // |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 132 | BasicBlock *OnlyPred = BB->getSinglePredecessor(); |
| 133 | if (!OnlyPred) return 0; |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 134 | |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 135 | if (OnlyPred->getTerminator()->getNumSuccessors() != 1) |
| 136 | return 0; |
| 137 | |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 138 | DOUT << "Merging: " << *BB << "into: " << *OnlyPred; |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 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 | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 151 | // Delete the unconditional branch from the predecessor... |
| 152 | OnlyPred->getInstList().pop_back(); |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 153 | |
Owen Anderson | a8a2e5c | 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 | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 156 | |
Owen Anderson | a8a2e5c | 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 | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 160 | |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 161 | std::string OldName = BB->getName(); |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 162 | |
Owen Anderson | a8a2e5c | 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 | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 166 | |
Owen Anderson | a8a2e5c | 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 | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 170 | |
Owen Anderson | a8a2e5c | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 171 | return OnlyPred; |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 946b255 | 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 | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 184 | BasicBlock* Header = L->getHeader(); |
| 185 | BasicBlock* LatchBlock = L->getLoopLatch(); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 186 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 187 | BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
Chris Lattner | 946b255 | 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 | 29fe20a | 2007-03-02 23:31:34 +0000 | [diff] [blame] | 193 | // Guard against huge trip counts. This also guards against assertions in |
| 194 | // APInt from the use of getZExtValue, below. |
| 195 | if (TripCountC->getValue().getActiveBits() > 32) |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 196 | return Changed; // More than 2^32 iterations??? |
| 197 | |
Reid Spencer | 29fe20a | 2007-03-02 23:31:34 +0000 | [diff] [blame] | 198 | uint64_t TripCountFull = TripCountC->getZExtValue(); |
| 199 | if (TripCountFull == 0) |
| 200 | return Changed; // Zero iteraitons? |
| 201 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 202 | unsigned LoopSize = ApproximateLoopSize(L); |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 203 | DOUT << "Loop Unroll: F[" << Header->getParent()->getName() |
| 204 | << "] Loop %" << Header->getName() << " Loop Size = " |
| 205 | << LoopSize << " Trip Count = " << TripCountFull << " - "; |
Chris Lattner | 47f395c | 2005-01-08 19:37:20 +0000 | [diff] [blame] | 206 | uint64_t Size = (uint64_t)LoopSize*TripCountFull; |
Chris Lattner | c12c945 | 2004-05-13 20:43:31 +0000 | [diff] [blame] | 207 | if (Size > UnrollThreshold) { |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 208 | DOUT << "TOO LARGE: " << Size << ">" << UnrollThreshold << "\n"; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 209 | return Changed; |
| 210 | } |
Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 211 | DOUT << "UNROLLING!\n"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 212 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 213 | std::vector<BasicBlock*> LoopBlocks = L->getBlocks(); |
| 214 | |
Chris Lattner | 47f395c | 2005-01-08 19:37:20 +0000 | [diff] [blame] | 215 | unsigned TripCount = (unsigned)TripCountFull; |
| 216 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 217 | BasicBlock *LoopExit = BI->getSuccessor(L->contains(BI->getSuccessor(0))); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 218 | |
| 219 | // For the first iteration of the loop, we should use the precloned values for |
| 220 | // PHI nodes. Insert associations now. |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 221 | DenseMap<const Value*, Value*> LastValueMap; |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 222 | std::vector<PHINode*> OrigPHINode; |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 223 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 224 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 225 | OrigPHINode.push_back(PN); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 226 | if (Instruction *I = |
| 227 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock))) |
| 228 | if (L->contains(I->getParent())) |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 229 | LastValueMap[I] = I; |
| 230 | } |
| 231 | |
| 232 | // Remove the exit branch from the loop |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 233 | LatchBlock->getInstList().erase(BI); |
| 234 | |
| 235 | std::vector<BasicBlock*> Headers; |
| 236 | std::vector<BasicBlock*> Latches; |
| 237 | Headers.push_back(Header); |
| 238 | Latches.push_back(LatchBlock); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 239 | |
| 240 | assert(TripCount != 0 && "Trip count of 0 is impossible!"); |
| 241 | for (unsigned It = 1; It != TripCount; ++It) { |
| 242 | char SuffixBuffer[100]; |
| 243 | sprintf(SuffixBuffer, ".%d", It); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 244 | |
| 245 | std::vector<BasicBlock*> NewBlocks; |
| 246 | |
| 247 | for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(), |
| 248 | E = LoopBlocks.end(); BB != E; ++BB) { |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 249 | DenseMap<const Value*, Value*> ValueMap; |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 250 | BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer); |
| 251 | Header->getParent()->getBasicBlockList().push_back(New); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 252 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 253 | // Loop over all of the PHI nodes in the block, changing them to use the |
| 254 | // incoming values from the previous block. |
| 255 | if (*BB == Header) |
| 256 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 257 | PHINode *NewPHI = cast<PHINode>(ValueMap[OrigPHINode[i]]); |
| 258 | Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); |
| 259 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) |
| 260 | if (It > 1 && L->contains(InValI->getParent())) |
| 261 | InVal = LastValueMap[InValI]; |
| 262 | ValueMap[OrigPHINode[i]] = InVal; |
| 263 | New->getInstList().erase(NewPHI); |
| 264 | } |
| 265 | |
| 266 | // Update our running map of newest clones |
| 267 | LastValueMap[*BB] = New; |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 268 | for (DenseMap<const Value*, Value*>::iterator VI = ValueMap.begin(), |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 269 | VE = ValueMap.end(); VI != VE; ++VI) |
| 270 | LastValueMap[VI->first] = VI->second; |
| 271 | |
| 272 | L->addBasicBlockToLoop(New, *LI); |
| 273 | |
| 274 | // Add phi entries for newly created values to all exit blocks except |
| 275 | // the successor of the latch block. The successor of the exit block will |
| 276 | // be updated specially after unrolling all the way. |
| 277 | if (*BB != LatchBlock) |
| 278 | for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end(); |
| 279 | UI != UE; ++UI) { |
| 280 | Instruction* UseInst = cast<Instruction>(*UI); |
| 281 | if (isa<PHINode>(UseInst) && !L->contains(UseInst->getParent())) { |
| 282 | PHINode* phi = cast<PHINode>(UseInst); |
| 283 | Value* Incoming = phi->getIncomingValueForBlock(*BB); |
| 284 | if (isa<Instruction>(Incoming)) |
| 285 | Incoming = LastValueMap[Incoming]; |
| 286 | |
| 287 | phi->addIncoming(Incoming, New); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Keep track of new headers and latches as we create them, so that |
| 292 | // we can insert the proper branches later. |
| 293 | if (*BB == Header) |
| 294 | Headers.push_back(New); |
| 295 | if (*BB == LatchBlock) |
| 296 | Latches.push_back(New); |
| 297 | |
| 298 | NewBlocks.push_back(New); |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 299 | } |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 300 | |
| 301 | // Remap all instructions in the most recent iteration |
| 302 | for (unsigned i = 0; i < NewBlocks.size(); ++i) |
| 303 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 304 | E = NewBlocks[i]->end(); I != E; ++I) |
Chris Lattner | 230bcb6 | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 305 | RemapInstruction(I, LastValueMap); |
Chris Lattner | 230bcb6 | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 306 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 307 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 308 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 309 | |
| 310 | // Update PHI nodes that reference the final latch block |
| 311 | if (TripCount > 1) { |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 312 | SmallPtrSet<PHINode*, 8> Users; |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 313 | for (Value::use_iterator UI = LatchBlock->use_begin(), |
| 314 | UE = LatchBlock->use_end(); UI != UE; ++UI) |
| 315 | if (PHINode* phi = dyn_cast<PHINode>(*UI)) |
| 316 | Users.insert(phi); |
| 317 | |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 318 | for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end(); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 319 | SI != SE; ++SI) { |
| 320 | Value* InVal = (*SI)->getIncomingValueForBlock(LatchBlock); |
| 321 | if (isa<Instruction>(InVal)) |
| 322 | InVal = LastValueMap[InVal]; |
| 323 | (*SI)->removeIncomingValue(LatchBlock, false); |
Owen Anderson | 403b95a | 2006-08-25 22:13:55 +0000 | [diff] [blame] | 324 | if (InVal) |
| 325 | (*SI)->addIncoming(InVal, cast<BasicBlock>(LastValueMap[LatchBlock])); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 328 | |
| 329 | // Now loop over the PHI nodes in the original block, setting them to their |
| 330 | // incoming values. |
| 331 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 332 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 333 | PHINode *PN = OrigPHINode[i]; |
| 334 | PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 335 | Header->getInstList().erase(PN); |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Insert the branches that link the different iterations together |
| 339 | for (unsigned i = 0; i < Latches.size()-1; ++i) { |
| 340 | new BranchInst(Headers[i+1], Latches[i]); |
| 341 | if(BasicBlock* Fold = FoldBlockIntoPredecessor(Headers[i+1])) { |
| 342 | std::replace(Latches.begin(), Latches.end(), Headers[i+1], Fold); |
| 343 | std::replace(Headers.begin(), Headers.end(), Headers[i+1], Fold); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Finally, add an unconditional branch to the block to continue into the exit |
| 348 | // block. |
| 349 | new BranchInst(LoopExit, Latches[Latches.size()-1]); |
| 350 | FoldBlockIntoPredecessor(LoopExit); |
| 351 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 352 | // At this point, the code is well formed. We now do a quick sweep over the |
| 353 | // inserted code, doing constant propagation and dead code elimination as we |
| 354 | // go. |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 355 | const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); |
| 356 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
Owen Anderson | 62c84fe | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 357 | BBE = NewLoopBlocks.end(); BB != BBE; ++BB) |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 358 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) { |
| 359 | Instruction *Inst = I++; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 360 | |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 361 | if (isInstructionTriviallyDead(Inst)) |
| 362 | (*BB)->getInstList().erase(Inst); |
| 363 | else if (Constant *C = ConstantFoldInstruction(Inst)) { |
| 364 | Inst->replaceAllUsesWith(C); |
| 365 | (*BB)->getInstList().erase(Inst); |
| 366 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 368 | |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 369 | // Update the loop information for this loop. |
| 370 | Loop *Parent = L->getParentLoop(); |
| 371 | |
| 372 | // Move all of the basic blocks in the loop into the parent loop. |
Owen Anderson | e001d81 | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 373 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
| 374 | E = NewLoopBlocks.end(); BB != E; ++BB) |
| 375 | LI->changeLoopFor(*BB, Parent); |
Chris Lattner | f2cc841 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 376 | |
| 377 | // Remove the loop from the parent. |
| 378 | if (Parent) |
| 379 | delete Parent->removeChildLoop(std::find(Parent->begin(), Parent->end(),L)); |
| 380 | else |
| 381 | delete LI->removeLoop(std::find(LI->begin(), LI->end(), L)); |
| 382 | |
Chris Lattner | 946b255 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 383 | ++NumUnrolled; |
| 384 | return true; |
| 385 | } |