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"
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)"));
35 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
37 int Inliner::getRecursiveInlineCost(CallSite CS) {
38 return getInlineCost(CS);
41 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
42 CallGraph &CG = getAnalysis<CallGraph>();
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"));
51 DEBUG(std::cerr << "\n");
54 for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(),
55 E = SCCFunctions.end(); SCCI != E; ++SCCI) {
57 if (F == 0 || F->isExternal()) continue;
58 DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n");
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);
71 // If the policy determines that we should inline this function,
73 int InlineCost = inSCC ? getRecursiveInlineCost(CS) :
75 if (InlineCost < (int)InlineThreshold) {
76 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
77 << ", Call: " << *CS.getInstruction());
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.
83 BasicBlock::iterator SI;
84 if (I != BB->begin()) {
85 SI = I; --SI; // Instruction before the call...
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
105 bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) {
106 Function *Callee = CS.getCalledFunction();
107 Function *Caller = CS.getInstruction()->getParent()->getParent();
109 // Attempt to inline the function...
110 if (!InlineFunction(CS)) return false;
113 if (Callee->hasOneUse())
114 if (ConstantPointerRef *CPR =
115 dyn_cast<ConstantPointerRef>(Callee->use_back()))
116 if (CPR->use_empty())
117 CPR->destroyConstant();
119 // 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())) {
123 DEBUG(std::cerr << " -> Deleting dead function: " << (void*)Callee
124 << Callee->getName() << "\n");
125 std::set<Function*>::iterator I = SCC.find(Callee);
126 if (I != SCC.end()) // Remove function from this SCC...
129 Callee->getParent()->getFunctionList().erase(Callee);