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