blob: e7e8efea88220c8765bc6bc406e344a82ad91ef6 [file] [log] [blame]
Andrew Lenharth701f5ac2005-11-28 00:58:09 +00001//===- RSProfiling.cpp - Various profiling using random sampling ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth701f5ac2005-11-28 00:58:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// These passes implement a random sampling based profiling. Different methods
11// of choosing when to sample are supported, as well as different types of
12// profiling. This is done as two passes. The first is a sequence of profiling
Andrew Lenharthbb227c12005-11-28 18:00:38 +000013// passes which insert profiling into the program, and remember what they
14// inserted.
15//
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000016// The second stage duplicates all instructions in a function, ignoring the
17// profiling code, then connects the two versions togeather at the entry and at
18// backedges. At each connection point a choice is made as to whether to jump
19// to the profiled code (take a sample) or execute the unprofiled code.
20//
Gordon Henriksen55cbec32007-10-26 03:03:51 +000021// It is highly recommended that after this pass one runs mem2reg and adce
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000022// (instcombine load-vn gdce dse also are good to run afterwards)
23//
24// This design is intended to make the profiling passes independent of the RS
25// framework, but any profiling pass that implements the RSProfiling interface
26// is compatible with the rs framework (and thus can be sampled)
27//
28// TODO: obviously the block and function profiling are almost identical to the
29// existing ones, so they can be unified (esp since these passes are valid
30// without the rs framework).
31// TODO: Fix choice code so that frequency is not hard coded
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/Pass.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000036#include "llvm/Module.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000037#include "llvm/Instructions.h"
38#include "llvm/Constants.h"
39#include "llvm/DerivedTypes.h"
40#include "llvm/Transforms/Scalar.h"
41#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000042#include "llvm/Support/CommandLine.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000043#include "llvm/Support/Compiler.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000044#include "llvm/Support/Debug.h"
45#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000046#include "RSProfiling.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000047#include <set>
48#include <map>
49#include <queue>
50#include <list>
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000051using namespace llvm;
52
53namespace {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000054 enum RandomMeth {
55 GBV, GBVO, HOSTCC
56 };
57
58 cl::opt<RandomMeth> RandomMethod("profile-randomness",
59 cl::desc("How to randomly choose to profile:"),
60 cl::values(
61 clEnumValN(GBV, "global", "global counter"),
Andrew Lenharthbb227c12005-11-28 18:00:38 +000062 clEnumValN(GBVO, "ra_global",
Anton Korobeynikovbed29462007-04-16 18:10:23 +000063 "register allocated global counter"),
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000064 clEnumValN(HOSTCC, "rdcc", "cycle counter"),
65 clEnumValEnd));
66
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000067 /// NullProfilerRS - The basic profiler that does nothing. It is the default
68 /// profiler and thus terminates RSProfiler chains. It is useful for
69 /// measuring framework overhead
Reid Spencer9133fe22007-02-05 23:32:05 +000070 class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000071 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000072 static char ID; // Pass identification, replacement for typeid
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000073 bool isProfiling(Value* v) {
74 return false;
75 }
76 bool runOnModule(Module &M) {
77 return false;
78 }
79 void getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.setPreservesAll();
81 }
82 };
83
84 static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
Chris Lattner7f8897f2006-08-27 22:42:52 +000085 static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
Anton Korobeynikovbed29462007-04-16 18:10:23 +000086 "Measure profiling framework overhead");
Chris Lattnera5370172006-08-28 00:42:29 +000087 static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000088
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000089 /// Chooser - Something that chooses when to make a sample of the profiled code
Reid Spencer9133fe22007-02-05 23:32:05 +000090 class VISIBILITY_HIDDEN Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000091 public:
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000092 /// ProcessChoicePoint - is called for each basic block inserted to choose
93 /// between normal and sample code
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000094 virtual void ProcessChoicePoint(BasicBlock*) = 0;
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000095 /// PrepFunction - is called once per function before other work is done.
96 /// This gives the opertunity to insert new allocas and such.
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000097 virtual void PrepFunction(Function*) = 0;
98 virtual ~Chooser() {}
99 };
100
101 //Things that implement sampling policies
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000102 //A global value that is read-mod-stored to choose when to sample.
103 //A sample is taken when the global counter hits 0
Reid Spencer9133fe22007-02-05 23:32:05 +0000104 class VISIBILITY_HIDDEN GlobalRandomCounter : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000105 GlobalVariable* Counter;
106 Value* ResetValue;
107 const Type* T;
108 public:
109 GlobalRandomCounter(Module& M, const Type* t, uint64_t resetval);
110 virtual ~GlobalRandomCounter();
111 virtual void PrepFunction(Function* F);
112 virtual void ProcessChoicePoint(BasicBlock* bb);
113 };
114
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000115 //Same is GRC, but allow register allocation of the global counter
Reid Spencer9133fe22007-02-05 23:32:05 +0000116 class VISIBILITY_HIDDEN GlobalRandomCounterOpt : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000117 GlobalVariable* Counter;
118 Value* ResetValue;
119 AllocaInst* AI;
120 const Type* T;
121 public:
122 GlobalRandomCounterOpt(Module& M, const Type* t, uint64_t resetval);
123 virtual ~GlobalRandomCounterOpt();
124 virtual void PrepFunction(Function* F);
125 virtual void ProcessChoicePoint(BasicBlock* bb);
126 };
127
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000128 //Use the cycle counter intrinsic as a source of pseudo randomness when
129 //deciding when to sample.
Reid Spencer9133fe22007-02-05 23:32:05 +0000130 class VISIBILITY_HIDDEN CycleCounter : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000131 uint64_t rm;
Chris Lattnerfebe5f12007-01-07 07:22:20 +0000132 Constant *F;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000133 public:
134 CycleCounter(Module& m, uint64_t resetmask);
135 virtual ~CycleCounter();
136 virtual void PrepFunction(Function* F);
137 virtual void ProcessChoicePoint(BasicBlock* bb);
138 };
139
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000140 /// ProfilerRS - Insert the random sampling framework
Reid Spencer9133fe22007-02-05 23:32:05 +0000141 struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000142 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +0000143 ProfilerRS() : FunctionPass((intptr_t)&ID) {}
144
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000145 std::map<Value*, Value*> TransCache;
146 std::set<BasicBlock*> ChoicePoints;
147 Chooser* c;
148
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000149 //Translate and duplicate values for the new profile free version of stuff
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000150 Value* Translate(Value* v);
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000151 //Duplicate an entire function (with out profiling)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000152 void Duplicate(Function& F, RSProfilers& LI);
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000153 //Called once for each backedge, handle the insertion of choice points and
154 //the interconection of the two versions of the code
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000155 void ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F);
156 bool runOnFunction(Function& F);
157 bool doInitialization(Module &M);
158 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
159 };
160
Chris Lattner7f8897f2006-08-27 22:42:52 +0000161 RegisterPass<ProfilerRS> X("insert-rs-profiling-framework",
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000162 "Insert random sampling instrumentation framework");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000163}
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000164
Devang Patel19974732007-05-03 01:11:54 +0000165char RSProfilers::ID = 0;
166char NullProfilerRS::ID = 0;
167char ProfilerRS::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000168
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000169//Local utilities
170static void ReplacePhiPred(BasicBlock* btarget,
171 BasicBlock* bold, BasicBlock* bnew);
172
173static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc);
174
175template<class T>
176static void recBackEdge(BasicBlock* bb, T& BackEdges,
177 std::map<BasicBlock*, int>& color,
178 std::map<BasicBlock*, int>& depth,
179 std::map<BasicBlock*, int>& finish,
180 int& time);
181
182//find the back edges and where they go to
183template<class T>
184static void getBackEdges(Function& F, T& BackEdges);
185
186
187///////////////////////////////////////
188// Methods of choosing when to profile
189///////////////////////////////////////
190
191GlobalRandomCounter::GlobalRandomCounter(Module& M, const Type* t,
192 uint64_t resetval) : T(t) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000193 ConstantInt* Init = ConstantInt::get(T, resetval);
194 ResetValue = Init;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000195 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencerb83eb642006-10-20 07:07:24 +0000196 Init, "RandomSteeringCounter", &M);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000197}
198
199GlobalRandomCounter::~GlobalRandomCounter() {}
200
201void GlobalRandomCounter::PrepFunction(Function* F) {}
202
203void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
204 BranchInst* t = cast<BranchInst>(bb->getTerminator());
205
206 //decrement counter
207 LoadInst* l = new LoadInst(Counter, "counter", t);
208
Reid Spencere4d87aa2006-12-23 06:05:41 +0000209 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
210 "countercc", t);
211
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000212 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000213 "counternew", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000214 new StoreInst(nv, Counter, t);
215 t->setCondition(s);
216
217 //reset counter
218 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greif051a9502008-04-06 20:25:17 +0000219 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
220 oldnext);
221 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000222 t->setSuccessor(0, resetblock);
223 new StoreInst(ResetValue, Counter, t2);
224 ReplacePhiPred(oldnext, bb, resetblock);
225}
226
227GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const Type* t,
228 uint64_t resetval)
229 : AI(0), T(t) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000230 ConstantInt* Init = ConstantInt::get(T, resetval);
231 ResetValue = Init;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000232 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencerb83eb642006-10-20 07:07:24 +0000233 Init, "RandomSteeringCounter", &M);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000234}
235
236GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {}
237
238void GlobalRandomCounterOpt::PrepFunction(Function* F) {
239 //make a local temporary to cache the global
240 BasicBlock& bb = F->getEntryBlock();
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000241 BasicBlock::iterator InsertPt = bb.begin();
242 AI = new AllocaInst(T, 0, "localcounter", InsertPt);
243 LoadInst* l = new LoadInst(Counter, "counterload", InsertPt);
244 new StoreInst(l, AI, InsertPt);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000245
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000246 //modify all functions and return values to restore the local variable to/from
247 //the global variable
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000248 for(Function::iterator fib = F->begin(), fie = F->end();
249 fib != fie; ++fib)
250 for(BasicBlock::iterator bib = fib->begin(), bie = fib->end();
251 bib != bie; ++bib)
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000252 if (isa<CallInst>(bib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000253 LoadInst* l = new LoadInst(AI, "counter", bib);
254 new StoreInst(l, Counter, bib);
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000255 l = new LoadInst(Counter, "counter", ++bib);
256 new StoreInst(l, AI, bib--);
257 } else if (isa<InvokeInst>(bib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000258 LoadInst* l = new LoadInst(AI, "counter", bib);
259 new StoreInst(l, Counter, bib);
260
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000261 BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest();
262 BasicBlock::iterator i = bb->begin();
263 while (isa<PHINode>(i))
264 ++i;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000265 l = new LoadInst(Counter, "counter", i);
266
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000267 bb = cast<InvokeInst>(bib)->getUnwindDest();
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000268 i = bb->begin();
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000269 while (isa<PHINode>(i)) ++i;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000270 l = new LoadInst(Counter, "counter", i);
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000271 new StoreInst(l, AI, i);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000272 } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
273 LoadInst* l = new LoadInst(AI, "counter", bib);
274 new StoreInst(l, Counter, bib);
275 }
276}
277
278void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
279 BranchInst* t = cast<BranchInst>(bb->getTerminator());
280
281 //decrement counter
282 LoadInst* l = new LoadInst(AI, "counter", t);
283
Reid Spencere4d87aa2006-12-23 06:05:41 +0000284 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
285 "countercc", t);
286
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000287 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000288 "counternew", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000289 new StoreInst(nv, AI, t);
290 t->setCondition(s);
291
292 //reset counter
293 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greif051a9502008-04-06 20:25:17 +0000294 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
295 oldnext);
296 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000297 t->setSuccessor(0, resetblock);
298 new StoreInst(ResetValue, AI, t2);
299 ReplacePhiPred(oldnext, bb, resetblock);
300}
301
302
303CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) {
Reid Spencerc5b206b2006-12-31 05:48:39 +0000304 F = m.getOrInsertFunction("llvm.readcyclecounter", Type::Int64Ty, NULL);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000305}
306
307CycleCounter::~CycleCounter() {}
308
309void CycleCounter::PrepFunction(Function* F) {}
310
311void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
312 BranchInst* t = cast<BranchInst>(bb->getTerminator());
313
Gabor Greif051a9502008-04-06 20:25:17 +0000314 CallInst* c = CallInst::Create(F, "rdcc", t);
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000315 BinaryOperator* b =
Reid Spencerc5b206b2006-12-31 05:48:39 +0000316 BinaryOperator::createAnd(c, ConstantInt::get(Type::Int64Ty, rm),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000317 "mrdcc", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000318
Reid Spencere4d87aa2006-12-23 06:05:41 +0000319 ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
Reid Spencerc5b206b2006-12-31 05:48:39 +0000320 ConstantInt::get(Type::Int64Ty, 0),
Reid Spencere4d87aa2006-12-23 06:05:41 +0000321 "mrdccc", t);
322
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000323 t->setCondition(s);
324}
325
326///////////////////////////////////////
327// Profiling:
328///////////////////////////////////////
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000329bool RSProfilers_std::isProfiling(Value* v) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000330 if (profcode.find(v) != profcode.end())
331 return true;
332 //else
333 RSProfilers& LI = getAnalysis<RSProfilers>();
334 return LI.isProfiling(v);
335}
336
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000337void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000338 GlobalValue *CounterArray) {
339 // Insert the increment after any alloca or PHI instructions...
340 BasicBlock::iterator InsertPos = BB->begin();
341 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
342 ++InsertPos;
343
344 // Create the getelementptr constant expression
345 std::vector<Constant*> Indices(2);
Reid Spencerc5b206b2006-12-31 05:48:39 +0000346 Indices[0] = Constant::getNullValue(Type::Int32Ty);
347 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000348 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray,
349 &Indices[0], 2);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000350
351 // Load, increment and store the value back.
352 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
353 profcode.insert(OldVal);
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000354 Value *NewVal = BinaryOperator::createAdd(OldVal,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000355 ConstantInt::get(Type::Int32Ty, 1),
356 "NewCounter", InsertPos);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000357 profcode.insert(NewVal);
358 profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos));
359}
360
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000361void RSProfilers_std::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000362 //grab any outstanding profiler, or get the null one
363 AU.addRequired<RSProfilers>();
364}
365
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000366///////////////////////////////////////
367// RS Framework
368///////////////////////////////////////
369
370Value* ProfilerRS::Translate(Value* v) {
371 if(TransCache[v])
372 return TransCache[v];
373
374 if (BasicBlock* bb = dyn_cast<BasicBlock>(v)) {
375 if (bb == &bb->getParent()->getEntryBlock())
376 TransCache[bb] = bb; //don't translate entry block
377 else
Gabor Greif051a9502008-04-06 20:25:17 +0000378 TransCache[bb] = BasicBlock::Create("dup_" + bb->getName(), bb->getParent(),
379 NULL);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000380 return TransCache[bb];
381 } else if (Instruction* i = dyn_cast<Instruction>(v)) {
382 //we have already translated this
383 //do not translate entry block allocas
384 if(&i->getParent()->getParent()->getEntryBlock() == i->getParent()) {
385 TransCache[i] = i;
386 return i;
387 } else {
388 //translate this
389 Instruction* i2 = i->clone();
390 if (i->hasName())
391 i2->setName("dup_" + i->getName());
392 TransCache[i] = i2;
393 //NumNewInst++;
394 for (unsigned x = 0; x < i2->getNumOperands(); ++x)
395 i2->setOperand(x, Translate(i2->getOperand(x)));
396 return i2;
397 }
398 } else if (isa<Function>(v) || isa<Constant>(v) || isa<Argument>(v)) {
399 TransCache[v] = v;
400 return v;
401 }
402 assert(0 && "Value not handled");
Jeff Cohen3523f6e2005-11-28 06:45:57 +0000403 return 0;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000404}
405
406void ProfilerRS::Duplicate(Function& F, RSProfilers& LI)
407{
408 //perform a breadth first search, building up a duplicate of the code
409 std::queue<BasicBlock*> worklist;
410 std::set<BasicBlock*> seen;
411
412 //This loop ensures proper BB order, to help performance
413 for (Function::iterator fib = F.begin(), fie = F.end(); fib != fie; ++fib)
414 worklist.push(fib);
415 while (!worklist.empty()) {
416 Translate(worklist.front());
417 worklist.pop();
418 }
419
420 //remember than reg2mem created a new entry block we don't want to duplicate
421 worklist.push(F.getEntryBlock().getTerminator()->getSuccessor(0));
422 seen.insert(&F.getEntryBlock());
423
424 while (!worklist.empty()) {
425 BasicBlock* bb = worklist.front();
426 worklist.pop();
427 if(seen.find(bb) == seen.end()) {
428 BasicBlock* bbtarget = cast<BasicBlock>(Translate(bb));
429 BasicBlock::InstListType& instlist = bbtarget->getInstList();
430 for (BasicBlock::iterator iib = bb->begin(), iie = bb->end();
431 iib != iie; ++iib) {
432 //NumOldInst++;
433 if (!LI.isProfiling(&*iib)) {
434 Instruction* i = cast<Instruction>(Translate(iib));
435 instlist.insert(bbtarget->end(), i);
436 }
437 }
438 //updated search state;
439 seen.insert(bb);
440 TerminatorInst* ti = bb->getTerminator();
441 for (unsigned x = 0; x < ti->getNumSuccessors(); ++x) {
442 BasicBlock* bbs = ti->getSuccessor(x);
443 if (seen.find(bbs) == seen.end()) {
444 worklist.push(bbs);
445 }
446 }
447 }
448 }
449}
450
451void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F) {
452 //given a backedge from B -> A, and translations A' and B',
453 //a: insert C and C'
454 //b: add branches in C to A and A' and in C' to A and A'
455 //c: mod terminators@B, replace A with C
456 //d: mod terminators@B', replace A' with C'
457 //e: mod phis@A for pred B to be pred C
458 // if multiple entries, simplify to one
459 //f: mod phis@A' for pred B' to be pred C'
460 // if multiple entries, simplify to one
461 //g: for all phis@A with pred C using x
462 // add in edge from C' using x'
463 // add in edge from C using x in A'
464
465 //a:
Chris Lattnere24c92a2007-04-17 17:54:12 +0000466 Function::iterator BBN = src; ++BBN;
Gabor Greif051a9502008-04-06 20:25:17 +0000467 BasicBlock* bbC = BasicBlock::Create("choice", &F, BBN);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000468 //ChoicePoints.insert(bbC);
Chris Lattnere24c92a2007-04-17 17:54:12 +0000469 BBN = cast<BasicBlock>(Translate(src));
Gabor Greif051a9502008-04-06 20:25:17 +0000470 BasicBlock* bbCp = BasicBlock::Create("choice", &F, ++BBN);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000471 ChoicePoints.insert(bbCp);
472
473 //b:
Gabor Greif051a9502008-04-06 20:25:17 +0000474 BranchInst::Create(cast<BasicBlock>(Translate(dst)), bbC);
475 BranchInst::Create(dst, cast<BasicBlock>(Translate(dst)),
476 ConstantInt::get(Type::Int1Ty, true), bbCp);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000477 //c:
478 {
479 TerminatorInst* iB = src->getTerminator();
480 for (unsigned x = 0; x < iB->getNumSuccessors(); ++x)
481 if (iB->getSuccessor(x) == dst)
482 iB->setSuccessor(x, bbC);
483 }
484 //d:
485 {
486 TerminatorInst* iBp = cast<TerminatorInst>(Translate(src->getTerminator()));
487 for (unsigned x = 0; x < iBp->getNumSuccessors(); ++x)
488 if (iBp->getSuccessor(x) == cast<BasicBlock>(Translate(dst)))
489 iBp->setSuccessor(x, bbCp);
490 }
491 //e:
492 ReplacePhiPred(dst, src, bbC);
493 //src could be a switch, in which case we are replacing several edges with one
494 //thus collapse those edges int the Phi
495 CollapsePhi(dst, bbC);
496 //f:
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000497 ReplacePhiPred(cast<BasicBlock>(Translate(dst)),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000498 cast<BasicBlock>(Translate(src)),bbCp);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000499 CollapsePhi(cast<BasicBlock>(Translate(dst)), bbCp);
500 //g:
501 for(BasicBlock::iterator ib = dst->begin(), ie = dst->end(); ib != ie;
502 ++ib)
503 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
504 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
505 if(bbC == phi->getIncomingBlock(x)) {
506 phi->addIncoming(Translate(phi->getIncomingValue(x)), bbCp);
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000507 cast<PHINode>(Translate(phi))->addIncoming(phi->getIncomingValue(x),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000508 bbC);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000509 }
510 phi->removeIncomingValue(bbC);
511 }
512}
513
514bool ProfilerRS::runOnFunction(Function& F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000515 if (!F.isDeclaration()) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000516 std::set<std::pair<BasicBlock*, BasicBlock*> > BackEdges;
517 RSProfilers& LI = getAnalysis<RSProfilers>();
518
519 getBackEdges(F, BackEdges);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000520 Duplicate(F, LI);
521 //assume that stuff worked. now connect the duplicated basic blocks
522 //with the originals in such a way as to preserve ssa. yuk!
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000523 for (std::set<std::pair<BasicBlock*, BasicBlock*> >::iterator
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000524 ib = BackEdges.begin(), ie = BackEdges.end(); ib != ie; ++ib)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000525 ProcessBackEdge(ib->first, ib->second, F);
526
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000527 //oh, and add the edge from the reg2mem created entry node to the
528 //duplicated second node
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000529 TerminatorInst* T = F.getEntryBlock().getTerminator();
Gabor Greif051a9502008-04-06 20:25:17 +0000530 ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0),
531 cast<BasicBlock>(
532 Translate(T->getSuccessor(0))),
533 ConstantInt::get(Type::Int1Ty,
534 true)));
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000535
536 //do whatever is needed now that the function is duplicated
537 c->PrepFunction(&F);
538
539 //add entry node to choice points
540 ChoicePoints.insert(&F.getEntryBlock());
541
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000542 for (std::set<BasicBlock*>::iterator
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000543 ii = ChoicePoints.begin(), ie = ChoicePoints.end(); ii != ie; ++ii)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000544 c->ProcessChoicePoint(*ii);
545
546 ChoicePoints.clear();
547 TransCache.clear();
548
549 return true;
550 }
551 return false;
552}
553
554bool ProfilerRS::doInitialization(Module &M) {
555 switch (RandomMethod) {
556 case GBV:
Reid Spencerc5b206b2006-12-31 05:48:39 +0000557 c = new GlobalRandomCounter(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000558 break;
559 case GBVO:
Reid Spencerc5b206b2006-12-31 05:48:39 +0000560 c = new GlobalRandomCounterOpt(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000561 break;
562 case HOSTCC:
563 c = new CycleCounter(M, (1 << 14) - 1);
564 break;
565 };
566 return true;
567}
568
569void ProfilerRS::getAnalysisUsage(AnalysisUsage &AU) const {
570 AU.addRequired<RSProfilers>();
571 AU.addRequiredID(DemoteRegisterToMemoryID);
572}
573
574///////////////////////////////////////
575// Utilities:
576///////////////////////////////////////
577static void ReplacePhiPred(BasicBlock* btarget,
578 BasicBlock* bold, BasicBlock* bnew) {
579 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
580 ib != ie; ++ib)
581 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
582 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
583 if(bold == phi->getIncomingBlock(x))
584 phi->setIncomingBlock(x, bnew);
585 }
586}
587
588static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc) {
589 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
590 ib != ie; ++ib)
591 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000592 std::map<BasicBlock*, Value*> counter;
593 for(unsigned i = 0; i < phi->getNumIncomingValues(); ) {
594 if (counter[phi->getIncomingBlock(i)]) {
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000595 assert(phi->getIncomingValue(i) == counter[phi->getIncomingBlock(i)]);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000596 phi->removeIncomingValue(i, false);
597 } else {
598 counter[phi->getIncomingBlock(i)] = phi->getIncomingValue(i);
599 ++i;
600 }
601 }
602 }
603}
604
605template<class T>
606static void recBackEdge(BasicBlock* bb, T& BackEdges,
607 std::map<BasicBlock*, int>& color,
608 std::map<BasicBlock*, int>& depth,
609 std::map<BasicBlock*, int>& finish,
610 int& time)
611{
612 color[bb] = 1;
613 ++time;
614 depth[bb] = time;
615 TerminatorInst* t= bb->getTerminator();
616 for(unsigned i = 0; i < t->getNumSuccessors(); ++i) {
617 BasicBlock* bbnew = t->getSuccessor(i);
618 if (color[bbnew] == 0)
619 recBackEdge(bbnew, BackEdges, color, depth, finish, time);
620 else if (color[bbnew] == 1) {
621 BackEdges.insert(std::make_pair(bb, bbnew));
622 //NumBackEdges++;
623 }
624 }
625 color[bb] = 2;
626 ++time;
627 finish[bb] = time;
628}
629
630
631
632//find the back edges and where they go to
633template<class T>
634static void getBackEdges(Function& F, T& BackEdges) {
635 std::map<BasicBlock*, int> color;
636 std::map<BasicBlock*, int> depth;
637 std::map<BasicBlock*, int> finish;
638 int time = 0;
639 recBackEdge(&F.getEntryBlock(), BackEdges, color, depth, finish, time);
Bill Wendling62c804a2006-11-26 09:17:06 +0000640 DOUT << F.getName() << " " << BackEdges.size() << "\n";
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000641}
642
643
644//Creation functions
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000645ModulePass* llvm::createNullProfilerRSPass() {
646 return new NullProfilerRS();
647}
648
649FunctionPass* llvm::createRSProfilingPass() {
650 return new ProfilerRS();
651}