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/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Format.h"
25 // Register the ProfileInfo interface, providing a nice name to refer to.
26 static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
27 char ProfileInfo::ID = 0;
29 ProfileInfo::~ProfileInfo() {}
31 const double ProfileInfo::MissingValue = -1;
33 double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
34 std::map<const Function*, BlockCounts>::iterator J =
35 BlockInformation.find(BB->getParent());
36 if (J != BlockInformation.end()) {
37 BlockCounts::iterator I = J->second.find(BB);
38 if (I != J->second.end())
42 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
44 // Are there zero predecessors of this block?
46 // If this is the entry block, look for the Null -> Entry edge.
47 if (BB == &BB->getParent()->getEntryBlock())
48 return getEdgeWeight(getEdge(0, BB));
50 return 0; // Otherwise, this is a dead block.
53 // Otherwise, if there are predecessors, the execution count of this block is
54 // the sum of the edge frequencies from the incoming edges.
55 std::set<const BasicBlock*> ProcessedPreds;
57 for (; PI != PE; ++PI)
58 if (ProcessedPreds.insert(*PI).second) {
59 double w = getEdgeWeight(getEdge(*PI, BB));
60 if (w == MissingValue) {
67 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
71 double ProfileInfo::getExecutionCount(const Function *F) {
72 std::map<const Function*, double>::iterator J =
73 FunctionInformation.find(F);
74 if (J != FunctionInformation.end())
77 // isDeclaration() is checked here and not at start of function to allow
78 // functions without a body still to have a execution count.
79 if (F->isDeclaration()) return MissingValue;
81 double Count = getExecutionCount(&F->getEntryBlock());
82 if (Count != MissingValue) FunctionInformation[F] = Count;
86 /// Replaces all occurences of RmBB in the ProfilingInfo with DestBB.
87 /// This checks all edges of the function the blocks reside in and replaces the
88 /// occurences of RmBB with DestBB.
89 void ProfileInfo::replaceAllUses(const BasicBlock *RmBB,
90 const BasicBlock *DestBB) {
91 DEBUG(errs() << "Replacing " << RmBB->getNameStr()
92 << " with " << DestBB->getNameStr() << "\n");
93 const Function *F = DestBB->getParent();
94 std::map<const Function*, EdgeWeights>::iterator J =
95 EdgeInformation.find(F);
96 if (J == EdgeInformation.end()) return;
98 for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
101 Edge newedge; bool foundedge = false;
102 if (e.first == RmBB) {
103 newedge = getEdge(DestBB, e.second);
106 if (e.second == RmBB) {
107 newedge = getEdge(e.first, DestBB);
111 double w = getEdgeWeight(e);
112 EdgeInformation[F][newedge] = w;
113 DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n");
119 /// Splits an edge in the ProfileInfo and redirects flow over NewBB.
120 /// Since its possible that there is more than one edge in the CFG from FristBB
121 /// to SecondBB its necessary to redirect the flow proporionally.
122 void ProfileInfo::splitEdge(const BasicBlock *FirstBB,
123 const BasicBlock *SecondBB,
124 const BasicBlock *NewBB,
125 bool MergeIdenticalEdges) {
126 const Function *F = FirstBB->getParent();
127 std::map<const Function*, EdgeWeights>::iterator J =
128 EdgeInformation.find(F);
129 if (J == EdgeInformation.end()) return;
131 // Generate edges and read current weight.
132 Edge e = getEdge(FirstBB, SecondBB);
133 Edge n1 = getEdge(FirstBB, NewBB);
134 Edge n2 = getEdge(NewBB, SecondBB);
135 EdgeWeights &ECs = J->second;
139 if (!MergeIdenticalEdges) {
140 // First count the edges from FristBB to SecondBB, if there is more than
141 // one, only slice out a proporional part for NewBB.
142 for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
144 if (*BBI == SecondBB) succ_count++;
146 // When the NewBB is completely new, increment the count by one so that
147 // the counts are properly distributed.
148 if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
150 // When the edges are merged anyway, then redirect all flow.
154 // We know now how many edges there are from FirstBB to SecondBB, reroute a
155 // proportional part of the edge weight over NewBB.
156 double neww = w / succ_count;
159 BlockInformation[F][NewBB] += neww;
160 if (succ_count == 1) {
167 raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) {
169 O << (E.first ? E.first->getNameStr() : "0");
171 O << (E.second ? E.second->getNameStr() : "0");
175 //===----------------------------------------------------------------------===//
176 // NoProfile ProfileInfo implementation
180 struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
181 static char ID; // Class identification, replacement for typeinfo
182 NoProfileInfo() : ImmutablePass(&ID) {}
184 } // End of anonymous namespace
186 char NoProfileInfo::ID = 0;
187 // Register this pass...
188 static RegisterPass<NoProfileInfo>
189 X("no-profile", "No Profile Information", false, true);
191 // Declare that we implement the ProfileInfo interface
192 static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
194 ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }