blob: 4e72d51c7d9f44a4aa7369e589c471b91cb2acac [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");
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000035STATISTIC(numReplaced, "Number of callers replaced by specialization");
36
37// Maximum number of arguments markable interested
38static const int MaxInterests = 6;
Andrew Lenharthcf996d42008-09-03 21:00:28 +000039
Andrew Lenharthef780322008-09-04 14:34:22 +000040// Call must be used at least occasionally
Andrew Lenharthcf996d42008-09-03 21:00:28 +000041static const int CallsMin = 5;
Andrew Lenharthef780322008-09-04 14:34:22 +000042
43// Must have 10% of calls having the same constant to specialize on
Andrew Lenharthcf996d42008-09-03 21:00:28 +000044static const double ConstValPercent = .1;
45
46namespace {
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000047 typedef SmallVector<int, MaxInterests> InterestingArgVector;
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048 class PartSpec : public ModulePass {
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000049 void scanForInterest(Function&, InterestingArgVector&);
Andrew Lenharthcf996d42008-09-03 21:00:28 +000050 int scanDistribution(Function&, int, std::map<Constant*, int>&);
51 public :
52 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000053 PartSpec() : ModulePass(&ID) {}
Andrew Lenharthcf996d42008-09-03 21:00:28 +000054 bool runOnModule(Module &M);
55 };
56}
57
58char PartSpec::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000059INITIALIZE_PASS(PartSpec, "partialspecialization",
60 "Partial Specialization", false, false);
Andrew Lenharthcf996d42008-09-03 21:00:28 +000061
Andrew Lenhartheb504792008-09-04 18:51:26 +000062// Specialize F by replacing the arguments (keys) in replacements with the
63// constants (values). Replace all calls to F with those constants with
64// a call to the specialized function. Returns the specialized function
65static Function*
66SpecializeFunction(Function* F,
Devang Patele9916a32010-06-24 00:33:28 +000067 ValueMap<const Value*, Value*>& replacements) {
Andrew Lenhartheb504792008-09-04 18:51:26 +000068 // arg numbers of deleted arguments
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000069 DenseMap<unsigned, const Argument*> deleted;
Devang Patele9916a32010-06-24 00:33:28 +000070 for (ValueMap<const Value*, Value*>::iterator
Andrew Lenhartheb504792008-09-04 18:51:26 +000071 repb = replacements.begin(), repe = replacements.end();
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000072 repb != repe; ++repb) {
73 Argument const *arg = cast<const Argument>(repb->first);
74 deleted[arg->getArgNo()] = arg;
75 }
Andrew Lenhartheb504792008-09-04 18:51:26 +000076
77 Function* NF = CloneFunction(F, replacements);
78 NF->setLinkage(GlobalValue::InternalLinkage);
79 F->getParent()->getFunctionList().push_back(NF);
80
81 for (Value::use_iterator ii = F->use_begin(), ee = F->use_end();
82 ii != ee; ) {
Nick Lewyckyd694a782009-03-08 19:02:17 +000083 Value::use_iterator i = ii;
Andrew Lenhartheb504792008-09-04 18:51:26 +000084 ++ii;
Gabor Greifefdf0392010-07-22 13:04:32 +000085 User *U = *i;
86 if (isa<CallInst>(U) || isa<InvokeInst>(U)) {
87 CallSite CS(cast<Instruction>(U));
Andrew Lenhartheb504792008-09-04 18:51:26 +000088 if (CS.getCalledFunction() == F) {
89
90 SmallVector<Value*, 6> args;
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +000091 // Assemble the non-specialized arguments for the updated callsite.
92 // In the process, make sure that the specialized arguments are
93 // constant and match the specialization. If that's not the case,
94 // this callsite needs to call the original or some other
95 // specialization; don't change it here.
96 CallSite::arg_iterator as = CS.arg_begin(), ae = CS.arg_end();
97 for (CallSite::arg_iterator ai = as; ai != ae; ++ai) {
98 DenseMap<unsigned, const Argument*>::iterator delit = deleted.find(
99 std::distance(as, ai));
100 if (delit == deleted.end())
101 args.push_back(cast<Value>(ai));
102 else {
103 Constant *ci = dyn_cast<Constant>(ai);
104 if (!(ci && ci == replacements[delit->second]))
105 goto next_use;
106 }
107 }
Andrew Lenhartheb504792008-09-04 18:51:26 +0000108 Value* NCall;
Gabor Greifefdf0392010-07-22 13:04:32 +0000109 if (CallInst *CI = dyn_cast<CallInst>(U)) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000110 NCall = CallInst::Create(NF, args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +0000111 CI->getName(), CI);
112 cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
113 cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
114 } else {
Gabor Greifefdf0392010-07-22 13:04:32 +0000115 InvokeInst *II = cast<InvokeInst>(U);
Nick Lewyckyd694a782009-03-08 19:02:17 +0000116 NCall = InvokeInst::Create(NF, II->getNormalDest(),
117 II->getUnwindDest(),
Andrew Lenhartheb504792008-09-04 18:51:26 +0000118 args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +0000119 II->getName(), II);
120 cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
121 }
Andrew Lenhartheb504792008-09-04 18:51:26 +0000122 CS.getInstruction()->replaceAllUsesWith(NCall);
123 CS.getInstruction()->eraseFromParent();
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +0000124 ++numReplaced;
Andrew Lenhartheb504792008-09-04 18:51:26 +0000125 }
126 }
Gabor Greifefdf0392010-07-22 13:04:32 +0000127 next_use:;
Andrew Lenhartheb504792008-09-04 18:51:26 +0000128 }
129 return NF;
130}
131
132
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000133bool PartSpec::runOnModule(Module &M) {
134 bool Changed = false;
135 for (Module::iterator I = M.begin(); I != M.end(); ++I) {
136 Function &F = *I;
Nuno Lopes7a85a622008-10-08 18:45:59 +0000137 if (F.isDeclaration() || F.mayBeOverridden()) continue;
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +0000138 InterestingArgVector interestingArgs;
Andrew Lenharthef780322008-09-04 14:34:22 +0000139 scanForInterest(F, interestingArgs);
140
141 // Find the first interesting Argument that we can specialize on
142 // If there are multiple interesting Arguments, then those will be found
143 // when processing the cloned function.
144 bool breakOuter = false;
145 for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
146 std::map<Constant*, int> distribution;
147 int total = scanDistribution(F, interestingArgs[x], distribution);
148 if (total > CallsMin)
149 for (std::map<Constant*, int>::iterator ii = distribution.begin(),
150 ee = distribution.end(); ii != ee; ++ii)
151 if (total > ii->second && ii->first &&
152 ii->second > total * ConstValPercent) {
Devang Patele9916a32010-06-24 00:33:28 +0000153 ValueMap<const Value*, Value*> m;
Andrew Lenhartheb504792008-09-04 18:51:26 +0000154 Function::arg_iterator arg = F.arg_begin();
155 for (int y = 0; y < interestingArgs[x]; ++y)
156 ++arg;
157 m[&*arg] = ii->first;
158 SpecializeFunction(&F, m);
159 ++numSpecialized;
Andrew Lenharthef780322008-09-04 14:34:22 +0000160 breakOuter = true;
161 Changed = true;
162 }
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000163 }
164 }
165 return Changed;
166}
167
168/// scanForInterest - This function decides which arguments would be worth
Andrew Lenharthef780322008-09-04 14:34:22 +0000169/// specializing on.
Kenneth Uildriks3a4340d2010-06-05 14:50:21 +0000170void PartSpec::scanForInterest(Function& F, InterestingArgVector& args) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000171 for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
172 ii != ee; ++ii) {
173 for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
174 ui != ue; ++ui) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000175
176 bool interesting = false;
Gabor Greifefdf0392010-07-22 13:04:32 +0000177 User *U = *ui;
178 if (isa<CmpInst>(U)) interesting = true;
179 else if (isa<CallInst>(U))
Andrew Lenhartheb504792008-09-04 18:51:26 +0000180 interesting = ui->getOperand(0) == ii;
Gabor Greifefdf0392010-07-22 13:04:32 +0000181 else if (isa<InvokeInst>(U))
Andrew Lenhartheb504792008-09-04 18:51:26 +0000182 interesting = ui->getOperand(0) == ii;
Gabor Greifefdf0392010-07-22 13:04:32 +0000183 else if (isa<SwitchInst>(U)) interesting = true;
184 else if (isa<BranchInst>(U)) interesting = true;
Andrew Lenhartheb504792008-09-04 18:51:26 +0000185
186 if (interesting) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000187 args.push_back(std::distance(F.arg_begin(), ii));
188 break;
189 }
190 }
191 }
192}
193
Andrew Lenharth06a12422008-11-03 19:29:29 +0000194/// scanDistribution - Construct a histogram of constants for arg of F at arg.
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000195int PartSpec::scanDistribution(Function& F, int arg,
196 std::map<Constant*, int>& dist) {
Andrew Lenharthef780322008-09-04 14:34:22 +0000197 bool hasIndirect = false;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000198 int total = 0;
Gabor Greifefdf0392010-07-22 13:04:32 +0000199 for (Value::use_iterator ii = F.use_begin(), ee = F.use_end();
200 ii != ee; ++ii) {
201 User *U = *ii;
202 CallSite CS(U);
203 if (CS && CS.getCalledFunction() == &F) {
204 ++dist[dyn_cast<Constant>(CS.getArgument(arg))];
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000205 ++total;
206 } else
Andrew Lenharthef780322008-09-04 14:34:22 +0000207 hasIndirect = true;
Gabor Greifefdf0392010-07-22 13:04:32 +0000208 }
Andrew Lenharthef780322008-09-04 14:34:22 +0000209
210 // Preserve the original address taken function even if all other uses
211 // will be specialized.
212 if (hasIndirect) ++total;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000213 return total;
214}
215
216ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }