1 //===- FunctionInlining.cpp - Code to perform function inlining -----------===//
3 // This file implements bottom-up inlining of functions into callees.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Transforms/IPO.h"
8 #include "llvm/Transforms/Utils/Cloning.h"
9 #include "llvm/Module.h"
10 #include "llvm/Pass.h"
11 #include "llvm/iOther.h"
12 #include "llvm/iMemory.h"
13 #include "Support/CommandLine.h"
14 #include "Support/Debug.h"
15 #include "Support/Statistic.h"
19 Statistic<> NumInlined("inline", "Number of functions inlined");
20 cl::opt<unsigned> // FIXME: 200 is VERY conservative
21 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
22 cl::desc("Control the amount of inlining to perform (default = 200)"));
24 struct FunctionInlining : public Pass {
25 virtual bool run(Module &M) {
27 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
28 Changed |= doInlining(I);
29 ProcessedFunctions.clear();
34 std::set<Function*> ProcessedFunctions; // Prevent infinite recursion
35 bool doInlining(Function *F);
37 RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
40 Pass *createFunctionInliningPass() { return new FunctionInlining(); }
43 // ShouldInlineFunction - The heuristic used to determine if we should inline
44 // the function call or not.
46 static inline bool ShouldInlineFunction(const CallInst *CI) {
47 assert(CI->getParent() && CI->getParent()->getParent() &&
48 "Call not embedded into a function!");
50 const Function *Callee = CI->getCalledFunction();
51 if (Callee == 0 || Callee->isExternal())
52 return false; // Cannot inline an indirect call... or external function.
54 // Don't inline a recursive call.
55 const Function *Caller = CI->getParent()->getParent();
56 if (Caller == Callee) return false;
58 // InlineQuality - This value measures how good of an inline candidate this
59 // call site is to inline. The initial value determines how aggressive the
60 // inliner is. If this value is negative after the final computation,
61 // inlining is not performed.
63 int InlineQuality = InlineLimit;
65 // If there is only one call of the function, and it has internal linkage,
66 // make it almost guaranteed to be inlined.
68 if (Callee->use_size() == 1 && Callee->hasInternalLinkage())
69 InlineQuality += 30000;
71 // Add to the inline quality for properties that make the call valueable to
72 // inline. This includes factors that indicate that the result of inlining
73 // the function will be optimizable. Currently this just looks at arguments
74 // passed into the function.
76 for (User::const_op_iterator I = CI->op_begin()+1, E = CI->op_end();
78 // Each argument passed in has a cost at both the caller and the callee
79 // sides. This favors functions that take many arguments over functions
80 // that take few arguments.
83 // If this is a function being passed in, it is very likely that we will be
84 // able to turn an indirect function call into a direct function call.
88 // If a constant, global variable or alloca is passed in, inlining this
89 // function is likely to allow significant future optimization possibilities
90 // (constant propagation, scalar promotion, and scalarization), so encourage
91 // the inlining of the function.
93 else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I))
97 // Now that we have considered all of the factors that make the call site more
98 // likely to be inlined, look at factors that make us not want to inline it.
99 // As soon as the inline quality gets negative, bail out.
101 // Look at the size of the callee. Each basic block counts as 20 units, and
102 // each instruction counts as 10.
103 for (Function::const_iterator BB = Callee->begin(), E = Callee->end();
105 InlineQuality -= BB->size()*10 + 20;
106 if (InlineQuality < 0) return false;
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 for (Function::const_iterator BB = Caller->begin(), E = Caller->end();
114 if (InlineQuality < 0) return false;
117 // If we get here, this call site is high enough "quality" to inline.
118 DEBUG(std::cerr << "Inlining in '" << Caller->getName()
119 << "', quality = " << InlineQuality << ": " << *CI);
124 // doInlining - Use a heuristic based approach to inline functions that seem to
127 bool FunctionInlining::doInlining(Function *F) {
128 // If we have already processed this function (ie, it is recursive) don't
130 std::set<Function*>::iterator PFI = ProcessedFunctions.lower_bound(F);
131 if (PFI != ProcessedFunctions.end() && *PFI == F) return false;
133 // Insert the function in the set so it doesn't get revisited.
134 ProcessedFunctions.insert(PFI, F);
136 bool Changed = false;
137 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB)
138 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
139 bool ShouldInc = true;
140 // Found a call instruction? FIXME: This should also handle INVOKEs
141 if (CallInst *CI = dyn_cast<CallInst>(I)) {
142 if (Function *Callee = CI->getCalledFunction())
143 doInlining(Callee); // Inline in callees before callers!
145 // Decide whether we should inline this function...
146 if (ShouldInlineFunction(CI)) {
147 // Save an iterator to the instruction before the call if it exists,
148 // otherwise get an iterator at the end of the block... because the
149 // call will be destroyed.
151 BasicBlock::iterator SI;
152 if (I != BB->begin()) {
153 SI = I; --SI; // Instruction before the call...
158 // Attempt to inline the function...
159 if (InlineFunction(CI)) {
162 // Move to instruction before the call...
163 I = (SI == BB->end()) ? BB->begin() : SI;
164 ShouldInc = false; // Don't increment iterator until next time