blob: 084b94e53566132bcfab5a3255c4abc9654b3fb4 [file] [log] [blame]
Andrew Lenharthcf996d42008-09-03 21:00:28 +00001//===-- 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//
Andrew Lenharthef780322008-09-04 14:34:22 +000013// This pass simply does the cloning for functions it specializes. It depends
14// on IPSCCP and DAE to clean up the results.
15//
16// The initial heuristic favors constant arguments that are used in control
17// flow.
Andrew Lenharthcf996d42008-09-03 21:00:28 +000018//
19//===----------------------------------------------------------------------===//
20
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"
Andrew Lenhartheb504792008-09-04 18:51:26 +000029#include "llvm/Support/CallSite.h"
Andrew Lenhartheb504792008-09-04 18:51:26 +000030#include "llvm/ADT/DenseSet.h"
Andrew Lenharthcf996d42008-09-03 21:00:28 +000031#include <map>
Andrew Lenharthcf996d42008-09-03 21:00:28 +000032using namespace llvm;
33
34STATISTIC(numSpecialized, "Number of specialized functions created");
35
Andrew Lenharthef780322008-09-04 14:34:22 +000036// Call must be used at least occasionally
Andrew Lenharthcf996d42008-09-03 21:00:28 +000037static const int CallsMin = 5;
Andrew Lenharthef780322008-09-04 14:34:22 +000038
39// Must have 10% of calls having the same constant to specialize on
Andrew Lenharthcf996d42008-09-03 21:00:28 +000040static const double ConstValPercent = .1;
41
42namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class PartSpec : public ModulePass {
Andrew Lenharthef780322008-09-04 14:34:22 +000044 void scanForInterest(Function&, SmallVector<int, 6>&);
Andrew Lenharthcf996d42008-09-03 21:00:28 +000045 int scanDistribution(Function&, int, std::map<Constant*, int>&);
46 public :
47 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000048 PartSpec() : ModulePass(&ID) {}
Andrew Lenharthcf996d42008-09-03 21:00:28 +000049 bool runOnModule(Module &M);
50 };
51}
52
53char PartSpec::ID = 0;
54static RegisterPass<PartSpec>
55X("partialspecialization", "Partial Specialization");
56
Andrew Lenhartheb504792008-09-04 18:51:26 +000057// Specialize F by replacing the arguments (keys) in replacements with the
58// constants (values). Replace all calls to F with those constants with
59// a call to the specialized function. Returns the specialized function
60static Function*
61SpecializeFunction(Function* F,
62 DenseMap<const Value*, Value*>& replacements) {
63 // arg numbers of deleted arguments
64 DenseSet<unsigned> deleted;
65 for (DenseMap<const Value*, Value*>::iterator
66 repb = replacements.begin(), repe = replacements.end();
Nick Lewyckyd694a782009-03-08 19:02:17 +000067 repb != repe; ++repb)
Andrew Lenhartheb504792008-09-04 18:51:26 +000068 deleted.insert(cast<Argument>(repb->first)->getArgNo());
69
70 Function* NF = CloneFunction(F, replacements);
71 NF->setLinkage(GlobalValue::InternalLinkage);
72 F->getParent()->getFunctionList().push_back(NF);
73
74 for (Value::use_iterator ii = F->use_begin(), ee = F->use_end();
75 ii != ee; ) {
Nick Lewyckyd694a782009-03-08 19:02:17 +000076 Value::use_iterator i = ii;
Andrew Lenhartheb504792008-09-04 18:51:26 +000077 ++ii;
78 if (isa<CallInst>(i) || isa<InvokeInst>(i)) {
79 CallSite CS(cast<Instruction>(i));
80 if (CS.getCalledFunction() == F) {
81
82 SmallVector<Value*, 6> args;
83 for (unsigned x = 0; x < CS.arg_size(); ++x)
84 if (!deleted.count(x))
85 args.push_back(CS.getArgument(x));
86 Value* NCall;
Nick Lewyckyd694a782009-03-08 19:02:17 +000087 if (CallInst *CI = dyn_cast<CallInst>(i)) {
Andrew Lenhartheb504792008-09-04 18:51:26 +000088 NCall = CallInst::Create(NF, args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +000089 CI->getName(), CI);
90 cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
91 cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
92 } else {
93 InvokeInst *II = cast<InvokeInst>(i);
94 NCall = InvokeInst::Create(NF, II->getNormalDest(),
95 II->getUnwindDest(),
Andrew Lenhartheb504792008-09-04 18:51:26 +000096 args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +000097 II->getName(), II);
98 cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
99 }
Andrew Lenhartheb504792008-09-04 18:51:26 +0000100 CS.getInstruction()->replaceAllUsesWith(NCall);
101 CS.getInstruction()->eraseFromParent();
102 }
103 }
104 }
105 return NF;
106}
107
108
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000109bool PartSpec::runOnModule(Module &M) {
110 bool Changed = false;
111 for (Module::iterator I = M.begin(); I != M.end(); ++I) {
112 Function &F = *I;
Nuno Lopes7a85a622008-10-08 18:45:59 +0000113 if (F.isDeclaration() || F.mayBeOverridden()) continue;
Andrew Lenharthef780322008-09-04 14:34:22 +0000114 SmallVector<int, 6> interestingArgs;
115 scanForInterest(F, interestingArgs);
116
117 // Find the first interesting Argument that we can specialize on
118 // If there are multiple interesting Arguments, then those will be found
119 // when processing the cloned function.
120 bool breakOuter = false;
121 for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
122 std::map<Constant*, int> distribution;
123 int total = scanDistribution(F, interestingArgs[x], distribution);
124 if (total > CallsMin)
125 for (std::map<Constant*, int>::iterator ii = distribution.begin(),
126 ee = distribution.end(); ii != ee; ++ii)
127 if (total > ii->second && ii->first &&
128 ii->second > total * ConstValPercent) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000129 DenseMap<const Value*, Value*> m;
130 Function::arg_iterator arg = F.arg_begin();
131 for (int y = 0; y < interestingArgs[x]; ++y)
132 ++arg;
133 m[&*arg] = ii->first;
134 SpecializeFunction(&F, m);
135 ++numSpecialized;
Andrew Lenharthef780322008-09-04 14:34:22 +0000136 breakOuter = true;
137 Changed = true;
138 }
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000139 }
140 }
141 return Changed;
142}
143
144/// scanForInterest - This function decides which arguments would be worth
Andrew Lenharthef780322008-09-04 14:34:22 +0000145/// specializing on.
146void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000147 for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
148 ii != ee; ++ii) {
149 for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
150 ui != ue; ++ui) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000151
152 bool interesting = false;
153
154 if (isa<CmpInst>(ui)) interesting = true;
155 else if (isa<CallInst>(ui))
156 interesting = ui->getOperand(0) == ii;
157 else if (isa<InvokeInst>(ui))
158 interesting = ui->getOperand(0) == ii;
159 else if (isa<SwitchInst>(ui)) interesting = true;
160 else if (isa<BranchInst>(ui)) interesting = true;
161
162 if (interesting) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000163 args.push_back(std::distance(F.arg_begin(), ii));
164 break;
165 }
166 }
167 }
168}
169
Andrew Lenharth06a12422008-11-03 19:29:29 +0000170/// scanDistribution - Construct a histogram of constants for arg of F at arg.
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000171int PartSpec::scanDistribution(Function& F, int arg,
172 std::map<Constant*, int>& dist) {
Andrew Lenharthef780322008-09-04 14:34:22 +0000173 bool hasIndirect = false;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000174 int total = 0;
175 for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
176 ii != ee; ++ii)
Andrew Lenharth97bd9a92008-11-03 16:05:35 +0000177 if ((isa<CallInst>(ii) || isa<InvokeInst>(ii))
178 && ii->getOperand(0) == &F) {
179 ++dist[dyn_cast<Constant>(ii->getOperand(arg + 1))];
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000180 ++total;
181 } else
Andrew Lenharthef780322008-09-04 14:34:22 +0000182 hasIndirect = true;
183
184 // Preserve the original address taken function even if all other uses
185 // will be specialized.
186 if (hasIndirect) ++total;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000187 return total;
188}
189
190ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }