blob: 83cfe901a09f383fa041aa304f0cd530f0e6b819 [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
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000014#define DEBUG_TYPE "inline"
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"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000018#include "llvm/Module.h"
Chris Lattner2dc85b22004-03-13 23:15:45 +000019#include "llvm/Type.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000020#include "llvm/Analysis/CallGraph.h"
Chris Lattnerd367d052003-08-24 06:59:28 +000021#include "llvm/Support/CallSite.h"
Reid Spencer557ab152007-02-05 23:32:05 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000023#include "llvm/Transforms/IPO.h"
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000024#include "llvm/Transforms/IPO/InlinerPass.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000025#include <set>
26
Chris Lattner51c28a52003-11-21 21:46:09 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner04805fa2002-02-26 21:46:54 +000029namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000030 struct VISIBILITY_HIDDEN ArgInfo {
Chris Lattner2dc85b22004-03-13 23:15:45 +000031 unsigned ConstantWeight;
32 unsigned AllocaWeight;
33
34 ArgInfo(unsigned CWeight, unsigned AWeight)
35 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
36 };
37
Chris Lattner6dc0ae22003-10-06 15:52:43 +000038 // FunctionInfo - For each function, calculate the size of it in blocks and
39 // instructions.
Reid Spencer557ab152007-02-05 23:32:05 +000040 struct VISIBILITY_HIDDEN FunctionInfo {
Chris Lattner51c28a52003-11-21 21:46:09 +000041 // NumInsts, NumBlocks - Keep track of how large each function is, which is
42 // used to estimate the code size cost of inlining it.
Chris Lattner6dc0ae22003-10-06 15:52:43 +000043 unsigned NumInsts, NumBlocks;
44
Chris Lattner2dc85b22004-03-13 23:15:45 +000045 // ArgumentWeights - Each formal argument of the function is inspected to
46 // see if it is used in any contexts where making it a constant or alloca
Chris Lattner51c28a52003-11-21 21:46:09 +000047 // would reduce the code size. If so, we add some value to the argument
48 // entry here.
Chris Lattner2dc85b22004-03-13 23:15:45 +000049 std::vector<ArgInfo> ArgumentWeights;
Chris Lattner51c28a52003-11-21 21:46:09 +000050
Chris Lattner45406c02006-01-13 19:35:43 +000051 FunctionInfo() : NumInsts(0), NumBlocks(0) {}
Chris Lattnercde351e2004-08-12 05:45:09 +000052
53 /// analyzeFunction - Fill in the current structure with information gleaned
54 /// from the specified function.
55 void analyzeFunction(Function *F);
Chris Lattner6dc0ae22003-10-06 15:52:43 +000056 };
57
Reid Spencer557ab152007-02-05 23:32:05 +000058 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000059 std::map<const Function*, FunctionInfo> CachedFunctionInfo;
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000060 std::set<const Function*> NeverInline; // Functions that are never inlined
Chris Lattner6dc0ae22003-10-06 15:52:43 +000061 public:
Chris Lattner3b6f75c2007-05-06 23:13:56 +000062 SimpleInliner() : Inliner(&ID) {}
Nick Lewyckye7da2d62007-05-06 13:37:16 +000063 static char ID; // Pass identification, replacement for typeid
Chris Lattnerd075cc22003-08-31 19:10:30 +000064 int getInlineCost(CallSite CS);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000065 virtual bool doInitialization(CallGraph &CG);
Chris Lattner04805fa2002-02-26 21:46:54 +000066 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000067 char SimpleInliner::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000068 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +000069}
70
Devang Patel13058a52007-01-26 00:47:38 +000071Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner51c28a52003-11-21 21:46:09 +000072
73// CountCodeReductionForConstant - Figure out an approximation for how many
74// instructions will be constant folded if the specified value is constant.
75//
76static unsigned CountCodeReductionForConstant(Value *V) {
77 unsigned Reduction = 0;
78 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
79 if (isa<BranchInst>(*UI))
80 Reduction += 40; // Eliminating a conditional branch is a big win
81 else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI))
82 // Eliminating a switch is a big win, proportional to the number of edges
83 // deleted.
84 Reduction += (SI->getNumSuccessors()-1) * 40;
85 else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
86 // Turning an indirect call into a direct call is a BIG win
87 Reduction += CI->getCalledValue() == V ? 500 : 0;
88 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
89 // Turning an indirect call into a direct call is a BIG win
Chris Lattner61b3f202003-11-21 21:57:29 +000090 Reduction += II->getCalledValue() == V ? 500 : 0;
Chris Lattner51c28a52003-11-21 21:46:09 +000091 } else {
92 // Figure out if this instruction will be removed due to simple constant
93 // propagation.
94 Instruction &Inst = cast<Instruction>(**UI);
95 bool AllOperandsConstant = true;
96 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
Reid Spenceref784f02004-07-18 00:32:14 +000097 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
Chris Lattner51c28a52003-11-21 21:46:09 +000098 AllOperandsConstant = false;
99 break;
100 }
101
102 if (AllOperandsConstant) {
103 // We will get to remove this instruction...
104 Reduction += 7;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105
Chris Lattner51c28a52003-11-21 21:46:09 +0000106 // And any other instructions that use it which become constants
107 // themselves.
108 Reduction += CountCodeReductionForConstant(&Inst);
109 }
110 }
111
112 return Reduction;
113}
Chris Lattner530d4bf2003-05-29 15:11:31 +0000114
Chris Lattner2dc85b22004-03-13 23:15:45 +0000115// CountCodeReductionForAlloca - Figure out an approximation of how much smaller
116// the function will be if it is inlined into a context where an argument
117// becomes an alloca.
118//
119static unsigned CountCodeReductionForAlloca(Value *V) {
120 if (!isa<PointerType>(V->getType())) return 0; // Not a pointer
121 unsigned Reduction = 0;
122 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
123 Instruction *I = cast<Instruction>(*UI);
124 if (isa<LoadInst>(I) || isa<StoreInst>(I))
125 Reduction += 10;
126 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
127 // If the GEP has variable indices, we won't be able to do much with it.
128 for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end();
129 I != E; ++I)
130 if (!isa<Constant>(*I)) return 0;
131 Reduction += CountCodeReductionForAlloca(GEP)+15;
132 } else {
133 // If there is some other strange instruction, we're not going to be able
134 // to do much if we inline this.
135 return 0;
136 }
137 }
138
139 return Reduction;
140}
141
Chris Lattnercde351e2004-08-12 05:45:09 +0000142/// analyzeFunction - Fill in the current structure with information gleaned
143/// from the specified function.
144void FunctionInfo::analyzeFunction(Function *F) {
145 unsigned NumInsts = 0, NumBlocks = 0;
146
147 // Look at the size of the callee. Each basic block counts as 20 units, and
148 // each instruction counts as 10.
149 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
150 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
Chris Lattner27ff96d2006-09-09 20:40:44 +0000151 II != E; ++II) {
152 if (isa<DbgInfoIntrinsic>(II)) continue; // Debug intrinsics don't count.
153
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000154 // Noop casts, including ptr <-> int, don't count.
Chris Lattner27ff96d2006-09-09 20:40:44 +0000155 if (const CastInst *CI = dyn_cast<CastInst>(II)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000156 if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
157 isa<PtrToIntInst>(CI))
Chris Lattner27ff96d2006-09-09 20:40:44 +0000158 continue;
Chris Lattner27ff96d2006-09-09 20:40:44 +0000159 } else if (const GetElementPtrInst *GEPI =
160 dyn_cast<GetElementPtrInst>(II)) {
161 // If a GEP has all constant indices, it will probably be folded with
162 // a load/store.
163 bool AllConstant = true;
164 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
165 if (!isa<ConstantInt>(GEPI->getOperand(i))) {
166 AllConstant = false;
167 break;
168 }
169 if (AllConstant) continue;
170 }
171
172 ++NumInsts;
173 }
Chris Lattnercde351e2004-08-12 05:45:09 +0000174
175 ++NumBlocks;
176 }
177
178 this->NumBlocks = NumBlocks;
179 this->NumInsts = NumInsts;
180
181 // Check out all of the arguments to the function, figuring out how much
182 // code can be eliminated if one of the arguments is a constant.
Chris Lattner531f9e92005-03-15 04:54:21 +0000183 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattnercde351e2004-08-12 05:45:09 +0000184 ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I),
185 CountCodeReductionForAlloca(I)));
186}
187
188
Chris Lattnerd075cc22003-08-31 19:10:30 +0000189// getInlineCost - The heuristic used to determine if we should inline the
190// function call or not.
Chris Lattner530d4bf2003-05-29 15:11:31 +0000191//
Chris Lattnerd075cc22003-08-31 19:10:30 +0000192int SimpleInliner::getInlineCost(CallSite CS) {
Chris Lattnerd367d052003-08-24 06:59:28 +0000193 Instruction *TheCall = CS.getInstruction();
Chris Lattner51c28a52003-11-21 21:46:09 +0000194 Function *Callee = CS.getCalledFunction();
Chris Lattnerd367d052003-08-24 06:59:28 +0000195 const Function *Caller = TheCall->getParent()->getParent();
Chris Lattner530d4bf2003-05-29 15:11:31 +0000196
Chris Lattnerd075cc22003-08-31 19:10:30 +0000197 // Don't inline a directly recursive call.
198 if (Caller == Callee) return 2000000000;
199
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000200 // Don't inline functions marked noinline
201 if (NeverInline.count(Callee)) return 2000000000;
202
Chris Lattnerd075cc22003-08-31 19:10:30 +0000203 // InlineCost - This value measures how good of an inline candidate this call
204 // site is to inline. A lower inline cost make is more likely for the call to
205 // be inlined. This value may go negative.
Chris Lattner530d4bf2003-05-29 15:11:31 +0000206 //
Chris Lattnerd075cc22003-08-31 19:10:30 +0000207 int InlineCost = 0;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000208
209 // If there is only one call of the function, and it has internal linkage,
210 // make it almost guaranteed to be inlined.
211 //
Chris Lattner2d9c1172003-10-20 05:54:26 +0000212 if (Callee->hasInternalLinkage() && Callee->hasOneUse())
Chris Lattner436285e2004-11-09 08:05:23 +0000213 InlineCost -= 30000;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000214
Chris Lattner05deb042005-05-18 04:30:33 +0000215 // If this function uses the coldcc calling convention, prefer not to inline
216 // it.
217 if (Callee->getCallingConv() == CallingConv::Cold)
218 InlineCost += 2000;
219
220 // If the instruction after the call, or if the normal destination of the
221 // invoke is an unreachable instruction, the function is noreturn. As such,
222 // there is little point in inlining this.
223 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
224 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
225 InlineCost += 10000;
226 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
227 InlineCost += 10000;
228
Chris Lattner51c28a52003-11-21 21:46:09 +0000229 // Get information about the callee...
Chris Lattner6dc0ae22003-10-06 15:52:43 +0000230 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Chris Lattner530d4bf2003-05-29 15:11:31 +0000231
Chris Lattnercde351e2004-08-12 05:45:09 +0000232 // If we haven't calculated this information yet, do so now.
233 if (CalleeFI.NumBlocks == 0)
234 CalleeFI.analyzeFunction(Callee);
Chris Lattner6dc0ae22003-10-06 15:52:43 +0000235
Chris Lattner51c28a52003-11-21 21:46:09 +0000236 // Add to the inline quality for properties that make the call valuable to
237 // inline. This includes factors that indicate that the result of inlining
238 // the function will be optimizable. Currently this just looks at arguments
239 // passed into the function.
240 //
241 unsigned ArgNo = 0;
242 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
243 I != E; ++I, ++ArgNo) {
244 // Each argument passed in has a cost at both the caller and the callee
245 // sides. This favors functions that take many arguments over functions
246 // that take few arguments.
247 InlineCost -= 20;
248
249 // If this is a function being passed in, it is very likely that we will be
250 // able to turn an indirect function call into a direct function call.
251 if (isa<Function>(I))
252 InlineCost -= 100;
253
254 // If an alloca is passed in, inlining this function is likely to allow
255 // significant future optimization possibilities (like scalar promotion, and
256 // scalarization), so encourage the inlining of the function.
257 //
Reid Spencerde46e482006-11-02 20:25:50 +0000258 else if (isa<AllocaInst>(I)) {
Chris Lattner2dc85b22004-03-13 23:15:45 +0000259 if (ArgNo < CalleeFI.ArgumentWeights.size())
260 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight;
Chris Lattner51c28a52003-11-21 21:46:09 +0000261
262 // If this is a constant being passed into the function, use the argument
263 // weights calculated for the callee to determine how much will be folded
264 // away with this information.
Reid Spenceref784f02004-07-18 00:32:14 +0000265 } else if (isa<Constant>(I)) {
Chris Lattner2dc85b22004-03-13 23:15:45 +0000266 if (ArgNo < CalleeFI.ArgumentWeights.size())
267 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight;
Chris Lattner51c28a52003-11-21 21:46:09 +0000268 }
269 }
270
271 // Now that we have considered all of the factors that make the call site more
272 // likely to be inlined, look at factors that make us not want to inline it.
273
Chris Lattnerf8492532003-10-07 19:33:31 +0000274 // Don't inline into something too big, which would make it bigger. Here, we
275 // count each basic block as a single unit.
Chris Lattner95ce36d2004-03-15 06:38:14 +0000276 //
Chris Lattner4d25c862004-04-08 06:34:31 +0000277 InlineCost += Caller->size()/20;
Chris Lattnerf8492532003-10-07 19:33:31 +0000278
279
280 // Look at the size of the callee. Each basic block counts as 20 units, and
Chris Lattner51c28a52003-11-21 21:46:09 +0000281 // each instruction counts as 5.
282 InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20;
Chris Lattnerd075cc22003-08-31 19:10:30 +0000283 return InlineCost;
Chris Lattner530d4bf2003-05-29 15:11:31 +0000284}
Brian Gaeke960707c2003-11-11 22:41:34 +0000285
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000286// doInitialization - Initializes the vector of functions that have been
287// annotated with the noinline attribute.
288bool SimpleInliner::doInitialization(CallGraph &CG) {
289
290 Module &M = CG.getModule();
291
292 // Get llvm.noinline
293 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
294
Tanya Lattner5801c232007-06-07 17:12:16 +0000295 if (GV == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000296 return false;
297
298 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
299
Tanya Lattner5801c232007-06-07 17:12:16 +0000300 if (InitList == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000301 return false;
302
303 // Iterate over each element and add to the NeverInline set
304 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
305
306 // Get Source
307 const Constant *Elt = InitList->getOperand(i);
308
309 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
310 if (CE->getOpcode() == Instruction::BitCast)
311 Elt = CE->getOperand(0);
312
313 // Insert into set of functions to never inline
Tanya Lattner5801c232007-06-07 17:12:16 +0000314 if (const Function *F = dyn_cast<Function>(Elt))
315 NeverInline.insert(F);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000316 }
317
318 return false;
319}