LLVM API Documentation

BlockProfiling.cpp

Go to the documentation of this file.
00001 //===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
00002 //
00003 //                      The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass instruments the specified program with counters for basic block or
00011 // function profiling.  This is the most basic form of profiling, which can tell
00012 // which blocks are hot, but cannot reliably detect hot paths through the CFG.
00013 // Block profiling counts the number of times each basic block executes, and
00014 // function profiling counts the number of times each function is called.
00015 //
00016 // Note that this implementation is very naive.  Control equivalent regions of
00017 // the CFG should not require duplicate counters, but we do put duplicate
00018 // counters in.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "llvm/Constants.h"
00023 #include "llvm/DerivedTypes.h"
00024 #include "llvm/Module.h"
00025 #include "llvm/Pass.h"
00026 #include "llvm/Transforms/Instrumentation.h"
00027 #include "RSProfiling.h"
00028 #include "ProfilingUtils.h"
00029 #include <iostream>
00030 
00031 using namespace llvm;
00032 
00033 namespace {
00034   class FunctionProfiler : public RSProfilers_std {
00035     bool runOnModule(Module &M);
00036   };
00037 
00038   RegisterOpt<FunctionProfiler> X("insert-function-profiling",
00039                                "Insert instrumentation for function profiling");
00040   RegisterAnalysisGroup<RSProfilers, FunctionProfiler> XG;
00041 
00042 }
00043 
00044 ModulePass *llvm::createFunctionProfilerPass() {
00045   return new FunctionProfiler();
00046 }
00047 
00048 bool FunctionProfiler::runOnModule(Module &M) {
00049   Function *Main = M.getMainFunction();
00050   if (Main == 0) {
00051     std::cerr << "WARNING: cannot insert function profiling into a module"
00052               << " with no main function!\n";
00053     return false;  // No main, no instrumentation!
00054   }
00055 
00056   unsigned NumFunctions = 0;
00057   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00058     if (!I->isExternal())
00059       ++NumFunctions;
00060 
00061   const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
00062   GlobalVariable *Counters =
00063     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
00064                        Constant::getNullValue(ATy), "FuncProfCounters", &M);
00065 
00066   // Instrument all of the functions...
00067   unsigned i = 0;
00068   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00069     if (!I->isExternal())
00070       // Insert counter at the start of the function
00071       IncrementCounterInBlock(I->begin(), i++, Counters);
00072 
00073   // Add the initialization call to main.
00074   InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
00075   return true;
00076 }
00077 
00078 
00079 namespace {
00080   class BlockProfiler : public RSProfilers_std {
00081     bool runOnModule(Module &M);
00082   };
00083 
00084   RegisterOpt<BlockProfiler> Y("insert-block-profiling",
00085                                "Insert instrumentation for block profiling");
00086   RegisterAnalysisGroup<RSProfilers, BlockProfiler> YG;
00087 }
00088 
00089 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
00090 
00091 bool BlockProfiler::runOnModule(Module &M) {
00092   Function *Main = M.getMainFunction();
00093   if (Main == 0) {
00094     std::cerr << "WARNING: cannot insert block profiling into a module"
00095               << " with no main function!\n";
00096     return false;  // No main, no instrumentation!
00097   }
00098 
00099   unsigned NumBlocks = 0;
00100   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00101     NumBlocks += I->size();
00102 
00103   const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
00104   GlobalVariable *Counters =
00105     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
00106                        Constant::getNullValue(ATy), "BlockProfCounters", &M);
00107 
00108   // Instrument all of the blocks...
00109   unsigned i = 0;
00110   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00111     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
00112       // Insert counter at the start of the block
00113       IncrementCounterInBlock(BB, i++, Counters);
00114 
00115   // Add the initialization call to main.
00116   InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
00117   return true;
00118 }
00119