blob: 98ea25199b02e44453e8c390e9a8c2d514aeb880 [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"
20#include <set>
21using namespace llvm;
22
23// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000024static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025char ProfileInfo::ID = 0;
26
27ProfileInfo::~ProfileInfo() {}
28
Daniel Dunbar4ae20272009-08-08 17:43:09 +000029double 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())
35 return I->second;
36 }
Daniel Dunbar23505092009-08-05 21:51:16 +000037
Daniel Dunbar042521c2009-07-14 06:58:59 +000038 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 // Are there zero predecessors of this block?
41 if (PI == PE) {
42 // If this is the entry block, look for the Null -> Entry edge.
43 if (BB == &BB->getParent()->getEntryBlock())
Daniel Dunbar4ae20272009-08-08 17:43:09 +000044 return getEdgeWeight(getEdge(0, BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else
46 return 0; // Otherwise, this is a dead block.
47 }
48
49 // Otherwise, if there are predecessors, the execution count of this block is
Daniel Dunbar4ae20272009-08-08 17:43:09 +000050 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar042521c2009-07-14 06:58:59 +000051 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000052 double Count = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 for (; PI != PE; ++PI)
Daniel Dunbar4ae20272009-08-08 17:43:09 +000054 if (ProcessedPreds.insert(*PI).second) {
55 double w = getEdgeWeight(getEdge(*PI, BB));
56 if (w == MissingValue) {
Andreas Neustifter375a8bc2009-08-19 05:44:39 +000057 Count = MissingValue;
58 break;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000059 }
60 Count += w;
61 }
62
63 BlockInformation[BB->getParent()][BB] = Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 return Count;
65}
66
Daniel Dunbar4ae20272009-08-08 17:43:09 +000067double ProfileInfo::getExecutionCount(const Function *F) {
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000068 if (F->isDeclaration()) return MissingValue;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000069 std::map<const Function*, double>::iterator J =
70 FunctionInformation.find(F);
71 if (J != FunctionInformation.end())
72 return J->second;
Daniel Dunbar23505092009-08-05 21:51:16 +000073
Daniel Dunbar4ae20272009-08-08 17:43:09 +000074 double Count = getExecutionCount(&F->getEntryBlock());
75 FunctionInformation[F] = Count;
76 return Count;
Daniel Dunbar042521c2009-07-14 06:58:59 +000077}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79
80//===----------------------------------------------------------------------===//
81// NoProfile ProfileInfo implementation
82//
83
84namespace {
85 struct VISIBILITY_HIDDEN NoProfileInfo
86 : public ImmutablePass, public ProfileInfo {
87 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000088 NoProfileInfo() : ImmutablePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090} // End of anonymous namespace
91
Dan Gohman089efff2008-05-13 00:00:25 +000092char NoProfileInfo::ID = 0;
93// Register this pass...
94static RegisterPass<NoProfileInfo>
95X("no-profile", "No Profile Information", false, true);
96
97// Declare that we implement the ProfileInfo interface
98static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
99
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }