Move ProfileInfo::Edge's operator<< out of line. Among other benefits,
[oota-llvm.git] / lib / Analysis / ProfileInfo.cpp
1 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the abstract ProfileInfo interface, and the default
11 // "no profile" implementation.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
21 #include <set>
22 using namespace llvm;
23
24 // Register the ProfileInfo interface, providing a nice name to refer to.
25 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
26 char ProfileInfo::ID = 0;
27
28 ProfileInfo::~ProfileInfo() {}
29
30 const double ProfileInfo::MissingValue = -1;
31
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())
38       return I->second;
39   }
40
41   pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
42
43   // Are there zero predecessors of this block?
44   if (PI == PE) {
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));
48     else
49       return 0;   // Otherwise, this is a dead block.
50   }
51
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;
55   double Count = 0;
56   for (; PI != PE; ++PI)
57     if (ProcessedPreds.insert(*PI).second) {
58       double w = getEdgeWeight(getEdge(*PI, BB));
59       if (w == MissingValue) {
60         Count = MissingValue;
61         break;
62       }
63       Count += w;
64     }
65
66   if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
67   return Count;
68 }
69
70 double ProfileInfo::getExecutionCount(const Function *F) {
71   std::map<const Function*, double>::iterator J =
72     FunctionInformation.find(F);
73   if (J != FunctionInformation.end())
74     return J->second;
75
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;
79
80   double Count = getExecutionCount(&F->getEntryBlock());
81   if (Count != MissingValue) FunctionInformation[F] = Count;
82   return Count;
83 }
84
85 raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) {
86   O << "(";
87   O << (E.first ? E.first->getNameStr() : "0");
88   O << ",";
89   O << (E.second ? E.second->getNameStr() : "0");
90   return O << ")";
91 }
92
93 //===----------------------------------------------------------------------===//
94 //  NoProfile ProfileInfo implementation
95 //
96
97 namespace {
98   struct VISIBILITY_HIDDEN NoProfileInfo 
99     : public ImmutablePass, public ProfileInfo {
100     static char ID; // Class identification, replacement for typeinfo
101     NoProfileInfo() : ImmutablePass(&ID) {}
102   };
103 }  // End of anonymous namespace
104
105 char NoProfileInfo::ID = 0;
106 // Register this pass...
107 static RegisterPass<NoProfileInfo>
108 X("no-profile", "No Profile Information", false, true);
109
110 // Declare that we implement the ProfileInfo interface
111 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
112
113 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }