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"
|
2007-07-27 18:34:27 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.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-27 18:34:27 +00:00
|
|
|
// Functions that are never inlined
|
|
|
|
SmallPtrSet<const Function*, 16> NeverInline;
|
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());
|
|
|
|
}
|
|
|
|
SimpleInliner(int Threshold) : Inliner(ID, Threshold) {
|
|
|
|
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) {
|
2007-07-25 18:00:25 +00:00
|
|
|
return CA.getInlineCost(CS, NeverInline);
|
|
|
|
}
|
2008-03-24 06:37:48 +00:00
|
|
|
float getInlineFudgeFactor(CallSite CS) {
|
|
|
|
return CA.getInlineFudgeFactor(CS);
|
|
|
|
}
|
2009-01-09 01:30:11 +00:00
|
|
|
void resetCachedCostInfo(Function *Caller) {
|
|
|
|
CA.resetCachedCostInfo(Caller);
|
|
|
|
}
|
2010-03-09 23:02:17 +00:00
|
|
|
void growCachedCostInfo(Function* Caller, Function* Callee) {
|
|
|
|
CA.growCachedCostInfo(Caller, Callee);
|
|
|
|
}
|
2007-06-06 21:59:26 +00:00
|
|
|
virtual bool doInitialization(CallGraph &CG);
|
2010-05-12 21:48:15 +00:00
|
|
|
void releaseMemory() {
|
|
|
|
CA.clear();
|
|
|
|
}
|
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
|
|
|
|
2008-01-12 06:49:13 +00:00
|
|
|
Pass *llvm::createFunctionInliningPass(int Threshold) {
|
|
|
|
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) {
|
|
|
|
|
|
|
|
Module &M = CG.getModule();
|
|
|
|
|
2008-09-03 18:10:21 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end();
|
|
|
|
I != E; ++I)
|
2008-09-26 23:51:19 +00:00
|
|
|
if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
|
2008-09-03 18:10:21 +00:00
|
|
|
NeverInline.insert(I);
|
|
|
|
|
2007-06-06 21:59:26 +00:00
|
|
|
// Get llvm.noinline
|
|
|
|
GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
|
|
|
|
|
2007-06-07 17:12:16 +00:00
|
|
|
if (GV == 0)
|
2007-06-06 21:59:26 +00:00
|
|
|
return false;
|
|
|
|
|
2007-11-22 22:30:10 +00:00
|
|
|
// Don't crash on invalid code
|
2009-08-19 18:20:44 +00:00
|
|
|
if (!GV->hasDefinitiveInitializer())
|
2007-11-22 22:30:10 +00:00
|
|
|
return false;
|
|
|
|
|
2007-06-06 21:59:26 +00:00
|
|
|
const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
|
|
|
|
|
2007-06-07 17:12:16 +00:00
|
|
|
if (InitList == 0)
|
2007-06-06 21:59:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Iterate over each element and add to the NeverInline set
|
|
|
|
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
|
|
|
|
|
|
|
|
// Get Source
|
|
|
|
const Constant *Elt = InitList->getOperand(i);
|
|
|
|
|
|
|
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
|
|
|
|
if (CE->getOpcode() == Instruction::BitCast)
|
|
|
|
Elt = CE->getOperand(0);
|
|
|
|
|
|
|
|
// Insert into set of functions to never inline
|
2007-06-07 17:12:16 +00:00
|
|
|
if (const Function *F = dyn_cast<Function>(Elt))
|
|
|
|
NeverInline.insert(F);
|
2007-06-06 21:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-25 18:00:25 +00:00
|
|
|
|