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