LLVM API Documentation
00001 //===- BlockProfiling.cpp - Insert counters for block profiling -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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/LLVMContext.h" 00025 #include "llvm/Module.h" 00026 #include "llvm/Pass.h" 00027 #include "llvm/Support/Compiler.h" 00028 #include "llvm/Support/Streams.h" 00029 #include "llvm/Transforms/Instrumentation.h" 00030 #include "RSProfiling.h" 00031 #include "ProfilingUtils.h" 00032 using namespace llvm; 00033 00034 namespace { 00035 class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std { 00036 public: 00037 static char ID; 00038 bool runOnModule(Module &M); 00039 }; 00040 } 00041 00042 char FunctionProfiler::ID = 0; 00043 00044 static RegisterPass<FunctionProfiler> 00045 X("insert-function-profiling", 00046 "Insert instrumentation for function profiling"); 00047 static RegisterAnalysisGroup<RSProfilers> XG(X); 00048 00049 ModulePass *llvm::createFunctionProfilerPass() { 00050 return new FunctionProfiler(); 00051 } 00052 00053 bool FunctionProfiler::runOnModule(Module &M) { 00054 Function *Main = M.getFunction("main"); 00055 if (Main == 0) { 00056 cerr << "WARNING: cannot insert function profiling into a module" 00057 << " with no main function!\n"; 00058 return false; // No main, no instrumentation! 00059 } 00060 00061 unsigned NumFunctions = 0; 00062 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00063 if (!I->isDeclaration()) 00064 ++NumFunctions; 00065 00066 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), 00067 NumFunctions); 00068 GlobalVariable *Counters = 00069 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, 00070 Constant::getNullValue(ATy), "FuncProfCounters"); 00071 00072 // Instrument all of the functions... 00073 unsigned i = 0; 00074 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00075 if (!I->isDeclaration()) 00076 // Insert counter at the start of the function 00077 IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters); 00078 00079 // Add the initialization call to main. 00080 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); 00081 return true; 00082 } 00083 00084 00085 namespace { 00086 class BlockProfiler : public RSProfilers_std { 00087 bool runOnModule(Module &M); 00088 public: 00089 static char ID; 00090 }; 00091 } 00092 00093 char BlockProfiler::ID = 0; 00094 static RegisterPass<BlockProfiler> 00095 Y("insert-block-profiling", "Insert instrumentation for block profiling"); 00096 static RegisterAnalysisGroup<RSProfilers> YG(Y); 00097 00098 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); } 00099 00100 bool BlockProfiler::runOnModule(Module &M) { 00101 Function *Main = M.getFunction("main"); 00102 if (Main == 0) { 00103 cerr << "WARNING: cannot insert block profiling into a module" 00104 << " with no main function!\n"; 00105 return false; // No main, no instrumentation! 00106 } 00107 00108 unsigned NumBlocks = 0; 00109 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00110 if (!I->isDeclaration()) 00111 NumBlocks += I->size(); 00112 00113 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks); 00114 GlobalVariable *Counters = 00115 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, 00116 Constant::getNullValue(ATy), "BlockProfCounters"); 00117 00118 // Instrument all of the blocks... 00119 unsigned i = 0; 00120 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 00121 if (I->isDeclaration()) continue; 00122 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) 00123 // Insert counter at the start of the block 00124 IncrementCounterInBlock(BB, i++, Counters); 00125 } 00126 00127 // Add the initialization call to main. 00128 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); 00129 return true; 00130 } 00131