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 | //===----------------------------------------------------------------------===// |
| 19 | |
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/Constants.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Instrumentation.h" |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 29 | #include <set> |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 33 | class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 34 | bool runOnModule(Module &M); |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 35 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 36 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 37 | EdgeProfiler() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 40 | char EdgeProfiler::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 41 | RegisterPass<EdgeProfiler> X("insert-edge-profiling", |
| 42 | "Insert instrumentation for edge profiling"); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 45 | ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); } |
| 46 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 47 | bool EdgeProfiler::runOnModule(Module &M) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 48 | Function *Main = M.getFunction("main"); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 49 | if (Main == 0) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 50 | cerr << "WARNING: cannot insert edge profiling into a module" |
| 51 | << " with no main function!\n"; |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 52 | return false; // No main, no instrumentation! |
| 53 | } |
| 54 | |
| 55 | std::set<BasicBlock*> BlocksToInstrument; |
| 56 | unsigned NumEdges = 0; |
| 57 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 58 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 59 | // Keep track of which blocks need to be instrumented. We don't want to |
| 60 | // instrument blocks that are added as the result of breaking critical |
| 61 | // edges! |
| 62 | BlocksToInstrument.insert(BB); |
| 63 | NumEdges += BB->getTerminator()->getNumSuccessors(); |
| 64 | } |
| 65 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 66 | const Type *ATy = ArrayType::get(Type::Int32Ty, NumEdges); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 67 | GlobalVariable *Counters = |
| 68 | new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, |
| 69 | Constant::getNullValue(ATy), "EdgeProfCounters", &M); |
| 70 | |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 71 | // Instrument all of the edges... |
| 72 | unsigned i = 0; |
| 73 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 74 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 75 | if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks |
| 76 | // Okay, we have to add a counter of each outgoing edge. If the |
| 77 | // outgoing edge is not critical don't split it, just insert the counter |
| 78 | // in the source or destination of the edge. |
| 79 | TerminatorInst *TI = BB->getTerminator(); |
| 80 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 81 | // If the edge is critical, split it. |
| 82 | SplitCriticalEdge(TI, s, this); |
| 83 | |
| 84 | // Okay, we are guaranteed that the edge is no longer critical. If we |
| 85 | // only have a single successor, insert the counter in this block, |
| 86 | // otherwise insert it in the successor block. |
Chris Lattner | 915c656 | 2007-12-13 00:04:46 +0000 | [diff] [blame] | 87 | if (TI->getNumSuccessors() == 1) { |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 88 | // Insert counter at the start of the block |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 89 | IncrementCounterInBlock(BB, i++, Counters); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 90 | } else { |
| 91 | // Insert counter at the start of the block |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 92 | IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters); |
Chris Lattner | b2f12a2 | 2004-03-08 17:54:34 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Add the initialization call to main. |
| 98 | InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters); |
| 99 | return true; |
| 100 | } |
| 101 | |