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