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 *Caller = CS.getInstruction()->getParent()->getParent();
45 Function *Callee = CS.getCalledFunction();
46 if (!InlineFunction(CS, &CG)) return false;
48 // If we inlined the last possible call site to the function, delete the
50 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
51 !SCCFunctions.count(Callee)) {
52 DEBUG(std::cerr << " -> Deleting dead function: "
53 << Callee->getName() << "\n");
55 // Remove any call graph edges from the callee to its callees.
56 CallGraphNode *CalleeNode = CG[Callee];
57 while (CalleeNode->begin() != CalleeNode->end())
58 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
60 // Removing the node for callee from the call graph and delete it.
61 delete CG.removeFunctionFromModule(CalleeNode);
67 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
68 CallGraph &CG = getAnalysis<CallGraph>();
70 std::set<Function*> SCCFunctions;
71 DEBUG(std::cerr << "Inliner visiting SCC:");
72 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
73 Function *F = SCC[i]->getFunction();
74 if (F) SCCFunctions.insert(F);
75 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
78 // Scan through and identify all call sites ahead of time so that we only
79 // inline call sites in the original functions, not call sites that result
80 // from inlining other functions.
81 std::vector<CallSite> CallSites;
83 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
84 if (Function *F = SCC[i]->getFunction())
85 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
86 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
87 CallSite CS = CallSite::get(I);
88 if (CS.getInstruction() && (!CS.getCalledFunction() ||
89 !CS.getCalledFunction()->isExternal()))
90 CallSites.push_back(CS);
93 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
95 // Now that we have all of the call sites, move the ones to functions in the
96 // current SCC to the end of the list.
97 unsigned FirstCallInSCC = CallSites.size();
98 for (unsigned i = 0; i < FirstCallInSCC; ++i)
99 if (Function *F = CallSites[i].getCalledFunction())
100 if (SCCFunctions.count(F))
101 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
103 // Now that we have all of the call sites, loop over them and inline them if
104 // it looks profitable to do so.
105 bool Changed = false;
109 // Iterate over the outer loop because inlining functions can cause indirect
110 // calls to become direct calls.
111 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
112 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
113 // Calls to external functions are never inlinable.
114 if (Callee->isExternal() ||
115 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
116 std::swap(CallSites[CSi], CallSites.back());
117 CallSites.pop_back();
122 // If the policy determines that we should inline this function,
124 CallSite CS = CallSites[CSi];
125 int InlineCost = getInlineCost(CS);
126 if (InlineCost >= (int)InlineThreshold) {
127 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
128 << ", Call: " << *CS.getInstruction());
130 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
131 << ", Call: " << *CS.getInstruction());
133 Function *Caller = CS.getInstruction()->getParent()->getParent();
135 // Attempt to inline the function...
136 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
137 // Remove this call site from the list.
138 std::swap(CallSites[CSi], CallSites.back());
139 CallSites.pop_back();
148 } while (LocalChange);
153 // doFinalization - Remove now-dead linkonce functions at the end of
154 // processing to avoid breaking the SCC traversal.
155 bool Inliner::doFinalization(CallGraph &CG) {
156 std::set<CallGraphNode*> FunctionsToRemove;
158 // Scan for all of the functions, looking for ones that should now be removed
159 // from the program. Insert the dead ones in the FunctionsToRemove set.
160 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
161 CallGraphNode *CGN = I->second;
162 if (Function *F = CGN ? CGN->getFunction() : 0) {
163 // If the only remaining users of the function are dead constants, remove
165 F->removeDeadConstantUsers();
167 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
170 // Remove any call graph edges from the function to its callees.
171 while (CGN->begin() != CGN->end())
172 CGN->removeCallEdgeTo(*(CGN->end()-1));
174 // Remove any edges from the external node to the function's call graph
175 // node. These edges might have been made irrelegant due to
176 // optimization of the program.
177 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
179 // Removing the node for callee from the call graph and delete it.
180 FunctionsToRemove.insert(CGN);
185 // Now that we know which functions to delete, do so. We didn't want to do
186 // this inline, because that would invalidate our CallGraph::iterator
188 bool Changed = false;
189 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
190 E = FunctionsToRemove.end(); I != E; ++I) {
191 delete CG.removeFunctionFromModule(*I);