cleanup as per Duncan's review

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55766 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Andrew Lenharth 2008-09-04 14:34:22 +00:00
parent a3971dfbfe
commit ef7803217a

View File

@ -10,7 +10,11 @@
// This pass finds function arguments that are often a common constant and
// specializes a version of the called function for that constant.
//
// The initial heuristic favors constant arguments that used in control flow.
// 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.
//
//===----------------------------------------------------------------------===//
@ -24,19 +28,19 @@
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Support/Compiler.h"
#include <map>
#include <vector>
using namespace llvm;
STATISTIC(numSpecialized, "Number of specialized functions created");
//Call must be used at least occasionally
// Call must be used at least occasionally
static const int CallsMin = 5;
//Must have 10% of calls having the same constant to specialize on
// Must have 10% of calls having the same constant to specialize on
static const double ConstValPercent = .1;
namespace {
class VISIBILITY_HIDDEN PartSpec : public ModulePass {
void scanForInterest(Function&, std::vector<int>&);
void scanForInterest(Function&, SmallVector<int, 6>&);
void replaceUsersFor(Function&, int, Constant*, Function*);
int scanDistribution(Function&, int, std::map<Constant*, int>&);
public :
@ -54,41 +58,43 @@ bool PartSpec::runOnModule(Module &M) {
bool Changed = false;
for (Module::iterator I = M.begin(); I != M.end(); ++I) {
Function &F = *I;
if (!F.isDeclaration()) {
std::vector<int> interestingArgs;
scanForInterest(F, interestingArgs);
//Find the first interesting Argument that we can specialize on
//If there are multiple intersting 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;
int total = scanDistribution(F, interestingArgs[x], distribution);
if (total > CallsMin)
for (std::map<Constant*, int>::iterator ii = distribution.begin(),
ee = distribution.end(); ii != ee; ++ii)
if ( total > ii->second && ii->first &&
ii->second > total * ConstValPercent ) {
Function* NF = CloneFunction(&F);
NF->setLinkage(GlobalValue::InternalLinkage);
M.getFunctionList().push_back(NF);
replaceUsersFor(F, interestingArgs[x], ii->first, NF);
breakOuter = true;
Changed = true;
}
}
if (F.isDeclaration()) continue;
SmallVector<int, 6> interestingArgs;
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;
int total = scanDistribution(F, interestingArgs[x], distribution);
if (total > CallsMin)
for (std::map<Constant*, int>::iterator ii = distribution.begin(),
ee = distribution.end(); ii != ee; ++ii)
if (total > ii->second && ii->first &&
ii->second > total * ConstValPercent) {
Function* NF = CloneFunction(&F);
NF->setLinkage(GlobalValue::InternalLinkage);
M.getFunctionList().push_back(NF);
replaceUsersFor(F, interestingArgs[x], ii->first, NF);
breakOuter = true;
Changed = true;
}
}
}
return Changed;
}
/// scanForInterest - This function decides which arguments would be worth
/// specializing on.
void PartSpec::scanForInterest(Function& F, std::vector<int>& args) {
/// specializing on.
void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
ii != ee; ++ii) {
for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
ui != ue; ++ui) {
// As an initial proxy for control flow, specialize on arguments
// that are used in comparisons.
if (isa<CmpInst>(ui)) {
args.push_back(std::distance(F.arg_begin(), ii));
break;
@ -111,7 +117,7 @@ void PartSpec::replaceUsersFor(Function& F , int argnum, Constant* val,
int PartSpec::scanDistribution(Function& F, int arg,
std::map<Constant*, int>& dist) {
bool hasInd = false;
bool hasIndirect = false;
int total = 0;
for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
ii != ee; ++ii)
@ -119,8 +125,11 @@ int PartSpec::scanDistribution(Function& F, int arg,
++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
++total;
} else
hasInd = true;
if (hasInd) ++total;
hasIndirect = true;
// Preserve the original address taken function even if all other uses
// will be specialized.
if (hasIndirect) ++total;
return total;
}