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 double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
30 std::map<const Function*, BlockCounts>::iterator J =
31 BlockInformation.find(BB->getParent());
32 if (J != BlockInformation.end()) {
33 BlockCounts::iterator I = J->second.find(BB);
34 if (I != J->second.end())
38 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
40 // Are there zero predecessors of this block?
42 // If this is the entry block, look for the Null -> Entry edge.
43 if (BB == &BB->getParent()->getEntryBlock())
44 return getEdgeWeight(getEdge(0, BB));
46 return 0; // Otherwise, this is a dead block.
49 // Otherwise, if there are predecessors, the execution count of this block is
50 // the sum of the edge frequencies from the incoming edges.
51 std::set<const BasicBlock*> ProcessedPreds;
53 for (; PI != PE; ++PI)
54 if (ProcessedPreds.insert(*PI).second) {
55 double w = getEdgeWeight(getEdge(*PI, BB));
56 if (w == MissingValue) {
57 Count = MissingValue; break;
62 BlockInformation[BB->getParent()][BB] = Count;
66 double ProfileInfo::getExecutionCount(const Function *F) {
67 if (F->isDeclaration()) return MissingValue;
68 std::map<const Function*, double>::iterator J =
69 FunctionInformation.find(F);
70 if (J != FunctionInformation.end())
73 double Count = getExecutionCount(&F->getEntryBlock());
74 FunctionInformation[F] = Count;
79 //===----------------------------------------------------------------------===//
80 // NoProfile ProfileInfo implementation
84 struct VISIBILITY_HIDDEN NoProfileInfo
85 : public ImmutablePass, public ProfileInfo {
86 static char ID; // Class identification, replacement for typeinfo
87 NoProfileInfo() : ImmutablePass(&ID) {}
89 } // End of anonymous namespace
91 char NoProfileInfo::ID = 0;
92 // Register this pass...
93 static RegisterPass<NoProfileInfo>
94 X("no-profile", "No Profile Information", false, true);
96 // Declare that we implement the ProfileInfo interface
97 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
99 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }