InstrProf: Move constructor to the header
[oota-llvm.git] / lib / ProfileData / InstrProfReader.cpp
1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
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 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
17
18 #include <cassert>
19
20 using namespace llvm;
21
22 error_code InstrProfReader::create(std::string Path,
23                                    std::unique_ptr<InstrProfReader> &Result) {
24   std::unique_ptr<MemoryBuffer> Buffer;
25   if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
26     return EC;
27
28   // Sanity check the file.
29   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
30     return instrprof_error::too_large;
31
32   // Create the reader.
33   if (RawInstrProfReader::hasFormat(*Buffer))
34     Result.reset(new RawInstrProfReader(std::move(Buffer)));
35   else
36     Result.reset(new TextInstrProfReader(std::move(Buffer)));
37
38   // Read the header and return the result.
39   return Result->readHeader();
40 }
41
42 void InstrProfIterator::Increment() {
43   if (Reader->readNextRecord(Record))
44     *this = InstrProfIterator();
45 }
46
47 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
48   // Skip empty lines.
49   while (!Line.is_at_end() && Line->empty())
50     ++Line;
51   // If we hit EOF while looking for a name, we're done.
52   if (Line.is_at_end())
53     return error(instrprof_error::eof);
54
55   // Read the function name.
56   Record.Name = *Line++;
57
58   // Read the function hash.
59   if (Line.is_at_end())
60     return error(instrprof_error::truncated);
61   if ((Line++)->getAsInteger(10, Record.Hash))
62     return error(instrprof_error::malformed);
63
64   // Read the number of counters.
65   uint64_t NumCounters;
66   if (Line.is_at_end())
67     return error(instrprof_error::truncated);
68   if ((Line++)->getAsInteger(10, NumCounters))
69     return error(instrprof_error::malformed);
70
71   // Read each counter and fill our internal storage with the values.
72   Counts.clear();
73   Counts.reserve(NumCounters);
74   for (uint64_t I = 0; I < NumCounters; ++I) {
75     if (Line.is_at_end())
76       return error(instrprof_error::truncated);
77     uint64_t Count;
78     if ((Line++)->getAsInteger(10, Count))
79       return error(instrprof_error::malformed);
80     Counts.push_back(Count);
81   }
82   // Give the record a reference to our internal counter storage.
83   Record.Counts = Counts;
84
85   return success();
86 }
87
88 static uint64_t getRawMagic() {
89   return
90     uint64_t(255) << 56 |
91     uint64_t('l') << 48 |
92     uint64_t('p') << 40 |
93     uint64_t('r') << 32 |
94     uint64_t('o') << 24 |
95     uint64_t('f') << 16 |
96     uint64_t('r') <<  8 |
97     uint64_t(129);
98 }
99
100 bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
101   if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
102     return false;
103   const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
104   return getRawMagic() == Header->Magic ||
105     sys::SwapByteOrder(getRawMagic()) == Header->Magic;
106 }
107
108 error_code RawInstrProfReader::readHeader() {
109   if (!hasFormat(*DataBuffer))
110     return error(instrprof_error::bad_magic);
111   if (DataBuffer->getBufferSize() < sizeof(RawHeader))
112     return error(instrprof_error::bad_header);
113   const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
114   ShouldSwapBytes = Header->Magic != getRawMagic();
115   return readHeader(*Header);
116 }
117
118 static uint64_t getRawVersion() {
119   return 1;
120 }
121
122 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
123   if (swap(Header.Version) != getRawVersion())
124     return error(instrprof_error::unsupported_version);
125
126   CountersDelta = swap(Header.CountersDelta);
127   NamesDelta = swap(Header.NamesDelta);
128   auto DataSize = swap(Header.DataSize);
129   auto CountersSize = swap(Header.CountersSize);
130   auto NamesSize = swap(Header.NamesSize);
131
132   ptrdiff_t DataOffset = sizeof(RawHeader);
133   ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
134   ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
135   size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
136
137   if (FileSize != DataBuffer->getBufferSize())
138     return error(instrprof_error::bad_header);
139
140   Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
141   DataEnd = Data + DataSize;
142   CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
143   NamesStart = DataBuffer->getBufferStart() + NamesOffset;
144
145   return success();
146 }
147
148 error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
149   if (Data == DataEnd)
150     return error(instrprof_error::eof);
151
152   // Get the raw data.
153   StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
154   auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
155                                 swap(Data->NumCounters));
156
157   // Check bounds.
158   if (RawName.data() < NamesStart ||
159       RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
160       RawCounts.data() < CountersStart ||
161       RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
162     return error(instrprof_error::malformed);
163
164   // Store the data in Record, byte-swapping as necessary.
165   Record.Hash = swap(Data->FuncHash);
166   Record.Name = RawName;
167   if (ShouldSwapBytes) {
168     Counts.clear();
169     Counts.reserve(RawCounts.size());
170     for (uint64_t Count : RawCounts)
171       Counts.push_back(swap(Count));
172     Record.Counts = Counts;
173   } else
174     Record.Counts = RawCounts;
175
176   // Iterate.
177   ++Data;
178   return success();
179 }