Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 1 | //===- RSProfiling.cpp - Various profiling using random sampling ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 13 | // passes which insert profiling into the program, and remember what they |
| 14 | // inserted. |
| 15 | // |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 Henriksen | 55cbec3 | 2007-10-26 03:03:51 +0000 | [diff] [blame] | 21 | // It is highly recommended that after this pass one runs mem2reg and adce |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 36 | #include "llvm/Module.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 37 | #include "llvm/Instructions.h" |
| 38 | #include "llvm/Constants.h" |
| 39 | #include "llvm/DerivedTypes.h" |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 40 | #include "llvm/Intrinsics.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar.h" |
| 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Compiler.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
| 46 | #include "llvm/Transforms/Instrumentation.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 47 | #include "RSProfiling.h" |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 48 | #include <set> |
| 49 | #include <map> |
| 50 | #include <queue> |
| 51 | #include <list> |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | |
| 54 | namespace { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 55 | enum RandomMeth { |
| 56 | GBV, GBVO, HOSTCC |
| 57 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 58 | } |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 59 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 60 | static 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)); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 68 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | namespace { |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 70 | /// 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 |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 73 | class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 74 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 75 | static char ID; // Pass identification, replacement for typeid |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 86 | } |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 88 | static RegisterAnalysisGroup<RSProfilers> A("Profiling passes"); |
| 89 | static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs", |
| 90 | "Measure profiling framework overhead"); |
| 91 | static RegisterAnalysisGroup<RSProfilers, true> NPT(NP); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 92 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 93 | namespace { |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 94 | /// Chooser - Something that chooses when to make a sample of the profiled code |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 95 | class VISIBILITY_HIDDEN Chooser { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 96 | public: |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 97 | /// ProcessChoicePoint - is called for each basic block inserted to choose |
| 98 | /// between normal and sample code |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 99 | virtual void ProcessChoicePoint(BasicBlock*) = 0; |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 100 | /// PrepFunction - is called once per function before other work is done. |
| 101 | /// This gives the opertunity to insert new allocas and such. |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 102 | virtual void PrepFunction(Function*) = 0; |
| 103 | virtual ~Chooser() {} |
| 104 | }; |
| 105 | |
| 106 | //Things that implement sampling policies |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 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 |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 109 | class VISIBILITY_HIDDEN GlobalRandomCounter : public Chooser { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 120 | //Same is GRC, but allow register allocation of the global counter |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 121 | class VISIBILITY_HIDDEN GlobalRandomCounterOpt : public Chooser { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 133 | //Use the cycle counter intrinsic as a source of pseudo randomness when |
| 134 | //deciding when to sample. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 135 | class VISIBILITY_HIDDEN CycleCounter : public Chooser { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 136 | uint64_t rm; |
Chris Lattner | febe5f1 | 2007-01-07 07:22:20 +0000 | [diff] [blame] | 137 | Constant *F; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 145 | /// ProfilerRS - Insert the random sampling framework |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 146 | struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 147 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 148 | ProfilerRS() : FunctionPass((intptr_t)&ID) {} |
| 149 | |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 150 | std::map<Value*, Value*> TransCache; |
| 151 | std::set<BasicBlock*> ChoicePoints; |
| 152 | Chooser* c; |
| 153 | |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 154 | //Translate and duplicate values for the new profile free version of stuff |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 155 | Value* Translate(Value* v); |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 156 | //Duplicate an entire function (with out profiling) |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 157 | void Duplicate(Function& F, RSProfilers& LI); |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 158 | //Called once for each backedge, handle the insertion of choice points and |
| 159 | //the interconection of the two versions of the code |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | }; |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 165 | } |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 166 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 167 | static RegisterPass<ProfilerRS> |
| 168 | X("insert-rs-profiling-framework", |
| 169 | "Insert random sampling instrumentation framework"); |
| 170 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 171 | char RSProfilers::ID = 0; |
| 172 | char NullProfilerRS::ID = 0; |
| 173 | char ProfilerRS::ID = 0; |
Lauro Ramos Venancio | c718288 | 2007-05-02 20:37:47 +0000 | [diff] [blame] | 174 | |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 175 | //Local utilities |
| 176 | static void ReplacePhiPred(BasicBlock* btarget, |
| 177 | BasicBlock* bold, BasicBlock* bnew); |
| 178 | |
| 179 | static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc); |
| 180 | |
| 181 | template<class T> |
| 182 | static 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 |
| 189 | template<class T> |
| 190 | static void getBackEdges(Function& F, T& BackEdges); |
| 191 | |
| 192 | |
| 193 | /////////////////////////////////////// |
| 194 | // Methods of choosing when to profile |
| 195 | /////////////////////////////////////// |
| 196 | |
| 197 | GlobalRandomCounter::GlobalRandomCounter(Module& M, const Type* t, |
| 198 | uint64_t resetval) : T(t) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 199 | ConstantInt* Init = ConstantInt::get(T, resetval); |
| 200 | ResetValue = Init; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 201 | Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage, |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 202 | Init, "RandomSteeringCounter", &M); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | GlobalRandomCounter::~GlobalRandomCounter() {} |
| 206 | |
| 207 | void GlobalRandomCounter::PrepFunction(Function* F) {} |
| 208 | |
| 209 | void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) { |
| 210 | BranchInst* t = cast<BranchInst>(bb->getTerminator()); |
| 211 | |
| 212 | //decrement counter |
| 213 | LoadInst* l = new LoadInst(Counter, "counter", t); |
| 214 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 215 | ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), |
| 216 | "countercc", t); |
| 217 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 218 | Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 219 | "counternew", t); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 220 | new StoreInst(nv, Counter, t); |
| 221 | t->setCondition(s); |
| 222 | |
| 223 | //reset counter |
| 224 | BasicBlock* oldnext = t->getSuccessor(0); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 225 | BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(), |
| 226 | oldnext); |
| 227 | TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 228 | t->setSuccessor(0, resetblock); |
| 229 | new StoreInst(ResetValue, Counter, t2); |
| 230 | ReplacePhiPred(oldnext, bb, resetblock); |
| 231 | } |
| 232 | |
| 233 | GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const Type* t, |
| 234 | uint64_t resetval) |
| 235 | : AI(0), T(t) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 236 | ConstantInt* Init = ConstantInt::get(T, resetval); |
| 237 | ResetValue = Init; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 238 | Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage, |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 239 | Init, "RandomSteeringCounter", &M); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {} |
| 243 | |
| 244 | void GlobalRandomCounterOpt::PrepFunction(Function* F) { |
| 245 | //make a local temporary to cache the global |
| 246 | BasicBlock& bb = F->getEntryBlock(); |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 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); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 251 | |
Andrew Lenharth | 8dc2d50 | 2005-11-28 18:10:59 +0000 | [diff] [blame] | 252 | //modify all functions and return values to restore the local variable to/from |
| 253 | //the global variable |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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) |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 258 | if (isa<CallInst>(bib)) { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 259 | LoadInst* l = new LoadInst(AI, "counter", bib); |
| 260 | new StoreInst(l, Counter, bib); |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 261 | l = new LoadInst(Counter, "counter", ++bib); |
| 262 | new StoreInst(l, AI, bib--); |
| 263 | } else if (isa<InvokeInst>(bib)) { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 264 | LoadInst* l = new LoadInst(AI, "counter", bib); |
| 265 | new StoreInst(l, Counter, bib); |
| 266 | |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 267 | BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest(); |
| 268 | BasicBlock::iterator i = bb->begin(); |
| 269 | while (isa<PHINode>(i)) |
| 270 | ++i; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 271 | l = new LoadInst(Counter, "counter", i); |
| 272 | |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 273 | bb = cast<InvokeInst>(bib)->getUnwindDest(); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 274 | i = bb->begin(); |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 275 | while (isa<PHINode>(i)) ++i; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 276 | l = new LoadInst(Counter, "counter", i); |
Chris Lattner | a0e1b0e | 2007-04-17 17:51:03 +0000 | [diff] [blame] | 277 | new StoreInst(l, AI, i); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 290 | ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), |
| 291 | "countercc", t); |
| 292 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 293 | Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 294 | "counternew", t); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 295 | new StoreInst(nv, AI, t); |
| 296 | t->setCondition(s); |
| 297 | |
| 298 | //reset counter |
| 299 | BasicBlock* oldnext = t->getSuccessor(0); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 300 | BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(), |
| 301 | oldnext); |
| 302 | TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 303 | t->setSuccessor(0, resetblock); |
| 304 | new StoreInst(ResetValue, AI, t2); |
| 305 | ReplacePhiPred(oldnext, bb, resetblock); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) { |
Duncan Sands | e2c4304 | 2008-04-07 13:45:04 +0000 | [diff] [blame] | 310 | F = Intrinsic::getDeclaration(&m, Intrinsic::readcyclecounter); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | CycleCounter::~CycleCounter() {} |
| 314 | |
| 315 | void CycleCounter::PrepFunction(Function* F) {} |
| 316 | |
| 317 | void CycleCounter::ProcessChoicePoint(BasicBlock* bb) { |
| 318 | BranchInst* t = cast<BranchInst>(bb->getTerminator()); |
| 319 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 320 | CallInst* c = CallInst::Create(F, "rdcc", t); |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 321 | BinaryOperator* b = |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 322 | BinaryOperator::createAnd(c, ConstantInt::get(Type::Int64Ty, rm), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 323 | "mrdcc", t); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 324 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 325 | ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 326 | ConstantInt::get(Type::Int64Ty, 0), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 327 | "mrdccc", t); |
| 328 | |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 329 | t->setCondition(s); |
| 330 | } |
| 331 | |
| 332 | /////////////////////////////////////// |
| 333 | // Profiling: |
| 334 | /////////////////////////////////////// |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 335 | bool RSProfilers_std::isProfiling(Value* v) { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 336 | if (profcode.find(v) != profcode.end()) |
| 337 | return true; |
| 338 | //else |
| 339 | RSProfilers& LI = getAnalysis<RSProfilers>(); |
| 340 | return LI.isProfiling(v); |
| 341 | } |
| 342 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 343 | void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 344 | GlobalValue *CounterArray) { |
| 345 | // Insert the increment after any alloca or PHI instructions... |
| 346 | BasicBlock::iterator InsertPos = BB->begin(); |
| 347 | while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos)) |
| 348 | ++InsertPos; |
| 349 | |
| 350 | // Create the getelementptr constant expression |
| 351 | std::vector<Constant*> Indices(2); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 352 | Indices[0] = Constant::getNullValue(Type::Int32Ty); |
| 353 | Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum); |
Chris Lattner | ec1f752 | 2007-02-19 07:34:47 +0000 | [diff] [blame] | 354 | Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, |
| 355 | &Indices[0], 2); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 356 | |
| 357 | // Load, increment and store the value back. |
| 358 | Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos); |
| 359 | profcode.insert(OldVal); |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 360 | Value *NewVal = BinaryOperator::createAdd(OldVal, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 361 | ConstantInt::get(Type::Int32Ty, 1), |
| 362 | "NewCounter", InsertPos); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 363 | profcode.insert(NewVal); |
| 364 | profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos)); |
| 365 | } |
| 366 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 367 | void RSProfilers_std::getAnalysisUsage(AnalysisUsage &AU) const { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 368 | //grab any outstanding profiler, or get the null one |
| 369 | AU.addRequired<RSProfilers>(); |
| 370 | } |
| 371 | |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 372 | /////////////////////////////////////// |
| 373 | // RS Framework |
| 374 | /////////////////////////////////////// |
| 375 | |
| 376 | Value* ProfilerRS::Translate(Value* v) { |
| 377 | if(TransCache[v]) |
| 378 | return TransCache[v]; |
| 379 | |
| 380 | if (BasicBlock* bb = dyn_cast<BasicBlock>(v)) { |
| 381 | if (bb == &bb->getParent()->getEntryBlock()) |
| 382 | TransCache[bb] = bb; //don't translate entry block |
| 383 | else |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 384 | TransCache[bb] = BasicBlock::Create("dup_" + bb->getName(), bb->getParent(), |
| 385 | NULL); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 386 | return TransCache[bb]; |
| 387 | } else if (Instruction* i = dyn_cast<Instruction>(v)) { |
| 388 | //we have already translated this |
| 389 | //do not translate entry block allocas |
| 390 | if(&i->getParent()->getParent()->getEntryBlock() == i->getParent()) { |
| 391 | TransCache[i] = i; |
| 392 | return i; |
| 393 | } else { |
| 394 | //translate this |
| 395 | Instruction* i2 = i->clone(); |
| 396 | if (i->hasName()) |
| 397 | i2->setName("dup_" + i->getName()); |
| 398 | TransCache[i] = i2; |
| 399 | //NumNewInst++; |
| 400 | for (unsigned x = 0; x < i2->getNumOperands(); ++x) |
| 401 | i2->setOperand(x, Translate(i2->getOperand(x))); |
| 402 | return i2; |
| 403 | } |
| 404 | } else if (isa<Function>(v) || isa<Constant>(v) || isa<Argument>(v)) { |
| 405 | TransCache[v] = v; |
| 406 | return v; |
| 407 | } |
| 408 | assert(0 && "Value not handled"); |
Jeff Cohen | 3523f6e | 2005-11-28 06:45:57 +0000 | [diff] [blame] | 409 | return 0; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void ProfilerRS::Duplicate(Function& F, RSProfilers& LI) |
| 413 | { |
| 414 | //perform a breadth first search, building up a duplicate of the code |
| 415 | std::queue<BasicBlock*> worklist; |
| 416 | std::set<BasicBlock*> seen; |
| 417 | |
| 418 | //This loop ensures proper BB order, to help performance |
| 419 | for (Function::iterator fib = F.begin(), fie = F.end(); fib != fie; ++fib) |
| 420 | worklist.push(fib); |
| 421 | while (!worklist.empty()) { |
| 422 | Translate(worklist.front()); |
| 423 | worklist.pop(); |
| 424 | } |
| 425 | |
| 426 | //remember than reg2mem created a new entry block we don't want to duplicate |
| 427 | worklist.push(F.getEntryBlock().getTerminator()->getSuccessor(0)); |
| 428 | seen.insert(&F.getEntryBlock()); |
| 429 | |
| 430 | while (!worklist.empty()) { |
| 431 | BasicBlock* bb = worklist.front(); |
| 432 | worklist.pop(); |
| 433 | if(seen.find(bb) == seen.end()) { |
| 434 | BasicBlock* bbtarget = cast<BasicBlock>(Translate(bb)); |
| 435 | BasicBlock::InstListType& instlist = bbtarget->getInstList(); |
| 436 | for (BasicBlock::iterator iib = bb->begin(), iie = bb->end(); |
| 437 | iib != iie; ++iib) { |
| 438 | //NumOldInst++; |
| 439 | if (!LI.isProfiling(&*iib)) { |
| 440 | Instruction* i = cast<Instruction>(Translate(iib)); |
| 441 | instlist.insert(bbtarget->end(), i); |
| 442 | } |
| 443 | } |
| 444 | //updated search state; |
| 445 | seen.insert(bb); |
| 446 | TerminatorInst* ti = bb->getTerminator(); |
| 447 | for (unsigned x = 0; x < ti->getNumSuccessors(); ++x) { |
| 448 | BasicBlock* bbs = ti->getSuccessor(x); |
| 449 | if (seen.find(bbs) == seen.end()) { |
| 450 | worklist.push(bbs); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F) { |
| 458 | //given a backedge from B -> A, and translations A' and B', |
| 459 | //a: insert C and C' |
| 460 | //b: add branches in C to A and A' and in C' to A and A' |
| 461 | //c: mod terminators@B, replace A with C |
| 462 | //d: mod terminators@B', replace A' with C' |
| 463 | //e: mod phis@A for pred B to be pred C |
| 464 | // if multiple entries, simplify to one |
| 465 | //f: mod phis@A' for pred B' to be pred C' |
| 466 | // if multiple entries, simplify to one |
| 467 | //g: for all phis@A with pred C using x |
| 468 | // add in edge from C' using x' |
| 469 | // add in edge from C using x in A' |
| 470 | |
| 471 | //a: |
Chris Lattner | e24c92a | 2007-04-17 17:54:12 +0000 | [diff] [blame] | 472 | Function::iterator BBN = src; ++BBN; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 473 | BasicBlock* bbC = BasicBlock::Create("choice", &F, BBN); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 474 | //ChoicePoints.insert(bbC); |
Chris Lattner | e24c92a | 2007-04-17 17:54:12 +0000 | [diff] [blame] | 475 | BBN = cast<BasicBlock>(Translate(src)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 476 | BasicBlock* bbCp = BasicBlock::Create("choice", &F, ++BBN); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 477 | ChoicePoints.insert(bbCp); |
| 478 | |
| 479 | //b: |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 480 | BranchInst::Create(cast<BasicBlock>(Translate(dst)), bbC); |
| 481 | BranchInst::Create(dst, cast<BasicBlock>(Translate(dst)), |
| 482 | ConstantInt::get(Type::Int1Ty, true), bbCp); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 483 | //c: |
| 484 | { |
| 485 | TerminatorInst* iB = src->getTerminator(); |
| 486 | for (unsigned x = 0; x < iB->getNumSuccessors(); ++x) |
| 487 | if (iB->getSuccessor(x) == dst) |
| 488 | iB->setSuccessor(x, bbC); |
| 489 | } |
| 490 | //d: |
| 491 | { |
| 492 | TerminatorInst* iBp = cast<TerminatorInst>(Translate(src->getTerminator())); |
| 493 | for (unsigned x = 0; x < iBp->getNumSuccessors(); ++x) |
| 494 | if (iBp->getSuccessor(x) == cast<BasicBlock>(Translate(dst))) |
| 495 | iBp->setSuccessor(x, bbCp); |
| 496 | } |
| 497 | //e: |
| 498 | ReplacePhiPred(dst, src, bbC); |
| 499 | //src could be a switch, in which case we are replacing several edges with one |
| 500 | //thus collapse those edges int the Phi |
| 501 | CollapsePhi(dst, bbC); |
| 502 | //f: |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 503 | ReplacePhiPred(cast<BasicBlock>(Translate(dst)), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 504 | cast<BasicBlock>(Translate(src)),bbCp); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 505 | CollapsePhi(cast<BasicBlock>(Translate(dst)), bbCp); |
| 506 | //g: |
| 507 | for(BasicBlock::iterator ib = dst->begin(), ie = dst->end(); ib != ie; |
| 508 | ++ib) |
| 509 | if (PHINode* phi = dyn_cast<PHINode>(&*ib)) { |
| 510 | for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x) |
| 511 | if(bbC == phi->getIncomingBlock(x)) { |
| 512 | phi->addIncoming(Translate(phi->getIncomingValue(x)), bbCp); |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 513 | cast<PHINode>(Translate(phi))->addIncoming(phi->getIncomingValue(x), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 514 | bbC); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 515 | } |
| 516 | phi->removeIncomingValue(bbC); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool ProfilerRS::runOnFunction(Function& F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 521 | if (!F.isDeclaration()) { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 522 | std::set<std::pair<BasicBlock*, BasicBlock*> > BackEdges; |
| 523 | RSProfilers& LI = getAnalysis<RSProfilers>(); |
| 524 | |
| 525 | getBackEdges(F, BackEdges); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 526 | Duplicate(F, LI); |
| 527 | //assume that stuff worked. now connect the duplicated basic blocks |
| 528 | //with the originals in such a way as to preserve ssa. yuk! |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 529 | for (std::set<std::pair<BasicBlock*, BasicBlock*> >::iterator |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 530 | ib = BackEdges.begin(), ie = BackEdges.end(); ib != ie; ++ib) |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 531 | ProcessBackEdge(ib->first, ib->second, F); |
| 532 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 533 | //oh, and add the edge from the reg2mem created entry node to the |
| 534 | //duplicated second node |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 535 | TerminatorInst* T = F.getEntryBlock().getTerminator(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 536 | ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0), |
| 537 | cast<BasicBlock>( |
| 538 | Translate(T->getSuccessor(0))), |
| 539 | ConstantInt::get(Type::Int1Ty, |
| 540 | true))); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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 | |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 548 | for (std::set<BasicBlock*>::iterator |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 549 | ii = ChoicePoints.begin(), ie = ChoicePoints.end(); ii != ie; ++ii) |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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: |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 563 | c = new GlobalRandomCounter(M, Type::Int32Ty, (1 << 14) - 1); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 564 | break; |
| 565 | case GBVO: |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 566 | c = new GlobalRandomCounterOpt(M, Type::Int32Ty, (1 << 14) - 1); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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)) { |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 598 | std::map<BasicBlock*, Value*> counter; |
| 599 | for(unsigned i = 0; i < phi->getNumIncomingValues(); ) { |
| 600 | if (counter[phi->getIncomingBlock(i)]) { |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 601 | assert(phi->getIncomingValue(i) == counter[phi->getIncomingBlock(i)]); |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 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); |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame] | 646 | DOUT << F.getName() << " " << BackEdges.size() << "\n"; |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | //Creation functions |
Andrew Lenharth | 701f5ac | 2005-11-28 00:58:09 +0000 | [diff] [blame] | 651 | ModulePass* llvm::createNullProfilerRSPass() { |
| 652 | return new NullProfilerRS(); |
| 653 | } |
| 654 | |
| 655 | FunctionPass* llvm::createRSProfilingPass() { |
| 656 | return new ProfilerRS(); |
| 657 | } |