1 //===- Inliner.cpp - Code common to all inliners --------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the mechanics required to implement inlining without
11 // missing any calls and updating the call graph. The decisions of which calls
12 // are profitable to inline are implemented elsewhere.
14 //===----------------------------------------------------------------------===//
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/Transforms/Utils/Cloning.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/Statistic.h"
29 Statistic NumInlined("inline", "Number of functions inlined");
30 Statistic NumDeleted("inline",
31 "Number of functions deleted because all callers found");
32 cl::opt<unsigned> // FIXME: 200 is VERY conservative
33 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
34 cl::desc("Control the amount of inlining to perform (default = 200)"));
37 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
39 // InlineCallIfPossible - If it is possible to inline the specified call site,
40 // do so and update the CallGraph for this operation.
41 static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
42 const std::set<Function*> &SCCFunctions) {
43 Function *Callee = CS.getCalledFunction();
44 if (!InlineFunction(CS, &CG)) return false;
46 // If we inlined the last possible call site to the function, delete the
48 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
49 !SCCFunctions.count(Callee)) {
50 DOUT << " -> Deleting dead function: " << Callee->getName() << "\n";
52 // Remove any call graph edges from the callee to its callees.
53 CallGraphNode *CalleeNode = CG[Callee];
54 while (CalleeNode->begin() != CalleeNode->end())
55 CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
57 // Removing the node for callee from the call graph and delete it.
58 delete CG.removeFunctionFromModule(CalleeNode);
64 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
65 CallGraph &CG = getAnalysis<CallGraph>();
67 std::set<Function*> SCCFunctions;
68 DOUT << "Inliner visiting SCC:";
69 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
70 Function *F = SCC[i]->getFunction();
71 if (F) SCCFunctions.insert(F);
72 DOUT << " " << (F ? F->getName() : "INDIRECTNODE");
75 // Scan through and identify all call sites ahead of time so that we only
76 // inline call sites in the original functions, not call sites that result
77 // from inlining other functions.
78 std::vector<CallSite> CallSites;
80 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
81 if (Function *F = SCC[i]->getFunction())
82 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
83 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
84 CallSite CS = CallSite::get(I);
85 if (CS.getInstruction() && (!CS.getCalledFunction() ||
86 !CS.getCalledFunction()->isExternal()))
87 CallSites.push_back(CS);
90 DOUT << ": " << CallSites.size() << " call sites.\n";
92 // Now that we have all of the call sites, move the ones to functions in the
93 // current SCC to the end of the list.
94 unsigned FirstCallInSCC = CallSites.size();
95 for (unsigned i = 0; i < FirstCallInSCC; ++i)
96 if (Function *F = CallSites[i].getCalledFunction())
97 if (SCCFunctions.count(F))
98 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
100 // Now that we have all of the call sites, loop over them and inline them if
101 // it looks profitable to do so.
102 bool Changed = false;
106 // Iterate over the outer loop because inlining functions can cause indirect
107 // calls to become direct calls.
108 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
109 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
110 // Calls to external functions are never inlinable.
111 if (Callee->isExternal() ||
112 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
113 if (SCC.size() == 1) {
114 std::swap(CallSites[CSi], CallSites.back());
115 CallSites.pop_back();
117 // Keep the 'in SCC / not in SCC' boundary correct.
118 CallSites.erase(CallSites.begin()+CSi);
124 // If the policy determines that we should inline this function,
126 CallSite CS = CallSites[CSi];
127 int InlineCost = getInlineCost(CS);
128 if (InlineCost >= (int)InlineThreshold) {
129 DOUT << " NOT Inlining: cost=" << InlineCost
130 << ", Call: " << *CS.getInstruction();
132 DOUT << " Inlining: cost=" << InlineCost
133 << ", Call: " << *CS.getInstruction();
135 // Attempt to inline the function...
136 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
137 // Remove this call site from the list. If possible, use
138 // swap/pop_back for efficiency, but do not use it if doing so would
139 // move a call site to a function in this SCC before the
140 // 'FirstCallInSCC' barrier.
141 if (SCC.size() == 1) {
142 std::swap(CallSites[CSi], CallSites.back());
143 CallSites.pop_back();
145 CallSites.erase(CallSites.begin()+CSi);
155 } while (LocalChange);
160 // doFinalization - Remove now-dead linkonce functions at the end of
161 // processing to avoid breaking the SCC traversal.
162 bool Inliner::doFinalization(CallGraph &CG) {
163 std::set<CallGraphNode*> FunctionsToRemove;
165 // Scan for all of the functions, looking for ones that should now be removed
166 // from the program. Insert the dead ones in the FunctionsToRemove set.
167 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
168 CallGraphNode *CGN = I->second;
169 if (Function *F = CGN ? CGN->getFunction() : 0) {
170 // If the only remaining users of the function are dead constants, remove
172 F->removeDeadConstantUsers();
174 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
177 // Remove any call graph edges from the function to its callees.
178 while (CGN->begin() != CGN->end())
179 CGN->removeCallEdgeTo((CGN->end()-1)->second);
181 // Remove any edges from the external node to the function's call graph
182 // node. These edges might have been made irrelegant due to
183 // optimization of the program.
184 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
186 // Removing the node for callee from the call graph and delete it.
187 FunctionsToRemove.insert(CGN);
192 // Now that we know which functions to delete, do so. We didn't want to do
193 // this inline, because that would invalidate our CallGraph::iterator
195 bool Changed = false;
196 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
197 E = FunctionsToRemove.end(); I != E; ++I) {
198 delete CG.removeFunctionFromModule(*I);