Initial support for edge profiling
[oota-llvm.git] / lib / Transforms / Instrumentation / EdgeProfiling.cpp
1 //===- 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"
26 #include <set>
27 using namespace llvm;
28
29 namespace {
30   class EdgeProfiler : public Pass {
31     bool run(Module &M);
32   };
33
34   RegisterOpt<EdgeProfiler> X("insert-edge-profiling",
35                               "Insert instrumentation for edge profiling");
36 }
37
38 bool EdgeProfiler::run(Module &M) {
39   Function *Main = M.getMainFunction();
40   if (Main == 0) {
41     std::cerr << "WARNING: cannot insert edge profiling into a module"
42               << " with no main function!\n";
43     return false;  // No main, no instrumentation!
44   }
45
46   std::set<BasicBlock*> BlocksToInstrument;
47   unsigned NumEdges = 0;
48   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
49     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
50       // Keep track of which blocks need to be instrumented.  We don't want to
51       // instrument blocks that are added as the result of breaking critical
52       // edges!
53       BlocksToInstrument.insert(BB);
54       NumEdges += BB->getTerminator()->getNumSuccessors();
55     }
56
57   const Type *ATy = ArrayType::get(Type::UIntTy, NumEdges);
58   GlobalVariable *Counters =
59     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
60                        Constant::getNullValue(ATy), "EdgeProfCounters", &M);
61
62   ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
63
64   // Instrument all of the edges...
65   unsigned i = 0;
66   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
67     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
68       if (BlocksToInstrument.count(BB)) {  // Don't instrument inserted blocks
69         // Okay, we have to add a counter of each outgoing edge.  If the
70         // outgoing edge is not critical don't split it, just insert the counter
71         // in the source or destination of the edge.
72         TerminatorInst *TI = BB->getTerminator();
73         for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
74           // If the edge is critical, split it.
75           SplitCriticalEdge(TI, s, this);
76
77           // Okay, we are guaranteed that the edge is no longer critical.  If we
78           // only have a single successor, insert the counter in this block,
79           // otherwise insert it in the successor block.
80           if (TI->getNumSuccessors() == 0) {
81             // Insert counter at the start of the block
82             IncrementCounterInBlock(BB, i++, CounterCPR);
83           } else {
84             // Insert counter at the start of the block
85             IncrementCounterInBlock(TI->getSuccessor(s), i++, CounterCPR);
86           }
87         }
88       }
89
90   // Add the initialization call to main.
91   InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
92   return true;
93 }
94