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/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"
23 // Register the ProfileInfo interface, providing a nice name to refer to.
25 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
27 char ProfileInfo::ID = 0;
29 ProfileInfo::~ProfileInfo() {}
31 unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
32 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
34 // Are there zero predecessors of this block?
36 // If this is the entry block, look for the Null -> Entry edge.
37 if (BB == &BB->getParent()->getEntryBlock())
38 return getEdgeWeight(0, BB);
40 return 0; // Otherwise, this is a dead block.
43 // Otherwise, if there are predecessors, the execution count of this block is
44 // the sum of the edge frequencies from the incoming edges. Note that if
45 // there are multiple edges from a predecessor to this block that we don't
46 // want to count its weight multiple times. For this reason, we keep track of
47 // the predecessors we've seen and only count them if we haven't run into them
50 // We don't want to create an std::set unless we are dealing with a block that
51 // has a LARGE number of in-edges. Handle the common case of having only a
52 // few in-edges with special code.
54 BasicBlock *FirstPred = *PI;
55 unsigned Count = getEdgeWeight(FirstPred, BB);
57 if (PI == PE) return Count; // Quick exit for single predecessor blocks
59 BasicBlock *SecondPred = *PI;
60 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
62 if (PI == PE) return Count; // Quick exit for two predecessor blocks
64 BasicBlock *ThirdPred = *PI;
65 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
66 Count += getEdgeWeight(ThirdPred, BB);
68 if (PI == PE) return Count; // Quick exit for three predecessor blocks
70 std::set<BasicBlock*> ProcessedPreds;
71 ProcessedPreds.insert(FirstPred);
72 ProcessedPreds.insert(SecondPred);
73 ProcessedPreds.insert(ThirdPred);
74 for (; PI != PE; ++PI)
75 if (ProcessedPreds.insert(*PI).second)
76 Count += getEdgeWeight(*PI, BB);
82 //===----------------------------------------------------------------------===//
83 // NoProfile ProfileInfo implementation
87 struct VISIBILITY_HIDDEN NoProfileInfo
88 : public ImmutablePass, public ProfileInfo {
89 static char ID; // Class identification, replacement for typeinfo
90 NoProfileInfo() : ImmutablePass((intptr_t)&ID) {}
93 char NoProfileInfo::ID = 0;
94 // Register this pass...
95 RegisterPass<NoProfileInfo>
96 X("no-profile", "No Profile Information");
98 // Declare that we implement the ProfileInfo interface
99 RegisterAnalysisGroup<ProfileInfo, true> Y(X);
100 } // End of anonymous namespace
102 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }