blob: 46a888365d4117195ba1f22890d5d116b26f4b2d [file] [log] [blame]
Chris Lattnerbc44aa62004-02-11 05:54:25 +00001//===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattnerbc44aa62004-02-11 05:54:25 +000019#include <cstdio>
Chris Lattnerdbbbfef2004-03-08 20:03:52 +000020#include <map>
Chris Lattnerbc44aa62004-02-11 05:54:25 +000021using namespace llvm;
22
Chris Lattnerbc44aa62004-02-11 05:54:25 +000023// ByteSwap - Byteswap 'Var' if 'Really' is true.
24//
25static inline unsigned ByteSwap(unsigned Var, bool Really) {
26 if (!Really) return Var;
27 return ((Var & (255<< 0)) << 24) |
28 ((Var & (255<< 8)) << 8) |
29 ((Var & (255<<16)) >> 8) |
30 ((Var & (255<<24)) >> 24);
31}
32
33static void ReadProfilingBlock(const char *ToolName, FILE *F,
34 bool ShouldByteSwap,
35 std::vector<unsigned> &Data) {
36 // Read the number of entries...
37 unsigned NumEntries;
38 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
39 std::cerr << ToolName << ": data packet truncated!\n";
40 perror(0);
41 exit(1);
42 }
43 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
44
45 // Read the counts...
46 std::vector<unsigned> TempSpace(NumEntries);
47
48 // Read in the block of data...
49 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
50 std::cerr << ToolName << ": data packet truncated!\n";
51 perror(0);
52 exit(1);
53 }
54
55 // Make sure we have enough space...
56 if (Data.size() < NumEntries)
57 Data.resize(NumEntries);
58
59 // Accumulate the data we just read into the data.
60 if (!ShouldByteSwap) {
61 for (unsigned i = 0; i != NumEntries; ++i)
62 Data[i] += TempSpace[i];
63 } else {
64 for (unsigned i = 0; i != NumEntries; ++i)
65 Data[i] += ByteSwap(TempSpace[i], true);
66 }
67}
68
69// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
70// program if the file is invalid or broken.
71//
72ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
73 const std::string &Filename,
74 Module &TheModule) : M(TheModule) {
75 FILE *F = fopen(Filename.c_str(), "r");
76 if (F == 0) {
Chris Lattnerc6fca422004-02-11 18:20:41 +000077 std::cerr << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000078 perror(0);
79 exit(1);
80 }
81
82 // Keep reading packets until we run out of them.
83 unsigned PacketType;
84 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
85 // If the low eight bits of the packet are zero, we must be dealing with an
86 // endianness mismatch. Byteswap all words read from the profiling
87 // information.
88 bool ShouldByteSwap = (char)PacketType == 0;
89 PacketType = ByteSwap(PacketType, ShouldByteSwap);
90
91 switch (PacketType) {
92 case ArgumentInfo: {
93 unsigned ArgLength;
94 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
95 std::cerr << ToolName << ": arguments packet truncated!\n";
96 perror(0);
97 exit(1);
98 }
99 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
100
101 // Read in the arguments...
102 std::vector<char> Chars(ArgLength+4);
103
104 if (ArgLength)
105 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
106 std::cerr << ToolName << ": arguments packet truncated!\n";
107 perror(0);
108 exit(1);
109 }
110 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
111 break;
112 }
113
114 case FunctionInfo:
115 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
116 break;
117
118 case BlockInfo:
119 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
120 break;
121
Chris Lattner01945c12004-03-08 18:20:18 +0000122 case EdgeInfo:
123 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
124 break;
125
Brian Gaekeb171d792004-05-04 17:11:14 +0000126 case BBTraceInfo:
127 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
128 break;
129
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000130 default:
131 std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
132 exit(1);
133 }
134 }
135
136 fclose(F);
137}
138
139
140// getFunctionCounts - This method is used by consumers of function counting
141// information. If we do not directly have function count information, we
142// compute it from other, more refined, types of profile information.
143//
144void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
145 unsigned> > &Counts) {
146 if (FunctionCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000147 if (hasAccurateBlockCounts()) {
148 // Synthesize function frequency information from the number of times
149 // their entry blocks were executed.
150 std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
151 getBlockCounts(BlockCounts);
152
153 for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
154 if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
155 Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
156 BlockCounts[i].second));
157 } else {
158 std::cerr << "Function counts are not available!\n";
159 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000160 return;
161 }
162
163 unsigned Counter = 0;
164 for (Module::iterator I = M.begin(), E = M.end();
165 I != E && Counter != FunctionCounts.size(); ++I)
166 if (!I->isExternal())
167 Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
168}
169
170// getBlockCounts - This method is used by consumers of block counting
171// information. If we do not directly have block count information, we
172// compute it from other, more refined, types of profile information.
173//
174void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
175 unsigned> > &Counts) {
176 if (BlockCounts.empty()) {
Chris Lattnerdbbbfef2004-03-08 20:03:52 +0000177 if (hasAccurateEdgeCounts()) {
178 // Synthesize block count information from edge frequency information.
179 // The block execution frequency is equal to the sum of the execution
180 // frequency of all outgoing edges from a block.
181 //
182 // If a block has no successors, this will not be correct, so we have to
183 // special case it. :(
184 std::vector<std::pair<Edge, unsigned> > EdgeCounts;
185 getEdgeCounts(EdgeCounts);
186
187 std::map<BasicBlock*, unsigned> InEdgeFreqs;
188
189 BasicBlock *LastBlock = 0;
190 TerminatorInst *TI = 0;
191 for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
192 if (EdgeCounts[i].first.first != LastBlock) {
193 LastBlock = EdgeCounts[i].first.first;
194 TI = LastBlock->getTerminator();
195 Counts.push_back(std::make_pair(LastBlock, 0));
196 }
197 Counts.back().second += EdgeCounts[i].second;
198 unsigned SuccNum = EdgeCounts[i].first.second;
199 if (SuccNum >= TI->getNumSuccessors()) {
200 static bool Warned = false;
201 if (!Warned) {
202 std::cerr << "WARNING: profile info doesn't seem to match"
203 << " the program!\n";
204 Warned = true;
205 }
206 } else {
207 // If this successor has no successors of its own, we will never
208 // compute an execution count for that block. Remember the incoming
209 // edge frequencies to add later.
210 BasicBlock *Succ = TI->getSuccessor(SuccNum);
211 if (Succ->getTerminator()->getNumSuccessors() == 0)
212 InEdgeFreqs[Succ] += EdgeCounts[i].second;
213 }
214 }
215
216 // Now we have to accumulate information for those blocks without
217 // successors into our table.
218 for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
219 E = InEdgeFreqs.end(); I != E; ++I) {
220 unsigned i = 0;
221 for (; i != Counts.size() && Counts[i].first != I->first; ++i)
222 /*empty*/;
223 if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
224 Counts[i].second += I->second;
225 }
226
227 } else {
228 std::cerr << "Block counts are not available!\n";
229 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000230 return;
231 }
232
233 unsigned Counter = 0;
234 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
235 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
236 Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
237 if (Counter == BlockCounts.size())
238 return;
239 }
240}
Chris Lattner01945c12004-03-08 18:20:18 +0000241
242// getEdgeCounts - This method is used by consumers of edge counting
243// information. If we do not directly have edge count information, we compute
244// it from other, more refined, types of profile information.
245//
246void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge,
247 unsigned> > &Counts) {
248 if (EdgeCounts.empty()) {
249 std::cerr << "Edge counts not available, and no synthesis "
250 << "is implemented yet!\n";
251 return;
252 }
253
254 unsigned Counter = 0;
255 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
256 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
257 for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors();
258 i != e; ++i) {
259 Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++]));
260 if (Counter == EdgeCounts.size())
261 return;
262 }
263}
Brian Gaekeb171d792004-05-04 17:11:14 +0000264
265// getBBTrace - This method is used by consumers of basic-block trace
266// information.
267//
268void ProfileInfoLoader::getBBTrace(std::vector<BasicBlock *> &Trace) {
269 if (BBTrace.empty ()) {
270 std::cerr << "Basic block trace is not available!\n";
271 return;
272 }
273 std::cerr << "Basic block trace loading is not implemented yet!\n";
274}
275