blob: 4694d01af8b3eea5314ae4b642a5f2e151322459 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
Andreas Neustifter231d7342009-09-01 19:08:51 +000014#define DEBUG_TYPE "profile-loader"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Daniel Dunbar13021f12009-08-05 15:55:56 +000017#include "llvm/Module.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Pass.h"
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Analysis/ProfileInfo.h"
21#include "llvm/Analysis/ProfileInfoLoader.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Compiler.h"
Andreas Neustifter231d7342009-09-01 19:08:51 +000024#include "llvm/Support/CFG.h"
25#include "llvm/Support/Debug.h"
Chris Lattner15016622009-08-23 07:33:14 +000026#include "llvm/Support/raw_ostream.h"
Andreas Neustifter231d7342009-09-01 19:08:51 +000027#include "llvm/Support/Format.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/SmallSet.h"
30#include <set>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031using namespace llvm;
32
Andreas Neustifter231d7342009-09-01 19:08:51 +000033STATISTIC(NumEdgesRead, "The # of edges read.");
34
Dan Gohman089efff2008-05-13 00:00:25 +000035static cl::opt<std::string>
36ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
37 cl::value_desc("filename"),
38 cl::desc("Profile file loaded by -profile-loader"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
Dan Gohman089efff2008-05-13 00:00:25 +000040namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
42 std::string Filename;
Andreas Neustifter231d7342009-09-01 19:08:51 +000043 std::set<Edge> SpanningTree;
44 std::set<const BasicBlock*> BBisUnvisited;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 public:
46 static char ID; // Class identification, replacement for typeinfo
Dan Gohman34c280e2007-08-01 15:32:29 +000047 explicit LoaderPass(const std::string &filename = "")
Dan Gohman26f8c272008-09-04 17:05:41 +000048 : ModulePass(&ID), Filename(filename) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 if (filename.empty()) Filename = ProfileInfoFilename;
50 }
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
54 }
55
56 virtual const char *getPassName() const {
57 return "Profiling information loader";
58 }
59
Andreas Neustifter231d7342009-09-01 19:08:51 +000060 // recurseBasicBlock() - Calculates the edge weights for as much basic
61 // blocks as possbile.
62 virtual void recurseBasicBlock(const BasicBlock *BB);
63 virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, unsigned &);
64 virtual void readOrRememberEdge(ProfileInfo::Edge, unsigned,
65 unsigned, Function*);
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 /// run - Load the profile information from the specified file.
68 virtual bool runOnModule(Module &M);
69 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070} // End of anonymous namespace
71
Dan Gohman089efff2008-05-13 00:00:25 +000072char LoaderPass::ID = 0;
73static RegisterPass<LoaderPass>
74X("profile-loader", "Load profile information from llvmprof.out", false, true);
75
76static RegisterAnalysisGroup<ProfileInfo> Y(X);
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
79
80/// createProfileLoaderPass - This function returns a Pass that loads the
81/// profiling information for the module from the specified filename, making it
82/// available to the optimizers.
83Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
84 return new LoaderPass(Filename);
85}
86
Andreas Neustifter231d7342009-09-01 19:08:51 +000087void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
88 unsigned &uncalc, unsigned &count) {
89 double w;
90 if ((w = getEdgeWeight(edge)) == MissingValue) {
91 tocalc = edge;
92 uncalc++;
93 } else {
94 count+=w;
95 }
96}
97
98// recurseBasicBlock - Visits all neighbours of a block and then tries to
99// calculate the missing edge values.
100void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
101
102 // break recursion if already visited
103 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
104 BBisUnvisited.erase(BB);
105 if (!BB) return;
106
107 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
108 bbi != bbe; ++bbi) {
109 recurseBasicBlock(*bbi);
110 }
111 for (pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
112 bbi != bbe; ++bbi) {
113 recurseBasicBlock(*bbi);
114 }
115
116 Edge edgetocalc;
117 unsigned uncalculated = 0;
118
119 // collect weights of all incoming and outgoing edges, rememer edges that
120 // have no value
121 unsigned incount = 0;
122 SmallSet<const BasicBlock*,8> pred_visited;
123 pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
124 if (bbi==bbe) {
125 readEdgeOrRemember(getEdge(0, BB),edgetocalc,uncalculated,incount);
126 }
127 for (;bbi != bbe; ++bbi) {
128 if (pred_visited.insert(*bbi)) {
129 readEdgeOrRemember(getEdge(*bbi, BB),edgetocalc,uncalculated,incount);
130 }
131 }
132
133 unsigned outcount = 0;
134 SmallSet<const BasicBlock*,8> succ_visited;
135 succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
136 if (sbbi==sbbe) {
137 readEdgeOrRemember(getEdge(BB, 0),edgetocalc,uncalculated,outcount);
138 }
139 for (;sbbi != sbbe; ++sbbi) {
140 if (succ_visited.insert(*sbbi)) {
141 readEdgeOrRemember(getEdge(BB, *sbbi),edgetocalc,uncalculated,outcount);
142 }
143 }
144
145 // if exactly one edge weight was missing, calculate it and remove it from
146 // spanning tree
147 if (uncalculated == 1) {
148 if (incount < outcount) {
149 EdgeInformation[BB->getParent()][edgetocalc] = outcount-incount;
150 } else {
151 EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
152 }
153 DEBUG(errs() << "--Calc Edge Counter for " << edgetocalc << ": "
154 << format("%g", getEdgeWeight(edgetocalc)) << "\n");
155 SpanningTree.erase(edgetocalc);
156 }
157}
158
159void LoaderPass::readOrRememberEdge(ProfileInfo::Edge e,
160 unsigned weight, unsigned ei,
161 Function *F) {
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000162 if (weight != ProfileInfoLoader::Uncounted) {
163 // Here the data realm changes from the unsigned of the file to the double
164 // of the ProfileInfo. This conversion is save because we know that
165 // everything thats representable in unsinged is also representable in
166 // double.
167 EdgeInformation[F][e] += (double)weight;
Andreas Neustifter231d7342009-09-01 19:08:51 +0000168 DEBUG(errs()<<"--Read Edge Counter for " << e
169 <<" (# "<<ei<<"): "<<(unsigned)getEdgeWeight(e)<<"\n");
170 } else {
171 SpanningTree.insert(e);
172 }
173}
174
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175bool LoaderPass::runOnModule(Module &M) {
176 ProfileInfoLoader PIL("profile-loader", Filename, M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000178 EdgeInformation.clear();
Daniel Dunbar13021f12009-08-05 15:55:56 +0000179 std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000180 if (ECs.size() > 0) {
181 unsigned ei = 0;
182 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
183 if (F->isDeclaration()) continue;
184 if (ei < ECs.size())
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000185 // Here the data realm changes from the unsigned of the file to the
186 // double of the ProfileInfo. This conversion is save because we know
187 // that everything thats representable in unsinged is also
188 // representable in double.
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000189 EdgeInformation[F][ProfileInfo::getEdge(0, &F->getEntryBlock())] +=
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000190 (double)ECs[ei++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000191 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
192 // Okay, we have to add a counter of each outgoing edge. If the
193 // outgoing edge is not critical don't split it, just insert the counter
194 // in the source or destination of the edge.
195 TerminatorInst *TI = BB->getTerminator();
196 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
197 if (ei < ECs.size())
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000198 // Here the data realm changes from the unsigned of the file to
199 // the double of the ProfileInfo.
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000200 EdgeInformation[F][ProfileInfo::getEdge(BB, TI->getSuccessor(s))] +=
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000201 (double)ECs[ei++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000205 if (ei != ECs.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000206 errs() << "WARNING: profile information is inconsistent with "
207 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000208 }
Andreas Neustifter231d7342009-09-01 19:08:51 +0000209 NumEdgesRead = ei;
210 }
211
212 ECs = PIL.getRawOptimalEdgeCounts();
213 if (ECs.size() > 0) {
214 unsigned ei = 0;
215 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
216 if (F->isDeclaration()) continue;
217 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
218 if (ei < ECs.size()) {
219 readOrRememberEdge(getEdge(0,&F->getEntryBlock()), ECs[ei], ei, F);
220 ei++;
221 }
222 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
223 TerminatorInst *TI = BB->getTerminator();
224 if (TI->getNumSuccessors() == 0) {
225 if (ei < ECs.size()) {
226 readOrRememberEdge(getEdge(BB,0), ECs[ei], ei, F); ei++;
227 }
228 }
229 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
230 if (ei < ECs.size()) {
231 readOrRememberEdge(getEdge(BB,TI->getSuccessor(s)), ECs[ei], ei, F);
232 ei++;
233 }
234 }
235 }
236 while (SpanningTree.size() > 0) {
237#if 0
238 unsigned size = SpanningTree.size();
239#endif
240 BBisUnvisited.clear();
241 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
242 ee = SpanningTree.end(); ei != ee; ++ei) {
243 BBisUnvisited.insert(ei->first);
244 BBisUnvisited.insert(ei->second);
245 }
246 while (BBisUnvisited.size() > 0) {
247 recurseBasicBlock(*BBisUnvisited.begin());
248 }
249#if 0
250 if (SpanningTree.size() == size) {
251 DEBUG(errs()<<"{");
252 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
253 ee = SpanningTree.end(); ei != ee; ++ei) {
254 DEBUG(errs()<<"("<<(ei->first?ei->first->getName():"0")<<","
255 <<(ei->second?ei->second->getName():"0")<<"),");
256 }
257 assert(0 && "No edge calculated!");
258 }
259#endif
260 }
261 }
262 if (ei != ECs.size()) {
263 errs() << "WARNING: profile information is inconsistent with "
264 << "the current program!\n";
265 }
266 NumEdgesRead = ei;
Daniel Dunbar23505092009-08-05 21:51:16 +0000267 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000268
269 BlockInformation.clear();
270 std::vector<unsigned> BCs = PIL.getRawBlockCounts();
271 if (BCs.size() > 0) {
272 unsigned bi = 0;
273 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
274 if (F->isDeclaration()) continue;
275 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
276 if (bi < BCs.size())
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000277 // Here the data realm changes from the unsigned of the file to the
278 // double of the ProfileInfo. This conversion is save because we know
279 // that everything thats representable in unsinged is also
280 // representable in double.
281 BlockInformation[F][BB] = (double)BCs[bi++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000282 }
283 if (bi != BCs.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000284 errs() << "WARNING: profile information is inconsistent with "
285 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000286 }
287 }
288
289 FunctionInformation.clear();
290 std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
291 if (FCs.size() > 0) {
292 unsigned fi = 0;
293 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
294 if (F->isDeclaration()) continue;
295 if (fi < FCs.size())
Andreas Neustifter20bfe162009-09-09 12:48:26 +0000296 // Here the data realm changes from the unsigned of the file to the
297 // double of the ProfileInfo. This conversion is save because we know
298 // that everything thats representable in unsinged is also
299 // representable in double.
300 FunctionInformation[F] = (double)FCs[fi++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000301 }
302 if (fi != FCs.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000303 errs() << "WARNING: profile information is inconsistent with "
304 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000305 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 }
307
308 return false;
309}