blob: 73e8bd84676b6301e45e9cd9f906610f1359cf28 [file] [log] [blame]
Devang Patel6899b312007-07-25 18:00:25 +00001//===- BasicInliner.cpp - Basic function level inliner --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6899b312007-07-25 18:00:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a simple function based inliner that does not use
11// call graph information.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "basicinliner"
16
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"
25#include <vector>
Devang Patel6899b312007-07-25 18:00:25 +000026
27using namespace llvm;
28
Dan Gohman844731a2008-05-13 00:00:25 +000029static cl::opt<unsigned>
30BasicInlineThreshold("inline-threshold", cl::Hidden, cl::init(200),
Daniel Dunbarf184c662008-10-28 23:24:26 +000031 cl::desc("Control the amount of basic inlining to perform (default = 200)"));
Devang Patel6899b312007-07-25 18:00:25 +000032
33namespace llvm {
34
35 /// BasicInlinerImpl - BasicInliner implemantation class. This hides
36 /// container info, used by basic inliner, from public interface.
37 struct VISIBILITY_HIDDEN BasicInlinerImpl {
38
39 BasicInlinerImpl(const BasicInlinerImpl&); // DO NOT IMPLEMENT
40 void operator=(const BasicInlinerImpl&); // DO NO IMPLEMENT
41 public:
42 BasicInlinerImpl(TargetData *T) : TD(T) {}
43
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);
49 }
50
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);
55 }
56
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();
61
62 private:
63 TargetData *TD;
64 std::vector<Function *> Functions;
Devang Patel29381fb2007-07-27 18:34:27 +000065 SmallPtrSet<const Function *, 16> NeverInline;
Devang Patel6899b312007-07-25 18:00:25 +000066 SmallPtrSet<Function *, 8> DeadFunctions;
67 InlineCostAnalyzer CA;
68 };
69
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.
73void BasicInlinerImpl::inlineFunctions() {
74
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;
79
80 for (std::vector<Function *>::iterator FI = Functions.begin(),
81 FE = Functions.end(); FI != FE; ++FI) {
82 Function *F = *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);
89 }
90 }
91
92 DOUT << ": " << CallSites.size() << " call sites.\n";
93
94 // Inline call sites.
95 bool Changed = false;
96 do {
97 Changed = false;
Daniel Dunbarf184c662008-10-28 23:24:26 +000098 for (unsigned index = 0; index != CallSites.size() && !CallSites.empty();
99 ++index) {
Devang Patel6899b312007-07-25 18:00:25 +0000100 CallSite CS = CallSites[index];
101 if (Function *Callee = CS.getCalledFunction()) {
102
Daniel Dunbarf184c662008-10-28 23:24:26 +0000103 // Eliminate calls that are never inlinable.
Devang Patel6899b312007-07-25 18:00:25 +0000104 if (Callee->isDeclaration() ||
105 CS.getInstruction()->getParent()->getParent() == Callee) {
106 CallSites.erase(CallSites.begin() + index);
Daniel Dunbarf184c662008-10-28 23:24:26 +0000107 --index;
108 continue;
Devang Patel6899b312007-07-25 18:00:25 +0000109 }
Daniel Dunbarc5e1ec42008-10-30 19:26:59 +0000110 InlineCost IC = CA.getInlineCost(CS, NeverInline);
111 if (IC.isAlways()) {
112 DOUT << " Inlining: cost=always"
113 <<", call: " << *CS.getInstruction();
114 } else if (IC.isNever()) {
115 DOUT << " NOT Inlining: cost=never"
116 <<", call: " << *CS.getInstruction();
Daniel Dunbarf184c662008-10-28 23:24:26 +0000117 continue;
Daniel Dunbarc5e1ec42008-10-30 19:26:59 +0000118 } else {
119 int Cost = IC.getValue();
120
121 if (Cost >= BasicInlineThreshold) {
122 DOUT << " NOT Inlining: cost = " << Cost
123 << ", call: " << *CS.getInstruction();
124 continue;
125 } else {
126 DOUT << " Inlining: cost = " << Cost
127 << ", call: " << *CS.getInstruction();
128 }
Devang Patel6899b312007-07-25 18:00:25 +0000129 }
130
Devang Patel6899b312007-07-25 18:00:25 +0000131 // Inline
132 if (InlineFunction(CS, NULL, TD)) {
133 if (Callee->use_empty() && Callee->hasInternalLinkage())
Daniel Dunbarf184c662008-10-28 23:24:26 +0000134 DeadFunctions.insert(Callee);
Devang Patel6899b312007-07-25 18:00:25 +0000135 Changed = true;
136 CallSites.erase(CallSites.begin() + index);
137 --index;
138 }
139 }
140 }
141 } while (Changed);
142
143 // Remove completely inlined functions from module.
144 for(SmallPtrSet<Function *, 8>::iterator I = DeadFunctions.begin(),
145 E = DeadFunctions.end(); I != E; ++I) {
146 Function *D = *I;
147 Module *M = D->getParent();
148 M->getFunctionList().remove(D);
149 }
150}
151
152BasicInliner::BasicInliner(TargetData *TD) {
153 Impl = new BasicInlinerImpl(TD);
154}
155
156BasicInliner::~BasicInliner() {
157 delete Impl;
158}
159
160/// addFunction - Add function into the list of functions to process.
161/// All functions must be inserted using this interface before invoking
162/// inlineFunctions().
163void BasicInliner::addFunction(Function *F) {
164 Impl->addFunction(F);
165}
166
167/// neverInlineFunction - Sometimes a function is never to be inlined because
168/// of one or other reason.
169void BasicInliner::neverInlineFunction(Function *F) {
170 Impl->neverInlineFunction(F);
171}
172
173/// inlineFuctions - Walk all call sites in all functions supplied by
174/// client. Inline as many call sites as possible. Delete completely
175/// inlined functions.
176void BasicInliner::inlineFunctions() {
177 Impl->inlineFunctions();
178}
179
180}