For PR950:
[oota-llvm.git] / lib / Transforms / Instrumentation / BlockProfiling.cpp
1 //===- BlockProfiling.cpp - Insert counters for block 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 basic block or
11 // function profiling.  This is the most basic form of profiling, which can tell
12 // which blocks are hot, but cannot reliably detect hot paths through the CFG.
13 // Block profiling counts the number of times each basic block executes, and
14 // function profiling counts the number of times each function is called.
15 //
16 // Note that this implementation is very naive.  Control equivalent regions of
17 // the CFG should not require duplicate counters, but we do put duplicate
18 // counters in.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Streams.h"
27 #include "llvm/Transforms/Instrumentation.h"
28 #include "RSProfiling.h"
29 #include "ProfilingUtils.h"
30 using namespace llvm;
31
32 namespace {
33   class FunctionProfiler : public RSProfilers_std {
34     bool runOnModule(Module &M);
35   };
36
37   RegisterPass<FunctionProfiler> X("insert-function-profiling",
38                                "Insert instrumentation for function profiling");
39   RegisterAnalysisGroup<RSProfilers> XG(X);
40
41 }
42
43 ModulePass *llvm::createFunctionProfilerPass() {
44   return new FunctionProfiler();
45 }
46
47 bool FunctionProfiler::runOnModule(Module &M) {
48   Function *Main = M.getMainFunction();
49   if (Main == 0) {
50     cerr << "WARNING: cannot insert function profiling into a module"
51          << " with no main function!\n";
52     return false;  // No main, no instrumentation!
53   }
54
55   unsigned NumFunctions = 0;
56   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
57     if (!I->isExternal())
58       ++NumFunctions;
59
60   const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
61   GlobalVariable *Counters =
62     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
63                        Constant::getNullValue(ATy), "FuncProfCounters", &M);
64
65   // Instrument all of the functions...
66   unsigned i = 0;
67   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
68     if (!I->isExternal())
69       // Insert counter at the start of the function
70       IncrementCounterInBlock(I->begin(), i++, Counters);
71
72   // Add the initialization call to main.
73   InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
74   return true;
75 }
76
77
78 namespace {
79   class BlockProfiler : public RSProfilers_std {
80     bool runOnModule(Module &M);
81   };
82
83   RegisterPass<BlockProfiler> Y("insert-block-profiling",
84                                 "Insert instrumentation for block profiling");
85   RegisterAnalysisGroup<RSProfilers> YG(Y);
86 }
87
88 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
89
90 bool BlockProfiler::runOnModule(Module &M) {
91   Function *Main = M.getMainFunction();
92   if (Main == 0) {
93     cerr << "WARNING: cannot insert block profiling into a module"
94          << " with no main function!\n";
95     return false;  // No main, no instrumentation!
96   }
97
98   unsigned NumBlocks = 0;
99   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
100     NumBlocks += I->size();
101
102   const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
103   GlobalVariable *Counters =
104     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
105                        Constant::getNullValue(ATy), "BlockProfCounters", &M);
106
107   // Instrument all of the blocks...
108   unsigned i = 0;
109   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
110     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
111       // Insert counter at the start of the block
112       IncrementCounterInBlock(BB, i++, Counters);
113
114   // Add the initialization call to main.
115   InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
116   return true;
117 }
118