LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 "ProfilingUtils.h"
00027 #include <iostream>
00028 
00029 using namespace llvm;
00030 
00031 namespace {
00032   class FunctionProfiler : public ModulePass {
00033     bool runOnModule(Module &M);
00034   };
00035 
00036   RegisterOpt<FunctionProfiler> X("insert-function-profiling",
00037                                "Insert instrumentation for function profiling");
00038 }
00039 
00040 bool FunctionProfiler::runOnModule(Module &M) {
00041   Function *Main = M.getMainFunction();
00042   if (Main == 0) {
00043     std::cerr << "WARNING: cannot insert function profiling into a module"
00044               << " with no main function!\n";
00045     return false;  // No main, no instrumentation!
00046   }
00047 
00048   unsigned NumFunctions = 0;
00049   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 
00050     if (!I->isExternal())
00051       ++NumFunctions;
00052 
00053   const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
00054   GlobalVariable *Counters =
00055     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
00056                        Constant::getNullValue(ATy), "FuncProfCounters", &M);
00057 
00058   // Instrument all of the functions...
00059   unsigned i = 0;
00060   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 
00061     if (!I->isExternal())
00062       // Insert counter at the start of the function
00063       IncrementCounterInBlock(I->begin(), i++, Counters);
00064 
00065   // Add the initialization call to main.
00066   InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
00067   return true;
00068 }
00069 
00070 
00071 namespace {
00072   class BlockProfiler : public ModulePass {
00073     bool runOnModule(Module &M);
00074   };
00075 
00076   RegisterOpt<BlockProfiler> Y("insert-block-profiling",
00077                                "Insert instrumentation for block profiling");
00078 }
00079 
00080 bool BlockProfiler::runOnModule(Module &M) {
00081   Function *Main = M.getMainFunction();
00082   if (Main == 0) {
00083     std::cerr << "WARNING: cannot insert block profiling into a module"
00084               << " with no main function!\n";
00085     return false;  // No main, no instrumentation!
00086   }
00087 
00088   unsigned NumBlocks = 0;
00089   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 
00090     NumBlocks += I->size();
00091 
00092   const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
00093   GlobalVariable *Counters =
00094     new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
00095                        Constant::getNullValue(ATy), "BlockProfCounters", &M);
00096 
00097   // Instrument all of the blocks...
00098   unsigned i = 0;
00099   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 
00100     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
00101       // Insert counter at the start of the block
00102       IncrementCounterInBlock(BB, i++, Counters);
00103 
00104   // Add the initialization call to main.
00105   InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
00106   return true;
00107 }
00108