1 //===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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.
18 //===----------------------------------------------------------------------===//
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"
25 #include "ProfilingUtils.h"
30 class EdgeProfiler : public Pass {
34 RegisterOpt<EdgeProfiler> X("insert-edge-profiling",
35 "Insert instrumentation for edge profiling");
38 bool EdgeProfiler::run(Module &M) {
39 Function *Main = M.getMainFunction();
41 std::cerr << "WARNING: cannot insert edge profiling into a module"
42 << " with no main function!\n";
43 return false; // No main, no instrumentation!
46 std::set<BasicBlock*> BlocksToInstrument;
47 unsigned NumEdges = 0;
48 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
49 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
50 // Keep track of which blocks need to be instrumented. We don't want to
51 // instrument blocks that are added as the result of breaking critical
53 BlocksToInstrument.insert(BB);
54 NumEdges += BB->getTerminator()->getNumSuccessors();
57 const Type *ATy = ArrayType::get(Type::UIntTy, NumEdges);
58 GlobalVariable *Counters =
59 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
60 Constant::getNullValue(ATy), "EdgeProfCounters", &M);
62 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
64 // Instrument all of the edges...
66 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
67 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
68 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
69 // Okay, we have to add a counter of each outgoing edge. If the
70 // outgoing edge is not critical don't split it, just insert the counter
71 // in the source or destination of the edge.
72 TerminatorInst *TI = BB->getTerminator();
73 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
74 // If the edge is critical, split it.
75 SplitCriticalEdge(TI, s, this);
77 // Okay, we are guaranteed that the edge is no longer critical. If we
78 // only have a single successor, insert the counter in this block,
79 // otherwise insert it in the successor block.
80 if (TI->getNumSuccessors() == 0) {
81 // Insert counter at the start of the block
82 IncrementCounterInBlock(BB, i++, CounterCPR);
84 // Insert counter at the start of the block
85 IncrementCounterInBlock(TI->getSuccessor(s), i++, CounterCPR);
90 // Add the initialization call to main.
91 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);