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/Constants.h" // ConstantPointerRef should die
18 #include "llvm/Module.h"
19 #include "llvm/iOther.h"
20 #include "llvm/iTerminators.h"
21 #include "llvm/Analysis/CallGraph.h"
22 #include "llvm/Support/CallSite.h"
23 #include "llvm/Transforms/Utils/Cloning.h"
24 #include "Support/CommandLine.h"
25 #include "Support/Debug.h"
26 #include "Support/Statistic.h"
31 Statistic<> NumInlined("inline", "Number of functions inlined");
32 Statistic<> NumDeleted("inline", "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)) return false;
48 // Update the call graph by deleting the edge from Callee to Caller
49 CallGraphNode *CalleeNode = CG[Callee];
50 CallGraphNode *CallerNode = CG[Caller];
51 CallerNode->removeCallEdgeTo(CalleeNode);
53 // Since we inlined all uninlined call sites in the callee into the caller,
54 // add edges from the caller to all of the callees of the callee.
55 for (CallGraphNode::iterator I = CalleeNode->begin(),
56 E = CalleeNode->end(); I != E; ++I)
57 CallerNode->addCalledFunction(*I);
59 // If we inlined the last possible call site to the function,
60 // delete the function body now.
61 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
62 !SCCFunctions.count(Callee)) {
63 DEBUG(std::cerr << " -> Deleting dead function: "
64 << Callee->getName() << "\n");
66 // Remove any call graph edges from the callee to its callees.
67 while (CalleeNode->begin() != CalleeNode->end())
68 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
70 // Removing the node for callee from the call graph and delete it.
71 delete CG.removeFunctionFromModule(CalleeNode);
77 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
78 CallGraph &CG = getAnalysis<CallGraph>();
80 std::set<Function*> SCCFunctions;
81 DEBUG(std::cerr << "Inliner visiting SCC:");
82 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
83 Function *F = SCC[i]->getFunction();
84 if (F) SCCFunctions.insert(F);
85 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
88 // Scan through and identify all call sites ahead of time so that we only
89 // inline call sites in the original functions, not call sites that result
90 // from inlining other functions.
91 std::vector<CallSite> CallSites;
93 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
94 if (Function *F = SCC[i]->getFunction())
95 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
96 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
97 CallSite CS = CallSite::get(I);
98 if (CS.getInstruction() && (!CS.getCalledFunction() ||
99 !CS.getCalledFunction()->isExternal()))
100 CallSites.push_back(CS);
103 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
105 // Now that we have all of the call sites, move the ones to functions in the
106 // current SCC to the end of the list.
107 unsigned FirstCallInSCC = CallSites.size();
108 for (unsigned i = 0; i < FirstCallInSCC; ++i)
109 if (Function *F = CallSites[i].getCalledFunction())
110 if (SCCFunctions.count(F))
111 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
113 // Now that we have all of the call sites, loop over them and inline them if
114 // it looks profitable to do so.
115 bool Changed = false;
119 // Iterate over the outer loop because inlining functions can cause indirect
120 // calls to become direct calls.
121 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
122 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
123 // Calls to external functions are never inlinable.
124 if (Callee->isExternal() ||
125 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
126 std::swap(CallSites[CSi], CallSites.back());
127 CallSites.pop_back();
132 // If the policy determines that we should inline this function,
134 CallSite CS = CallSites[CSi];
135 int InlineCost = getInlineCost(CS);
136 if (InlineCost >= (int)InlineThreshold) {
137 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
138 << ", Call: " << *CS.getInstruction());
140 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
141 << ", Call: " << *CS.getInstruction());
143 Function *Caller = CS.getInstruction()->getParent()->getParent();
145 // Attempt to inline the function...
146 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
147 // Remove this call site from the list.
148 std::swap(CallSites[CSi], CallSites.back());
149 CallSites.pop_back();
158 } while (LocalChange);
163 // doFinalization - Remove now-dead linkonce functions at the end of
164 // processing to avoid breaking the SCC traversal.
165 bool Inliner::doFinalization(CallGraph &CG) {
166 std::set<CallGraphNode*> FunctionsToRemove;
168 // Scan for all of the functions, looking for ones that should now be removed
169 // from the program. Insert the dead ones in the FunctionsToRemove set.
170 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
171 CallGraphNode *CGN = I->second;
172 Function *F = CGN ? CGN->getFunction() : 0;
174 // If the only remaining use of the function is a dead constant
175 // pointer ref, remove it.
176 if (F && F->hasOneUse())
177 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(F->use_back()))
178 if (CPR->use_empty()) {
179 CPR->destroyConstant();
180 if (F->hasInternalLinkage()) {
181 // There *MAY* be an edge from the external call node to this
182 // function. If so, remove it.
183 CallGraphNode *EN = CG.getExternalCallingNode();
184 CallGraphNode::iterator I = std::find(EN->begin(), EN->end(), CGN);
185 if (I != EN->end()) EN->removeCallEdgeTo(CGN);
189 if (F && (F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
191 // Remove any call graph edges from the function to its callees.
192 while (CGN->begin() != CGN->end())
193 CGN->removeCallEdgeTo(*(CGN->end()-1));
195 // If the function has external linkage (basically if it's a linkonce
196 // function) remove the edge from the external node to the callee node.
197 if (!F->hasInternalLinkage())
198 CG.getExternalCallingNode()->removeCallEdgeTo(CGN);
200 // Removing the node for callee from the call graph and delete it.
201 FunctionsToRemove.insert(CGN);
205 // Now that we know which functions to delete, do so. We didn't want to do
206 // this inline, because that would invalidate our CallGraph::iterator
208 bool Changed = false;
209 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
210 E = FunctionsToRemove.end(); I != E; ++I) {
211 delete CG.removeFunctionFromModule(*I);