LLVM API Documentation
00001 //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass implements a simple loop unroller. It works best when loops have 00011 // been canonicalized by the -indvars pass, allowing it to determine the trip 00012 // counts of loops easily. 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "loop-unroll" 00016 #include "llvm/IntrinsicInst.h" 00017 #include "llvm/Transforms/Scalar.h" 00018 #include "llvm/Analysis/LoopInfo.h" 00019 #include "llvm/Analysis/LoopPass.h" 00020 #include "llvm/Support/Compiler.h" 00021 #include "llvm/Support/CommandLine.h" 00022 #include "llvm/Support/Debug.h" 00023 #include "llvm/Transforms/Utils/UnrollLoop.h" 00024 #include <climits> 00025 00026 using namespace llvm; 00027 00028 static cl::opt<unsigned> 00029 UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden, 00030 cl::desc("The cut-off point for automatic loop unrolling")); 00031 00032 static cl::opt<unsigned> 00033 UnrollCount("unroll-count", cl::init(0), cl::Hidden, 00034 cl::desc("Use this unroll count for all loops, for testing purposes")); 00035 00036 static cl::opt<bool> 00037 UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, 00038 cl::desc("Allows loops to be partially unrolled until " 00039 "-unroll-threshold loop size is reached.")); 00040 00041 namespace { 00042 class VISIBILITY_HIDDEN LoopUnroll : public LoopPass { 00043 public: 00044 static char ID; // Pass ID, replacement for typeid 00045 LoopUnroll() : LoopPass(&ID) {} 00046 00047 /// A magic value for use with the Threshold parameter to indicate 00048 /// that the loop unroll should be performed regardless of how much 00049 /// code expansion would result. 00050 static const unsigned NoThreshold = UINT_MAX; 00051 00052 bool runOnLoop(Loop *L, LPPassManager &LPM); 00053 00054 /// This transformation requires natural loop information & requires that 00055 /// loop preheaders be inserted into the CFG... 00056 /// 00057 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00058 AU.addRequiredID(LoopSimplifyID); 00059 AU.addRequiredID(LCSSAID); 00060 AU.addRequired<LoopInfo>(); 00061 AU.addPreservedID(LCSSAID); 00062 AU.addPreserved<LoopInfo>(); 00063 // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info. 00064 // If loop unroll does not preserve dom info then LCSSA pass on next 00065 // loop will receive invalid dom info. 00066 // For now, recreate dom info, if loop is unrolled. 00067 AU.addPreserved<DominatorTree>(); 00068 AU.addPreserved<DominanceFrontier>(); 00069 } 00070 }; 00071 } 00072 00073 char LoopUnroll::ID = 0; 00074 static RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops"); 00075 00076 LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); } 00077 00078 /// ApproximateLoopSize - Approximate the size of the loop. 00079 static unsigned ApproximateLoopSize(const Loop *L) { 00080 unsigned Size = 0; 00081 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 00082 I != E; ++I) { 00083 BasicBlock *BB = *I; 00084 Instruction *Term = BB->getTerminator(); 00085 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 00086 if (isa<PHINode>(I) && BB == L->getHeader()) { 00087 // Ignore PHI nodes in the header. 00088 } else if (I->hasOneUse() && I->use_back() == Term) { 00089 // Ignore instructions only used by the loop terminator. 00090 } else if (isa<DbgInfoIntrinsic>(I)) { 00091 // Ignore debug instructions 00092 } else if (isa<CallInst>(I)) { 00093 // Estimate size overhead introduced by call instructions which 00094 // is higher than other instructions. Here 3 and 10 are magic 00095 // numbers that help one isolated test case from PR2067 without 00096 // negatively impacting measured benchmarks. 00097 if (isa<IntrinsicInst>(I)) 00098 Size = Size + 3; 00099 else 00100 Size = Size + 10; 00101 } else { 00102 ++Size; 00103 } 00104 00105 // TODO: Ignore expressions derived from PHI and constants if inval of phi 00106 // is a constant, or if operation is associative. This will get induction 00107 // variables. 00108 } 00109 } 00110 00111 return Size; 00112 } 00113 00114 bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) { 00115 assert(L->isLCSSAForm()); 00116 LoopInfo *LI = &getAnalysis<LoopInfo>(); 00117 00118 BasicBlock *Header = L->getHeader(); 00119 DOUT << "Loop Unroll: F[" << Header->getParent()->getName() 00120 << "] Loop %" << Header->getName() << "\n"; 00121 00122 // Find trip count 00123 unsigned TripCount = L->getSmallConstantTripCount(); 00124 unsigned Count = UnrollCount; 00125 00126 // Automatically select an unroll count. 00127 if (Count == 0) { 00128 // Conservative heuristic: if we know the trip count, see if we can 00129 // completely unroll (subject to the threshold, checked below); otherwise 00130 // try to find greatest modulo of the trip count which is still under 00131 // threshold value. 00132 if (TripCount != 0) { 00133 Count = TripCount; 00134 } else { 00135 return false; 00136 } 00137 } 00138 00139 // Enforce the threshold. 00140 if (UnrollThreshold != NoThreshold) { 00141 unsigned LoopSize = ApproximateLoopSize(L); 00142 DOUT << " Loop Size = " << LoopSize << "\n"; 00143 uint64_t Size = (uint64_t)LoopSize*Count; 00144 if (TripCount != 1 && Size > UnrollThreshold) { 00145 DOUT << " Too large to fully unroll with count: " << Count 00146 << " because size: " << Size << ">" << UnrollThreshold << "\n"; 00147 if (UnrollAllowPartial) { 00148 // Reduce unroll count to be modulo of TripCount for partial unrolling 00149 Count = UnrollThreshold / LoopSize; 00150 while (Count != 0 && TripCount%Count != 0) { 00151 Count--; 00152 } 00153 if (Count < 2) { 00154 DOUT << " could not unroll partially\n"; 00155 return false; 00156 } else { 00157 DOUT << " partially unrolling with count: " << Count << "\n"; 00158 } 00159 } else { 00160 DOUT << " will not try to unroll partially because " 00161 << "-unroll-allow-partial not given\n"; 00162 return false; 00163 } 00164 } 00165 } 00166 00167 // Unroll the loop. 00168 Function *F = L->getHeader()->getParent(); 00169 if (!UnrollLoop(L, Count, LI, &LPM)) 00170 return false; 00171 00172 // FIXME: Reconstruct dom info, because it is not preserved properly. 00173 DominatorTree *DT = getAnalysisToUpdate<DominatorTree>(); 00174 if (DT) { 00175 DT->runOnFunction(*F); 00176 DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>(); 00177 if (DF) 00178 DF->runOnFunction(*F); 00179 } 00180 return true; 00181 }