blob: ef334f22f3353fcb91d1a37afa3ac44d26a350b5 [file] [log] [blame]
Chris Lattnerb2f12a22004-03-08 17:54:34 +00001//===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
2//
3// 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.
7//
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 "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"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
Chris Lattnerb2f12a22004-03-08 17:54:34 +000027#include <set>
28using namespace llvm;
29
30namespace {
31 class EdgeProfiler : public Pass {
32 bool run(Module &M);
33 };
34
35 RegisterOpt<EdgeProfiler> X("insert-edge-profiling",
36 "Insert instrumentation for edge profiling");
37}
38
39bool EdgeProfiler::run(Module &M) {
40 Function *Main = M.getMainFunction();
41 if (Main == 0) {
42 std::cerr << "WARNING: cannot insert edge profiling into a module"
43 << " with no main function!\n";
44 return false; // No main, no instrumentation!
45 }
46
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
53 // edges!
54 BlocksToInstrument.insert(BB);
55 NumEdges += BB->getTerminator()->getNumSuccessors();
56 }
57
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);
62
63 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
64
65 // Instrument all of the edges...
66 unsigned i = 0;
67 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
68 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
69 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
70 // Okay, we have to add a counter of each outgoing edge. If the
71 // outgoing edge is not critical don't split it, just insert the counter
72 // in the source or destination of the edge.
73 TerminatorInst *TI = BB->getTerminator();
74 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
75 // If the edge is critical, split it.
76 SplitCriticalEdge(TI, s, this);
77
78 // Okay, we are guaranteed that the edge is no longer critical. If we
79 // only have a single successor, insert the counter in this block,
80 // otherwise insert it in the successor block.
81 if (TI->getNumSuccessors() == 0) {
82 // Insert counter at the start of the block
83 IncrementCounterInBlock(BB, i++, CounterCPR);
84 } else {
85 // Insert counter at the start of the block
86 IncrementCounterInBlock(TI->getSuccessor(s), i++, CounterCPR);
87 }
88 }
89 }
90
91 // Add the initialization call to main.
92 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
93 return true;
94}
95