blob: 65cc47db4034ba3e0abc8d63d10618c8c7d5ccd7 [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) {
44 Function *Caller = CS.getInstruction()->getParent()->getParent();
45 Function *Callee = CS.getCalledFunction();
Chris Lattnerf6d68232006-01-14 20:09:18 +000046 if (!InlineFunction(CS, &CG)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000047
Chris Lattner4956a322004-08-08 03:29:50 +000048 // If we inlined the last possible call site to the function, delete the
49 // function body now.
Chris Lattner6754b822004-05-23 21:22:17 +000050 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
51 !SCCFunctions.count(Callee)) {
52 DEBUG(std::cerr << " -> Deleting dead function: "
53 << Callee->getName() << "\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +000054
Chris Lattner6754b822004-05-23 21:22:17 +000055 // Remove any call graph edges from the callee to its callees.
Chris Lattnerf6d68232006-01-14 20:09:18 +000056 CallGraphNode *CalleeNode = CG[Callee];
Chris Lattner6754b822004-05-23 21:22:17 +000057 while (CalleeNode->begin() != CalleeNode->end())
58 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
Misha Brukmanb1c93172005-04-21 23:48:37 +000059
Chris Lattner6754b822004-05-23 21:22:17 +000060 // Removing the node for callee from the call graph and delete it.
61 delete CG.removeFunctionFromModule(CalleeNode);
62 ++NumDeleted;
63 }
64 return true;
Chris Lattnerd075cc22003-08-31 19:10:30 +000065}
66
67bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
68 CallGraph &CG = getAnalysis<CallGraph>();
69
70 std::set<Function*> SCCFunctions;
71 DEBUG(std::cerr << "Inliner visiting SCC:");
72 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattner6754b822004-05-23 21:22:17 +000073 Function *F = SCC[i]->getFunction();
74 if (F) SCCFunctions.insert(F);
75 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
Chris Lattnerd075cc22003-08-31 19:10:30 +000076 }
Chris Lattnerd075cc22003-08-31 19:10:30 +000077
Chris Lattner6754b822004-05-23 21:22:17 +000078 // 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;
82
Chris Lattner7d30a6c2004-06-20 04:11:48 +000083 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
84 if (Function *F = SCC[i]->getFunction())
Chris Lattner6754b822004-05-23 21:22:17 +000085 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);
91 }
Chris Lattnerd075cc22003-08-31 19:10:30 +000092
Chris Lattner6754b822004-05-23 21:22:17 +000093 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +000094
Chris Lattner6754b822004-05-23 21:22:17 +000095 // 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]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000102
Chris Lattner6754b822004-05-23 21:22:17 +0000103 // 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;
106 bool LocalChange;
107 do {
108 LocalChange = 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());
Chris Lattner6f027142004-05-24 06:24:46 +0000117 CallSites.pop_back();
Chris Lattner6754b822004-05-23 21:22:17 +0000118 --CSi;
119 continue;
120 }
Chris Lattner4d25c862004-04-08 06:34:31 +0000121
Chris Lattner6754b822004-05-23 21:22:17 +0000122 // If the policy determines that we should inline this function,
123 // try to do so.
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());
129 } else {
130 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
131 << ", Call: " << *CS.getInstruction());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000132
Chris Lattner6754b822004-05-23 21:22:17 +0000133 Function *Caller = CS.getInstruction()->getParent()->getParent();
Chris Lattner4d25c862004-04-08 06:34:31 +0000134
Chris Lattner6754b822004-05-23 21:22:17 +0000135 // 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();
140 --CSi;
Chris Lattnerbe435442004-04-12 05:37:29 +0000141
Chris Lattner6754b822004-05-23 21:22:17 +0000142 ++NumInlined;
143 Changed = true;
144 LocalChange = true;
Chris Lattner4d25c862004-04-08 06:34:31 +0000145 }
Chris Lattner4d25c862004-04-08 06:34:31 +0000146 }
147 }
Chris Lattner6754b822004-05-23 21:22:17 +0000148 } while (LocalChange);
Chris Lattner4d25c862004-04-08 06:34:31 +0000149
Chris Lattnerd075cc22003-08-31 19:10:30 +0000150 return Changed;
151}
152
Chris Lattnerc87784f2004-04-20 22:06:53 +0000153// doFinalization - Remove now-dead linkonce functions at the end of
154// processing to avoid breaking the SCC traversal.
155bool Inliner::doFinalization(CallGraph &CG) {
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000156 std::set<CallGraphNode*> FunctionsToRemove;
157
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;
Chris Lattner4956a322004-08-08 03:29:50 +0000162 if (Function *F = CGN ? CGN->getFunction() : 0) {
Chris Lattner6455c512004-09-18 21:37:03 +0000163 // If the only remaining users of the function are dead constants, remove
164 // them.
Chris Lattner4956a322004-08-08 03:29:50 +0000165 F->removeDeadConstantUsers();
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000166
Chris Lattner4956a322004-08-08 03:29:50 +0000167 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
168 F->use_empty()) {
Chris Lattner6455c512004-09-18 21:37:03 +0000169
Chris Lattner4956a322004-08-08 03:29:50 +0000170 // Remove any call graph edges from the function to its callees.
171 while (CGN->begin() != CGN->end())
172 CGN->removeCallEdgeTo(*(CGN->end()-1));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000173
Chris Lattner6455c512004-09-18 21:37:03 +0000174 // 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);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000178
Chris Lattner4956a322004-08-08 03:29:50 +0000179 // Removing the node for callee from the call graph and delete it.
180 FunctionsToRemove.insert(CGN);
181 }
Chris Lattnerc87784f2004-04-20 22:06:53 +0000182 }
183 }
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000184
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
187 // objects. :(
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);
192 ++NumDeleted;
193 Changed = true;
194 }
195
Chris Lattnerc87784f2004-04-20 22:06:53 +0000196 return Changed;
197}