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/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"
27 char ProfileDataT<Function,BasicBlock>::ID = 0;
29 raw_ostream& operator<<(raw_ostream &O, const Function *F) {
30 return O << F->getName();
33 raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
34 return O << BB->getName();
37 raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E) {
57 /// ByteSwap - Byteswap 'Var' if 'Really' is true. Required when the compiler
58 /// host and target have different endianness.
59 static 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);
67 /// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
68 /// (or both) may not be defined.
69 static 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;
75 // Saturate to the maximum storable value. This could change taken/nottaken
76 // ratios, but is presumably better than wrapping and thus potentially
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;
84 /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
86 static 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";
96 /// ReadProfilingNumEntries - Read how many entries are in this profiling data
98 static 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);
105 /// ReadProfilingBlock - Read the number of entries in the next profiling data
106 /// packet and then accumulate the entries into 'Data'.
107 static void ReadProfilingBlock(const char *ToolName, FILE *F,
109 std::vector<unsigned> &Data) {
110 // Read the number of entries...
111 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
114 std::vector<unsigned> TempSpace(NumEntries);
115 ReadProfilingData<unsigned>(ToolName, F, TempSpace, (size_t)NumEntries);
117 // Make sure we have enough space ...
118 if (Data.size() < NumEntries)
119 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
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]);
127 /// ReadProfilingArgBlock - Read the command line arguments that the progam was
128 /// run with when the current profiling data packet(s) were generated.
129 static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
131 std::vector<std::string> &CommandLines) {
132 // Read the number of bytes ...
133 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
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);
139 ReadProfilingData<char>(ToolName, F, Args, (ArgLength+3) & ~3);
141 // Store the arguments.
142 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
145 const unsigned ProfileDataLoader::Uncounted = ~0U;
146 const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
148 /// ProfileDataLoader ctor - Read the specified profiling data file, exiting
149 /// the program if the file is invalid or broken.
150 ProfileDataLoader::ProfileDataLoader(const char *ToolName,
151 const std::string &Filename)
152 : Filename(Filename) {
153 FILE *F = fopen(Filename.c_str(), "rb");
155 errs() << ToolName << ": Error opening '" << Filename << "': ";
160 // Keep reading packets until we run out of them.
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);
170 switch (PacketType) {
172 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
176 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
180 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";