1 //===- BasicInliner.cpp - Basic function level inliner --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple function based inliner that does not use
11 // call graph information.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "basicinliner"
17 #include "llvm/Module.h"
18 #include "llvm/Function.h"
19 #include "llvm/Transforms/Utils/BasicInliner.h"
20 #include "llvm/Transforms/Utils/Cloning.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/SmallPtrSet.h"
32 BasicInlineThreshold("inline-threshold", cl::Hidden, cl::init(200),
33 cl::desc("Control the amount of basic inlining to perform (default = 200)"));
38 /// BasicInlinerImpl - BasicInliner implemantation class. This hides
39 /// container info, used by basic inliner, from public interface.
40 struct VISIBILITY_HIDDEN BasicInlinerImpl {
42 BasicInlinerImpl(const BasicInlinerImpl&); // DO NOT IMPLEMENT
43 void operator=(const BasicInlinerImpl&); // DO NO IMPLEMENT
45 BasicInlinerImpl(TargetData *T) : TD(T) {}
47 /// addFunction - Add function into the list of functions to process.
48 /// All functions must be inserted using this interface before invoking
49 /// inlineFunctions().
50 void addFunction(Function *F) {
51 Functions.push_back(F);
54 /// neverInlineFunction - Sometimes a function is never to be inlined
55 /// because of one or other reason.
56 void neverInlineFunction(Function *F) {
57 NeverInline.insert(F);
60 /// inlineFuctions - Walk all call sites in all functions supplied by
61 /// client. Inline as many call sites as possible. Delete completely
62 /// inlined functions.
63 void inlineFunctions();
67 std::vector<Function *> Functions;
68 std::set<const Function *> NeverInline;
69 SmallPtrSet<Function *, 8> DeadFunctions;
70 InlineCostAnalyzer CA;
73 /// inlineFuctions - Walk all call sites in all functions supplied by
74 /// client. Inline as many call sites as possible. Delete completely
75 /// inlined functions.
76 void BasicInlinerImpl::inlineFunctions() {
78 // Scan through and identify all call sites ahead of time so that we only
79 // inline call sites in the original functions, not call sites that result
80 // from inlining other functions.
81 std::vector<CallSite> CallSites;
83 for (std::vector<Function *>::iterator FI = Functions.begin(),
84 FE = Functions.end(); FI != FE; ++FI) {
86 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
87 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
88 CallSite CS = CallSite::get(I);
89 if (CS.getInstruction() && CS.getCalledFunction()
90 && !CS.getCalledFunction()->isDeclaration())
91 CallSites.push_back(CS);
95 DOUT << ": " << CallSites.size() << " call sites.\n";
101 for (unsigned index = 0; index != CallSites.size() && !CallSites.empty(); ++index) {
102 CallSite CS = CallSites[index];
103 if (Function *Callee = CS.getCalledFunction()) {
105 // Eliminate calls taht are never inlinable.
106 if (Callee->isDeclaration() ||
107 CS.getInstruction()->getParent()->getParent() == Callee) {
108 CallSites.erase(CallSites.begin() + index);
112 int InlineCost = CA.getInlineCost(CS, NeverInline);
113 if (InlineCost >= (int) BasicInlineThreshold) {
114 DOUT << " NOT Inlining: cost = " << InlineCost
115 << ", call: " << *CS.getInstruction();
119 DOUT << " Inlining: cost=" << InlineCost
120 <<", call: " << *CS.getInstruction();
123 if (InlineFunction(CS, NULL, TD)) {
124 if (Callee->use_empty() && Callee->hasInternalLinkage())
125 DeadFunctions.insert(Callee);
127 CallSites.erase(CallSites.begin() + index);
134 // Remove completely inlined functions from module.
135 for(SmallPtrSet<Function *, 8>::iterator I = DeadFunctions.begin(),
136 E = DeadFunctions.end(); I != E; ++I) {
138 Module *M = D->getParent();
139 M->getFunctionList().remove(D);
143 BasicInliner::BasicInliner(TargetData *TD) {
144 Impl = new BasicInlinerImpl(TD);
147 BasicInliner::~BasicInliner() {
151 /// addFunction - Add function into the list of functions to process.
152 /// All functions must be inserted using this interface before invoking
153 /// inlineFunctions().
154 void BasicInliner::addFunction(Function *F) {
155 Impl->addFunction(F);
158 /// neverInlineFunction - Sometimes a function is never to be inlined because
159 /// of one or other reason.
160 void BasicInliner::neverInlineFunction(Function *F) {
161 Impl->neverInlineFunction(F);
164 /// inlineFuctions - Walk all call sites in all functions supplied by
165 /// client. Inline as many call sites as possible. Delete completely
166 /// inlined functions.
167 void BasicInliner::inlineFunctions() {
168 Impl->inlineFunctions();