blob: e45d2bfd151f9c37a4994c36b826037daa0461ae [file] [log] [blame]
Chris Lattner721264a2004-03-08 17:54:34 +00001//===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner721264a2004-03-08 17:54:34 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner721264a2004-03-08 17:54:34 +00008//===----------------------------------------------------------------------===//
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
Bill Wendlingf3baad32006-12-07 01:30:32 +000020#include "ProfilingUtils.h"
Chris Lattner721264a2004-03-08 17:54:34 +000021#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Module.h"
24#include "llvm/Pass.h"
Reid Spencer557ab152007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Bill Wendlinga7459ca2006-11-26 09:17:06 +000026#include "llvm/Support/Streams.h"
Chris Lattner721264a2004-03-08 17:54:34 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Jeff Cohen9a7ac162005-01-07 06:57:28 +000028#include "llvm/Transforms/Instrumentation.h"
Chris Lattner721264a2004-03-08 17:54:34 +000029#include <set>
30using namespace llvm;
31
32namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass {
Chris Lattner4f2cf032004-09-20 04:48:05 +000034 bool runOnModule(Module &M);
Chris Lattner721264a2004-03-08 17:54:34 +000035 };
36
Chris Lattnerc2d3d312006-08-27 22:42:52 +000037 RegisterPass<EdgeProfiler> X("insert-edge-profiling",
38 "Insert instrumentation for edge profiling");
Chris Lattner721264a2004-03-08 17:54:34 +000039}
40
Jeff Cohen9a7ac162005-01-07 06:57:28 +000041ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
42
Chris Lattner4f2cf032004-09-20 04:48:05 +000043bool EdgeProfiler::runOnModule(Module &M) {
Reid Spencer1241d6d2007-02-05 21:19:13 +000044 Function *Main = M.getFunction("main");
Chris Lattner721264a2004-03-08 17:54:34 +000045 if (Main == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000046 cerr << "WARNING: cannot insert edge profiling into a module"
47 << " with no main function!\n";
Chris Lattner721264a2004-03-08 17:54:34 +000048 return false; // No main, no instrumentation!
49 }
50
51 std::set<BasicBlock*> BlocksToInstrument;
52 unsigned NumEdges = 0;
53 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
54 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
55 // Keep track of which blocks need to be instrumented. We don't want to
56 // instrument blocks that are added as the result of breaking critical
57 // edges!
58 BlocksToInstrument.insert(BB);
59 NumEdges += BB->getTerminator()->getNumSuccessors();
60 }
61
Reid Spencerc635f472006-12-31 05:48:39 +000062 const Type *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
Chris Lattner721264a2004-03-08 17:54:34 +000063 GlobalVariable *Counters =
64 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
65 Constant::getNullValue(ATy), "EdgeProfCounters", &M);
66
Chris Lattner721264a2004-03-08 17:54:34 +000067 // Instrument all of the edges...
68 unsigned i = 0;
69 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
70 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
71 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
72 // Okay, we have to add a counter of each outgoing edge. If the
73 // outgoing edge is not critical don't split it, just insert the counter
74 // in the source or destination of the edge.
75 TerminatorInst *TI = BB->getTerminator();
76 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
77 // If the edge is critical, split it.
78 SplitCriticalEdge(TI, s, this);
79
80 // Okay, we are guaranteed that the edge is no longer critical. If we
81 // only have a single successor, insert the counter in this block,
82 // otherwise insert it in the successor block.
83 if (TI->getNumSuccessors() == 0) {
84 // Insert counter at the start of the block
Reid Spencercb3fb5d2004-07-18 00:44:37 +000085 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner721264a2004-03-08 17:54:34 +000086 } else {
87 // Insert counter at the start of the block
Reid Spencercb3fb5d2004-07-18 00:44:37 +000088 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
Chris Lattner721264a2004-03-08 17:54:34 +000089 }
90 }
91 }
92
93 // Add the initialization call to main.
94 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
95 return true;
96}
97