blob: 492db4db097357cc66c6003dcffd66b1e1a9edc6 [file] [log] [blame]
Chris Lattner83bf2882004-04-18 05:20:17 +00001//===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattner83bf2882004-04-18 05:20:17 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattner83bf2882004-04-18 05:20:17 +00008//===----------------------------------------------------------------------===//
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.
Chris Lattner83bf2882004-04-18 05:20:17 +000013//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "loop-unroll"
Dan Gohman45b31972008-05-14 00:24:14 +000016#include "llvm/IntrinsicInst.h"
Chris Lattner83bf2882004-04-18 05:20:17 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner83bf2882004-04-18 05:20:17 +000018#include "llvm/Analysis/LoopInfo.h"
Devang Patel3f1a1e02007-03-07 01:38:05 +000019#include "llvm/Analysis/LoopPass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000020#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
Dan Gohman45b31972008-05-14 00:24:14 +000023#include "llvm/Transforms/Utils/UnrollLoop.h"
Duncan Sands1a2d6672008-05-16 09:30:00 +000024#include <climits>
Chris Lattner83bf2882004-04-18 05:20:17 +000025
Dan Gohman45b31972008-05-14 00:24:14 +000026using namespace llvm;
Chris Lattner83bf2882004-04-18 05:20:17 +000027
Dan Gohman844731a2008-05-13 00:00:25 +000028static cl::opt<unsigned>
29UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden,
30 cl::desc("The cut-off point for automatic loop unrolling"));
31
32static cl::opt<unsigned>
33UnrollCount("unroll-count", cl::init(0), cl::Hidden,
34 cl::desc("Use this unroll count for all loops, for testing purposes"));
35
Chris Lattner0e5f4992006-12-19 21:40:18 +000036namespace {
Devang Patel3f1a1e02007-03-07 01:38:05 +000037 class VISIBILITY_HIDDEN LoopUnroll : public LoopPass {
Chris Lattner83bf2882004-04-18 05:20:17 +000038 public:
Devang Patel19974732007-05-03 01:11:54 +000039 static char ID; // Pass ID, replacement for typeid
Dan Gohman1cc00962007-05-08 15:19:19 +000040 LoopUnroll() : LoopPass((intptr_t)&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000041
Dan Gohmanc7678442007-05-11 20:53:41 +000042 /// A magic value for use with the Threshold parameter to indicate
43 /// that the loop unroll should be performed regardless of how much
44 /// code expansion would result.
45 static const unsigned NoThreshold = UINT_MAX;
46
Devang Patel3f1a1e02007-03-07 01:38:05 +000047 bool runOnLoop(Loop *L, LPPassManager &LPM);
Chris Lattner83bf2882004-04-18 05:20:17 +000048
49 /// This transformation requires natural loop information & requires that
50 /// loop preheaders be inserted into the CFG...
51 ///
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner83bf2882004-04-18 05:20:17 +000053 AU.addRequiredID(LoopSimplifyID);
Owen Anderson3b53c4e2006-08-24 21:28:19 +000054 AU.addRequiredID(LCSSAID);
Chris Lattner83bf2882004-04-18 05:20:17 +000055 AU.addRequired<LoopInfo>();
Owen Anderson3b53c4e2006-08-24 21:28:19 +000056 AU.addPreservedID(LCSSAID);
Chris Lattner9c2cc462004-04-18 05:38:37 +000057 AU.addPreserved<LoopInfo>();
Devang Patel98260a42008-07-03 07:04:22 +000058 // FIXME: Loop unroll requires LCSSA. And LCSSA requires dom info.
59 // If loop unroll does not preserve dom info then LCSSA pass on next
60 // loop will receive invalid dom info.
61 // For now, recreate dom info, if loop is unrolled.
62 AU.addPreserved<DominatorTree>();
63 AU.addPreserved<DominanceFrontier>();
Chris Lattner83bf2882004-04-18 05:20:17 +000064 }
65 };
Chris Lattner83bf2882004-04-18 05:20:17 +000066}
67
Dan Gohman844731a2008-05-13 00:00:25 +000068char LoopUnroll::ID = 0;
69static RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops");
70
Devang Patel3f1a1e02007-03-07 01:38:05 +000071LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
Chris Lattner83bf2882004-04-18 05:20:17 +000072
Dan Gohmanf742c232007-05-08 15:14:19 +000073/// ApproximateLoopSize - Approximate the size of the loop.
Chris Lattner83bf2882004-04-18 05:20:17 +000074static unsigned ApproximateLoopSize(const Loop *L) {
75 unsigned Size = 0;
Dan Gohman9b787632008-06-22 20:18:58 +000076 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
77 I != E; ++I) {
78 BasicBlock *BB = *I;
Chris Lattner83bf2882004-04-18 05:20:17 +000079 Instruction *Term = BB->getTerminator();
80 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
81 if (isa<PHINode>(I) && BB == L->getHeader()) {
82 // Ignore PHI nodes in the header.
83 } else if (I->hasOneUse() && I->use_back() == Term) {
84 // Ignore instructions only used by the loop terminator.
Reid Spencer3ed469c2006-11-02 20:25:50 +000085 } else if (isa<DbgInfoIntrinsic>(I)) {
Jeff Cohen9d809302005-04-23 21:38:35 +000086 // Ignore debug instructions
Devang Patelb1760382008-03-17 23:41:20 +000087 } else if (isa<CallInst>(I)) {
Devang Patelac3844d2008-03-19 23:05:52 +000088 // Estimate size overhead introduced by call instructions which
89 // is higher than other instructions. Here 3 and 10 are magic
90 // numbers that help one isolated test case from PR2067 without
91 // negatively impacting measured benchmarks.
Devang Patelb1760382008-03-17 23:41:20 +000092 if (isa<IntrinsicInst>(I))
93 Size = Size + 3;
94 else
95 Size = Size + 10;
Chris Lattner83bf2882004-04-18 05:20:17 +000096 } else {
97 ++Size;
98 }
99
100 // TODO: Ignore expressions derived from PHI and constants if inval of phi
101 // is a constant, or if operation is associative. This will get induction
102 // variables.
103 }
104 }
105
106 return Size;
107}
108
Devang Patel3f1a1e02007-03-07 01:38:05 +0000109bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000110 assert(L->isLCSSAForm());
Dan Gohman45b31972008-05-14 00:24:14 +0000111 LoopInfo *LI = &getAnalysis<LoopInfo>();
Dan Gohmanc7678442007-05-11 20:53:41 +0000112
Dan Gohman1cc00962007-05-08 15:19:19 +0000113 BasicBlock *Header = L->getHeader();
Bill Wendlingb7427032006-11-26 09:46:52 +0000114 DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
Dan Gohmanc7678442007-05-11 20:53:41 +0000115 << "] Loop %" << Header->getName() << "\n";
116
Dan Gohman45b31972008-05-14 00:24:14 +0000117 // Find trip count
118 unsigned TripCount = L->getSmallConstantTripCount();
119 unsigned Count = UnrollCount;
120
Dan Gohmanc7678442007-05-11 20:53:41 +0000121 // Automatically select an unroll count.
122 if (Count == 0) {
123 // Conservative heuristic: if we know the trip count, see if we can
124 // completely unroll (subject to the threshold, checked below); otherwise
125 // don't unroll.
126 if (TripCount != 0) {
127 Count = TripCount;
128 } else {
129 return false;
130 }
131 }
132
Dan Gohmanc7678442007-05-11 20:53:41 +0000133 // Enforce the threshold.
Dan Gohman45b31972008-05-14 00:24:14 +0000134 if (UnrollThreshold != NoThreshold) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000135 unsigned LoopSize = ApproximateLoopSize(L);
136 DOUT << " Loop Size = " << LoopSize << "\n";
137 uint64_t Size = (uint64_t)LoopSize*Count;
Dan Gohman45b31972008-05-14 00:24:14 +0000138 if (TripCount != 1 && Size > UnrollThreshold) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000139 DOUT << " TOO LARGE TO UNROLL: "
Dan Gohman45b31972008-05-14 00:24:14 +0000140 << Size << ">" << UnrollThreshold << "\n";
Dan Gohmanc7678442007-05-11 20:53:41 +0000141 return false;
142 }
143 }
144
Dan Gohman45b31972008-05-14 00:24:14 +0000145 // Unroll the loop.
Devang Patel98260a42008-07-03 07:04:22 +0000146 Function *F = L->getHeader()->getParent();
Dan Gohman45b31972008-05-14 00:24:14 +0000147 if (!UnrollLoop(L, Count, LI, &LPM))
148 return false;
Dan Gohmanc7678442007-05-11 20:53:41 +0000149
Devang Patel98260a42008-07-03 07:04:22 +0000150 // FIXME: Reconstruct dom info, because it is not preserved properly.
151 DominatorTree *DT = getAnalysisToUpdate<DominatorTree>();
152 if (DT) {
153 DT->runOnFunction(*F);
154 DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>();
155 if (DF)
156 DF->runOnFunction(*F);
157 }
Chris Lattner83bf2882004-04-18 05:20:17 +0000158 return true;
159}