blob: fc464c00ec2b37fcf175e51b1d5faf843dd3286c [file] [log] [blame]
Andrew Lenharth93e59f62005-11-28 00:58:09 +00001//===- RSProfiling.cpp - Various profiling using random sampling ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Lenharth517caef2005-11-28 18:00:38 +000013// passes which insert profiling into the program, and remember what they
14// inserted.
15//
Andrew Lenharth93e59f62005-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//
21// It is highly recommeneded that after this pass one runs mem2reg and adce
22// (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 Lenharth93e59f62005-11-28 00:58:09 +000036#include "llvm/Module.h"
Andrew Lenharth93e59f62005-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 Lenharth93e59f62005-11-28 00:58:09 +000042#include "llvm/Support/CommandLine.h"
Reid Spencer557ab152007-02-05 23:32:05 +000043#include "llvm/Support/Compiler.h"
Andrew Lenharth93e59f62005-11-28 00:58:09 +000044#include "llvm/Support/Debug.h"
45#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharth93e59f62005-11-28 00:58:09 +000046#include "RSProfiling.h"
Andrew Lenharth93e59f62005-11-28 00:58:09 +000047#include <set>
48#include <map>
49#include <queue>
50#include <list>
Andrew Lenharth93e59f62005-11-28 00:58:09 +000051using namespace llvm;
52
53namespace {
Andrew Lenharth93e59f62005-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 Lenharth517caef2005-11-28 18:00:38 +000062 clEnumValN(GBVO, "ra_global",
63 "register allocated global counter"),
Andrew Lenharth93e59f62005-11-28 00:58:09 +000064 clEnumValN(HOSTCC, "rdcc", "cycle counter"),
65 clEnumValEnd));
66
Andrew Lenharthd2511922005-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 Spencer557ab152007-02-05 23:32:05 +000070 class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers {
Andrew Lenharth93e59f62005-11-28 00:58:09 +000071 public:
72 bool isProfiling(Value* v) {
73 return false;
74 }
75 bool runOnModule(Module &M) {
76 return false;
77 }
78 void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
80 }
81 };
82
83 static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
Chris Lattnerc2d3d312006-08-27 22:42:52 +000084 static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
Andrew Lenharth517caef2005-11-28 18:00:38 +000085 "Measure profiling framework overhead");
Chris Lattner97c9f202006-08-28 00:42:29 +000086 static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
Andrew Lenharth93e59f62005-11-28 00:58:09 +000087
Andrew Lenharthd2511922005-11-28 18:10:59 +000088 /// Chooser - Something that chooses when to make a sample of the profiled code
Reid Spencer557ab152007-02-05 23:32:05 +000089 class VISIBILITY_HIDDEN Chooser {
Andrew Lenharth93e59f62005-11-28 00:58:09 +000090 public:
Andrew Lenharthd2511922005-11-28 18:10:59 +000091 /// ProcessChoicePoint - is called for each basic block inserted to choose
92 /// between normal and sample code
Andrew Lenharth93e59f62005-11-28 00:58:09 +000093 virtual void ProcessChoicePoint(BasicBlock*) = 0;
Andrew Lenharthd2511922005-11-28 18:10:59 +000094 /// PrepFunction - is called once per function before other work is done.
95 /// This gives the opertunity to insert new allocas and such.
Andrew Lenharth93e59f62005-11-28 00:58:09 +000096 virtual void PrepFunction(Function*) = 0;
97 virtual ~Chooser() {}
98 };
99
100 //Things that implement sampling policies
Andrew Lenharthd2511922005-11-28 18:10:59 +0000101 //A global value that is read-mod-stored to choose when to sample.
102 //A sample is taken when the global counter hits 0
Reid Spencer557ab152007-02-05 23:32:05 +0000103 class VISIBILITY_HIDDEN GlobalRandomCounter : public Chooser {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000104 GlobalVariable* Counter;
105 Value* ResetValue;
106 const Type* T;
107 public:
108 GlobalRandomCounter(Module& M, const Type* t, uint64_t resetval);
109 virtual ~GlobalRandomCounter();
110 virtual void PrepFunction(Function* F);
111 virtual void ProcessChoicePoint(BasicBlock* bb);
112 };
113
Andrew Lenharthd2511922005-11-28 18:10:59 +0000114 //Same is GRC, but allow register allocation of the global counter
Reid Spencer557ab152007-02-05 23:32:05 +0000115 class VISIBILITY_HIDDEN GlobalRandomCounterOpt : public Chooser {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000116 GlobalVariable* Counter;
117 Value* ResetValue;
118 AllocaInst* AI;
119 const Type* T;
120 public:
121 GlobalRandomCounterOpt(Module& M, const Type* t, uint64_t resetval);
122 virtual ~GlobalRandomCounterOpt();
123 virtual void PrepFunction(Function* F);
124 virtual void ProcessChoicePoint(BasicBlock* bb);
125 };
126
Andrew Lenharthd2511922005-11-28 18:10:59 +0000127 //Use the cycle counter intrinsic as a source of pseudo randomness when
128 //deciding when to sample.
Reid Spencer557ab152007-02-05 23:32:05 +0000129 class VISIBILITY_HIDDEN CycleCounter : public Chooser {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000130 uint64_t rm;
Chris Lattnercc4715e2007-01-07 07:22:20 +0000131 Constant *F;
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000132 public:
133 CycleCounter(Module& m, uint64_t resetmask);
134 virtual ~CycleCounter();
135 virtual void PrepFunction(Function* F);
136 virtual void ProcessChoicePoint(BasicBlock* bb);
137 };
138
Andrew Lenharthd2511922005-11-28 18:10:59 +0000139 /// ProfilerRS - Insert the random sampling framework
Reid Spencer557ab152007-02-05 23:32:05 +0000140 struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000141 std::map<Value*, Value*> TransCache;
142 std::set<BasicBlock*> ChoicePoints;
143 Chooser* c;
144
Andrew Lenharthd2511922005-11-28 18:10:59 +0000145 //Translate and duplicate values for the new profile free version of stuff
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000146 Value* Translate(Value* v);
Andrew Lenharthd2511922005-11-28 18:10:59 +0000147 //Duplicate an entire function (with out profiling)
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000148 void Duplicate(Function& F, RSProfilers& LI);
Andrew Lenharthd2511922005-11-28 18:10:59 +0000149 //Called once for each backedge, handle the insertion of choice points and
150 //the interconection of the two versions of the code
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000151 void ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F);
152 bool runOnFunction(Function& F);
153 bool doInitialization(Module &M);
154 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
155 };
156
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000157 RegisterPass<ProfilerRS> X("insert-rs-profiling-framework",
158 "Insert random sampling instrumentation framework");
Chris Lattneraa2372562006-05-24 17:04:05 +0000159}
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000160
161//Local utilities
162static void ReplacePhiPred(BasicBlock* btarget,
163 BasicBlock* bold, BasicBlock* bnew);
164
165static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc);
166
167template<class T>
168static void recBackEdge(BasicBlock* bb, T& BackEdges,
169 std::map<BasicBlock*, int>& color,
170 std::map<BasicBlock*, int>& depth,
171 std::map<BasicBlock*, int>& finish,
172 int& time);
173
174//find the back edges and where they go to
175template<class T>
176static void getBackEdges(Function& F, T& BackEdges);
177
178
179///////////////////////////////////////
180// Methods of choosing when to profile
181///////////////////////////////////////
182
183GlobalRandomCounter::GlobalRandomCounter(Module& M, const Type* t,
184 uint64_t resetval) : T(t) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000185 ConstantInt* Init = ConstantInt::get(T, resetval);
186 ResetValue = Init;
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000187 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000188 Init, "RandomSteeringCounter", &M);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000189}
190
191GlobalRandomCounter::~GlobalRandomCounter() {}
192
193void GlobalRandomCounter::PrepFunction(Function* F) {}
194
195void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
196 BranchInst* t = cast<BranchInst>(bb->getTerminator());
197
198 //decrement counter
199 LoadInst* l = new LoadInst(Counter, "counter", t);
200
Reid Spencer266e42b2006-12-23 06:05:41 +0000201 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
202 "countercc", t);
203
Andrew Lenharth517caef2005-11-28 18:00:38 +0000204 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000205 "counternew", t);
206 new StoreInst(nv, Counter, t);
207 t->setCondition(s);
208
209 //reset counter
210 BasicBlock* oldnext = t->getSuccessor(0);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000211 BasicBlock* resetblock = new BasicBlock("reset", oldnext->getParent(),
212 oldnext);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000213 TerminatorInst* t2 = new BranchInst(oldnext, resetblock);
214 t->setSuccessor(0, resetblock);
215 new StoreInst(ResetValue, Counter, t2);
216 ReplacePhiPred(oldnext, bb, resetblock);
217}
218
219GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const Type* t,
220 uint64_t resetval)
221 : AI(0), T(t) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000222 ConstantInt* Init = ConstantInt::get(T, resetval);
223 ResetValue = Init;
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000224 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000225 Init, "RandomSteeringCounter", &M);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000226}
227
228GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {}
229
230void GlobalRandomCounterOpt::PrepFunction(Function* F) {
231 //make a local temporary to cache the global
232 BasicBlock& bb = F->getEntryBlock();
233 AI = new AllocaInst(T, 0, "localcounter", bb.begin());
234 LoadInst* l = new LoadInst(Counter, "counterload", AI->getNext());
235 new StoreInst(l, AI, l->getNext());
236
Andrew Lenharthd2511922005-11-28 18:10:59 +0000237 //modify all functions and return values to restore the local variable to/from
238 //the global variable
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000239 for(Function::iterator fib = F->begin(), fie = F->end();
240 fib != fie; ++fib)
241 for(BasicBlock::iterator bib = fib->begin(), bie = fib->end();
242 bib != bie; ++bib)
243 if (isa<CallInst>(&*bib)) {
244 LoadInst* l = new LoadInst(AI, "counter", bib);
245 new StoreInst(l, Counter, bib);
246 l = new LoadInst(Counter, "counter", bib->getNext());
247 new StoreInst(l, AI, l->getNext());
248 } else if (isa<InvokeInst>(&*bib)) {
249 LoadInst* l = new LoadInst(AI, "counter", bib);
250 new StoreInst(l, Counter, bib);
251
252 BasicBlock* bb = cast<InvokeInst>(&*bib)->getNormalDest();
253 Instruction* i = bb->begin();
254 while (isa<PHINode>(i)) i = i->getNext();
255 l = new LoadInst(Counter, "counter", i);
256
257 bb = cast<InvokeInst>(&*bib)->getUnwindDest();
258 i = bb->begin();
259 while (isa<PHINode>(i)) i = i->getNext();
260 l = new LoadInst(Counter, "counter", i);
261 new StoreInst(l, AI, l->getNext());
262 } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
263 LoadInst* l = new LoadInst(AI, "counter", bib);
264 new StoreInst(l, Counter, bib);
265 }
266}
267
268void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
269 BranchInst* t = cast<BranchInst>(bb->getTerminator());
270
271 //decrement counter
272 LoadInst* l = new LoadInst(AI, "counter", t);
273
Reid Spencer266e42b2006-12-23 06:05:41 +0000274 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
275 "countercc", t);
276
Andrew Lenharth517caef2005-11-28 18:00:38 +0000277 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000278 "counternew", t);
279 new StoreInst(nv, AI, t);
280 t->setCondition(s);
281
282 //reset counter
283 BasicBlock* oldnext = t->getSuccessor(0);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000284 BasicBlock* resetblock = new BasicBlock("reset", oldnext->getParent(),
285 oldnext);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000286 TerminatorInst* t2 = new BranchInst(oldnext, resetblock);
287 t->setSuccessor(0, resetblock);
288 new StoreInst(ResetValue, AI, t2);
289 ReplacePhiPred(oldnext, bb, resetblock);
290}
291
292
293CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) {
Reid Spencerc635f472006-12-31 05:48:39 +0000294 F = m.getOrInsertFunction("llvm.readcyclecounter", Type::Int64Ty, NULL);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000295}
296
297CycleCounter::~CycleCounter() {}
298
299void CycleCounter::PrepFunction(Function* F) {}
300
301void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
302 BranchInst* t = cast<BranchInst>(bb->getTerminator());
303
304 CallInst* c = new CallInst(F, "rdcc", t);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000305 BinaryOperator* b =
Reid Spencerc635f472006-12-31 05:48:39 +0000306 BinaryOperator::createAnd(c, ConstantInt::get(Type::Int64Ty, rm),
Andrew Lenharth517caef2005-11-28 18:00:38 +0000307 "mrdcc", t);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000308
Reid Spencer266e42b2006-12-23 06:05:41 +0000309 ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
Reid Spencerc635f472006-12-31 05:48:39 +0000310 ConstantInt::get(Type::Int64Ty, 0),
Reid Spencer266e42b2006-12-23 06:05:41 +0000311 "mrdccc", t);
312
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000313 t->setCondition(s);
314}
315
316///////////////////////////////////////
317// Profiling:
318///////////////////////////////////////
Andrew Lenharth517caef2005-11-28 18:00:38 +0000319bool RSProfilers_std::isProfiling(Value* v) {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000320 if (profcode.find(v) != profcode.end())
321 return true;
322 //else
323 RSProfilers& LI = getAnalysis<RSProfilers>();
324 return LI.isProfiling(v);
325}
326
Andrew Lenharth517caef2005-11-28 18:00:38 +0000327void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000328 GlobalValue *CounterArray) {
329 // Insert the increment after any alloca or PHI instructions...
330 BasicBlock::iterator InsertPos = BB->begin();
331 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
332 ++InsertPos;
333
334 // Create the getelementptr constant expression
335 std::vector<Constant*> Indices(2);
Reid Spencerc635f472006-12-31 05:48:39 +0000336 Indices[0] = Constant::getNullValue(Type::Int32Ty);
337 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000338 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
339
340 // Load, increment and store the value back.
341 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
342 profcode.insert(OldVal);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000343 Value *NewVal = BinaryOperator::createAdd(OldVal,
Reid Spencerc635f472006-12-31 05:48:39 +0000344 ConstantInt::get(Type::Int32Ty, 1),
Andrew Lenharth517caef2005-11-28 18:00:38 +0000345 "NewCounter", InsertPos);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000346 profcode.insert(NewVal);
347 profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos));
348}
349
Andrew Lenharth517caef2005-11-28 18:00:38 +0000350void RSProfilers_std::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000351 //grab any outstanding profiler, or get the null one
352 AU.addRequired<RSProfilers>();
353}
354
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000355///////////////////////////////////////
356// RS Framework
357///////////////////////////////////////
358
359Value* ProfilerRS::Translate(Value* v) {
360 if(TransCache[v])
361 return TransCache[v];
362
363 if (BasicBlock* bb = dyn_cast<BasicBlock>(v)) {
364 if (bb == &bb->getParent()->getEntryBlock())
365 TransCache[bb] = bb; //don't translate entry block
366 else
Andrew Lenharth517caef2005-11-28 18:00:38 +0000367 TransCache[bb] = new BasicBlock("dup_" + bb->getName(), bb->getParent(),
368 NULL);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000369 return TransCache[bb];
370 } else if (Instruction* i = dyn_cast<Instruction>(v)) {
371 //we have already translated this
372 //do not translate entry block allocas
373 if(&i->getParent()->getParent()->getEntryBlock() == i->getParent()) {
374 TransCache[i] = i;
375 return i;
376 } else {
377 //translate this
378 Instruction* i2 = i->clone();
379 if (i->hasName())
380 i2->setName("dup_" + i->getName());
381 TransCache[i] = i2;
382 //NumNewInst++;
383 for (unsigned x = 0; x < i2->getNumOperands(); ++x)
384 i2->setOperand(x, Translate(i2->getOperand(x)));
385 return i2;
386 }
387 } else if (isa<Function>(v) || isa<Constant>(v) || isa<Argument>(v)) {
388 TransCache[v] = v;
389 return v;
390 }
391 assert(0 && "Value not handled");
Jeff Cohen7ff44ec2005-11-28 06:45:57 +0000392 return 0;
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000393}
394
395void ProfilerRS::Duplicate(Function& F, RSProfilers& LI)
396{
397 //perform a breadth first search, building up a duplicate of the code
398 std::queue<BasicBlock*> worklist;
399 std::set<BasicBlock*> seen;
400
401 //This loop ensures proper BB order, to help performance
402 for (Function::iterator fib = F.begin(), fie = F.end(); fib != fie; ++fib)
403 worklist.push(fib);
404 while (!worklist.empty()) {
405 Translate(worklist.front());
406 worklist.pop();
407 }
408
409 //remember than reg2mem created a new entry block we don't want to duplicate
410 worklist.push(F.getEntryBlock().getTerminator()->getSuccessor(0));
411 seen.insert(&F.getEntryBlock());
412
413 while (!worklist.empty()) {
414 BasicBlock* bb = worklist.front();
415 worklist.pop();
416 if(seen.find(bb) == seen.end()) {
417 BasicBlock* bbtarget = cast<BasicBlock>(Translate(bb));
418 BasicBlock::InstListType& instlist = bbtarget->getInstList();
419 for (BasicBlock::iterator iib = bb->begin(), iie = bb->end();
420 iib != iie; ++iib) {
421 //NumOldInst++;
422 if (!LI.isProfiling(&*iib)) {
423 Instruction* i = cast<Instruction>(Translate(iib));
424 instlist.insert(bbtarget->end(), i);
425 }
426 }
427 //updated search state;
428 seen.insert(bb);
429 TerminatorInst* ti = bb->getTerminator();
430 for (unsigned x = 0; x < ti->getNumSuccessors(); ++x) {
431 BasicBlock* bbs = ti->getSuccessor(x);
432 if (seen.find(bbs) == seen.end()) {
433 worklist.push(bbs);
434 }
435 }
436 }
437 }
438}
439
440void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F) {
441 //given a backedge from B -> A, and translations A' and B',
442 //a: insert C and C'
443 //b: add branches in C to A and A' and in C' to A and A'
444 //c: mod terminators@B, replace A with C
445 //d: mod terminators@B', replace A' with C'
446 //e: mod phis@A for pred B to be pred C
447 // if multiple entries, simplify to one
448 //f: mod phis@A' for pred B' to be pred C'
449 // if multiple entries, simplify to one
450 //g: for all phis@A with pred C using x
451 // add in edge from C' using x'
452 // add in edge from C using x in A'
453
454 //a:
455 BasicBlock* bbC = new BasicBlock("choice", &F, src->getNext() );
456 //ChoicePoints.insert(bbC);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000457 BasicBlock* bbCp =
458 new BasicBlock("choice", &F, cast<BasicBlock>(Translate(src))->getNext() );
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000459 ChoicePoints.insert(bbCp);
460
461 //b:
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000462 new BranchInst(cast<BasicBlock>(Translate(dst)), bbC);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000463 new BranchInst(dst, cast<BasicBlock>(Translate(dst)),
Reid Spencercddc9df2007-01-12 04:24:46 +0000464 ConstantInt::get(Type::Int1Ty, true), bbCp);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000465 //c:
466 {
467 TerminatorInst* iB = src->getTerminator();
468 for (unsigned x = 0; x < iB->getNumSuccessors(); ++x)
469 if (iB->getSuccessor(x) == dst)
470 iB->setSuccessor(x, bbC);
471 }
472 //d:
473 {
474 TerminatorInst* iBp = cast<TerminatorInst>(Translate(src->getTerminator()));
475 for (unsigned x = 0; x < iBp->getNumSuccessors(); ++x)
476 if (iBp->getSuccessor(x) == cast<BasicBlock>(Translate(dst)))
477 iBp->setSuccessor(x, bbCp);
478 }
479 //e:
480 ReplacePhiPred(dst, src, bbC);
481 //src could be a switch, in which case we are replacing several edges with one
482 //thus collapse those edges int the Phi
483 CollapsePhi(dst, bbC);
484 //f:
Andrew Lenharth517caef2005-11-28 18:00:38 +0000485 ReplacePhiPred(cast<BasicBlock>(Translate(dst)),
486 cast<BasicBlock>(Translate(src)),bbCp);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000487 CollapsePhi(cast<BasicBlock>(Translate(dst)), bbCp);
488 //g:
489 for(BasicBlock::iterator ib = dst->begin(), ie = dst->end(); ib != ie;
490 ++ib)
491 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
492 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
493 if(bbC == phi->getIncomingBlock(x)) {
494 phi->addIncoming(Translate(phi->getIncomingValue(x)), bbCp);
Andrew Lenharth517caef2005-11-28 18:00:38 +0000495 cast<PHINode>(Translate(phi))->addIncoming(phi->getIncomingValue(x),
496 bbC);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000497 }
498 phi->removeIncomingValue(bbC);
499 }
500}
501
502bool ProfilerRS::runOnFunction(Function& F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +0000503 if (!F.isDeclaration()) {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000504 std::set<std::pair<BasicBlock*, BasicBlock*> > BackEdges;
505 RSProfilers& LI = getAnalysis<RSProfilers>();
506
507 getBackEdges(F, BackEdges);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000508 Duplicate(F, LI);
509 //assume that stuff worked. now connect the duplicated basic blocks
510 //with the originals in such a way as to preserve ssa. yuk!
Andrew Lenharth517caef2005-11-28 18:00:38 +0000511 for (std::set<std::pair<BasicBlock*, BasicBlock*> >::iterator
512 ib = BackEdges.begin(), ie = BackEdges.end(); ib != ie; ++ib)
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000513 ProcessBackEdge(ib->first, ib->second, F);
514
Andrew Lenharth517caef2005-11-28 18:00:38 +0000515 //oh, and add the edge from the reg2mem created entry node to the
516 //duplicated second node
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000517 TerminatorInst* T = F.getEntryBlock().getTerminator();
518 ReplaceInstWithInst(T, new BranchInst(T->getSuccessor(0),
Andrew Lenharth517caef2005-11-28 18:00:38 +0000519 cast<BasicBlock>(Translate(T->getSuccessor(0))),
Reid Spencercddc9df2007-01-12 04:24:46 +0000520 ConstantInt::get(Type::Int1Ty, true)));
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000521
522 //do whatever is needed now that the function is duplicated
523 c->PrepFunction(&F);
524
525 //add entry node to choice points
526 ChoicePoints.insert(&F.getEntryBlock());
527
Andrew Lenharth517caef2005-11-28 18:00:38 +0000528 for (std::set<BasicBlock*>::iterator
529 ii = ChoicePoints.begin(), ie = ChoicePoints.end(); ii != ie; ++ii)
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000530 c->ProcessChoicePoint(*ii);
531
532 ChoicePoints.clear();
533 TransCache.clear();
534
535 return true;
536 }
537 return false;
538}
539
540bool ProfilerRS::doInitialization(Module &M) {
541 switch (RandomMethod) {
542 case GBV:
Reid Spencerc635f472006-12-31 05:48:39 +0000543 c = new GlobalRandomCounter(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000544 break;
545 case GBVO:
Reid Spencerc635f472006-12-31 05:48:39 +0000546 c = new GlobalRandomCounterOpt(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000547 break;
548 case HOSTCC:
549 c = new CycleCounter(M, (1 << 14) - 1);
550 break;
551 };
552 return true;
553}
554
555void ProfilerRS::getAnalysisUsage(AnalysisUsage &AU) const {
556 AU.addRequired<RSProfilers>();
557 AU.addRequiredID(DemoteRegisterToMemoryID);
558}
559
560///////////////////////////////////////
561// Utilities:
562///////////////////////////////////////
563static void ReplacePhiPred(BasicBlock* btarget,
564 BasicBlock* bold, BasicBlock* bnew) {
565 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
566 ib != ie; ++ib)
567 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
568 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
569 if(bold == phi->getIncomingBlock(x))
570 phi->setIncomingBlock(x, bnew);
571 }
572}
573
574static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc) {
575 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
576 ib != ie; ++ib)
577 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000578 std::map<BasicBlock*, Value*> counter;
579 for(unsigned i = 0; i < phi->getNumIncomingValues(); ) {
580 if (counter[phi->getIncomingBlock(i)]) {
Andrew Lenharth517caef2005-11-28 18:00:38 +0000581 assert(phi->getIncomingValue(i) == counter[phi->getIncomingBlock(i)]);
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000582 phi->removeIncomingValue(i, false);
583 } else {
584 counter[phi->getIncomingBlock(i)] = phi->getIncomingValue(i);
585 ++i;
586 }
587 }
588 }
589}
590
591template<class T>
592static void recBackEdge(BasicBlock* bb, T& BackEdges,
593 std::map<BasicBlock*, int>& color,
594 std::map<BasicBlock*, int>& depth,
595 std::map<BasicBlock*, int>& finish,
596 int& time)
597{
598 color[bb] = 1;
599 ++time;
600 depth[bb] = time;
601 TerminatorInst* t= bb->getTerminator();
602 for(unsigned i = 0; i < t->getNumSuccessors(); ++i) {
603 BasicBlock* bbnew = t->getSuccessor(i);
604 if (color[bbnew] == 0)
605 recBackEdge(bbnew, BackEdges, color, depth, finish, time);
606 else if (color[bbnew] == 1) {
607 BackEdges.insert(std::make_pair(bb, bbnew));
608 //NumBackEdges++;
609 }
610 }
611 color[bb] = 2;
612 ++time;
613 finish[bb] = time;
614}
615
616
617
618//find the back edges and where they go to
619template<class T>
620static void getBackEdges(Function& F, T& BackEdges) {
621 std::map<BasicBlock*, int> color;
622 std::map<BasicBlock*, int> depth;
623 std::map<BasicBlock*, int> finish;
624 int time = 0;
625 recBackEdge(&F.getEntryBlock(), BackEdges, color, depth, finish, time);
Bill Wendlinga7459ca2006-11-26 09:17:06 +0000626 DOUT << F.getName() << " " << BackEdges.size() << "\n";
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000627}
628
629
630//Creation functions
Andrew Lenharth93e59f62005-11-28 00:58:09 +0000631ModulePass* llvm::createNullProfilerRSPass() {
632 return new NullProfilerRS();
633}
634
635FunctionPass* llvm::createRSProfilingPass() {
636 return new ProfilerRS();
637}