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