blob: cf12b1f75873f6f11543a558edf9d4561c59bab6 [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//===----------------------------------------------------------------------===//
Andreas Neustifter62353a82009-09-01 10:08:39 +000019#define DEBUG_TYPE "insert-edge-profiling"
Andrew Trick04317cc2011-01-29 01:09:53 +000020
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Transforms/Instrumentation.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000022#include "ProfilingUtils.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ADT/Statistic.h"
Bob Wilsona0be09f2012-12-24 18:15:21 +000024#include "llvm/LLVMContext.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000025#include "llvm/Module.h"
26#include "llvm/Pass.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000028#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerb2f12a22004-03-08 17:54:34 +000029#include <set>
30using namespace llvm;
31
Andreas Neustifter62353a82009-09-01 10:08:39 +000032STATISTIC(NumEdgesInserted, "The # of edges inserted.");
33
Chris Lattnerb2f12a22004-03-08 17:54:34 +000034namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000035 class EdgeProfiler : public ModulePass {
Chris Lattnerb12914b2004-09-20 04:48:05 +000036 bool runOnModule(Module &M);
Devang Patel794fd752007-05-01 21:15:47 +000037 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000039 EdgeProfiler() : ModulePass(ID) {
40 initializeEdgeProfilerPass(*PassRegistry::getPassRegistry());
41 }
Andreas Neustifter62353a82009-09-01 10:08:39 +000042
43 virtual const char *getPassName() const {
44 return "Edge Profiler";
45 }
Chris Lattnerb2f12a22004-03-08 17:54:34 +000046 };
Chris Lattnerb2f12a22004-03-08 17:54:34 +000047}
48
Dan Gohman844731a2008-05-13 00:00:25 +000049char EdgeProfiler::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000050INITIALIZE_PASS(EdgeProfiler, "insert-edge-profiling",
Owen Andersonce665bd2010-10-07 22:25:06 +000051 "Insert instrumentation for edge profiling", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000052
Jeff Cohend9ed8c82005-01-07 06:57:28 +000053ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
54
Chris Lattnerb12914b2004-09-20 04:48:05 +000055bool EdgeProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000056 Function *Main = M.getFunction("main");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000057 if (Main == 0) {
Bob Wilsona0be09f2012-12-24 18:15:21 +000058 M.getContext().emitWarning("cannot insert edge profiling into a module"
59 " with no main function");
Chris Lattnerb2f12a22004-03-08 17:54:34 +000060 return false; // No main, no instrumentation!
61 }
62
63 std::set<BasicBlock*> BlocksToInstrument;
64 unsigned NumEdges = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000065 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
66 if (F->isDeclaration()) continue;
67 // Reserve space for (0,entry) edge.
68 ++NumEdges;
Chris Lattnerb2f12a22004-03-08 17:54:34 +000069 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
70 // Keep track of which blocks need to be instrumented. We don't want to
71 // instrument blocks that are added as the result of breaking critical
72 // edges!
73 BlocksToInstrument.insert(BB);
74 NumEdges += BB->getTerminator()->getNumSuccessors();
75 }
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000076 }
Chris Lattnerb2f12a22004-03-08 17:54:34 +000077
Chris Lattnerdb125cf2011-07-18 04:54:35 +000078 Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000079 GlobalVariable *Counters =
Owen Andersone9b11b42009-07-08 19:03:57 +000080 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersona7235ea2009-07-31 20:28:14 +000081 Constant::getNullValue(ATy), "EdgeProfCounters");
Andreas Neustifter62353a82009-09-01 10:08:39 +000082 NumEdgesInserted = NumEdges;
Chris Lattnerb2f12a22004-03-08 17:54:34 +000083
Chris Lattnerb2f12a22004-03-08 17:54:34 +000084 // Instrument all of the edges...
85 unsigned i = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000086 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
87 if (F->isDeclaration()) continue;
88 // Create counter for (0,entry) edge.
89 IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +000090 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
91 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
92 // Okay, we have to add a counter of each outgoing edge. If the
93 // outgoing edge is not critical don't split it, just insert the counter
94 // in the source or destination of the edge.
95 TerminatorInst *TI = BB->getTerminator();
96 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
97 // If the edge is critical, split it.
98 SplitCriticalEdge(TI, s, this);
99
100 // Okay, we are guaranteed that the edge is no longer critical. If we
101 // only have a single successor, insert the counter in this block,
102 // otherwise insert it in the successor block.
Chris Lattner915c6562007-12-13 00:04:46 +0000103 if (TI->getNumSuccessors() == 1) {
Chris Lattnerb2f12a22004-03-08 17:54:34 +0000104 // Insert counter at the start of the block
Andrew Trick04317cc2011-01-29 01:09:53 +0000105 IncrementCounterInBlock(BB, i++, Counters, false);
Chris Lattnerb2f12a22004-03-08 17:54:34 +0000106 } else {
107 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000108 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
Chris Lattnerb2f12a22004-03-08 17:54:34 +0000109 }
110 }
111 }
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000112 }
Chris Lattnerb2f12a22004-03-08 17:54:34 +0000113
114 // Add the initialization call to main.
115 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
116 return true;
117}
118