blob: 732829887e91391587fcccd0c2e95861f3ab3f07 [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>
Chris Lattnerdbbbfef2004-03-08 20:03:52 +000021#include <map>
Chris Lattnerbc44aa62004-02-11 05:54:25 +000022using namespace llvm;
23
Chris Lattnerbc44aa62004-02-11 05:54:25 +000024// ByteSwap - Byteswap 'Var' if 'Really' is true.
25//
26static inline unsigned ByteSwap(unsigned Var, bool Really) {
27 if (!Really) return Var;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000028 return ((Var & (255<< 0)) << 24) |
29 ((Var & (255<< 8)) << 8) |
30 ((Var & (255<<16)) >> 8) |
Chris Lattnerbc44aa62004-02-11 05:54:25 +000031 ((Var & (255<<24)) >> 24);
32}
33
34static void ReadProfilingBlock(const char *ToolName, FILE *F,
35 bool ShouldByteSwap,
36 std::vector<unsigned> &Data) {
37 // Read the number of entries...
38 unsigned NumEntries;
39 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000040 cerr << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000041 perror(0);
42 exit(1);
43 }
44 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
45
46 // Read the counts...
47 std::vector<unsigned> TempSpace(NumEntries);
48
49 // Read in the block of data...
50 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000051 cerr << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000052 perror(0);
53 exit(1);
54 }
55
56 // Make sure we have enough space...
57 if (Data.size() < NumEntries)
58 Data.resize(NumEntries);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000059
Chris Lattnerbc44aa62004-02-11 05:54:25 +000060 // Accumulate the data we just read into the data.
61 if (!ShouldByteSwap) {
62 for (unsigned i = 0; i != NumEntries; ++i)
63 Data[i] += TempSpace[i];
64 } else {
65 for (unsigned i = 0; i != NumEntries; ++i)
66 Data[i] += ByteSwap(TempSpace[i], true);
67 }
68}
69
70// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
71// program if the file is invalid or broken.
72//
73ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
74 const std::string &Filename,
75 Module &TheModule) : M(TheModule) {
76 FILE *F = fopen(Filename.c_str(), "r");
77 if (F == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000078 cerr << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000079 perror(0);
80 exit(1);
81 }
82
83 // Keep reading packets until we run out of them.
84 unsigned PacketType;
85 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
86 // If the low eight bits of the packet are zero, we must be dealing with an
87 // endianness mismatch. Byteswap all words read from the profiling
88 // information.
89 bool ShouldByteSwap = (char)PacketType == 0;
90 PacketType = ByteSwap(PacketType, ShouldByteSwap);
91
92 switch (PacketType) {
93 case ArgumentInfo: {
94 unsigned ArgLength;
95 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +000096 cerr << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000097 perror(0);
98 exit(1);
99 }
100 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
101
102 // Read in the arguments...
103 std::vector<char> Chars(ArgLength+4);
104
105 if (ArgLength)
106 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000107 cerr << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000108 perror(0);
109 exit(1);
110 }
111 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
112 break;
113 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000114
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000115 case FunctionInfo:
116 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
117 break;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000118
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000119 case BlockInfo:
120 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
121 break;
122
Chris Lattner01945c12004-03-08 18:20:18 +0000123 case EdgeInfo:
124 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
125 break;
126
Brian Gaekeb171d792004-05-04 17:11:14 +0000127 case BBTraceInfo:
128 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
129 break;
130
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000131 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000132 cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000133 exit(1);
134 }
135 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000136
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000137 fclose(F);
138}
139
140
141// getFunctionCounts - This method is used by consumers of function counting
142// information. If we do not directly have function count information, we
143// compute it from other, more refined, types of profile information.
144//
145void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
146 unsigned> > &Counts) {
147 if (FunctionCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000148 if (hasAccurateBlockCounts()) {
149 // Synthesize function frequency information from the number of times
150 // their entry blocks were executed.
151 std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
152 getBlockCounts(BlockCounts);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000153
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000154 for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
Dan Gohmanecb7a772007-03-22 16:38:57 +0000155 if (&BlockCounts[i].first->getParent()->getEntryBlock() ==
156 BlockCounts[i].first)
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000157 Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
158 BlockCounts[i].second));
159 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000160 cerr << "Function counts are not available!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000161 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000162 return;
163 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000164
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000165 unsigned Counter = 0;
166 for (Module::iterator I = M.begin(), E = M.end();
167 I != E && Counter != FunctionCounts.size(); ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000168 if (!I->isDeclaration())
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000169 Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
170}
171
172// getBlockCounts - This method is used by consumers of block counting
173// information. If we do not directly have block count information, we
174// compute it from other, more refined, types of profile information.
175//
176void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
177 unsigned> > &Counts) {
178 if (BlockCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000179 if (hasAccurateEdgeCounts()) {
180 // Synthesize block count information from edge frequency information.
181 // The block execution frequency is equal to the sum of the execution
182 // frequency of all outgoing edges from a block.
183 //
184 // If a block has no successors, this will not be correct, so we have to
185 // special case it. :(
186 std::vector<std::pair<Edge, unsigned> > EdgeCounts;
187 getEdgeCounts(EdgeCounts);
188
189 std::map<BasicBlock*, unsigned> InEdgeFreqs;
190
191 BasicBlock *LastBlock = 0;
192 TerminatorInst *TI = 0;
193 for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
194 if (EdgeCounts[i].first.first != LastBlock) {
195 LastBlock = EdgeCounts[i].first.first;
196 TI = LastBlock->getTerminator();
197 Counts.push_back(std::make_pair(LastBlock, 0));
198 }
199 Counts.back().second += EdgeCounts[i].second;
200 unsigned SuccNum = EdgeCounts[i].first.second;
201 if (SuccNum >= TI->getNumSuccessors()) {
202 static bool Warned = false;
203 if (!Warned) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000204 cerr << "WARNING: profile info doesn't seem to match"
205 << " the program!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000206 Warned = true;
207 }
208 } else {
209 // If this successor has no successors of its own, we will never
210 // compute an execution count for that block. Remember the incoming
211 // edge frequencies to add later.
212 BasicBlock *Succ = TI->getSuccessor(SuccNum);
213 if (Succ->getTerminator()->getNumSuccessors() == 0)
214 InEdgeFreqs[Succ] += EdgeCounts[i].second;
215 }
216 }
217
218 // Now we have to accumulate information for those blocks without
219 // successors into our table.
220 for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
221 E = InEdgeFreqs.end(); I != E; ++I) {
222 unsigned i = 0;
223 for (; i != Counts.size() && Counts[i].first != I->first; ++i)
224 /*empty*/;
225 if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
226 Counts[i].second += I->second;
227 }
228
229 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000230 cerr << "Block counts are not available!\n";
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000231 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000232 return;
233 }
234
235 unsigned Counter = 0;
236 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
237 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
238 Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
239 if (Counter == BlockCounts.size())
240 return;
241 }
242}
Chris Lattner01945c12004-03-08 18:20:18 +0000243
244// getEdgeCounts - This method is used by consumers of edge counting
245// information. If we do not directly have edge count information, we compute
246// it from other, more refined, types of profile information.
247//
248void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge,
249 unsigned> > &Counts) {
250 if (EdgeCounts.empty()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000251 cerr << "Edge counts not available, and no synthesis "
252 << "is implemented yet!\n";
Chris Lattner01945c12004-03-08 18:20:18 +0000253 return;
254 }
255
256 unsigned Counter = 0;
257 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
258 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
259 for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors();
260 i != e; ++i) {
261 Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++]));
262 if (Counter == EdgeCounts.size())
263 return;
264 }
265}
Brian Gaekeb171d792004-05-04 17:11:14 +0000266
267// getBBTrace - This method is used by consumers of basic-block trace
268// information.
269//
270void ProfileInfoLoader::getBBTrace(std::vector<BasicBlock *> &Trace) {
271 if (BBTrace.empty ()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000272 cerr << "Basic block trace is not available!\n";
Brian Gaekeb171d792004-05-04 17:11:14 +0000273 return;
274 }
Bill Wendlinge8156192006-12-07 01:30:32 +0000275 cerr << "Basic block trace loading is not implemented yet!\n";
Brian Gaekeb171d792004-05-04 17:11:14 +0000276}