blob: 36955e1f4dcd97c6a2485e17a2a3dcac0b679b0c [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");
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)"));
34}
35
36Inliner::Inliner() : InlineThreshold(InlineLimit) {}
37
Chris Lattnerbefa4992004-05-23 21:22:17 +000038// InlineCallIfPossible - If it is possible to inline the specified call site,
39// do so and update the CallGraph for this operation.
40static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
41 const std::set<Function*> &SCCFunctions) {
42 Function *Caller = CS.getInstruction()->getParent()->getParent();
43 Function *Callee = CS.getCalledFunction();
44 if (!InlineFunction(CS)) return false;
45
46 // Update the call graph by deleting the edge from Callee to Caller
47 CallGraphNode *CalleeNode = CG[Callee];
48 CallGraphNode *CallerNode = CG[Caller];
49 CallerNode->removeCallEdgeTo(CalleeNode);
50
51 // Since we inlined all uninlined call sites in the callee into the caller,
52 // add edges from the caller to all of the callees of the callee.
53 for (CallGraphNode::iterator I = CalleeNode->begin(),
54 E = CalleeNode->end(); I != E; ++I)
55 CallerNode->addCalledFunction(*I);
Misha Brukmanfd939082005-04-21 23:48:37 +000056
Chris Lattner54970c02004-08-08 03:29:50 +000057 // If we inlined the last possible call site to the function, delete the
58 // function body now.
Chris Lattnerbefa4992004-05-23 21:22:17 +000059 if (Callee->use_empty() && Callee->hasInternalLinkage() &&
60 !SCCFunctions.count(Callee)) {
61 DEBUG(std::cerr << " -> Deleting dead function: "
62 << Callee->getName() << "\n");
Misha Brukmanfd939082005-04-21 23:48:37 +000063
Chris Lattnerbefa4992004-05-23 21:22:17 +000064 // Remove any call graph edges from the callee to its callees.
65 while (CalleeNode->begin() != CalleeNode->end())
66 CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
Misha Brukmanfd939082005-04-21 23:48:37 +000067
Chris Lattnerbefa4992004-05-23 21:22:17 +000068 // Removing the node for callee from the call graph and delete it.
69 delete CG.removeFunctionFromModule(CalleeNode);
70 ++NumDeleted;
71 }
72 return true;
Chris Lattner237ef562003-08-31 19:10:30 +000073}
74
75bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
76 CallGraph &CG = getAnalysis<CallGraph>();
77
78 std::set<Function*> SCCFunctions;
79 DEBUG(std::cerr << "Inliner visiting SCC:");
80 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Chris Lattnerbefa4992004-05-23 21:22:17 +000081 Function *F = SCC[i]->getFunction();
82 if (F) SCCFunctions.insert(F);
83 DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
Chris Lattner237ef562003-08-31 19:10:30 +000084 }
Chris Lattner237ef562003-08-31 19:10:30 +000085
Chris Lattnerbefa4992004-05-23 21:22:17 +000086 // Scan through and identify all call sites ahead of time so that we only
87 // inline call sites in the original functions, not call sites that result
88 // from inlining other functions.
89 std::vector<CallSite> CallSites;
90
Chris Lattnercf5933a2004-06-20 04:11:48 +000091 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
92 if (Function *F = SCC[i]->getFunction())
Chris Lattnerbefa4992004-05-23 21:22:17 +000093 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
94 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
95 CallSite CS = CallSite::get(I);
96 if (CS.getInstruction() && (!CS.getCalledFunction() ||
97 !CS.getCalledFunction()->isExternal()))
98 CallSites.push_back(CS);
99 }
Chris Lattner237ef562003-08-31 19:10:30 +0000100
Chris Lattnerbefa4992004-05-23 21:22:17 +0000101 DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
Misha Brukmanfd939082005-04-21 23:48:37 +0000102
Chris Lattnerbefa4992004-05-23 21:22:17 +0000103 // Now that we have all of the call sites, move the ones to functions in the
104 // current SCC to the end of the list.
105 unsigned FirstCallInSCC = CallSites.size();
106 for (unsigned i = 0; i < FirstCallInSCC; ++i)
107 if (Function *F = CallSites[i].getCalledFunction())
108 if (SCCFunctions.count(F))
109 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
Misha Brukmanfd939082005-04-21 23:48:37 +0000110
Chris Lattnerbefa4992004-05-23 21:22:17 +0000111 // Now that we have all of the call sites, loop over them and inline them if
112 // it looks profitable to do so.
113 bool Changed = false;
114 bool LocalChange;
115 do {
116 LocalChange = false;
117 // Iterate over the outer loop because inlining functions can cause indirect
118 // calls to become direct calls.
119 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
120 if (Function *Callee = CallSites[CSi].getCalledFunction()) {
121 // Calls to external functions are never inlinable.
122 if (Callee->isExternal() ||
123 CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
124 std::swap(CallSites[CSi], CallSites.back());
Chris Lattnerb11a99b2004-05-24 06:24:46 +0000125 CallSites.pop_back();
Chris Lattnerbefa4992004-05-23 21:22:17 +0000126 --CSi;
127 continue;
128 }
Chris Lattner775cbdd2004-04-08 06:34:31 +0000129
Chris Lattnerbefa4992004-05-23 21:22:17 +0000130 // If the policy determines that we should inline this function,
131 // try to do so.
132 CallSite CS = CallSites[CSi];
133 int InlineCost = getInlineCost(CS);
134 if (InlineCost >= (int)InlineThreshold) {
135 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
136 << ", Call: " << *CS.getInstruction());
137 } else {
138 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
139 << ", Call: " << *CS.getInstruction());
Misha Brukmanfd939082005-04-21 23:48:37 +0000140
Chris Lattnerbefa4992004-05-23 21:22:17 +0000141 Function *Caller = CS.getInstruction()->getParent()->getParent();
Chris Lattner775cbdd2004-04-08 06:34:31 +0000142
Chris Lattnerbefa4992004-05-23 21:22:17 +0000143 // Attempt to inline the function...
144 if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
145 // Remove this call site from the list.
146 std::swap(CallSites[CSi], CallSites.back());
147 CallSites.pop_back();
148 --CSi;
Chris Lattnerce1a3a22004-04-12 05:37:29 +0000149
Chris Lattnerbefa4992004-05-23 21:22:17 +0000150 ++NumInlined;
151 Changed = true;
152 LocalChange = true;
Chris Lattner775cbdd2004-04-08 06:34:31 +0000153 }
Chris Lattner775cbdd2004-04-08 06:34:31 +0000154 }
155 }
Chris Lattnerbefa4992004-05-23 21:22:17 +0000156 } while (LocalChange);
Chris Lattner775cbdd2004-04-08 06:34:31 +0000157
Chris Lattner237ef562003-08-31 19:10:30 +0000158 return Changed;
159}
160
Chris Lattner68d57e72004-04-20 22:06:53 +0000161// doFinalization - Remove now-dead linkonce functions at the end of
162// processing to avoid breaking the SCC traversal.
163bool Inliner::doFinalization(CallGraph &CG) {
Chris Lattner3e1358a2004-04-21 20:44:33 +0000164 std::set<CallGraphNode*> FunctionsToRemove;
165
166 // Scan for all of the functions, looking for ones that should now be removed
167 // from the program. Insert the dead ones in the FunctionsToRemove set.
168 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
169 CallGraphNode *CGN = I->second;
Chris Lattner54970c02004-08-08 03:29:50 +0000170 if (Function *F = CGN ? CGN->getFunction() : 0) {
Chris Lattner0c0aa712004-09-18 21:37:03 +0000171 // If the only remaining users of the function are dead constants, remove
172 // them.
Chris Lattner54970c02004-08-08 03:29:50 +0000173 F->removeDeadConstantUsers();
Chris Lattner3e1358a2004-04-21 20:44:33 +0000174
Chris Lattner54970c02004-08-08 03:29:50 +0000175 if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
176 F->use_empty()) {
Chris Lattner0c0aa712004-09-18 21:37:03 +0000177
Chris Lattner54970c02004-08-08 03:29:50 +0000178 // Remove any call graph edges from the function to its callees.
179 while (CGN->begin() != CGN->end())
180 CGN->removeCallEdgeTo(*(CGN->end()-1));
Misha Brukmanfd939082005-04-21 23:48:37 +0000181
Chris Lattner0c0aa712004-09-18 21:37:03 +0000182 // Remove any edges from the external node to the function's call graph
183 // node. These edges might have been made irrelegant due to
184 // optimization of the program.
185 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000186
Chris Lattner54970c02004-08-08 03:29:50 +0000187 // Removing the node for callee from the call graph and delete it.
188 FunctionsToRemove.insert(CGN);
189 }
Chris Lattner68d57e72004-04-20 22:06:53 +0000190 }
191 }
Chris Lattner3e1358a2004-04-21 20:44:33 +0000192
193 // Now that we know which functions to delete, do so. We didn't want to do
194 // this inline, because that would invalidate our CallGraph::iterator
195 // objects. :(
196 bool Changed = false;
197 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
198 E = FunctionsToRemove.end(); I != E; ++I) {
199 delete CG.removeFunctionFromModule(*I);
200 ++NumDeleted;
201 Changed = true;
202 }
203
Chris Lattner68d57e72004-04-20 22:06:53 +0000204 return Changed;
205}