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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 64613ea | 2004-11-22 17:18:36 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" |
Reid Spencer | 17e6e44 | 2004-10-18 14:38:48 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 40 | #include <climits> |
| 41 | #include <cstdio> |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 44 | STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled"); |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 45 | STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)"); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 48 | cl::opt<unsigned> |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 49 | UnrollThreshold |
| 50 | ("unroll-threshold", cl::init(100), cl::Hidden, |
| 51 | cl::desc("The cut-off point for automatic loop unrolling")); |
| 52 | |
| 53 | cl::opt<unsigned> |
| 54 | UnrollCount |
| 55 | ("unroll-count", cl::init(0), cl::Hidden, |
| 56 | cl::desc("Use this unroll count for all loops, for testing purposes")); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 57 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 58 | class VISIBILITY_HIDDEN LoopUnroll : public LoopPass { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 59 | LoopInfo *LI; // The current loop information |
| 60 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 61 | static char ID; // Pass ID, replacement for typeid |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 62 | LoopUnroll() : LoopPass((intptr_t)&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 63 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 64 | /// A magic value for use with the Threshold parameter to indicate |
| 65 | /// that the loop unroll should be performed regardless of how much |
| 66 | /// code expansion would result. |
| 67 | static const unsigned NoThreshold = UINT_MAX; |
| 68 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 69 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 70 | bool unrollLoop(Loop *L, unsigned Count, unsigned Threshold); |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 71 | BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 72 | |
| 73 | /// This transformation requires natural loop information & requires that |
| 74 | /// loop preheaders be inserted into the CFG... |
| 75 | /// |
| 76 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 77 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 78 | AU.addRequiredID(LCSSAID); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 79 | AU.addRequired<LoopInfo>(); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 80 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 9c2cc46 | 2004-04-18 05:38:37 +0000 | [diff] [blame] | 81 | AU.addPreserved<LoopInfo>(); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 84 | char LoopUnroll::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 85 | RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops"); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 88 | LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 89 | |
Dan Gohman | f742c23 | 2007-05-08 15:14:19 +0000 | [diff] [blame] | 90 | /// ApproximateLoopSize - Approximate the size of the loop. |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 91 | static unsigned ApproximateLoopSize(const Loop *L) { |
| 92 | unsigned Size = 0; |
| 93 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) { |
| 94 | BasicBlock *BB = L->getBlocks()[i]; |
| 95 | Instruction *Term = BB->getTerminator(); |
| 96 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 97 | if (isa<PHINode>(I) && BB == L->getHeader()) { |
| 98 | // Ignore PHI nodes in the header. |
| 99 | } else if (I->hasOneUse() && I->use_back() == Term) { |
| 100 | // Ignore instructions only used by the loop terminator. |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 101 | } else if (isa<DbgInfoIntrinsic>(I)) { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 102 | // Ignore debug instructions |
Devang Patel | b176038 | 2008-03-17 23:41:20 +0000 | [diff] [blame] | 103 | } else if (isa<CallInst>(I)) { |
Devang Patel | ac3844d | 2008-03-19 23:05:52 +0000 | [diff] [blame] | 104 | // Estimate size overhead introduced by call instructions which |
| 105 | // is higher than other instructions. Here 3 and 10 are magic |
| 106 | // numbers that help one isolated test case from PR2067 without |
| 107 | // negatively impacting measured benchmarks. |
Devang Patel | b176038 | 2008-03-17 23:41:20 +0000 | [diff] [blame] | 108 | if (isa<IntrinsicInst>(I)) |
| 109 | Size = Size + 3; |
| 110 | else |
| 111 | Size = Size + 10; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 112 | } else { |
| 113 | ++Size; |
| 114 | } |
| 115 | |
| 116 | // TODO: Ignore expressions derived from PHI and constants if inval of phi |
| 117 | // is a constant, or if operation is associative. This will get induction |
| 118 | // variables. |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return Size; |
| 123 | } |
| 124 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 125 | // RemapInstruction - Convert the instruction operands from referencing the |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 126 | // current values into those specified by ValueMap. |
| 127 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 128 | static inline void RemapInstruction(Instruction *I, |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 129 | DenseMap<const Value *, Value*> &ValueMap) { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 130 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 131 | Value *Op = I->getOperand(op); |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 132 | DenseMap<const Value *, Value*>::iterator It = ValueMap.find(Op); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 133 | if (It != ValueMap.end()) Op = It->second; |
| 134 | I->setOperand(op, Op); |
| 135 | } |
| 136 | } |
| 137 | |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 138 | // FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it |
| 139 | // only has one predecessor, and that predecessor only has one successor. |
| 140 | // Returns the new combined block. |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 141 | BasicBlock *LoopUnroll::FoldBlockIntoPredecessor(BasicBlock *BB) { |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 142 | // Merge basic blocks into their predecessor if there is only one distinct |
| 143 | // pred, and if there is only one distinct successor of the predecessor, and |
| 144 | // if there are no PHI nodes. |
| 145 | // |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 146 | BasicBlock *OnlyPred = BB->getSinglePredecessor(); |
| 147 | if (!OnlyPred) return 0; |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 148 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 149 | if (OnlyPred->getTerminator()->getNumSuccessors() != 1) |
| 150 | return 0; |
| 151 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 152 | DOUT << "Merging: " << *BB << "into: " << *OnlyPred; |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 153 | |
| 154 | // Resolve any PHI nodes at the start of the block. They are all |
| 155 | // guaranteed to have exactly one entry if they exist, unless there are |
| 156 | // multiple duplicate (but guaranteed to be equal) entries for the |
| 157 | // incoming edges. This occurs when there are multiple edges from |
| 158 | // OnlyPred to OnlySucc. |
| 159 | // |
| 160 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 161 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 162 | BB->getInstList().pop_front(); // Delete the phi node... |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 165 | // Delete the unconditional branch from the predecessor... |
| 166 | OnlyPred->getInstList().pop_back(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 167 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 168 | // Move all definitions in the successor to the predecessor... |
| 169 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
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 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 172 | // source... |
| 173 | BB->replaceAllUsesWith(OnlyPred); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 174 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 175 | std::string OldName = BB->getName(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 176 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 177 | // Erase basic block from the function... |
| 178 | LI->removeBlock(BB); |
| 179 | BB->eraseFromParent(); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 180 | |
Dan Gohman | d9efdb2 | 2007-05-14 14:31:17 +0000 | [diff] [blame] | 181 | // Inherit predecessor's name if it exists... |
Owen Anderson | 6bc41e8 | 2008-04-14 17:38:21 +0000 | [diff] [blame^] | 182 | if (!OldName.empty() && !OnlyPred->hasName()) |
| 183 | OnlyPred->setName(OldName); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 184 | |
Owen Anderson | d648e14 | 2006-08-29 06:10:56 +0000 | [diff] [blame] | 185 | return OnlyPred; |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 188 | bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Devang Patel | 3f1a1e0 | 2007-03-07 01:38:05 +0000 | [diff] [blame] | 189 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 190 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 191 | // Unroll the loop. |
| 192 | if (!unrollLoop(L, UnrollCount, UnrollThreshold)) |
| 193 | return false; |
| 194 | |
| 195 | // Update the loop information for this loop. |
| 196 | // If we completely unrolled the loop, remove it from the parent. |
| 197 | if (L->getNumBackEdges() == 0) |
| 198 | LPM.deleteLoopFromQueue(L); |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | /// Unroll the given loop by UnrollCount, or by a heuristically-determined |
Dan Gohman | d9efdb2 | 2007-05-14 14:31:17 +0000 | [diff] [blame] | 204 | /// value if Count is zero. If Threshold is not NoThreshold, it is a value |
| 205 | /// to limit code size expansion. If the loop size would expand beyond the |
| 206 | /// threshold value, unrolling is suppressed. The return value is true if |
| 207 | /// any transformations are performed. |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 208 | /// |
| 209 | bool LoopUnroll::unrollLoop(Loop *L, unsigned Count, unsigned Threshold) { |
| 210 | assert(L->isLCSSAForm()); |
| 211 | |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 212 | BasicBlock *Header = L->getHeader(); |
| 213 | BasicBlock *LatchBlock = L->getLoopLatch(); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 214 | BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 215 | |
Bill Wendling | b742703 | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 216 | DOUT << "Loop Unroll: F[" << Header->getParent()->getName() |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 217 | << "] Loop %" << Header->getName() << "\n"; |
| 218 | |
| 219 | if (!BI || BI->isUnconditional()) { |
Wojciech Matyjewicz | 21a715a | 2008-01-04 20:02:18 +0000 | [diff] [blame] | 220 | // The loop-rotate pass can be helpful to avoid this in many cases. |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 221 | DOUT << " Can't unroll; loop not terminated by a conditional branch.\n"; |
| 222 | return false; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 223 | } |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 224 | |
| 225 | // Determine the trip count and/or trip multiple. A TripCount value of zero |
| 226 | // is used to mean an unknown trip count. The TripMultiple value is the |
| 227 | // greatest known integer multiple of the trip count. |
| 228 | unsigned TripCount = 0; |
| 229 | unsigned TripMultiple = 1; |
| 230 | if (Value *TripCountValue = L->getTripCount()) { |
| 231 | if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCountValue)) { |
| 232 | // Guard against huge trip counts. This also guards against assertions in |
| 233 | // APInt from the use of getZExtValue, below. |
| 234 | if (TripCountC->getValue().getActiveBits() <= 32) { |
| 235 | TripCount = (unsigned)TripCountC->getZExtValue(); |
| 236 | } |
| 237 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCountValue)) { |
| 238 | switch (BO->getOpcode()) { |
| 239 | case BinaryOperator::Mul: |
| 240 | if (ConstantInt *MultipleC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 241 | if (MultipleC->getValue().getActiveBits() <= 32) { |
| 242 | TripMultiple = (unsigned)MultipleC->getZExtValue(); |
| 243 | } |
| 244 | } |
| 245 | break; |
| 246 | default: break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | if (TripCount != 0) |
| 251 | DOUT << " Trip Count = " << TripCount << "\n"; |
| 252 | if (TripMultiple != 1) |
| 253 | DOUT << " Trip Multiple = " << TripMultiple << "\n"; |
| 254 | |
| 255 | // Automatically select an unroll count. |
| 256 | if (Count == 0) { |
| 257 | // Conservative heuristic: if we know the trip count, see if we can |
| 258 | // completely unroll (subject to the threshold, checked below); otherwise |
| 259 | // don't unroll. |
| 260 | if (TripCount != 0) { |
| 261 | Count = TripCount; |
| 262 | } else { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Effectively "DCE" unrolled iterations that are beyond the tripcount |
| 268 | // and will never be executed. |
| 269 | if (TripCount != 0 && Count > TripCount) |
| 270 | Count = TripCount; |
| 271 | |
| 272 | assert(Count > 0); |
| 273 | assert(TripMultiple > 0); |
| 274 | assert(TripCount == 0 || TripCount % TripMultiple == 0); |
| 275 | |
| 276 | // Enforce the threshold. |
| 277 | if (Threshold != NoThreshold) { |
| 278 | unsigned LoopSize = ApproximateLoopSize(L); |
| 279 | DOUT << " Loop Size = " << LoopSize << "\n"; |
| 280 | uint64_t Size = (uint64_t)LoopSize*Count; |
| 281 | if (TripCount != 1 && Size > Threshold) { |
| 282 | DOUT << " TOO LARGE TO UNROLL: " |
| 283 | << Size << ">" << Threshold << "\n"; |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Are we eliminating the loop control altogether? |
| 289 | bool CompletelyUnroll = Count == TripCount; |
| 290 | |
| 291 | // If we know the trip count, we know the multiple... |
| 292 | unsigned BreakoutTrip = 0; |
| 293 | if (TripCount != 0) { |
| 294 | BreakoutTrip = TripCount % Count; |
| 295 | TripMultiple = 0; |
| 296 | } else { |
| 297 | // Figure out what multiple to use. |
| 298 | BreakoutTrip = TripMultiple = |
| 299 | (unsigned)GreatestCommonDivisor64(Count, TripMultiple); |
| 300 | } |
| 301 | |
| 302 | if (CompletelyUnroll) { |
| 303 | DOUT << "COMPLETELY UNROLLING loop %" << Header->getName() |
| 304 | << " with trip count " << TripCount << "!\n"; |
| 305 | } else { |
| 306 | DOUT << "UNROLLING loop %" << Header->getName() |
| 307 | << " by " << Count; |
| 308 | if (TripMultiple == 0 || BreakoutTrip != TripMultiple) { |
| 309 | DOUT << " with a breakout at trip " << BreakoutTrip; |
| 310 | } else if (TripMultiple != 1) { |
| 311 | DOUT << " with " << TripMultiple << " trips per branch"; |
| 312 | } |
| 313 | DOUT << "!\n"; |
| 314 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 315 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 316 | std::vector<BasicBlock*> LoopBlocks = L->getBlocks(); |
| 317 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 318 | bool ContinueOnTrue = L->contains(BI->getSuccessor(0)); |
| 319 | BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 320 | |
| 321 | // For the first iteration of the loop, we should use the precloned values for |
| 322 | // PHI nodes. Insert associations now. |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 323 | typedef DenseMap<const Value*, Value*> ValueMapTy; |
| 324 | ValueMapTy LastValueMap; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 325 | std::vector<PHINode*> OrigPHINode; |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 326 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 327 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 328 | OrigPHINode.push_back(PN); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 329 | if (Instruction *I = |
| 330 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock))) |
| 331 | if (L->contains(I->getParent())) |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 332 | LastValueMap[I] = I; |
| 333 | } |
| 334 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 335 | std::vector<BasicBlock*> Headers; |
| 336 | std::vector<BasicBlock*> Latches; |
| 337 | Headers.push_back(Header); |
| 338 | Latches.push_back(LatchBlock); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 339 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 340 | for (unsigned It = 1; It != Count; ++It) { |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 341 | char SuffixBuffer[100]; |
| 342 | sprintf(SuffixBuffer, ".%d", It); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 343 | |
| 344 | std::vector<BasicBlock*> NewBlocks; |
| 345 | |
| 346 | for (std::vector<BasicBlock*>::iterator BB = LoopBlocks.begin(), |
| 347 | E = LoopBlocks.end(); BB != E; ++BB) { |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 348 | ValueMapTy ValueMap; |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 349 | BasicBlock *New = CloneBasicBlock(*BB, ValueMap, SuffixBuffer); |
| 350 | Header->getParent()->getBasicBlockList().push_back(New); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 351 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 352 | // Loop over all of the PHI nodes in the block, changing them to use the |
| 353 | // incoming values from the previous block. |
| 354 | if (*BB == Header) |
| 355 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 356 | PHINode *NewPHI = cast<PHINode>(ValueMap[OrigPHINode[i]]); |
| 357 | Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); |
| 358 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) |
| 359 | if (It > 1 && L->contains(InValI->getParent())) |
| 360 | InVal = LastValueMap[InValI]; |
| 361 | ValueMap[OrigPHINode[i]] = InVal; |
| 362 | New->getInstList().erase(NewPHI); |
| 363 | } |
| 364 | |
| 365 | // Update our running map of newest clones |
| 366 | LastValueMap[*BB] = New; |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 367 | for (ValueMapTy::iterator VI = ValueMap.begin(), VE = ValueMap.end(); |
| 368 | VI != VE; ++VI) |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 369 | LastValueMap[VI->first] = VI->second; |
| 370 | |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 371 | L->addBasicBlockToLoop(New, LI->getBase()); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 372 | |
| 373 | // Add phi entries for newly created values to all exit blocks except |
| 374 | // the successor of the latch block. The successor of the exit block will |
| 375 | // be updated specially after unrolling all the way. |
| 376 | if (*BB != LatchBlock) |
| 377 | for (Value::use_iterator UI = (*BB)->use_begin(), UE = (*BB)->use_end(); |
Nick Lewycky | c837abe | 2008-01-27 18:35:00 +0000 | [diff] [blame] | 378 | UI != UE;) { |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 379 | Instruction *UseInst = cast<Instruction>(*UI); |
Nick Lewycky | c837abe | 2008-01-27 18:35:00 +0000 | [diff] [blame] | 380 | ++UI; |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 381 | if (isa<PHINode>(UseInst) && !L->contains(UseInst->getParent())) { |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 382 | PHINode *phi = cast<PHINode>(UseInst); |
| 383 | Value *Incoming = phi->getIncomingValueForBlock(*BB); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 384 | phi->addIncoming(Incoming, New); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // Keep track of new headers and latches as we create them, so that |
| 389 | // we can insert the proper branches later. |
| 390 | if (*BB == Header) |
| 391 | Headers.push_back(New); |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 392 | if (*BB == LatchBlock) { |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 393 | Latches.push_back(New); |
| 394 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 395 | // Also, clear out the new latch's back edge so that it doesn't look |
| 396 | // like a new loop, so that it's amenable to being merged with adjacent |
| 397 | // blocks later on. |
| 398 | TerminatorInst *Term = New->getTerminator(); |
| 399 | assert(L->contains(Term->getSuccessor(!ContinueOnTrue))); |
| 400 | assert(Term->getSuccessor(ContinueOnTrue) == LoopExit); |
| 401 | Term->setSuccessor(!ContinueOnTrue, NULL); |
| 402 | } |
| 403 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 404 | NewBlocks.push_back(New); |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 405 | } |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 406 | |
| 407 | // Remap all instructions in the most recent iteration |
Nick Lewycky | 529de8a | 2008-03-09 05:24:34 +0000 | [diff] [blame] | 408 | for (unsigned i = 0; i < NewBlocks.size(); ++i) { |
| 409 | BasicBlock *NB = NewBlocks[i]; |
| 410 | if (BasicBlock *UnwindDest = NB->getUnwindDest()) |
| 411 | NB->setUnwindDest(cast<BasicBlock>(LastValueMap[UnwindDest])); |
| 412 | |
| 413 | for (BasicBlock::iterator I = NB->begin(), E = NB->end(); I != E; ++I) |
Chris Lattner | 998f44f | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 414 | RemapInstruction(I, LastValueMap); |
Nick Lewycky | 529de8a | 2008-03-09 05:24:34 +0000 | [diff] [blame] | 415 | } |
Chris Lattner | 998f44f | 2004-04-18 17:32:39 +0000 | [diff] [blame] | 416 | } |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 417 | |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 418 | // The latch block exits the loop. If there are any PHI nodes in the |
| 419 | // successor blocks, update them to use the appropriate values computed as the |
| 420 | // last iteration of the loop. |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 421 | if (Count != 1) { |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 422 | SmallPtrSet<PHINode*, 8> Users; |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 423 | for (Value::use_iterator UI = LatchBlock->use_begin(), |
| 424 | UE = LatchBlock->use_end(); UI != UE; ++UI) |
Dan Gohman | 1cc0096 | 2007-05-08 15:19:19 +0000 | [diff] [blame] | 425 | if (PHINode *phi = dyn_cast<PHINode>(*UI)) |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 426 | Users.insert(phi); |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 427 | |
| 428 | BasicBlock *LastIterationBB = cast<BasicBlock>(LastValueMap[LatchBlock]); |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 429 | for (SmallPtrSet<PHINode*,8>::iterator SI = Users.begin(), SE = Users.end(); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 430 | SI != SE; ++SI) { |
Chris Lattner | a0c54f3 | 2007-05-05 18:36:36 +0000 | [diff] [blame] | 431 | PHINode *PN = *SI; |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 432 | Value *InVal = PN->removeIncomingValue(LatchBlock, false); |
| 433 | // If this value was defined in the loop, take the value defined by the |
| 434 | // last iteration of the loop. |
| 435 | if (Instruction *InValI = dyn_cast<Instruction>(InVal)) { |
| 436 | if (L->contains(InValI->getParent())) |
| 437 | InVal = LastValueMap[InVal]; |
Devang Patel | ac58516 | 2007-04-16 23:03:45 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | caccc99 | 2007-05-05 18:49:57 +0000 | [diff] [blame] | 439 | PN->addIncoming(InVal, LastIterationBB); |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 442 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 443 | // Now, if we're doing complete unrolling, loop over the PHI nodes in the |
| 444 | // original block, setting them to their incoming values. |
| 445 | if (CompletelyUnroll) { |
| 446 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 447 | for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) { |
| 448 | PHINode *PN = OrigPHINode[i]; |
| 449 | PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); |
| 450 | Header->getInstList().erase(PN); |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 453 | |
| 454 | // Now that all the basic blocks for the unrolled iterations are in place, |
| 455 | // set up the branches to connect them. |
| 456 | for (unsigned i = 0, e = Latches.size(); i != e; ++i) { |
| 457 | // The original branch was replicated in each unrolled iteration. |
| 458 | BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); |
| 459 | |
| 460 | // The branch destination. |
| 461 | unsigned j = (i + 1) % e; |
| 462 | BasicBlock *Dest = Headers[j]; |
| 463 | bool NeedConditional = true; |
| 464 | |
| 465 | // For a complete unroll, make the last iteration end with a branch |
| 466 | // to the exit block. |
| 467 | if (CompletelyUnroll && j == 0) { |
| 468 | Dest = LoopExit; |
| 469 | NeedConditional = false; |
| 470 | } |
| 471 | |
| 472 | // If we know the trip count or a multiple of it, we can safely use an |
| 473 | // unconditional branch for some iterations. |
| 474 | if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) { |
| 475 | NeedConditional = false; |
| 476 | } |
| 477 | |
| 478 | if (NeedConditional) { |
| 479 | // Update the conditional branch's successor for the following |
| 480 | // iteration. |
| 481 | Term->setSuccessor(!ContinueOnTrue, Dest); |
| 482 | } else { |
| 483 | Term->setUnconditionalDest(Dest); |
| 484 | // Merge adjacent basic blocks, if possible. |
| 485 | if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest)) { |
| 486 | std::replace(Latches.begin(), Latches.end(), Dest, Fold); |
| 487 | std::replace(Headers.begin(), Headers.end(), Dest, Fold); |
| 488 | } |
| 489 | } |
| 490 | } |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 492 | // At this point, the code is well formed. We now do a quick sweep over the |
| 493 | // inserted code, doing constant propagation and dead code elimination as we |
| 494 | // go. |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 495 | const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); |
| 496 | for (std::vector<BasicBlock*>::const_iterator BB = NewLoopBlocks.begin(), |
Owen Anderson | 59312b1 | 2006-08-28 02:09:46 +0000 | [diff] [blame] | 497 | BBE = NewLoopBlocks.end(); BB != BBE; ++BB) |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 498 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ) { |
| 499 | Instruction *Inst = I++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 500 | |
Owen Anderson | 3b53c4e | 2006-08-24 21:28:19 +0000 | [diff] [blame] | 501 | if (isInstructionTriviallyDead(Inst)) |
| 502 | (*BB)->getInstList().erase(Inst); |
| 503 | else if (Constant *C = ConstantFoldInstruction(Inst)) { |
| 504 | Inst->replaceAllUsesWith(C); |
| 505 | (*BB)->getInstList().erase(Inst); |
| 506 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 508 | |
Dan Gohman | c767844 | 2007-05-11 20:53:41 +0000 | [diff] [blame] | 509 | NumCompletelyUnrolled += CompletelyUnroll; |
Chris Lattner | 83bf288 | 2004-04-18 05:20:17 +0000 | [diff] [blame] | 510 | ++NumUnrolled; |
| 511 | return true; |
| 512 | } |