2007-07-25 18:00:25 +00:00
|
|
|
//===- BasicInliner.cpp - Basic function level inliner --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-25 18:00:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a simple function based inliner that does not use
|
|
|
|
// call graph information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "basicinliner"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicInliner.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-23 04:37:46 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-07-25 18:00:25 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static cl::opt<unsigned>
|
2009-02-23 07:07:56 +00:00
|
|
|
BasicInlineThreshold("basic-inline-threshold", cl::Hidden, cl::init(200),
|
2008-10-28 23:24:26 +00:00
|
|
|
cl::desc("Control the amount of basic inlining to perform (default = 200)"));
|
2007-07-25 18:00:25 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// BasicInlinerImpl - BasicInliner implemantation class. This hides
|
|
|
|
/// container info, used by basic inliner, from public interface.
|
2009-10-25 06:33:48 +00:00
|
|
|
struct BasicInlinerImpl {
|
2007-07-25 18:00:25 +00:00
|
|
|
|
|
|
|
BasicInlinerImpl(const BasicInlinerImpl&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const BasicInlinerImpl&); // DO NO IMPLEMENT
|
|
|
|
public:
|
|
|
|
BasicInlinerImpl(TargetData *T) : TD(T) {}
|
|
|
|
|
|
|
|
/// addFunction - Add function into the list of functions to process.
|
|
|
|
/// All functions must be inserted using this interface before invoking
|
|
|
|
/// inlineFunctions().
|
|
|
|
void addFunction(Function *F) {
|
|
|
|
Functions.push_back(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// neverInlineFunction - Sometimes a function is never to be inlined
|
|
|
|
/// because of one or other reason.
|
|
|
|
void neverInlineFunction(Function *F) {
|
|
|
|
NeverInline.insert(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// inlineFuctions - Walk all call sites in all functions supplied by
|
|
|
|
/// client. Inline as many call sites as possible. Delete completely
|
|
|
|
/// inlined functions.
|
|
|
|
void inlineFunctions();
|
|
|
|
|
|
|
|
private:
|
|
|
|
TargetData *TD;
|
|
|
|
std::vector<Function *> Functions;
|
2007-07-27 18:34:27 +00:00
|
|
|
SmallPtrSet<const Function *, 16> NeverInline;
|
2007-07-25 18:00:25 +00:00
|
|
|
SmallPtrSet<Function *, 8> DeadFunctions;
|
|
|
|
InlineCostAnalyzer CA;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// inlineFuctions - Walk all call sites in all functions supplied by
|
|
|
|
/// client. Inline as many call sites as possible. Delete completely
|
|
|
|
/// inlined functions.
|
|
|
|
void BasicInlinerImpl::inlineFunctions() {
|
|
|
|
|
|
|
|
// Scan through and identify all call sites ahead of time so that we only
|
|
|
|
// inline call sites in the original functions, not call sites that result
|
|
|
|
// from inlining other functions.
|
|
|
|
std::vector<CallSite> CallSites;
|
|
|
|
|
|
|
|
for (std::vector<Function *>::iterator FI = Functions.begin(),
|
|
|
|
FE = Functions.end(); FI != FE; ++FI) {
|
|
|
|
Function *F = *FI;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
|
|
|
|
CallSite CS = CallSite::get(I);
|
|
|
|
if (CS.getInstruction() && CS.getCalledFunction()
|
|
|
|
&& !CS.getCalledFunction()->isDeclaration())
|
|
|
|
CallSites.push_back(CS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 01:26:39 +00:00
|
|
|
DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
|
2007-07-25 18:00:25 +00:00
|
|
|
|
|
|
|
// Inline call sites.
|
|
|
|
bool Changed = false;
|
|
|
|
do {
|
|
|
|
Changed = false;
|
2008-10-28 23:24:26 +00:00
|
|
|
for (unsigned index = 0; index != CallSites.size() && !CallSites.empty();
|
|
|
|
++index) {
|
2007-07-25 18:00:25 +00:00
|
|
|
CallSite CS = CallSites[index];
|
|
|
|
if (Function *Callee = CS.getCalledFunction()) {
|
|
|
|
|
2008-10-28 23:24:26 +00:00
|
|
|
// Eliminate calls that are never inlinable.
|
2007-07-25 18:00:25 +00:00
|
|
|
if (Callee->isDeclaration() ||
|
|
|
|
CS.getInstruction()->getParent()->getParent() == Callee) {
|
|
|
|
CallSites.erase(CallSites.begin() + index);
|
2008-10-28 23:24:26 +00:00
|
|
|
--index;
|
|
|
|
continue;
|
2007-07-25 18:00:25 +00:00
|
|
|
}
|
2008-10-30 19:26:59 +00:00
|
|
|
InlineCost IC = CA.getInlineCost(CS, NeverInline);
|
|
|
|
if (IC.isAlways()) {
|
2010-01-05 01:26:39 +00:00
|
|
|
DEBUG(dbgs() << " Inlining: cost=always"
|
2009-08-23 04:37:46 +00:00
|
|
|
<<", call: " << *CS.getInstruction());
|
2008-10-30 19:26:59 +00:00
|
|
|
} else if (IC.isNever()) {
|
2010-01-05 01:26:39 +00:00
|
|
|
DEBUG(dbgs() << " NOT Inlining: cost=never"
|
2009-08-23 04:37:46 +00:00
|
|
|
<<", call: " << *CS.getInstruction());
|
2008-10-28 23:24:26 +00:00
|
|
|
continue;
|
2008-10-30 19:26:59 +00:00
|
|
|
} else {
|
|
|
|
int Cost = IC.getValue();
|
|
|
|
|
2008-10-31 01:50:01 +00:00
|
|
|
if (Cost >= (int) BasicInlineThreshold) {
|
2010-01-05 01:26:39 +00:00
|
|
|
DEBUG(dbgs() << " NOT Inlining: cost = " << Cost
|
2009-08-23 04:37:46 +00:00
|
|
|
<< ", call: " << *CS.getInstruction());
|
2008-10-30 19:26:59 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2010-01-05 01:26:39 +00:00
|
|
|
DEBUG(dbgs() << " Inlining: cost = " << Cost
|
2009-08-23 04:37:46 +00:00
|
|
|
<< ", call: " << *CS.getInstruction());
|
2008-10-30 19:26:59 +00:00
|
|
|
}
|
2007-07-25 18:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inline
|
|
|
|
if (InlineFunction(CS, NULL, TD)) {
|
2009-05-23 14:06:57 +00:00
|
|
|
if (Callee->use_empty() && (Callee->hasLocalLinkage() ||
|
|
|
|
Callee->hasAvailableExternallyLinkage()))
|
2008-10-28 23:24:26 +00:00
|
|
|
DeadFunctions.insert(Callee);
|
2007-07-25 18:00:25 +00:00
|
|
|
Changed = true;
|
|
|
|
CallSites.erase(CallSites.begin() + index);
|
|
|
|
--index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (Changed);
|
|
|
|
|
|
|
|
// Remove completely inlined functions from module.
|
|
|
|
for(SmallPtrSet<Function *, 8>::iterator I = DeadFunctions.begin(),
|
|
|
|
E = DeadFunctions.end(); I != E; ++I) {
|
|
|
|
Function *D = *I;
|
|
|
|
Module *M = D->getParent();
|
|
|
|
M->getFunctionList().remove(D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicInliner::BasicInliner(TargetData *TD) {
|
|
|
|
Impl = new BasicInlinerImpl(TD);
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicInliner::~BasicInliner() {
|
|
|
|
delete Impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addFunction - Add function into the list of functions to process.
|
|
|
|
/// All functions must be inserted using this interface before invoking
|
|
|
|
/// inlineFunctions().
|
|
|
|
void BasicInliner::addFunction(Function *F) {
|
|
|
|
Impl->addFunction(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// neverInlineFunction - Sometimes a function is never to be inlined because
|
|
|
|
/// of one or other reason.
|
|
|
|
void BasicInliner::neverInlineFunction(Function *F) {
|
|
|
|
Impl->neverInlineFunction(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// inlineFuctions - Walk all call sites in all functions supplied by
|
|
|
|
/// client. Inline as many call sites as possible. Delete completely
|
|
|
|
/// inlined functions.
|
|
|
|
void BasicInliner::inlineFunctions() {
|
|
|
|
Impl->inlineFunctions();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|