blob: 4cf65a8b8c7ca58664c4283eed60e30bd19e4d59 [file] [log] [blame]
Chris Lattnerb2f12a22004-03-08 17:54:34 +00001//===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnerb2f12a22004-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnerb2f12a22004-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 Wendlinge8156192006-12-07 01:30:32 +000020#include "ProfilingUtils.h"
Chris Lattnerb2f12a22004-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 Spencer9133fe22007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Bill Wendling62c804a2006-11-26 09:17:06 +000026#include "llvm/Support/Streams.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000028#include "llvm/Transforms/Instrumentation.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000029#include <set>
30using namespace llvm;
31
32namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass {
Chris Lattnerb12914b2004-09-20 04:48:05 +000034 bool runOnModule(Module &M);
Devang Patel794fd752007-05-01 21:15:47 +000035 public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000037 EdgeProfiler() : ModulePass((intptr_t)&ID) {}
Chris Lattnerb2f12a22004-03-08 17:54:34 +000038 };
39
Devang Patel19974732007-05-03 01:11:54 +000040 char EdgeProfiler::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000041 RegisterPass<EdgeProfiler> X("insert-edge-profiling",
42 "Insert instrumentation for edge profiling");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000043}
44
Jeff Cohend9ed8c82005-01-07 06:57:28 +000045ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
46
Chris Lattnerb12914b2004-09-20 04:48:05 +000047bool EdgeProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000048 Function *Main = M.getFunction("main");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000049 if (Main == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000050 cerr << "WARNING: cannot insert edge profiling into a module"
51 << " with no main function!\n";
Chris Lattnerb2f12a22004-03-08 17:54:34 +000052 return false; // No main, no instrumentation!
53 }
54
55 std::set<BasicBlock*> BlocksToInstrument;
56 unsigned NumEdges = 0;
57 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
58 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
59 // Keep track of which blocks need to be instrumented. We don't want to
60 // instrument blocks that are added as the result of breaking critical
61 // edges!
62 BlocksToInstrument.insert(BB);
63 NumEdges += BB->getTerminator()->getNumSuccessors();
64 }
65
Reid Spencerc5b206b2006-12-31 05:48:39 +000066 const Type *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000067 GlobalVariable *Counters =
68 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
69 Constant::getNullValue(ATy), "EdgeProfCounters", &M);
70
Chris Lattnerb2f12a22004-03-08 17:54:34 +000071 // Instrument all of the edges...
72 unsigned i = 0;
73 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
74 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
75 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
76 // Okay, we have to add a counter of each outgoing edge. If the
77 // outgoing edge is not critical don't split it, just insert the counter
78 // in the source or destination of the edge.
79 TerminatorInst *TI = BB->getTerminator();
80 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
81 // If the edge is critical, split it.
82 SplitCriticalEdge(TI, s, this);
83
84 // Okay, we are guaranteed that the edge is no longer critical. If we
85 // only have a single successor, insert the counter in this block,
86 // otherwise insert it in the successor block.
87 if (TI->getNumSuccessors() == 0) {
88 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +000089 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000090 } else {
91 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +000092 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000093 }
94 }
95 }
96
97 // Add the initialization call to main.
98 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
99 return true;
100}
101