blob: ad8cfd4eeb2aea59250a31c4180a5fd4b7a404d1 [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"
Chris Lattner83bf2882004-04-18 05:20:17 +000024
Dan Gohman45b31972008-05-14 00:24:14 +000025using namespace llvm;
Chris Lattner83bf2882004-04-18 05:20:17 +000026
Dan Gohman844731a2008-05-13 00:00:25 +000027static cl::opt<unsigned>
28UnrollThreshold("unroll-threshold", cl::init(100), cl::Hidden,
29 cl::desc("The cut-off point for automatic loop unrolling"));
30
31static cl::opt<unsigned>
32UnrollCount("unroll-count", cl::init(0), cl::Hidden,
33 cl::desc("Use this unroll count for all loops, for testing purposes"));
34
Chris Lattner0e5f4992006-12-19 21:40:18 +000035namespace {
Devang Patel3f1a1e02007-03-07 01:38:05 +000036 class VISIBILITY_HIDDEN LoopUnroll : public LoopPass {
Chris Lattner83bf2882004-04-18 05:20:17 +000037 public:
Devang Patel19974732007-05-03 01:11:54 +000038 static char ID; // Pass ID, replacement for typeid
Dan Gohman1cc00962007-05-08 15:19:19 +000039 LoopUnroll() : LoopPass((intptr_t)&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000040
Dan Gohmanc7678442007-05-11 20:53:41 +000041 /// A magic value for use with the Threshold parameter to indicate
42 /// that the loop unroll should be performed regardless of how much
43 /// code expansion would result.
44 static const unsigned NoThreshold = UINT_MAX;
45
Devang Patel3f1a1e02007-03-07 01:38:05 +000046 bool runOnLoop(Loop *L, LPPassManager &LPM);
Chris Lattner83bf2882004-04-18 05:20:17 +000047
48 /// This transformation requires natural loop information & requires that
49 /// loop preheaders be inserted into the CFG...
50 ///
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner83bf2882004-04-18 05:20:17 +000052 AU.addRequiredID(LoopSimplifyID);
Owen Anderson3b53c4e2006-08-24 21:28:19 +000053 AU.addRequiredID(LCSSAID);
Chris Lattner83bf2882004-04-18 05:20:17 +000054 AU.addRequired<LoopInfo>();
Owen Anderson3b53c4e2006-08-24 21:28:19 +000055 AU.addPreservedID(LCSSAID);
Chris Lattner9c2cc462004-04-18 05:38:37 +000056 AU.addPreserved<LoopInfo>();
Chris Lattner83bf2882004-04-18 05:20:17 +000057 }
58 };
Chris Lattner83bf2882004-04-18 05:20:17 +000059}
60
Dan Gohman844731a2008-05-13 00:00:25 +000061char LoopUnroll::ID = 0;
62static RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops");
63
Devang Patel3f1a1e02007-03-07 01:38:05 +000064LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
Chris Lattner83bf2882004-04-18 05:20:17 +000065
Dan Gohmanf742c232007-05-08 15:14:19 +000066/// ApproximateLoopSize - Approximate the size of the loop.
Chris Lattner83bf2882004-04-18 05:20:17 +000067static unsigned ApproximateLoopSize(const Loop *L) {
68 unsigned Size = 0;
69 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
70 BasicBlock *BB = L->getBlocks()[i];
71 Instruction *Term = BB->getTerminator();
72 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
73 if (isa<PHINode>(I) && BB == L->getHeader()) {
74 // Ignore PHI nodes in the header.
75 } else if (I->hasOneUse() && I->use_back() == Term) {
76 // Ignore instructions only used by the loop terminator.
Reid Spencer3ed469c2006-11-02 20:25:50 +000077 } else if (isa<DbgInfoIntrinsic>(I)) {
Jeff Cohen9d809302005-04-23 21:38:35 +000078 // Ignore debug instructions
Devang Patelb1760382008-03-17 23:41:20 +000079 } else if (isa<CallInst>(I)) {
Devang Patelac3844d2008-03-19 23:05:52 +000080 // Estimate size overhead introduced by call instructions which
81 // is higher than other instructions. Here 3 and 10 are magic
82 // numbers that help one isolated test case from PR2067 without
83 // negatively impacting measured benchmarks.
Devang Patelb1760382008-03-17 23:41:20 +000084 if (isa<IntrinsicInst>(I))
85 Size = Size + 3;
86 else
87 Size = Size + 10;
Chris Lattner83bf2882004-04-18 05:20:17 +000088 } else {
89 ++Size;
90 }
91
92 // TODO: Ignore expressions derived from PHI and constants if inval of phi
93 // is a constant, or if operation is associative. This will get induction
94 // variables.
95 }
96 }
97
98 return Size;
99}
100
Devang Patel3f1a1e02007-03-07 01:38:05 +0000101bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000102 assert(L->isLCSSAForm());
Dan Gohman45b31972008-05-14 00:24:14 +0000103 LoopInfo *LI = &getAnalysis<LoopInfo>();
Dan Gohmanc7678442007-05-11 20:53:41 +0000104
Dan Gohman1cc00962007-05-08 15:19:19 +0000105 BasicBlock *Header = L->getHeader();
Bill Wendlingb7427032006-11-26 09:46:52 +0000106 DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
Dan Gohmanc7678442007-05-11 20:53:41 +0000107 << "] Loop %" << Header->getName() << "\n";
108
Dan Gohman45b31972008-05-14 00:24:14 +0000109 // Find trip count
110 unsigned TripCount = L->getSmallConstantTripCount();
111 unsigned Count = UnrollCount;
112
Dan Gohmanc7678442007-05-11 20:53:41 +0000113 // Automatically select an unroll count.
114 if (Count == 0) {
115 // Conservative heuristic: if we know the trip count, see if we can
116 // completely unroll (subject to the threshold, checked below); otherwise
117 // don't unroll.
118 if (TripCount != 0) {
119 Count = TripCount;
120 } else {
121 return false;
122 }
123 }
124
Dan Gohmanc7678442007-05-11 20:53:41 +0000125 // Enforce the threshold.
Dan Gohman45b31972008-05-14 00:24:14 +0000126 if (UnrollThreshold != NoThreshold) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000127 unsigned LoopSize = ApproximateLoopSize(L);
128 DOUT << " Loop Size = " << LoopSize << "\n";
129 uint64_t Size = (uint64_t)LoopSize*Count;
Dan Gohman45b31972008-05-14 00:24:14 +0000130 if (TripCount != 1 && Size > UnrollThreshold) {
Dan Gohmanc7678442007-05-11 20:53:41 +0000131 DOUT << " TOO LARGE TO UNROLL: "
Dan Gohman45b31972008-05-14 00:24:14 +0000132 << Size << ">" << UnrollThreshold << "\n";
Dan Gohmanc7678442007-05-11 20:53:41 +0000133 return false;
134 }
135 }
136
Dan Gohman45b31972008-05-14 00:24:14 +0000137 // Unroll the loop.
138 if (!UnrollLoop(L, Count, LI, &LPM))
139 return false;
Dan Gohmanc7678442007-05-11 20:53:41 +0000140
Chris Lattner83bf2882004-04-18 05:20:17 +0000141 return true;
142}