2008-09-03 21:00:28 +00:00
|
|
|
//===-- PartialSpecialization.cpp - Specialize for common constants--------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass finds function arguments that are often a common constant and
|
|
|
|
// specializes a version of the called function for that constant.
|
|
|
|
//
|
2008-09-04 14:34:22 +00:00
|
|
|
// This pass simply does the cloning for functions it specializes. It depends
|
|
|
|
// on IPSCCP and DAE to clean up the results.
|
|
|
|
//
|
|
|
|
// The initial heuristic favors constant arguments that are used in control
|
|
|
|
// flow.
|
2008-09-03 21:00:28 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "partialspecialization"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Constant.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-10-09 22:06:36 +00:00
|
|
|
#include "llvm/Analysis/InlineCost.h"
|
2008-09-03 21:00:28 +00:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2008-09-04 18:51:26 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2008-09-03 21:00:28 +00:00
|
|
|
#include <map>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(numSpecialized, "Number of specialized functions created");
|
2010-06-05 14:50:21 +00:00
|
|
|
STATISTIC(numReplaced, "Number of callers replaced by specialization");
|
|
|
|
|
|
|
|
// Maximum number of arguments markable interested
|
|
|
|
static const int MaxInterests = 6;
|
2008-09-03 21:00:28 +00:00
|
|
|
|
|
|
|
namespace {
|
2010-06-05 14:50:21 +00:00
|
|
|
typedef SmallVector<int, MaxInterests> InterestingArgVector;
|
2009-10-25 06:33:48 +00:00
|
|
|
class PartSpec : public ModulePass {
|
2010-06-05 14:50:21 +00:00
|
|
|
void scanForInterest(Function&, InterestingArgVector&);
|
2008-09-03 21:00:28 +00:00
|
|
|
int scanDistribution(Function&, int, std::map<Constant*, int>&);
|
2010-10-09 22:06:36 +00:00
|
|
|
InlineCostAnalyzer CA;
|
2008-09-03 21:00:28 +00:00
|
|
|
public :
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
PartSpec() : ModulePass(ID) {
|
|
|
|
initializePartSpecPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-09-03 21:00:28 +00:00
|
|
|
bool runOnModule(Module &M);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char PartSpec::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(PartSpec, "partialspecialization",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Partial Specialization", false, false)
|
2008-09-03 21:00:28 +00:00
|
|
|
|
2008-09-04 18:51:26 +00:00
|
|
|
// Specialize F by replacing the arguments (keys) in replacements with the
|
|
|
|
// constants (values). Replace all calls to F with those constants with
|
|
|
|
// a call to the specialized function. Returns the specialized function
|
|
|
|
static Function*
|
|
|
|
SpecializeFunction(Function* F,
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy& replacements) {
|
2008-09-04 18:51:26 +00:00
|
|
|
// arg numbers of deleted arguments
|
2010-06-05 14:50:21 +00:00
|
|
|
DenseMap<unsigned, const Argument*> deleted;
|
2010-10-13 01:36:30 +00:00
|
|
|
for (ValueToValueMapTy::iterator
|
2008-09-04 18:51:26 +00:00
|
|
|
repb = replacements.begin(), repe = replacements.end();
|
2010-06-05 14:50:21 +00:00
|
|
|
repb != repe; ++repb) {
|
|
|
|
Argument const *arg = cast<const Argument>(repb->first);
|
|
|
|
deleted[arg->getArgNo()] = arg;
|
|
|
|
}
|
2008-09-04 18:51:26 +00:00
|
|
|
|
2010-08-26 15:41:53 +00:00
|
|
|
Function* NF = CloneFunction(F, replacements,
|
|
|
|
/*ModuleLevelChanges=*/false);
|
2008-09-04 18:51:26 +00:00
|
|
|
NF->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
F->getParent()->getFunctionList().push_back(NF);
|
|
|
|
|
2010-10-09 22:06:36 +00:00
|
|
|
// FIXME: Specialized versions getting the same constants should also get
|
|
|
|
// the same name. That way, specializations for public functions can be
|
|
|
|
// marked linkonce_odr and reused across modules.
|
|
|
|
|
2008-09-04 18:51:26 +00:00
|
|
|
for (Value::use_iterator ii = F->use_begin(), ee = F->use_end();
|
|
|
|
ii != ee; ) {
|
2009-03-08 19:02:17 +00:00
|
|
|
Value::use_iterator i = ii;
|
2008-09-04 18:51:26 +00:00
|
|
|
++ii;
|
2010-07-22 13:04:32 +00:00
|
|
|
User *U = *i;
|
2010-07-22 13:07:39 +00:00
|
|
|
CallSite CS(U);
|
|
|
|
if (CS) {
|
2008-09-04 18:51:26 +00:00
|
|
|
if (CS.getCalledFunction() == F) {
|
|
|
|
SmallVector<Value*, 6> args;
|
2010-06-05 14:50:21 +00:00
|
|
|
// Assemble the non-specialized arguments for the updated callsite.
|
|
|
|
// In the process, make sure that the specialized arguments are
|
|
|
|
// constant and match the specialization. If that's not the case,
|
|
|
|
// this callsite needs to call the original or some other
|
|
|
|
// specialization; don't change it here.
|
|
|
|
CallSite::arg_iterator as = CS.arg_begin(), ae = CS.arg_end();
|
|
|
|
for (CallSite::arg_iterator ai = as; ai != ae; ++ai) {
|
|
|
|
DenseMap<unsigned, const Argument*>::iterator delit = deleted.find(
|
|
|
|
std::distance(as, ai));
|
|
|
|
if (delit == deleted.end())
|
|
|
|
args.push_back(cast<Value>(ai));
|
|
|
|
else {
|
|
|
|
Constant *ci = dyn_cast<Constant>(ai);
|
|
|
|
if (!(ci && ci == replacements[delit->second]))
|
|
|
|
goto next_use;
|
|
|
|
}
|
|
|
|
}
|
2008-09-04 18:51:26 +00:00
|
|
|
Value* NCall;
|
2010-07-22 13:04:32 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(U)) {
|
2008-09-04 18:51:26 +00:00
|
|
|
NCall = CallInst::Create(NF, args.begin(), args.end(),
|
2009-03-08 19:02:17 +00:00
|
|
|
CI->getName(), CI);
|
|
|
|
cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
|
|
|
|
cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
|
|
|
|
} else {
|
2010-07-22 13:04:32 +00:00
|
|
|
InvokeInst *II = cast<InvokeInst>(U);
|
2009-03-08 19:02:17 +00:00
|
|
|
NCall = InvokeInst::Create(NF, II->getNormalDest(),
|
|
|
|
II->getUnwindDest(),
|
2008-09-04 18:51:26 +00:00
|
|
|
args.begin(), args.end(),
|
2009-03-08 19:02:17 +00:00
|
|
|
II->getName(), II);
|
|
|
|
cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
|
|
|
|
}
|
2008-09-04 18:51:26 +00:00
|
|
|
CS.getInstruction()->replaceAllUsesWith(NCall);
|
|
|
|
CS.getInstruction()->eraseFromParent();
|
2010-06-05 14:50:21 +00:00
|
|
|
++numReplaced;
|
2008-09-04 18:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-22 13:04:32 +00:00
|
|
|
next_use:;
|
2008-09-04 18:51:26 +00:00
|
|
|
}
|
|
|
|
return NF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-03 21:00:28 +00:00
|
|
|
bool PartSpec::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Module::iterator I = M.begin(); I != M.end(); ++I) {
|
|
|
|
Function &F = *I;
|
2008-10-08 18:45:59 +00:00
|
|
|
if (F.isDeclaration() || F.mayBeOverridden()) continue;
|
2010-06-05 14:50:21 +00:00
|
|
|
InterestingArgVector interestingArgs;
|
2008-09-04 14:34:22 +00:00
|
|
|
scanForInterest(F, interestingArgs);
|
|
|
|
|
|
|
|
// Find the first interesting Argument that we can specialize on
|
|
|
|
// If there are multiple interesting Arguments, then those will be found
|
|
|
|
// when processing the cloned function.
|
|
|
|
bool breakOuter = false;
|
|
|
|
for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
|
|
|
|
std::map<Constant*, int> distribution;
|
2010-10-09 22:06:36 +00:00
|
|
|
scanDistribution(F, interestingArgs[x], distribution);
|
|
|
|
for (std::map<Constant*, int>::iterator ii = distribution.begin(),
|
|
|
|
ee = distribution.end(); ii != ee; ++ii) {
|
|
|
|
// The distribution map might have an entry for NULL (i.e., one or more
|
|
|
|
// callsites were passing a non-constant there). We allow that to
|
|
|
|
// happen so that we can see whether any callsites pass a non-constant;
|
|
|
|
// if none do and the function is internal, we might have an opportunity
|
|
|
|
// to kill the original function.
|
|
|
|
if (!ii->first) continue;
|
|
|
|
int bonus = ii->second;
|
|
|
|
SmallVector<unsigned, 1> argnos;
|
|
|
|
argnos.push_back(interestingArgs[x]);
|
|
|
|
InlineCost cost = CA.getSpecializationCost(&F, argnos);
|
|
|
|
// FIXME: If this is the last constant entry, and no non-constant
|
|
|
|
// entries exist, and the target function is internal, the cost should
|
|
|
|
// be reduced by the original size of the target function, almost
|
|
|
|
// certainly making it negative and causing a specialization that will
|
|
|
|
// leave the original function dead and removable.
|
|
|
|
if (cost.isAlways() ||
|
|
|
|
(cost.isVariable() && cost.getValue() < bonus)) {
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy m;
|
2010-10-09 22:06:36 +00:00
|
|
|
Function::arg_iterator arg = F.arg_begin();
|
|
|
|
for (int y = 0; y < interestingArgs[x]; ++y)
|
|
|
|
++arg;
|
|
|
|
m[&*arg] = ii->first;
|
|
|
|
SpecializeFunction(&F, m);
|
|
|
|
++numSpecialized;
|
|
|
|
breakOuter = true;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
2008-09-03 21:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// scanForInterest - This function decides which arguments would be worth
|
2008-09-04 14:34:22 +00:00
|
|
|
/// specializing on.
|
2010-06-05 14:50:21 +00:00
|
|
|
void PartSpec::scanForInterest(Function& F, InterestingArgVector& args) {
|
2008-09-03 21:00:28 +00:00
|
|
|
for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
|
|
|
|
ii != ee; ++ii) {
|
2010-10-09 22:06:36 +00:00
|
|
|
int argno = std::distance(F.arg_begin(), ii);
|
|
|
|
SmallVector<unsigned, 1> argnos;
|
|
|
|
argnos.push_back(argno);
|
|
|
|
int bonus = CA.getSpecializationBonus(&F, argnos);
|
|
|
|
if (bonus > 0) {
|
|
|
|
args.push_back(argno);
|
2008-09-03 21:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-03 19:29:29 +00:00
|
|
|
/// scanDistribution - Construct a histogram of constants for arg of F at arg.
|
2010-10-09 22:06:36 +00:00
|
|
|
/// For each distinct constant, we'll compute the total of the specialization
|
|
|
|
/// bonus across all callsites passing that constant; if that total exceeds
|
|
|
|
/// the specialization cost, we will create the specialization.
|
2008-09-03 21:00:28 +00:00
|
|
|
int PartSpec::scanDistribution(Function& F, int arg,
|
|
|
|
std::map<Constant*, int>& dist) {
|
2008-09-04 14:34:22 +00:00
|
|
|
bool hasIndirect = false;
|
2008-09-03 21:00:28 +00:00
|
|
|
int total = 0;
|
2010-07-22 13:04:32 +00:00
|
|
|
for (Value::use_iterator ii = F.use_begin(), ee = F.use_end();
|
|
|
|
ii != ee; ++ii) {
|
|
|
|
User *U = *ii;
|
|
|
|
CallSite CS(U);
|
|
|
|
if (CS && CS.getCalledFunction() == &F) {
|
2010-10-09 22:06:36 +00:00
|
|
|
SmallVector<unsigned, 1> argnos;
|
|
|
|
argnos.push_back(arg);
|
|
|
|
dist[dyn_cast<Constant>(CS.getArgument(arg))] +=
|
|
|
|
CA.getSpecializationBonus(&F, argnos);
|
2008-09-03 21:00:28 +00:00
|
|
|
++total;
|
|
|
|
} else
|
2008-09-04 14:34:22 +00:00
|
|
|
hasIndirect = true;
|
2010-07-22 13:04:32 +00:00
|
|
|
}
|
2008-09-04 14:34:22 +00:00
|
|
|
|
|
|
|
// Preserve the original address taken function even if all other uses
|
|
|
|
// will be specialized.
|
|
|
|
if (hasIndirect) ++total;
|
2008-09-03 21:00:28 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }
|