Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Transforms / IPO / InlineSimple.cpp
1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
2 // 
3 //                     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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements bottom-up inlining of functions into callees.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Inliner.h"
15 #include "llvm/Function.h"
16 #include "llvm/iMemory.h"
17 #include "llvm/Support/CallSite.h"
18 #include "llvm/Transforms/IPO.h"
19
20 namespace {
21   // FunctionInfo - For each function, calculate the size of it in blocks and
22   // instructions.
23   struct FunctionInfo {
24     unsigned NumInsts, NumBlocks;
25
26     FunctionInfo() : NumInsts(0), NumBlocks(0) {}
27   };
28
29   class SimpleInliner : public Inliner {
30     std::map<const Function*, FunctionInfo> CachedFunctionInfo;
31   public:
32     int getInlineCost(CallSite CS);
33   };
34   RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining");
35 }
36
37 Pass *createFunctionInliningPass() { return new SimpleInliner(); }
38
39 // getInlineCost - The heuristic used to determine if we should inline the
40 // function call or not.
41 //
42 int SimpleInliner::getInlineCost(CallSite CS) {
43   Instruction *TheCall = CS.getInstruction();
44   const Function *Callee = CS.getCalledFunction();
45   const Function *Caller = TheCall->getParent()->getParent();
46
47   // Don't inline a directly recursive call.
48   if (Caller == Callee) return 2000000000;
49
50   // InlineCost - This value measures how good of an inline candidate this call
51   // site is to inline.  A lower inline cost make is more likely for the call to
52   // be inlined.  This value may go negative.
53   //
54   int InlineCost = 0;
55
56   // If there is only one call of the function, and it has internal linkage,
57   // make it almost guaranteed to be inlined.
58   //
59   if (Callee->hasInternalLinkage() && Callee->hasOneUse())
60     InlineCost -= 30000;
61
62   // Add to the inline quality for properties that make the call valuable to
63   // inline.  This includes factors that indicate that the result of inlining
64   // the function will be optimizable.  Currently this just looks at arguments
65   // passed into the function.
66   //
67   for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
68        I != E; ++I) {
69     // Each argument passed in has a cost at both the caller and the callee
70     // sides.  This favors functions that take many arguments over functions
71     // that take few arguments.
72     InlineCost -= 20;
73
74     // If this is a function being passed in, it is very likely that we will be
75     // able to turn an indirect function call into a direct function call.
76     if (isa<Function>(I))
77       InlineCost -= 100;
78
79     // If a constant, global variable or alloca is passed in, inlining this
80     // function is likely to allow significant future optimization possibilities
81     // (constant propagation, scalar promotion, and scalarization), so encourage
82     // the inlining of the function.
83     //
84     else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I))
85       InlineCost -= 60;
86   }
87
88   // Now that we have considered all of the factors that make the call site more
89   // likely to be inlined, look at factors that make us not want to inline it.
90   FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
91
92   // If we haven't calculated this information yet...
93   if (CalleeFI.NumBlocks == 0) {
94     unsigned NumInsts = 0, NumBlocks = 0;
95
96     // Look at the size of the callee.  Each basic block counts as 20 units, and
97     // each instruction counts as 10.
98     for (Function::const_iterator BB = Callee->begin(), E = Callee->end();
99          BB != E; ++BB) {
100       NumInsts += BB->size();
101       NumBlocks++;
102     }
103     CalleeFI.NumBlocks = NumBlocks;
104     CalleeFI.NumInsts  = NumInsts;
105   }
106
107   // Don't inline into something too big, which would make it bigger.  Here, we
108   // count each basic block as a single unit.
109   InlineCost += Caller->size()*2;
110
111
112   // Look at the size of the callee.  Each basic block counts as 20 units, and
113   // each instruction counts as 10.
114   InlineCost += CalleeFI.NumInsts*10 + CalleeFI.NumBlocks*20;
115   return InlineCost;
116 }