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