1 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the abstract ProfileInfo interface, and the default
11 // "no profile" implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ProfileInfo.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/CFG.h"
21 // Register the ProfileInfo interface, providing a nice name to refer to.
23 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
26 ProfileInfo::~ProfileInfo() {}
28 unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
29 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
31 // Are there zero predecessors of this block?
33 // If this is the entry block, look for the Null -> Entry edge.
34 if (BB == &BB->getParent()->front())
35 return getEdgeWeight(0, BB);
37 return 0; // Otherwise, this is a dead block.
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
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.
51 BasicBlock *FirstPred = *PI;
52 unsigned Count = getEdgeWeight(FirstPred, BB);
54 if (PI == PE) return Count; // Quick exit for single predecessor blocks
56 BasicBlock *SecondPred = *PI;
57 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
59 if (PI == PE) return Count; // Quick exit for two predecessor blocks
61 BasicBlock *ThirdPred = *PI;
62 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
63 Count += getEdgeWeight(ThirdPred, BB);
65 if (PI == PE) return Count; // Quick exit for three predecessor blocks
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);
79 //===----------------------------------------------------------------------===//
80 // NoProfile ProfileInfo implementation
84 struct NoProfileInfo : public ImmutablePass, public ProfileInfo {};
86 // Register this pass...
87 RegisterOpt<NoProfileInfo>
88 X("no-profile", "No Profile Information");
90 // Declare that we implement the ProfileInfo interface
91 RegisterAnalysisGroup<ProfileInfo, NoProfileInfo, true> Y;
92 } // End of anonymous namespace