blob: ea43dc21da356f11f2fdf668c001e9c386a41372 [file] [log] [blame]
Chris Lattnercf5933a2004-06-20 04:11:48 +00001//===- Inliner.cpp - Code common to all inliners --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner237ef562003-08-31 19:10:30 +00009//
Chris Lattnerbefa4992004-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 Lattner237ef562003-08-31 19:10:30 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "Inliner.h"
17#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner237ef562003-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 Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattnerbefa4992004-05-23 21:22:17 +000025#include <set>
Chris Lattnera51bcb52003-11-21 21:45:31 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner237ef562003-08-31 19:10:30 +000028namespace {
29 Statistic<> NumInlined("inline", "Number of functions inlined");
Chris Lattner8acb2492006-01-13 18:06:56 +000030 Statistic<> NumDeleted("inline",
31 "Number of functions deleted because all callers found");
Chris Lattner237ef562003-08-31 19:10:30 +000032 cl::opt<unsigned> // FIXME: 200 is VERY conservative
33 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
Chris Lattner8acb2492006-01-13 18:06:56 +000034 cl::desc("Control the amount of inlining to perform (default = 200)"));
Chris Lattner237ef562003-08-31 19:10:30 +000035}
36
37Inliner::Inliner() : InlineThreshold(InlineLimit) {}
38
Chris Lattnerbefa4992004-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) {
Chris Lattnerbefa4992004-05-23 21:22:17 +000043 Function *Callee = CS.getCalledFunction();
Chris Lattner432a2052006-01-14 20:09:18 +000044 if (!InlineFunction(CS, &CG)) return false;
Misha Brukmanfd939082005-04-21 23:48:37 +000045
Chris Lattner54970c02004-08-08 03:29:50 +000046 // If we inlined the last possible call site to the function, delete the
47 // function body now.
Chris Lattnerbefa4992004-05-23 21:22:17 +000048 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
49 !SCCFunctions.count(Callee)) {
Bill Wendling0a81aac2006-11-26 10:02:32 +000050 DOUT << " -> Deleting dead function: " << Callee->getName() << "\n";
Misha Brukmanfd939082005-04-21 23:48:37 +000051
Chris Lattnerbefa4992004-05-23 21:22:17 +000052 // Remove any call graph edges from the callee to its callees.
Chris Lattner432a2052006-01-14 20:09:18 +000053 CallGraphNode *CalleeNode = CG[Callee];
Chris Lattnerbefa4992004-05-23 21:22:17 +000054 while (CalleeNode->begin() != CalleeNode->end())
Chris Lattnerd85340f2006-07-12 18:29:36 +000055 CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
Misha Brukmanfd939082005-04-21 23:48:37 +000056
Chris Lattnerbefa4992004-05-23 21:22:17 +000057 // Removing the node for callee from the call graph and delete it.
58 delete CG.removeFunctionFromModule(CalleeNode);
59 ++NumDeleted;
60 }
61 return true;
Chris Lattner237ef562003-08-31 19:10:30 +000062}
63
64bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
65 CallGraph &CG = getAnalysis<CallGraph>();
66
67 std::set<Function*> SCCFunctions;
Bill Wendling0a81aac2006-11-26 10:02:32 +000068 DOUT << "Inliner visiting SCC:";
Chris Lattner237ef562003-08-31 19:10:30 +000069 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattnerbefa4992004-05-23 21:22:17 +000070 Function *F = SCC[i]->getFunction();
71 if (F) SCCFunctions.insert(F);
Bill Wendling0a81aac2006-11-26 10:02:32 +000072 DOUT << " " << (F ? F->getName() : "INDIRECTNODE");
Chris Lattner237ef562003-08-31 19:10:30 +000073 }
Chris Lattner237ef562003-08-31 19:10:30 +000074
Chris Lattnerbefa4992004-05-23 21:22:17 +000075 // Scan through and identify all call sites ahead of time so that we only
76 // inline call sites in the original functions, not call sites that result
77 // from inlining other functions.
78 std::vector<CallSite> CallSites;
79
Chris Lattnercf5933a2004-06-20 04:11:48 +000080 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
81 if (Function *F = SCC[i]->getFunction())
Chris Lattnerbefa4992004-05-23 21:22:17 +000082 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
83 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
84 CallSite CS = CallSite::get(I);
85 if (CS.getInstruction() && (!CS.getCalledFunction() ||
86 !CS.getCalledFunction()->isExternal()))
87 CallSites.push_back(CS);
88 }
Chris Lattner237ef562003-08-31 19:10:30 +000089
Bill Wendling0a81aac2006-11-26 10:02:32 +000090 DOUT << ": " << CallSites.size() << " call sites.\n";
Misha Brukmanfd939082005-04-21 23:48:37 +000091
Chris Lattnerbefa4992004-05-23 21:22:17 +000092 // Now that we have all of the call sites, move the ones to functions in the
93 // current SCC to the end of the list.
94 unsigned FirstCallInSCC = CallSites.size();
95 for (unsigned i = 0; i < FirstCallInSCC; ++i)
96 if (Function *F = CallSites[i].getCalledFunction())
97 if (SCCFunctions.count(F))
98 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
Misha Brukmanfd939082005-04-21 23:48:37 +000099
Chris Lattnerbefa4992004-05-23 21:22:17 +0000100 // Now that we have all of the call sites, loop over them and inline them if
101 // it looks profitable to do so.
102 bool Changed = false;
103 bool LocalChange;
104 do {
105 LocalChange = false;
106 // Iterate over the outer loop because inlining functions can cause indirect
107 // calls to become direct calls.
108 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
109 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
110 // Calls to external functions are never inlinable.
111 if (Callee->isExternal() ||
112 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
Chris Lattner08ff1482006-11-09 23:36:08 +0000113 if (SCC.size() == 1) {
114 std::swap(CallSites[CSi], CallSites.back());
115 CallSites.pop_back();
116 } else {
117 // Keep the 'in SCC / not in SCC' boundary correct.
118 CallSites.erase(CallSites.begin()+CSi);
119 }
Chris Lattnerbefa4992004-05-23 21:22:17 +0000120 --CSi;
121 continue;
122 }
Chris Lattner775cbdd2004-04-08 06:34:31 +0000123
Chris Lattnerbefa4992004-05-23 21:22:17 +0000124 // If the policy determines that we should inline this function,
125 // try to do so.
126 CallSite CS = CallSites[CSi];
127 int InlineCost = getInlineCost(CS);
128 if (InlineCost >= (int)InlineThreshold) {
Bill Wendling0a81aac2006-11-26 10:02:32 +0000129 DOUT << " NOT Inlining: cost=" << InlineCost
130 << ", Call: " << *CS.getInstruction();
Chris Lattnerbefa4992004-05-23 21:22:17 +0000131 } else {
Bill Wendling0a81aac2006-11-26 10:02:32 +0000132 DOUT << " Inlining: cost=" << InlineCost
133 << ", Call: " << *CS.getInstruction();
Misha Brukmanfd939082005-04-21 23:48:37 +0000134
Chris Lattnerbefa4992004-05-23 21:22:17 +0000135 // Attempt to inline the function...
136 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
Chris Lattner08ff1482006-11-09 23:36:08 +0000137 // Remove this call site from the list. If possible, use
138 // swap/pop_back for efficiency, but do not use it if doing so would
139 // move a call site to a function in this SCC before the
140 // 'FirstCallInSCC' barrier.
141 if (SCC.size() == 1) {
142 std::swap(CallSites[CSi], CallSites.back());
143 CallSites.pop_back();
144 } else {
145 CallSites.erase(CallSites.begin()+CSi);
146 }
Chris Lattnerbefa4992004-05-23 21:22:17 +0000147 --CSi;
Chris Lattnerce1a3a22004-04-12 05:37:29 +0000148
Chris Lattnerbefa4992004-05-23 21:22:17 +0000149 ++NumInlined;
150 Changed = true;
151 LocalChange = true;
Chris Lattner775cbdd2004-04-08 06:34:31 +0000152 }
Chris Lattner775cbdd2004-04-08 06:34:31 +0000153 }
154 }
Chris Lattnerbefa4992004-05-23 21:22:17 +0000155 } while (LocalChange);
Chris Lattner775cbdd2004-04-08 06:34:31 +0000156
Chris Lattner237ef562003-08-31 19:10:30 +0000157 return Changed;
158}
159
Chris Lattner68d57e72004-04-20 22:06:53 +0000160// doFinalization - Remove now-dead linkonce functions at the end of
161// processing to avoid breaking the SCC traversal.
162bool Inliner::doFinalization(CallGraph &CG) {
Chris Lattner3e1358a2004-04-21 20:44:33 +0000163 std::set<CallGraphNode*> FunctionsToRemove;
164
165 // Scan for all of the functions, looking for ones that should now be removed
166 // from the program. Insert the dead ones in the FunctionsToRemove set.
167 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
168 CallGraphNode *CGN = I->second;
Chris Lattner54970c02004-08-08 03:29:50 +0000169 if (Function *F = CGN ? CGN->getFunction() : 0) {
Chris Lattner0c0aa712004-09-18 21:37:03 +0000170 // If the only remaining users of the function are dead constants, remove
171 // them.
Chris Lattner54970c02004-08-08 03:29:50 +0000172 F->removeDeadConstantUsers();
Chris Lattner3e1358a2004-04-21 20:44:33 +0000173
Chris Lattner54970c02004-08-08 03:29:50 +0000174 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
175 F->use_empty()) {
Chris Lattner0c0aa712004-09-18 21:37:03 +0000176
Chris Lattner54970c02004-08-08 03:29:50 +0000177 // Remove any call graph edges from the function to its callees.
178 while (CGN->begin() != CGN->end())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000179 CGN->removeCallEdgeTo((CGN->end()-1)->second);
Misha Brukmanfd939082005-04-21 23:48:37 +0000180
Chris Lattner0c0aa712004-09-18 21:37:03 +0000181 // Remove any edges from the external node to the function's call graph
182 // node. These edges might have been made irrelegant due to
183 // optimization of the program.
184 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000185
Chris Lattner54970c02004-08-08 03:29:50 +0000186 // Removing the node for callee from the call graph and delete it.
187 FunctionsToRemove.insert(CGN);
188 }
Chris Lattner68d57e72004-04-20 22:06:53 +0000189 }
190 }
Chris Lattner3e1358a2004-04-21 20:44:33 +0000191
192 // Now that we know which functions to delete, do so. We didn't want to do
193 // this inline, because that would invalidate our CallGraph::iterator
194 // objects. :(
195 bool Changed = false;
196 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
197 E = FunctionsToRemove.end(); I != E; ++I) {
198 delete CG.removeFunctionFromModule(*I);
199 ++NumDeleted;
200 Changed = true;
201 }
202
Chris Lattner68d57e72004-04-20 22:06:53 +0000203 return Changed;
204}