LLVM API Documentation

ProfileInfo.cpp

Go to the documentation of this file.
00001 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
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 file implements the abstract ProfileInfo interface, and the default
00011 // "no profile" implementation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Analysis/Passes.h"
00016 #include "llvm/Analysis/ProfileInfo.h"
00017 #include "llvm/Pass.h"
00018 #include "llvm/Support/CFG.h"
00019 #include "llvm/Support/Compiler.h"
00020 #include <set>
00021 using namespace llvm;
00022 
00023 // Register the ProfileInfo interface, providing a nice name to refer to.
00024 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
00025 char ProfileInfo::ID = 0;
00026 
00027 ProfileInfo::~ProfileInfo() {}
00028 
00029 unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
00030   pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
00031 
00032   // Are there zero predecessors of this block?
00033   if (PI == PE) {
00034     // If this is the entry block, look for the Null -> Entry edge.
00035     if (BB == &BB->getParent()->getEntryBlock())
00036       return getEdgeWeight(0, BB);
00037     else
00038       return 0;   // Otherwise, this is a dead block.
00039   }
00040 
00041   // Otherwise, if there are predecessors, the execution count of this block is
00042   // the sum of the edge frequencies from the incoming edges.  Note that if
00043   // there are multiple edges from a predecessor to this block that we don't
00044   // want to count its weight multiple times.  For this reason, we keep track of
00045   // the predecessors we've seen and only count them if we haven't run into them
00046   // yet.
00047   //
00048   // We don't want to create an std::set unless we are dealing with a block that
00049   // has a LARGE number of in-edges.  Handle the common case of having only a
00050   // few in-edges with special code.
00051   //
00052   BasicBlock *FirstPred = *PI;
00053   unsigned Count = getEdgeWeight(FirstPred, BB);
00054   ++PI;
00055   if (PI == PE) return Count;   // Quick exit for single predecessor blocks
00056 
00057   BasicBlock *SecondPred = *PI;
00058   if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
00059   ++PI;
00060   if (PI == PE) return Count;   // Quick exit for two predecessor blocks
00061 
00062   BasicBlock *ThirdPred = *PI;
00063   if (ThirdPred != FirstPred && ThirdPred != SecondPred)
00064     Count += getEdgeWeight(ThirdPred, BB);
00065   ++PI;
00066   if (PI == PE) return Count;   // Quick exit for three predecessor blocks
00067 
00068   std::set<BasicBlock*> ProcessedPreds;
00069   ProcessedPreds.insert(FirstPred);
00070   ProcessedPreds.insert(SecondPred);
00071   ProcessedPreds.insert(ThirdPred);
00072   for (; PI != PE; ++PI)
00073     if (ProcessedPreds.insert(*PI).second)
00074       Count += getEdgeWeight(*PI, BB);
00075   return Count;
00076 }
00077 
00078 
00079 
00080 //===----------------------------------------------------------------------===//
00081 //  NoProfile ProfileInfo implementation
00082 //
00083 
00084 namespace {
00085   struct VISIBILITY_HIDDEN NoProfileInfo 
00086     : public ImmutablePass, public ProfileInfo {
00087     static char ID; // Class identification, replacement for typeinfo
00088     NoProfileInfo() : ImmutablePass(&ID) {}
00089   };
00090 }  // End of anonymous namespace
00091 
00092 char NoProfileInfo::ID = 0;
00093 // Register this pass...
00094 static RegisterPass<NoProfileInfo>
00095 X("no-profile", "No Profile Information", false, true);
00096 
00097 // Declare that we implement the ProfileInfo interface
00098 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
00099 
00100 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }