From eb50479714047c5b8d88e5e879089925156194f2 Mon Sep 17 00:00:00 2001 From: Andrew Lenharth Date: Thu, 4 Sep 2008 18:51:26 +0000 Subject: [PATCH] try to seperate the mechanism into something others can use git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55785 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/PartialSpecialization.cpp | 90 +++++++++++++++----- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/lib/Transforms/IPO/PartialSpecialization.cpp b/lib/Transforms/IPO/PartialSpecialization.cpp index 949358e7bc9..da127cb9f5f 100644 --- a/lib/Transforms/IPO/PartialSpecialization.cpp +++ b/lib/Transforms/IPO/PartialSpecialization.cpp @@ -26,7 +26,9 @@ #include "llvm/Pass.h" #include "llvm/ADT/Statistic.h" #include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/Support/CallSite.h" #include "llvm/Support/Compiler.h" +#include "llvm/ADT/DenseSet.h" #include using namespace llvm; @@ -41,7 +43,6 @@ static const double ConstValPercent = .1; namespace { class VISIBILITY_HIDDEN PartSpec : public ModulePass { void scanForInterest(Function&, SmallVector&); - void replaceUsersFor(Function&, int, Constant*, Function*); int scanDistribution(Function&, int, std::map&); public : static char ID; // Pass identification, replacement for typeid @@ -54,6 +55,55 @@ char PartSpec::ID = 0; static RegisterPass X("partialspecialization", "Partial Specialization"); +// 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, + DenseMap& replacements) { + // arg numbers of deleted arguments + DenseSet deleted; + for (DenseMap::iterator + repb = replacements.begin(), repe = replacements.end(); + repb != repe; ++ repb) + deleted.insert(cast(repb->first)->getArgNo()); + + Function* NF = CloneFunction(F, replacements); + NF->setLinkage(GlobalValue::InternalLinkage); + F->getParent()->getFunctionList().push_back(NF); + + for (Value::use_iterator ii = F->use_begin(), ee = F->use_end(); + ii != ee; ) { + Value::use_iterator i = ii;; + ++ii; + if (isa(i) || isa(i)) { + CallSite CS(cast(i)); + if (CS.getCalledFunction() == F) { + + SmallVector args; + for (unsigned x = 0; x < CS.arg_size(); ++x) + if (!deleted.count(x)) + args.push_back(CS.getArgument(x)); + Value* NCall; + if (isa(i)) + NCall = CallInst::Create(NF, args.begin(), args.end(), + CS.getInstruction()->getName(), + CS.getInstruction()); + else + NCall = InvokeInst::Create(NF, cast(i)->getNormalDest(), + cast(i)->getUnwindDest(), + args.begin(), args.end(), + CS.getInstruction()->getName(), + CS.getInstruction()); + CS.getInstruction()->replaceAllUsesWith(NCall); + CS.getInstruction()->eraseFromParent(); + } + } + } + return NF; +} + + bool PartSpec::runOnModule(Module &M) { bool Changed = false; for (Module::iterator I = M.begin(); I != M.end(); ++I) { @@ -74,10 +124,13 @@ bool PartSpec::runOnModule(Module &M) { 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); + DenseMap m; + 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; } @@ -93,9 +146,18 @@ void PartSpec::scanForInterest(Function& F, SmallVector& args) { 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(ui)) { + + bool interesting = false; + + if (isa(ui)) interesting = true; + else if (isa(ui)) + interesting = ui->getOperand(0) == ii; + else if (isa(ui)) + interesting = ui->getOperand(0) == ii; + else if (isa(ui)) interesting = true; + else if (isa(ui)) interesting = true; + + if (interesting) { args.push_back(std::distance(F.arg_begin(), ii)); break; } @@ -103,18 +165,6 @@ void PartSpec::scanForInterest(Function& F, SmallVector& args) { } } -/// replaceUsersFor - Replace direct calls to F with NF if the arg argnum is -/// the constant val -void PartSpec::replaceUsersFor(Function& F , int argnum, Constant* val, - Function* NF) { - ++numSpecialized; - for(Value::use_iterator ii = F.use_begin(), ee = F.use_end(); - ii != ee; ++ii) - if (CallInst* CI = dyn_cast(ii)) - if (CI->getOperand(0) == &F && CI->getOperand(argnum + 1) == val) - CI->setOperand(0, NF); -} - int PartSpec::scanDistribution(Function& F, int arg, std::map& dist) { bool hasIndirect = false; -- 2.34.1