blob: 7f24f5a238ebff5cb18697c1092d748dd8321dff [file] [log] [blame]
Chris Lattner171de652004-02-10 22:11:42 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner171de652004-02-10 22:11:42 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner171de652004-02-10 22:11:42 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the abstract ProfileInfo interface, and the default
11// "no profile" implementation.
12//
13//===----------------------------------------------------------------------===//
14
Jeff Cohen534927d2005-01-08 22:01:16 +000015#include "llvm/Analysis/Passes.h"
Chris Lattner171de652004-02-10 22:11:42 +000016#include "llvm/Analysis/ProfileInfo.h"
17#include "llvm/Pass.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000018#include "llvm/Support/CFG.h"
Andreas Neustifter07abe172009-09-09 17:52:57 +000019#include "llvm/Support/Debug.h"
Dan Gohman4bac4b92009-08-26 15:56:38 +000020#include "llvm/Support/raw_ostream.h"
Andreas Neustifter07abe172009-09-09 17:52:57 +000021#include "llvm/Support/Format.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000022#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000023using namespace llvm;
24
Chris Lattnerecefc962004-02-11 06:10:18 +000025// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000026static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Devang Patel19974732007-05-03 01:11:54 +000027char ProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000028
29ProfileInfo::~ProfileInfo() {}
30
Andreas Neustifter96135b62009-08-24 21:37:48 +000031const double ProfileInfo::MissingValue = -1;
32
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000033double 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())
39 return I->second;
40 }
Daniel Dunbarc9008c52009-08-05 21:51:16 +000041
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000042 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000043
44 // Are there zero predecessors of this block?
45 if (PI == PE) {
46 // If this is the entry block, look for the Null -> Entry edge.
Dan Gohmanecb7a772007-03-22 16:38:57 +000047 if (BB == &BB->getParent()->getEntryBlock())
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000048 return getEdgeWeight(getEdge(0, BB));
Chris Lattner96ab5ca2004-03-08 22:04:08 +000049 else
50 return 0; // Otherwise, this is a dead block.
51 }
52
53 // Otherwise, if there are predecessors, the execution count of this block is
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000054 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000055 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000056 double Count = 0;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000057 for (; PI != PE; ++PI)
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000058 if (ProcessedPreds.insert(*PI).second) {
59 double w = getEdgeWeight(getEdge(*PI, BB));
60 if (w == MissingValue) {
Andreas Neustifter0a324aa2009-08-19 05:44:39 +000061 Count = MissingValue;
62 break;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000063 }
64 Count += w;
65 }
66
Andreas Neustifter96135b62009-08-24 21:37:48 +000067 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000068 return Count;
69}
70
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000071double ProfileInfo::getExecutionCount(const Function *F) {
72 std::map<const Function*, double>::iterator J =
73 FunctionInformation.find(F);
74 if (J != FunctionInformation.end())
75 return J->second;
Daniel Dunbarc9008c52009-08-05 21:51:16 +000076
Andreas Neustifter3772fb12009-08-26 13:33:09 +000077 // 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;
80
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000081 double Count = getExecutionCount(&F->getEntryBlock());
Andreas Neustifter96135b62009-08-24 21:37:48 +000082 if (Count != MissingValue) FunctionInformation[F] = Count;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000083 return Count;
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000084}
Chris Lattner96ab5ca2004-03-08 22:04:08 +000085
Andreas Neustifter07abe172009-09-09 17:52:57 +000086/// 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.
89void 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;
97
98 for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
99 I != E; ++I) {
100 Edge e = I->first;
101 Edge newedge; bool foundedge = false;
102 if (e.first == RmBB) {
103 newedge = getEdge(DestBB, e.second);
104 foundedge = true;
105 }
106 if (e.second == RmBB) {
107 newedge = getEdge(e.first, DestBB);
108 foundedge = true;
109 }
110 if (foundedge) {
111 double w = getEdgeWeight(e);
112 EdgeInformation[F][newedge] = w;
113 DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n");
114 J->second.erase(e);
115 }
116 }
117}
118
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.
122void 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;
130
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;
136 double w = ECs[e];
137
138 int succ_count = 0;
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);
143 BBI != BBE; ++BBI) {
144 if (*BBI == SecondBB) succ_count++;
145 }
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++;
149 } else {
150 // When the edges are merged anyway, then redirect all flow.
151 succ_count = 1;
152 }
153
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;
157 ECs[n1] += neww;
158 ECs[n2] += neww;
159 BlockInformation[F][NewBB] += neww;
160 if (succ_count == 1) {
161 ECs.erase(e);
162 } else {
163 ECs[e] -= neww;
164 }
165}
166
Dan Gohman4bac4b92009-08-26 15:56:38 +0000167raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) {
168 O << "(";
169 O << (E.first ? E.first->getNameStr() : "0");
170 O << ",";
171 O << (E.second ? E.second->getNameStr() : "0");
172 return O << ")";
173}
Chris Lattner171de652004-02-10 22:11:42 +0000174
175//===----------------------------------------------------------------------===//
176// NoProfile ProfileInfo implementation
177//
178
179namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000180 struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +0000181 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000182 NoProfileInfo() : ImmutablePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000183 };
Chris Lattner171de652004-02-10 22:11:42 +0000184} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +0000185
Dan Gohman844731a2008-05-13 00:00:25 +0000186char NoProfileInfo::ID = 0;
187// Register this pass...
188static RegisterPass<NoProfileInfo>
189X("no-profile", "No Profile Information", false, true);
190
191// Declare that we implement the ProfileInfo interface
192static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
193
Jeff Cohen534927d2005-01-08 22:01:16 +0000194ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }