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 if (F->isDeclaration()) return MissingValue;
71 std::map<const Function*, double>::iterator J =
72 FunctionInformation.find(F);
73 if (J != FunctionInformation.end())
76 double Count = getExecutionCount(&F->getEntryBlock());
77 if (Count != MissingValue) FunctionInformation[F] = Count;
82 //===----------------------------------------------------------------------===//
83 // NoProfile ProfileInfo implementation
87 struct VISIBILITY_HIDDEN NoProfileInfo
88 : public ImmutablePass, public ProfileInfo {
89 static char ID; // Class identification, replacement for typeinfo
90 NoProfileInfo() : ImmutablePass(&ID) {}
92 } // End of anonymous namespace
94 char NoProfileInfo::ID = 0;
95 // Register this pass...
96 static RegisterPass<NoProfileInfo>
97 X("no-profile", "No Profile Information", false, true);
99 // Declare that we implement the ProfileInfo interface
100 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
102 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }