1 //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 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 //===----------------------------------------------------------------------===// 14 15 #define DEBUG_TYPE "loop-unroll" 16 #include "llvm/IntrinsicInst.h" 17 #include "llvm/Transforms/Scalar.h" 18 #include "llvm/Analysis/LoopPass.h" 19 #include "llvm/Analysis/InlineCost.h" 20 #include "llvm/Analysis/ScalarEvolution.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include "llvm/Transforms/Utils/UnrollLoop.h" 25 #include <climits> 26 27 using namespace llvm; 28 29 static cl::opt<unsigned> 30 UnrollThreshold("unroll-threshold", cl::init(200), cl::Hidden, 31 cl::desc("The cut-off point for automatic loop unrolling")); 32 33 static cl::opt<unsigned> 34 UnrollCount("unroll-count", cl::init(0), cl::Hidden, 35 cl::desc("Use this unroll count for all loops, for testing purposes")); 36 37 static cl::opt<bool> 38 UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, 39 cl::desc("Allows loops to be partially unrolled until " 40 "-unroll-threshold loop size is reached.")); 41 42 namespace { 43 class LoopUnroll : public LoopPass { 44 public: 45 static char ID; // Pass ID, replacement for typeid 46 LoopUnroll() : LoopPass(ID) {} 47 48 /// A magic value for use with the Threshold parameter to indicate 49 /// that the loop unroll should be performed regardless of how much 50 /// code expansion would result. 51 static const unsigned NoThreshold = UINT_MAX; 52 53 bool runOnLoop(Loop *L, LPPassManager &LPM); 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 { 59 AU.addRequired<LoopInfo>(); 60 AU.addPreserved<LoopInfo>(); 61 AU.addRequiredID(LoopSimplifyID); 62 AU.addPreservedID(LoopSimplifyID); 63 AU.addRequiredID(LCSSAID); 64 AU.addPreservedID(LCSSAID); 65 AU.addPreserved<ScalarEvolution>(); 66 // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. 67 // If loop unroll does not preserve dom info then LCSSA pass on next 68 // loop will receive invalid dom info. 69 // For now, recreate dom info, if loop is unrolled. 70 AU.addPreserved<DominatorTree>(); 71 } 72 }; 73 } 74 75 char LoopUnroll::ID = 0; 76 INITIALIZE_PASS(LoopUnroll, "loop-unroll", "Unroll loops", false, false); 77 78 Pass *llvm::createLoopUnrollPass() { return new LoopUnroll(); } 79 80 /// ApproximateLoopSize - Approximate the size of the loop. 81 static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls) { 82 CodeMetrics Metrics; 83 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 84 I != E; ++I) 85 Metrics.analyzeBasicBlock(*I); 86 NumCalls = Metrics.NumCalls; 87 return Metrics.NumInsts; 88 } 89 90 bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { 91 LoopInfo *LI = &getAnalysis<LoopInfo>(); 92 93 BasicBlock *Header = L->getHeader(); 94 DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName() 95 << "] Loop %" << Header->getName() << "\n"); 96 (void)Header; 97 98 // Find trip count 99 unsigned TripCount = L->getSmallConstantTripCount(); 100 unsigned Count = UnrollCount; 101 102 // Automatically select an unroll count. 103 if (Count == 0) { 104 // Conservative heuristic: if we know the trip count, see if we can 105 // completely unroll (subject to the threshold, checked below); otherwise 106 // try to find greatest modulo of the trip count which is still under 107 // threshold value. 108 if (TripCount == 0) 109 return false; 110 Count = TripCount; 111 } 112 113 // Enforce the threshold. 114 if (UnrollThreshold != NoThreshold) { 115 unsigned NumCalls; 116 unsigned LoopSize = ApproximateLoopSize(L, NumCalls); 117 DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n"); 118 if (NumCalls != 0) { 119 DEBUG(dbgs() << " Not unrolling loop with function calls.\n"); 120 return false; 121 } 122 uint64_t Size = (uint64_t)LoopSize*Count; 123 if (TripCount != 1 && Size > UnrollThreshold) { 124 DEBUG(dbgs() << " Too large to fully unroll with count: " << Count 125 << " because size: " << Size << ">" << UnrollThreshold << "\n"); 126 if (!UnrollAllowPartial) { 127 DEBUG(dbgs() << " will not try to unroll partially because " 128 << "-unroll-allow-partial not given\n"); 129 return false; 130 } 131 // Reduce unroll count to be modulo of TripCount for partial unrolling 132 Count = UnrollThreshold / LoopSize; 133 while (Count != 0 && TripCount%Count != 0) { 134 Count--; 135 } 136 if (Count < 2) { 137 DEBUG(dbgs() << " could not unroll partially\n"); 138 return false; 139 } 140 DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n"); 141 } 142 } 143 144 // Unroll the loop. 145 Function *F = L->getHeader()->getParent(); 146 if (!UnrollLoop(L, Count, LI, &LPM)) 147 return false; 148 149 // FIXME: Reconstruct dom info, because it is not preserved properly. 150 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) 151 DT->runOnFunction(*F); 152 return true; 153 } 154