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"
20 #include "llvm/Support/raw_ostream.h"
24 // Register the ProfileInfo interface, providing a nice name to refer to.
25 static RegisterAnalysisGroup
<ProfileInfo
> Z("Profile Information");
26 char ProfileInfo::ID
= 0;
28 ProfileInfo::~ProfileInfo() {}
30 const double ProfileInfo::MissingValue
= -1;
32 double ProfileInfo::getExecutionCount(const BasicBlock
*BB
) {
33 std::map
<const Function
*, BlockCounts
>::iterator J
=
34 BlockInformation
.find(BB
->getParent());
35 if (J
!= BlockInformation
.end()) {
36 BlockCounts::iterator I
= J
->second
.find(BB
);
37 if (I
!= J
->second
.end())
41 pred_const_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
);
43 // Are there zero predecessors of this block?
45 // If this is the entry block, look for the Null -> Entry edge.
46 if (BB
== &BB
->getParent()->getEntryBlock())
47 return getEdgeWeight(getEdge(0, BB
));
49 return 0; // Otherwise, this is a dead block.
52 // Otherwise, if there are predecessors, the execution count of this block is
53 // the sum of the edge frequencies from the incoming edges.
54 std::set
<const BasicBlock
*> ProcessedPreds
;
56 for (; PI
!= PE
; ++PI
)
57 if (ProcessedPreds
.insert(*PI
).second
) {
58 double w
= getEdgeWeight(getEdge(*PI
, BB
));
59 if (w
== MissingValue
) {
66 if (Count
!= MissingValue
) BlockInformation
[BB
->getParent()][BB
] = Count
;
70 double ProfileInfo::getExecutionCount(const Function
*F
) {
71 std::map
<const Function
*, double>::iterator J
=
72 FunctionInformation
.find(F
);
73 if (J
!= FunctionInformation
.end())
76 // isDeclaration() is checked here and not at start of function to allow
77 // functions without a body still to have a execution count.
78 if (F
->isDeclaration()) return MissingValue
;
80 double Count
= getExecutionCount(&F
->getEntryBlock());
81 if (Count
!= MissingValue
) FunctionInformation
[F
] = Count
;
85 raw_ostream
& llvm::operator<<(raw_ostream
&O
, ProfileInfo::Edge E
) {
87 O
<< (E
.first
? E
.first
->getNameStr() : "0");
89 O
<< (E
.second
? E
.second
->getNameStr() : "0");
93 //===----------------------------------------------------------------------===//
94 // NoProfile ProfileInfo implementation
98 struct VISIBILITY_HIDDEN NoProfileInfo
99 : public ImmutablePass
, public ProfileInfo
{
100 static char ID
; // Class identification, replacement for typeinfo
101 NoProfileInfo() : ImmutablePass(&ID
) {}
103 } // End of anonymous namespace
105 char NoProfileInfo::ID
= 0;
106 // Register this pass...
107 static RegisterPass
<NoProfileInfo
>
108 X("no-profile", "No Profile Information", false, true);
110 // Declare that we implement the ProfileInfo interface
111 static RegisterAnalysisGroup
<ProfileInfo
, true> Y(X
);
113 ImmutablePass
*llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }