blob: 01b50384f9473a869579def0ddcb93b29a8b56d6 [file] [log] [blame]
Manman Rend2620042012-08-28 22:21:25 +00001//===- ProfileDataLoader.cpp - Load profile information from disk ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// The ProfileDataLoader class is used to load raw profiling data from the dump
11// file.
12//
13//===----------------------------------------------------------------------===//
14
Bill Wendlingf91e4002012-08-31 05:18:31 +000015#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/OwningPtr.h"
Manman Rend2620042012-08-28 22:21:25 +000017#include "llvm/Module.h"
18#include "llvm/InstrTypes.h"
19#include "llvm/Analysis/ProfileDataLoader.h"
20#include "llvm/Analysis/ProfileDataTypes.h"
21#include "llvm/Support/raw_ostream.h"
Bill Wendlingf91e4002012-08-31 05:18:31 +000022#include "llvm/Support/system_error.h"
Manman Rend2620042012-08-28 22:21:25 +000023#include <cstdio>
24#include <cstdlib>
25using namespace llvm;
26
27namespace llvm {
28
29template<>
30char ProfileDataT<Function,BasicBlock>::ID = 0;
31
32raw_ostream& operator<<(raw_ostream &O, const Function *F) {
33 return O << F->getName();
34}
35
36raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
37 return O << BB->getName();
38}
39
Bill Wendlingf91e4002012-08-31 05:18:31 +000040raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
41 const BasicBlock *> E) {
Manman Rend2620042012-08-28 22:21:25 +000042 O << "(";
43
44 if (E.first)
45 O << E.first;
46 else
47 O << "0";
48
49 O << ",";
50
51 if (E.second)
52 O << E.second;
53 else
54 O << "0";
55
56 return O << ")";
57}
58
59} // namespace llvm
60
Bill Wendlingf91e4002012-08-31 05:18:31 +000061/// ByteSwap - Byteswap 'Var'. Required when the compiler host and target have
62/// different endianness.
63static inline unsigned ByteSwap(unsigned Var) {
Manman Rend2620042012-08-28 22:21:25 +000064 return ((Var & (255U<< 0U)) << 24U) |
65 ((Var & (255U<< 8U)) << 8U) |
66 ((Var & (255U<<16U)) >> 8U) |
67 ((Var & (255U<<24U)) >> 24U);
68}
69
70/// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
71/// (or both) may not be defined.
72static unsigned AddCounts(unsigned A, unsigned B) {
73 // If either value is undefined, use the other.
74 // Undefined + undefined = undefined.
75 if (A == ProfileDataLoader::Uncounted) return B;
76 if (B == ProfileDataLoader::Uncounted) return A;
77
78 // Saturate to the maximum storable value. This could change taken/nottaken
79 // ratios, but is presumably better than wrapping and thus potentially
80 // inverting ratios.
Bill Wendlingf91e4002012-08-31 05:18:31 +000081 uint64_t tmp = (uint64_t)A + (uint64_t)B;
82 if (tmp > (uint64_t)ProfileDataLoader::MaxCount)
Manman Rend2620042012-08-28 22:21:25 +000083 tmp = ProfileDataLoader::MaxCount;
84 return (unsigned)tmp;
85}
86
87/// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
Bill Wendlingf91e4002012-08-31 05:18:31 +000088template <typename T, unsigned N>
Manman Rend2620042012-08-28 22:21:25 +000089static void ReadProfilingData(const char *ToolName, FILE *F,
Bill Wendlingf91e4002012-08-31 05:18:31 +000090 SmallVector<T, N> &Data, size_t NumEntries) {
Manman Rend2620042012-08-28 22:21:25 +000091 // Read in the block of data...
92 if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) {
Bill Wendlingf91e4002012-08-31 05:18:31 +000093 report_fatal_error(std::string(ToolName) + ": Profiling data truncated");
Manman Rend2620042012-08-28 22:21:25 +000094 }
95}
96
97/// ReadProfilingNumEntries - Read how many entries are in this profiling data
98/// packet.
99static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
100 bool ShouldByteSwap) {
Bill Wendlingf91e4002012-08-31 05:18:31 +0000101 SmallVector<unsigned, 1> NumEntries(1);
102 ReadProfilingData<unsigned, 1>(ToolName, F, NumEntries, 1);
103 return ShouldByteSwap ? ByteSwap(NumEntries[0]) : NumEntries[0];
Manman Rend2620042012-08-28 22:21:25 +0000104}
105
106/// ReadProfilingBlock - Read the number of entries in the next profiling data
107/// packet and then accumulate the entries into 'Data'.
108static void ReadProfilingBlock(const char *ToolName, FILE *F,
109 bool ShouldByteSwap,
Bill Wendlingf91e4002012-08-31 05:18:31 +0000110 SmallVector<unsigned, 32> &Data) {
Manman Rend2620042012-08-28 22:21:25 +0000111 // Read the number of entries...
112 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
113
114 // Read in the data.
Bill Wendlingf91e4002012-08-31 05:18:31 +0000115 SmallVector<unsigned, 8> TempSpace(NumEntries);
116 ReadProfilingData<unsigned, 8>(ToolName, F, TempSpace, (size_t)NumEntries);
Manman Rend2620042012-08-28 22:21:25 +0000117
118 // Make sure we have enough space ...
119 if (Data.size() < NumEntries)
120 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
121
122 // Accumulate the data we just read into the existing data.
Bill Wendlingf91e4002012-08-31 05:18:31 +0000123 for (unsigned i = 0; i < NumEntries; ++i)
124 Data[i] = AddCounts(ShouldByteSwap ? ByteSwap(TempSpace[i]) : TempSpace[i],
125 Data[i]);
Manman Rend2620042012-08-28 22:21:25 +0000126}
127
128/// ReadProfilingArgBlock - Read the command line arguments that the progam was
129/// run with when the current profiling data packet(s) were generated.
130static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
131 bool ShouldByteSwap,
Bill Wendlingf91e4002012-08-31 05:18:31 +0000132 SmallVector<std::string, 1> &CommandLines) {
Manman Rend2620042012-08-28 22:21:25 +0000133 // Read the number of bytes ...
134 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
135
136 // Read in the arguments (if there are any to read). Round up the length to
137 // the nearest 4-byte multiple.
Bill Wendlingf91e4002012-08-31 05:18:31 +0000138 SmallVector<char, 8> Args(ArgLength+4);
Manman Rend2620042012-08-28 22:21:25 +0000139 if (ArgLength)
Bill Wendlingf91e4002012-08-31 05:18:31 +0000140 ReadProfilingData<char, 8>(ToolName, F, Args, (ArgLength+3) & ~3);
Manman Rend2620042012-08-28 22:21:25 +0000141
142 // Store the arguments.
143 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
144}
145
146const unsigned ProfileDataLoader::Uncounted = ~0U;
147const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
148
Bill Wendlingf91e4002012-08-31 05:18:31 +0000149/// ProfileDataLoader ctor - Read the specified profiling data file, reporting
150/// a fatal error if the file is invalid or broken.
Manman Rend2620042012-08-28 22:21:25 +0000151ProfileDataLoader::ProfileDataLoader(const char *ToolName,
152 const std::string &Filename)
153 : Filename(Filename) {
154 FILE *F = fopen(Filename.c_str(), "rb");
Bill Wendlingf91e4002012-08-31 05:18:31 +0000155 if (F == 0)
156 report_fatal_error(std::string(ToolName) + ": Error opening '" +
157 Filename + "': ");
Manman Rend2620042012-08-28 22:21:25 +0000158
159 // Keep reading packets until we run out of them.
160 unsigned PacketType;
161 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
162 // If the low eight bits of the packet are zero, we must be dealing with an
163 // endianness mismatch. Byteswap all words read from the profiling
164 // information. This can happen when the compiler host and target have
165 // different endianness.
166 bool ShouldByteSwap = (char)PacketType == 0;
Bill Wendlingf91e4002012-08-31 05:18:31 +0000167 PacketType = ShouldByteSwap ? ByteSwap(PacketType) : PacketType;
Manman Rend2620042012-08-28 22:21:25 +0000168
169 switch (PacketType) {
170 case ArgumentInfo:
171 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
172 break;
173
174 case EdgeInfo:
175 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
176 break;
177
178 default:
Bill Wendlingf91e4002012-08-31 05:18:31 +0000179 report_fatal_error(std::string(ToolName)
180 + ": Unknown profiling packet type");
181 break;
Manman Rend2620042012-08-28 22:21:25 +0000182 }
183 }
184
185 fclose(F);
186}