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"
30 Statistic<> NumInlined("inline", "Number of functions inlined");
31 Statistic<> NumDeleted("inline",
32 "Number of functions deleted because all callers found");
33 cl::opt<unsigned> // FIXME: 200 is VERY conservative
34 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
35 cl::desc("Control the amount of inlining to perform (default = 200)"));
38 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
40 // InlineCallIfPossible - If it is possible to inline the specified call site,
41 // do so and update the CallGraph for this operation.
42 static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
43 const std::set<Function*> &SCCFunctions) {
44 Function *Callee = CS.getCalledFunction();
45 if (!InlineFunction(CS, &CG)) return false;
47 // If we inlined the last possible call site to the function, delete the
49 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
50 !SCCFunctions.count(Callee)) {
51 DEBUG(std::cerr << " -> Deleting dead function: "
52 << Callee->getName() << "\n");
54 // Remove any call graph edges from the callee to its callees.
55 CallGraphNode *CalleeNode = CG[Callee];
56 while (CalleeNode->begin() != CalleeNode->end())
57 CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
59 // Removing the node for callee from the call graph and delete it.
60 delete CG.removeFunctionFromModule(CalleeNode);
66 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
67 CallGraph &CG = getAnalysis<CallGraph>();
69 std::set<Function*> SCCFunctions;
70 DEBUG(std::cerr << "Inliner visiting SCC:");
71 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
72 Function *F = SCC[i]->getFunction();
73 if (F) SCCFunctions.insert(F);
74 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
77 // Scan through and identify all call sites ahead of time so that we only
78 // inline call sites in the original functions, not call sites that result
79 // from inlining other functions.
80 std::vector<CallSite> CallSites;
82 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
83 if (Function *F = SCC[i]->getFunction())
84 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
85 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
86 CallSite CS = CallSite::get(I);
87 if (CS.getInstruction() && (!CS.getCalledFunction() ||
88 !CS.getCalledFunction()->isExternal()))
89 CallSites.push_back(CS);
92 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
94 // Now that we have all of the call sites, move the ones to functions in the
95 // current SCC to the end of the list.
96 unsigned FirstCallInSCC = CallSites.size();
97 for (unsigned i = 0; i < FirstCallInSCC; ++i)
98 if (Function *F = CallSites[i].getCalledFunction())
99 if (SCCFunctions.count(F))
100 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
102 // Now that we have all of the call sites, loop over them and inline them if
103 // it looks profitable to do so.
104 bool Changed = false;
108 // Iterate over the outer loop because inlining functions can cause indirect
109 // calls to become direct calls.
110 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
111 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
112 // Calls to external functions are never inlinable.
113 if (Callee->isExternal() ||
114 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
115 std::swap(CallSites[CSi], CallSites.back());
116 CallSites.pop_back();
121 // If the policy determines that we should inline this function,
123 CallSite CS = CallSites[CSi];
124 int InlineCost = getInlineCost(CS);
125 if (InlineCost >= (int)InlineThreshold) {
126 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
127 << ", Call: " << *CS.getInstruction());
129 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
130 << ", Call: " << *CS.getInstruction());
132 Function *Caller = CS.getInstruction()->getParent()->getParent();
134 // Attempt to inline the function...
135 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
136 // Remove this call site from the list.
137 std::swap(CallSites[CSi], CallSites.back());
138 CallSites.pop_back();
147 } while (LocalChange);
152 // doFinalization - Remove now-dead linkonce functions at the end of
153 // processing to avoid breaking the SCC traversal.
154 bool Inliner::doFinalization(CallGraph &CG) {
155 std::set<CallGraphNode*> FunctionsToRemove;
157 // Scan for all of the functions, looking for ones that should now be removed
158 // from the program. Insert the dead ones in the FunctionsToRemove set.
159 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
160 CallGraphNode *CGN = I->second;
161 if (Function *F = CGN ? CGN->getFunction() : 0) {
162 // If the only remaining users of the function are dead constants, remove
164 F->removeDeadConstantUsers();
166 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
169 // Remove any call graph edges from the function to its callees.
170 while (CGN->begin() != CGN->end())
171 CGN->removeCallEdgeTo((CGN->end()-1)->second);
173 // Remove any edges from the external node to the function's call graph
174 // node. These edges might have been made irrelegant due to
175 // optimization of the program.
176 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
178 // Removing the node for callee from the call graph and delete it.
179 FunctionsToRemove.insert(CGN);
184 // Now that we know which functions to delete, do so. We didn't want to do
185 // this inline, because that would invalidate our CallGraph::iterator
187 bool Changed = false;
188 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
189 E = FunctionsToRemove.end(); I != E; ++I) {
190 delete CG.removeFunctionFromModule(*I);