1 //===- InlineCommon.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 code shared between the LLVM inliners. This
11 // implements all of the boring mechanics of the bottom-up inlining.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Constants.h" // ConstantPointerRef should die
17 #include "llvm/Module.h"
18 #include "llvm/iOther.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
23 #include "Support/CommandLine.h"
24 #include "Support/Debug.h"
25 #include "Support/Statistic.h"
29 Statistic<> NumInlined("inline", "Number of functions inlined");
30 Statistic<> NumDeleted("inline", "Number of functions deleted because all callers found");
31 cl::opt<unsigned> // FIXME: 200 is VERY conservative
32 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
33 cl::desc("Control the amount of inlining to perform (default = 200)"));
36 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
38 int Inliner::getRecursiveInlineCost(CallSite CS) {
39 return getInlineCost(CS);
42 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
43 CallGraph &CG = getAnalysis<CallGraph>();
45 std::set<Function*> SCCFunctions;
46 DEBUG(std::cerr << "Inliner visiting SCC:");
47 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
48 SCCFunctions.insert(SCC[i]->getFunction());
49 DEBUG(std::cerr << " " << (SCC[i]->getFunction() ?
50 SCC[i]->getFunction()->getName() : "INDIRECTNODE"));
52 DEBUG(std::cerr << "\n");
55 for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(),
56 E = SCCFunctions.end(); SCCI != E; ++SCCI) {
58 if (F == 0 || F->isExternal()) continue;
59 DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n");
61 // Scan through and identify all call sites ahead of time so that we only
62 // inline call sites in the original functions, not call sites that result
63 // in inlining other functions.
64 std::vector<CallSite> CallSites;
65 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
66 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
67 CallSite CS = CallSite::get(I);
68 if (CS.getInstruction() && CS.getCalledFunction() &&
69 !CS.getCalledFunction()->isExternal())
70 CallSites.push_back(CS);
73 // Now that we have all of the call sites, loop over them and inline them if
74 // it looks profitable to do so.
75 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
76 CallSite CS = CallSites[i];
77 Function *Callee = CS.getCalledFunction();
78 // Determine whether this is a function IN the SCC...
79 bool inSCC = SCCFunctions.count(Callee);
81 // If the policy determines that we should inline this function,
83 int InlineCost = inSCC ? getRecursiveInlineCost(CS) : getInlineCost(CS);
84 if (InlineCost >= (int)InlineThreshold) {
85 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
86 << ", Call: " << *CS.getInstruction());
88 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
89 << ", Call: " << *CS.getInstruction());
91 Function *Caller = CS.getInstruction()->getParent()->getParent();
93 // Attempt to inline the function...
94 if (InlineFunction(CS)) {
97 // Update the call graph by deleting the edge from Callee to Caller
98 CallGraphNode *CalleeNode = CG[Callee];
99 CallGraphNode *CallerNode = CG[Caller];
100 CallerNode->removeCallEdgeTo(CalleeNode);
102 // Since we inlined all uninlinable call sites in the callee into the
103 // caller, add edges from the caller to all of the callees of the
105 for (CallGraphNode::iterator I = CalleeNode->begin(),
106 E = CalleeNode->end(); I != E; ++I)
107 CallerNode->addCalledFunction(*I);
109 // If the only remaining use of the function is a dead constant
110 // pointer ref, remove it.
111 if (Callee->hasOneUse())
112 if (ConstantPointerRef *CPR =
113 dyn_cast<ConstantPointerRef>(Callee->use_back()))
114 if (CPR->use_empty())
115 CPR->destroyConstant();
117 // If we inlined the last possible call site to the function,
118 // delete the function body now.
119 if (Callee->use_empty() && Callee != Caller &&
120 Callee->hasInternalLinkage()) {
121 DEBUG(std::cerr << " -> Deleting dead function: "
122 << Callee->getName() << "\n");
123 SCCFunctions.erase(Callee); // Remove function from this SCC.
125 // Remove any call graph edges from the callee to its callees.
126 while (CalleeNode->begin() != CalleeNode->end())
127 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
129 // Removing the node for callee from the call graph and delete it.
130 delete CG.removeFunctionFromModule(CalleeNode);
142 // doFinalization - Remove now-dead linkonce functions at the end of
143 // processing to avoid breaking the SCC traversal.
144 bool Inliner::doFinalization(CallGraph &CG) {
145 bool Changed = false;
146 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ) {
147 CallGraphNode *CGN = (++I)->second;
148 Function *F = CGN ? CGN->getFunction() : 0;
149 if (F && (F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
151 // Remove any call graph edges from the callee to its callees.
152 while (CGN->begin() != CGN->end())
153 CGN->removeCallEdgeTo(*(CGN->end()-1));
155 // If the function has external linkage (basically if it's a
156 // linkonce function) remove the edge from the external node to the
158 if (!F->hasInternalLinkage())
159 CG.getExternalCallingNode()->removeCallEdgeTo(CGN);
161 // Removing the node for callee from the call graph and delete it.
162 delete CG.removeFunctionFromModule(CGN);