blob: 64de9c395e32e000ec744ef27f02a9102ab0c5bb [file] [log] [blame]
Chris Lattnerd075cc22003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner530d4bf2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Chris Lattnerd075cc22003-08-31 19:10:30 +000014#include "Inliner.h"
Chris Lattner05deb042005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner51c28a52003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner79e87e32004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000018#include "llvm/Function.h"
Chris Lattner2dc85b22004-03-13 23:15:45 +000019#include "llvm/Type.h"
Chris Lattnerd367d052003-08-24 06:59:28 +000020#include "llvm/Support/CallSite.h"
Reid Spencer557ab152007-02-05 23:32:05 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000022#include "llvm/Transforms/IPO.h"
Chris Lattner51c28a52003-11-21 21:46:09 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner04805fa2002-02-26 21:46:54 +000025namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000026 struct VISIBILITY_HIDDEN ArgInfo {
Chris Lattner2dc85b22004-03-13 23:15:45 +000027 unsigned ConstantWeight;
28 unsigned AllocaWeight;
29
30 ArgInfo(unsigned CWeight, unsigned AWeight)
31 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
32 };
33
Chris Lattner6dc0ae22003-10-06 15:52:43 +000034 // FunctionInfo - For each function, calculate the size of it in blocks and
35 // instructions.
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN FunctionInfo {
Chris Lattner51c28a52003-11-21 21:46:09 +000037 // NumInsts, NumBlocks - Keep track of how large each function is, which is
38 // used to estimate the code size cost of inlining it.
Chris Lattner6dc0ae22003-10-06 15:52:43 +000039 unsigned NumInsts, NumBlocks;
40
Chris Lattner2dc85b22004-03-13 23:15:45 +000041 // ArgumentWeights - Each formal argument of the function is inspected to
42 // see if it is used in any contexts where making it a constant or alloca
Chris Lattner51c28a52003-11-21 21:46:09 +000043 // would reduce the code size. If so, we add some value to the argument
44 // entry here.
Chris Lattner2dc85b22004-03-13 23:15:45 +000045 std::vector<ArgInfo> ArgumentWeights;
Chris Lattner51c28a52003-11-21 21:46:09 +000046
Chris Lattner45406c02006-01-13 19:35:43 +000047 FunctionInfo() : NumInsts(0), NumBlocks(0) {}
Chris Lattnercde351e2004-08-12 05:45:09 +000048
49 /// analyzeFunction - Fill in the current structure with information gleaned
50 /// from the specified function.
51 void analyzeFunction(Function *F);
Chris Lattner6dc0ae22003-10-06 15:52:43 +000052 };
53
Reid Spencer557ab152007-02-05 23:32:05 +000054 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000055 std::map<const Function*, FunctionInfo> CachedFunctionInfo;
56 public:
Chris Lattnerd075cc22003-08-31 19:10:30 +000057 int getInlineCost(CallSite CS);
Chris Lattner04805fa2002-02-26 21:46:54 +000058 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000059 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +000060}
61
Devang Patel13058a52007-01-26 00:47:38 +000062Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner51c28a52003-11-21 21:46:09 +000063
64// CountCodeReductionForConstant - Figure out an approximation for how many
65// instructions will be constant folded if the specified value is constant.
66//
67static unsigned CountCodeReductionForConstant(Value *V) {
68 unsigned Reduction = 0;
69 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
70 if (isa<BranchInst>(*UI))
71 Reduction += 40; // Eliminating a conditional branch is a big win
72 else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI))
73 // Eliminating a switch is a big win, proportional to the number of edges
74 // deleted.
75 Reduction += (SI->getNumSuccessors()-1) * 40;
76 else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
77 // Turning an indirect call into a direct call is a BIG win
78 Reduction += CI->getCalledValue() == V ? 500 : 0;
79 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
80 // Turning an indirect call into a direct call is a BIG win
Chris Lattner61b3f202003-11-21 21:57:29 +000081 Reduction += II->getCalledValue() == V ? 500 : 0;
Chris Lattner51c28a52003-11-21 21:46:09 +000082 } else {
83 // Figure out if this instruction will be removed due to simple constant
84 // propagation.
85 Instruction &Inst = cast<Instruction>(**UI);
86 bool AllOperandsConstant = true;
87 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
Reid Spenceref784f02004-07-18 00:32:14 +000088 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
Chris Lattner51c28a52003-11-21 21:46:09 +000089 AllOperandsConstant = false;
90 break;
91 }
92
93 if (AllOperandsConstant) {
94 // We will get to remove this instruction...
95 Reduction += 7;
Misha Brukmanb1c93172005-04-21 23:48:37 +000096
Chris Lattner51c28a52003-11-21 21:46:09 +000097 // And any other instructions that use it which become constants
98 // themselves.
99 Reduction += CountCodeReductionForConstant(&Inst);
100 }
101 }
102
103 return Reduction;
104}
Chris Lattner530d4bf2003-05-29 15:11:31 +0000105
Chris Lattner2dc85b22004-03-13 23:15:45 +0000106// CountCodeReductionForAlloca - Figure out an approximation of how much smaller
107// the function will be if it is inlined into a context where an argument
108// becomes an alloca.
109//
110static unsigned CountCodeReductionForAlloca(Value *V) {
111 if (!isa<PointerType>(V->getType())) return 0; // Not a pointer
112 unsigned Reduction = 0;
113 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
114 Instruction *I = cast<Instruction>(*UI);
115 if (isa<LoadInst>(I) || isa<StoreInst>(I))
116 Reduction += 10;
117 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
118 // If the GEP has variable indices, we won't be able to do much with it.
119 for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end();
120 I != E; ++I)
121 if (!isa<Constant>(*I)) return 0;
122 Reduction += CountCodeReductionForAlloca(GEP)+15;
123 } else {
124 // If there is some other strange instruction, we're not going to be able
125 // to do much if we inline this.
126 return 0;
127 }
128 }
129
130 return Reduction;
131}
132
Chris Lattnercde351e2004-08-12 05:45:09 +0000133/// analyzeFunction - Fill in the current structure with information gleaned
134/// from the specified function.
135void FunctionInfo::analyzeFunction(Function *F) {
136 unsigned NumInsts = 0, NumBlocks = 0;
137
138 // Look at the size of the callee. Each basic block counts as 20 units, and
139 // each instruction counts as 10.
140 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
141 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
Chris Lattner27ff96d2006-09-09 20:40:44 +0000142 II != E; ++II) {
143 if (isa<DbgInfoIntrinsic>(II)) continue; // Debug intrinsics don't count.
144
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000145 // Noop casts, including ptr <-> int, don't count.
Chris Lattner27ff96d2006-09-09 20:40:44 +0000146 if (const CastInst *CI = dyn_cast<CastInst>(II)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000147 if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
148 isa<PtrToIntInst>(CI))
Chris Lattner27ff96d2006-09-09 20:40:44 +0000149 continue;
Chris Lattner27ff96d2006-09-09 20:40:44 +0000150 } else if (const GetElementPtrInst *GEPI =
151 dyn_cast<GetElementPtrInst>(II)) {
152 // If a GEP has all constant indices, it will probably be folded with
153 // a load/store.
154 bool AllConstant = true;
155 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
156 if (!isa<ConstantInt>(GEPI->getOperand(i))) {
157 AllConstant = false;
158 break;
159 }
160 if (AllConstant) continue;
161 }
162
163 ++NumInsts;
164 }
Chris Lattnercde351e2004-08-12 05:45:09 +0000165
166 ++NumBlocks;
167 }
168
169 this->NumBlocks = NumBlocks;
170 this->NumInsts = NumInsts;
171
172 // Check out all of the arguments to the function, figuring out how much
173 // code can be eliminated if one of the arguments is a constant.
Chris Lattner531f9e92005-03-15 04:54:21 +0000174 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattnercde351e2004-08-12 05:45:09 +0000175 ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I),
176 CountCodeReductionForAlloca(I)));
177}
178
179
Chris Lattnerd075cc22003-08-31 19:10:30 +0000180// getInlineCost - The heuristic used to determine if we should inline the
181// function call or not.
Chris Lattner530d4bf2003-05-29 15:11:31 +0000182//
Chris Lattnerd075cc22003-08-31 19:10:30 +0000183int SimpleInliner::getInlineCost(CallSite CS) {
Chris Lattnerd367d052003-08-24 06:59:28 +0000184 Instruction *TheCall = CS.getInstruction();
Chris Lattner51c28a52003-11-21 21:46:09 +0000185 Function *Callee = CS.getCalledFunction();
Chris Lattnerd367d052003-08-24 06:59:28 +0000186 const Function *Caller = TheCall->getParent()->getParent();
Chris Lattner530d4bf2003-05-29 15:11:31 +0000187
Chris Lattnerd075cc22003-08-31 19:10:30 +0000188 // Don't inline a directly recursive call.
189 if (Caller == Callee) return 2000000000;
190
191 // InlineCost - This value measures how good of an inline candidate this call
192 // site is to inline. A lower inline cost make is more likely for the call to
193 // be inlined. This value may go negative.
Chris Lattner530d4bf2003-05-29 15:11:31 +0000194 //
Chris Lattnerd075cc22003-08-31 19:10:30 +0000195 int InlineCost = 0;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000196
197 // If there is only one call of the function, and it has internal linkage,
198 // make it almost guaranteed to be inlined.
199 //
Chris Lattner2d9c1172003-10-20 05:54:26 +0000200 if (Callee->hasInternalLinkage() && Callee->hasOneUse())
Chris Lattner436285e2004-11-09 08:05:23 +0000201 InlineCost -= 30000;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000202
Chris Lattner05deb042005-05-18 04:30:33 +0000203 // If this function uses the coldcc calling convention, prefer not to inline
204 // it.
205 if (Callee->getCallingConv() == CallingConv::Cold)
206 InlineCost += 2000;
207
208 // If the instruction after the call, or if the normal destination of the
209 // invoke is an unreachable instruction, the function is noreturn. As such,
210 // there is little point in inlining this.
211 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
212 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
213 InlineCost += 10000;
214 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
215 InlineCost += 10000;
216
Chris Lattner51c28a52003-11-21 21:46:09 +0000217 // Get information about the callee...
Chris Lattner6dc0ae22003-10-06 15:52:43 +0000218 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Chris Lattner530d4bf2003-05-29 15:11:31 +0000219
Chris Lattnercde351e2004-08-12 05:45:09 +0000220 // If we haven't calculated this information yet, do so now.
221 if (CalleeFI.NumBlocks == 0)
222 CalleeFI.analyzeFunction(Callee);
Chris Lattner6dc0ae22003-10-06 15:52:43 +0000223
Chris Lattner51c28a52003-11-21 21:46:09 +0000224 // Add to the inline quality for properties that make the call valuable to
225 // inline. This includes factors that indicate that the result of inlining
226 // the function will be optimizable. Currently this just looks at arguments
227 // passed into the function.
228 //
229 unsigned ArgNo = 0;
230 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
231 I != E; ++I, ++ArgNo) {
232 // Each argument passed in has a cost at both the caller and the callee
233 // sides. This favors functions that take many arguments over functions
234 // that take few arguments.
235 InlineCost -= 20;
236
237 // If this is a function being passed in, it is very likely that we will be
238 // able to turn an indirect function call into a direct function call.
239 if (isa<Function>(I))
240 InlineCost -= 100;
241
242 // If an alloca is passed in, inlining this function is likely to allow
243 // significant future optimization possibilities (like scalar promotion, and
244 // scalarization), so encourage the inlining of the function.
245 //
Reid Spencerde46e482006-11-02 20:25:50 +0000246 else if (isa<AllocaInst>(I)) {
Chris Lattner2dc85b22004-03-13 23:15:45 +0000247 if (ArgNo < CalleeFI.ArgumentWeights.size())
248 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight;
Chris Lattner51c28a52003-11-21 21:46:09 +0000249
250 // If this is a constant being passed into the function, use the argument
251 // weights calculated for the callee to determine how much will be folded
252 // away with this information.
Reid Spenceref784f02004-07-18 00:32:14 +0000253 } else if (isa<Constant>(I)) {
Chris Lattner2dc85b22004-03-13 23:15:45 +0000254 if (ArgNo < CalleeFI.ArgumentWeights.size())
255 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight;
Chris Lattner51c28a52003-11-21 21:46:09 +0000256 }
257 }
258
259 // Now that we have considered all of the factors that make the call site more
260 // likely to be inlined, look at factors that make us not want to inline it.
261
Chris Lattnerf8492532003-10-07 19:33:31 +0000262 // Don't inline into something too big, which would make it bigger. Here, we
263 // count each basic block as a single unit.
Chris Lattner95ce36d2004-03-15 06:38:14 +0000264 //
Chris Lattner4d25c862004-04-08 06:34:31 +0000265 InlineCost += Caller->size()/20;
Chris Lattnerf8492532003-10-07 19:33:31 +0000266
267
268 // Look at the size of the callee. Each basic block counts as 20 units, and
Chris Lattner51c28a52003-11-21 21:46:09 +0000269 // each instruction counts as 5.
270 InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20;
Chris Lattnerd075cc22003-08-31 19:10:30 +0000271 return InlineCost;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000272}
Brian Gaeke960707c2003-11-11 22:41:34 +0000273