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