2008-09-03 19:52:17 +00:00
|
|
|
//===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
|
2008-09-03 18:50:53 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-09-03 19:52:17 +00:00
|
|
|
// This file implements a custom inliner that handles only functions that
|
2008-09-03 20:25:40 +00:00
|
|
|
// are marked as "always inline".
|
2008-09-03 18:50:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2008-09-03 18:50:53 +00:00
|
|
|
#include "llvm/Transforms/IPO/InlinerPass.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "inline"
|
|
|
|
|
2008-09-03 18:50:53 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-01-21 11:39:16 +00:00
|
|
|
/// \brief Inliner pass which only handles "always inline" functions.
|
|
|
|
class AlwaysInliner : public Inliner {
|
2013-01-21 11:39:18 +00:00
|
|
|
InlineCostAnalysis *ICA;
|
2013-01-21 11:39:16 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Use extremely low threshold.
|
2014-04-25 05:29:35 +00:00
|
|
|
AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true),
|
|
|
|
ICA(nullptr) {
|
2013-01-21 11:39:16 +00:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
AlwaysInliner(bool InsertLifetime)
|
2014-04-25 05:29:35 +00:00
|
|
|
: Inliner(ID, -2000000000, InsertLifetime), ICA(nullptr) {
|
2013-01-21 11:39:16 +00:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
InlineCost getInlineCost(CallSite CS) override;
|
2013-01-21 11:39:16 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnSCC(CallGraphSCC &SCC) override;
|
2013-01-21 11:39:18 +00:00
|
|
|
|
2013-01-21 11:39:16 +00:00
|
|
|
using llvm::Pass::doFinalization;
|
2014-03-05 09:10:37 +00:00
|
|
|
bool doFinalization(CallGraph &CG) override {
|
2013-01-21 11:39:16 +00:00
|
|
|
return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-03 18:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char AlwaysInliner::ID = 0;
|
2010-10-19 17:21:58 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
|
|
|
|
"Inliner for always_inline functions", false, false)
|
2013-11-26 04:19:30 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
|
2013-01-21 11:39:18 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
|
2010-10-19 17:21:58 +00:00
|
|
|
INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Inliner for always_inline functions", false, false)
|
2008-09-03 18:50:53 +00:00
|
|
|
|
|
|
|
Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
|
|
|
|
|
2012-02-25 02:56:01 +00:00
|
|
|
Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
|
|
|
|
return new AlwaysInliner(InsertLifetime);
|
|
|
|
}
|
|
|
|
|
2012-03-31 13:17:18 +00:00
|
|
|
/// \brief Get the inline cost for the always-inliner.
|
|
|
|
///
|
|
|
|
/// The always inliner *only* handles functions which are marked with the
|
|
|
|
/// attribute to force inlining. As such, it is dramatically simpler and avoids
|
|
|
|
/// using the powerful (but expensive) inline cost analysis. Instead it uses
|
|
|
|
/// a very simple and boring direct walk of the instructions looking for
|
|
|
|
/// impossible-to-inline constructs.
|
|
|
|
///
|
|
|
|
/// Note, it would be possible to go to some lengths to cache the information
|
|
|
|
/// computed here, but as we only expect to do this for relatively few and
|
|
|
|
/// small functions which have the explicit attribute to force inlining, it is
|
|
|
|
/// likely not worth it in practice.
|
|
|
|
InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
|
|
|
|
Function *Callee = CS.getCalledFunction();
|
|
|
|
|
2012-11-19 07:04:35 +00:00
|
|
|
// Only inline direct calls to functions with always-inline attributes
|
|
|
|
// that are viable for inlining. FIXME: We shouldn't even get here for
|
|
|
|
// declarations.
|
|
|
|
if (Callee && !Callee->isDeclaration() &&
|
2012-12-30 10:32:01 +00:00
|
|
|
Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
|
|
|
|
Attribute::AlwaysInline) &&
|
2013-01-21 11:39:18 +00:00
|
|
|
ICA->isInlineViable(*Callee))
|
2012-11-19 07:04:35 +00:00
|
|
|
return InlineCost::getAlways();
|
2012-03-31 13:17:18 +00:00
|
|
|
|
2012-11-19 07:04:35 +00:00
|
|
|
return InlineCost::getNever();
|
2012-03-31 13:17:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 11:39:18 +00:00
|
|
|
bool AlwaysInliner::runOnSCC(CallGraphSCC &SCC) {
|
|
|
|
ICA = &getAnalysis<InlineCostAnalysis>();
|
|
|
|
return Inliner::runOnSCC(SCC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlwaysInliner::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<InlineCostAnalysis>();
|
|
|
|
Inliner::getAnalysisUsage(AU);
|
2008-09-03 18:50:53 +00:00
|
|
|
}
|