LLVM API Documentation
00001 //===- EdgeProfiling.cpp - Insert counters for edge 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 edge profiling. 00011 // Edge profiling can give a reasonable approximation of the hot paths through a 00012 // program, and is used for a wide variety of program transformations. 00013 // 00014 // Note that this implementation is very naive. We insert a counter for *every* 00015 // edge in the program, instead of using control flow information to prune the 00016 // number of counters inserted. 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #include "llvm/Constants.h" 00021 #include "llvm/DerivedTypes.h" 00022 #include "llvm/Module.h" 00023 #include "llvm/Pass.h" 00024 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00025 #include "ProfilingUtils.h" 00026 #include <iostream> 00027 #include <set> 00028 using namespace llvm; 00029 00030 namespace { 00031 class EdgeProfiler : public ModulePass { 00032 bool runOnModule(Module &M); 00033 }; 00034 00035 RegisterOpt<EdgeProfiler> X("insert-edge-profiling", 00036 "Insert instrumentation for edge profiling"); 00037 } 00038 00039 bool EdgeProfiler::runOnModule(Module &M) { 00040 Function *Main = M.getMainFunction(); 00041 if (Main == 0) { 00042 std::cerr << "WARNING: cannot insert edge profiling into a module" 00043 << " with no main function!\n"; 00044 return false; // No main, no instrumentation! 00045 } 00046 00047 std::set<BasicBlock*> BlocksToInstrument; 00048 unsigned NumEdges = 0; 00049 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) 00050 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { 00051 // Keep track of which blocks need to be instrumented. We don't want to 00052 // instrument blocks that are added as the result of breaking critical 00053 // edges! 00054 BlocksToInstrument.insert(BB); 00055 NumEdges += BB->getTerminator()->getNumSuccessors(); 00056 } 00057 00058 const Type *ATy = ArrayType::get(Type::UIntTy, NumEdges); 00059 GlobalVariable *Counters = 00060 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, 00061 Constant::getNullValue(ATy), "EdgeProfCounters", &M); 00062 00063 // Instrument all of the edges... 00064 unsigned i = 0; 00065 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) 00066 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 00067 if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks 00068 // Okay, we have to add a counter of each outgoing edge. If the 00069 // outgoing edge is not critical don't split it, just insert the counter 00070 // in the source or destination of the edge. 00071 TerminatorInst *TI = BB->getTerminator(); 00072 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { 00073 // If the edge is critical, split it. 00074 SplitCriticalEdge(TI, s, this); 00075 00076 // Okay, we are guaranteed that the edge is no longer critical. If we 00077 // only have a single successor, insert the counter in this block, 00078 // otherwise insert it in the successor block. 00079 if (TI->getNumSuccessors() == 0) { 00080 // Insert counter at the start of the block 00081 IncrementCounterInBlock(BB, i++, Counters); 00082 } else { 00083 // Insert counter at the start of the block 00084 IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters); 00085 } 00086 } 00087 } 00088 00089 // Add the initialization call to main. 00090 InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters); 00091 return true; 00092 } 00093