LLVM API Documentation

EdgeProfiling.cpp

Go to the documentation of this file.
00001 //===- EdgeProfiling.cpp - Insert counters for edge 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 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 "ProfilingUtils.h"
00021 #include "llvm/Constants.h"
00022 #include "llvm/DerivedTypes.h"
00023 #include "llvm/LLVMContext.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/Utils/BasicBlockUtils.h"
00029 #include "llvm/Transforms/Instrumentation.h"
00030 #include <set>
00031 using namespace llvm;
00032 
00033 namespace {
00034   class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass {
00035     bool runOnModule(Module &M);
00036   public:
00037     static char ID; // Pass identification, replacement for typeid
00038     EdgeProfiler() : ModulePass(&ID) {}
00039   };
00040 }
00041 
00042 char EdgeProfiler::ID = 0;
00043 static RegisterPass<EdgeProfiler>
00044 X("insert-edge-profiling", "Insert instrumentation for edge profiling");
00045 
00046 ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
00047 
00048 bool EdgeProfiler::runOnModule(Module &M) {
00049   Function *Main = M.getFunction("main");
00050   if (Main == 0) {
00051     cerr << "WARNING: cannot insert edge profiling into a module"
00052          << " with no main function!\n";
00053     return false;  // No main, no instrumentation!
00054   }
00055 
00056   std::set<BasicBlock*> BlocksToInstrument;
00057   unsigned NumEdges = 0;
00058   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
00059     if (F->isDeclaration()) continue;
00060     // Reserve space for (0,entry) edge.
00061     ++NumEdges;
00062     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
00063       // Keep track of which blocks need to be instrumented.  We don't want to
00064       // instrument blocks that are added as the result of breaking critical
00065       // edges!
00066       BlocksToInstrument.insert(BB);
00067       NumEdges += BB->getTerminator()->getNumSuccessors();
00068     }
00069   }
00070 
00071   const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
00072   GlobalVariable *Counters =
00073     new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
00074                        Constant::getNullValue(ATy), "EdgeProfCounters");
00075 
00076   // Instrument all of the edges...
00077   unsigned i = 0;
00078   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
00079     if (F->isDeclaration()) continue;
00080     // Create counter for (0,entry) edge.
00081     IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters);
00082     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00083       if (BlocksToInstrument.count(BB)) {  // Don't instrument inserted blocks
00084         // Okay, we have to add a counter of each outgoing edge.  If the
00085         // outgoing edge is not critical don't split it, just insert the counter
00086         // in the source or destination of the edge.
00087         TerminatorInst *TI = BB->getTerminator();
00088         for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
00089           // If the edge is critical, split it.
00090           SplitCriticalEdge(TI, s, this);
00091 
00092           // Okay, we are guaranteed that the edge is no longer critical.  If we
00093           // only have a single successor, insert the counter in this block,
00094           // otherwise insert it in the successor block.
00095           if (TI->getNumSuccessors() == 1) {
00096             // Insert counter at the start of the block
00097             IncrementCounterInBlock(BB, i++, Counters);
00098           } else {
00099             // Insert counter at the start of the block
00100             IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
00101           }
00102         }
00103       }
00104   }
00105 
00106   // Add the initialization call to main.
00107   InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
00108   return true;
00109 }
00110