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