1 //===- InlineCommon.cpp - Code common to all inliners ---------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the code shared between the LLVM inliners. This
11 // implements all of the boring mechanics of the bottom-up inlining.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Constants.h" // ConstantPointerRef should die
17 #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"
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)"));
36 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
38 int Inliner::getRecursiveInlineCost(CallSite CS) {
39 return getInlineCost(CS);
42 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
43 CallGraph &CG = getAnalysis<CallGraph>();
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"));
52 DEBUG(std::cerr << "\n");
55 for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(),
56 E = SCCFunctions.end(); SCCI != E; ++SCCI) {
58 if (F == 0 || F->isExternal()) continue;
59 DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n");
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())
68 if (!Callee->isExternal() && !IsRecursiveFunction.count(Callee)) {
69 // Determine whether this is a function IN the SCC...
70 bool inSCC = SCCFunctions.count(Callee);
72 // Keep track of whether this is a directly recursive function.
73 if (Callee == F) IsRecursiveFunction.insert(F);
75 // If the policy determines that we should inline this function,
77 int InlineCost = inSCC ? getRecursiveInlineCost(CS) :
79 if (InlineCost >= (int)InlineThreshold) {
80 DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
81 << ", Call: " << *CS.getInstruction());
83 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
84 << ", Call: " << *CS.getInstruction());
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.
90 BasicBlock::iterator SI;
91 if (I != BB->begin()) {
92 SI = I; --SI; // Instruction before the call...
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
112 bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) {
113 Function *Callee = CS.getCalledFunction();
114 Function *Caller = CS.getInstruction()->getParent()->getParent();
116 // Attempt to inline the function...
117 if (!InlineFunction(CS)) return false;
120 if (Callee->hasOneUse())
121 if (ConstantPointerRef *CPR =
122 dyn_cast<ConstantPointerRef>(Callee->use_back()))
123 if (CPR->use_empty())
124 CPR->destroyConstant();
126 // 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())) {
130 DEBUG(std::cerr << " -> Deleting dead function: " << (void*)Callee
131 << Callee->getName() << "\n");
132 std::set<Function*>::iterator I = SCC.find(Callee);
133 if (I != SCC.end()) // Remove function from this SCC...
136 Callee->getParent()->getFunctionList().erase(Callee);