1 //===- BasicInliner.cpp - Basic function level inliner --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
16 #include "llvm/Module.h"
17 #include "llvm/Function.h"
18 #include "llvm/Transforms/Utils/BasicInliner.h"
19 #include "llvm/Transforms/Utils/Cloning.h"
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/ADT/SmallPtrSet.h"
29 static cl::opt
<unsigned>
30 BasicInlineThreshold("basic-inline-threshold", cl::Hidden
, cl::init(200),
31 cl::desc("Control the amount of basic inlining to perform (default = 200)"));
35 /// BasicInlinerImpl - BasicInliner implemantation class. This hides
36 /// container info, used by basic inliner, from public interface.
37 struct BasicInlinerImpl
{
39 BasicInlinerImpl(const BasicInlinerImpl
&); // DO NOT IMPLEMENT
40 void operator=(const BasicInlinerImpl
&); // DO NO IMPLEMENT
42 BasicInlinerImpl(TargetData
*T
) : TD(T
) {}
44 /// addFunction - Add function into the list of functions to process.
45 /// All functions must be inserted using this interface before invoking
46 /// inlineFunctions().
47 void addFunction(Function
*F
) {
48 Functions
.push_back(F
);
51 /// neverInlineFunction - Sometimes a function is never to be inlined
52 /// because of one or other reason.
53 void neverInlineFunction(Function
*F
) {
54 NeverInline
.insert(F
);
57 /// inlineFuctions - Walk all call sites in all functions supplied by
58 /// client. Inline as many call sites as possible. Delete completely
59 /// inlined functions.
60 void inlineFunctions();
64 std::vector
<Function
*> Functions
;
65 SmallPtrSet
<const Function
*, 16> NeverInline
;
66 SmallPtrSet
<Function
*, 8> DeadFunctions
;
67 InlineCostAnalyzer CA
;
70 /// inlineFuctions - Walk all call sites in all functions supplied by
71 /// client. Inline as many call sites as possible. Delete completely
72 /// inlined functions.
73 void BasicInlinerImpl::inlineFunctions() {
75 // Scan through and identify all call sites ahead of time so that we only
76 // inline call sites in the original functions, not call sites that result
77 // from inlining other functions.
78 std::vector
<CallSite
> CallSites
;
80 for (std::vector
<Function
*>::iterator FI
= Functions
.begin(),
81 FE
= Functions
.end(); FI
!= FE
; ++FI
) {
83 for (Function::iterator BB
= F
->begin(), E
= F
->end(); BB
!= E
; ++BB
)
84 for (BasicBlock::iterator I
= BB
->begin(); I
!= BB
->end(); ++I
) {
85 CallSite CS
= CallSite::get(I
);
86 if (CS
.getInstruction() && CS
.getCalledFunction()
87 && !CS
.getCalledFunction()->isDeclaration())
88 CallSites
.push_back(CS
);
92 DEBUG(dbgs() << ": " << CallSites
.size() << " call sites.\n");
98 for (unsigned index
= 0; index
!= CallSites
.size() && !CallSites
.empty();
100 CallSite CS
= CallSites
[index
];
101 if (Function
*Callee
= CS
.getCalledFunction()) {
103 // Eliminate calls that are never inlinable.
104 if (Callee
->isDeclaration() ||
105 CS
.getInstruction()->getParent()->getParent() == Callee
) {
106 CallSites
.erase(CallSites
.begin() + index
);
110 InlineCost IC
= CA
.getInlineCost(CS
, NeverInline
);
112 DEBUG(dbgs() << " Inlining: cost=always"
113 <<", call: " << *CS
.getInstruction());
114 } else if (IC
.isNever()) {
115 DEBUG(dbgs() << " NOT Inlining: cost=never"
116 <<", call: " << *CS
.getInstruction());
119 int Cost
= IC
.getValue();
121 if (Cost
>= (int) BasicInlineThreshold
) {
122 DEBUG(dbgs() << " NOT Inlining: cost = " << Cost
123 << ", call: " << *CS
.getInstruction());
126 DEBUG(dbgs() << " Inlining: cost = " << Cost
127 << ", call: " << *CS
.getInstruction());
132 if (InlineFunction(CS
, NULL
, TD
)) {
133 if (Callee
->use_empty() && (Callee
->hasLocalLinkage() ||
134 Callee
->hasAvailableExternallyLinkage()))
135 DeadFunctions
.insert(Callee
);
137 CallSites
.erase(CallSites
.begin() + index
);
144 // Remove completely inlined functions from module.
145 for(SmallPtrSet
<Function
*, 8>::iterator I
= DeadFunctions
.begin(),
146 E
= DeadFunctions
.end(); I
!= E
; ++I
) {
148 Module
*M
= D
->getParent();
149 M
->getFunctionList().remove(D
);
153 BasicInliner::BasicInliner(TargetData
*TD
) {
154 Impl
= new BasicInlinerImpl(TD
);
157 BasicInliner::~BasicInliner() {
161 /// addFunction - Add function into the list of functions to process.
162 /// All functions must be inserted using this interface before invoking
163 /// inlineFunctions().
164 void BasicInliner::addFunction(Function
*F
) {
165 Impl
->addFunction(F
);
168 /// neverInlineFunction - Sometimes a function is never to be inlined because
169 /// of one or other reason.
170 void BasicInliner::neverInlineFunction(Function
*F
) {
171 Impl
->neverInlineFunction(F
);
174 /// inlineFuctions - Walk all call sites in all functions supplied by
175 /// client. Inline as many call sites as possible. Delete completely
176 /// inlined functions.
177 void BasicInliner::inlineFunctions() {
178 Impl
->inlineFunctions();