blob: 503879e43038a1aaa8e354bd2c65bf219fbc22da [file] [log] [blame]
Chris Lattner237ef562003-08-31 19:10:30 +00001//===- InlineCommon.cpp - Code common to all inliners ---------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner237ef562003-08-31 19:10:30 +00009//
10// This file implements the code shared between the LLVM inliners. This
11// implements all of the boring mechanics of the bottom-up inlining.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Inliner.h"
16#include "llvm/Module.h"
17#include "llvm/iOther.h"
18#include "llvm/iTerminators.h"
19#include "llvm/Analysis/CallGraph.h"
20#include "llvm/Support/CallSite.h"
21#include "llvm/Transforms/Utils/Cloning.h"
22#include "Support/CommandLine.h"
23#include "Support/Debug.h"
24#include "Support/Statistic.h"
25
26namespace {
27 Statistic<> NumInlined("inline", "Number of functions inlined");
28 Statistic<> NumDeleted("inline", "Number of functions deleted because all callers found");
29 cl::opt<unsigned> // FIXME: 200 is VERY conservative
30 InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
31 cl::desc("Control the amount of inlining to perform (default = 200)"));
32}
33
34Inliner::Inliner() : InlineThreshold(InlineLimit) {}
35
36int Inliner::getRecursiveInlineCost(CallSite CS) {
37 return getInlineCost(CS);
38}
39
40bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
41 CallGraph &CG = getAnalysis<CallGraph>();
42
43 std::set<Function*> SCCFunctions;
44 DEBUG(std::cerr << "Inliner visiting SCC:");
45 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
46 SCCFunctions.insert(SCC[i]->getFunction());
47 DEBUG(std::cerr << " " << (SCC[i]->getFunction() ?
48 SCC[i]->getFunction()->getName() : "INDIRECTNODE"));
49 }
50 DEBUG(std::cerr << "\n");
51
52 bool Changed = false;
53 for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(),
54 E = SCCFunctions.end(); SCCI != E; ++SCCI) {
55 Function *F = *SCCI;
56 if (F == 0 || F->isExternal()) continue;
57 DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n");
58
59 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
60 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
61 bool ShouldInc = true;
62 // Found a call or invoke instruction?
63 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
64 CallSite CS = CallSite::get(I);
65 if (Function *Callee = CS.getCalledFunction())
66 if (!Callee->isExternal()) {
67 // Determine whether this is a function IN the SCC...
68 bool inSCC = SCCFunctions.count(Callee);
69
70 // If the policy determines that we should inline this function,
71 // try to do so...
72 int InlineCost = inSCC ? getRecursiveInlineCost(CS) :
73 getInlineCost(CS);
74 if (InlineCost < (int)InlineThreshold) {
75 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
76 << ", Call: " << *CS.getInstruction());
77
78 // Save an iterator to the instruction before the call if it
79 // exists, otherwise get an iterator at the end of the
80 // block... because the call will be destroyed.
81 //
82 BasicBlock::iterator SI;
83 if (I != BB->begin()) {
84 SI = I; --SI; // Instruction before the call...
85 } else {
86 SI = BB->end();
87 }
88
89 if (performInlining(CS, SCCFunctions)) {
90 // Move to instruction before the call...
91 I = (SI == BB->end()) ? BB->begin() : SI;
92 ShouldInc = false; // Don't increment iterator until next time
93 Changed = true;
94 }
95 }
96 }
97 }
98 if (ShouldInc) ++I;
99 }
100 }
101 return Changed;
102}
103
104bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) {
105 Function *Callee = CS.getCalledFunction();
106 Function *Caller = CS.getInstruction()->getParent()->getParent();
107
108 // Attempt to inline the function...
109 if (!InlineFunction(CS)) return false;
110 ++NumInlined;
111
112 // If we inlined the last possible call site to the function,
113 // delete the function body now.
114 if (Callee->use_empty() && Callee != Caller &&
115 (Callee->hasInternalLinkage() || Callee->hasLinkOnceLinkage())) {
116 DEBUG(std::cerr << " -> Deleting dead function: "
117 << Callee->getName() << "\n");
118 std::set<Function*>::iterator I = SCC.find(Callee);
119 if (I != SCC.end()) // Remove function from this SCC...
120 SCC.erase(I);
121
122 Callee->getParent()->getFunctionList().erase(Callee);
123 ++NumDeleted;
124 }
125 return true;
126}