Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 1 | //===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass instruments the specified program with counters for edge profiling. |
| 11 | // Edge profiling can give a reasonable approximation of the hot paths through a |
| 12 | // program, and is used for a wide variety of program transformations. |
| 13 | // |
| 14 | // Note that this implementation is very naive. We insert a counter for *every* |
| 15 | // edge in the program, instead of using control flow information to prune the |
| 16 | // number of counters inserted. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
Andreas Neustifter | 62353a8 | 2009-09-01 10:08:39 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "insert-edge-profiling" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 20 | #include "ProfilingUtils.h" |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Pass.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Instrumentation.h" |
Andreas Neustifter | 62353a8 | 2009-09-01 10:08:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 28 | #include <set> |
| 29 | using namespace llvm; |
| 30 | |
Andreas Neustifter | 62353a8 | 2009-09-01 10:08:39 +0000 | [diff] [blame] | 31 | STATISTIC(NumEdgesInserted, "The # of edges inserted."); |
| 32 | |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 33 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 34 | class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 35 | bool runOnModule(Module &M); |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 36 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 37 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 38 | EdgeProfiler() : ModulePass(&ID) {} |
Andreas Neustifter | 62353a8 | 2009-09-01 10:08:39 +0000 | [diff] [blame] | 39 | |
| 40 | virtual const char *getPassName() const { |
| 41 | return "Edge Profiler"; |
| 42 | } |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 43 | }; |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 46 | char EdgeProfiler::ID = 0; |
| 47 | static RegisterPass<EdgeProfiler> |
| 48 | X("insert-edge-profiling", "Insert instrumentation for edge profiling"); |
| 49 | |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 50 | ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); } |
| 51 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 52 | bool EdgeProfiler::runOnModule(Module &M) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 53 | Function *Main = M.getFunction("main"); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 54 | if (Main == 0) { |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 55 | errs() << "WARNING: cannot insert edge profiling into a module" |
| 56 | << " with no main function!\n"; |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 57 | return false; // No main, no instrumentation! |
| 58 | } |
| 59 | |
| 60 | std::set<BasicBlock*> BlocksToInstrument; |
| 61 | unsigned NumEdges = 0; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 62 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 63 | if (F->isDeclaration()) continue; |
| 64 | // Reserve space for (0,entry) edge. |
| 65 | ++NumEdges; |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 66 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 67 | // Keep track of which blocks need to be instrumented. We don't want to |
| 68 | // instrument blocks that are added as the result of breaking critical |
| 69 | // edges! |
| 70 | BlocksToInstrument.insert(BB); |
| 71 | NumEdges += BB->getTerminator()->getNumSuccessors(); |
| 72 | } |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 74 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 75 | const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 76 | GlobalVariable *Counters = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 77 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 78 | Constant::getNullValue(ATy), "EdgeProfCounters"); |
Andreas Neustifter | 62353a8 | 2009-09-01 10:08:39 +0000 | [diff] [blame] | 79 | NumEdgesInserted = NumEdges; |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 80 | |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 81 | // Instrument all of the edges... |
| 82 | unsigned i = 0; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 83 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 84 | if (F->isDeclaration()) continue; |
| 85 | // Create counter for (0,entry) edge. |
| 86 | IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 87 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 88 | if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks |
| 89 | // Okay, we have to add a counter of each outgoing edge. If the |
| 90 | // outgoing edge is not critical don't split it, just insert the counter |
| 91 | // in the source or destination of the edge. |
| 92 | TerminatorInst *TI = BB->getTerminator(); |
| 93 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 94 | // If the edge is critical, split it. |
| 95 | SplitCriticalEdge(TI, s, this); |
| 96 | |
| 97 | // Okay, we are guaranteed that the edge is no longer critical. If we |
| 98 | // only have a single successor, insert the counter in this block, |
| 99 | // otherwise insert it in the successor block. |
Chris Lattner | 915c656 | 2007-12-13 00:04:46 +0000 | [diff] [blame] | 100 | if (TI->getNumSuccessors() == 1) { |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 101 | // Insert counter at the start of the block |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 102 | IncrementCounterInBlock(BB, i++, Counters); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 103 | } else { |
| 104 | // Insert counter at the start of the block |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 105 | IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 110 | |
| 111 | // Add the initialization call to main. |
| 112 | InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters); |
| 113 | return true; |
| 114 | } |
| 115 | |