blob: 670d4e737973cccefd3b44218b303f71ac30afbf [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"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000020#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000021using namespace llvm;
22
Chris Lattnerecefc962004-02-11 06:10:18 +000023// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000024static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Devang Patel19974732007-05-03 01:11:54 +000025char ProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000026
27ProfileInfo::~ProfileInfo() {}
28
Daniel Dunbarcaaa4932009-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 Dunbarc9008c52009-08-05 21:51:16 +000037
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000038 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Chris Lattner96ab5ca2004-03-08 22:04:08 +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.
Dan Gohmanecb7a772007-03-22 16:38:57 +000043 if (BB == &BB->getParent()->getEntryBlock())
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000044 return getEdgeWeight(getEdge(0, BB));
Chris Lattner96ab5ca2004-03-08 22:04:08 +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 Dunbarcaaa4932009-08-08 17:43:09 +000050 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000051 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000052 double Count = 0;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000053 for (; PI != PE; ++PI)
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000054 if (ProcessedPreds.insert(*PI).second) {
55 double w = getEdgeWeight(getEdge(*PI, BB));
56 if (w == MissingValue) {
57 Count = MissingValue; break;
58 }
59 Count += w;
60 }
61
62 BlockInformation[BB->getParent()][BB] = Count;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000063 return Count;
64}
65
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000066double ProfileInfo::getExecutionCount(const Function *F) {
67 std::map<const Function*, double>::iterator J =
68 FunctionInformation.find(F);
69 if (J != FunctionInformation.end())
70 return J->second;
Daniel Dunbarc9008c52009-08-05 21:51:16 +000071
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000072 double Count = getExecutionCount(&F->getEntryBlock());
73 FunctionInformation[F] = Count;
74 return Count;
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000075}
Chris Lattner96ab5ca2004-03-08 22:04:08 +000076
Chris Lattner171de652004-02-10 22:11:42 +000077
78//===----------------------------------------------------------------------===//
79// NoProfile ProfileInfo implementation
80//
81
82namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000083 struct VISIBILITY_HIDDEN NoProfileInfo
Devang Patel794fd752007-05-01 21:15:47 +000084 : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +000085 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +000086 NoProfileInfo() : ImmutablePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000087 };
Chris Lattner171de652004-02-10 22:11:42 +000088} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +000089
Dan Gohman844731a2008-05-13 00:00:25 +000090char NoProfileInfo::ID = 0;
91// Register this pass...
92static RegisterPass<NoProfileInfo>
93X("no-profile", "No Profile Information", false, true);
94
95// Declare that we implement the ProfileInfo interface
96static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
97
Jeff Cohen534927d2005-01-08 22:01:16 +000098ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }