blob: a4f634af531e946292314521ad154a1770b7cf3e [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
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000027raw_ostream &llvm::operator<<(raw_ostream &O, std::pair<const BasicBlock *,
28 const BasicBlock *> E) {
Manman Rend2620042012-08-28 22:21:25 +000029 O << "(";
30
31 if (E.first)
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000032 O << E.first->getName();
Manman Rend2620042012-08-28 22:21:25 +000033 else
34 O << "0";
35
36 O << ",";
37
38 if (E.second)
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000039 O << E.second->getName();
Manman Rend2620042012-08-28 22:21:25 +000040 else
41 O << "0";
42
43 return O << ")";
44}
45
Manman Rend2620042012-08-28 22:21:25 +000046/// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one
47/// (or both) may not be defined.
48static unsigned AddCounts(unsigned A, unsigned B) {
49 // If either value is undefined, use the other.
50 // Undefined + undefined = undefined.
51 if (A == ProfileDataLoader::Uncounted) return B;
52 if (B == ProfileDataLoader::Uncounted) return A;
53
Bob Wilson6a090982012-10-29 17:27:39 +000054 return A + B;
Manman Rend2620042012-08-28 22:21:25 +000055}
56
57/// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000058template <typename T>
Manman Rend2620042012-08-28 22:21:25 +000059static void ReadProfilingData(const char *ToolName, FILE *F,
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000060 T *Data, size_t NumEntries) {
Manman Rend2620042012-08-28 22:21:25 +000061 // Read in the block of data...
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000062 if (fread(Data, sizeof(T), NumEntries, F) != NumEntries)
63 report_fatal_error(Twine(ToolName) + ": Profiling data truncated");
Manman Rend2620042012-08-28 22:21:25 +000064}
65
66/// ReadProfilingNumEntries - Read how many entries are in this profiling data
67/// packet.
68static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
69 bool ShouldByteSwap) {
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000070 unsigned Entry;
71 ReadProfilingData<unsigned>(ToolName, F, &Entry, 1);
72 return ShouldByteSwap ? ByteSwap_32(Entry) : Entry;
Manman Rend2620042012-08-28 22:21:25 +000073}
74
75/// ReadProfilingBlock - Read the number of entries in the next profiling data
76/// packet and then accumulate the entries into 'Data'.
77static void ReadProfilingBlock(const char *ToolName, FILE *F,
78 bool ShouldByteSwap,
Bill Wendlingf91e4002012-08-31 05:18:31 +000079 SmallVector<unsigned, 32> &Data) {
Manman Rend2620042012-08-28 22:21:25 +000080 // Read the number of entries...
81 unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
82
83 // Read in the data.
Bill Wendlingf91e4002012-08-31 05:18:31 +000084 SmallVector<unsigned, 8> TempSpace(NumEntries);
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000085 ReadProfilingData<unsigned>(ToolName, F, TempSpace.data(), NumEntries);
Manman Rend2620042012-08-28 22:21:25 +000086
87 // Make sure we have enough space ...
88 if (Data.size() < NumEntries)
89 Data.resize(NumEntries, ProfileDataLoader::Uncounted);
90
91 // Accumulate the data we just read into the existing data.
Benjamin Kramercb5f63d2012-08-31 12:43:07 +000092 for (unsigned i = 0; i < NumEntries; ++i) {
93 unsigned Entry = ShouldByteSwap ? ByteSwap_32(TempSpace[i]) : TempSpace[i];
94 Data[i] = AddCounts(Entry, Data[i]);
95 }
Manman Rend2620042012-08-28 22:21:25 +000096}
97
98/// ReadProfilingArgBlock - Read the command line arguments that the progam was
99/// run with when the current profiling data packet(s) were generated.
100static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
101 bool ShouldByteSwap,
Bill Wendlingf91e4002012-08-31 05:18:31 +0000102 SmallVector<std::string, 1> &CommandLines) {
Manman Rend2620042012-08-28 22:21:25 +0000103 // Read the number of bytes ...
104 unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
105
106 // Read in the arguments (if there are any to read). Round up the length to
107 // the nearest 4-byte multiple.
Bill Wendlingf91e4002012-08-31 05:18:31 +0000108 SmallVector<char, 8> Args(ArgLength+4);
Manman Rend2620042012-08-28 22:21:25 +0000109 if (ArgLength)
Benjamin Kramercb5f63d2012-08-31 12:43:07 +0000110 ReadProfilingData<char>(ToolName, F, Args.data(), (ArgLength+3) & ~3);
Manman Rend2620042012-08-28 22:21:25 +0000111
112 // Store the arguments.
113 CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
114}
115
116const unsigned ProfileDataLoader::Uncounted = ~0U;
Manman Rend2620042012-08-28 22:21:25 +0000117
Bill Wendlingf91e4002012-08-31 05:18:31 +0000118/// ProfileDataLoader ctor - Read the specified profiling data file, reporting
119/// a fatal error if the file is invalid or broken.
Manman Rend2620042012-08-28 22:21:25 +0000120ProfileDataLoader::ProfileDataLoader(const char *ToolName,
121 const std::string &Filename)
122 : Filename(Filename) {
123 FILE *F = fopen(Filename.c_str(), "rb");
Bill Wendlingf91e4002012-08-31 05:18:31 +0000124 if (F == 0)
Benjamin Kramercb5f63d2012-08-31 12:43:07 +0000125 report_fatal_error(Twine(ToolName) + ": Error opening '" +
Bill Wendlingf91e4002012-08-31 05:18:31 +0000126 Filename + "': ");
Manman Rend2620042012-08-28 22:21:25 +0000127
128 // Keep reading packets until we run out of them.
129 unsigned PacketType;
130 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
131 // If the low eight bits of the packet are zero, we must be dealing with an
132 // endianness mismatch. Byteswap all words read from the profiling
133 // information. This can happen when the compiler host and target have
134 // different endianness.
135 bool ShouldByteSwap = (char)PacketType == 0;
Benjamin Kramercb5f63d2012-08-31 12:43:07 +0000136 PacketType = ShouldByteSwap ? ByteSwap_32(PacketType) : PacketType;
Manman Rend2620042012-08-28 22:21:25 +0000137
138 switch (PacketType) {
139 case ArgumentInfo:
140 ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
141 break;
142
143 case EdgeInfo:
144 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
145 break;
146
147 default:
Bill Wendlingf91e4002012-08-31 05:18:31 +0000148 report_fatal_error(std::string(ToolName)
149 + ": Unknown profiling packet type");
150 break;
Manman Rend2620042012-08-28 22:21:25 +0000151 }
152 }
153
154 fclose(F);
155}