blob: ed9ff5087750410a75ddb06ff77e39294d193dcc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 "ProfilingUtils.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Module.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/Compiler.h"
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Transforms/Utils/BasicBlockUtils.h"
26#include "llvm/Transforms/Instrumentation.h"
27#include <set>
28using namespace llvm;
29
30namespace {
31 class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass {
32 bool runOnModule(Module &M);
33 public:
34 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000035 EdgeProfiler() : ModulePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037}
38
Dan Gohman089efff2008-05-13 00:00:25 +000039char EdgeProfiler::ID = 0;
40static RegisterPass<EdgeProfiler>
41X("insert-edge-profiling", "Insert instrumentation for edge profiling");
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
44
45bool EdgeProfiler::runOnModule(Module &M) {
46 Function *Main = M.getFunction("main");
47 if (Main == 0) {
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000048 errs() << "WARNING: cannot insert edge profiling into a module"
49 << " with no main function!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 return false; // No main, no instrumentation!
51 }
52
53 std::set<BasicBlock*> BlocksToInstrument;
54 unsigned NumEdges = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000055 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
56 if (F->isDeclaration()) continue;
57 // Reserve space for (0,entry) edge.
58 ++NumEdges;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
60 // Keep track of which blocks need to be instrumented. We don't want to
61 // instrument blocks that are added as the result of breaking critical
62 // edges!
63 BlocksToInstrument.insert(BB);
64 NumEdges += BB->getTerminator()->getNumSuccessors();
65 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +000066 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
Owen Anderson35b47072009-08-13 21:58:54 +000068 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 GlobalVariable *Counters =
Owen Andersone17fc1d2009-07-08 19:03:57 +000070 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersonaac28372009-07-31 20:28:14 +000071 Constant::getNullValue(ATy), "EdgeProfCounters");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
73 // Instrument all of the edges...
74 unsigned i = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000075 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
76 if (F->isDeclaration()) continue;
77 // Create counter for (0,entry) edge.
78 IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
80 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
81 // Okay, we have to add a counter of each outgoing edge. If the
82 // outgoing edge is not critical don't split it, just insert the counter
83 // in the source or destination of the edge.
84 TerminatorInst *TI = BB->getTerminator();
85 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
86 // If the edge is critical, split it.
87 SplitCriticalEdge(TI, s, this);
88
89 // Okay, we are guaranteed that the edge is no longer critical. If we
90 // only have a single successor, insert the counter in this block,
91 // otherwise insert it in the successor block.
Chris Lattnera0b4c942007-12-13 00:04:46 +000092 if (TI->getNumSuccessors() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 // Insert counter at the start of the block
94 IncrementCounterInBlock(BB, i++, Counters);
95 } else {
96 // Insert counter at the start of the block
97 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
98 }
99 }
100 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000101 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 // Add the initialization call to main.
104 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
105 return true;
106}
107