blob: cbd0430a6516b6b4f47ab62e38fecc38c1317238 [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"
Andreas Neustifter231d7342009-09-01 19:08:51 +000023#include "llvm/Support/CFG.h"
24#include "llvm/Support/Debug.h"
Chris Lattner15016622009-08-23 07:33:14 +000025#include "llvm/Support/raw_ostream.h"
Andreas Neustifter231d7342009-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>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
Andreas Neustifter231d7342009-09-01 19:08:51 +000032STATISTIC(NumEdgesRead, "The # of edges read.");
33
Dan Gohman089efff2008-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"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
Dan Gohman089efff2008-05-13 00:00:25 +000039namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000040 class LoaderPass : public ModulePass, public ProfileInfo {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 std::string Filename;
Andreas Neustifter231d7342009-09-01 19:08:51 +000042 std::set<Edge> SpanningTree;
43 std::set<const BasicBlock*> BBisUnvisited;
Andreas Neustifter61a788c2009-09-10 07:12:35 +000044 unsigned ReadCount;
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);
Edward O'Callaghan9e974a72009-11-02 02:55:39 +000063 virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, double &);
Andreas Neustifter61a788c2009-09-10 07:12:35 +000064 virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
Andreas Neustifter231d7342009-09-01 19:08:51 +000065
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 /// run - Load the profile information from the specified file.
67 virtual bool runOnModule(Module &M);
68 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069} // End of anonymous namespace
70
Dan Gohman089efff2008-05-13 00:00:25 +000071char LoaderPass::ID = 0;
72static RegisterPass<LoaderPass>
73X("profile-loader", "Load profile information from llvmprof.out", false, true);
74
75static RegisterAnalysisGroup<ProfileInfo> Y(X);
76
Andreas Neustifter86f33f82009-12-03 11:00:37 +000077const PassInfo *llvm::ProfileLoaderPassID = &X;
78
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
80
81/// createProfileLoaderPass - This function returns a Pass that loads the
82/// profiling information for the module from the specified filename, making it
83/// available to the optimizers.
84Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
85 return new LoaderPass(Filename);
86}
87
Andreas Neustifter231d7342009-09-01 19:08:51 +000088void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
Edward O'Callaghan9e974a72009-11-02 02:55:39 +000089 unsigned &uncalc, double &count) {
Andreas Neustifter231d7342009-09-01 19:08:51 +000090 double w;
91 if ((w = getEdgeWeight(edge)) == MissingValue) {
92 tocalc = edge;
93 uncalc++;
94 } else {
95 count+=w;
96 }
97}
98
99// recurseBasicBlock - Visits all neighbours of a block and then tries to
100// calculate the missing edge values.
101void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
102
103 // break recursion if already visited
104 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
105 BBisUnvisited.erase(BB);
106 if (!BB) return;
107
108 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
109 bbi != bbe; ++bbi) {
110 recurseBasicBlock(*bbi);
111 }
112 for (pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
113 bbi != bbe; ++bbi) {
114 recurseBasicBlock(*bbi);
115 }
116
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000117 Edge tocalc;
118 if (CalculateMissingEdge(BB, tocalc)) {
119 SpanningTree.erase(tocalc);
Andreas Neustifter231d7342009-09-01 19:08:51 +0000120 }
121}
122
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000123void LoaderPass::readEdge(ProfileInfo::Edge e,
124 std::vector<unsigned> &ECs) {
125 if (ReadCount < ECs.size()) {
126 double weight = ECs[ReadCount++];
Andreas Neustifterec139a02009-09-16 11:35:50 +0000127 if (weight != ProfileInfoLoader::Uncounted) {
128 // Here the data realm changes from the unsigned of the file to the
129 // double of the ProfileInfo. This conversion is save because we know
130 // that everything thats representable in unsinged is also representable
131 // in double.
132 EdgeInformation[getFunction(e)][e] += (double)weight;
133
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000134 DEBUG(errs() << "--Read Edge Counter for " << e
135 << " (# "<< (ReadCount-1) << "): "
136 << (unsigned)getEdgeWeight(e) << "\n");
137 } else {
138 // This happens only if reading optimal profiling information, not when
139 // reading regular profiling information.
140 SpanningTree.insert(e);
141 }
Andreas Neustifter231d7342009-09-01 19:08:51 +0000142 }
143}
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145bool LoaderPass::runOnModule(Module &M) {
146 ProfileInfoLoader PIL("profile-loader", Filename, M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000148 EdgeInformation.clear();
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000149 std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
150 if (Counters.size() > 0) {
151 ReadCount = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000152 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
153 if (F->isDeclaration()) continue;
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000154 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
155 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000156 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000157 TerminatorInst *TI = BB->getTerminator();
158 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000159 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000160 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000163 if (ReadCount != Counters.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000164 errs() << "WARNING: profile information is inconsistent with "
165 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000166 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000167 NumEdgesRead = ReadCount;
Andreas Neustifter231d7342009-09-01 19:08:51 +0000168 }
169
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000170 Counters = PIL.getRawOptimalEdgeCounts();
171 if (Counters.size() > 0) {
172 ReadCount = 0;
Andreas Neustifter231d7342009-09-01 19:08:51 +0000173 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
174 if (F->isDeclaration()) continue;
175 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000176 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Andreas Neustifter231d7342009-09-01 19:08:51 +0000177 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
178 TerminatorInst *TI = BB->getTerminator();
179 if (TI->getNumSuccessors() == 0) {
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000180 readEdge(getEdge(BB,0), Counters);
Andreas Neustifter231d7342009-09-01 19:08:51 +0000181 }
182 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000183 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Andreas Neustifter231d7342009-09-01 19:08:51 +0000184 }
185 }
186 while (SpanningTree.size() > 0) {
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000187
Andreas Neustifter231d7342009-09-01 19:08:51 +0000188 unsigned size = SpanningTree.size();
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000189
Andreas Neustifter231d7342009-09-01 19:08:51 +0000190 BBisUnvisited.clear();
191 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
192 ee = SpanningTree.end(); ei != ee; ++ei) {
193 BBisUnvisited.insert(ei->first);
194 BBisUnvisited.insert(ei->second);
195 }
196 while (BBisUnvisited.size() > 0) {
197 recurseBasicBlock(*BBisUnvisited.begin());
198 }
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000199
Andreas Neustifter231d7342009-09-01 19:08:51 +0000200 if (SpanningTree.size() == size) {
201 DEBUG(errs()<<"{");
202 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
203 ee = SpanningTree.end(); ei != ee; ++ei) {
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000204 DEBUG(errs()<< *ei <<",");
Andreas Neustifter231d7342009-09-01 19:08:51 +0000205 }
206 assert(0 && "No edge calculated!");
207 }
Andreas Neustifter86f33f82009-12-03 11:00:37 +0000208
Andreas Neustifter231d7342009-09-01 19:08:51 +0000209 }
210 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000211 if (ReadCount != Counters.size()) {
Andreas Neustifter231d7342009-09-01 19:08:51 +0000212 errs() << "WARNING: profile information is inconsistent with "
213 << "the current program!\n";
214 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000215 NumEdgesRead = ReadCount;
Daniel Dunbar23505092009-08-05 21:51:16 +0000216 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000217
218 BlockInformation.clear();
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000219 Counters = PIL.getRawBlockCounts();
220 if (Counters.size() > 0) {
221 ReadCount = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000222 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
223 if (F->isDeclaration()) continue;
224 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000225 if (ReadCount < Counters.size())
Andreas Neustifterec139a02009-09-16 11:35:50 +0000226 // Here the data realm changes from the unsigned of the file to the
227 // double of the ProfileInfo. This conversion is save because we know
228 // that everything thats representable in unsinged is also
229 // representable in double.
230 BlockInformation[F][BB] = (double)Counters[ReadCount++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000231 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000232 if (ReadCount != Counters.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000233 errs() << "WARNING: profile information is inconsistent with "
234 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000235 }
236 }
237
238 FunctionInformation.clear();
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000239 Counters = PIL.getRawFunctionCounts();
240 if (Counters.size() > 0) {
241 ReadCount = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000242 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
243 if (F->isDeclaration()) continue;
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000244 if (ReadCount < Counters.size())
Andreas Neustifterec139a02009-09-16 11:35:50 +0000245 // Here the data realm changes from the unsigned of the file to the
246 // double of the ProfileInfo. This conversion is save because we know
247 // that everything thats representable in unsinged is also
248 // representable in double.
249 FunctionInformation[F] = (double)Counters[ReadCount++];
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000250 }
Andreas Neustifter61a788c2009-09-10 07:12:35 +0000251 if (ReadCount != Counters.size()) {
Chris Lattner15016622009-08-23 07:33:14 +0000252 errs() << "WARNING: profile information is inconsistent with "
253 << "the current program!\n";
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000254 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 }
256
257 return false;
258}