blob: 6eead0fd6ecad2a040d2b1b3ebe333cbf8dea0d8 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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.
Chris Lattner171de652004-02-10 22:11:42 +000024namespace {
25 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
26}
27
28ProfileInfo::~ProfileInfo() {}
29
Chris Lattner96ab5ca2004-03-08 22:04:08 +000030unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
31 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
32
33 // Are there zero predecessors of this block?
34 if (PI == PE) {
35 // If this is the entry block, look for the Null -> Entry edge.
Dan Gohmanecb7a772007-03-22 16:38:57 +000036 if (BB == &BB->getParent()->getEntryBlock())
Chris Lattner96ab5ca2004-03-08 22:04:08 +000037 return getEdgeWeight(0, BB);
38 else
39 return 0; // Otherwise, this is a dead block.
40 }
41
42 // Otherwise, if there are predecessors, the execution count of this block is
43 // the sum of the edge frequencies from the incoming edges. Note that if
44 // there are multiple edges from a predecessor to this block that we don't
45 // want to count its weight multiple times. For this reason, we keep track of
46 // the predecessors we've seen and only count them if we haven't run into them
47 // yet.
48 //
49 // We don't want to create an std::set unless we are dealing with a block that
50 // has a LARGE number of in-edges. Handle the common case of having only a
51 // few in-edges with special code.
52 //
53 BasicBlock *FirstPred = *PI;
54 unsigned Count = getEdgeWeight(FirstPred, BB);
55 ++PI;
56 if (PI == PE) return Count; // Quick exit for single predecessor blocks
57
58 BasicBlock *SecondPred = *PI;
59 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
60 ++PI;
61 if (PI == PE) return Count; // Quick exit for two predecessor blocks
62
63 BasicBlock *ThirdPred = *PI;
64 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
65 Count += getEdgeWeight(ThirdPred, BB);
66 ++PI;
67 if (PI == PE) return Count; // Quick exit for three predecessor blocks
68
69 std::set<BasicBlock*> ProcessedPreds;
70 ProcessedPreds.insert(FirstPred);
71 ProcessedPreds.insert(SecondPred);
72 ProcessedPreds.insert(ThirdPred);
73 for (; PI != PE; ++PI)
74 if (ProcessedPreds.insert(*PI).second)
75 Count += getEdgeWeight(*PI, BB);
76 return Count;
77}
78
79
Chris Lattner171de652004-02-10 22:11:42 +000080
81//===----------------------------------------------------------------------===//
82// NoProfile ProfileInfo implementation
83//
84
85namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000086 struct VISIBILITY_HIDDEN NoProfileInfo
87 : public ImmutablePass, public ProfileInfo {};
Misha Brukman2b37d7c2005-04-21 21:13:18 +000088
Chris Lattner171de652004-02-10 22:11:42 +000089 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000090 RegisterPass<NoProfileInfo>
Chris Lattner171de652004-02-10 22:11:42 +000091 X("no-profile", "No Profile Information");
92
Chris Lattnerecefc962004-02-11 06:10:18 +000093 // Declare that we implement the ProfileInfo interface
Chris Lattnera5370172006-08-28 00:42:29 +000094 RegisterAnalysisGroup<ProfileInfo, true> Y(X);
Chris Lattner171de652004-02-10 22:11:42 +000095} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +000096
97ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }