blob: 9efdd23081c41befb4fb255cce17237bb47d4519 [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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000019#include "llvm/Support/Compiler.h"
Andreas Neustifter07abe172009-09-09 17:52:57 +000020#include "llvm/Support/Debug.h"
Dan Gohman4bac4b92009-08-26 15:56:38 +000021#include "llvm/Support/raw_ostream.h"
Andreas Neustifter07abe172009-09-09 17:52:57 +000022#include "llvm/Support/Format.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000023#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000024using namespace llvm;
25
Chris Lattnerecefc962004-02-11 06:10:18 +000026// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000027static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Devang Patel19974732007-05-03 01:11:54 +000028char ProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000029
30ProfileInfo::~ProfileInfo() {}
31
Andreas Neustifter96135b62009-08-24 21:37:48 +000032const double ProfileInfo::MissingValue = -1;
33
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000034double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
35 std::map<const Function*, BlockCounts>::iterator J =
36 BlockInformation.find(BB->getParent());
37 if (J != BlockInformation.end()) {
38 BlockCounts::iterator I = J->second.find(BB);
39 if (I != J->second.end())
40 return I->second;
41 }
Daniel Dunbarc9008c52009-08-05 21:51:16 +000042
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000043 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000044
45 // Are there zero predecessors of this block?
46 if (PI == PE) {
47 // If this is the entry block, look for the Null -> Entry edge.
Dan Gohmanecb7a772007-03-22 16:38:57 +000048 if (BB == &BB->getParent()->getEntryBlock())
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000049 return getEdgeWeight(getEdge(0, BB));
Chris Lattner96ab5ca2004-03-08 22:04:08 +000050 else
51 return 0; // Otherwise, this is a dead block.
52 }
53
54 // Otherwise, if there are predecessors, the execution count of this block is
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000055 // the sum of the edge frequencies from the incoming edges.
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000056 std::set<const BasicBlock*> ProcessedPreds;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000057 double Count = 0;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000058 for (; PI != PE; ++PI)
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000059 if (ProcessedPreds.insert(*PI).second) {
60 double w = getEdgeWeight(getEdge(*PI, BB));
61 if (w == MissingValue) {
Andreas Neustifter0a324aa2009-08-19 05:44:39 +000062 Count = MissingValue;
63 break;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000064 }
65 Count += w;
66 }
67
Andreas Neustifter96135b62009-08-24 21:37:48 +000068 if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
Chris Lattner96ab5ca2004-03-08 22:04:08 +000069 return Count;
70}
71
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000072double ProfileInfo::getExecutionCount(const Function *F) {
73 std::map<const Function*, double>::iterator J =
74 FunctionInformation.find(F);
75 if (J != FunctionInformation.end())
76 return J->second;
Daniel Dunbarc9008c52009-08-05 21:51:16 +000077
Andreas Neustifter3772fb12009-08-26 13:33:09 +000078 // isDeclaration() is checked here and not at start of function to allow
79 // functions without a body still to have a execution count.
80 if (F->isDeclaration()) return MissingValue;
81
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000082 double Count = getExecutionCount(&F->getEntryBlock());
Andreas Neustifter96135b62009-08-24 21:37:48 +000083 if (Count != MissingValue) FunctionInformation[F] = Count;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000084 return Count;
Daniel Dunbar858cb8a2009-07-14 06:58:59 +000085}
Chris Lattner96ab5ca2004-03-08 22:04:08 +000086
Andreas Neustifter07abe172009-09-09 17:52:57 +000087/// Replaces all occurences of RmBB in the ProfilingInfo with DestBB.
88/// This checks all edges of the function the blocks reside in and replaces the
89/// occurences of RmBB with DestBB.
90void ProfileInfo::replaceAllUses(const BasicBlock *RmBB,
91 const BasicBlock *DestBB) {
92 DEBUG(errs() << "Replacing " << RmBB->getNameStr()
93 << " with " << DestBB->getNameStr() << "\n");
94 const Function *F = DestBB->getParent();
95 std::map<const Function*, EdgeWeights>::iterator J =
96 EdgeInformation.find(F);
97 if (J == EdgeInformation.end()) return;
98
99 for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
100 I != E; ++I) {
101 Edge e = I->first;
102 Edge newedge; bool foundedge = false;
103 if (e.first == RmBB) {
104 newedge = getEdge(DestBB, e.second);
105 foundedge = true;
106 }
107 if (e.second == RmBB) {
108 newedge = getEdge(e.first, DestBB);
109 foundedge = true;
110 }
111 if (foundedge) {
112 double w = getEdgeWeight(e);
113 EdgeInformation[F][newedge] = w;
114 DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n");
115 J->second.erase(e);
116 }
117 }
118}
119
120/// Splits an edge in the ProfileInfo and redirects flow over NewBB.
121/// Since its possible that there is more than one edge in the CFG from FristBB
122/// to SecondBB its necessary to redirect the flow proporionally.
123void ProfileInfo::splitEdge(const BasicBlock *FirstBB,
124 const BasicBlock *SecondBB,
125 const BasicBlock *NewBB,
126 bool MergeIdenticalEdges) {
127 const Function *F = FirstBB->getParent();
128 std::map<const Function*, EdgeWeights>::iterator J =
129 EdgeInformation.find(F);
130 if (J == EdgeInformation.end()) return;
131
132 // Generate edges and read current weight.
133 Edge e = getEdge(FirstBB, SecondBB);
134 Edge n1 = getEdge(FirstBB, NewBB);
135 Edge n2 = getEdge(NewBB, SecondBB);
136 EdgeWeights &ECs = J->second;
137 double w = ECs[e];
138
139 int succ_count = 0;
140 if (!MergeIdenticalEdges) {
141 // First count the edges from FristBB to SecondBB, if there is more than
142 // one, only slice out a proporional part for NewBB.
143 for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
144 BBI != BBE; ++BBI) {
145 if (*BBI == SecondBB) succ_count++;
146 }
147 // When the NewBB is completely new, increment the count by one so that
148 // the counts are properly distributed.
149 if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
150 } else {
151 // When the edges are merged anyway, then redirect all flow.
152 succ_count = 1;
153 }
154
155 // We know now how many edges there are from FirstBB to SecondBB, reroute a
156 // proportional part of the edge weight over NewBB.
157 double neww = w / succ_count;
158 ECs[n1] += neww;
159 ECs[n2] += neww;
160 BlockInformation[F][NewBB] += neww;
161 if (succ_count == 1) {
162 ECs.erase(e);
163 } else {
164 ECs[e] -= neww;
165 }
166}
167
Dan Gohman4bac4b92009-08-26 15:56:38 +0000168raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) {
169 O << "(";
170 O << (E.first ? E.first->getNameStr() : "0");
171 O << ",";
172 O << (E.second ? E.second->getNameStr() : "0");
173 return O << ")";
174}
Chris Lattner171de652004-02-10 22:11:42 +0000175
176//===----------------------------------------------------------------------===//
177// NoProfile ProfileInfo implementation
178//
179
180namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000181 struct VISIBILITY_HIDDEN NoProfileInfo
Devang Patel794fd752007-05-01 21:15:47 +0000182 : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +0000183 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000184 NoProfileInfo() : ImmutablePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000185 };
Chris Lattner171de652004-02-10 22:11:42 +0000186} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +0000187
Dan Gohman844731a2008-05-13 00:00:25 +0000188char NoProfileInfo::ID = 0;
189// Register this pass...
190static RegisterPass<NoProfileInfo>
191X("no-profile", "No Profile Information", false, true);
192
193// Declare that we implement the ProfileInfo interface
194static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
195
Jeff Cohen534927d2005-01-08 22:01:16 +0000196ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }