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