Detemplatize the Statistic class. The only type it is instantiated with
[oota-llvm.git] / lib / Transforms / IPO / Inliner.cpp
1 //===- Inliner.cpp - Code common to all inliners --------------------------===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "Inliner.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/Transforms/Utils/Cloning.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/Statistic.h"
25 #include <set>
26 using namespace llvm;
27
28 namespace {
29   Statistic NumInlined("inline", "Number of functions inlined");
30   Statistic NumDeleted("inline",
31                        "Number of functions deleted because all callers found");
32   cl::opt<unsigned>             // FIXME: 200 is VERY conservative
33   InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
34         cl::desc("Control the amount of inlining to perform (default = 200)"));
35 }
36
37 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
38
39 // InlineCallIfPossible - If it is possible to inline the specified call site,
40 // do so and update the CallGraph for this operation.
41 static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
42                                  const std::set<Function*> &SCCFunctions) {
43   Function *Callee = CS.getCalledFunction();
44   if (!InlineFunction(CS, &CG)) return false;
45
46   // If we inlined the last possible call site to the function, delete the
47   // function body now.
48   if (Callee->use_empty() && Callee->hasInternalLinkage() &&
49       !SCCFunctions.count(Callee)) {
50     DOUT << "    -> Deleting dead function: " << Callee->getName() << "\n";
51
52     // Remove any call graph edges from the callee to its callees.
53     CallGraphNode *CalleeNode = CG[Callee];
54     while (CalleeNode->begin() != CalleeNode->end())
55       CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
56
57     // Removing the node for callee from the call graph and delete it.
58     delete CG.removeFunctionFromModule(CalleeNode);
59     ++NumDeleted;
60   }
61   return true;
62 }
63
64 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
65   CallGraph &CG = getAnalysis<CallGraph>();
66
67   std::set<Function*> SCCFunctions;
68   DOUT << "Inliner visiting SCC:";
69   for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
70     Function *F = SCC[i]->getFunction();
71     if (F) SCCFunctions.insert(F);
72     DOUT << " " << (F ? F->getName() : "INDIRECTNODE");
73   }
74
75   // 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
80   for (unsigned i = 0, e = SCC.size(); i != e; ++i)
81     if (Function *F = SCC[i]->getFunction())
82       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         }
89
90   DOUT << ": " << CallSites.size() << " call sites.\n";
91
92   // 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]);
99
100   // 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){
113           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           }
120           --CSi;
121           continue;
122         }
123
124         // 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) {
129           DOUT << "    NOT Inlining: cost=" << InlineCost
130                << ", Call: " << *CS.getInstruction();
131         } else {
132           DOUT << "    Inlining: cost=" << InlineCost
133                << ", Call: " << *CS.getInstruction();
134
135           // Attempt to inline the function...
136           if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
137             // 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             }
147             --CSi;
148
149             ++NumInlined;
150             Changed = true;
151             LocalChange = true;
152           }
153         }
154       }
155   } while (LocalChange);
156
157   return Changed;
158 }
159
160 // doFinalization - Remove now-dead linkonce functions at the end of
161 // processing to avoid breaking the SCC traversal.
162 bool Inliner::doFinalization(CallGraph &CG) {
163   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;
169     if (Function *F = CGN ? CGN->getFunction() : 0) {
170       // If the only remaining users of the function are dead constants, remove
171       // them.
172       F->removeDeadConstantUsers();
173
174       if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
175           F->use_empty()) {
176
177         // Remove any call graph edges from the function to its callees.
178         while (CGN->begin() != CGN->end())
179           CGN->removeCallEdgeTo((CGN->end()-1)->second);
180
181         // 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);
185
186         // Removing the node for callee from the call graph and delete it.
187         FunctionsToRemove.insert(CGN);
188       }
189     }
190   }
191
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
203   return Changed;
204 }