blob: c0861f1b673c72ecd57611979dc4f64ac4e8074c [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"
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
38int Inliner::getRecursiveInlineCost(CallSite CS) {
39 return getInlineCost(CS);
40}
41
42bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
43 CallGraph &CG = getAnalysis<CallGraph>();
44
45 std::set<Function*> SCCFunctions;
46 DEBUG(std::cerr << "Inliner visiting SCC:");
47 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
48 SCCFunctions.insert(SCC[i]->getFunction());
49 DEBUG(std::cerr << " " << (SCC[i]->getFunction() ?
50 SCC[i]->getFunction()->getName() : "INDIRECTNODE"));
51 }
52 DEBUG(std::cerr << "\n");
53
54 bool Changed = false;
55 for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(),
56 E = SCCFunctions.end(); SCCI != E; ++SCCI) {
57 Function *F = *SCCI;
58 if (F == 0 || F->isExternal()) continue;
59 DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n");
60
61 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
62 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
63 bool ShouldInc = true;
64 // Found a call or invoke instruction?
65 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
66 CallSite CS = CallSite::get(I);
67 if (Function *Callee = CS.getCalledFunction())
Chris Lattnerd77922f2003-11-09 05:05:36 +000068 if (!Callee->isExternal() && !IsRecursiveFunction.count(Callee)) {
Chris Lattner237ef562003-08-31 19:10:30 +000069 // Determine whether this is a function IN the SCC...
70 bool inSCC = SCCFunctions.count(Callee);
71
Chris Lattnerd77922f2003-11-09 05:05:36 +000072 // Keep track of whether this is a directly recursive function.
73 if (Callee == F) IsRecursiveFunction.insert(F);
74
Chris Lattner237ef562003-08-31 19:10:30 +000075 // If the policy determines that we should inline this function,
76 // try to do so...
77 int InlineCost = inSCC ? getRecursiveInlineCost(CS) :
78 getInlineCost(CS);
Chris Lattnera51bcb52003-11-21 21:45:31 +000079 if (InlineCost >= (int)InlineThreshold) {
80 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
81 << ", Call: " << *CS.getInstruction());
82 } else {
Chris Lattner237ef562003-08-31 19:10:30 +000083 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
84 << ", Call: " << *CS.getInstruction());
85
86 // Save an iterator to the instruction before the call if it
87 // exists, otherwise get an iterator at the end of the
88 // block... because the call will be destroyed.
89 //
90 BasicBlock::iterator SI;
91 if (I != BB->begin()) {
92 SI = I; --SI; // Instruction before the call...
93 } else {
94 SI = BB->end();
95 }
96
97 if (performInlining(CS, SCCFunctions)) {
98 // Move to instruction before the call...
99 I = (SI == BB->end()) ? BB->begin() : SI;
100 ShouldInc = false; // Don't increment iterator until next time
101 Changed = true;
102 }
103 }
104 }
105 }
106 if (ShouldInc) ++I;
107 }
108 }
109 return Changed;
110}
111
112bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) {
113 Function *Callee = CS.getCalledFunction();
114 Function *Caller = CS.getInstruction()->getParent()->getParent();
115
116 // Attempt to inline the function...
117 if (!InlineFunction(CS)) return false;
118 ++NumInlined;
Chris Lattnerbb9ae152003-10-31 21:05:58 +0000119
120 if (Callee->hasOneUse())
121 if (ConstantPointerRef *CPR =
122 dyn_cast<ConstantPointerRef>(Callee->use_back()))
123 if (CPR->use_empty())
124 CPR->destroyConstant();
125
Chris Lattner237ef562003-08-31 19:10:30 +0000126 // If we inlined the last possible call site to the function,
127 // delete the function body now.
128 if (Callee->use_empty() && Callee != Caller &&
129 (Callee->hasInternalLinkage() || Callee->hasLinkOnceLinkage())) {
Chris Lattnerbb9ae152003-10-31 21:05:58 +0000130 DEBUG(std::cerr << " -> Deleting dead function: " << (void*)Callee
Chris Lattner237ef562003-08-31 19:10:30 +0000131 << Callee->getName() << "\n");
132 std::set<Function*>::iterator I = SCC.find(Callee);
133 if (I != SCC.end()) // Remove function from this SCC...
134 SCC.erase(I);
135
136 Callee->getParent()->getFunctionList().erase(Callee);
137 ++NumDeleted;
138 }
139 return true;
140}
Brian Gaeked0fde302003-11-11 22:41:34 +0000141