blob: a94c42dcaaf7a23f20f1f8ae9ffc57609d5a9a93 [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
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"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000025#include "llvm/Transforms/Instrumentation.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000026#include "ProfilingUtils.h"
Reid Spencer954da372004-07-04 12:19:56 +000027#include <iostream>
Chris Lattnerb2f12a22004-03-08 17:54:34 +000028#include <set>
29using namespace llvm;
30
31namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000032 class EdgeProfiler : public ModulePass {
33 bool runOnModule(Module &M);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000034 };
35
Chris Lattner7f8897f2006-08-27 22:42:52 +000036 RegisterPass<EdgeProfiler> X("insert-edge-profiling",
37 "Insert instrumentation for edge profiling");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000038}
39
Jeff Cohend9ed8c82005-01-07 06:57:28 +000040ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
41
Chris Lattnerb12914b2004-09-20 04:48:05 +000042bool EdgeProfiler::runOnModule(Module &M) {
Chris Lattnerb2f12a22004-03-08 17:54:34 +000043 Function *Main = M.getMainFunction();
44 if (Main == 0) {
45 std::cerr << "WARNING: cannot insert edge profiling into a module"
46 << " with no main function!\n";
47 return false; // No main, no instrumentation!
48 }
49
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
56 // edges!
57 BlocksToInstrument.insert(BB);
58 NumEdges += BB->getTerminator()->getNumSuccessors();
59 }
60
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);
65
Chris Lattnerb2f12a22004-03-08 17:54:34 +000066 // Instrument all of the edges...
67 unsigned i = 0;
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);
78
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
Reid Spencer518310c2004-07-18 00:44:37 +000084 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000085 } else {
86 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +000087 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000088 }
89 }
90 }
91
92 // Add the initialization call to main.
93 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
94 return true;
95}
96