blob: e767891eab3c81b13fe4006a6770898f59d4b607 [file] [log] [blame]
Daniel Dunbar55e354a2009-08-08 18:44:18 +00001//===- ProfileEstimatorPass.cpp - LLVM Pass to estimate profile info ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// estimates the profiling information in a very crude and unimaginative way.
12//
13//===----------------------------------------------------------------------===//
14#define DEBUG_TYPE "profile-estimator"
15#include "llvm/Pass.h"
16#include "llvm/Analysis/Passes.h"
17#include "llvm/Analysis/ProfileInfo.h"
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Andreas Neustifterff271e12009-08-26 15:13:44 +000022#include "llvm/Support/Format.h"
Daniel Dunbar55e354a2009-08-08 18:44:18 +000023using namespace llvm;
24
25static cl::opt<double>
Andreas Neustifterff271e12009-08-26 15:13:44 +000026LoopWeight(
Daniel Dunbar55e354a2009-08-08 18:44:18 +000027 "profile-estimator-loop-weight", cl::init(10),
28 cl::value_desc("loop-weight"),
29 cl::desc("Number of loop executions used for profile-estimator")
30);
31
32namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000033 class ProfileEstimatorPass : public FunctionPass, public ProfileInfo {
Daniel Dunbar55e354a2009-08-08 18:44:18 +000034 double ExecCount;
35 LoopInfo *LI;
Andreas Neustifter8a58c182009-09-11 08:39:33 +000036 std::set<BasicBlock*> BBToVisit;
Daniel Dunbar55e354a2009-08-08 18:44:18 +000037 std::map<Loop*,double> LoopExitWeights;
38 public:
39 static char ID; // Class identification, replacement for typeinfo
40 explicit ProfileEstimatorPass(const double execcount = 0)
41 : FunctionPass(&ID), ExecCount(execcount) {
Andreas Neustifterff271e12009-08-26 15:13:44 +000042 if (execcount == 0) ExecCount = LoopWeight;
Daniel Dunbar55e354a2009-08-08 18:44:18 +000043 }
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 AU.addRequired<LoopInfo>();
48 }
49
50 virtual const char *getPassName() const {
51 return "Profiling information estimator";
52 }
53
54 /// run - Estimate the profile information from the specified file.
55 virtual bool runOnFunction(Function &F);
56
Andreas Neustifter8a58c182009-09-11 08:39:33 +000057 virtual void recurseBasicBlock(BasicBlock *BB);
Andreas Neustifterff271e12009-08-26 15:13:44 +000058
59 void inline printEdgeWeight(Edge);
Daniel Dunbar55e354a2009-08-08 18:44:18 +000060 };
61} // End of anonymous namespace
62
63char ProfileEstimatorPass::ID = 0;
64static RegisterPass<ProfileEstimatorPass>
65X("profile-estimator", "Estimate profiling information", false, true);
66
67static RegisterAnalysisGroup<ProfileInfo> Y(X);
68
69namespace llvm {
70 const PassInfo *ProfileEstimatorPassID = &X;
71
72 FunctionPass *createProfileEstimatorPass() {
73 return new ProfileEstimatorPass();
74 }
75
Andreas Neustifterff271e12009-08-26 15:13:44 +000076 /// createProfileEstimatorPass - This function returns a Pass that estimates
77 /// profiling information using the given loop execution count.
Daniel Dunbar55e354a2009-08-08 18:44:18 +000078 Pass *createProfileEstimatorPass(const unsigned execcount) {
79 return new ProfileEstimatorPass(execcount);
80 }
81}
82
83static double ignoreMissing(double w) {
84 if (w == ProfileInfo::MissingValue) return 0;
85 return w;
86}
87
Andreas Neustifter8a58c182009-09-11 08:39:33 +000088static void inline printEdgeError(ProfileInfo::Edge e, const char *M) {
89 DEBUG(errs() << "-- Edge " << e << " is not calculated, " << M << "\n");
Andreas Neustifterff271e12009-08-26 15:13:44 +000090}
Daniel Dunbar55e354a2009-08-08 18:44:18 +000091
Andreas Neustifterff271e12009-08-26 15:13:44 +000092void inline ProfileEstimatorPass::printEdgeWeight(Edge E) {
93 DEBUG(errs() << "-- Weight of Edge " << E << ":"
94 << format("%g", getEdgeWeight(E)) << "\n");
95}
Daniel Dunbar55e354a2009-08-08 18:44:18 +000096
97// recurseBasicBlock() - This calculates the ProfileInfo estimation for a
98// single block and then recurses into the successors.
Andreas Neustifter8a58c182009-09-11 08:39:33 +000099// The algorithm preserves the flow condition, meaning that the sum of the
100// weight of the incoming edges must be equal the block weight which must in
101// turn be equal to the sume of the weights of the outgoing edges.
102// Since the flow of an block is deterimined from the current state of the
103// flow, once an edge has a flow assigned this flow is never changed again,
104// otherwise it would be possible to violate the flow condition in another
105// block.
106void ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) {
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000107
Andreas Neustifterff271e12009-08-26 15:13:44 +0000108 // Break the recursion if this BasicBlock was already visited.
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000109 if (BBToVisit.find(BB) == BBToVisit.end()) return;
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000110
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000111 // Read the LoopInfo for this block.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000112 bool BBisHeader = LI->isLoopHeader(BB);
113 Loop* BBLoop = LI->getLoopFor(BB);
114
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000115 // To get the block weight, read all incoming edges.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000116 double BBWeight = 0;
117 std::set<BasicBlock*> ProcessedPreds;
118 for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
119 bbi != bbe; ++bbi ) {
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000120 // If this block was not considered already, add weight.
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000121 Edge edge = getEdge(*bbi,BB);
122 double w = getEdgeWeight(edge);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000123 if (ProcessedPreds.insert(*bbi).second) {
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000124 BBWeight += ignoreMissing(w);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000125 }
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000126 // If this block is a loop header and the predecessor is contained in this
127 // loop, thus the edge is a backedge, continue and do not check if the
128 // value is valid.
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000129 if (BBisHeader && BBLoop->contains(*bbi)) {
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000130 printEdgeError(edge, "but is backedge, continueing");
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000131 continue;
132 }
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000133 // If the edges value is missing (and this is no loop header, and this is
134 // no backedge) return, this block is currently non estimatable.
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000135 if (w == MissingValue) {
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000136 printEdgeError(edge, "returning");
137 return;
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000138 }
139 }
140 if (getExecutionCount(BB) != MissingValue) {
141 BBWeight = getExecutionCount(BB);
142 }
143
Andreas Neustifterff271e12009-08-26 15:13:44 +0000144 // Fetch all necessary information for current block.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000145 SmallVector<Edge, 8> ExitEdges;
146 SmallVector<Edge, 8> Edges;
147 if (BBLoop) {
148 BBLoop->getExitEdges(ExitEdges);
149 }
150
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000151 // If this is a loop header, consider the following:
152 // Exactly the flow that is entering this block, must exit this block too. So
153 // do the following:
154 // *) get all the exit edges, read the flow that is already leaving this
155 // loop, remember the edges that do not have any flow on them right now.
156 // (The edges that have already flow on them are most likely exiting edges of
157 // other loops, do not touch those flows because the previously caclulated
158 // loopheaders would not be exact anymore.)
159 // *) In case there is not a single exiting edge left, create one at the loop
160 // latch to prevent the flow from building up in the loop.
161 // *) Take the flow that is not leaving the loop already and distribute it on
162 // the remaining exiting edges.
163 // (This ensures that all flow that enters the loop also leaves it.)
164 // *) Increase the flow into the loop by increasing the weight of this block.
165 // There is at least one incoming backedge that will bring us this flow later
166 // on. (So that the flow condition in this node is valid again.)
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000167 if (BBisHeader) {
168 double incoming = BBWeight;
Andreas Neustifterff271e12009-08-26 15:13:44 +0000169 // Subtract the flow leaving the loop.
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000170 std::set<Edge> ProcessedExits;
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000171 for (SmallVector<Edge, 8>::iterator ei = ExitEdges.begin(),
172 ee = ExitEdges.end(); ei != ee; ++ei) {
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000173 if (ProcessedExits.insert(*ei).second) {
174 double w = getEdgeWeight(*ei);
175 if (w == MissingValue) {
176 Edges.push_back(*ei);
177 } else {
178 incoming -= w;
179 }
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000180 }
181 }
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000182 // If no exit edges, create one:
183 if (Edges.size() == 0) {
184 BasicBlock *Latch = BBLoop->getLoopLatch();
185 if (Latch) {
186 Edge edge = getEdge(Latch,0);
187 EdgeInformation[BB->getParent()][edge] = BBWeight;
188 printEdgeWeight(edge);
189 edge = getEdge(Latch, BB);
190 EdgeInformation[BB->getParent()][edge] = BBWeight * ExecCount;
191 printEdgeWeight(edge);
192 }
193 }
Andreas Neustifterff271e12009-08-26 15:13:44 +0000194 // Distribute remaining weight onto the exit edges.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000195 for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
196 ei != ee; ++ei) {
197 EdgeInformation[BB->getParent()][*ei] += incoming/Edges.size();
Andreas Neustifterff271e12009-08-26 15:13:44 +0000198 printEdgeWeight(*ei);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000199 }
Andreas Neustifterff271e12009-08-26 15:13:44 +0000200 // Increase flow into the loop.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000201 BBWeight *= (ExecCount+1);
202 }
203
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000204 BlockInformation[BB->getParent()][BB] = BBWeight;
205 // Up until now we considered only the loop exiting edges, now we have a
206 // definite block weight and must ditribute this onto the outgoing edges.
207 // Since there may be already flow attached to some of the edges, read this
208 // flow first and remember the edges that have still now flow attached.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000209 Edges.clear();
210 std::set<BasicBlock*> ProcessedSuccs;
Andreas Neustifterff271e12009-08-26 15:13:44 +0000211
Andreas Neustiftere885af92009-09-01 10:06:05 +0000212 succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000213 // Also check for (BB,0) edges that may already contain some flow. (But only
214 // in case there are no successors.)
Andreas Neustiftere885af92009-09-01 10:06:05 +0000215 if (bbi == bbe) {
216 Edge edge = getEdge(BB,0);
217 EdgeInformation[BB->getParent()][edge] = BBWeight;
218 printEdgeWeight(edge);
219 }
Andreas Neustifter19531d12009-09-01 19:01:59 +0000220 for ( ; bbi != bbe; ++bbi ) {
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000221 if (ProcessedSuccs.insert(*bbi).second) {
222 Edge edge = getEdge(BB,*bbi);
223 double w = getEdgeWeight(edge);
224 if (w != MissingValue) {
225 BBWeight -= getEdgeWeight(edge);
226 } else {
227 Edges.push_back(edge);
228 }
229 }
230 }
231
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000232 // Finally we know what flow is still not leaving the block, distribute this
233 // flow onto the empty edges.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000234 for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
235 ei != ee; ++ei) {
236 EdgeInformation[BB->getParent()][*ei] += BBWeight/Edges.size();
Andreas Neustifterff271e12009-08-26 15:13:44 +0000237 printEdgeWeight(*ei);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000238 }
239
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000240 // This block is visited, mark this before the recursion.
241 BBToVisit.erase(BB);
242
243 // Recurse into successors.
244 for (succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
245 bbi != bbe; ++bbi) {
246 recurseBasicBlock(*bbi);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000247 }
248}
249
250bool ProfileEstimatorPass::runOnFunction(Function &F) {
251 if (F.isDeclaration()) return false;
252
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000253 // Fetch LoopInfo and clear ProfileInfo for this function.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000254 LI = &getAnalysis<LoopInfo>();
255 FunctionInformation.erase(&F);
256 BlockInformation[&F].clear();
257 EdgeInformation[&F].clear();
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000258
259 // Mark all blocks as to visit.
260 for (Function::iterator bi = F.begin(), be = F.end(); bi != be; ++bi)
261 BBToVisit.insert(bi);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000262
Andreas Neustifterff271e12009-08-26 15:13:44 +0000263 DEBUG(errs() << "Working on function " << F.getNameStr() << "\n");
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000264
Andreas Neustifterff271e12009-08-26 15:13:44 +0000265 // Since the entry block is the first one and has no predecessors, the edge
266 // (0,entry) is inserted with the starting weight of 1.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000267 BasicBlock *entry = &F.getEntryBlock();
268 BlockInformation[&F][entry] = 1;
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000269 Edge edge = getEdge(0,entry);
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000270 EdgeInformation[&F][edge] = 1;
271 printEdgeWeight(edge);
272
273 // Since recurseBasicBlock() maybe returns with a block which was not fully
274 // estimated, use recurseBasicBlock() until everything is calculated.
275 recurseBasicBlock(entry);
276 while (BBToVisit.size() > 0) {
277 // Remember number of open blocks, this is later used to check if progress
278 // was made.
279 unsigned size = BBToVisit.size();
280
281 // Try to calculate all blocks in turn.
282 for (std::set<BasicBlock*>::iterator bi = BBToVisit.begin(),
283 be = BBToVisit.end(); bi != be; ++bi) {
284 recurseBasicBlock(*bi);
285 // If at least one block was finished, break because iterator may be
286 // invalid.
287 if (BBToVisit.size() < size) break;
288 }
289
290 // If there was not a single block resovled, make some assumptions.
291 if (BBToVisit.size() == size) {
292 BasicBlock *BB = *(BBToVisit.begin());
293 // Since this BB was not calculated because of missing incoming edges,
294 // set these edges to zero.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000295 for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
296 bbi != bbe; ++bbi) {
297 Edge e = getEdge(*bbi,BB);
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000298 double w = getEdgeWeight(e);
299 if (w == MissingValue) {
300 EdgeInformation[&F][e] = 0;
Andreas Neustifter8a58c182009-09-11 08:39:33 +0000301 DEBUG(errs() << "Assuming edge weight: ");
Andreas Neustifter0c0de662009-09-10 16:30:38 +0000302 printEdgeWeight(e);
303 }
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000304 }
305 }
306 }
307
308 return false;
309}