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"
31 class EdgeProfiler : public ModulePass {
32 bool runOnModule(Module &M);
35 RegisterOpt<EdgeProfiler> X("insert-edge-profiling",
36 "Insert instrumentation for edge profiling");
39 bool EdgeProfiler::runOnModule(Module &M) {
40 Function *Main = M.getMainFunction();
42 std::cerr << "WARNING: cannot insert edge profiling into a module"
43 << " with no main function!\n";
44 return false; // No main, no instrumentation!
47 std::set<BasicBlock*> BlocksToInstrument;
48 unsigned NumEdges = 0;
49 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
50 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
51 // Keep track of which blocks need to be instrumented. We don't want to
52 // instrument blocks that are added as the result of breaking critical
54 BlocksToInstrument.insert(BB);
55 NumEdges += BB->getTerminator()->getNumSuccessors();
58 const Type *ATy = ArrayType::get(Type::UIntTy, NumEdges);
59 GlobalVariable *Counters =
60 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
61 Constant::getNullValue(ATy), "EdgeProfCounters", &M);
63 // Instrument all of the edges...
65 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
66 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
67 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
68 // Okay, we have to add a counter of each outgoing edge. If the
69 // outgoing edge is not critical don't split it, just insert the counter
70 // in the source or destination of the edge.
71 TerminatorInst *TI = BB->getTerminator();
72 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
73 // If the edge is critical, split it.
74 SplitCriticalEdge(TI, s, this);
76 // Okay, we are guaranteed that the edge is no longer critical. If we
77 // only have a single successor, insert the counter in this block,
78 // otherwise insert it in the successor block.
79 if (TI->getNumSuccessors() == 0) {
80 // Insert counter at the start of the block
81 IncrementCounterInBlock(BB, i++, Counters);
83 // Insert counter at the start of the block
84 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
89 // Add the initialization call to main.
90 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);