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