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 "ProfilingUtils.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 #include "llvm/Transforms/Instrumentation.h"
32 class EdgeProfiler : public ModulePass {
33 bool runOnModule(Module &M);
36 RegisterPass<EdgeProfiler> X("insert-edge-profiling",
37 "Insert instrumentation for edge profiling");
40 ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
42 bool EdgeProfiler::runOnModule(Module &M) {
43 Function *Main = M.getMainFunction();
45 cerr << "WARNING: cannot insert edge profiling into a module"
46 << " with no main function!\n";
47 return false; // No main, no instrumentation!
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
57 BlocksToInstrument.insert(BB);
58 NumEdges += BB->getTerminator()->getNumSuccessors();
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);
66 // Instrument all of the edges...
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);
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
84 IncrementCounterInBlock(BB, i++, Counters);
86 // Insert counter at the start of the block
87 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
92 // Add the initialization call to main.
93 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);