1 //===- ProfileDataLoader.cpp - Load profile information from disk ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The ProfileDataLoader class is used to load raw profiling data from the dump
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #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"
22 #include "llvm/Support/system_error.h"
30 char ProfileDataT<Function,BasicBlock>::ID = 0;
32 raw_ostream& operator<<(raw_ostream &O, const Function *F) {
33 return O << F->getName();
36 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
37 return O << BB->getName();
40 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
41 const BasicBlock *> E) {
61 /// ByteSwap - Byteswap 'Var'. Required when the compiler host and target have
62 /// different endianness.
63 static inline unsigned ByteSwap(unsigned Var) {
64 return ((Var & (255U<< 0U)) << 24U) |
65 ((Var & (255U<< 8U)) << 8U) |
66 ((Var & (255U<<16U)) >> 8U) |
67 ((Var & (255U<<24U)) >> 24U);
70 /// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
71 /// (or both) may not be defined.
72 static 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;
78 // Saturate to the maximum storable value. This could change taken/nottaken
79 // ratios, but is presumably better than wrapping and thus potentially
81 uint64_t tmp = (uint64_t)A + (uint64_t)B;
82 if (tmp > (uint64_t)ProfileDataLoader::MaxCount)
83 tmp = ProfileDataLoader::MaxCount;
87 /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
88 template <typename T, unsigned N>
89 static void ReadProfilingData(const char *ToolName, FILE *F,
90 SmallVector<T, N> &Data, size_t NumEntries) {
91 // Read in the block of data...
92 if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) {
93 report_fatal_error(std::string(ToolName) + ": Profiling data truncated");
97 /// ReadProfilingNumEntries - Read how many entries are in this profiling data
99 static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
100 bool ShouldByteSwap) {
101 SmallVector<unsigned, 1> NumEntries(1);
102 ReadProfilingData<unsigned, 1>(ToolName, F, NumEntries, 1);
103 return ShouldByteSwap ? ByteSwap(NumEntries[0]) : NumEntries[0];
106 /// ReadProfilingBlock - Read the number of entries in the next profiling data
107 /// packet and then accumulate the entries into 'Data'.
108 static void ReadProfilingBlock(const char *ToolName, FILE *F,
110 SmallVector<unsigned, 32> &Data) {
111 // Read the number of entries...
112 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
115 SmallVector<unsigned, 8> TempSpace(NumEntries);
116 ReadProfilingData<unsigned, 8>(ToolName, F, TempSpace, (size_t)NumEntries);
118 // Make sure we have enough space ...
119 if (Data.size() < NumEntries)
120 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
122 // Accumulate the data we just read into the existing data.
123 for (unsigned i = 0; i < NumEntries; ++i)
124 Data[i] = AddCounts(ShouldByteSwap ? ByteSwap(TempSpace[i]) : TempSpace[i],
128 /// ReadProfilingArgBlock - Read the command line arguments that the progam was
129 /// run with when the current profiling data packet(s) were generated.
130 static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
132 SmallVector<std::string, 1> &CommandLines) {
133 // Read the number of bytes ...
134 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
136 // Read in the arguments (if there are any to read). Round up the length to
137 // the nearest 4-byte multiple.
138 SmallVector<char, 8> Args(ArgLength+4);
140 ReadProfilingData<char, 8>(ToolName, F, Args, (ArgLength+3) & ~3);
142 // Store the arguments.
143 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
146 const unsigned ProfileDataLoader::Uncounted = ~0U;
147 const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
149 /// ProfileDataLoader ctor - Read the specified profiling data file, reporting
150 /// a fatal error if the file is invalid or broken.
151 ProfileDataLoader::ProfileDataLoader(const char *ToolName,
152 const std::string &Filename)
153 : Filename(Filename) {
154 FILE *F = fopen(Filename.c_str(), "rb");
156 report_fatal_error(std::string(ToolName) + ": Error opening '" +
159 // Keep reading packets until we run out of them.
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;
167 PacketType = ShouldByteSwap ? ByteSwap(PacketType) : PacketType;
169 switch (PacketType) {
171 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
175 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
179 report_fatal_error(std::string(ToolName)
180 + ": Unknown profiling packet type");