Move all of the header files which are involved in modelling the LLVM IR
[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 is distributed under the University of Illinois Open Source
6 // 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 #define DEBUG_TYPE "insert-edge-profiling"
20
21 #include "llvm/Transforms/Instrumentation.h"
22 #include "ProfilingUtils.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
29 #include <set>
30 using namespace llvm;
31
32 STATISTIC(NumEdgesInserted, "The # of edges inserted.");
33
34 namespace {
35   class EdgeProfiler : public ModulePass {
36     bool runOnModule(Module &M);
37   public:
38     static char ID; // Pass identification, replacement for typeid
39     EdgeProfiler() : ModulePass(ID) {
40       initializeEdgeProfilerPass(*PassRegistry::getPassRegistry());
41     }
42
43     virtual const char *getPassName() const {
44       return "Edge Profiler";
45     }
46   };
47 }
48
49 char EdgeProfiler::ID = 0;
50 INITIALIZE_PASS(EdgeProfiler, "insert-edge-profiling",
51                 "Insert instrumentation for edge profiling", false, false)
52
53 ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
54
55 bool EdgeProfiler::runOnModule(Module &M) {
56   Function *Main = M.getFunction("main");
57   if (Main == 0) {
58     M.getContext().emitWarning("cannot insert edge profiling into a module"
59                                " with no main function");
60     return false;  // No main, no instrumentation!
61   }
62
63   std::set<BasicBlock*> BlocksToInstrument;
64   unsigned NumEdges = 0;
65   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;
69     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     }
76   }
77
78   Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
79   GlobalVariable *Counters =
80     new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
81                        Constant::getNullValue(ATy), "EdgeProfCounters");
82   NumEdgesInserted = NumEdges;
83
84   // Instrument all of the edges...
85   unsigned i = 0;
86   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);
90     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.
103           if (TI->getNumSuccessors() == 1) {
104             // Insert counter at the start of the block
105             IncrementCounterInBlock(BB, i++, Counters, false);
106           } else {
107             // Insert counter at the start of the block
108             IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
109           }
110         }
111       }
112   }
113
114   // Add the initialization call to main.
115   InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
116   return true;
117 }
118