1 //===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
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"
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"));
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"));
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."));
43 class LoopUnroll : public LoopPass {
45 static char ID; // Pass ID, replacement for typeid
46 LoopUnroll() : LoopPass(ID) {}
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;
53 bool runOnLoop(Loop *L, LPPassManager &LPM);
55 /// This transformation requires natural loop information & requires that
56 /// loop preheaders be inserted into the CFG...
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 // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info.
66 // If loop unroll does not preserve dom info then LCSSA pass on next
67 // loop will receive invalid dom info.
68 // For now, recreate dom info, if loop is unrolled.
69 AU.addPreserved<DominatorTree>();
70 AU.addPreserved<DominanceFrontier>();
71 AU.addPreserved<ScalarEvolution>();
76 char LoopUnroll::ID = 0;
77 INITIALIZE_PASS(LoopUnroll, "loop-unroll", "Unroll loops", false, false);
79 Pass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
81 /// ApproximateLoopSize - Approximate the size of the loop.
82 static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls) {
84 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
86 Metrics.analyzeBasicBlock(*I);
87 NumCalls = Metrics.NumCalls;
88 return Metrics.NumInsts;
91 bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
92 LoopInfo *LI = &getAnalysis<LoopInfo>();
94 BasicBlock *Header = L->getHeader();
95 DEBUG(dbgs() << "Loop Unroll: F[" << Header->getParent()->getName()
96 << "] Loop %" << Header->getName() << "\n");
100 unsigned TripCount = L->getSmallConstantTripCount();
101 unsigned Count = UnrollCount;
103 // Automatically select an unroll count.
105 // Conservative heuristic: if we know the trip count, see if we can
106 // completely unroll (subject to the threshold, checked below); otherwise
107 // try to find greatest modulo of the trip count which is still under
114 // Enforce the threshold.
115 if (UnrollThreshold != NoThreshold) {
117 unsigned LoopSize = ApproximateLoopSize(L, NumCalls);
118 DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
120 DEBUG(dbgs() << " Not unrolling loop with function calls.\n");
123 uint64_t Size = (uint64_t)LoopSize*Count;
124 if (TripCount != 1 && Size > UnrollThreshold) {
125 DEBUG(dbgs() << " Too large to fully unroll with count: " << Count
126 << " because size: " << Size << ">" << UnrollThreshold << "\n");
127 if (!UnrollAllowPartial) {
128 DEBUG(dbgs() << " will not try to unroll partially because "
129 << "-unroll-allow-partial not given\n");
132 // Reduce unroll count to be modulo of TripCount for partial unrolling
133 Count = UnrollThreshold / LoopSize;
134 while (Count != 0 && TripCount%Count != 0) {
138 DEBUG(dbgs() << " could not unroll partially\n");
141 DEBUG(dbgs() << " partially unrolling with count: " << Count << "\n");
146 Function *F = L->getHeader()->getParent();
147 if (!UnrollLoop(L, Count, LI, &LPM))
150 // FIXME: Reconstruct dom info, because it is not preserved properly.
151 DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
153 DT->runOnFunction(*F);
154 DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>();
156 DF->runOnFunction(*F);