1 //===-- PartialSpecialization.cpp - Specialize for common constants--------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass finds function arguments that are often a common constant and
11 // specializes a version of the called function for that constant.
13 // This pass simply does the cloning for functions it specializes. It depends
14 // on IPSCCP and DAE to clean up the results.
16 // The initial heuristic favors constant arguments that are used in control
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "partialspecialization"
22 #include "llvm/Transforms/IPO.h"
23 #include "llvm/Constant.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Module.h"
26 #include "llvm/Pass.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Support/Compiler.h"
33 STATISTIC(numSpecialized, "Number of specialized functions created");
35 // Call must be used at least occasionally
36 static const int CallsMin = 5;
38 // Must have 10% of calls having the same constant to specialize on
39 static const double ConstValPercent = .1;
42 class VISIBILITY_HIDDEN PartSpec : public ModulePass {
43 void scanForInterest(Function&, SmallVector<int, 6>&);
44 void replaceUsersFor(Function&, int, Constant*, Function*);
45 int scanDistribution(Function&, int, std::map<Constant*, int>&);
47 static char ID; // Pass identification, replacement for typeid
48 PartSpec() : ModulePass(&ID) {}
49 bool runOnModule(Module &M);
53 char PartSpec::ID = 0;
54 static RegisterPass<PartSpec>
55 X("partialspecialization", "Partial Specialization");
57 bool PartSpec::runOnModule(Module &M) {
59 for (Module::iterator I = M.begin(); I != M.end(); ++I) {
61 if (F.isDeclaration()) continue;
62 SmallVector<int, 6> interestingArgs;
63 scanForInterest(F, interestingArgs);
65 // Find the first interesting Argument that we can specialize on
66 // If there are multiple interesting Arguments, then those will be found
67 // when processing the cloned function.
68 bool breakOuter = false;
69 for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
70 std::map<Constant*, int> distribution;
71 int total = scanDistribution(F, interestingArgs[x], distribution);
73 for (std::map<Constant*, int>::iterator ii = distribution.begin(),
74 ee = distribution.end(); ii != ee; ++ii)
75 if (total > ii->second && ii->first &&
76 ii->second > total * ConstValPercent) {
77 Function* NF = CloneFunction(&F);
78 NF->setLinkage(GlobalValue::InternalLinkage);
79 M.getFunctionList().push_back(NF);
80 replaceUsersFor(F, interestingArgs[x], ii->first, NF);
89 /// scanForInterest - This function decides which arguments would be worth
91 void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
92 for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
94 for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
96 // As an initial proxy for control flow, specialize on arguments
97 // that are used in comparisons.
98 if (isa<CmpInst>(ui)) {
99 args.push_back(std::distance(F.arg_begin(), ii));
106 /// replaceUsersFor - Replace direct calls to F with NF if the arg argnum is
108 void PartSpec::replaceUsersFor(Function& F , int argnum, Constant* val,
111 for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
113 if (CallInst* CI = dyn_cast<CallInst>(ii))
114 if (CI->getOperand(0) == &F && CI->getOperand(argnum + 1) == val)
115 CI->setOperand(0, NF);
118 int PartSpec::scanDistribution(Function& F, int arg,
119 std::map<Constant*, int>& dist) {
120 bool hasIndirect = false;
122 for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
124 if (CallInst* CI = dyn_cast<CallInst>(ii)) {
125 ++dist[dyn_cast<Constant>(CI->getOperand(arg + 1))];
130 // Preserve the original address taken function even if all other uses
131 // will be specialized.
132 if (hasIndirect) ++total;
136 ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }