Rename Instruction::isIdenticalTo to Instruction::isIdenticalToWhenDefined,
[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 <set>
21 using namespace llvm;
22
23 // Register the ProfileInfo interface, providing a nice name to refer to.
24 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
25 char ProfileInfo::ID = 0;
26
27 ProfileInfo::~ProfileInfo() {}
28
29 const double ProfileInfo::MissingValue = -1;
30
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())
37       return I->second;
38   }
39
40   pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
41
42   // Are there zero predecessors of this block?
43   if (PI == PE) {
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));
47     else
48       return 0;   // Otherwise, this is a dead block.
49   }
50
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;
54   double Count = 0;
55   for (; PI != PE; ++PI)
56     if (ProcessedPreds.insert(*PI).second) {
57       double w = getEdgeWeight(getEdge(*PI, BB));
58       if (w == MissingValue) {
59         Count = MissingValue;
60         break;
61       }
62       Count += w;
63     }
64
65   if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
66   return Count;
67 }
68
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())
74     return J->second;
75
76   double Count = getExecutionCount(&F->getEntryBlock());
77   if (Count != MissingValue) FunctionInformation[F] = Count;
78   return Count;
79 }
80
81
82 //===----------------------------------------------------------------------===//
83 //  NoProfile ProfileInfo implementation
84 //
85
86 namespace {
87   struct VISIBILITY_HIDDEN NoProfileInfo 
88     : public ImmutablePass, public ProfileInfo {
89     static char ID; // Class identification, replacement for typeinfo
90     NoProfileInfo() : ImmutablePass(&ID) {}
91   };
92 }  // End of anonymous namespace
93
94 char NoProfileInfo::ID = 0;
95 // Register this pass...
96 static RegisterPass<NoProfileInfo>
97 X("no-profile", "No Profile Information", false, true);
98
99 // Declare that we implement the ProfileInfo interface
100 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
101
102 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }