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