blob: ad8cfd4eeb2aea59250a31c4180a5fd4b7a404d1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LoopUnroll.cpp - Loop unroller pass -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "loop-unroll"
Dan Gohman664b5162008-05-14 00:24:14 +000016#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Transforms/Scalar.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/LoopPass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Support/Compiler.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
Dan Gohman664b5162008-05-14 00:24:14 +000023#include "llvm/Transforms/Utils/UnrollLoop.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024
Dan Gohman664b5162008-05-14 00:24:14 +000025using namespace llvm;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026
Dan Gohman089efff2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 class VISIBILITY_HIDDEN LoopUnroll : public LoopPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 public:
38 static char ID; // Pass ID, replacement for typeid
39 LoopUnroll() : LoopPass((intptr_t)&ID) {}
40
41 /// 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
46 bool runOnLoop(Loop *L, LPPassManager &LPM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 {
52 AU.addRequiredID(LoopSimplifyID);
53 AU.addRequiredID(LCSSAID);
54 AU.addRequired<LoopInfo>();
55 AU.addPreservedID(LCSSAID);
56 AU.addPreserved<LoopInfo>();
57 }
58 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
Dan Gohman089efff2008-05-13 00:00:25 +000061char LoopUnroll::ID = 0;
62static RegisterPass<LoopUnroll> X("loop-unroll", "Unroll loops");
63
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064LoopPass *llvm::createLoopUnrollPass() { return new LoopUnroll(); }
65
66/// ApproximateLoopSize - Approximate the size of the loop.
67static 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.
77 } else if (isa<DbgInfoIntrinsic>(I)) {
78 // Ignore debug instructions
Devang Pateleb485232008-03-17 23:41:20 +000079 } else if (isa<CallInst>(I)) {
Devang Patel86ad6a22008-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 Pateleb485232008-03-17 23:41:20 +000084 if (isa<IntrinsicInst>(I))
85 Size = Size + 3;
86 else
87 Size = Size + 10;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101bool LoopUnroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 assert(L->isLCSSAForm());
Dan Gohman664b5162008-05-14 00:24:14 +0000103 LoopInfo *LI = &getAnalysis<LoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105 BasicBlock *Header = L->getHeader();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 DOUT << "Loop Unroll: F[" << Header->getParent()->getName()
107 << "] Loop %" << Header->getName() << "\n";
108
Dan Gohman664b5162008-05-14 00:24:14 +0000109 // Find trip count
110 unsigned TripCount = L->getSmallConstantTripCount();
111 unsigned Count = UnrollCount;
112
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohmanf17a25c2007-07-18 16:29:46 +0000125 // Enforce the threshold.
Dan Gohman664b5162008-05-14 00:24:14 +0000126 if (UnrollThreshold != NoThreshold) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 unsigned LoopSize = ApproximateLoopSize(L);
128 DOUT << " Loop Size = " << LoopSize << "\n";
129 uint64_t Size = (uint64_t)LoopSize*Count;
Dan Gohman664b5162008-05-14 00:24:14 +0000130 if (TripCount != 1 && Size > UnrollThreshold) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 DOUT << " TOO LARGE TO UNROLL: "
Dan Gohman664b5162008-05-14 00:24:14 +0000132 << Size << ">" << UnrollThreshold << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 return false;
134 }
135 }
136
Dan Gohman664b5162008-05-14 00:24:14 +0000137 // Unroll the loop.
138 if (!UnrollLoop(L, Count, LI, &LPM))
139 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 return true;
142}