blob: ef239bd7b9756296fa0bdcfb4e5877af23ac4406 [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 Lattnerc597b8a2006-01-22 23:32:06 +000025#include <iostream>
Chris Lattner6754b822004-05-23 21:22:17 +000026#include <set>
Chris Lattnera82f1312003-11-21 21:45:31 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattnerd075cc22003-08-31 19:10:30 +000029namespace {
30 Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattner49c4d532006-01-13 18:06:56 +000031 Statistic<> NumDeleted("inline",
32 "Number of functions deleted because all callers found");
Chris Lattnerd075cc22003-08-31 19:10:30 +000033 cl::opt<unsigned> // FIXME: 200 is VERY conservative
34 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
Chris Lattner49c4d532006-01-13 18:06:56 +000035 cl::desc("Control the amount of inlining to perform (default = 200)"));
Chris Lattnerd075cc22003-08-31 19:10:30 +000036}
37
38Inliner::Inliner() : InlineThreshold(InlineLimit) {}
39
Chris Lattner6754b822004-05-23 21:22:17 +000040// InlineCallIfPossible - If it is possible to inline the specified call site,
41// do so and update the CallGraph for this operation.
42static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
43 const std::set<Function*> &SCCFunctions) {
Chris Lattner6754b822004-05-23 21:22:17 +000044 Function *Callee = CS.getCalledFunction();
Chris Lattnerf6d68232006-01-14 20:09:18 +000045 if (!InlineFunction(CS, &CG)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000046
Chris Lattner4956a322004-08-08 03:29:50 +000047 // If we inlined the last possible call site to the function, delete the
48 // function body now.
Chris Lattner6754b822004-05-23 21:22:17 +000049 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
50 !SCCFunctions.count(Callee)) {
51 DEBUG(std::cerr << " -> Deleting dead function: "
52 << Callee->getName() << "\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +000053
Chris Lattner6754b822004-05-23 21:22:17 +000054 // Remove any call graph edges from the callee to its callees.
Chris Lattnerf6d68232006-01-14 20:09:18 +000055 CallGraphNode *CalleeNode = CG[Callee];
Chris Lattner6754b822004-05-23 21:22:17 +000056 while (CalleeNode->begin() != CalleeNode->end())
Chris Lattner5de3b8b2006-07-12 18:29:36 +000057 CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
Misha Brukmanb1c93172005-04-21 23:48:37 +000058
Chris Lattner6754b822004-05-23 21:22:17 +000059 // Removing the node for callee from the call graph and delete it.
60 delete CG.removeFunctionFromModule(CalleeNode);
61 ++NumDeleted;
62 }
63 return true;
Chris Lattnerd075cc22003-08-31 19:10:30 +000064}
65
66bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
67 CallGraph &CG = getAnalysis<CallGraph>();
68
69 std::set<Function*> SCCFunctions;
70 DEBUG(std::cerr << "Inliner visiting SCC:");
71 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner6754b822004-05-23 21:22:17 +000072 Function *F = SCC[i]->getFunction();
73 if (F) SCCFunctions.insert(F);
74 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
Chris Lattnerd075cc22003-08-31 19:10:30 +000075 }
Chris Lattnerd075cc22003-08-31 19:10:30 +000076
Chris Lattner6754b822004-05-23 21:22:17 +000077 // 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;
81
Chris Lattner7d30a6c2004-06-20 04:11:48 +000082 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
83 if (Function *F = SCC[i]->getFunction())
Chris Lattner6754b822004-05-23 21:22:17 +000084 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);
90 }
Chris Lattnerd075cc22003-08-31 19:10:30 +000091
Chris Lattner6754b822004-05-23 21:22:17 +000092 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +000093
Chris Lattner6754b822004-05-23 21:22:17 +000094 // 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]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101
Chris Lattner6754b822004-05-23 21:22:17 +0000102 // 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;
105 bool LocalChange;
106 do {
107 LocalChange = 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){
Chris Lattner82928ca2006-11-09 23:36:08 +0000115 if (SCC.size() == 1) {
116 std::swap(CallSites[CSi], CallSites.back());
117 CallSites.pop_back();
118 } else {
119 // Keep the 'in SCC / not in SCC' boundary correct.
120 CallSites.erase(CallSites.begin()+CSi);
121 }
Chris Lattner6754b822004-05-23 21:22:17 +0000122 --CSi;
123 continue;
124 }
Chris Lattner4d25c862004-04-08 06:34:31 +0000125
Chris Lattner6754b822004-05-23 21:22:17 +0000126 // If the policy determines that we should inline this function,
127 // try to do so.
128 CallSite CS = CallSites[CSi];
129 int InlineCost = getInlineCost(CS);
130 if (InlineCost >= (int)InlineThreshold) {
131 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
132 << ", Call: " << *CS.getInstruction());
133 } else {
134 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
135 << ", Call: " << *CS.getInstruction());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000136
Chris Lattner6754b822004-05-23 21:22:17 +0000137 // Attempt to inline the function...
138 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
Chris Lattner82928ca2006-11-09 23:36:08 +0000139 // Remove this call site from the list. If possible, use
140 // swap/pop_back for efficiency, but do not use it if doing so would
141 // move a call site to a function in this SCC before the
142 // 'FirstCallInSCC' barrier.
143 if (SCC.size() == 1) {
144 std::swap(CallSites[CSi], CallSites.back());
145 CallSites.pop_back();
146 } else {
147 CallSites.erase(CallSites.begin()+CSi);
148 }
Chris Lattner6754b822004-05-23 21:22:17 +0000149 --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())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000181 CGN->removeCallEdgeTo((CGN->end()-1)->second);
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}