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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "inline"
|
|
|
|
#include "llvm/CallingConv.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/IntrinsicInst.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2009-10-14 02:30:07 +08:00
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
2008-09-04 02:50:53 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/InlinerPass.h"
|
2012-10-09 00:38:25 +08:00
|
|
|
#include "llvm/DataLayout.h"
|
2008-09-04 02:50:53 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// AlwaysInliner only inlines functions that are mark as "always inline".
|
2009-10-25 14:33:48 +08:00
|
|
|
class AlwaysInliner : public Inliner {
|
2012-11-19 15:04:35 +08:00
|
|
|
InlineCostAnalyzer CA;
|
2008-09-04 02:50:53 +08:00
|
|
|
public:
|
2011-10-01 09:27:56 +08:00
|
|
|
// Use extremely low threshold.
|
2012-02-25 11:07:57 +08:00
|
|
|
AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/true) {
|
2012-02-25 10:56:01 +08:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
AlwaysInliner(bool InsertLifetime) : Inliner(ID, -2000000000,
|
|
|
|
InsertLifetime) {
|
2010-10-20 01:21:58 +08:00
|
|
|
initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-09-04 02:50:53 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2012-03-31 21:17:18 +08:00
|
|
|
virtual InlineCost getInlineCost(CallSite CS);
|
2011-10-01 09:27:56 +08:00
|
|
|
virtual bool doFinalization(CallGraph &CG) {
|
Start removing the use of an ad-hoc 'never inline' set and instead
directly query the function information which this set was representing.
This simplifies the interface of the inline cost analysis, and makes the
always-inline pass significantly more efficient.
Previously, always-inline would first make a single set of every
function in the module *except* those marked with the always-inline
attribute. It would then query this set at every call site to see if the
function was a member of the set, and if so, refuse to inline it. This
is quite wasteful. Instead, simply check the function attribute directly
when looking at the callsite.
The normal inliner also had similar redundancy. It added every function
in the module with the noinline attribute to its set to ignore, even
though inside the cost analysis function we *already tested* the
noinline attribute and produced the same result.
The only tricky part of removing this is that we have to be able to
correctly remove only the functions inlined by the always-inline pass
when finalizing, which requires a bit of a hack. Still, much less of
a hack than the set of all non-always-inline functions was. While I was
touching this function, I switched a heavy-weight set to a vector with
sort+unique. The algorithm already had a two-phase insert and removal
pattern, we were just needlessly paying the uniquing cost on every
insert.
This probably speeds up some compiles by a small amount (-O0 compiles
with lots of always-inline, so potentially heavy libc++ users), but I've
not tried to measure it.
I believe there is no functional change here, but yell if you spot one.
None are intended.
Finally, the direction this is going in is to greatly simplify the
inline cost query interface so that we can replace its implementation
with a much more clever one. Along the way, all the APIs get simplified,
so it seems incrementally good.
llvm-svn: 152903
2012-03-16 14:10:13 +08:00
|
|
|
return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
|
2008-11-05 09:39:16 +08:00
|
|
|
}
|
2008-09-04 02:50:53 +08:00
|
|
|
virtual bool doInitialization(CallGraph &CG);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(CallGraph)
|
|
|
|
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() &&
|
|
|
|
Callee->getFnAttributes().hasAttribute(Attributes::AlwaysInline) &&
|
|
|
|
CA.isInlineViable(*Callee))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-01 09:27:56 +08:00
|
|
|
// doInitialization - Initializes the vector of functions that have not
|
2008-09-04 02:50:53 +08:00
|
|
|
// been annotated with the "always inline" attribute.
|
|
|
|
bool AlwaysInliner::doInitialization(CallGraph &CG) {
|
2012-11-19 15:04:35 +08:00
|
|
|
CA.setDataLayout(getAnalysisIfAvailable<DataLayout>());
|
2008-09-04 02:50:53 +08:00
|
|
|
return false;
|
|
|
|
}
|