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 double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
00030   std::map<const Function*, BlockCounts>::iterator J =
00031     BlockInformation.find(BB->getParent());
00032   if (J != BlockInformation.end()) {
00033     BlockCounts::iterator I = J->second.find(BB);
00034     if (I != J->second.end())
00035       return I->second;
00036   }
00037 
00038   pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
00039 
00040   // Are there zero predecessors of this block?
00041   if (PI == PE) {
00042     // If this is the entry block, look for the Null -> Entry edge.
00043     if (BB == &BB->getParent()->getEntryBlock())
00044       return getEdgeWeight(getEdge(0, BB));
00045     else
00046       return 0;   // Otherwise, this is a dead block.
00047   }
00048 
00049   // Otherwise, if there are predecessors, the execution count of this block is
00050   // the sum of the edge frequencies from the incoming edges.
00051   std::set<const BasicBlock*> ProcessedPreds;
00052   double Count = 0;
00053   for (; PI != PE; ++PI)
00054     if (ProcessedPreds.insert(*PI).second) {
00055       double w = getEdgeWeight(getEdge(*PI, BB));
00056       if (w == MissingValue) {
00057         Count = MissingValue;
00058         break;
00059       }
00060       Count += w;
00061     }
00062 
00063   BlockInformation[BB->getParent()][BB] = Count;
00064   return Count;
00065 }
00066 
00067 double ProfileInfo::getExecutionCount(const Function *F) {
00068   if (F->isDeclaration()) return MissingValue;
00069   std::map<const Function*, double>::iterator J =
00070     FunctionInformation.find(F);
00071   if (J != FunctionInformation.end())
00072     return J->second;
00073 
00074   double Count = getExecutionCount(&F->getEntryBlock());
00075   FunctionInformation[F] = Count;
00076   return Count;
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(); }