blob: 098079bcffc4ee2b702060b75a671e376db5aa91 [file] [log] [blame]
Chris Lattner927fec32004-02-11 06:10:05 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner927fec32004-02-11 06:10:05 +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 Lattner927fec32004-02-11 06:10:05 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
Andreas Neustifterda5ea942009-09-01 19:08:51 +000014#define DEBUG_TYPE "profile-loader"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000015#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Daniel Dunbaree166382009-08-05 15:55:56 +000017#include "llvm/Module.h"
Chris Lattner927fec32004-02-11 06:10:05 +000018#include "llvm/Pass.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000019#include "llvm/Analysis/Passes.h"
Chris Lattner927fec32004-02-11 06:10:05 +000020#include "llvm/Analysis/ProfileInfo.h"
21#include "llvm/Analysis/ProfileInfoLoader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Andreas Neustifterda5ea942009-09-01 19:08:51 +000023#include "llvm/Support/CFG.h"
24#include "llvm/Support/Debug.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000025#include "llvm/Support/raw_ostream.h"
Andreas Neustifterda5ea942009-09-01 19:08:51 +000026#include "llvm/Support/Format.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/SmallSet.h"
29#include <set>
Chris Lattner927fec32004-02-11 06:10:05 +000030using namespace llvm;
31
Andreas Neustifterda5ea942009-09-01 19:08:51 +000032STATISTIC(NumEdgesRead, "The # of edges read.");
33
Dan Gohman844731a2008-05-13 00:00:25 +000034static cl::opt<std::string>
35ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
36 cl::value_desc("filename"),
37 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000038
Dan Gohman844731a2008-05-13 00:00:25 +000039namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000040 class LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000041 std::string Filename;
Andreas Neustifterda5ea942009-09-01 19:08:51 +000042 std::set<Edge> SpanningTree;
43 std::set<const BasicBlock*> BBisUnvisited;
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +000044 unsigned ReadCount;
Chris Lattner927fec32004-02-11 06:10:05 +000045 public:
Devang Patel19974732007-05-03 01:11:54 +000046 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000047 explicit LoaderPass(const std::string &filename = "")
Owen Anderson90c579d2010-08-06 18:33:48 +000048 : ModulePass(ID), Filename(filename) {
Owen Anderson081c34b2010-10-19 17:21:58 +000049 initializeLoaderPassPass(*PassRegistry::getPassRegistry());
Chris Lattner945871d2004-02-11 18:21:05 +000050 if (filename.empty()) Filename = ProfileInfoFilename;
51 }
Chris Lattner927fec32004-02-11 06:10:05 +000052
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56
57 virtual const char *getPassName() const {
58 return "Profiling information loader";
59 }
60
Andreas Neustifterda5ea942009-09-01 19:08:51 +000061 // recurseBasicBlock() - Calculates the edge weights for as much basic
62 // blocks as possbile.
63 virtual void recurseBasicBlock(const BasicBlock *BB);
Edward O'Callaghan2afd0722009-11-02 02:55:39 +000064 virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, double &);
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +000065 virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000066
Chris Lattner1bc76d42010-01-20 20:09:02 +000067 /// getAdjustedAnalysisPointer - This method is used when a pass implements
68 /// an analysis interface through multiple inheritance. If needed, it
69 /// should override this to adjust the this pointer as needed for the
70 /// specified pass info.
Owen Anderson90c579d2010-08-06 18:33:48 +000071 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
72 if (PI == &ProfileInfo::ID)
Chris Lattner1bc76d42010-01-20 20:09:02 +000073 return (ProfileInfo*)this;
74 return this;
75 }
76
Chris Lattner927fec32004-02-11 06:10:05 +000077 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000078 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000079 };
Chris Lattner927fec32004-02-11 06:10:05 +000080} // End of anonymous namespace
81
Dan Gohman844731a2008-05-13 00:00:25 +000082char LoaderPass::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +000083INITIALIZE_AG_PASS(LoaderPass, ProfileInfo, "profile-loader",
Owen Andersonce665bd2010-10-07 22:25:06 +000084 "Load profile information from llvmprof.out", false, true, false)
Dan Gohman844731a2008-05-13 00:00:25 +000085
Owen Anderson90c579d2010-08-06 18:33:48 +000086char &llvm::ProfileLoaderPassID = LoaderPass::ID;
Andreas Neustifter44299c92009-12-03 11:00:37 +000087
Jeff Cohen6e400f72005-01-10 03:56:27 +000088ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000089
90/// createProfileLoaderPass - This function returns a Pass that loads the
91/// profiling information for the module from the specified filename, making it
92/// available to the optimizers.
93Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
94 return new LoaderPass(Filename);
95}
96
Andreas Neustifterda5ea942009-09-01 19:08:51 +000097void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
Edward O'Callaghan2afd0722009-11-02 02:55:39 +000098 unsigned &uncalc, double &count) {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000099 double w;
100 if ((w = getEdgeWeight(edge)) == MissingValue) {
101 tocalc = edge;
102 uncalc++;
103 } else {
104 count+=w;
105 }
106}
107
108// recurseBasicBlock - Visits all neighbours of a block and then tries to
109// calculate the missing edge values.
110void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
111
112 // break recursion if already visited
113 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
114 BBisUnvisited.erase(BB);
115 if (!BB) return;
116
117 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
118 bbi != bbe; ++bbi) {
119 recurseBasicBlock(*bbi);
120 }
Gabor Greif44424642010-03-25 23:25:28 +0000121 for (const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000122 bbi != bbe; ++bbi) {
123 recurseBasicBlock(*bbi);
124 }
125
Andreas Neustifter44299c92009-12-03 11:00:37 +0000126 Edge tocalc;
127 if (CalculateMissingEdge(BB, tocalc)) {
128 SpanningTree.erase(tocalc);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000129 }
130}
131
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000132void LoaderPass::readEdge(ProfileInfo::Edge e,
133 std::vector<unsigned> &ECs) {
134 if (ReadCount < ECs.size()) {
135 double weight = ECs[ReadCount++];
Andreas Neustifter92332722009-09-16 11:35:50 +0000136 if (weight != ProfileInfoLoader::Uncounted) {
137 // Here the data realm changes from the unsigned of the file to the
138 // double of the ProfileInfo. This conversion is save because we know
139 // that everything thats representable in unsinged is also representable
140 // in double.
141 EdgeInformation[getFunction(e)][e] += (double)weight;
142
David Greeneb511efa2009-12-23 21:58:29 +0000143 DEBUG(dbgs() << "--Read Edge Counter for " << e
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000144 << " (# "<< (ReadCount-1) << "): "
145 << (unsigned)getEdgeWeight(e) << "\n");
146 } else {
147 // This happens only if reading optimal profiling information, not when
148 // reading regular profiling information.
149 SpanningTree.insert(e);
150 }
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000151 }
152}
153
Chris Lattnerb12914b2004-09-20 04:48:05 +0000154bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +0000155 ProfileInfoLoader PIL("profile-loader", Filename, M);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000156
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000157 EdgeInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000158 std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
159 if (Counters.size() > 0) {
160 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000161 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
162 if (F->isDeclaration()) continue;
David Greeneb511efa2009-12-23 21:58:29 +0000163 DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000164 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000165 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000166 TerminatorInst *TI = BB->getTerminator();
167 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000168 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000169 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000170 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000171 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000172 if (ReadCount != Counters.size()) {
David Greenefb64f112009-12-23 23:00:50 +0000173 errs() << "WARNING: profile information is inconsistent with "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000174 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000175 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000176 NumEdgesRead = ReadCount;
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000177 }
178
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000179 Counters = PIL.getRawOptimalEdgeCounts();
180 if (Counters.size() > 0) {
181 ReadCount = 0;
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000182 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
183 if (F->isDeclaration()) continue;
David Greeneb511efa2009-12-23 21:58:29 +0000184 DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000185 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000186 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
187 TerminatorInst *TI = BB->getTerminator();
188 if (TI->getNumSuccessors() == 0) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000189 readEdge(getEdge(BB,0), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000190 }
191 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000192 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000193 }
194 }
195 while (SpanningTree.size() > 0) {
Andreas Neustifter44299c92009-12-03 11:00:37 +0000196
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000197 unsigned size = SpanningTree.size();
Andreas Neustifter44299c92009-12-03 11:00:37 +0000198
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000199 BBisUnvisited.clear();
200 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
201 ee = SpanningTree.end(); ei != ee; ++ei) {
202 BBisUnvisited.insert(ei->first);
203 BBisUnvisited.insert(ei->second);
204 }
205 while (BBisUnvisited.size() > 0) {
206 recurseBasicBlock(*BBisUnvisited.begin());
207 }
Andreas Neustifter44299c92009-12-03 11:00:37 +0000208
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000209 if (SpanningTree.size() == size) {
David Greeneb511efa2009-12-23 21:58:29 +0000210 DEBUG(dbgs()<<"{");
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000211 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
212 ee = SpanningTree.end(); ei != ee; ++ei) {
David Greeneb511efa2009-12-23 21:58:29 +0000213 DEBUG(dbgs()<< *ei <<",");
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000214 }
215 assert(0 && "No edge calculated!");
216 }
Andreas Neustifter44299c92009-12-03 11:00:37 +0000217
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000218 }
219 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000220 if (ReadCount != Counters.size()) {
David Greenefb64f112009-12-23 23:00:50 +0000221 errs() << "WARNING: profile information is inconsistent with "
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000222 << "the current program!\n";
223 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000224 NumEdgesRead = ReadCount;
Daniel Dunbarc9008c52009-08-05 21:51:16 +0000225 }
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000226
227 BlockInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000228 Counters = PIL.getRawBlockCounts();
229 if (Counters.size() > 0) {
230 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000231 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
232 if (F->isDeclaration()) continue;
233 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000234 if (ReadCount < Counters.size())
Andreas Neustifter92332722009-09-16 11:35:50 +0000235 // Here the data realm changes from the unsigned of the file to the
236 // double of the ProfileInfo. This conversion is save because we know
237 // that everything thats representable in unsinged is also
238 // representable in double.
239 BlockInformation[F][BB] = (double)Counters[ReadCount++];
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000240 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000241 if (ReadCount != Counters.size()) {
David Greenefb64f112009-12-23 23:00:50 +0000242 errs() << "WARNING: profile information is inconsistent with "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000243 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000244 }
245 }
246
247 FunctionInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000248 Counters = PIL.getRawFunctionCounts();
249 if (Counters.size() > 0) {
250 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000251 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
252 if (F->isDeclaration()) continue;
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000253 if (ReadCount < Counters.size())
Andreas Neustifter92332722009-09-16 11:35:50 +0000254 // Here the data realm changes from the unsigned of the file to the
255 // double of the ProfileInfo. This conversion is save because we know
256 // that everything thats representable in unsinged is also
257 // representable in double.
258 FunctionInformation[F] = (double)Counters[ReadCount++];
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000259 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000260 if (ReadCount != Counters.size()) {
David Greenefb64f112009-12-23 23:00:50 +0000261 errs() << "WARNING: profile information is inconsistent with "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000262 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000263 }
Chris Lattner945871d2004-02-11 18:21:05 +0000264 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000265
Chris Lattner927fec32004-02-11 06:10:05 +0000266 return false;
267}