blob: 0e1fdb9915ac969969251c78da1e9d71551485bf [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 Lenharthcf996d42008-09-03 21:00:28 +000030#include "llvm/Support/Compiler.h"
Andrew Lenhartheb504792008-09-04 18:51:26 +000031#include "llvm/ADT/DenseSet.h"
Andrew Lenharthcf996d42008-09-03 21:00:28 +000032#include <map>
Andrew Lenharthcf996d42008-09-03 21:00:28 +000033using namespace llvm;
34
35STATISTIC(numSpecialized, "Number of specialized functions created");
36
Andrew Lenharthef780322008-09-04 14:34:22 +000037// Call must be used at least occasionally
Andrew Lenharthcf996d42008-09-03 21:00:28 +000038static const int CallsMin = 5;
Andrew Lenharthef780322008-09-04 14:34:22 +000039
40// Must have 10% of calls having the same constant to specialize on
Andrew Lenharthcf996d42008-09-03 21:00:28 +000041static const double ConstValPercent = .1;
42
43namespace {
44 class VISIBILITY_HIDDEN PartSpec : public ModulePass {
Andrew Lenharthef780322008-09-04 14:34:22 +000045 void scanForInterest(Function&, SmallVector<int, 6>&);
Andrew Lenharthcf996d42008-09-03 21:00:28 +000046 int scanDistribution(Function&, int, std::map<Constant*, int>&);
47 public :
48 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000049 PartSpec() : ModulePass(&ID) {}
Andrew Lenharthcf996d42008-09-03 21:00:28 +000050 bool runOnModule(Module &M);
51 };
52}
53
54char PartSpec::ID = 0;
55static RegisterPass<PartSpec>
56X("partialspecialization", "Partial Specialization");
57
Andrew Lenhartheb504792008-09-04 18:51:26 +000058// Specialize F by replacing the arguments (keys) in replacements with the
59// constants (values). Replace all calls to F with those constants with
60// a call to the specialized function. Returns the specialized function
61static Function*
62SpecializeFunction(Function* F,
63 DenseMap<const Value*, Value*>& replacements) {
64 // arg numbers of deleted arguments
65 DenseSet<unsigned> deleted;
66 for (DenseMap<const Value*, Value*>::iterator
67 repb = replacements.begin(), repe = replacements.end();
Nick Lewyckyd694a782009-03-08 19:02:17 +000068 repb != repe; ++repb)
Andrew Lenhartheb504792008-09-04 18:51:26 +000069 deleted.insert(cast<Argument>(repb->first)->getArgNo());
70
71 Function* NF = CloneFunction(F, replacements);
72 NF->setLinkage(GlobalValue::InternalLinkage);
73 F->getParent()->getFunctionList().push_back(NF);
74
75 for (Value::use_iterator ii = F->use_begin(), ee = F->use_end();
76 ii != ee; ) {
Nick Lewyckyd694a782009-03-08 19:02:17 +000077 Value::use_iterator i = ii;
Andrew Lenhartheb504792008-09-04 18:51:26 +000078 ++ii;
79 if (isa<CallInst>(i) || isa<InvokeInst>(i)) {
80 CallSite CS(cast<Instruction>(i));
81 if (CS.getCalledFunction() == F) {
82
83 SmallVector<Value*, 6> args;
84 for (unsigned x = 0; x < CS.arg_size(); ++x)
85 if (!deleted.count(x))
86 args.push_back(CS.getArgument(x));
87 Value* NCall;
Nick Lewyckyd694a782009-03-08 19:02:17 +000088 if (CallInst *CI = dyn_cast<CallInst>(i)) {
Andrew Lenhartheb504792008-09-04 18:51:26 +000089 NCall = CallInst::Create(NF, args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +000090 CI->getName(), CI);
91 cast<CallInst>(NCall)->setTailCall(CI->isTailCall());
92 cast<CallInst>(NCall)->setCallingConv(CI->getCallingConv());
93 } else {
94 InvokeInst *II = cast<InvokeInst>(i);
95 NCall = InvokeInst::Create(NF, II->getNormalDest(),
96 II->getUnwindDest(),
Andrew Lenhartheb504792008-09-04 18:51:26 +000097 args.begin(), args.end(),
Nick Lewyckyd694a782009-03-08 19:02:17 +000098 II->getName(), II);
99 cast<InvokeInst>(NCall)->setCallingConv(II->getCallingConv());
100 }
Andrew Lenhartheb504792008-09-04 18:51:26 +0000101 CS.getInstruction()->replaceAllUsesWith(NCall);
102 CS.getInstruction()->eraseFromParent();
103 }
104 }
105 }
106 return NF;
107}
108
109
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000110bool PartSpec::runOnModule(Module &M) {
111 bool Changed = false;
112 for (Module::iterator I = M.begin(); I != M.end(); ++I) {
113 Function &F = *I;
Nuno Lopes7a85a622008-10-08 18:45:59 +0000114 if (F.isDeclaration() || F.mayBeOverridden()) continue;
Andrew Lenharthef780322008-09-04 14:34:22 +0000115 SmallVector<int, 6> interestingArgs;
116 scanForInterest(F, interestingArgs);
117
118 // Find the first interesting Argument that we can specialize on
119 // If there are multiple interesting Arguments, then those will be found
120 // when processing the cloned function.
121 bool breakOuter = false;
122 for (unsigned int x = 0; !breakOuter && x < interestingArgs.size(); ++x) {
123 std::map<Constant*, int> distribution;
124 int total = scanDistribution(F, interestingArgs[x], distribution);
125 if (total > CallsMin)
126 for (std::map<Constant*, int>::iterator ii = distribution.begin(),
127 ee = distribution.end(); ii != ee; ++ii)
128 if (total > ii->second && ii->first &&
129 ii->second > total * ConstValPercent) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000130 DenseMap<const Value*, Value*> m;
131 Function::arg_iterator arg = F.arg_begin();
132 for (int y = 0; y < interestingArgs[x]; ++y)
133 ++arg;
134 m[&*arg] = ii->first;
135 SpecializeFunction(&F, m);
136 ++numSpecialized;
Andrew Lenharthef780322008-09-04 14:34:22 +0000137 breakOuter = true;
138 Changed = true;
139 }
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000140 }
141 }
142 return Changed;
143}
144
145/// scanForInterest - This function decides which arguments would be worth
Andrew Lenharthef780322008-09-04 14:34:22 +0000146/// specializing on.
147void PartSpec::scanForInterest(Function& F, SmallVector<int, 6>& args) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000148 for(Function::arg_iterator ii = F.arg_begin(), ee = F.arg_end();
149 ii != ee; ++ii) {
150 for(Value::use_iterator ui = ii->use_begin(), ue = ii->use_end();
151 ui != ue; ++ui) {
Andrew Lenhartheb504792008-09-04 18:51:26 +0000152
153 bool interesting = false;
154
155 if (isa<CmpInst>(ui)) interesting = true;
156 else if (isa<CallInst>(ui))
157 interesting = ui->getOperand(0) == ii;
158 else if (isa<InvokeInst>(ui))
159 interesting = ui->getOperand(0) == ii;
160 else if (isa<SwitchInst>(ui)) interesting = true;
161 else if (isa<BranchInst>(ui)) interesting = true;
162
163 if (interesting) {
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000164 args.push_back(std::distance(F.arg_begin(), ii));
165 break;
166 }
167 }
168 }
169}
170
Andrew Lenharth06a12422008-11-03 19:29:29 +0000171/// scanDistribution - Construct a histogram of constants for arg of F at arg.
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000172int PartSpec::scanDistribution(Function& F, int arg,
173 std::map<Constant*, int>& dist) {
Andrew Lenharthef780322008-09-04 14:34:22 +0000174 bool hasIndirect = false;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000175 int total = 0;
176 for(Value::use_iterator ii = F.use_begin(), ee = F.use_end();
177 ii != ee; ++ii)
Andrew Lenharth97bd9a92008-11-03 16:05:35 +0000178 if ((isa<CallInst>(ii) || isa<InvokeInst>(ii))
179 && ii->getOperand(0) == &F) {
180 ++dist[dyn_cast<Constant>(ii->getOperand(arg + 1))];
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000181 ++total;
182 } else
Andrew Lenharthef780322008-09-04 14:34:22 +0000183 hasIndirect = true;
184
185 // Preserve the original address taken function even if all other uses
186 // will be specialized.
187 if (hasIndirect) ++total;
Andrew Lenharthcf996d42008-09-03 21:00:28 +0000188 return total;
189}
190
191ModulePass* llvm::createPartialSpecializationPass() { return new PartSpec(); }