2008-09-04 03:52:17 +08:00
|
|
|
//===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
|
2008-09-04 02:50:53 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-09-04 03:52:17 +08:00
|
|
|
// This file implements a custom inliner that handles only functions that
|
2008-09-04 04:25:40 +08:00
|
|
|
// are marked as "always inline".
|
2008-09-04 02:50:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
2014-03-04 19:01:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:36:10 +08: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-04 02:50:53 +08:00
|
|
|
#include "llvm/Transforms/IPO/InlinerPass.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "inline"
|
|
|
|
|
2008-09-04 02:50:53 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-01-21 19:39:16 +08:00
|
|
|
/// \brief Inliner pass which only handles "always inline" functions.
|
|
|
|
class AlwaysInliner : public Inliner {
|
2013-01-21 19:39:18 +08:00
|
|
|
InlineCostAnalysis *ICA;
|
2013-01-21 19:39:16 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Use extremely low threshold.
|
2014-04-25 13:29:35 +08:00
|
|
|
AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true),
|
|
|
|
ICA(nullptr) {
|
2013-01-21 19:39:16 +08:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
AlwaysInliner(bool InsertLifetime)
|
2014-04-25 13:29:35 +08:00
|
|
|
: Inliner(ID, -2000000000, InsertLifetime), ICA(nullptr) {
|
2013-01-21 19:39:16 +08:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
InlineCost getInlineCost(CallSite CS) override;
|
2013-01-21 19:39:16 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnSCC(CallGraphSCC &SCC) override;
|
2013-01-21 19:39:18 +08:00
|
|
|
|
2013-01-21 19:39:16 +08:00
|
|
|
using llvm::Pass::doFinalization;
|
2014-03-05 17:10:37 +08:00
|
|
|
bool doFinalization(CallGraph &CG) override {
|
2013-01-21 19:39:16 +08:00
|
|
|
return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-04 02:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char AlwaysInliner::ID = 0;
|
2010-10-20 01:21:58 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
|
|
|
|
"Inliner for always_inline functions", false, false)
|
2013-11-26 12:19:30 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
|
2013-01-21 19:39:18 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
|
2010-10-20 01:21:58 +08:00
|
|
|
INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Inliner for always_inline functions", false, false)
|
2008-09-04 02:50:53 +08:00
|
|
|
|
|
|
|
Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
|
|
|
|
|
2012-02-25 10:56:01 +08:00
|
|
|
Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
|
|
|
|
return new AlwaysInliner(InsertLifetime);
|
|
|
|
}
|
|
|
|
|
2012-03-31 21:17:18 +08: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 15:04:35 +08: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() &&
|
2014-05-20 02:25:54 +08:00
|
|
|
CS.hasFnAttr(Attribute::AlwaysInline) &&
|
2013-01-21 19:39:18 +08:00
|
|
|
ICA->isInlineViable(*Callee))
|
2012-11-19 15:04:35 +08:00
|
|
|
return InlineCost::getAlways();
|
2012-03-31 21:17:18 +08:00
|
|
|
|
2012-11-19 15:04:35 +08:00
|
|
|
return InlineCost::getNever();
|
2012-03-31 21:17:18 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 19:39:18 +08: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-04 02:50:53 +08:00
|
|
|
}
|