1 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the abstract ProfileInfo interface, and the default
11 // "no profile" implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Analysis/ProfileInfo.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Support/Compiler.h"
23 // Register the ProfileInfo interface, providing a nice name to refer to.
24 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
25 char ProfileInfo::ID = 0;
27 ProfileInfo::~ProfileInfo() {}
29 const double ProfileInfo::MissingValue = -1;
31 double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
32 std::map<const Function*, BlockCounts>::iterator J =
33 BlockInformation.find(BB->getParent());
34 if (J != BlockInformation.end()) {
35 BlockCounts::iterator I = J->second.find(BB);
36 if (I != J->second.end())
40 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
42 // Are there zero predecessors of this block?
44 // If this is the entry block, look for the Null -> Entry edge.
45 if (BB == &BB->getParent()->getEntryBlock())
46 return getEdgeWeight(getEdge(0, BB));
48 return 0; // Otherwise, this is a dead block.
51 // Otherwise, if there are predecessors, the execution count of this block is
52 // the sum of the edge frequencies from the incoming edges.
53 std::set<const BasicBlock*> ProcessedPreds;
55 for (; PI != PE; ++PI)
56 if (ProcessedPreds.insert(*PI).second) {
57 double w = getEdgeWeight(getEdge(*PI, BB));
58 if (w == MissingValue) {
65 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
69 double ProfileInfo::getExecutionCount(const Function *F) {
70 std::map<const Function*, double>::iterator J =
71 FunctionInformation.find(F);
72 if (J != FunctionInformation.end())
75 // isDeclaration() is checked here and not at start of function to allow
76 // functions without a body still to have a execution count.
77 if (F->isDeclaration()) return MissingValue;
79 double Count = getExecutionCount(&F->getEntryBlock());
80 if (Count != MissingValue) FunctionInformation[F] = Count;
85 //===----------------------------------------------------------------------===//
86 // NoProfile ProfileInfo implementation
90 struct VISIBILITY_HIDDEN NoProfileInfo
91 : public ImmutablePass, public ProfileInfo {
92 static char ID; // Class identification, replacement for typeinfo
93 NoProfileInfo() : ImmutablePass(&ID) {}
95 } // End of anonymous namespace
97 char NoProfileInfo::ID = 0;
98 // Register this pass...
99 static RegisterPass<NoProfileInfo>
100 X("no-profile", "No Profile Information", false, true);
102 // Declare that we implement the ProfileInfo interface
103 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
105 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }