blob: b110f4eb368b6174e6bbf1b258e3b9ae95c7a47b [file] [log] [blame]
Andrew Lenharth701f5ac2005-11-28 00:58:09 +00001//===- RSProfiling.cpp - Various profiling using random sampling ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth701f5ac2005-11-28 00:58:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// These passes implement a random sampling based profiling. Different methods
11// of choosing when to sample are supported, as well as different types of
12// profiling. This is done as two passes. The first is a sequence of profiling
Andrew Lenharthbb227c12005-11-28 18:00:38 +000013// passes which insert profiling into the program, and remember what they
14// inserted.
15//
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000016// 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 Henriksen55cbec32007-10-26 03:03:51 +000021// It is highly recommended that after this pass one runs mem2reg and adce
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000022// (instcombine load-vn gdce dse also are good to run afterwards)
23//
24// This design is intended to make the profiling passes independent of the RS
25// framework, but any profiling pass that implements the RSProfiling interface
26// is compatible with the rs framework (and thus can be sampled)
27//
28// TODO: obviously the block and function profiling are almost identical to the
29// existing ones, so they can be unified (esp since these passes are valid
30// without the rs framework).
31// TODO: Fix choice code so that frequency is not hard coded
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/Pass.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000036#include "llvm/Module.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000037#include "llvm/Instructions.h"
38#include "llvm/Constants.h"
39#include "llvm/DerivedTypes.h"
Duncan Sandse2c43042008-04-07 13:45:04 +000040#include "llvm/Intrinsics.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000041#include "llvm/Transforms/Scalar.h"
42#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000043#include "llvm/Support/CommandLine.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000044#include "llvm/Support/Compiler.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000045#include "llvm/Support/Debug.h"
46#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000047#include "RSProfiling.h"
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000048#include <set>
49#include <map>
50#include <queue>
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000051using namespace llvm;
52
53namespace {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000054 enum RandomMeth {
55 GBV, GBVO, HOSTCC
56 };
Dan Gohman844731a2008-05-13 00:00:25 +000057}
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000058
Dan Gohman844731a2008-05-13 00:00:25 +000059static cl::opt<RandomMeth> RandomMethod("profile-randomness",
60 cl::desc("How to randomly choose to profile:"),
61 cl::values(
62 clEnumValN(GBV, "global", "global counter"),
63 clEnumValN(GBVO, "ra_global",
64 "register allocated global counter"),
65 clEnumValN(HOSTCC, "rdcc", "cycle counter"),
66 clEnumValEnd));
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000067
Dan Gohman844731a2008-05-13 00:00:25 +000068namespace {
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000069 /// NullProfilerRS - The basic profiler that does nothing. It is the default
70 /// profiler and thus terminates RSProfiler chains. It is useful for
71 /// measuring framework overhead
Reid Spencer9133fe22007-02-05 23:32:05 +000072 class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000073 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000074 static char ID; // Pass identification, replacement for typeid
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000075 bool isProfiling(Value* v) {
76 return false;
77 }
78 bool runOnModule(Module &M) {
79 return false;
80 }
81 void getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.setPreservesAll();
83 }
84 };
Dan Gohman844731a2008-05-13 00:00:25 +000085}
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000086
Dan Gohman844731a2008-05-13 00:00:25 +000087static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
88static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
89 "Measure profiling framework overhead");
90static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000091
Dan Gohman844731a2008-05-13 00:00:25 +000092namespace {
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000093 /// Chooser - Something that chooses when to make a sample of the profiled code
Reid Spencer9133fe22007-02-05 23:32:05 +000094 class VISIBILITY_HIDDEN Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000095 public:
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000096 /// ProcessChoicePoint - is called for each basic block inserted to choose
97 /// between normal and sample code
Andrew Lenharth701f5ac2005-11-28 00:58:09 +000098 virtual void ProcessChoicePoint(BasicBlock*) = 0;
Andrew Lenharth8dc2d502005-11-28 18:10:59 +000099 /// PrepFunction - is called once per function before other work is done.
100 /// This gives the opertunity to insert new allocas and such.
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000101 virtual void PrepFunction(Function*) = 0;
102 virtual ~Chooser() {}
103 };
104
105 //Things that implement sampling policies
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000106 //A global value that is read-mod-stored to choose when to sample.
107 //A sample is taken when the global counter hits 0
Reid Spencer9133fe22007-02-05 23:32:05 +0000108 class VISIBILITY_HIDDEN GlobalRandomCounter : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000109 GlobalVariable* Counter;
110 Value* ResetValue;
Dan Gohman6de29f82009-06-15 22:12:54 +0000111 const IntegerType* T;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000112 public:
Dan Gohman6de29f82009-06-15 22:12:54 +0000113 GlobalRandomCounter(Module& M, const IntegerType* t, uint64_t resetval);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000114 virtual ~GlobalRandomCounter();
115 virtual void PrepFunction(Function* F);
116 virtual void ProcessChoicePoint(BasicBlock* bb);
117 };
118
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000119 //Same is GRC, but allow register allocation of the global counter
Reid Spencer9133fe22007-02-05 23:32:05 +0000120 class VISIBILITY_HIDDEN GlobalRandomCounterOpt : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000121 GlobalVariable* Counter;
122 Value* ResetValue;
123 AllocaInst* AI;
Dan Gohman6de29f82009-06-15 22:12:54 +0000124 const IntegerType* T;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000125 public:
Dan Gohman6de29f82009-06-15 22:12:54 +0000126 GlobalRandomCounterOpt(Module& M, const IntegerType* t, uint64_t resetval);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000127 virtual ~GlobalRandomCounterOpt();
128 virtual void PrepFunction(Function* F);
129 virtual void ProcessChoicePoint(BasicBlock* bb);
130 };
131
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000132 //Use the cycle counter intrinsic as a source of pseudo randomness when
133 //deciding when to sample.
Reid Spencer9133fe22007-02-05 23:32:05 +0000134 class VISIBILITY_HIDDEN CycleCounter : public Chooser {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000135 uint64_t rm;
Chris Lattnerfebe5f12007-01-07 07:22:20 +0000136 Constant *F;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000137 public:
138 CycleCounter(Module& m, uint64_t resetmask);
139 virtual ~CycleCounter();
140 virtual void PrepFunction(Function* F);
141 virtual void ProcessChoicePoint(BasicBlock* bb);
142 };
143
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000144 /// ProfilerRS - Insert the random sampling framework
Reid Spencer9133fe22007-02-05 23:32:05 +0000145 struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000146 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000147 ProfilerRS() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000148
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000149 std::map<Value*, Value*> TransCache;
150 std::set<BasicBlock*> ChoicePoints;
151 Chooser* c;
152
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000153 //Translate and duplicate values for the new profile free version of stuff
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000154 Value* Translate(Value* v);
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000155 //Duplicate an entire function (with out profiling)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000156 void Duplicate(Function& F, RSProfilers& LI);
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000157 //Called once for each backedge, handle the insertion of choice points and
158 //the interconection of the two versions of the code
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000159 void ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F);
160 bool runOnFunction(Function& F);
161 bool doInitialization(Module &M);
162 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
163 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000164}
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000165
Dan Gohman844731a2008-05-13 00:00:25 +0000166static RegisterPass<ProfilerRS>
167X("insert-rs-profiling-framework",
168 "Insert random sampling instrumentation framework");
169
Devang Patel19974732007-05-03 01:11:54 +0000170char RSProfilers::ID = 0;
171char NullProfilerRS::ID = 0;
172char ProfilerRS::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000173
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000174//Local utilities
175static void ReplacePhiPred(BasicBlock* btarget,
176 BasicBlock* bold, BasicBlock* bnew);
177
178static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc);
179
180template<class T>
181static void recBackEdge(BasicBlock* bb, T& BackEdges,
182 std::map<BasicBlock*, int>& color,
183 std::map<BasicBlock*, int>& depth,
184 std::map<BasicBlock*, int>& finish,
185 int& time);
186
187//find the back edges and where they go to
188template<class T>
189static void getBackEdges(Function& F, T& BackEdges);
190
191
192///////////////////////////////////////
193// Methods of choosing when to profile
194///////////////////////////////////////
195
Dan Gohman6de29f82009-06-15 22:12:54 +0000196GlobalRandomCounter::GlobalRandomCounter(Module& M, const IntegerType* t,
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000197 uint64_t resetval) : T(t) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000198 ConstantInt* Init = ConstantInt::get(T, resetval);
199 ResetValue = Init;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000200 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencerb83eb642006-10-20 07:07:24 +0000201 Init, "RandomSteeringCounter", &M);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000202}
203
204GlobalRandomCounter::~GlobalRandomCounter() {}
205
206void GlobalRandomCounter::PrepFunction(Function* F) {}
207
208void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
209 BranchInst* t = cast<BranchInst>(bb->getTerminator());
210
211 //decrement counter
212 LoadInst* l = new LoadInst(Counter, "counter", t);
213
Reid Spencere4d87aa2006-12-23 06:05:41 +0000214 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
215 "countercc", t);
216
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000217 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000218 "counternew", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000219 new StoreInst(nv, Counter, t);
220 t->setCondition(s);
221
222 //reset counter
223 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greif051a9502008-04-06 20:25:17 +0000224 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
225 oldnext);
226 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000227 t->setSuccessor(0, resetblock);
228 new StoreInst(ResetValue, Counter, t2);
229 ReplacePhiPred(oldnext, bb, resetblock);
230}
231
Dan Gohman6de29f82009-06-15 22:12:54 +0000232GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const IntegerType* t,
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000233 uint64_t resetval)
234 : AI(0), T(t) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000235 ConstantInt* Init = ConstantInt::get(T, resetval);
236 ResetValue = Init;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000237 Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage,
Reid Spencerb83eb642006-10-20 07:07:24 +0000238 Init, "RandomSteeringCounter", &M);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000239}
240
241GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {}
242
243void GlobalRandomCounterOpt::PrepFunction(Function* F) {
244 //make a local temporary to cache the global
245 BasicBlock& bb = F->getEntryBlock();
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000246 BasicBlock::iterator InsertPt = bb.begin();
247 AI = new AllocaInst(T, 0, "localcounter", InsertPt);
248 LoadInst* l = new LoadInst(Counter, "counterload", InsertPt);
249 new StoreInst(l, AI, InsertPt);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000250
Andrew Lenharth8dc2d502005-11-28 18:10:59 +0000251 //modify all functions and return values to restore the local variable to/from
252 //the global variable
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000253 for(Function::iterator fib = F->begin(), fie = F->end();
254 fib != fie; ++fib)
255 for(BasicBlock::iterator bib = fib->begin(), bie = fib->end();
256 bib != bie; ++bib)
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000257 if (isa<CallInst>(bib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000258 LoadInst* l = new LoadInst(AI, "counter", bib);
259 new StoreInst(l, Counter, bib);
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000260 l = new LoadInst(Counter, "counter", ++bib);
261 new StoreInst(l, AI, bib--);
262 } else if (isa<InvokeInst>(bib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000263 LoadInst* l = new LoadInst(AI, "counter", bib);
264 new StoreInst(l, Counter, bib);
265
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000266 BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest();
Dan Gohman02dea8b2008-05-23 21:05:58 +0000267 BasicBlock::iterator i = bb->getFirstNonPHI();
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000268 l = new LoadInst(Counter, "counter", i);
269
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000270 bb = cast<InvokeInst>(bib)->getUnwindDest();
Dan Gohman02dea8b2008-05-23 21:05:58 +0000271 i = bb->getFirstNonPHI();
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000272 l = new LoadInst(Counter, "counter", i);
Chris Lattnera0e1b0e2007-04-17 17:51:03 +0000273 new StoreInst(l, AI, i);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000274 } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
275 LoadInst* l = new LoadInst(AI, "counter", bib);
276 new StoreInst(l, Counter, bib);
277 }
278}
279
280void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
281 BranchInst* t = cast<BranchInst>(bb->getTerminator());
282
283 //decrement counter
284 LoadInst* l = new LoadInst(AI, "counter", t);
285
Reid Spencere4d87aa2006-12-23 06:05:41 +0000286 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
287 "countercc", t);
288
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000289 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000290 "counternew", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000291 new StoreInst(nv, AI, t);
292 t->setCondition(s);
293
294 //reset counter
295 BasicBlock* oldnext = t->getSuccessor(0);
Gabor Greif051a9502008-04-06 20:25:17 +0000296 BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(),
297 oldnext);
298 TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000299 t->setSuccessor(0, resetblock);
300 new StoreInst(ResetValue, AI, t2);
301 ReplacePhiPred(oldnext, bb, resetblock);
302}
303
304
305CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) {
Duncan Sandse2c43042008-04-07 13:45:04 +0000306 F = Intrinsic::getDeclaration(&m, Intrinsic::readcyclecounter);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000307}
308
309CycleCounter::~CycleCounter() {}
310
311void CycleCounter::PrepFunction(Function* F) {}
312
313void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
314 BranchInst* t = cast<BranchInst>(bb->getTerminator());
315
Gabor Greif051a9502008-04-06 20:25:17 +0000316 CallInst* c = CallInst::Create(F, "rdcc", t);
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000317 BinaryOperator* b =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000318 BinaryOperator::CreateAnd(c, ConstantInt::get(Type::Int64Ty, rm),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000319 "mrdcc", t);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000320
Reid Spencere4d87aa2006-12-23 06:05:41 +0000321 ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
Reid Spencerc5b206b2006-12-31 05:48:39 +0000322 ConstantInt::get(Type::Int64Ty, 0),
Reid Spencere4d87aa2006-12-23 06:05:41 +0000323 "mrdccc", t);
324
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000325 t->setCondition(s);
326}
327
328///////////////////////////////////////
329// Profiling:
330///////////////////////////////////////
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000331bool RSProfilers_std::isProfiling(Value* v) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000332 if (profcode.find(v) != profcode.end())
333 return true;
334 //else
335 RSProfilers& LI = getAnalysis<RSProfilers>();
336 return LI.isProfiling(v);
337}
338
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000339void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000340 GlobalValue *CounterArray) {
341 // Insert the increment after any alloca or PHI instructions...
Dan Gohman02dea8b2008-05-23 21:05:58 +0000342 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
343 while (isa<AllocaInst>(InsertPos))
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000344 ++InsertPos;
345
346 // Create the getelementptr constant expression
347 std::vector<Constant*> Indices(2);
Reid Spencerc5b206b2006-12-31 05:48:39 +0000348 Indices[0] = Constant::getNullValue(Type::Int32Ty);
349 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000350 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray,
351 &Indices[0], 2);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000352
353 // Load, increment and store the value back.
354 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
355 profcode.insert(OldVal);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000356 Value *NewVal = BinaryOperator::CreateAdd(OldVal,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000357 ConstantInt::get(Type::Int32Ty, 1),
358 "NewCounter", InsertPos);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000359 profcode.insert(NewVal);
360 profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos));
361}
362
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000363void RSProfilers_std::getAnalysisUsage(AnalysisUsage &AU) const {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000364 //grab any outstanding profiler, or get the null one
365 AU.addRequired<RSProfilers>();
366}
367
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000368///////////////////////////////////////
369// RS Framework
370///////////////////////////////////////
371
372Value* ProfilerRS::Translate(Value* v) {
373 if(TransCache[v])
374 return TransCache[v];
375
376 if (BasicBlock* bb = dyn_cast<BasicBlock>(v)) {
377 if (bb == &bb->getParent()->getEntryBlock())
378 TransCache[bb] = bb; //don't translate entry block
379 else
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000380 TransCache[bb] = BasicBlock::Create("dup_" + bb->getName(),
381 bb->getParent(), NULL);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000382 return TransCache[bb];
383 } else if (Instruction* i = dyn_cast<Instruction>(v)) {
384 //we have already translated this
385 //do not translate entry block allocas
386 if(&i->getParent()->getParent()->getEntryBlock() == i->getParent()) {
387 TransCache[i] = i;
388 return i;
389 } else {
390 //translate this
391 Instruction* i2 = i->clone();
392 if (i->hasName())
393 i2->setName("dup_" + i->getName());
394 TransCache[i] = i2;
395 //NumNewInst++;
396 for (unsigned x = 0; x < i2->getNumOperands(); ++x)
397 i2->setOperand(x, Translate(i2->getOperand(x)));
398 return i2;
399 }
400 } else if (isa<Function>(v) || isa<Constant>(v) || isa<Argument>(v)) {
401 TransCache[v] = v;
402 return v;
403 }
404 assert(0 && "Value not handled");
Jeff Cohen3523f6e2005-11-28 06:45:57 +0000405 return 0;
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000406}
407
408void ProfilerRS::Duplicate(Function& F, RSProfilers& LI)
409{
410 //perform a breadth first search, building up a duplicate of the code
411 std::queue<BasicBlock*> worklist;
412 std::set<BasicBlock*> seen;
413
414 //This loop ensures proper BB order, to help performance
415 for (Function::iterator fib = F.begin(), fie = F.end(); fib != fie; ++fib)
416 worklist.push(fib);
417 while (!worklist.empty()) {
418 Translate(worklist.front());
419 worklist.pop();
420 }
421
422 //remember than reg2mem created a new entry block we don't want to duplicate
423 worklist.push(F.getEntryBlock().getTerminator()->getSuccessor(0));
424 seen.insert(&F.getEntryBlock());
425
426 while (!worklist.empty()) {
427 BasicBlock* bb = worklist.front();
428 worklist.pop();
429 if(seen.find(bb) == seen.end()) {
430 BasicBlock* bbtarget = cast<BasicBlock>(Translate(bb));
431 BasicBlock::InstListType& instlist = bbtarget->getInstList();
432 for (BasicBlock::iterator iib = bb->begin(), iie = bb->end();
433 iib != iie; ++iib) {
434 //NumOldInst++;
435 if (!LI.isProfiling(&*iib)) {
436 Instruction* i = cast<Instruction>(Translate(iib));
437 instlist.insert(bbtarget->end(), i);
438 }
439 }
440 //updated search state;
441 seen.insert(bb);
442 TerminatorInst* ti = bb->getTerminator();
443 for (unsigned x = 0; x < ti->getNumSuccessors(); ++x) {
444 BasicBlock* bbs = ti->getSuccessor(x);
445 if (seen.find(bbs) == seen.end()) {
446 worklist.push(bbs);
447 }
448 }
449 }
450 }
451}
452
453void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F) {
454 //given a backedge from B -> A, and translations A' and B',
455 //a: insert C and C'
456 //b: add branches in C to A and A' and in C' to A and A'
457 //c: mod terminators@B, replace A with C
458 //d: mod terminators@B', replace A' with C'
459 //e: mod phis@A for pred B to be pred C
460 // if multiple entries, simplify to one
461 //f: mod phis@A' for pred B' to be pred C'
462 // if multiple entries, simplify to one
463 //g: for all phis@A with pred C using x
464 // add in edge from C' using x'
465 // add in edge from C using x in A'
466
467 //a:
Chris Lattnere24c92a2007-04-17 17:54:12 +0000468 Function::iterator BBN = src; ++BBN;
Gabor Greif051a9502008-04-06 20:25:17 +0000469 BasicBlock* bbC = BasicBlock::Create("choice", &F, BBN);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000470 //ChoicePoints.insert(bbC);
Chris Lattnere24c92a2007-04-17 17:54:12 +0000471 BBN = cast<BasicBlock>(Translate(src));
Gabor Greif051a9502008-04-06 20:25:17 +0000472 BasicBlock* bbCp = BasicBlock::Create("choice", &F, ++BBN);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000473 ChoicePoints.insert(bbCp);
474
475 //b:
Gabor Greif051a9502008-04-06 20:25:17 +0000476 BranchInst::Create(cast<BasicBlock>(Translate(dst)), bbC);
477 BranchInst::Create(dst, cast<BasicBlock>(Translate(dst)),
478 ConstantInt::get(Type::Int1Ty, true), bbCp);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000479 //c:
480 {
481 TerminatorInst* iB = src->getTerminator();
482 for (unsigned x = 0; x < iB->getNumSuccessors(); ++x)
483 if (iB->getSuccessor(x) == dst)
484 iB->setSuccessor(x, bbC);
485 }
486 //d:
487 {
488 TerminatorInst* iBp = cast<TerminatorInst>(Translate(src->getTerminator()));
489 for (unsigned x = 0; x < iBp->getNumSuccessors(); ++x)
490 if (iBp->getSuccessor(x) == cast<BasicBlock>(Translate(dst)))
491 iBp->setSuccessor(x, bbCp);
492 }
493 //e:
494 ReplacePhiPred(dst, src, bbC);
495 //src could be a switch, in which case we are replacing several edges with one
496 //thus collapse those edges int the Phi
497 CollapsePhi(dst, bbC);
498 //f:
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000499 ReplacePhiPred(cast<BasicBlock>(Translate(dst)),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000500 cast<BasicBlock>(Translate(src)),bbCp);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000501 CollapsePhi(cast<BasicBlock>(Translate(dst)), bbCp);
502 //g:
503 for(BasicBlock::iterator ib = dst->begin(), ie = dst->end(); ib != ie;
504 ++ib)
505 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
506 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
507 if(bbC == phi->getIncomingBlock(x)) {
508 phi->addIncoming(Translate(phi->getIncomingValue(x)), bbCp);
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000509 cast<PHINode>(Translate(phi))->addIncoming(phi->getIncomingValue(x),
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000510 bbC);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000511 }
512 phi->removeIncomingValue(bbC);
513 }
514}
515
516bool ProfilerRS::runOnFunction(Function& F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000517 if (!F.isDeclaration()) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000518 std::set<std::pair<BasicBlock*, BasicBlock*> > BackEdges;
519 RSProfilers& LI = getAnalysis<RSProfilers>();
520
521 getBackEdges(F, BackEdges);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000522 Duplicate(F, LI);
523 //assume that stuff worked. now connect the duplicated basic blocks
524 //with the originals in such a way as to preserve ssa. yuk!
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000525 for (std::set<std::pair<BasicBlock*, BasicBlock*> >::iterator
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000526 ib = BackEdges.begin(), ie = BackEdges.end(); ib != ie; ++ib)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000527 ProcessBackEdge(ib->first, ib->second, F);
528
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000529 //oh, and add the edge from the reg2mem created entry node to the
530 //duplicated second node
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000531 TerminatorInst* T = F.getEntryBlock().getTerminator();
Gabor Greif051a9502008-04-06 20:25:17 +0000532 ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0),
533 cast<BasicBlock>(
534 Translate(T->getSuccessor(0))),
535 ConstantInt::get(Type::Int1Ty,
536 true)));
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000537
538 //do whatever is needed now that the function is duplicated
539 c->PrepFunction(&F);
540
541 //add entry node to choice points
542 ChoicePoints.insert(&F.getEntryBlock());
543
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000544 for (std::set<BasicBlock*>::iterator
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000545 ii = ChoicePoints.begin(), ie = ChoicePoints.end(); ii != ie; ++ii)
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000546 c->ProcessChoicePoint(*ii);
547
548 ChoicePoints.clear();
549 TransCache.clear();
550
551 return true;
552 }
553 return false;
554}
555
556bool ProfilerRS::doInitialization(Module &M) {
557 switch (RandomMethod) {
558 case GBV:
Reid Spencerc5b206b2006-12-31 05:48:39 +0000559 c = new GlobalRandomCounter(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000560 break;
561 case GBVO:
Reid Spencerc5b206b2006-12-31 05:48:39 +0000562 c = new GlobalRandomCounterOpt(M, Type::Int32Ty, (1 << 14) - 1);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000563 break;
564 case HOSTCC:
565 c = new CycleCounter(M, (1 << 14) - 1);
566 break;
567 };
568 return true;
569}
570
571void ProfilerRS::getAnalysisUsage(AnalysisUsage &AU) const {
572 AU.addRequired<RSProfilers>();
573 AU.addRequiredID(DemoteRegisterToMemoryID);
574}
575
576///////////////////////////////////////
577// Utilities:
578///////////////////////////////////////
579static void ReplacePhiPred(BasicBlock* btarget,
580 BasicBlock* bold, BasicBlock* bnew) {
581 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
582 ib != ie; ++ib)
583 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
584 for(unsigned x = 0; x < phi->getNumIncomingValues(); ++x)
585 if(bold == phi->getIncomingBlock(x))
586 phi->setIncomingBlock(x, bnew);
587 }
588}
589
590static void CollapsePhi(BasicBlock* btarget, BasicBlock* bsrc) {
591 for(BasicBlock::iterator ib = btarget->begin(), ie = btarget->end();
592 ib != ie; ++ib)
593 if (PHINode* phi = dyn_cast<PHINode>(&*ib)) {
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000594 std::map<BasicBlock*, Value*> counter;
595 for(unsigned i = 0; i < phi->getNumIncomingValues(); ) {
596 if (counter[phi->getIncomingBlock(i)]) {
Andrew Lenharthbb227c12005-11-28 18:00:38 +0000597 assert(phi->getIncomingValue(i) == counter[phi->getIncomingBlock(i)]);
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000598 phi->removeIncomingValue(i, false);
599 } else {
600 counter[phi->getIncomingBlock(i)] = phi->getIncomingValue(i);
601 ++i;
602 }
603 }
604 }
605}
606
607template<class T>
608static void recBackEdge(BasicBlock* bb, T& BackEdges,
609 std::map<BasicBlock*, int>& color,
610 std::map<BasicBlock*, int>& depth,
611 std::map<BasicBlock*, int>& finish,
612 int& time)
613{
614 color[bb] = 1;
615 ++time;
616 depth[bb] = time;
617 TerminatorInst* t= bb->getTerminator();
618 for(unsigned i = 0; i < t->getNumSuccessors(); ++i) {
619 BasicBlock* bbnew = t->getSuccessor(i);
620 if (color[bbnew] == 0)
621 recBackEdge(bbnew, BackEdges, color, depth, finish, time);
622 else if (color[bbnew] == 1) {
623 BackEdges.insert(std::make_pair(bb, bbnew));
624 //NumBackEdges++;
625 }
626 }
627 color[bb] = 2;
628 ++time;
629 finish[bb] = time;
630}
631
632
633
634//find the back edges and where they go to
635template<class T>
636static void getBackEdges(Function& F, T& BackEdges) {
637 std::map<BasicBlock*, int> color;
638 std::map<BasicBlock*, int> depth;
639 std::map<BasicBlock*, int> finish;
640 int time = 0;
641 recBackEdge(&F.getEntryBlock(), BackEdges, color, depth, finish, time);
Bill Wendling62c804a2006-11-26 09:17:06 +0000642 DOUT << F.getName() << " " << BackEdges.size() << "\n";
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000643}
644
645
646//Creation functions
Andrew Lenharth701f5ac2005-11-28 00:58:09 +0000647ModulePass* llvm::createNullProfilerRSPass() {
648 return new NullProfilerRS();
649}
650
651FunctionPass* llvm::createRSProfilingPass() {
652 return new ProfilerRS();
653}