Coverage: improve efficiency of the counter propagation to the expansion regions.
[oota-llvm.git] / lib / ProfileData / CoverageMappingReader.cpp
1 //=-- CoverageMappingReader.cpp - Code coverage mapping reader ----*- C++ -*-=//
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 coverage mapping data for
11 // instrumentation based coverage.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ProfileData/CoverageMappingReader.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Support/LEB128.h"
19
20 using namespace llvm;
21 using namespace coverage;
22 using namespace object;
23
24 void CoverageMappingIterator::increment() {
25   // Check if all the records were read or if an error occurred while reading
26   // the next record.
27   if (Reader->readNextRecord(Record))
28     *this = CoverageMappingIterator();
29 }
30
31 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
32   if (Data.size() < 1)
33     return error(instrprof_error::truncated);
34   unsigned N = 0;
35   Result = decodeULEB128(reinterpret_cast<const uint8_t *>(Data.data()), &N);
36   if (N > Data.size())
37     return error(instrprof_error::malformed);
38   Data = Data.substr(N);
39   return success();
40 }
41
42 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
43                                               uint64_t MaxPlus1) {
44   if (auto Err = readULEB128(Result))
45     return Err;
46   if (Result >= MaxPlus1)
47     return error(instrprof_error::malformed);
48   return success();
49 }
50
51 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
52   if (auto Err = readULEB128(Result))
53     return Err;
54   // Sanity check the number.
55   if (Result > Data.size())
56     return error(instrprof_error::malformed);
57   return success();
58 }
59
60 std::error_code RawCoverageReader::readString(StringRef &Result) {
61   uint64_t Length;
62   if (auto Err = readSize(Length))
63     return Err;
64   Result = Data.substr(0, Length);
65   Data = Data.substr(Length);
66   return success();
67 }
68
69 std::error_code RawCoverageFilenamesReader::read() {
70   uint64_t NumFilenames;
71   if (auto Err = readSize(NumFilenames))
72     return Err;
73   for (size_t I = 0; I < NumFilenames; ++I) {
74     StringRef Filename;
75     if (auto Err = readString(Filename))
76       return Err;
77     Filenames.push_back(Filename);
78   }
79   return success();
80 }
81
82 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
83                                                         Counter &C) {
84   auto Tag = Value & Counter::EncodingTagMask;
85   switch (Tag) {
86   case Counter::Zero:
87     C = Counter::getZero();
88     return success();
89   case Counter::CounterValueReference:
90     C = Counter::getCounter(Value >> Counter::EncodingTagBits);
91     return success();
92   default:
93     break;
94   }
95   Tag -= Counter::Expression;
96   switch (Tag) {
97   case CounterExpression::Subtract:
98   case CounterExpression::Add: {
99     auto ID = Value >> Counter::EncodingTagBits;
100     if (ID >= Expressions.size())
101       return error(instrprof_error::malformed);
102     Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
103     C = Counter::getExpression(ID);
104     break;
105   }
106   default:
107     return error(instrprof_error::malformed);
108   }
109   return success();
110 }
111
112 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
113   uint64_t EncodedCounter;
114   if (auto Err =
115           readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
116     return Err;
117   if (auto Err = decodeCounter(EncodedCounter, C))
118     return Err;
119   return success();
120 }
121
122 static const unsigned EncodingExpansionRegionBit = 1
123                                                    << Counter::EncodingTagBits;
124
125 /// \brief Read the sub-array of regions for the given inferred file id.
126 /// \param NumFileIDs the number of file ids that are defined for this
127 /// function.
128 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
129     std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
130     size_t NumFileIDs) {
131   uint64_t NumRegions;
132   if (auto Err = readSize(NumRegions))
133     return Err;
134   unsigned LineStart = 0;
135   for (size_t I = 0; I < NumRegions; ++I) {
136     Counter C;
137     CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion;
138
139     // Read the combined counter + region kind.
140     uint64_t EncodedCounterAndRegion;
141     if (auto Err = readIntMax(EncodedCounterAndRegion,
142                               std::numeric_limits<unsigned>::max()))
143       return Err;
144     unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
145     uint64_t ExpandedFileID = 0;
146     if (Tag != Counter::Zero) {
147       if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
148         return Err;
149     } else {
150       // Is it an expansion region?
151       if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
152         Kind = CounterMappingRegion::ExpansionRegion;
153         ExpandedFileID = EncodedCounterAndRegion >>
154                          Counter::EncodingCounterTagAndExpansionRegionTagBits;
155         if (ExpandedFileID >= NumFileIDs)
156           return error(instrprof_error::malformed);
157       } else {
158         switch (EncodedCounterAndRegion >>
159                 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
160         case CounterMappingRegion::CodeRegion:
161           // Don't do anything when we have a code region with a zero counter.
162           break;
163         case CounterMappingRegion::SkippedRegion:
164           Kind = CounterMappingRegion::SkippedRegion;
165           break;
166         default:
167           return error(instrprof_error::malformed);
168         }
169       }
170     }
171
172     // Read the source range.
173     uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
174     if (auto Err =
175             readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
176       return Err;
177     if (auto Err =
178             readIntMax(ColumnStart, std::numeric_limits<unsigned>::max()))
179       return Err;
180     if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
181       return Err;
182     if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
183       return Err;
184     LineStart += LineStartDelta;
185     // Adjust the column locations for the empty regions that are supposed to
186     // cover whole lines. Those regions should be encoded with the
187     // column range (1 -> std::numeric_limits<unsigned>::max()), but because
188     // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
189     // we set the column range to (0 -> 0) to ensure that the column start and
190     // column end take up one byte each.
191     // The std::numeric_limits<unsigned>::max() is used to represent a column
192     // position at the end of the line without knowing the length of that line.
193     if (ColumnStart == 0 && ColumnEnd == 0) {
194       ColumnStart = 1;
195       ColumnEnd = std::numeric_limits<unsigned>::max();
196     }
197     MappingRegions.push_back(
198         CounterMappingRegion(C, InferredFileID, LineStart, ColumnStart,
199                              LineStart + NumLines, ColumnEnd, Kind));
200     MappingRegions.back().ExpandedFileID = ExpandedFileID;
201   }
202   return success();
203 }
204
205 std::error_code RawCoverageMappingReader::read(CoverageMappingRecord &Record) {
206
207   // Read the virtual file mapping.
208   llvm::SmallVector<unsigned, 8> VirtualFileMapping;
209   uint64_t NumFileMappings;
210   if (auto Err = readSize(NumFileMappings))
211     return Err;
212   for (size_t I = 0; I < NumFileMappings; ++I) {
213     uint64_t FilenameIndex;
214     if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
215       return Err;
216     VirtualFileMapping.push_back(FilenameIndex);
217   }
218
219   // Construct the files using unique filenames and virtual file mapping.
220   for (auto I : VirtualFileMapping) {
221     Filenames.push_back(TranslationUnitFilenames[I]);
222   }
223
224   // Read the expressions.
225   uint64_t NumExpressions;
226   if (auto Err = readSize(NumExpressions))
227     return Err;
228   // Create an array of dummy expressions that get the proper counters
229   // when the expressions are read, and the proper kinds when the counters
230   // are decoded.
231   Expressions.resize(
232       NumExpressions,
233       CounterExpression(CounterExpression::Subtract, Counter(), Counter()));
234   for (size_t I = 0; I < NumExpressions; ++I) {
235     if (auto Err = readCounter(Expressions[I].LHS))
236       return Err;
237     if (auto Err = readCounter(Expressions[I].RHS))
238       return Err;
239   }
240
241   // Read the mapping regions sub-arrays.
242   for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
243        InferredFileID < S; ++InferredFileID) {
244     if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
245                                               VirtualFileMapping.size()))
246       return Err;
247   }
248
249   // Set the counters for the expansion regions.
250   // i.e. Counter of expansion region = counter of the first region
251   // from the expanded file.
252   // Perform multiple passes to correctly propagate the counters through
253   // all the nested expansion regions.
254   SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
255   FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
256   for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
257     for (auto &R : MappingRegions) {
258       if (R.Kind != CounterMappingRegion::ExpansionRegion)
259         continue;
260       assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
261       FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
262     }
263     for (auto &R : MappingRegions) {
264       if (FileIDExpansionRegionMapping[R.FileID]) {
265         FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
266         FileIDExpansionRegionMapping[R.FileID] = nullptr;
267       }
268     }
269   }
270
271   Record.FunctionName = FunctionName;
272   Record.Filenames = Filenames;
273   Record.Expressions = Expressions;
274   Record.MappingRegions = MappingRegions;
275   return success();
276 }
277
278 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
279     StringRef FileName)
280     : CurrentRecord(0) {
281   auto File = llvm::object::ObjectFile::createObjectFile(FileName);
282   if (!File)
283     error(File.getError());
284   else
285     Object.reset(File.get());
286 }
287
288 ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
289     std::unique_ptr<MemoryBuffer> &ObjectBuffer, sys::fs::file_magic Type)
290     : CurrentRecord(0) {
291   auto File = llvm::object::ObjectFile::createObjectFile(ObjectBuffer, Type);
292   if (!File)
293     error(File.getError());
294   else
295     Object.reset(File.get());
296 }
297
298 namespace {
299 /// \brief The coverage mapping data for a single function.
300 /// It points to the function's name.
301 template <typename IntPtrT> struct CoverageMappingFunctionRecord {
302   IntPtrT FunctionNamePtr;
303   uint32_t FunctionNameSize;
304   uint32_t CoverageMappingSize;
305 };
306
307 /// \brief The coverage mapping data for a single translation unit.
308 /// It points to the array of function coverage mapping records and the encoded
309 /// filenames array.
310 template <typename IntPtrT> struct CoverageMappingTURecord {
311   uint32_t FunctionRecordsSize;
312   uint32_t FilenamesSize;
313   uint32_t CoverageMappingsSize;
314   uint32_t Version;
315 };
316
317 /// \brief A helper structure to access the data from a section
318 /// in an object file.
319 struct SectionData {
320   StringRef Data;
321   uint64_t Address;
322
323   std::error_code load(SectionRef &Section) {
324     if (auto Err = Section.getContents(Data))
325       return Err;
326     return Section.getAddress(Address);
327   }
328
329   std::error_code get(uint64_t Pointer, size_t Size, StringRef &Result) {
330     if (Pointer < Address)
331       return instrprof_error::malformed;
332     auto Offset = Pointer - Address;
333     if (Offset + Size > Data.size())
334       return instrprof_error::malformed;
335     Result = Data.substr(Pointer - Address, Size);
336     return instrprof_error::success;
337   }
338 };
339 }
340
341 template <typename T>
342 std::error_code readCoverageMappingData(
343     SectionRef &ProfileNames, SectionRef &CoverageMapping,
344     std::vector<ObjectFileCoverageMappingReader::ProfileMappingRecord> &Records,
345     std::vector<StringRef> &Filenames) {
346   llvm::DenseSet<T> UniqueFunctionMappingData;
347
348   // Get the contents of the given sections.
349   StringRef Data;
350   if (auto Err = CoverageMapping.getContents(Data))
351     return Err;
352   SectionData ProfileNamesData;
353   if (auto Err = ProfileNamesData.load(ProfileNames))
354     return Err;
355
356   // Read the records in the coverage data section.
357   while (!Data.empty()) {
358     if (Data.size() < sizeof(CoverageMappingTURecord<T>))
359       return instrprof_error::malformed;
360     auto TU = reinterpret_cast<const CoverageMappingTURecord<T> *>(Data.data());
361     Data = Data.substr(sizeof(CoverageMappingTURecord<T>));
362     switch (TU->Version) {
363     case CoverageMappingVersion1:
364       break;
365     default:
366       return instrprof_error::unsupported_version;
367     }
368     auto Version = CoverageMappingVersion(TU->Version);
369
370     // Get the function records.
371     auto FunctionRecords =
372         reinterpret_cast<const CoverageMappingFunctionRecord<T> *>(Data.data());
373     if (Data.size() <
374         sizeof(CoverageMappingFunctionRecord<T>) * TU->FunctionRecordsSize)
375       return instrprof_error::malformed;
376     Data = Data.substr(sizeof(CoverageMappingFunctionRecord<T>) *
377                        TU->FunctionRecordsSize);
378
379     // Get the filenames.
380     if (Data.size() < TU->FilenamesSize)
381       return instrprof_error::malformed;
382     auto RawFilenames = Data.substr(0, TU->FilenamesSize);
383     Data = Data.substr(TU->FilenamesSize);
384     size_t FilenamesBegin = Filenames.size();
385     RawCoverageFilenamesReader Reader(RawFilenames, Filenames);
386     if (auto Err = Reader.read())
387       return Err;
388
389     // Get the coverage mappings.
390     if (Data.size() < TU->CoverageMappingsSize)
391       return instrprof_error::malformed;
392     auto CoverageMappings = Data.substr(0, TU->CoverageMappingsSize);
393     Data = Data.substr(TU->CoverageMappingsSize);
394
395     for (unsigned I = 0; I < TU->FunctionRecordsSize; ++I) {
396       auto &MappingRecord = FunctionRecords[I];
397
398       // Get the coverage mapping.
399       if (CoverageMappings.size() < MappingRecord.CoverageMappingSize)
400         return instrprof_error::malformed;
401       auto Mapping =
402           CoverageMappings.substr(0, MappingRecord.CoverageMappingSize);
403       CoverageMappings =
404           CoverageMappings.substr(MappingRecord.CoverageMappingSize);
405
406       // Ignore this record if we already have a record that points to the same
407       // function name.
408       // This is useful to ignore the redundant records for the functions
409       // with ODR linkage.
410       if (UniqueFunctionMappingData.count(MappingRecord.FunctionNamePtr))
411         continue;
412       UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr);
413       StringRef FunctionName;
414       if (auto Err = ProfileNamesData.get(MappingRecord.FunctionNamePtr,
415                                           MappingRecord.FunctionNameSize,
416                                           FunctionName))
417         return Err;
418       Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord(
419           Version, FunctionName, Mapping, FilenamesBegin,
420           Filenames.size() - FilenamesBegin));
421     }
422   }
423
424   return instrprof_error::success;
425 }
426
427 std::error_code ObjectFileCoverageMappingReader::readHeader() {
428   if (!Object)
429     return getError();
430   auto BytesInAddress = Object->getBytesInAddress();
431   if (BytesInAddress != 4 && BytesInAddress != 8)
432     return error(instrprof_error::malformed);
433
434   // Look for the sections that we are interested in.
435   int FoundSectionCount = 0;
436   SectionRef ProfileNames, CoverageMapping;
437   for (const auto &Section : Object->sections()) {
438     StringRef Name;
439     if (auto Err = Section.getName(Name))
440       return Err;
441     if (Name == "__llvm_prf_names") {
442       ProfileNames = Section;
443     } else if (Name == "__llvm_covmap") {
444       CoverageMapping = Section;
445     } else
446       continue;
447     ++FoundSectionCount;
448   }
449   if (FoundSectionCount != 2)
450     return error(instrprof_error::bad_header);
451
452   // Load the data from the found sections.
453   std::error_code Err;
454   if (BytesInAddress == 4)
455     Err = readCoverageMappingData<uint32_t>(ProfileNames, CoverageMapping,
456                                             MappingRecords, Filenames);
457   else
458     Err = readCoverageMappingData<uint64_t>(ProfileNames, CoverageMapping,
459                                             MappingRecords, Filenames);
460   if (Err)
461     return error(Err);
462
463   return success();
464 }
465
466 std::error_code
467 ObjectFileCoverageMappingReader::readNextRecord(CoverageMappingRecord &Record) {
468   if (CurrentRecord >= MappingRecords.size())
469     return error(instrprof_error::eof);
470
471   FunctionsFilenames.clear();
472   Expressions.clear();
473   MappingRegions.clear();
474   auto &R = MappingRecords[CurrentRecord];
475   RawCoverageMappingReader Reader(
476       R.FunctionName, R.CoverageMapping,
477       makeArrayRef(Filenames.data() + R.FilenamesBegin, R.FilenamesSize),
478       FunctionsFilenames, Expressions, MappingRegions);
479   if (auto Err = Reader.read(Record))
480     return Err;
481   ++CurrentRecord;
482   return success();
483 }