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() && !IsRecursiveFunction.count(Callee)) {
68 // Determine whether this is a function IN the SCC...
69 bool inSCC = SCCFunctions.count(Callee);
71 // Keep track of whether this is a directly recursive function.
72 if (Callee == F) IsRecursiveFunction.insert(F);
74 // If the policy determines that we should inline this function,
76 int InlineCost = inSCC ? getRecursiveInlineCost(CS) :
78 if (InlineCost < (int)InlineThreshold) {
79 DEBUG(std::cerr << " Inlining: cost=" << InlineCost
80 << ", Call: " << *CS.getInstruction());
82 // Save an iterator to the instruction before the call if it
83 // exists, otherwise get an iterator at the end of the
84 // block... because the call will be destroyed.
86 BasicBlock::iterator SI;
87 if (I != BB->begin()) {
88 SI = I; --SI; // Instruction before the call...
93 if (performInlining(CS, SCCFunctions)) {
94 // Move to instruction before the call...
95 I = (SI == BB->end()) ? BB->begin() : SI;
96 ShouldInc = false; // Don't increment iterator until next time
108 bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) {
109 Function *Callee = CS.getCalledFunction();
110 Function *Caller = CS.getInstruction()->getParent()->getParent();
112 // Attempt to inline the function...
113 if (!InlineFunction(CS)) return false;
116 if (Callee->hasOneUse())
117 if (ConstantPointerRef *CPR =
118 dyn_cast<ConstantPointerRef>(Callee->use_back()))
119 if (CPR->use_empty())
120 CPR->destroyConstant();
122 // If we inlined the last possible call site to the function,
123 // delete the function body now.
124 if (Callee->use_empty() && Callee != Caller &&
125 (Callee->hasInternalLinkage() || Callee->hasLinkOnceLinkage())) {
126 DEBUG(std::cerr << " -> Deleting dead function: " << (void*)Callee
127 << Callee->getName() << "\n");
128 std::set<Function*>::iterator I = SCC.find(Callee);
129 if (I != SCC.end()) // Remove function from this SCC...
132 Callee->getParent()->getFunctionList().erase(Callee);