blob: b625341273a8dc98a3a346207eea8f576d0e981e [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Owen Anderson50895512009-07-06 18:42:36 +000023#include "llvm/LLVMContext.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000024#include "llvm/Module.h"
25#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000026#include "llvm/Support/Compiler.h"
Bill Wendling62c804a2006-11-26 09:17:06 +000027#include "llvm/Support/Streams.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000028#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000029#include "llvm/Transforms/Instrumentation.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000030#include <set>
31using namespace llvm;
32
33namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000034 class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass {
Chris Lattnerb12914b2004-09-20 04:48:05 +000035 bool runOnModule(Module &M);
Devang Patel794fd752007-05-01 21:15:47 +000036 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000038 EdgeProfiler() : ModulePass(&ID) {}
Chris Lattnerb2f12a22004-03-08 17:54:34 +000039 };
Chris Lattnerb2f12a22004-03-08 17:54:34 +000040}
41
Dan Gohman844731a2008-05-13 00:00:25 +000042char EdgeProfiler::ID = 0;
43static RegisterPass<EdgeProfiler>
44X("insert-edge-profiling", "Insert instrumentation for edge profiling");
45
Jeff Cohend9ed8c82005-01-07 06:57:28 +000046ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
47
Chris Lattnerb12914b2004-09-20 04:48:05 +000048bool EdgeProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000049 Function *Main = M.getFunction("main");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000050 if (Main == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000051 cerr << "WARNING: cannot insert edge profiling into a module"
52 << " with no main function!\n";
Chris Lattnerb2f12a22004-03-08 17:54:34 +000053 return false; // No main, no instrumentation!
54 }
55
56 std::set<BasicBlock*> BlocksToInstrument;
57 unsigned NumEdges = 0;
58 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
59 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 }
66
Owen Anderson50895512009-07-06 18:42:36 +000067 const Type *ATy = Context->getArrayType(Type::Int32Ty, NumEdges);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000068 GlobalVariable *Counters =
Owen Anderson3d29df32009-07-08 01:26:06 +000069 new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage,
Owen Anderson50895512009-07-06 18:42:36 +000070 Context->getNullValue(ATy), "EdgeProfCounters", &M);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000071
Chris Lattnerb2f12a22004-03-08 17:54:34 +000072 // Instrument all of the edges...
73 unsigned i = 0;
74 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
75 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
76 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
77 // Okay, we have to add a counter of each outgoing edge. If the
78 // outgoing edge is not critical don't split it, just insert the counter
79 // in the source or destination of the edge.
80 TerminatorInst *TI = BB->getTerminator();
81 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
82 // If the edge is critical, split it.
83 SplitCriticalEdge(TI, s, this);
84
85 // Okay, we are guaranteed that the edge is no longer critical. If we
86 // only have a single successor, insert the counter in this block,
87 // otherwise insert it in the successor block.
Chris Lattner915c6562007-12-13 00:04:46 +000088 if (TI->getNumSuccessors() == 1) {
Chris Lattnerb2f12a22004-03-08 17:54:34 +000089 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +000090 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000091 } else {
92 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +000093 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000094 }
95 }
96 }
97
98 // Add the initialization call to main.
99 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
100 return true;
101}
102