blob: 26328d073cf33e46423739e05c2b25ac322931f5 [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 Dunbar858cb8a2009-07-14 06:58:59 +000029unsigned ProfileInfo::getExecutionCount(const BasicBlock *BB) const {
30 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000031
32 // Are there zero predecessors of this block?
33 if (PI == PE) {
34 // If this is the entry block, look for the Null -> Entry edge.
Dan Gohmanecb7a772007-03-22 16:38:57 +000035 if (BB == &BB->getParent()->getEntryBlock())
Chris Lattner96ab5ca2004-03-08 22:04:08 +000036 return getEdgeWeight(0, BB);
37 else
38 return 0; // Otherwise, this is a dead block.
39 }
40
41 // Otherwise, if there are predecessors, the execution count of this block is
42 // the sum of the edge frequencies from the incoming edges. Note that if
43 // there are multiple edges from a predecessor to this block that we don't
44 // want to count its weight multiple times. For this reason, we keep track of
45 // the predecessors we've seen and only count them if we haven't run into them
46 // yet.
47 //
48 // We don't want to create an std::set unless we are dealing with a block that
49 // has a LARGE number of in-edges. Handle the common case of having only a
50 // few in-edges with special code.
51 //
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000052 const BasicBlock *FirstPred = *PI;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000053 unsigned Count = getEdgeWeight(FirstPred, BB);
54 ++PI;
55 if (PI == PE) return Count; // Quick exit for single predecessor blocks
56
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000057 const BasicBlock *SecondPred = *PI;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000058 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
59 ++PI;
60 if (PI == PE) return Count; // Quick exit for two predecessor blocks
61
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000062 const BasicBlock *ThirdPred = *PI;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000063 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
64 Count += getEdgeWeight(ThirdPred, BB);
65 ++PI;
66 if (PI == PE) return Count; // Quick exit for three predecessor blocks
67
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000068 std::set<const BasicBlock*> ProcessedPreds;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000069 ProcessedPreds.insert(FirstPred);
70 ProcessedPreds.insert(SecondPred);
71 ProcessedPreds.insert(ThirdPred);
72 for (; PI != PE; ++PI)
73 if (ProcessedPreds.insert(*PI).second)
74 Count += getEdgeWeight(*PI, BB);
75 return Count;
76}
77
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000078unsigned ProfileInfo::getExecutionCount(const Function *F) const {
79 if (F->isDeclaration()) return -1;
80 return getExecutionCount(&F->getEntryBlock());
81}
Chris Lattner96ab5ca2004-03-08 22:04:08 +000082
Chris Lattner171de652004-02-10 22:11:42 +000083
84//===----------------------------------------------------------------------===//
85// NoProfile ProfileInfo implementation
86//
87
88namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000089 struct VISIBILITY_HIDDEN NoProfileInfo
Devang Patel794fd752007-05-01 21:15:47 +000090 : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +000091 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +000092 NoProfileInfo() : ImmutablePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000093 };
Chris Lattner171de652004-02-10 22:11:42 +000094} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +000095
Dan Gohman844731a2008-05-13 00:00:25 +000096char NoProfileInfo::ID = 0;
97// Register this pass...
98static RegisterPass<NoProfileInfo>
99X("no-profile", "No Profile Information", false, true);
100
101// Declare that we implement the ProfileInfo interface
102static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
103
Jeff Cohen534927d2005-01-08 22:01:16 +0000104ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }