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/Passes.h" 00016 #include "llvm/Analysis/ProfileInfo.h" 00017 #include "llvm/Pass.h" 00018 #include "llvm/Support/CFG.h" 00019 #include <set> 00020 using namespace llvm; 00021 00022 // Register the ProfileInfo interface, providing a nice name to refer to. 00023 namespace { 00024 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); 00025 } 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()->front()) 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 NoProfileInfo : public ImmutablePass, public ProfileInfo {}; 00086 00087 // Register this pass... 00088 RegisterOpt<NoProfileInfo> 00089 X("no-profile", "No Profile Information"); 00090 00091 // Declare that we implement the ProfileInfo interface 00092 RegisterAnalysisGroup<ProfileInfo, NoProfileInfo, true> Y; 00093 } // End of anonymous namespace 00094 00095 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }