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