Andrew Lenharth | cf996d4 | 2008-09-03 21:00:28 +0000 | [diff] [blame^] | 1 | //===-- PartialSpecialization.cpp - Specialize for common constants--------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass finds function arguments that are often a common constant and |
| 11 | // specializes a version of the called function for that constant. |
| 12 | // |
| 13 | // The initial heuristic favors constant arguments that used in control flow. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "partialspecialization" |
| 18 | #include "llvm/Transforms/IPO.h" |
| 19 | #include "llvm/Constant.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Transforms/Utils/Cloning.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <map> |
| 27 | #include <vector> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | STATISTIC(numSpecialized, "Number of specialized functions created"); |
| 31 | |
| 32 | //Call must be used at least occasionally |
| 33 | static const int CallsMin = 5; |
| 34 | //Must have 10% of calls having the same constant to specialize on |
| 35 | static const double ConstValPercent = .1; |
| 36 | |
| 37 | namespace { |
| 38 | class VISIBILITY_HIDDEN PartSpec : public ModulePass { |
| 39 | void scanForInterest(Function&, std::vector<int>&); |
| 40 | void replaceUsersFor(Function&, int, Constant*, Function*); |
| 41 | int scanDistribution(Function&, int, std::map<Constant*, int>&); |
| 42 | public : |
| 43 | static char ID; // Pass identification, replacement for typeid |
| 44 | PartSpec() : ModulePass((intptr_t)&ID) {} |
| 45 | bool runOnModule(Module &M); |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | char PartSpec::ID = 0; |
| 50 | static RegisterPass<PartSpec> |
| 51 | X("partialspecialization", "Partial Specialization"); |
| 52 | |
| 53 | bool PartSpec::runOnModule(Module &M) { |
| 54 | bool Changed = false; |
| 55 | for (Module::iterator I = M.begin(); I != M.end(); ++I) { |
| 56 | Function &F = *I; |
| 57 | if (!F.isDeclaration()) { |
| 58 | std::vector<int> interestingArgs; |
| 59 | scanForInterest(F, interestingArgs); |
| 60 | //Find the first interesting Argument that we can specialize on |
| 61 | //If there are multiple intersting Arguments, then those will be found |
| 62 | //when processing the cloned function. |
| 63 | bool breakOuter = false; |
| 64 | for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) { |
| 65 | std::map<Constant*, int> distribution; |
| 66 | int total = scanDistribution(F, interestingArgs[x], distribution); |
| 67 | if (total > CallsMin) |
| 68 | for (std::map<Constant*, int>::iterator ii = distribution.begin(), |
| 69 | ee = distribution.end(); ii != ee; ++ii) |
| 70 | if ( total > ii->second && ii->first && |
| 71 | ii->second > total * ConstValPercent ) { |
| 72 | Function* NF = CloneFunction(&F); |
| 73 | NF->setLinkage(GlobalValue::InternalLinkage); |
| 74 | M.getFunctionList().push_back(NF); |
| 75 | replaceUsersFor(F, interestingArgs[x], ii->first, NF); |
| 76 | breakOuter = true; |
| 77 | Changed = true; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return Changed; |
| 83 | } |
| 84 | |
| 85 | /// scanForInterest - This function decides which arguments would be worth |
| 86 | /// specializing on. |
| 87 | void PartSpec::scanForInterest(Function& F, std::vector<int>& args) { |
| 88 | for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end(); |
| 89 | ii != ee; ++ii) { |
| 90 | for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end(); |
| 91 | ui != ue; ++ui) { |
| 92 | if (isa<CmpInst>(ui)) { |
| 93 | args.push_back(std::distance(F.arg_begin(), ii)); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// replaceUsersFor - Replace direct calls to F with NF if the arg argnum is |
| 101 | /// the constant val |
| 102 | void PartSpec::replaceUsersFor(Function& F , int argnum, Constant* val, |
| 103 | Function* NF) { |
| 104 | ++numSpecialized; |
| 105 | for(Value::use_iterator ii = F.use_begin(), ee = F.use_end(); |
| 106 | ii != ee; ++ii) |
| 107 | if (CallInst* CI = dyn_cast<CallInst>(ii)) |
| 108 | if (CI->getOperand(0) == &F && CI->getOperand(argnum + 1) == val) |
| 109 | CI->setOperand(0, NF); |
| 110 | } |
| 111 | |
| 112 | int PartSpec::scanDistribution(Function& F, int arg, |
| 113 | std::map<Constant*, int>& dist) { |
| 114 | bool hasInd = false; |
| 115 | int total = 0; |
| 116 | for(Value::use_iterator ii = F.use_begin(), ee = F.use_end(); |
| 117 | ii != ee; ++ii) |
| 118 | if (CallInst* CI = dyn_cast<CallInst>(ii)) { |
| 119 | ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))]; |
| 120 | ++total; |
| 121 | } else |
| 122 | hasInd = true; |
| 123 | if (hasInd) ++total; |
| 124 | return total; |
| 125 | } |
| 126 | |
| 127 | ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); } |