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