2003-08-31 19:10:30 +00:00
|
|
|
//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// 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.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2003-05-29 15:11:31 +00:00
|
|
|
// This file implements bottom-up inlining of functions into callees.
|
2002-04-18 18:52:03 +00:00
|
|
|
//
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-19 22:29:50 +00:00
|
|
|
#define DEBUG_TYPE "inline"
|
2005-05-18 04:30:33 +00:00
|
|
|
#include "llvm/CallingConv.h"
|
2003-11-21 21:46:09 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-11-22 17:21:44 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2007-06-06 21:59:26 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-03-13 23:15:45 +00:00
|
|
|
#include "llvm/Type.h"
|
2007-06-06 21:59:26 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2009-10-13 18:30:07 +00:00
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
2003-08-24 06:59:28 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2003-08-31 19:10:30 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2007-06-19 22:29:50 +00:00
|
|
|
#include "llvm/Transforms/IPO/InlinerPass.h"
|
2011-10-01 01:39:05 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-06 21:59:26 +00:00
|
|
|
|
2003-11-21 21:46:09 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-05-29 15:11:31 +00:00
|
|
|
namespace {
|
2003-10-06 15:52:43 +00:00
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
class SimpleInliner : public Inliner {
|
2007-07-25 18:00:25 +00:00
|
|
|
InlineCostAnalyzer CA;
|
2003-10-06 15:52:43 +00:00
|
|
|
public:
|
2010-10-19 17:21:58 +00:00
|
|
|
SimpleInliner() : Inliner(ID) {
|
|
|
|
initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-02-25 03:07:57 +00:00
|
|
|
SimpleInliner(int Threshold) : Inliner(ID, Threshold,
|
|
|
|
/*InsertLifetime*/true) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-10-30 19:26:59 +00:00
|
|
|
InlineCost getInlineCost(CallSite CS) {
|
Initial commit for the rewrite of the inline cost analysis to operate
on a per-callsite walk of the called function's instructions, in
breadth-first order over the potentially reachable set of basic blocks.
This is a major shift in how inline cost analysis works to improve the
accuracy and rationality of inlining decisions. A brief outline of the
algorithm this moves to:
- Build a simplification mapping based on the callsite arguments to the
function arguments.
- Push the entry block onto a worklist of potentially-live basic blocks.
- Pop the first block off of the *front* of the worklist (for
breadth-first ordering) and walk its instructions using a custom
InstVisitor.
- For each instruction's operands, re-map them based on the
simplification mappings available for the given callsite.
- Compute any simplification possible of the instruction after
re-mapping, and store that back int othe simplification mapping.
- Compute any bonuses, costs, or other impacts of the instruction on the
cost metric.
- When the terminator is reached, replace any conditional value in the
terminator with any simplifications from the mapping we have, and add
any successors which are not proven to be dead from these
simplifications to the worklist.
- Pop the next block off of the front of the worklist, and repeat.
- As soon as the cost of inlining exceeds the threshold for the
callsite, stop analyzing the function in order to bound cost.
The primary goal of this algorithm is to perfectly handle dead code
paths. We do not want any code in trivially dead code paths to impact
inlining decisions. The previous metric was *extremely* flawed here, and
would always subtract the average cost of two successors of
a conditional branch when it was proven to become an unconditional
branch at the callsite. There was no handling of wildly different costs
between the two successors, which would cause inlining when the path
actually taken was too large, and no inlining when the path actually
taken was trivially simple. There was also no handling of the code
*path*, only the immediate successors. These problems vanish completely
now. See the added regression tests for the shiny new features -- we
skip recursive function calls, SROA-killing instructions, and high cost
complex CFG structures when dead at the callsite being analyzed.
Switching to this algorithm required refactoring the inline cost
interface to accept the actual threshold rather than simply returning
a single cost. The resulting interface is pretty bad, and I'm planning
to do lots of interface cleanup after this patch.
Several other refactorings fell out of this, but I've tried to minimize
them for this patch. =/ There is still more cleanup that can be done
here. Please point out anything that you see in review.
I've worked really hard to try to mirror at least the spirit of all of
the previous heuristics in the new model. It's not clear that they are
all correct any more, but I wanted to minimize the change in this single
patch, it's already a bit ridiculous. One heuristic that is *not* yet
mirrored is to allow inlining of functions with a dynamic alloca *if*
the caller has a dynamic alloca. I will add this back, but I think the
most reasonable way requires changes to the inliner itself rather than
just the cost metric, and so I've deferred this for a subsequent patch.
The test case is XFAIL-ed until then.
As mentioned in the review mail, this seems to make Clang run about 1%
to 2% faster in -O0, but makes its binary size grow by just under 4%.
I've looked into the 4% growth, and it can be fixed, but requires
changes to other parts of the inliner.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153812 91177308-0d34-0410-b5e6-96231b3b80d8
2012-03-31 12:42:41 +00:00
|
|
|
return CA.getInlineCost(CS, getInlineThreshold(CS));
|
2008-03-24 06:37:48 +00:00
|
|
|
}
|
2007-06-06 21:59:26 +00:00
|
|
|
virtual bool doInitialization(CallGraph &CG);
|
2003-05-29 15:11:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char SimpleInliner::ID = 0;
|
2010-10-13 22:00:45 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
|
|
|
|
"Function Integration/Inlining", false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(CallGraph)
|
|
|
|
INITIALIZE_PASS_END(SimpleInliner, "inline",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Function Integration/Inlining", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2007-01-26 00:47:38 +00:00
|
|
|
Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
|
2003-11-21 21:46:09 +00:00
|
|
|
|
2011-10-01 01:27:56 +00:00
|
|
|
Pass *llvm::createFunctionInliningPass(int Threshold) {
|
2008-01-12 06:49:13 +00:00
|
|
|
return new SimpleInliner(Threshold);
|
|
|
|
}
|
|
|
|
|
2007-06-06 21:59:26 +00:00
|
|
|
// doInitialization - Initializes the vector of functions that have been
|
|
|
|
// annotated with the noinline attribute.
|
|
|
|
bool SimpleInliner::doInitialization(CallGraph &CG) {
|
2011-10-01 01:39:05 +00:00
|
|
|
CA.setTargetData(getAnalysisIfAvailable<TargetData>());
|
2007-06-06 21:59:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-07-25 18:00:25 +00:00
|
|
|
|