blob: 55c5cab7510b8e1853f36ae630813a05de0e7947 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Dan Gohman4637d172009-08-26 15:56:38 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include <set>
22using namespace llvm;
23
24// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000025static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026char ProfileInfo::ID = 0;
27
28ProfileInfo::~ProfileInfo() {}
29
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000030const double ProfileInfo::MissingValue = -1;
31
Daniel Dunbar4ae20272009-08-08 17:43:09 +000032double 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 }
Daniel Dunbar23505092009-08-05 21:51:16 +000040
Daniel Dunbar042521c2009-07-14 06:58:59 +000041 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
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())
Daniel Dunbar4ae20272009-08-08 17:43:09 +000047 return getEdgeWeight(getEdge(0, BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 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
Daniel Dunbar4ae20272009-08-08 17:43:09 +000053 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar042521c2009-07-14 06:58:59 +000054 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000055 double Count = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 for (; PI != PE; ++PI)
Daniel Dunbar4ae20272009-08-08 17:43:09 +000057 if (ProcessedPreds.insert(*PI).second) {
58 double w = getEdgeWeight(getEdge(*PI, BB));
59 if (w == MissingValue) {
Andreas Neustifter375a8bc2009-08-19 05:44:39 +000060 Count = MissingValue;
61 break;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000062 }
63 Count += w;
64 }
65
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000066 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 return Count;
68}
69
Daniel Dunbar4ae20272009-08-08 17:43:09 +000070double 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;
Daniel Dunbar23505092009-08-05 21:51:16 +000075
Andreas Neustifter39fd40a2009-08-26 13:33:09 +000076 // 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
Daniel Dunbar4ae20272009-08-08 17:43:09 +000080 double Count = getExecutionCount(&F->getEntryBlock());
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000081 if (Count != MissingValue) FunctionInformation[F] = Count;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000082 return Count;
Daniel Dunbar042521c2009-07-14 06:58:59 +000083}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
Dan Gohman4637d172009-08-26 15:56:38 +000085raw_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}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93//===----------------------------------------------------------------------===//
94// NoProfile ProfileInfo implementation
95//
96
97namespace {
98 struct VISIBILITY_HIDDEN NoProfileInfo
99 : public ImmutablePass, public ProfileInfo {
100 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +0000101 NoProfileInfo() : ImmutablePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103} // End of anonymous namespace
104
Dan Gohman089efff2008-05-13 00:00:25 +0000105char NoProfileInfo::ID = 0;
106// Register this pass...
107static RegisterPass<NoProfileInfo>
108X("no-profile", "No Profile Information", false, true);
109
110// Declare that we implement the ProfileInfo interface
111static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }