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