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