blob: 8cb1e0017646ceb25b4bdbacaf894eb57f12a3e1 [file] [log] [blame]
Chris Lattner171de652004-02-10 22:11:42 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the abstract ProfileInfo interface, and the default
11// "no profile" implementation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileInfo.h"
16#include "llvm/Pass.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000017#include "llvm/Support/CFG.h"
18#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000019using namespace llvm;
20
Chris Lattnerecefc962004-02-11 06:10:18 +000021// Register the ProfileInfo interface, providing a nice name to refer to.
Chris Lattner171de652004-02-10 22:11:42 +000022namespace {
23 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
24}
25
26ProfileInfo::~ProfileInfo() {}
27
Chris Lattner96ab5ca2004-03-08 22:04:08 +000028unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
29 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
30
31 // Are there zero predecessors of this block?
32 if (PI == PE) {
33 // If this is the entry block, look for the Null -> Entry edge.
34 if (BB == &BB->getParent()->front())
35 return getEdgeWeight(0, BB);
36 else
37 return 0; // Otherwise, this is a dead block.
38 }
39
40 // Otherwise, if there are predecessors, the execution count of this block is
41 // the sum of the edge frequencies from the incoming edges. Note that if
42 // there are multiple edges from a predecessor to this block that we don't
43 // want to count its weight multiple times. For this reason, we keep track of
44 // the predecessors we've seen and only count them if we haven't run into them
45 // yet.
46 //
47 // We don't want to create an std::set unless we are dealing with a block that
48 // has a LARGE number of in-edges. Handle the common case of having only a
49 // few in-edges with special code.
50 //
51 BasicBlock *FirstPred = *PI;
52 unsigned Count = getEdgeWeight(FirstPred, BB);
53 ++PI;
54 if (PI == PE) return Count; // Quick exit for single predecessor blocks
55
56 BasicBlock *SecondPred = *PI;
57 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
58 ++PI;
59 if (PI == PE) return Count; // Quick exit for two predecessor blocks
60
61 BasicBlock *ThirdPred = *PI;
62 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
63 Count += getEdgeWeight(ThirdPred, BB);
64 ++PI;
65 if (PI == PE) return Count; // Quick exit for three predecessor blocks
66
67 std::set<BasicBlock*> ProcessedPreds;
68 ProcessedPreds.insert(FirstPred);
69 ProcessedPreds.insert(SecondPred);
70 ProcessedPreds.insert(ThirdPred);
71 for (; PI != PE; ++PI)
72 if (ProcessedPreds.insert(*PI).second)
73 Count += getEdgeWeight(*PI, BB);
74 return Count;
75}
76
77
Chris Lattner171de652004-02-10 22:11:42 +000078
79//===----------------------------------------------------------------------===//
80// NoProfile ProfileInfo implementation
81//
82
83namespace {
Chris Lattner62e84f32004-03-08 21:30:35 +000084 struct NoProfileInfo : public ImmutablePass, public ProfileInfo {};
Chris Lattner171de652004-02-10 22:11:42 +000085
86 // Register this pass...
87 RegisterOpt<NoProfileInfo>
88 X("no-profile", "No Profile Information");
89
Chris Lattnerecefc962004-02-11 06:10:18 +000090 // Declare that we implement the ProfileInfo interface
Chris Lattnerb0601942004-02-11 04:47:54 +000091 RegisterAnalysisGroup<ProfileInfo, NoProfileInfo, true> Y;
Chris Lattner171de652004-02-10 22:11:42 +000092} // End of anonymous namespace