Remove attribution from file headers, per discussion on llvmdev.
[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 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 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/Compiler.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/Transforms/Instrumentation.h"
29 #include "RSProfiling.h"
30 #include "ProfilingUtils.h"
31 using namespace llvm;
32
33 namespace {
34   class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
35   public:
36     static char ID;
37     bool runOnModule(Module &M);
38   };
39
40   char FunctionProfiler::ID = 0;
41
42   RegisterPass<FunctionProfiler> X("insert-function-profiling",
43                                "Insert instrumentation for function profiling");
44   RegisterAnalysisGroup<RSProfilers> XG(X);
45
46 }
47
48 ModulePass *llvm::createFunctionProfilerPass() {
49   return new FunctionProfiler();
50 }
51
52 bool FunctionProfiler::runOnModule(Module &M) {
53   Function *Main = M.getFunction("main");
54   if (Main == 0) {
55     cerr << "WARNING: cannot insert function profiling into a module"
56          << " with no main function!\n";
57     return false;  // No main, no instrumentation!
58   }
59
60   unsigned NumFunctions = 0;
61   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
62     if (!I->isDeclaration())
63       ++NumFunctions;
64
65   const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
66   GlobalVariable *Counters =
67     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
68                        Constant::getNullValue(ATy), "FuncProfCounters", &M);
69
70   // Instrument all of the functions...
71   unsigned i = 0;
72   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
73     if (!I->isDeclaration())
74       // Insert counter at the start of the function
75       IncrementCounterInBlock(I->begin(), i++, Counters);
76
77   // Add the initialization call to main.
78   InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
79   return true;
80 }
81
82
83 namespace {
84   class BlockProfiler : public RSProfilers_std {
85     bool runOnModule(Module &M);
86   public:
87     static char ID;
88   };
89
90   char BlockProfiler::ID = 0;
91   RegisterPass<BlockProfiler> Y("insert-block-profiling",
92                                 "Insert instrumentation for block profiling");
93   RegisterAnalysisGroup<RSProfilers> YG(Y);
94 }
95
96 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
97
98 bool BlockProfiler::runOnModule(Module &M) {
99   Function *Main = M.getFunction("main");
100   if (Main == 0) {
101     cerr << "WARNING: cannot insert block profiling into a module"
102          << " with no main function!\n";
103     return false;  // No main, no instrumentation!
104   }
105
106   unsigned NumBlocks = 0;
107   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
108     NumBlocks += I->size();
109
110   const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
111   GlobalVariable *Counters =
112     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
113                        Constant::getNullValue(ATy), "BlockProfCounters", &M);
114
115   // Instrument all of the blocks...
116   unsigned i = 0;
117   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
118     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
119       // Insert counter at the start of the block
120       IncrementCounterInBlock(BB, i++, Counters);
121
122   // Add the initialization call to main.
123   InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
124   return true;
125 }
126