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