blob: 000649228a6234ed614badfe3fa8de3e1ca6e96e [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
15#include "llvm/Module.h"
16#include "llvm/InstrTypes.h"
17#include "llvm/Analysis/ProfileDataLoader.h"
18#include "llvm/Analysis/ProfileDataTypes.h"
19#include "llvm/Support/raw_ostream.h"
20#include <cstdio>
21#include <cstdlib>
22using namespace llvm;
23
24namespace llvm {
25
26template<>
27char ProfileDataT<Function,BasicBlock>::ID = 0;
28
29raw_ostream& operator<<(raw_ostream &O, const Function *F) {
30 return O << F->getName();
31}
32
33raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
34 return O << BB->getName();
35}
36
37raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E) {
38 O << "(";
39
40 if (E.first)
41 O << E.first;
42 else
43 O << "0";
44
45 O << ",";
46
47 if (E.second)
48 O << E.second;
49 else
50 O << "0";
51
52 return O << ")";
53}
54
55} // namespace llvm
56
57/// ByteSwap - Byteswap 'Var' if 'Really' is true. Required when the compiler
58/// host and target have different endianness.
59static inline unsigned ByteSwap(unsigned Var, bool Really) {
60 if (!Really) return Var;
61 return ((Var & (255U<< 0U)) << 24U) |
62 ((Var & (255U<< 8U)) << 8U) |
63 ((Var & (255U<<16U)) >> 8U) |
64 ((Var & (255U<<24U)) >> 24U);
65}
66
67/// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
68/// (or both) may not be defined.
69static unsigned AddCounts(unsigned A, unsigned B) {
70 // If either value is undefined, use the other.
71 // Undefined + undefined = undefined.
72 if (A == ProfileDataLoader::Uncounted) return B;
73 if (B == ProfileDataLoader::Uncounted) return A;
74
75 // Saturate to the maximum storable value. This could change taken/nottaken
76 // ratios, but is presumably better than wrapping and thus potentially
77 // inverting ratios.
78 unsigned long long tmp = (unsigned long long)A + (unsigned long long)B;
79 if (tmp > (unsigned long long)ProfileDataLoader::MaxCount)
80 tmp = ProfileDataLoader::MaxCount;
81 return (unsigned)tmp;
82}
83
84/// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
85template <typename T>
86static void ReadProfilingData(const char *ToolName, FILE *F,
87 std::vector<T> &Data, size_t NumEntries) {
88 // Read in the block of data...
89 if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) {
90 errs() << ToolName << ": profiling data truncated!\n";
91 perror(0);
92 exit(1);
93 }
94}
95
96/// ReadProfilingNumEntries - Read how many entries are in this profiling data
97/// packet.
98static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
99 bool ShouldByteSwap) {
100 std::vector<unsigned> NumEntries(1);
101 ReadProfilingData<unsigned>(ToolName, F, NumEntries, 1);
102 return ByteSwap(NumEntries[0], ShouldByteSwap);
103}
104
105/// ReadProfilingBlock - Read the number of entries in the next profiling data
106/// packet and then accumulate the entries into 'Data'.
107static void ReadProfilingBlock(const char *ToolName, FILE *F,
108 bool ShouldByteSwap,
109 std::vector<unsigned> &Data) {
110 // Read the number of entries...
111 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
112
113 // Read in the data.
114 std::vector<unsigned> TempSpace(NumEntries);
115 ReadProfilingData<unsigned>(ToolName, F, TempSpace, (size_t)NumEntries);
116
117 // Make sure we have enough space ...
118 if (Data.size() < NumEntries)
119 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
120
121 // Accumulate the data we just read into the existing data.
122 for (unsigned i = 0; i < NumEntries; ++i) {
123 Data[i] = AddCounts(ByteSwap(TempSpace[i], ShouldByteSwap), Data[i]);
124 }
125}
126
127/// ReadProfilingArgBlock - Read the command line arguments that the progam was
128/// run with when the current profiling data packet(s) were generated.
129static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
130 bool ShouldByteSwap,
131 std::vector<std::string> &CommandLines) {
132 // Read the number of bytes ...
133 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
134
135 // Read in the arguments (if there are any to read). Round up the length to
136 // the nearest 4-byte multiple.
137 std::vector<char> Args(ArgLength+4);
138 if (ArgLength)
139 ReadProfilingData<char>(ToolName, F, Args, (ArgLength+3) & ~3);
140
141 // Store the arguments.
142 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
143}
144
145const unsigned ProfileDataLoader::Uncounted = ~0U;
146const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
147
148/// ProfileDataLoader ctor - Read the specified profiling data file, exiting
149/// the program if the file is invalid or broken.
150ProfileDataLoader::ProfileDataLoader(const char *ToolName,
151 const std::string &Filename)
152 : Filename(Filename) {
153 FILE *F = fopen(Filename.c_str(), "rb");
154 if (F == 0) {
155 errs() << ToolName << ": Error opening '" << Filename << "': ";
156 perror(0);
157 exit(1);
158 }
159
160 // Keep reading packets until we run out of them.
161 unsigned PacketType;
162 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
163 // If the low eight bits of the packet are zero, we must be dealing with an
164 // endianness mismatch. Byteswap all words read from the profiling
165 // information. This can happen when the compiler host and target have
166 // different endianness.
167 bool ShouldByteSwap = (char)PacketType == 0;
168 PacketType = ByteSwap(PacketType, ShouldByteSwap);
169
170 switch (PacketType) {
171 case ArgumentInfo:
172 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
173 break;
174
175 case EdgeInfo:
176 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
177 break;
178
179 default:
180 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
181 exit(1);
182 }
183 }
184
185 fclose(F);
186}