blob: a10879a15079e8ea5a54feeeedf0ed3f7e82cbbb [file] [log] [blame]
Chris Lattner7d30a6c2004-06-20 04:11:48 +00001//===- Inliner.cpp - Code common to all inliners --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd075cc22003-08-31 19:10:30 +00009//
Chris Lattner6754b822004-05-23 21:22:17 +000010// 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.
Chris Lattnerd075cc22003-08-31 19:10:30 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "Inliner.h"
17#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000019#include "llvm/Analysis/CallGraph.h"
20#include "llvm/Support/CallSite.h"
21#include "llvm/Transforms/Utils/Cloning.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner6754b822004-05-23 21:22:17 +000025#include <set>
Chris Lattnera82f1312003-11-21 21:45:31 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattnerd075cc22003-08-31 19:10:30 +000028namespace {
29 Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattner49c4d532006-01-13 18:06:56 +000030 Statistic<> NumDeleted("inline",
31 "Number of functions deleted because all callers found");
Chris Lattnerd075cc22003-08-31 19:10:30 +000032 cl::opt<unsigned> // FIXME: 200 is VERY conservative
33 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
Chris Lattner49c4d532006-01-13 18:06:56 +000034 cl::desc("Control the amount of inlining to perform (default = 200)"));
Chris Lattnerd075cc22003-08-31 19:10:30 +000035}
36
37Inliner::Inliner() : InlineThreshold(InlineLimit) {}
38
Chris Lattner6754b822004-05-23 21:22:17 +000039// InlineCallIfPossible - If it is possible to inline the specified call site,
40// do so and update the CallGraph for this operation.
41static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
42 const std::set<Function*> &SCCFunctions) {
43 Function *Caller = CS.getInstruction()->getParent()->getParent();
44 Function *Callee = CS.getCalledFunction();
45 if (!InlineFunction(CS)) return false;
46
47 // Update the call graph by deleting the edge from Callee to Caller
48 CallGraphNode *CalleeNode = CG[Callee];
49 CallGraphNode *CallerNode = CG[Caller];
50 CallerNode->removeCallEdgeTo(CalleeNode);
51
52 // Since we inlined all uninlined call sites in the callee into the caller,
53 // add edges from the caller to all of the callees of the callee.
54 for (CallGraphNode::iterator I = CalleeNode->begin(),
55 E = CalleeNode->end(); I != E; ++I)
56 CallerNode->addCalledFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000057
Chris Lattner4956a322004-08-08 03:29:50 +000058 // If we inlined the last possible call site to the function, delete the
59 // function body now.
Chris Lattner6754b822004-05-23 21:22:17 +000060 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
61 !SCCFunctions.count(Callee)) {
62 DEBUG(std::cerr << " -> Deleting dead function: "
63 << Callee->getName() << "\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +000064
Chris Lattner6754b822004-05-23 21:22:17 +000065 // Remove any call graph edges from the callee to its callees.
66 while (CalleeNode->begin() != CalleeNode->end())
67 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
Misha Brukmanb1c93172005-04-21 23:48:37 +000068
Chris Lattner6754b822004-05-23 21:22:17 +000069 // Removing the node for callee from the call graph and delete it.
70 delete CG.removeFunctionFromModule(CalleeNode);
71 ++NumDeleted;
72 }
73 return true;
Chris Lattnerd075cc22003-08-31 19:10:30 +000074}
75
76bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
77 CallGraph &CG = getAnalysis<CallGraph>();
78
79 std::set<Function*> SCCFunctions;
80 DEBUG(std::cerr << "Inliner visiting SCC:");
81 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner6754b822004-05-23 21:22:17 +000082 Function *F = SCC[i]->getFunction();
83 if (F) SCCFunctions.insert(F);
84 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
Chris Lattnerd075cc22003-08-31 19:10:30 +000085 }
Chris Lattnerd075cc22003-08-31 19:10:30 +000086
Chris Lattner6754b822004-05-23 21:22:17 +000087 // Scan through and identify all call sites ahead of time so that we only
88 // inline call sites in the original functions, not call sites that result
89 // from inlining other functions.
90 std::vector<CallSite> CallSites;
91
Chris Lattner7d30a6c2004-06-20 04:11:48 +000092 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
93 if (Function *F = SCC[i]->getFunction())
Chris Lattner6754b822004-05-23 21:22:17 +000094 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
95 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
96 CallSite CS = CallSite::get(I);
97 if (CS.getInstruction() && (!CS.getCalledFunction() ||
98 !CS.getCalledFunction()->isExternal()))
99 CallSites.push_back(CS);
100 }
Chris Lattnerd075cc22003-08-31 19:10:30 +0000101
Chris Lattner6754b822004-05-23 21:22:17 +0000102 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000103
Chris Lattner6754b822004-05-23 21:22:17 +0000104 // Now that we have all of the call sites, move the ones to functions in the
105 // current SCC to the end of the list.
106 unsigned FirstCallInSCC = CallSites.size();
107 for (unsigned i = 0; i < FirstCallInSCC; ++i)
108 if (Function *F = CallSites[i].getCalledFunction())
109 if (SCCFunctions.count(F))
110 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111
Chris Lattner6754b822004-05-23 21:22:17 +0000112 // Now that we have all of the call sites, loop over them and inline them if
113 // it looks profitable to do so.
114 bool Changed = false;
115 bool LocalChange;
116 do {
117 LocalChange = false;
118 // Iterate over the outer loop because inlining functions can cause indirect
119 // calls to become direct calls.
120 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
121 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
122 // Calls to external functions are never inlinable.
123 if (Callee->isExternal() ||
124 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
125 std::swap(CallSites[CSi], CallSites.back());
Chris Lattner6f027142004-05-24 06:24:46 +0000126 CallSites.pop_back();
Chris Lattner6754b822004-05-23 21:22:17 +0000127 --CSi;
128 continue;
129 }
Chris Lattner4d25c862004-04-08 06:34:31 +0000130
Chris Lattner6754b822004-05-23 21:22:17 +0000131 // If the policy determines that we should inline this function,
132 // try to do so.
133 CallSite CS = CallSites[CSi];
134 int InlineCost = getInlineCost(CS);
135 if (InlineCost >= (int)InlineThreshold) {
136 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
137 << ", Call: " << *CS.getInstruction());
138 } else {
139 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
140 << ", Call: " << *CS.getInstruction());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000141
Chris Lattner6754b822004-05-23 21:22:17 +0000142 Function *Caller = CS.getInstruction()->getParent()->getParent();
Chris Lattner4d25c862004-04-08 06:34:31 +0000143
Chris Lattner6754b822004-05-23 21:22:17 +0000144 // Attempt to inline the function...
145 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
146 // Remove this call site from the list.
147 std::swap(CallSites[CSi], CallSites.back());
148 CallSites.pop_back();
149 --CSi;
Chris Lattnerbe435442004-04-12 05:37:29 +0000150
Chris Lattner6754b822004-05-23 21:22:17 +0000151 ++NumInlined;
152 Changed = true;
153 LocalChange = true;
Chris Lattner4d25c862004-04-08 06:34:31 +0000154 }
Chris Lattner4d25c862004-04-08 06:34:31 +0000155 }
156 }
Chris Lattner6754b822004-05-23 21:22:17 +0000157 } while (LocalChange);
Chris Lattner4d25c862004-04-08 06:34:31 +0000158
Chris Lattnerd075cc22003-08-31 19:10:30 +0000159 return Changed;
160}
161
Chris Lattnerc87784f2004-04-20 22:06:53 +0000162// doFinalization - Remove now-dead linkonce functions at the end of
163// processing to avoid breaking the SCC traversal.
164bool Inliner::doFinalization(CallGraph &CG) {
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000165 std::set<CallGraphNode*> FunctionsToRemove;
166
167 // Scan for all of the functions, looking for ones that should now be removed
168 // from the program. Insert the dead ones in the FunctionsToRemove set.
169 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
170 CallGraphNode *CGN = I->second;
Chris Lattner4956a322004-08-08 03:29:50 +0000171 if (Function *F = CGN ? CGN->getFunction() : 0) {
Chris Lattner6455c512004-09-18 21:37:03 +0000172 // If the only remaining users of the function are dead constants, remove
173 // them.
Chris Lattner4956a322004-08-08 03:29:50 +0000174 F->removeDeadConstantUsers();
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000175
Chris Lattner4956a322004-08-08 03:29:50 +0000176 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
177 F->use_empty()) {
Chris Lattner6455c512004-09-18 21:37:03 +0000178
Chris Lattner4956a322004-08-08 03:29:50 +0000179 // Remove any call graph edges from the function to its callees.
180 while (CGN->begin() != CGN->end())
181 CGN->removeCallEdgeTo(*(CGN->end()-1));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000182
Chris Lattner6455c512004-09-18 21:37:03 +0000183 // Remove any edges from the external node to the function's call graph
184 // node. These edges might have been made irrelegant due to
185 // optimization of the program.
186 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187
Chris Lattner4956a322004-08-08 03:29:50 +0000188 // Removing the node for callee from the call graph and delete it.
189 FunctionsToRemove.insert(CGN);
190 }
Chris Lattnerc87784f2004-04-20 22:06:53 +0000191 }
192 }
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000193
194 // Now that we know which functions to delete, do so. We didn't want to do
195 // this inline, because that would invalidate our CallGraph::iterator
196 // objects. :(
197 bool Changed = false;
198 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
199 E = FunctionsToRemove.end(); I != E; ++I) {
200 delete CG.removeFunctionFromModule(*I);
201 ++NumDeleted;
202 Changed = true;
203 }
204
Chris Lattnerc87784f2004-04-20 22:06:53 +0000205 return Changed;
206}