LLVM  14.0.0git
CoverageMappingReader.cpp
Go to the documentation of this file.
1 //===- CoverageMappingReader.cpp - Code coverage mapping reader -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for reading coverage mapping data for
10 // instrumentation based coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/Error.h"
26 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/Error.h"
34 #include "llvm/Support/LEB128.h"
36 #include "llvm/Support/Path.h"
38 #include <vector>
39 
40 using namespace llvm;
41 using namespace coverage;
42 using namespace object;
43 
44 #define DEBUG_TYPE "coverage-mapping"
45 
46 STATISTIC(CovMapNumRecords, "The # of coverage function records");
47 STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records");
48 
49 void CoverageMappingIterator::increment() {
50  if (ReadErr != coveragemap_error::success)
51  return;
52 
53  // Check if all the records were read or if an error occurred while reading
54  // the next record.
55  if (auto E = Reader->readNextRecord(Record))
56  handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
57  if (CME.get() == coveragemap_error::eof)
58  *this = CoverageMappingIterator();
59  else
60  ReadErr = CME.get();
61  });
62 }
63 
65  if (Data.empty())
66  return make_error<CoverageMapError>(coveragemap_error::truncated);
67  unsigned N = 0;
68  Result = decodeULEB128(Data.bytes_begin(), &N);
69  if (N > Data.size())
70  return make_error<CoverageMapError>(coveragemap_error::malformed);
71  Data = Data.substr(N);
72  return Error::success();
73 }
74 
76  if (auto Err = readULEB128(Result))
77  return Err;
78  if (Result >= MaxPlus1)
79  return make_error<CoverageMapError>(coveragemap_error::malformed);
80  return Error::success();
81 }
82 
84  if (auto Err = readULEB128(Result))
85  return Err;
86  if (Result > Data.size())
87  return make_error<CoverageMapError>(coveragemap_error::malformed);
88  return Error::success();
89 }
90 
92  uint64_t Length;
93  if (auto Err = readSize(Length))
94  return Err;
95  Result = Data.substr(0, Length);
96  Data = Data.substr(Length);
97  return Error::success();
98 }
99 
101  uint64_t NumFilenames;
102  if (auto Err = readSize(NumFilenames))
103  return Err;
104  if (!NumFilenames)
105  return make_error<CoverageMapError>(coveragemap_error::malformed);
106 
108  return readUncompressed(Version, NumFilenames);
109 
110  // The uncompressed length may exceed the size of the encoded filenames.
111  // Skip size validation.
112  uint64_t UncompressedLen;
113  if (auto Err = readULEB128(UncompressedLen))
114  return Err;
115 
116  uint64_t CompressedLen;
117  if (auto Err = readSize(CompressedLen))
118  return Err;
119 
120  if (CompressedLen > 0) {
121  if (!zlib::isAvailable())
122  return make_error<CoverageMapError>(
124 
125  // Allocate memory for the decompressed filenames.
126  SmallVector<char, 0> StorageBuf;
127 
128  // Read compressed filenames.
129  StringRef CompressedFilenames = Data.substr(0, CompressedLen);
130  Data = Data.substr(CompressedLen);
131  auto Err =
132  zlib::uncompress(CompressedFilenames, StorageBuf, UncompressedLen);
133  if (Err) {
134  consumeError(std::move(Err));
135  return make_error<CoverageMapError>(
137  }
138 
139  StringRef UncompressedFilenames(StorageBuf.data(), StorageBuf.size());
140  RawCoverageFilenamesReader Delegate(UncompressedFilenames, Filenames,
141  CompilationDir);
142  return Delegate.readUncompressed(Version, NumFilenames);
143  }
144 
145  return readUncompressed(Version, NumFilenames);
146 }
147 
148 Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version,
149  uint64_t NumFilenames) {
150  // Read uncompressed filenames.
152  for (size_t I = 0; I < NumFilenames; ++I) {
153  StringRef Filename;
154  if (auto Err = readString(Filename))
155  return Err;
156  Filenames.push_back(Filename.str());
157  }
158  } else {
159  StringRef CWD;
160  if (auto Err = readString(CWD))
161  return Err;
162  Filenames.push_back(CWD.str());
163 
164  for (size_t I = 1; I < NumFilenames; ++I) {
165  StringRef Filename;
166  if (auto Err = readString(Filename))
167  return Err;
168  if (sys::path::is_absolute(Filename)) {
169  Filenames.push_back(Filename.str());
170  } else {
172  if (!CompilationDir.empty())
173  P.assign(CompilationDir);
174  else
175  P.assign(CWD);
176  llvm::sys::path::append(P, Filename);
177  Filenames.push_back(static_cast<std::string>(P));
178  }
179  }
180  }
181  return Error::success();
182 }
183 
184 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
186  switch (Tag) {
187  case Counter::Zero:
188  C = Counter::getZero();
189  return Error::success();
192  return Error::success();
193  default:
194  break;
195  }
197  switch (Tag) {
199  case CounterExpression::Add: {
201  if (ID >= Expressions.size())
202  return make_error<CoverageMapError>(coveragemap_error::malformed);
205  break;
206  }
207  default:
208  return make_error<CoverageMapError>(coveragemap_error::malformed);
209  }
210  return Error::success();
211 }
212 
213 Error RawCoverageMappingReader::readCounter(Counter &C) {
214  uint64_t EncodedCounter;
215  if (auto Err =
216  readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
217  return Err;
218  if (auto Err = decodeCounter(EncodedCounter, C))
219  return Err;
220  return Error::success();
221 }
222 
223 static const unsigned EncodingExpansionRegionBit = 1
225 
226 /// Read the sub-array of regions for the given inferred file id.
227 /// \param NumFileIDs the number of file ids that are defined for this
228 /// function.
229 Error RawCoverageMappingReader::readMappingRegionsSubArray(
230  std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
231  size_t NumFileIDs) {
232  uint64_t NumRegions;
233  if (auto Err = readSize(NumRegions))
234  return Err;
235  unsigned LineStart = 0;
236  for (size_t I = 0; I < NumRegions; ++I) {
237  Counter C, C2;
239 
240  // Read the combined counter + region kind.
241  uint64_t EncodedCounterAndRegion;
242  if (auto Err = readIntMax(EncodedCounterAndRegion,
244  return Err;
245  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
246  uint64_t ExpandedFileID = 0;
247 
248  // If Tag does not represent a ZeroCounter, then it is understood to refer
249  // to a counter or counter expression with region kind assumed to be
250  // "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the
251  // referenced counter or counter expression (and nothing else).
252  //
253  // If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set,
254  // then EncodedCounterAndRegion is interpreted to represent an
255  // ExpansionRegion. In all other cases, EncodedCounterAndRegion is
256  // interpreted to refer to a specific region kind, after which additional
257  // fields may be read (e.g. BranchRegions have two encoded counters that
258  // follow an encoded region kind value).
259  if (Tag != Counter::Zero) {
260  if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
261  return Err;
262  } else {
263  // Is it an expansion region?
264  if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
266  ExpandedFileID = EncodedCounterAndRegion >>
268  if (ExpandedFileID >= NumFileIDs)
269  return make_error<CoverageMapError>(coveragemap_error::malformed);
270  } else {
271  switch (EncodedCounterAndRegion >>
274  // Don't do anything when we have a code region with a zero counter.
275  break;
278  break;
280  // For a Branch Region, read two successive counters.
282  if (auto Err = readCounter(C))
283  return Err;
284  if (auto Err = readCounter(C2))
285  return Err;
286  break;
287  default:
288  return make_error<CoverageMapError>(coveragemap_error::malformed);
289  }
290  }
291  }
292 
293  // Read the source range.
294  uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
295  if (auto Err =
296  readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
297  return Err;
298  if (auto Err = readULEB128(ColumnStart))
299  return Err;
300  if (ColumnStart > std::numeric_limits<unsigned>::max())
301  return make_error<CoverageMapError>(coveragemap_error::malformed);
302  if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
303  return Err;
304  if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
305  return Err;
306  LineStart += LineStartDelta;
307 
308  // If the high bit of ColumnEnd is set, this is a gap region.
309  if (ColumnEnd & (1U << 31)) {
311  ColumnEnd &= ~(1U << 31);
312  }
313 
314  // Adjust the column locations for the empty regions that are supposed to
315  // cover whole lines. Those regions should be encoded with the
316  // column range (1 -> std::numeric_limits<unsigned>::max()), but because
317  // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
318  // we set the column range to (0 -> 0) to ensure that the column start and
319  // column end take up one byte each.
320  // The std::numeric_limits<unsigned>::max() is used to represent a column
321  // position at the end of the line without knowing the length of that line.
322  if (ColumnStart == 0 && ColumnEnd == 0) {
323  ColumnStart = 1;
325  }
326 
327  LLVM_DEBUG({
328  dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
329  << ColumnStart << " -> " << (LineStart + NumLines) << ":"
330  << ColumnEnd << ", ";
332  dbgs() << "Expands to file " << ExpandedFileID;
333  else
335  dbgs() << "\n";
336  });
337 
338  auto CMR = CounterMappingRegion(C, C2, InferredFileID, ExpandedFileID,
339  LineStart, ColumnStart,
340  LineStart + NumLines, ColumnEnd, Kind);
341  if (CMR.startLoc() > CMR.endLoc())
342  return make_error<CoverageMapError>(coveragemap_error::malformed);
343  MappingRegions.push_back(CMR);
344  }
345  return Error::success();
346 }
347 
349  // Read the virtual file mapping.
350  SmallVector<unsigned, 8> VirtualFileMapping;
351  uint64_t NumFileMappings;
352  if (auto Err = readSize(NumFileMappings))
353  return Err;
354  for (size_t I = 0; I < NumFileMappings; ++I) {
355  uint64_t FilenameIndex;
356  if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
357  return Err;
358  VirtualFileMapping.push_back(FilenameIndex);
359  }
360 
361  // Construct the files using unique filenames and virtual file mapping.
362  for (auto I : VirtualFileMapping) {
363  Filenames.push_back(TranslationUnitFilenames[I]);
364  }
365 
366  // Read the expressions.
367  uint64_t NumExpressions;
368  if (auto Err = readSize(NumExpressions))
369  return Err;
370  // Create an array of dummy expressions that get the proper counters
371  // when the expressions are read, and the proper kinds when the counters
372  // are decoded.
373  Expressions.resize(
374  NumExpressions,
376  for (size_t I = 0; I < NumExpressions; ++I) {
377  if (auto Err = readCounter(Expressions[I].LHS))
378  return Err;
379  if (auto Err = readCounter(Expressions[I].RHS))
380  return Err;
381  }
382 
383  // Read the mapping regions sub-arrays.
384  for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
385  InferredFileID < S; ++InferredFileID) {
386  if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
387  VirtualFileMapping.size()))
388  return Err;
389  }
390 
391  // Set the counters for the expansion regions.
392  // i.e. Counter of expansion region = counter of the first region
393  // from the expanded file.
394  // Perform multiple passes to correctly propagate the counters through
395  // all the nested expansion regions.
396  SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
397  FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
398  for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
399  for (auto &R : MappingRegions) {
401  continue;
402  assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
403  FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
404  }
405  for (auto &R : MappingRegions) {
406  if (FileIDExpansionRegionMapping[R.FileID]) {
407  FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
408  FileIDExpansionRegionMapping[R.FileID] = nullptr;
409  }
410  }
411  }
412 
413  return Error::success();
414 }
415 
417  // A dummy coverage mapping data consists of just one region with zero count.
418  uint64_t NumFileMappings;
419  if (Error Err = readSize(NumFileMappings))
420  return std::move(Err);
421  if (NumFileMappings != 1)
422  return false;
423  // We don't expect any specific value for the filename index, just skip it.
424  uint64_t FilenameIndex;
425  if (Error Err =
426  readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
427  return std::move(Err);
428  uint64_t NumExpressions;
429  if (Error Err = readSize(NumExpressions))
430  return std::move(Err);
431  if (NumExpressions != 0)
432  return false;
433  uint64_t NumRegions;
434  if (Error Err = readSize(NumRegions))
435  return std::move(Err);
436  if (NumRegions != 1)
437  return false;
438  uint64_t EncodedCounterAndRegion;
439  if (Error Err = readIntMax(EncodedCounterAndRegion,
441  return std::move(Err);
442  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
443  return Tag == Counter::Zero;
444 }
445 
447  Expected<StringRef> DataOrErr = Section.getContents();
448  if (!DataOrErr)
449  return DataOrErr.takeError();
450  Data = *DataOrErr;
451  Address = Section.getAddress();
452 
453  // If this is a linked PE/COFF file, then we have to skip over the null byte
454  // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
455  const ObjectFile *Obj = Section.getObject();
456  if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
457  Data = Data.drop_front(1);
458 
459  return Error::success();
460 }
461 
463  if (Pointer < Address)
464  return StringRef();
465  auto Offset = Pointer - Address;
466  if (Offset + Size > Data.size())
467  return StringRef();
468  return Data.substr(Pointer - Address, Size);
469 }
470 
471 // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
473  // The hash value of dummy mapping records is always zero.
474  if (Hash)
475  return false;
476  return RawCoverageMappingDummyChecker(Mapping).isDummy();
477 }
478 
479 /// A range of filename indices. Used to specify the location of a batch of
480 /// filenames in a vector-like container.
482  unsigned StartingIndex;
483  unsigned Length;
484 
485  FilenameRange(unsigned StartingIndex, unsigned Length)
486  : StartingIndex(StartingIndex), Length(Length) {}
487 
488  void markInvalid() { Length = 0; }
489  bool isInvalid() const { return Length == 0; }
490 };
491 
492 namespace {
493 
494 /// The interface to read coverage mapping function records for a module.
495 struct CovMapFuncRecordReader {
496  virtual ~CovMapFuncRecordReader() = default;
497 
498  // Read a coverage header.
499  //
500  // \p CovBuf points to the buffer containing the \c CovHeader of the coverage
501  // mapping data associated with the module.
502  //
503  // Returns a pointer to the next \c CovHeader if it exists, or to an address
504  // greater than \p CovEnd if not.
505  virtual Expected<const char *> readCoverageHeader(const char *CovBuf,
506  const char *CovBufEnd) = 0;
507 
508  // Read function records.
509  //
510  // \p FuncRecBuf points to the buffer containing a batch of function records.
511  // \p FuncRecBufEnd points past the end of the batch of records.
512  //
513  // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames
514  // associated with the function records. It is unused in Version4.
515  //
516  // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage
517  // mappings associated with the function records. It is unused in Version4.
518  virtual Error readFunctionRecords(const char *FuncRecBuf,
519  const char *FuncRecBufEnd,
520  Optional<FilenameRange> OutOfLineFileRange,
521  const char *OutOfLineMappingBuf,
522  const char *OutOfLineMappingBufEnd) = 0;
523 
524  template <class IntPtrT, support::endianness Endian>
527  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
528  std::vector<std::string> &F);
529 };
530 
531 // A class for reading coverage mapping function records for a module.
532 template <CovMapVersion Version, class IntPtrT, support::endianness Endian>
533 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
534  using FuncRecordType =
536  using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
537 
538  // Maps function's name references to the indexes of their records
539  // in \c Records.
540  DenseMap<NameRefType, size_t> FunctionRecords;
541  InstrProfSymtab &ProfileNames;
542  StringRef CompilationDir;
543  std::vector<std::string> &Filenames;
544  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
545 
546  // Maps a hash of the filenames in a TU to a \c FileRange. The range
547  // specifies the location of the hashed filenames in \c Filenames.
549 
550  // Add the record to the collection if we don't already have a record that
551  // points to the same function name. This is useful to ignore the redundant
552  // records for the functions with ODR linkage.
553  // In addition, prefer records with real coverage mapping data to dummy
554  // records, which were emitted for inline functions which were seen but
555  // not used in the corresponding translation unit.
556  Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
557  StringRef Mapping,
558  FilenameRange FileRange) {
559  ++CovMapNumRecords;
560  uint64_t FuncHash = CFR->template getFuncHash<Endian>();
561  NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
562  auto InsertResult =
563  FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
564  if (InsertResult.second) {
565  StringRef FuncName;
566  if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
567  return Err;
568  if (FuncName.empty())
569  return make_error<InstrProfError>(instrprof_error::malformed,
570  "function name is empty");
571  ++CovMapNumUsedRecords;
572  Records.emplace_back(Version, FuncName, FuncHash, Mapping,
573  FileRange.StartingIndex, FileRange.Length);
574  return Error::success();
575  }
576  // Update the existing record if it's a dummy and the new record is real.
577  size_t OldRecordIndex = InsertResult.first->second;
579  Records[OldRecordIndex];
580  Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
581  OldRecord.FunctionHash, OldRecord.CoverageMapping);
582  if (Error Err = OldIsDummyExpected.takeError())
583  return Err;
584  if (!*OldIsDummyExpected)
585  return Error::success();
586  Expected<bool> NewIsDummyExpected =
587  isCoverageMappingDummy(FuncHash, Mapping);
588  if (Error Err = NewIsDummyExpected.takeError())
589  return Err;
590  if (*NewIsDummyExpected)
591  return Error::success();
592  ++CovMapNumUsedRecords;
593  OldRecord.FunctionHash = FuncHash;
594  OldRecord.CoverageMapping = Mapping;
595  OldRecord.FilenamesBegin = FileRange.StartingIndex;
596  OldRecord.FilenamesSize = FileRange.Length;
597  return Error::success();
598  }
599 
600 public:
601  VersionedCovMapFuncRecordReader(
603  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
604  std::vector<std::string> &F)
605  : ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}
606 
607  ~VersionedCovMapFuncRecordReader() override = default;
608 
609  Expected<const char *> readCoverageHeader(const char *CovBuf,
610  const char *CovBufEnd) override {
611  using namespace support;
612 
613  if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
614  return make_error<CoverageMapError>(coveragemap_error::malformed);
615  auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
616  uint32_t NRecords = CovHeader->getNRecords<Endian>();
617  uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
618  uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
619  assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
620  CovBuf = reinterpret_cast<const char *>(CovHeader + 1);
621 
622  // Skip past the function records, saving the start and end for later.
623  // This is a no-op in Version4 (function records are read after all headers
624  // are read).
625  const char *FuncRecBuf = nullptr;
626  const char *FuncRecBufEnd = nullptr;
628  FuncRecBuf = CovBuf;
629  CovBuf += NRecords * sizeof(FuncRecordType);
631  FuncRecBufEnd = CovBuf;
632 
633  // Get the filenames.
634  if (CovBuf + FilenamesSize > CovBufEnd)
635  return make_error<CoverageMapError>(coveragemap_error::malformed);
636  size_t FilenamesBegin = Filenames.size();
637  StringRef FilenameRegion(CovBuf, FilenamesSize);
638  RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
639  CompilationDir);
640  if (auto Err = Reader.read(Version))
641  return std::move(Err);
642  CovBuf += FilenamesSize;
643  FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);
644 
646  // Map a hash of the filenames region to the filename range associated
647  // with this coverage header.
648  int64_t FilenamesRef =
649  llvm::IndexedInstrProf::ComputeHash(FilenameRegion);
650  auto Insert =
651  FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));
652  if (!Insert.second) {
653  // The same filenames ref was encountered twice. It's possible that
654  // the associated filenames are the same.
655  auto It = Filenames.begin();
656  FilenameRange &OrigRange = Insert.first->getSecond();
657  if (std::equal(It + OrigRange.StartingIndex,
658  It + OrigRange.StartingIndex + OrigRange.Length,
659  It + FileRange.StartingIndex,
660  It + FileRange.StartingIndex + FileRange.Length))
661  // Map the new range to the original one.
662  FileRange = OrigRange;
663  else
664  // This is a hash collision. Mark the filenames ref invalid.
665  OrigRange.markInvalid();
666  }
667  }
668 
669  // We'll read the coverage mapping records in the loop below.
670  // This is a no-op in Version4 (coverage mappings are not affixed to the
671  // coverage header).
672  const char *MappingBuf = CovBuf;
673  if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
674  return make_error<CoverageMapError>(coveragemap_error::malformed);
675  CovBuf += CoverageSize;
676  const char *MappingEnd = CovBuf;
677 
678  if (CovBuf > CovBufEnd)
679  return make_error<CoverageMapError>(coveragemap_error::malformed);
680 
682  // Read each function record.
683  if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,
684  MappingBuf, MappingEnd))
685  return std::move(E);
686  }
687 
688  // Each coverage map has an alignment of 8, so we need to adjust alignment
689  // before reading the next map.
690  CovBuf += offsetToAlignedAddr(CovBuf, Align(8));
691 
692  return CovBuf;
693  }
694 
695  Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
696  Optional<FilenameRange> OutOfLineFileRange,
697  const char *OutOfLineMappingBuf,
698  const char *OutOfLineMappingBufEnd) override {
699  auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);
700  while ((const char *)CFR < FuncRecBufEnd) {
701  // Validate the length of the coverage mapping for this function.
702  const char *NextMappingBuf;
703  const FuncRecordType *NextCFR;
704  std::tie(NextMappingBuf, NextCFR) =
705  CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
707  if (NextMappingBuf > OutOfLineMappingBufEnd)
708  return make_error<CoverageMapError>(coveragemap_error::malformed);
709 
710  // Look up the set of filenames associated with this function record.
711  Optional<FilenameRange> FileRange;
713  FileRange = OutOfLineFileRange;
714  } else {
715  uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
716  auto It = FileRangeMap.find(FilenamesRef);
717  if (It == FileRangeMap.end())
718  return make_error<CoverageMapError>(coveragemap_error::malformed);
719  else
720  FileRange = It->getSecond();
721  }
722 
723  // Now, read the coverage data.
724  if (FileRange && !FileRange->isInvalid()) {
725  StringRef Mapping =
726  CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
728  Mapping.data() + Mapping.size() > FuncRecBufEnd)
729  return make_error<CoverageMapError>(coveragemap_error::malformed);
730  if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
731  return Err;
732  }
733 
734  std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);
735  }
736  return Error::success();
737  }
738 };
739 
740 } // end anonymous namespace
741 
742 template <class IntPtrT, support::endianness Endian>
745  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
746  std::vector<std::string> &F) {
747  using namespace coverage;
748 
749  switch (Version) {
751  return std::make_unique<VersionedCovMapFuncRecordReader<
752  CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);
758  // Decompress the name data.
759  if (Error E = P.create(P.getNameData()))
760  return std::move(E);
762  return std::make_unique<VersionedCovMapFuncRecordReader<
763  CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);
764  else if (Version == CovMapVersion::Version3)
765  return std::make_unique<VersionedCovMapFuncRecordReader<
766  CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);
767  else if (Version == CovMapVersion::Version4)
768  return std::make_unique<VersionedCovMapFuncRecordReader<
769  CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);
770  else if (Version == CovMapVersion::Version5)
771  return std::make_unique<VersionedCovMapFuncRecordReader<
772  CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);
773  else if (Version == CovMapVersion::Version6)
774  return std::make_unique<VersionedCovMapFuncRecordReader<
775  CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
776  }
777  llvm_unreachable("Unsupported version");
778 }
779 
780 template <typename T, support::endianness Endian>
782  InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
783  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
784  StringRef CompilationDir, std::vector<std::string> &Filenames) {
785  using namespace coverage;
786 
787  // Read the records in the coverage data section.
788  auto CovHeader =
789  reinterpret_cast<const CovMapHeader *>(CovMap.data());
790  CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
792  return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
794  CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
795  CompilationDir, Filenames);
796  if (Error E = ReaderExpected.takeError())
797  return E;
798  auto Reader = std::move(ReaderExpected.get());
799  const char *CovBuf = CovMap.data();
800  const char *CovBufEnd = CovBuf + CovMap.size();
801  const char *FuncRecBuf = FuncRecords.data();
802  const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();
803  while (CovBuf < CovBufEnd) {
804  // Read the current coverage header & filename data.
805  //
806  // Prior to Version4, this also reads all function records affixed to the
807  // header.
808  //
809  // Return a pointer to the next coverage header.
810  auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);
811  if (auto E = NextOrErr.takeError())
812  return E;
813  CovBuf = NextOrErr.get();
814  }
815  // In Version4, function records are not affixed to coverage headers. Read
816  // the records from their dedicated section.
818  return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, None, nullptr,
819  nullptr);
820  return Error::success();
821 }
822 
823 static const char *TestingFormatMagic = "llvmcovmtestdata";
824 
827  StringRef Coverage, FuncRecordsStorage &&FuncRecords,
828  InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress,
829  support::endianness Endian, StringRef CompilationDir) {
830  std::unique_ptr<BinaryCoverageReader> Reader(
831  new BinaryCoverageReader(std::move(FuncRecords)));
832  Reader->ProfileNames = std::move(ProfileNames);
833  StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
834  if (BytesInAddress == 4 && Endian == support::endianness::little) {
835  if (Error E =
836  readCoverageMappingData<uint32_t, support::endianness::little>(
837  Reader->ProfileNames, Coverage, FuncRecordsRef,
838  Reader->MappingRecords, CompilationDir, Reader->Filenames))
839  return std::move(E);
840  } else if (BytesInAddress == 4 && Endian == support::endianness::big) {
841  if (Error E = readCoverageMappingData<uint32_t, support::endianness::big>(
842  Reader->ProfileNames, Coverage, FuncRecordsRef,
843  Reader->MappingRecords, CompilationDir, Reader->Filenames))
844  return std::move(E);
845  } else if (BytesInAddress == 8 && Endian == support::endianness::little) {
846  if (Error E =
847  readCoverageMappingData<uint64_t, support::endianness::little>(
848  Reader->ProfileNames, Coverage, FuncRecordsRef,
849  Reader->MappingRecords, CompilationDir, Reader->Filenames))
850  return std::move(E);
851  } else if (BytesInAddress == 8 && Endian == support::endianness::big) {
852  if (Error E = readCoverageMappingData<uint64_t, support::endianness::big>(
853  Reader->ProfileNames, Coverage, FuncRecordsRef,
854  Reader->MappingRecords, CompilationDir, Reader->Filenames))
855  return std::move(E);
856  } else
857  return make_error<CoverageMapError>(coveragemap_error::malformed);
858  return std::move(Reader);
859 }
860 
862 loadTestingFormat(StringRef Data, StringRef CompilationDir) {
863  uint8_t BytesInAddress = 8;
865 
867  if (Data.empty())
868  return make_error<CoverageMapError>(coveragemap_error::truncated);
869  unsigned N = 0;
870  uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
871  if (N > Data.size())
872  return make_error<CoverageMapError>(coveragemap_error::malformed);
873  Data = Data.substr(N);
874  if (Data.empty())
875  return make_error<CoverageMapError>(coveragemap_error::truncated);
876  N = 0;
877  uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
878  if (N > Data.size())
879  return make_error<CoverageMapError>(coveragemap_error::malformed);
880  Data = Data.substr(N);
881  if (Data.size() < ProfileNamesSize)
882  return make_error<CoverageMapError>(coveragemap_error::malformed);
883  InstrProfSymtab ProfileNames;
884  if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
885  return std::move(E);
886  Data = Data.substr(ProfileNamesSize);
887  // Skip the padding bytes because coverage map data has an alignment of 8.
888  size_t Pad = offsetToAlignedAddr(Data.data(), Align(8));
889  if (Data.size() < Pad)
890  return make_error<CoverageMapError>(coveragemap_error::malformed);
891  Data = Data.substr(Pad);
892  if (Data.size() < sizeof(CovMapHeader))
893  return make_error<CoverageMapError>(coveragemap_error::malformed);
894  auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
895  Data.substr(0, sizeof(CovMapHeader)).data());
897  (CovMapVersion)CovHeader->getVersion<support::endianness::little>();
902  if (CoverageMapping.empty())
903  return make_error<CoverageMapError>(coveragemap_error::truncated);
904  CoverageRecords = MemoryBuffer::getMemBuffer("");
905  } else {
906  uint32_t FilenamesSize =
907  CovHeader->getFilenamesSize<support::endianness::little>();
908  uint32_t CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
909  CoverageMapping = Data.substr(0, CoverageMappingSize);
910  if (CoverageMapping.empty())
911  return make_error<CoverageMapError>(coveragemap_error::truncated);
912  Data = Data.substr(CoverageMappingSize);
913  // Skip the padding bytes because coverage records data has an alignment
914  // of 8.
915  Pad = offsetToAlignedAddr(Data.data(), Align(8));
916  if (Data.size() < Pad)
917  return make_error<CoverageMapError>(coveragemap_error::malformed);
918  CoverageRecords = MemoryBuffer::getMemBuffer(Data.substr(Pad));
919  if (CoverageRecords->getBufferSize() == 0)
920  return make_error<CoverageMapError>(coveragemap_error::truncated);
921  }
923  CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),
924  BytesInAddress, Endian, CompilationDir);
925 }
926 
927 /// Find all sections that match \p Name. There may be more than one if comdats
928 /// are in use, e.g. for the __llvm_covfun section on ELF.
930  StringRef Name) {
931  // On COFF, the object file section name may end in "$M". This tells the
932  // linker to sort these sections between "$A" and "$Z". The linker removes the
933  // dollar and everything after it in the final binary. Do the same to match.
934  bool IsCOFF = isa<COFFObjectFile>(OF);
935  auto stripSuffix = [IsCOFF](StringRef N) {
936  return IsCOFF ? N.split('$').first : N;
937  };
938  Name = stripSuffix(Name);
939 
940  std::vector<SectionRef> Sections;
941  for (const auto &Section : OF.sections()) {
942  Expected<StringRef> NameOrErr = Section.getName();
943  if (!NameOrErr)
944  return NameOrErr.takeError();
945  if (stripSuffix(*NameOrErr) == Name)
946  Sections.push_back(Section);
947  }
948  if (Sections.empty())
949  return make_error<CoverageMapError>(coveragemap_error::no_data_found);
950  return Sections;
951 }
952 
954 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
955  StringRef CompilationDir = "") {
956  std::unique_ptr<ObjectFile> OF;
957  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
958  // If we have a universal binary, try to look up the object for the
959  // appropriate architecture.
960  auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
961  if (!ObjectFileOrErr)
962  return ObjectFileOrErr.takeError();
963  OF = std::move(ObjectFileOrErr.get());
964  } else if (isa<ObjectFile>(Bin.get())) {
965  // For any other object file, upcast and take ownership.
966  OF.reset(cast<ObjectFile>(Bin.release()));
967  // If we've asked for a particular arch, make sure they match.
968  if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
969  return errorCodeToError(object_error::arch_not_found);
970  } else
971  // We can only handle object files.
972  return make_error<CoverageMapError>(coveragemap_error::malformed);
973 
974  // The coverage uses native pointer sizes for the object it's written in.
975  uint8_t BytesInAddress = OF->getBytesInAddress();
976  support::endianness Endian = OF->isLittleEndian()
979 
980  // Look for the sections that we are interested in.
981  auto ObjFormat = OF->getTripleObjectFormat();
982  auto NamesSection =
983  lookupSections(*OF, getInstrProfSectionName(IPSK_name, ObjFormat,
984  /*AddSegmentInfo=*/false));
985  if (auto E = NamesSection.takeError())
986  return std::move(E);
987  auto CoverageSection =
988  lookupSections(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat,
989  /*AddSegmentInfo=*/false));
990  if (auto E = CoverageSection.takeError())
991  return std::move(E);
992  std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
993  if (CoverageSectionRefs.size() != 1)
994  return make_error<CoverageMapError>(coveragemap_error::malformed);
995  auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
996  if (!CoverageMappingOrErr)
997  return CoverageMappingOrErr.takeError();
998  StringRef CoverageMapping = CoverageMappingOrErr.get();
999 
1000  InstrProfSymtab ProfileNames;
1001  std::vector<SectionRef> NamesSectionRefs = *NamesSection;
1002  if (NamesSectionRefs.size() != 1)
1003  return make_error<CoverageMapError>(coveragemap_error::malformed);
1004  if (Error E = ProfileNames.create(NamesSectionRefs.back()))
1005  return std::move(E);
1006 
1007  // Look for the coverage records section (Version4 only).
1008  auto CoverageRecordsSections =
1009  lookupSections(*OF, getInstrProfSectionName(IPSK_covfun, ObjFormat,
1010  /*AddSegmentInfo=*/false));
1011 
1013  if (auto E = CoverageRecordsSections.takeError()) {
1015  FuncRecords = MemoryBuffer::getMemBuffer("");
1016  } else {
1017  // Compute the FuncRecordsBuffer of the buffer, taking into account the
1018  // padding between each record, and making sure the first block is aligned
1019  // in memory to maintain consistency between buffer address and size
1020  // alignment.
1021  const Align RecordAlignment(8);
1022  uint64_t FuncRecordsSize = 0;
1023  for (SectionRef Section : *CoverageRecordsSections) {
1024  auto CoverageRecordsOrErr = Section.getContents();
1025  if (!CoverageRecordsOrErr)
1026  return CoverageRecordsOrErr.takeError();
1027  FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1028  }
1029  auto WritableBuffer =
1031  char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1032  assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1033  "Allocated memory is correctly aligned");
1034 
1035  for (SectionRef Section : *CoverageRecordsSections) {
1036  auto CoverageRecordsOrErr = Section.getContents();
1037  if (!CoverageRecordsOrErr)
1038  return CoverageRecordsOrErr.takeError();
1039  const auto &CoverageRecords = CoverageRecordsOrErr.get();
1040  FuncRecordsBuffer = std::copy(CoverageRecords.begin(),
1041  CoverageRecords.end(), FuncRecordsBuffer);
1042  FuncRecordsBuffer =
1043  std::fill_n(FuncRecordsBuffer,
1044  alignAddr(FuncRecordsBuffer, RecordAlignment) -
1045  (uintptr_t)FuncRecordsBuffer,
1046  '\0');
1047  }
1048  assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1049  "consistent init");
1050  FuncRecords = std::move(WritableBuffer);
1051  }
1052 
1054  CoverageMapping, std::move(FuncRecords), std::move(ProfileNames),
1055  BytesInAddress, Endian, CompilationDir);
1056 }
1057 
1058 /// Determine whether \p Arch is invalid or empty, given \p Bin.
1059 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
1060  // If we have a universal binary and Arch doesn't identify any of its slices,
1061  // it's user error.
1062  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1063  for (auto &ObjForArch : Universal->objects())
1064  if (Arch == ObjForArch.getArchFlagName())
1065  return false;
1066  return true;
1067  }
1068  return false;
1069 }
1070 
1073  MemoryBufferRef ObjectBuffer, StringRef Arch,
1074  SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1075  StringRef CompilationDir) {
1076  std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1077 
1078  if (ObjectBuffer.getBuffer().startswith(TestingFormatMagic)) {
1079  // This is a special format used for testing.
1080  auto ReaderOrErr =
1081  loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1082  if (!ReaderOrErr)
1083  return ReaderOrErr.takeError();
1084  Readers.push_back(std::move(ReaderOrErr.get()));
1085  return std::move(Readers);
1086  }
1087 
1088  auto BinOrErr = createBinary(ObjectBuffer);
1089  if (!BinOrErr)
1090  return BinOrErr.takeError();
1091  std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1092 
1093  if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1094  return make_error<CoverageMapError>(
1096 
1097  // MachO universal binaries which contain archives need to be treated as
1098  // archives, not as regular binaries.
1099  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1100  for (auto &ObjForArch : Universal->objects()) {
1101  // Skip slices within the universal binary which target the wrong arch.
1102  std::string ObjArch = ObjForArch.getArchFlagName();
1103  if (Arch != ObjArch)
1104  continue;
1105 
1106  auto ArchiveOrErr = ObjForArch.getAsArchive();
1107  if (!ArchiveOrErr) {
1108  // If this is not an archive, try treating it as a regular object.
1109  consumeError(ArchiveOrErr.takeError());
1110  break;
1111  }
1112 
1114  ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1115  CompilationDir);
1116  }
1117  }
1118 
1119  // Load coverage out of archive members.
1120  if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1121  Error Err = Error::success();
1122  for (auto &Child : Ar->children(Err)) {
1123  Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1124  if (!ChildBufOrErr)
1125  return ChildBufOrErr.takeError();
1126 
1127  auto ChildReadersOrErr = BinaryCoverageReader::create(
1128  ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir);
1129  if (!ChildReadersOrErr)
1130  return ChildReadersOrErr.takeError();
1131  for (auto &Reader : ChildReadersOrErr.get())
1132  Readers.push_back(std::move(Reader));
1133  }
1134  if (Err)
1135  return std::move(Err);
1136 
1137  // Thin archives reference object files outside of the archive file, i.e.
1138  // files which reside in memory not owned by the caller. Transfer ownership
1139  // to the caller.
1140  if (Ar->isThin())
1141  for (auto &Buffer : Ar->takeThinBuffers())
1142  ObjectFileBuffers.push_back(std::move(Buffer));
1143 
1144  return std::move(Readers);
1145  }
1146 
1147  auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir);
1148  if (!ReaderOrErr)
1149  return ReaderOrErr.takeError();
1150  Readers.push_back(std::move(ReaderOrErr.get()));
1151  return std::move(Readers);
1152 }
1153 
1155  if (CurrentRecord >= MappingRecords.size())
1156  return make_error<CoverageMapError>(coveragemap_error::eof);
1157 
1158  FunctionsFilenames.clear();
1159  Expressions.clear();
1160  MappingRegions.clear();
1161  auto &R = MappingRecords[CurrentRecord];
1162  auto F = makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1163  RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1164  Expressions, MappingRegions);
1165  if (auto Err = Reader.read())
1166  return Err;
1167 
1168  Record.FunctionName = R.FunctionName;
1169  Record.FunctionHash = R.FunctionHash;
1170  Record.Filenames = FunctionsFilenames;
1171  Record.Expressions = Expressions;
1172  Record.MappingRegions = MappingRegions;
1173 
1174  ++CurrentRecord;
1175  return Error::success();
1176 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::coverage::Counter::getExpression
static Counter getExpression(unsigned ExpressionId)
Return the counter that corresponds to a specific addition counter expression.
Definition: CoverageMapping.h:142
llvm::coverage::coveragemap_error::success
@ success
llvm::coverage::CounterExpression
A Counter expression is a value that represents an arithmetic operation with two counters.
Definition: CoverageMapping.h:149
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
Compression.h
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:285
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FilenamesBegin
size_t FilenamesBegin
Definition: CoverageMappingReader.h:171
FilenameRange::Length
unsigned Length
Definition: CoverageMappingReader.cpp:483
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::coverage::Version4
@ Version4
Definition: CoverageMapping.h:1002
TestingFormatMagic
static const char * TestingFormatMagic
Definition: CoverageMappingReader.cpp:823
llvm::coverage::CoverageMapError
Definition: CoverageMapping.h:68
llvm::coverage::CoverageMapError::get
coveragemap_error get() const
Definition: CoverageMapping.h:82
Insert
Vector Rotate Left Mask Mask Insert
Definition: README_P9.txt:112
FilenameRange
A range of filename indices.
Definition: CoverageMappingReader.cpp:481
llvm::coverage::coveragemap_error::no_data_found
@ no_data_found
llvm::coverage::Version3
@ Version3
Definition: CoverageMapping.h:1000
llvm::coverage::CounterExpression::ExprKind
ExprKind
Definition: CoverageMapping.h:150
llvm::coverage::CovMapVersion
CovMapVersion
Definition: CoverageMapping.h:992
llvm::coverage::CounterExpression::Subtract
@ Subtract
Definition: CoverageMapping.h:150
llvm::coverage::CounterMappingRegion
A Counter mapping region associates a source range with a specific counter.
Definition: CoverageMapping.h:208
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::SmallVector< char, 0 >
Statistic.h
llvm::IndexedInstrProf::ComputeHash
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:1003
Path.h
llvm::sys::path::is_absolute
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:672
llvm::coverage::Counter::Expression
@ Expression
Definition: CoverageMapping.h:95
ErrorHandling.h
llvm::InstrProfSymtab::create
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
llvm::zlib::uncompress
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:63
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::coverage::BinaryCoverageReader::readNextRecord
Error readNextRecord(CoverageMappingRecord &Record) override
Definition: CoverageMappingReader.cpp:1154
FilenameRange::isInvalid
bool isInvalid() const
Definition: CoverageMappingReader.cpp:489
Error.h
isArchSpecifierInvalidOrMissing
static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch)
Determine whether Arch is invalid or empty, given Bin.
Definition: CoverageMappingReader.cpp:1059
llvm::coverage::Counter::getCounter
static Counter getCounter(unsigned CounterId)
Return the counter that corresponds to a specific profile counter.
Definition: CoverageMapping.h:136
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::alignAddr
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition: Alignment.h:186
llvm::coverage::Counter::CounterValueReference
@ CounterValueReference
Definition: CoverageMapping.h:95
DenseMap.h
llvm::coverage::BinaryCoverageReader
Reader for the coverage mapping data that is emitted by the frontend and stored in an object file.
Definition: CoverageMappingReader.h:164
readCoverageMappingData
static Error readCoverageMappingData(InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords, std::vector< BinaryCoverageReader::ProfileMappingRecord > &Records, StringRef CompilationDir, std::vector< std::string > &Filenames)
Definition: CoverageMappingReader.cpp:781
llvm::Optional
Definition: APInt.h:33
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::coverage::CounterMappingRegion::ExpansionRegion
@ ExpansionRegion
An ExpansionRegion represents a file expansion region that associates a source range with the expansi...
Definition: CoverageMapping.h:216
llvm::coverage::CounterExpression::Add
@ Add
Definition: CoverageMapping.h:150
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
RHS
Value * RHS
Definition: X86PartialReduction.cpp:74
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::CoverageMapping
StringRef CoverageMapping
Definition: CoverageMappingReader.h:170
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:104
llvm::coverage::coveragemap_error::unsupported_version
@ unsupported_version
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1045
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::coverage::BinaryCoverageReader::createCoverageReaderFromBuffer
static Expected< std::unique_ptr< BinaryCoverageReader > > createCoverageReaderFromBuffer(StringRef Coverage, FuncRecordsStorage &&FuncRecords, InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress, support::endianness Endian, StringRef CompilationDir="")
Definition: CoverageMappingReader.cpp:826
llvm::coverage::coveragemap_error::decompression_failed
@ decompression_failed
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:207
llvm::lltok::equal
@ equal
Definition: LLToken.h:25
llvm::sys::path::append
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:82
readULEB128
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
Definition: WasmObjectFile.cpp:104
LHS
Value * LHS
Definition: X86PartialReduction.cpp:73
llvm::coverage::coveragemap_error::eof
@ eof
llvm::decodeULEB128
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:128
llvm::coverage::Version1
@ Version1
Definition: CoverageMapping.h:993
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord
Definition: CoverageMappingReader.h:166
llvm::coverage::CurrentVersion
@ CurrentVersion
Definition: CoverageMapping.h:1009
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:113
Error.h
llvm::coverage::RawCoverageMappingDummyChecker::isDummy
Expected< bool > isDummy()
Definition: CoverageMappingReader.cpp:416
llvm::coverage::Version5
@ Version5
Definition: CoverageMapping.h:1004
llvm::coverage::coveragemap_error::malformed
@ malformed
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::support::little
@ little
Definition: Endian.h:27
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:90
MachOUniversal.h
llvm::coverage::CoverageMappingIterator
A file format agnostic iterator over coverage mapping data.
Definition: CoverageMappingReader.h:44
InstrProf.h
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::coverage::coveragemap_error::invalid_or_missing_arch_specifier
@ invalid_or_missing_arch_specifier
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:609
llvm::coverage::RawCoverageReader::readSize
Error readSize(uint64_t &Result)
Definition: CoverageMappingReader.cpp:83
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::coverage::CounterMappingContext::dump
void dump(const Counter &C, raw_ostream &OS) const
Definition: CoverageMapping.cpp:135
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:244
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FunctionHash
uint64_t FunctionHash
Definition: CoverageMappingReader.h:169
llvm::None
const NoneType None
Definition: None.h:23
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::coverage::BinaryCoverageReader::create
static Expected< std::vector< std::unique_ptr< BinaryCoverageReader > > > create(MemoryBufferRef ObjectBuffer, StringRef Arch, SmallVectorImpl< std::unique_ptr< MemoryBuffer >> &ObjectFileBuffers, StringRef CompilationDir="")
Definition: CoverageMappingReader.cpp:1072
llvm::SmallString< 256 >
llvm::coverage::coveragemap_error::truncated
@ truncated
llvm::coverage::RawCoverageMappingReader
Reader for the raw coverage mapping data.
Definition: CoverageMappingReader.h:132
CoverageMappingReader.h
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FilenamesSize
size_t FilenamesSize
Definition: CoverageMappingReader.h:172
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:152
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
LEB128.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
llvm::coverage::CovMapFunctionRecordV3
Definition: CoverageMapping.h:914
llvm::coverage::Counter
A Counter is an abstract value that describes how to compute the execution count for a region of code...
Definition: CoverageMapping.h:92
I
#define I(x, y, z)
Definition: MD5.cpp:58
ArrayRef.h
llvm::HighlightColor::Address
@ Address
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::coverage::CovMapHeader
Definition: CoverageMapping.h:970
llvm::WritableMemoryBuffer::getNewUninitMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
Definition: MemoryBuffer.cpp:280
ObjectFile.h
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
llvm::zlib::isAvailable
bool isAvailable()
Definition: Compression.cpp:47
llvm::Record
Definition: Record.h:1485
llvm::coverage::CounterMappingRegion::BranchRegion
@ BranchRegion
A BranchRegion represents leaf-level boolean expressions and is associated with two counters,...
Definition: CoverageMapping.h:229
llvm::coverage::RawCoverageMappingDummyChecker
Checks if the given coverage mapping data is exported for an unused function.
Definition: CoverageMappingReader.h:123
llvm::coverage::CounterMappingRegion::GapRegion
@ GapRegion
A GapRegion is like a CodeRegion, but its count is only set as the line execution count when its the ...
Definition: CoverageMapping.h:224
Triple.h
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1630
llvm::offsetToAlignedAddr
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
Definition: Alignment.h:202
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::coverage::Counter::EncodingCounterTagAndExpansionRegionTagBits
static const unsigned EncodingCounterTagAndExpansionRegionTagBits
Definition: CoverageMapping.h:98
llvm::coverage::RawCoverageMappingReader::read
Error read()
Definition: CoverageMappingReader.cpp:348
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::coverage::CoverageMapping
The mapping of profile information to coverage data.
Definition: CoverageMapping.h:570
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:569
isCoverageMappingDummy
static Expected< bool > isCoverageMappingDummy(uint64_t Hash, StringRef Mapping)
Definition: CoverageMappingReader.cpp:472
uint32_t
EncodingExpansionRegionBit
static const unsigned EncodingExpansionRegionBit
Definition: CoverageMappingReader.cpp:223
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::coverage::RawCoverageReader::readULEB128
Error readULEB128(uint64_t &Result)
Definition: CoverageMappingReader.cpp:64
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
llvm::coverage::RawCoverageFilenamesReader
Reader for the raw coverage filenames.
Definition: CoverageMappingReader.h:222
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:156
llvm::coverage::CounterMappingRegion::RegionKind
RegionKind
Definition: CoverageMapping.h:209
llvm::coverage::RawCoverageReader::readIntMax
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
Definition: CoverageMappingReader.cpp:75
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:421
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:83
FilenameRange::markInvalid
void markInvalid()
Definition: CoverageMappingReader.cpp:488
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::isAddrAligned
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition: Alignment.h:143
Casting.h
FilenameRange::FilenameRange
FilenameRange(unsigned StartingIndex, unsigned Length)
Definition: CoverageMappingReader.cpp:485
llvm::coverage::CounterMappingContext
A Counter mapping context is used to connect the counters, expressions and the obtained counter value...
Definition: CoverageMapping.h:320
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:474
llvm::InstrProfSymtab::getFuncName
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize)
Return function's PGO name from the function name's symbol address in the object file.
Definition: CoverageMappingReader.cpp:462
llvm::getInstrProfSectionName
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:216
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:599
llvm::coverage::Counter::Zero
@ Zero
Definition: CoverageMapping.h:95
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
Binary.h
FilenameRange::StartingIndex
unsigned StartingIndex
Definition: CoverageMappingReader.cpp:482
Version
uint64_t Version
Definition: RawMemProfReader.cpp:25
SmallVector.h
COFF.h
N
#define N
llvm::coverage::Version6
@ Version6
Definition: CoverageMapping.h:1007
Expressions
gvn Early GVN Hoisting of Expressions
Definition: GVNHoist.cpp:1261
llvm::coverage::Version2
@ Version2
Definition: CoverageMapping.h:997
llvm::coverage::Counter::EncodingTagMask
static const unsigned EncodingTagMask
Definition: CoverageMapping.h:97
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::object::createBinary
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:46
llvm::support::endianness
endianness
Definition: Endian.h:27
loadTestingFormat
static Expected< std::unique_ptr< BinaryCoverageReader > > loadTestingFormat(StringRef Data, StringRef CompilationDir)
Definition: CoverageMappingReader.cpp:862
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::instrprof_error::malformed
@ malformed
llvm::coverage::Counter::getZero
static Counter getZero()
Return the counter that represents the number zero.
Definition: CoverageMapping.h:133
loadBinaryFormat
static Expected< std::unique_ptr< BinaryCoverageReader > > loadBinaryFormat(std::unique_ptr< Binary > Bin, StringRef Arch, StringRef CompilationDir="")
Definition: CoverageMappingReader.cpp:954
llvm::coverage::CounterMappingRegion::CodeRegion
@ CodeRegion
A CodeRegion associates some code with a counter.
Definition: CoverageMapping.h:211
lookupSections
static Expected< std::vector< SectionRef > > lookupSections(ObjectFile &OF, StringRef Name)
Find all sections that match Name.
Definition: CoverageMappingReader.cpp:929
readString
static StringRef readString(WasmObjectFile::ReadContext &Ctx)
Definition: WasmObjectFile.cpp:114
raw_ostream.h
llvm::coverage::RawCoverageFilenamesReader::read
Error read(CovMapVersion Version)
Definition: CoverageMappingReader.cpp:100
llvm::coverage::BinaryCoverageReader::FuncRecordsStorage
std::unique_ptr< MemoryBuffer > FuncRecordsStorage
Definition: CoverageMappingReader.h:182
llvm::coverage::Counter::EncodingTagBits
static const unsigned EncodingTagBits
Definition: CoverageMapping.h:96
Endian.h
copy
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same we currently get code like const It could be done with a smaller encoding like local tee $pop5 local copy
Definition: README.txt:101
llvm::coverage::CoverageMappingRecord
Coverage mapping information for a single function.
Definition: CoverageMappingReader.h:35
llvm::handleAllErrors
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:968
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::coverage::CounterMappingRegion::SkippedRegion
@ SkippedRegion
A SkippedRegion represents a source range with code that was skipped by a preprocessor or similar mea...
Definition: CoverageMapping.h:220
llvm::support::big
@ big
Definition: Endian.h:27
llvm::coverage::RawCoverageReader::readString
Error readString(StringRef &Result)
Definition: CoverageMappingReader.cpp:91
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38