blob: 55c5cab7510b8e1853f36ae630813a05de0e7947 [file] [log] [blame]
Chris Lattner171de652004-02-10 22:11:42 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner171de652004-02-10 22:11:42 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner171de652004-02-10 22:11:42 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the abstract ProfileInfo interface, and the default
11// "no profile" implementation.
12//
13//===----------------------------------------------------------------------===//
14
Jeff Cohen534927d2005-01-08 22:01:16 +000015#include "llvm/Analysis/Passes.h"
Chris Lattner171de652004-02-10 22:11:42 +000016#include "llvm/Analysis/ProfileInfo.h"
17#include "llvm/Pass.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000018#include "llvm/Support/CFG.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000019#include "llvm/Support/Compiler.h"
Dan Gohman4bac4b92009-08-26 15:56:38 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000021#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000022using namespace llvm;
23
Chris Lattnerecefc962004-02-11 06:10:18 +000024// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000025static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Devang Patel19974732007-05-03 01:11:54 +000026char ProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000027
28ProfileInfo::~ProfileInfo() {}
29
Andreas Neustifter96135b62009-08-24 21:37:48 +000030const double ProfileInfo::MissingValue = -1;
31
Daniel Dunbarcaaa4932009-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 Dunbarc9008c52009-08-05 21:51:16 +000040
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000041 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Chris Lattner96ab5ca2004-03-08 22:04:08 +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.
Dan Gohmanecb7a772007-03-22 16:38:57 +000046 if (BB == &BB->getParent()->getEntryBlock())
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000047 return getEdgeWeight(getEdge(0, BB));
Chris Lattner96ab5ca2004-03-08 22:04:08 +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 Dunbarcaaa4932009-08-08 17:43:09 +000053 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000054 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000055 double Count = 0;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000056 for (; PI != PE; ++PI)
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000057 if (ProcessedPreds.insert(*PI).second) {
58 double w = getEdgeWeight(getEdge(*PI, BB));
59 if (w == MissingValue) {
Andreas Neustifter0a324aa2009-08-19 05:44:39 +000060 Count = MissingValue;
61 break;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000062 }
63 Count += w;
64 }
65
Andreas Neustifter96135b62009-08-24 21:37:48 +000066 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000067 return Count;
68}
69
Daniel Dunbarcaaa4932009-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 Dunbarc9008c52009-08-05 21:51:16 +000075
Andreas Neustifter3772fb12009-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 Dunbarcaaa4932009-08-08 17:43:09 +000080 double Count = getExecutionCount(&F->getEntryBlock());
Andreas Neustifter96135b62009-08-24 21:37:48 +000081 if (Count != MissingValue) FunctionInformation[F] = Count;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000082 return Count;
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000083}
Chris Lattner96ab5ca2004-03-08 22:04:08 +000084
Dan Gohman4bac4b92009-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}
Chris Lattner171de652004-02-10 22:11:42 +000092
93//===----------------------------------------------------------------------===//
94// NoProfile ProfileInfo implementation
95//
96
97namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000098 struct VISIBILITY_HIDDEN NoProfileInfo
Devang Patel794fd752007-05-01 21:15:47 +000099 : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +0000100 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000101 NoProfileInfo() : ImmutablePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000102 };
Chris Lattner171de652004-02-10 22:11:42 +0000103} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +0000104
Dan Gohman844731a2008-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
Jeff Cohen534927d2005-01-08 22:01:16 +0000113ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }