blob: 5bd62c8197dd0eda0b0378101d6824ae82499e63 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- RSProfiling.cpp - Various profiling using random sampling ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
13// passes which insert profiling into the program, and remember what they
14// inserted.
15//
16// 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 Henriksen9adadb42007-10-26 03:03:51 +000021// It is highly recommended that after this pass one runs mem2reg and adce
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
36#include "llvm/Module.h"
37#include "llvm/Instructions.h"
38#include "llvm/Constants.h"
39#include "llvm/DerivedTypes.h"
Duncan Sands3c812692008-04-07 13:45:04 +000040#include "llvm/Intrinsics.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Transforms/Scalar.h"
42#include "llvm/Transforms/Utils/BasicBlockUtils.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Compiler.h"
45#include "llvm/Support/Debug.h"
46#include "llvm/Transforms/Instrumentation.h"
47#include "RSProfiling.h"
48#include <set>
49#include <map>
50#include <queue>
51#include <list>
52using namespace llvm;
53
54namespace {
55 enum RandomMeth {
56 GBV, GBVO, HOSTCC
57 };
Dan Gohman089efff2008-05-13 00:00:25 +000058}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
Dan Gohman089efff2008-05-13 00:00:25 +000060static cl::opt<RandomMeth> RandomMethod("profile-randomness",
61 cl::desc("How to randomly choose to profile:"),
62 cl::values(
63 clEnumValN(GBV, "global", "global counter"),
64 clEnumValN(GBVO, "ra_global",
65 "register allocated global counter"),
66 clEnumValN(HOSTCC, "rdcc", "cycle counter"),
67 clEnumValEnd));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
Dan Gohman089efff2008-05-13 00:00:25 +000069namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 /// NullProfilerRS - The basic profiler that does nothing. It is the default
71 /// profiler and thus terminates RSProfiler chains. It is useful for
72 /// measuring framework overhead
73 class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers {
74 public:
75 static char ID; // Pass identification, replacement for typeid
76 bool isProfiling(Value* v) {
77 return false;
78 }
79 bool runOnModule(Module &M) {
80 return false;
81 }
82 void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.setPreservesAll();
84 }
85 };
Dan Gohman089efff2008-05-13 00:00:25 +000086}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Dan Gohman089efff2008-05-13 00:00:25 +000088static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
89static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
90 "Measure profiling framework overhead");
91static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Dan Gohman089efff2008-05-13 00:00:25 +000093namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 /// Chooser - Something that chooses when to make a sample of the profiled code
95 class VISIBILITY_HIDDEN Chooser {
96 public:
97 /// ProcessChoicePoint - is called for each basic block inserted to choose
98 /// between normal and sample code
99 virtual void ProcessChoicePoint(BasicBlock*) = 0;
100 /// PrepFunction - is called once per function before other work is done.
101 /// This gives the opertunity to insert new allocas and such.
102 virtual void PrepFunction(Function*) = 0;
103 virtual ~Chooser() {}
104 };
105
106 //Things that implement sampling policies
107 //A global value that is read-mod-stored to choose when to sample.
108 //A sample is taken when the global counter hits 0
109 class VISIBILITY_HIDDEN GlobalRandomCounter : public Chooser {
110 GlobalVariable* Counter;
111 Value* ResetValue;
112 const Type* T;
113 public:
114 GlobalRandomCounter(Module& M, const Type* t, uint64_t resetval);
115 virtual ~GlobalRandomCounter();
116 virtual void PrepFunction(Function* F);
117 virtual void ProcessChoicePoint(BasicBlock* bb);
118 };
119
120 //Same is GRC, but allow register allocation of the global counter
121 class VISIBILITY_HIDDEN GlobalRandomCounterOpt : public Chooser {
122 GlobalVariable* Counter;
123 Value* ResetValue;
124 AllocaInst* AI;
125 const Type* T;
126 public:
127 GlobalRandomCounterOpt(Module& M, const Type* t, uint64_t resetval);
128 virtual ~GlobalRandomCounterOpt();
129 virtual void PrepFunction(Function* F);
130 virtual void ProcessChoicePoint(BasicBlock* bb);
131 };
132
133 //Use the cycle counter intrinsic as a source of pseudo randomness when
134 //deciding when to sample.
135 class VISIBILITY_HIDDEN CycleCounter : public Chooser {
136 uint64_t rm;
137 Constant *F;
138 public:
139 CycleCounter(Module& m, uint64_t resetmask);
140 virtual ~CycleCounter();
141 virtual void PrepFunction(Function* F);
142 virtual void ProcessChoicePoint(BasicBlock* bb);
143 };
144
145 /// ProfilerRS - Insert the random sampling framework
146 struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass {
147 static char ID; // Pass identification, replacement for typeid
148 ProfilerRS() : FunctionPass((intptr_t)&ID) {}
149
150 std::map<Value*, Value*> TransCache;
151 std::set<BasicBlock*> ChoicePoints;
152 Chooser* c;
153
154 //Translate and duplicate values for the new profile free version of stuff
155 Value* Translate(Value* v);
156 //Duplicate an entire function (with out profiling)
157 void Duplicate(Function& F, RSProfilers& LI);
158 //Called once for each backedge, handle the insertion of choice points and
159 //the interconection of the two versions of the code
160 void ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F);
161 bool runOnFunction(Function& F);
162 bool doInitialization(Module &M);
163 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
164 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165}
166
Dan Gohman089efff2008-05-13 00:00:25 +0000167static RegisterPass<ProfilerRS>
168X("insert-rs-profiling-framework",
169 "Insert random sampling instrumentation framework");
170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171char RSProfilers::ID = 0;
172char NullProfilerRS::ID = 0;
173char ProfilerRS::ID = 0;
174
175//Local utilities
176static void ReplacePhiPred(BasicBlock* btarget,
177 BasicBlock* bold, BasicBlock* bnew);
178
179static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc);
180
181template<class T>
182static void recBackEdge(BasicBlock* bb, T& BackEdges,
183 std::map<BasicBlock*, int>& color,
184 std::map<BasicBlock*, int>& depth,
185 std::map<BasicBlock*, int>& finish,
186 int& time);
187
188//find the back edges and where they go to
189template<class T>
190static void getBackEdges(Function& F, T& BackEdges);
191
192
193///////////////////////////////////////
194// Methods of choosing when to profile
195///////////////////////////////////////
196
197GlobalRandomCounter::GlobalRandomCounter(Module& M, const Type* t,
198 uint64_t resetval) : T(t) {
199 ConstantInt* Init = ConstantInt::get(T, resetval);
200 ResetValue = Init;
201 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
202 Init, "RandomSteeringCounter", &M);
203}
204
205GlobalRandomCounter::~GlobalRandomCounter() {}
206
207void GlobalRandomCounter::PrepFunction(Function* F) {}
208
209void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
210 BranchInst* t = cast<BranchInst>(bb->getTerminator());
211
212 //decrement counter
213 LoadInst* l = new LoadInst(Counter, "counter", t);
214
215 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
216 "countercc", t);
217
Gabor Greifa645dd32008-05-16 19:29:10 +0000218 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 "counternew", t);
220 new StoreInst(nv, Counter, t);
221 t->setCondition(s);
222
223 //reset counter
224 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000225 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
226 oldnext);
227 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 t->setSuccessor(0, resetblock);
229 new StoreInst(ResetValue, Counter, t2);
230 ReplacePhiPred(oldnext, bb, resetblock);
231}
232
233GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const Type* t,
234 uint64_t resetval)
235 : AI(0), T(t) {
236 ConstantInt* Init = ConstantInt::get(T, resetval);
237 ResetValue = Init;
238 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
239 Init, "RandomSteeringCounter", &M);
240}
241
242GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {}
243
244void GlobalRandomCounterOpt::PrepFunction(Function* F) {
245 //make a local temporary to cache the global
246 BasicBlock& bb = F->getEntryBlock();
247 BasicBlock::iterator InsertPt = bb.begin();
248 AI = new AllocaInst(T, 0, "localcounter", InsertPt);
249 LoadInst* l = new LoadInst(Counter, "counterload", InsertPt);
250 new StoreInst(l, AI, InsertPt);
251
252 //modify all functions and return values to restore the local variable to/from
253 //the global variable
254 for(Function::iterator fib = F->begin(), fie = F->end();
255 fib != fie; ++fib)
256 for(BasicBlock::iterator bib = fib->begin(), bie = fib->end();
257 bib != bie; ++bib)
258 if (isa<CallInst>(bib)) {
259 LoadInst* l = new LoadInst(AI, "counter", bib);
260 new StoreInst(l, Counter, bib);
261 l = new LoadInst(Counter, "counter", ++bib);
262 new StoreInst(l, AI, bib--);
263 } else if (isa<InvokeInst>(bib)) {
264 LoadInst* l = new LoadInst(AI, "counter", bib);
265 new StoreInst(l, Counter, bib);
266
267 BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest();
Dan Gohman514277c2008-05-23 21:05:58 +0000268 BasicBlock::iterator i = bb->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 l = new LoadInst(Counter, "counter", i);
270
271 bb = cast<InvokeInst>(bib)->getUnwindDest();
Dan Gohman514277c2008-05-23 21:05:58 +0000272 i = bb->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 l = new LoadInst(Counter, "counter", i);
274 new StoreInst(l, AI, i);
275 } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
276 LoadInst* l = new LoadInst(AI, "counter", bib);
277 new StoreInst(l, Counter, bib);
278 }
279}
280
281void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
282 BranchInst* t = cast<BranchInst>(bb->getTerminator());
283
284 //decrement counter
285 LoadInst* l = new LoadInst(AI, "counter", t);
286
287 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
288 "countercc", t);
289
Gabor Greifa645dd32008-05-16 19:29:10 +0000290 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 "counternew", t);
292 new StoreInst(nv, AI, t);
293 t->setCondition(s);
294
295 //reset counter
296 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000297 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
298 oldnext);
299 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 t->setSuccessor(0, resetblock);
301 new StoreInst(ResetValue, AI, t2);
302 ReplacePhiPred(oldnext, bb, resetblock);
303}
304
305
306CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) {
Duncan Sands3c812692008-04-07 13:45:04 +0000307 F = Intrinsic::getDeclaration(&m, Intrinsic::readcyclecounter);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308}
309
310CycleCounter::~CycleCounter() {}
311
312void CycleCounter::PrepFunction(Function* F) {}
313
314void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
315 BranchInst* t = cast<BranchInst>(bb->getTerminator());
316
Gabor Greifd6da1d02008-04-06 20:25:17 +0000317 CallInst* c = CallInst::Create(F, "rdcc", t);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 BinaryOperator* b =
Gabor Greifa645dd32008-05-16 19:29:10 +0000319 BinaryOperator::CreateAnd(c, ConstantInt::get(Type::Int64Ty, rm),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 "mrdcc", t);
321
322 ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
323 ConstantInt::get(Type::Int64Ty, 0),
324 "mrdccc", t);
325
326 t->setCondition(s);
327}
328
329///////////////////////////////////////
330// Profiling:
331///////////////////////////////////////
332bool RSProfilers_std::isProfiling(Value* v) {
333 if (profcode.find(v) != profcode.end())
334 return true;
335 //else
336 RSProfilers& LI = getAnalysis<RSProfilers>();
337 return LI.isProfiling(v);
338}
339
340void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
341 GlobalValue *CounterArray) {
342 // Insert the increment after any alloca or PHI instructions...
Dan Gohman514277c2008-05-23 21:05:58 +0000343 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
344 while (isa<AllocaInst>(InsertPos))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 ++InsertPos;
346
347 // Create the getelementptr constant expression
348 std::vector<Constant*> Indices(2);
349 Indices[0] = Constant::getNullValue(Type::Int32Ty);
350 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
351 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray,
352 &Indices[0], 2);
353
354 // Load, increment and store the value back.
355 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
356 profcode.insert(OldVal);
Gabor Greifa645dd32008-05-16 19:29:10 +0000357 Value *NewVal = BinaryOperator::CreateAdd(OldVal,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 ConstantInt::get(Type::Int32Ty, 1),
359 "NewCounter", InsertPos);
360 profcode.insert(NewVal);
361 profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos));
362}
363
364void RSProfilers_std::getAnalysisUsage(AnalysisUsage &AU) const {
365 //grab any outstanding profiler, or get the null one
366 AU.addRequired<RSProfilers>();
367}
368
369///////////////////////////////////////
370// RS Framework
371///////////////////////////////////////
372
373Value* ProfilerRS::Translate(Value* v) {
374 if(TransCache[v])
375 return TransCache[v];
376
377 if (BasicBlock* bb = dyn_cast<BasicBlock>(v)) {
378 if (bb == &bb->getParent()->getEntryBlock())
379 TransCache[bb] = bb; //don't translate entry block
380 else
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000381 TransCache[bb] = BasicBlock::Create("dup_" + bb->getName(),
382 bb->getParent(), NULL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 return TransCache[bb];
384 } else if (Instruction* i = dyn_cast<Instruction>(v)) {
385 //we have already translated this
386 //do not translate entry block allocas
387 if(&i->getParent()->getParent()->getEntryBlock() == i->getParent()) {
388 TransCache[i] = i;
389 return i;
390 } else {
391 //translate this
392 Instruction* i2 = i->clone();
393 if (i->hasName())
394 i2->setName("dup_" + i->getName());
395 TransCache[i] = i2;
396 //NumNewInst++;
397 for (unsigned x = 0; x < i2->getNumOperands(); ++x)
398 i2->setOperand(x, Translate(i2->getOperand(x)));
399 return i2;
400 }
401 } else if (isa<Function>(v) || isa<Constant>(v) || isa<Argument>(v)) {
402 TransCache[v] = v;
403 return v;
404 }
405 assert(0 && "Value not handled");
406 return 0;
407}
408
409void ProfilerRS::Duplicate(Function& F, RSProfilers& LI)
410{
411 //perform a breadth first search, building up a duplicate of the code
412 std::queue<BasicBlock*> worklist;
413 std::set<BasicBlock*> seen;
414
415 //This loop ensures proper BB order, to help performance
416 for (Function::iterator fib = F.begin(), fie = F.end(); fib != fie; ++fib)
417 worklist.push(fib);
418 while (!worklist.empty()) {
419 Translate(worklist.front());
420 worklist.pop();
421 }
422
423 //remember than reg2mem created a new entry block we don't want to duplicate
424 worklist.push(F.getEntryBlock().getTerminator()->getSuccessor(0));
425 seen.insert(&F.getEntryBlock());
426
427 while (!worklist.empty()) {
428 BasicBlock* bb = worklist.front();
429 worklist.pop();
430 if(seen.find(bb) == seen.end()) {
431 BasicBlock* bbtarget = cast<BasicBlock>(Translate(bb));
432 BasicBlock::InstListType& instlist = bbtarget->getInstList();
433 for (BasicBlock::iterator iib = bb->begin(), iie = bb->end();
434 iib != iie; ++iib) {
435 //NumOldInst++;
436 if (!LI.isProfiling(&*iib)) {
437 Instruction* i = cast<Instruction>(Translate(iib));
438 instlist.insert(bbtarget->end(), i);
439 }
440 }
441 //updated search state;
442 seen.insert(bb);
443 TerminatorInst* ti = bb->getTerminator();
444 for (unsigned x = 0; x < ti->getNumSuccessors(); ++x) {
445 BasicBlock* bbs = ti->getSuccessor(x);
446 if (seen.find(bbs) == seen.end()) {
447 worklist.push(bbs);
448 }
449 }
450 }
451 }
452}
453
454void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F) {
455 //given a backedge from B -> A, and translations A' and B',
456 //a: insert C and C'
457 //b: add branches in C to A and A' and in C' to A and A'
458 //c: mod terminators@B, replace A with C
459 //d: mod terminators@B', replace A' with C'
460 //e: mod phis@A for pred B to be pred C
461 // if multiple entries, simplify to one
462 //f: mod phis@A' for pred B' to be pred C'
463 // if multiple entries, simplify to one
464 //g: for all phis@A with pred C using x
465 // add in edge from C' using x'
466 // add in edge from C using x in A'
467
468 //a:
469 Function::iterator BBN = src; ++BBN;
Gabor Greifd6da1d02008-04-06 20:25:17 +0000470 BasicBlock* bbC = BasicBlock::Create("choice", &F, BBN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 //ChoicePoints.insert(bbC);
472 BBN = cast<BasicBlock>(Translate(src));
Gabor Greifd6da1d02008-04-06 20:25:17 +0000473 BasicBlock* bbCp = BasicBlock::Create("choice", &F, ++BBN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 ChoicePoints.insert(bbCp);
475
476 //b:
Gabor Greifd6da1d02008-04-06 20:25:17 +0000477 BranchInst::Create(cast<BasicBlock>(Translate(dst)), bbC);
478 BranchInst::Create(dst, cast<BasicBlock>(Translate(dst)),
479 ConstantInt::get(Type::Int1Ty, true), bbCp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 //c:
481 {
482 TerminatorInst* iB = src->getTerminator();
483 for (unsigned x = 0; x < iB->getNumSuccessors(); ++x)
484 if (iB->getSuccessor(x) == dst)
485 iB->setSuccessor(x, bbC);
486 }
487 //d:
488 {
489 TerminatorInst* iBp = cast<TerminatorInst>(Translate(src->getTerminator()));
490 for (unsigned x = 0; x < iBp->getNumSuccessors(); ++x)
491 if (iBp->getSuccessor(x) == cast<BasicBlock>(Translate(dst)))
492 iBp->setSuccessor(x, bbCp);
493 }
494 //e:
495 ReplacePhiPred(dst, src, bbC);
496 //src could be a switch, in which case we are replacing several edges with one
497 //thus collapse those edges int the Phi
498 CollapsePhi(dst, bbC);
499 //f:
500 ReplacePhiPred(cast<BasicBlock>(Translate(dst)),
501 cast<BasicBlock>(Translate(src)),bbCp);
502 CollapsePhi(cast<BasicBlock>(Translate(dst)), bbCp);
503 //g:
504 for(BasicBlock::iterator ib = dst->begin(), ie = dst->end(); ib != ie;
505 ++ib)
506 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
507 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
508 if(bbC == phi->getIncomingBlock(x)) {
509 phi->addIncoming(Translate(phi->getIncomingValue(x)), bbCp);
510 cast<PHINode>(Translate(phi))->addIncoming(phi->getIncomingValue(x),
511 bbC);
512 }
513 phi->removeIncomingValue(bbC);
514 }
515}
516
517bool ProfilerRS::runOnFunction(Function& F) {
518 if (!F.isDeclaration()) {
519 std::set<std::pair<BasicBlock*, BasicBlock*> > BackEdges;
520 RSProfilers& LI = getAnalysis<RSProfilers>();
521
522 getBackEdges(F, BackEdges);
523 Duplicate(F, LI);
524 //assume that stuff worked. now connect the duplicated basic blocks
525 //with the originals in such a way as to preserve ssa. yuk!
526 for (std::set<std::pair<BasicBlock*, BasicBlock*> >::iterator
527 ib = BackEdges.begin(), ie = BackEdges.end(); ib != ie; ++ib)
528 ProcessBackEdge(ib->first, ib->second, F);
529
530 //oh, and add the edge from the reg2mem created entry node to the
531 //duplicated second node
532 TerminatorInst* T = F.getEntryBlock().getTerminator();
Gabor Greifd6da1d02008-04-06 20:25:17 +0000533 ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0),
534 cast<BasicBlock>(
535 Translate(T->getSuccessor(0))),
536 ConstantInt::get(Type::Int1Ty,
537 true)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 //do whatever is needed now that the function is duplicated
540 c->PrepFunction(&F);
541
542 //add entry node to choice points
543 ChoicePoints.insert(&F.getEntryBlock());
544
545 for (std::set<BasicBlock*>::iterator
546 ii = ChoicePoints.begin(), ie = ChoicePoints.end(); ii != ie; ++ii)
547 c->ProcessChoicePoint(*ii);
548
549 ChoicePoints.clear();
550 TransCache.clear();
551
552 return true;
553 }
554 return false;
555}
556
557bool ProfilerRS::doInitialization(Module &M) {
558 switch (RandomMethod) {
559 case GBV:
560 c = new GlobalRandomCounter(M, Type::Int32Ty, (1 << 14) - 1);
561 break;
562 case GBVO:
563 c = new GlobalRandomCounterOpt(M, Type::Int32Ty, (1 << 14) - 1);
564 break;
565 case HOSTCC:
566 c = new CycleCounter(M, (1 << 14) - 1);
567 break;
568 };
569 return true;
570}
571
572void ProfilerRS::getAnalysisUsage(AnalysisUsage &AU) const {
573 AU.addRequired<RSProfilers>();
574 AU.addRequiredID(DemoteRegisterToMemoryID);
575}
576
577///////////////////////////////////////
578// Utilities:
579///////////////////////////////////////
580static void ReplacePhiPred(BasicBlock* btarget,
581 BasicBlock* bold, BasicBlock* bnew) {
582 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
583 ib != ie; ++ib)
584 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
585 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
586 if(bold == phi->getIncomingBlock(x))
587 phi->setIncomingBlock(x, bnew);
588 }
589}
590
591static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc) {
592 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
593 ib != ie; ++ib)
594 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
595 std::map<BasicBlock*, Value*> counter;
596 for(unsigned i = 0; i < phi->getNumIncomingValues(); ) {
597 if (counter[phi->getIncomingBlock(i)]) {
598 assert(phi->getIncomingValue(i) == counter[phi->getIncomingBlock(i)]);
599 phi->removeIncomingValue(i, false);
600 } else {
601 counter[phi->getIncomingBlock(i)] = phi->getIncomingValue(i);
602 ++i;
603 }
604 }
605 }
606}
607
608template<class T>
609static void recBackEdge(BasicBlock* bb, T& BackEdges,
610 std::map<BasicBlock*, int>& color,
611 std::map<BasicBlock*, int>& depth,
612 std::map<BasicBlock*, int>& finish,
613 int& time)
614{
615 color[bb] = 1;
616 ++time;
617 depth[bb] = time;
618 TerminatorInst* t= bb->getTerminator();
619 for(unsigned i = 0; i < t->getNumSuccessors(); ++i) {
620 BasicBlock* bbnew = t->getSuccessor(i);
621 if (color[bbnew] == 0)
622 recBackEdge(bbnew, BackEdges, color, depth, finish, time);
623 else if (color[bbnew] == 1) {
624 BackEdges.insert(std::make_pair(bb, bbnew));
625 //NumBackEdges++;
626 }
627 }
628 color[bb] = 2;
629 ++time;
630 finish[bb] = time;
631}
632
633
634
635//find the back edges and where they go to
636template<class T>
637static void getBackEdges(Function& F, T& BackEdges) {
638 std::map<BasicBlock*, int> color;
639 std::map<BasicBlock*, int> depth;
640 std::map<BasicBlock*, int> finish;
641 int time = 0;
642 recBackEdge(&F.getEntryBlock(), BackEdges, color, depth, finish, time);
643 DOUT << F.getName() << " " << BackEdges.size() << "\n";
644}
645
646
647//Creation functions
648ModulePass* llvm::createNullProfilerRSPass() {
649 return new NullProfilerRS();
650}
651
652FunctionPass* llvm::createRSProfilingPass() {
653 return new ProfilerRS();
654}