blob: a7fc4c49d5ee2b36ffc386269c98bcd0b17e6dbd [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
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000029const double ProfileInfo::MissingValue = -1;
30
Daniel Dunbar4ae20272009-08-08 17:43:09 +000031double 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 }
Daniel Dunbar23505092009-08-05 21:51:16 +000039
Daniel Dunbar042521c2009-07-14 06:58:59 +000040 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
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())
Daniel Dunbar4ae20272009-08-08 17:43:09 +000046 return getEdgeWeight(getEdge(0, BB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 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
Daniel Dunbar4ae20272009-08-08 17:43:09 +000052 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar042521c2009-07-14 06:58:59 +000053 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000054 double Count = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 for (; PI != PE; ++PI)
Daniel Dunbar4ae20272009-08-08 17:43:09 +000056 if (ProcessedPreds.insert(*PI).second) {
57 double w = getEdgeWeight(getEdge(*PI, BB));
58 if (w == MissingValue) {
Andreas Neustifter375a8bc2009-08-19 05:44:39 +000059 Count = MissingValue;
60 break;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000061 }
62 Count += w;
63 }
64
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000065 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return Count;
67}
68
Daniel Dunbar4ae20272009-08-08 17:43:09 +000069double ProfileInfo::getExecutionCount(const Function *F) {
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000070 if (F->isDeclaration()) return MissingValue;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000071 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
Daniel Dunbar4ae20272009-08-08 17:43:09 +000076 double Count = getExecutionCount(&F->getEntryBlock());
Andreas Neustifter92b39ac2009-08-24 21:37:48 +000077 if (Count != MissingValue) FunctionInformation[F] = Count;
Daniel Dunbar4ae20272009-08-08 17:43:09 +000078 return Count;
Daniel Dunbar042521c2009-07-14 06:58:59 +000079}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
81
82//===----------------------------------------------------------------------===//
83// NoProfile ProfileInfo implementation
84//
85
86namespace {
87 struct VISIBILITY_HIDDEN NoProfileInfo
88 : public ImmutablePass, public ProfileInfo {
89 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000090 NoProfileInfo() : ImmutablePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092} // End of anonymous namespace
93
Dan Gohman089efff2008-05-13 00:00:25 +000094char NoProfileInfo::ID = 0;
95// Register this pass...
96static RegisterPass<NoProfileInfo>
97X("no-profile", "No Profile Information", false, true);
98
99// Declare that we implement the ProfileInfo interface
100static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }