blob: 3a0a740f0035dac54044516398947360793bf367 [file] [log] [blame]
Chris Lattnerbc44aa62004-02-11 05:54:25 +00001//===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattnerbc44aa62004-02-11 05:54:25 +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 Lattnerbc44aa62004-02-11 05:54:25 +00008//===----------------------------------------------------------------------===//
9//
10// The ProfileInfoLoader class is used to load and represent profiling
11// information read in from the dump file.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileInfoLoader.h"
Brian Gaeke660ef702004-05-04 16:53:07 +000016#include "llvm/Analysis/ProfileInfoTypes.h"
Chris Lattnerbc44aa62004-02-11 05:54:25 +000017#include "llvm/Module.h"
Chris Lattner01945c12004-03-08 18:20:18 +000018#include "llvm/InstrTypes.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000019#include "llvm/Support/Streams.h"
Chris Lattnerbc44aa62004-02-11 05:54:25 +000020#include <cstdio>
Dan Gohmand68a0762009-01-05 17:59:02 +000021#include <cstdlib>
Chris Lattnerdbbbfef2004-03-08 20:03:52 +000022#include <map>
Chris Lattnerbc44aa62004-02-11 05:54:25 +000023using namespace llvm;
24
Chris Lattnerbc44aa62004-02-11 05:54:25 +000025// ByteSwap - Byteswap 'Var' if 'Really' is true.
26//
27static inline unsigned ByteSwap(unsigned Var, bool Really) {
28 if (!Really) return Var;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000029 return ((Var & (255<< 0)) << 24) |
30 ((Var & (255<< 8)) << 8) |
31 ((Var & (255<<16)) >> 8) |
Chris Lattnerbc44aa62004-02-11 05:54:25 +000032 ((Var & (255<<24)) >> 24);
33}
34
35static void ReadProfilingBlock(const char *ToolName, FILE *F,
36 bool ShouldByteSwap,
37 std::vector<unsigned> &Data) {
38 // Read the number of entries...
39 unsigned NumEntries;
40 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000041 cerr << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000042 perror(0);
43 exit(1);
44 }
45 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
46
47 // Read the counts...
48 std::vector<unsigned> TempSpace(NumEntries);
49
50 // Read in the block of data...
51 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000052 cerr << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000053 perror(0);
54 exit(1);
55 }
56
57 // Make sure we have enough space...
58 if (Data.size() < NumEntries)
59 Data.resize(NumEntries);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000060
Chris Lattnerbc44aa62004-02-11 05:54:25 +000061 // Accumulate the data we just read into the data.
62 if (!ShouldByteSwap) {
63 for (unsigned i = 0; i != NumEntries; ++i)
64 Data[i] += TempSpace[i];
65 } else {
66 for (unsigned i = 0; i != NumEntries; ++i)
67 Data[i] += ByteSwap(TempSpace[i], true);
68 }
69}
70
71// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
72// program if the file is invalid or broken.
73//
74ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
75 const std::string &Filename,
76 Module &TheModule) : M(TheModule) {
77 FILE *F = fopen(Filename.c_str(), "r");
78 if (F == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000079 cerr << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000080 perror(0);
81 exit(1);
82 }
83
84 // Keep reading packets until we run out of them.
85 unsigned PacketType;
86 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
87 // If the low eight bits of the packet are zero, we must be dealing with an
88 // endianness mismatch. Byteswap all words read from the profiling
89 // information.
90 bool ShouldByteSwap = (char)PacketType == 0;
91 PacketType = ByteSwap(PacketType, ShouldByteSwap);
92
93 switch (PacketType) {
94 case ArgumentInfo: {
95 unsigned ArgLength;
96 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000097 cerr << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000098 perror(0);
99 exit(1);
100 }
101 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
102
103 // Read in the arguments...
104 std::vector<char> Chars(ArgLength+4);
105
106 if (ArgLength)
107 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000108 cerr << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000109 perror(0);
110 exit(1);
111 }
112 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
113 break;
114 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000115
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000116 case FunctionInfo:
117 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
118 break;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000119
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000120 case BlockInfo:
121 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
122 break;
123
Chris Lattner01945c12004-03-08 18:20:18 +0000124 case EdgeInfo:
125 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
126 break;
127
Brian Gaekeb171d792004-05-04 17:11:14 +0000128 case BBTraceInfo:
129 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
130 break;
131
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000132 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000133 cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000134 exit(1);
135 }
136 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000137
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000138 fclose(F);
139}
140
141
142// getFunctionCounts - This method is used by consumers of function counting
143// information. If we do not directly have function count information, we
144// compute it from other, more refined, types of profile information.
145//
146void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
147 unsigned> > &Counts) {
148 if (FunctionCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000149 if (hasAccurateBlockCounts()) {
150 // Synthesize function frequency information from the number of times
151 // their entry blocks were executed.
152 std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
153 getBlockCounts(BlockCounts);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000154
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000155 for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
Dan Gohmanecb7a772007-03-22 16:38:57 +0000156 if (&BlockCounts[i].first->getParent()->getEntryBlock() ==
157 BlockCounts[i].first)
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000158 Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
159 BlockCounts[i].second));
160 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000161 cerr << "Function counts are not available!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000162 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000163 return;
164 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000165
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000166 unsigned Counter = 0;
167 for (Module::iterator I = M.begin(), E = M.end();
168 I != E && Counter != FunctionCounts.size(); ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000169 if (!I->isDeclaration())
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000170 Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
171}
172
173// getBlockCounts - This method is used by consumers of block counting
174// information. If we do not directly have block count information, we
175// compute it from other, more refined, types of profile information.
176//
177void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
178 unsigned> > &Counts) {
179 if (BlockCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000180 if (hasAccurateEdgeCounts()) {
181 // Synthesize block count information from edge frequency information.
182 // The block execution frequency is equal to the sum of the execution
183 // frequency of all outgoing edges from a block.
184 //
185 // If a block has no successors, this will not be correct, so we have to
186 // special case it. :(
187 std::vector<std::pair<Edge, unsigned> > EdgeCounts;
188 getEdgeCounts(EdgeCounts);
189
190 std::map<BasicBlock*, unsigned> InEdgeFreqs;
191
192 BasicBlock *LastBlock = 0;
193 TerminatorInst *TI = 0;
194 for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
195 if (EdgeCounts[i].first.first != LastBlock) {
196 LastBlock = EdgeCounts[i].first.first;
197 TI = LastBlock->getTerminator();
198 Counts.push_back(std::make_pair(LastBlock, 0));
199 }
200 Counts.back().second += EdgeCounts[i].second;
201 unsigned SuccNum = EdgeCounts[i].first.second;
202 if (SuccNum >= TI->getNumSuccessors()) {
203 static bool Warned = false;
204 if (!Warned) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000205 cerr << "WARNING: profile info doesn't seem to match"
206 << " the program!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000207 Warned = true;
208 }
209 } else {
210 // If this successor has no successors of its own, we will never
211 // compute an execution count for that block. Remember the incoming
212 // edge frequencies to add later.
213 BasicBlock *Succ = TI->getSuccessor(SuccNum);
214 if (Succ->getTerminator()->getNumSuccessors() == 0)
215 InEdgeFreqs[Succ] += EdgeCounts[i].second;
216 }
217 }
218
219 // Now we have to accumulate information for those blocks without
220 // successors into our table.
221 for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
222 E = InEdgeFreqs.end(); I != E; ++I) {
223 unsigned i = 0;
224 for (; i != Counts.size() && Counts[i].first != I->first; ++i)
225 /*empty*/;
226 if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
227 Counts[i].second += I->second;
228 }
229
230 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000231 cerr << "Block counts are not available!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000232 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000233 return;
234 }
235
236 unsigned Counter = 0;
237 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
238 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
239 Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
240 if (Counter == BlockCounts.size())
241 return;
242 }
243}
Chris Lattner01945c12004-03-08 18:20:18 +0000244
245// getEdgeCounts - This method is used by consumers of edge counting
246// information. If we do not directly have edge count information, we compute
247// it from other, more refined, types of profile information.
248//
249void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge,
250 unsigned> > &Counts) {
251 if (EdgeCounts.empty()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000252 cerr << "Edge counts not available, and no synthesis "
253 << "is implemented yet!\n";
Chris Lattner01945c12004-03-08 18:20:18 +0000254 return;
255 }
256
257 unsigned Counter = 0;
258 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
259 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
260 for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors();
261 i != e; ++i) {
262 Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++]));
263 if (Counter == EdgeCounts.size())
264 return;
265 }
266}
Brian Gaekeb171d792004-05-04 17:11:14 +0000267
268// getBBTrace - This method is used by consumers of basic-block trace
269// information.
270//
271void ProfileInfoLoader::getBBTrace(std::vector<BasicBlock *> &Trace) {
272 if (BBTrace.empty ()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000273 cerr << "Basic block trace is not available!\n";
Brian Gaekeb171d792004-05-04 17:11:14 +0000274 return;
275 }
Bill Wendlinge8156192006-12-07 01:30:32 +0000276 cerr << "Basic block trace loading is not implemented yet!\n";
Brian Gaekeb171d792004-05-04 17:11:14 +0000277}