1 //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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.
24 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
25 char ProfileInfo::ID = 0;
27 ProfileInfo::~ProfileInfo() {}
29 unsigned ProfileInfo::getExecutionCount(const BasicBlock *BB) const {
30 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
32 // Are there zero predecessors of this block?
34 // If this is the entry block, look for the Null -> Entry edge.
35 if (BB == &BB->getParent()->getEntryBlock())
36 return getEdgeWeight(0, BB);
38 return 0; // Otherwise, this is a dead block.
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
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.
52 const BasicBlock *FirstPred = *PI;
53 unsigned Count = getEdgeWeight(FirstPred, BB);
55 if (PI == PE) return Count; // Quick exit for single predecessor blocks
57 const BasicBlock *SecondPred = *PI;
58 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
60 if (PI == PE) return Count; // Quick exit for two predecessor blocks
62 const BasicBlock *ThirdPred = *PI;
63 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
64 Count += getEdgeWeight(ThirdPred, BB);
66 if (PI == PE) return Count; // Quick exit for three predecessor blocks
68 std::set<const BasicBlock*> ProcessedPreds;
69 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);
78 unsigned ProfileInfo::getExecutionCount(const Function *F) const {
79 if (F->isDeclaration()) return -1;
80 return getExecutionCount(&F->getEntryBlock());
84 //===----------------------------------------------------------------------===//
85 // NoProfile ProfileInfo implementation
89 struct VISIBILITY_HIDDEN NoProfileInfo
90 : public ImmutablePass, public ProfileInfo {
91 static char ID; // Class identification, replacement for typeinfo
92 NoProfileInfo() : ImmutablePass(&ID) {}
94 } // End of anonymous namespace
96 char NoProfileInfo::ID = 0;
97 // Register this pass...
98 static RegisterPass<NoProfileInfo>
99 X("no-profile", "No Profile Information", false, true);
101 // Declare that we implement the ProfileInfo interface
102 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
104 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }