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