LLVM  16.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/Archive.h"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/Error.h"
27 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/Endian.h"
33 #include "llvm/Support/Error.h"
35 #include "llvm/Support/LEB128.h"
37 #include "llvm/Support/Path.h"
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace coverage;
43 using namespace object;
44 
45 #define DEBUG_TYPE "coverage-mapping"
46 
47 STATISTIC(CovMapNumRecords, "The # of coverage function records");
48 STATISTIC(CovMapNumUsedRecords, "The # of used coverage function records");
49 
50 void CoverageMappingIterator::increment() {
51  if (ReadErr != coveragemap_error::success)
52  return;
53 
54  // Check if all the records were read or if an error occurred while reading
55  // the next record.
56  if (auto E = Reader->readNextRecord(Record))
57  handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
58  if (CME.get() == coveragemap_error::eof)
59  *this = CoverageMappingIterator();
60  else
61  ReadErr = CME.get();
62  });
63 }
64 
66  if (Data.empty())
67  return make_error<CoverageMapError>(coveragemap_error::truncated);
68  unsigned N = 0;
69  Result = decodeULEB128(Data.bytes_begin(), &N);
70  if (N > Data.size())
71  return make_error<CoverageMapError>(coveragemap_error::malformed);
72  Data = Data.substr(N);
73  return Error::success();
74 }
75 
77  if (auto Err = readULEB128(Result))
78  return Err;
79  if (Result >= MaxPlus1)
80  return make_error<CoverageMapError>(coveragemap_error::malformed);
81  return Error::success();
82 }
83 
85  if (auto Err = readULEB128(Result))
86  return Err;
87  if (Result > Data.size())
88  return make_error<CoverageMapError>(coveragemap_error::malformed);
89  return Error::success();
90 }
91 
93  uint64_t Length;
94  if (auto Err = readSize(Length))
95  return Err;
96  Result = Data.substr(0, Length);
97  Data = Data.substr(Length);
98  return Error::success();
99 }
100 
102  uint64_t NumFilenames;
103  if (auto Err = readSize(NumFilenames))
104  return Err;
105  if (!NumFilenames)
106  return make_error<CoverageMapError>(coveragemap_error::malformed);
107 
109  return readUncompressed(Version, NumFilenames);
110 
111  // The uncompressed length may exceed the size of the encoded filenames.
112  // Skip size validation.
113  uint64_t UncompressedLen;
114  if (auto Err = readULEB128(UncompressedLen))
115  return Err;
116 
117  uint64_t CompressedLen;
118  if (auto Err = readSize(CompressedLen))
119  return Err;
120 
121  if (CompressedLen > 0) {
123  return make_error<CoverageMapError>(
125 
126  // Allocate memory for the decompressed filenames.
127  SmallVector<uint8_t, 0> StorageBuf;
128 
129  // Read compressed filenames.
130  StringRef CompressedFilenames = Data.substr(0, CompressedLen);
131  Data = Data.substr(CompressedLen);
133  arrayRefFromStringRef(CompressedFilenames), StorageBuf,
134  UncompressedLen);
135  if (Err) {
136  consumeError(std::move(Err));
137  return make_error<CoverageMapError>(
139  }
140 
141  RawCoverageFilenamesReader Delegate(toStringRef(StorageBuf), Filenames,
142  CompilationDir);
143  return Delegate.readUncompressed(Version, NumFilenames);
144  }
145 
146  return readUncompressed(Version, NumFilenames);
147 }
148 
149 Error RawCoverageFilenamesReader::readUncompressed(CovMapVersion Version,
150  uint64_t NumFilenames) {
151  // Read uncompressed filenames.
153  for (size_t I = 0; I < NumFilenames; ++I) {
154  StringRef Filename;
155  if (auto Err = readString(Filename))
156  return Err;
157  Filenames.push_back(Filename.str());
158  }
159  } else {
160  StringRef CWD;
161  if (auto Err = readString(CWD))
162  return Err;
163  Filenames.push_back(CWD.str());
164 
165  for (size_t I = 1; I < NumFilenames; ++I) {
166  StringRef Filename;
167  if (auto Err = readString(Filename))
168  return Err;
169  if (sys::path::is_absolute(Filename)) {
170  Filenames.push_back(Filename.str());
171  } else {
173  if (!CompilationDir.empty())
174  P.assign(CompilationDir);
175  else
176  P.assign(CWD);
177  llvm::sys::path::append(P, Filename);
178  sys::path::remove_dots(P, /*remove_dot_dot=*/true);
179  Filenames.push_back(static_cast<std::string>(P.str()));
180  }
181  }
182  }
183  return Error::success();
184 }
185 
186 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
188  switch (Tag) {
189  case Counter::Zero:
190  C = Counter::getZero();
191  return Error::success();
194  return Error::success();
195  default:
196  break;
197  }
199  switch (Tag) {
201  case CounterExpression::Add: {
203  if (ID >= Expressions.size())
204  return make_error<CoverageMapError>(coveragemap_error::malformed);
207  break;
208  }
209  default:
210  return make_error<CoverageMapError>(coveragemap_error::malformed);
211  }
212  return Error::success();
213 }
214 
215 Error RawCoverageMappingReader::readCounter(Counter &C) {
216  uint64_t EncodedCounter;
217  if (auto Err =
218  readIntMax(EncodedCounter, std::numeric_limits<unsigned>::max()))
219  return Err;
220  if (auto Err = decodeCounter(EncodedCounter, C))
221  return Err;
222  return Error::success();
223 }
224 
225 static const unsigned EncodingExpansionRegionBit = 1
227 
228 /// Read the sub-array of regions for the given inferred file id.
229 /// \param NumFileIDs the number of file ids that are defined for this
230 /// function.
231 Error RawCoverageMappingReader::readMappingRegionsSubArray(
232  std::vector<CounterMappingRegion> &MappingRegions, unsigned InferredFileID,
233  size_t NumFileIDs) {
234  uint64_t NumRegions;
235  if (auto Err = readSize(NumRegions))
236  return Err;
237  unsigned LineStart = 0;
238  for (size_t I = 0; I < NumRegions; ++I) {
239  Counter C, C2;
241 
242  // Read the combined counter + region kind.
243  uint64_t EncodedCounterAndRegion;
244  if (auto Err = readIntMax(EncodedCounterAndRegion,
246  return Err;
247  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
248  uint64_t ExpandedFileID = 0;
249 
250  // If Tag does not represent a ZeroCounter, then it is understood to refer
251  // to a counter or counter expression with region kind assumed to be
252  // "CodeRegion". In that case, EncodedCounterAndRegion actually encodes the
253  // referenced counter or counter expression (and nothing else).
254  //
255  // If Tag represents a ZeroCounter and EncodingExpansionRegionBit is set,
256  // then EncodedCounterAndRegion is interpreted to represent an
257  // ExpansionRegion. In all other cases, EncodedCounterAndRegion is
258  // interpreted to refer to a specific region kind, after which additional
259  // fields may be read (e.g. BranchRegions have two encoded counters that
260  // follow an encoded region kind value).
261  if (Tag != Counter::Zero) {
262  if (auto Err = decodeCounter(EncodedCounterAndRegion, C))
263  return Err;
264  } else {
265  // Is it an expansion region?
266  if (EncodedCounterAndRegion & EncodingExpansionRegionBit) {
268  ExpandedFileID = EncodedCounterAndRegion >>
270  if (ExpandedFileID >= NumFileIDs)
271  return make_error<CoverageMapError>(coveragemap_error::malformed);
272  } else {
273  switch (EncodedCounterAndRegion >>
276  // Don't do anything when we have a code region with a zero counter.
277  break;
280  break;
282  // For a Branch Region, read two successive counters.
284  if (auto Err = readCounter(C))
285  return Err;
286  if (auto Err = readCounter(C2))
287  return Err;
288  break;
289  default:
290  return make_error<CoverageMapError>(coveragemap_error::malformed);
291  }
292  }
293  }
294 
295  // Read the source range.
296  uint64_t LineStartDelta, ColumnStart, NumLines, ColumnEnd;
297  if (auto Err =
298  readIntMax(LineStartDelta, std::numeric_limits<unsigned>::max()))
299  return Err;
300  if (auto Err = readULEB128(ColumnStart))
301  return Err;
302  if (ColumnStart > std::numeric_limits<unsigned>::max())
303  return make_error<CoverageMapError>(coveragemap_error::malformed);
304  if (auto Err = readIntMax(NumLines, std::numeric_limits<unsigned>::max()))
305  return Err;
306  if (auto Err = readIntMax(ColumnEnd, std::numeric_limits<unsigned>::max()))
307  return Err;
308  LineStart += LineStartDelta;
309 
310  // If the high bit of ColumnEnd is set, this is a gap region.
311  if (ColumnEnd & (1U << 31)) {
313  ColumnEnd &= ~(1U << 31);
314  }
315 
316  // Adjust the column locations for the empty regions that are supposed to
317  // cover whole lines. Those regions should be encoded with the
318  // column range (1 -> std::numeric_limits<unsigned>::max()), but because
319  // the encoded std::numeric_limits<unsigned>::max() is several bytes long,
320  // we set the column range to (0 -> 0) to ensure that the column start and
321  // column end take up one byte each.
322  // The std::numeric_limits<unsigned>::max() is used to represent a column
323  // position at the end of the line without knowing the length of that line.
324  if (ColumnStart == 0 && ColumnEnd == 0) {
325  ColumnStart = 1;
327  }
328 
329  LLVM_DEBUG({
330  dbgs() << "Counter in file " << InferredFileID << " " << LineStart << ":"
331  << ColumnStart << " -> " << (LineStart + NumLines) << ":"
332  << ColumnEnd << ", ";
334  dbgs() << "Expands to file " << ExpandedFileID;
335  else
337  dbgs() << "\n";
338  });
339 
340  auto CMR = CounterMappingRegion(C, C2, InferredFileID, ExpandedFileID,
341  LineStart, ColumnStart,
342  LineStart + NumLines, ColumnEnd, Kind);
343  if (CMR.startLoc() > CMR.endLoc())
344  return make_error<CoverageMapError>(coveragemap_error::malformed);
345  MappingRegions.push_back(CMR);
346  }
347  return Error::success();
348 }
349 
351  // Read the virtual file mapping.
352  SmallVector<unsigned, 8> VirtualFileMapping;
353  uint64_t NumFileMappings;
354  if (auto Err = readSize(NumFileMappings))
355  return Err;
356  for (size_t I = 0; I < NumFileMappings; ++I) {
357  uint64_t FilenameIndex;
358  if (auto Err = readIntMax(FilenameIndex, TranslationUnitFilenames.size()))
359  return Err;
360  VirtualFileMapping.push_back(FilenameIndex);
361  }
362 
363  // Construct the files using unique filenames and virtual file mapping.
364  for (auto I : VirtualFileMapping) {
365  Filenames.push_back(TranslationUnitFilenames[I]);
366  }
367 
368  // Read the expressions.
369  uint64_t NumExpressions;
370  if (auto Err = readSize(NumExpressions))
371  return Err;
372  // Create an array of dummy expressions that get the proper counters
373  // when the expressions are read, and the proper kinds when the counters
374  // are decoded.
375  Expressions.resize(
376  NumExpressions,
378  for (size_t I = 0; I < NumExpressions; ++I) {
379  if (auto Err = readCounter(Expressions[I].LHS))
380  return Err;
381  if (auto Err = readCounter(Expressions[I].RHS))
382  return Err;
383  }
384 
385  // Read the mapping regions sub-arrays.
386  for (unsigned InferredFileID = 0, S = VirtualFileMapping.size();
387  InferredFileID < S; ++InferredFileID) {
388  if (auto Err = readMappingRegionsSubArray(MappingRegions, InferredFileID,
389  VirtualFileMapping.size()))
390  return Err;
391  }
392 
393  // Set the counters for the expansion regions.
394  // i.e. Counter of expansion region = counter of the first region
395  // from the expanded file.
396  // Perform multiple passes to correctly propagate the counters through
397  // all the nested expansion regions.
398  SmallVector<CounterMappingRegion *, 8> FileIDExpansionRegionMapping;
399  FileIDExpansionRegionMapping.resize(VirtualFileMapping.size(), nullptr);
400  for (unsigned Pass = 1, S = VirtualFileMapping.size(); Pass < S; ++Pass) {
401  for (auto &R : MappingRegions) {
403  continue;
404  assert(!FileIDExpansionRegionMapping[R.ExpandedFileID]);
405  FileIDExpansionRegionMapping[R.ExpandedFileID] = &R;
406  }
407  for (auto &R : MappingRegions) {
408  if (FileIDExpansionRegionMapping[R.FileID]) {
409  FileIDExpansionRegionMapping[R.FileID]->Count = R.Count;
410  FileIDExpansionRegionMapping[R.FileID] = nullptr;
411  }
412  }
413  }
414 
415  return Error::success();
416 }
417 
419  // A dummy coverage mapping data consists of just one region with zero count.
420  uint64_t NumFileMappings;
421  if (Error Err = readSize(NumFileMappings))
422  return std::move(Err);
423  if (NumFileMappings != 1)
424  return false;
425  // We don't expect any specific value for the filename index, just skip it.
426  uint64_t FilenameIndex;
427  if (Error Err =
428  readIntMax(FilenameIndex, std::numeric_limits<unsigned>::max()))
429  return std::move(Err);
430  uint64_t NumExpressions;
431  if (Error Err = readSize(NumExpressions))
432  return std::move(Err);
433  if (NumExpressions != 0)
434  return false;
435  uint64_t NumRegions;
436  if (Error Err = readSize(NumRegions))
437  return std::move(Err);
438  if (NumRegions != 1)
439  return false;
440  uint64_t EncodedCounterAndRegion;
441  if (Error Err = readIntMax(EncodedCounterAndRegion,
443  return std::move(Err);
444  unsigned Tag = EncodedCounterAndRegion & Counter::EncodingTagMask;
445  return Tag == Counter::Zero;
446 }
447 
448 Error InstrProfSymtab::create(SectionRef &Section) {
449  Expected<StringRef> DataOrErr = Section.getContents();
450  if (!DataOrErr)
451  return DataOrErr.takeError();
452  Data = *DataOrErr;
453  Address = Section.getAddress();
454 
455  // If this is a linked PE/COFF file, then we have to skip over the null byte
456  // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
457  const ObjectFile *Obj = Section.getObject();
458  if (isa<COFFObjectFile>(Obj) && !Obj->isRelocatableObject())
459  Data = Data.drop_front(1);
460 
461  return Error::success();
462 }
463 
465  if (Pointer < Address)
466  return StringRef();
467  auto Offset = Pointer - Address;
468  if (Offset + Size > Data.size())
469  return StringRef();
470  return Data.substr(Pointer - Address, Size);
471 }
472 
473 // Check if the mapping data is a dummy, i.e. is emitted for an unused function.
475  // The hash value of dummy mapping records is always zero.
476  if (Hash)
477  return false;
478  return RawCoverageMappingDummyChecker(Mapping).isDummy();
479 }
480 
481 /// A range of filename indices. Used to specify the location of a batch of
482 /// filenames in a vector-like container.
484  unsigned StartingIndex;
485  unsigned Length;
486 
487  FilenameRange(unsigned StartingIndex, unsigned Length)
488  : StartingIndex(StartingIndex), Length(Length) {}
489 
490  void markInvalid() { Length = 0; }
491  bool isInvalid() const { return Length == 0; }
492 };
493 
494 namespace {
495 
496 /// The interface to read coverage mapping function records for a module.
497 struct CovMapFuncRecordReader {
498  virtual ~CovMapFuncRecordReader() = default;
499 
500  // Read a coverage header.
501  //
502  // \p CovBuf points to the buffer containing the \c CovHeader of the coverage
503  // mapping data associated with the module.
504  //
505  // Returns a pointer to the next \c CovHeader if it exists, or to an address
506  // greater than \p CovEnd if not.
507  virtual Expected<const char *> readCoverageHeader(const char *CovBuf,
508  const char *CovBufEnd) = 0;
509 
510  // Read function records.
511  //
512  // \p FuncRecBuf points to the buffer containing a batch of function records.
513  // \p FuncRecBufEnd points past the end of the batch of records.
514  //
515  // Prior to Version4, \p OutOfLineFileRange points to a sequence of filenames
516  // associated with the function records. It is unused in Version4.
517  //
518  // Prior to Version4, \p OutOfLineMappingBuf points to a sequence of coverage
519  // mappings associated with the function records. It is unused in Version4.
520  virtual Error readFunctionRecords(const char *FuncRecBuf,
521  const char *FuncRecBufEnd,
522  Optional<FilenameRange> OutOfLineFileRange,
523  const char *OutOfLineMappingBuf,
524  const char *OutOfLineMappingBufEnd) = 0;
525 
526  template <class IntPtrT, support::endianness Endian>
529  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
530  std::vector<std::string> &F);
531 };
532 
533 // A class for reading coverage mapping function records for a module.
534 template <CovMapVersion Version, class IntPtrT, support::endianness Endian>
535 class VersionedCovMapFuncRecordReader : public CovMapFuncRecordReader {
536  using FuncRecordType =
538  using NameRefType = typename CovMapTraits<Version, IntPtrT>::NameRefType;
539 
540  // Maps function's name references to the indexes of their records
541  // in \c Records.
542  DenseMap<NameRefType, size_t> FunctionRecords;
543  InstrProfSymtab &ProfileNames;
544  StringRef CompilationDir;
545  std::vector<std::string> &Filenames;
546  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records;
547 
548  // Maps a hash of the filenames in a TU to a \c FileRange. The range
549  // specifies the location of the hashed filenames in \c Filenames.
551 
552  // Add the record to the collection if we don't already have a record that
553  // points to the same function name. This is useful to ignore the redundant
554  // records for the functions with ODR linkage.
555  // In addition, prefer records with real coverage mapping data to dummy
556  // records, which were emitted for inline functions which were seen but
557  // not used in the corresponding translation unit.
558  Error insertFunctionRecordIfNeeded(const FuncRecordType *CFR,
559  StringRef Mapping,
560  FilenameRange FileRange) {
561  ++CovMapNumRecords;
562  uint64_t FuncHash = CFR->template getFuncHash<Endian>();
563  NameRefType NameRef = CFR->template getFuncNameRef<Endian>();
564  auto InsertResult =
565  FunctionRecords.insert(std::make_pair(NameRef, Records.size()));
566  if (InsertResult.second) {
567  StringRef FuncName;
568  if (Error Err = CFR->template getFuncName<Endian>(ProfileNames, FuncName))
569  return Err;
570  if (FuncName.empty())
571  return make_error<InstrProfError>(instrprof_error::malformed,
572  "function name is empty");
573  ++CovMapNumUsedRecords;
574  Records.emplace_back(Version, FuncName, FuncHash, Mapping,
575  FileRange.StartingIndex, FileRange.Length);
576  return Error::success();
577  }
578  // Update the existing record if it's a dummy and the new record is real.
579  size_t OldRecordIndex = InsertResult.first->second;
581  Records[OldRecordIndex];
582  Expected<bool> OldIsDummyExpected = isCoverageMappingDummy(
583  OldRecord.FunctionHash, OldRecord.CoverageMapping);
584  if (Error Err = OldIsDummyExpected.takeError())
585  return Err;
586  if (!*OldIsDummyExpected)
587  return Error::success();
588  Expected<bool> NewIsDummyExpected =
589  isCoverageMappingDummy(FuncHash, Mapping);
590  if (Error Err = NewIsDummyExpected.takeError())
591  return Err;
592  if (*NewIsDummyExpected)
593  return Error::success();
594  ++CovMapNumUsedRecords;
595  OldRecord.FunctionHash = FuncHash;
596  OldRecord.CoverageMapping = Mapping;
597  OldRecord.FilenamesBegin = FileRange.StartingIndex;
598  OldRecord.FilenamesSize = FileRange.Length;
599  return Error::success();
600  }
601 
602 public:
603  VersionedCovMapFuncRecordReader(
605  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
606  std::vector<std::string> &F)
607  : ProfileNames(P), CompilationDir(D), Filenames(F), Records(R) {}
608 
609  ~VersionedCovMapFuncRecordReader() override = default;
610 
611  Expected<const char *> readCoverageHeader(const char *CovBuf,
612  const char *CovBufEnd) override {
613  using namespace support;
614 
615  if (CovBuf + sizeof(CovMapHeader) > CovBufEnd)
616  return make_error<CoverageMapError>(coveragemap_error::malformed);
617  auto CovHeader = reinterpret_cast<const CovMapHeader *>(CovBuf);
618  uint32_t NRecords = CovHeader->getNRecords<Endian>();
619  uint32_t FilenamesSize = CovHeader->getFilenamesSize<Endian>();
620  uint32_t CoverageSize = CovHeader->getCoverageSize<Endian>();
621  assert((CovMapVersion)CovHeader->getVersion<Endian>() == Version);
622  CovBuf = reinterpret_cast<const char *>(CovHeader + 1);
623 
624  // Skip past the function records, saving the start and end for later.
625  // This is a no-op in Version4 (function records are read after all headers
626  // are read).
627  const char *FuncRecBuf = nullptr;
628  const char *FuncRecBufEnd = nullptr;
630  FuncRecBuf = CovBuf;
631  CovBuf += NRecords * sizeof(FuncRecordType);
633  FuncRecBufEnd = CovBuf;
634 
635  // Get the filenames.
636  if (CovBuf + FilenamesSize > CovBufEnd)
637  return make_error<CoverageMapError>(coveragemap_error::malformed);
638  size_t FilenamesBegin = Filenames.size();
639  StringRef FilenameRegion(CovBuf, FilenamesSize);
640  RawCoverageFilenamesReader Reader(FilenameRegion, Filenames,
641  CompilationDir);
642  if (auto Err = Reader.read(Version))
643  return std::move(Err);
644  CovBuf += FilenamesSize;
645  FilenameRange FileRange(FilenamesBegin, Filenames.size() - FilenamesBegin);
646 
648  // Map a hash of the filenames region to the filename range associated
649  // with this coverage header.
650  int64_t FilenamesRef =
651  llvm::IndexedInstrProf::ComputeHash(FilenameRegion);
652  auto Insert =
653  FileRangeMap.insert(std::make_pair(FilenamesRef, FileRange));
654  if (!Insert.second) {
655  // The same filenames ref was encountered twice. It's possible that
656  // the associated filenames are the same.
657  auto It = Filenames.begin();
658  FilenameRange &OrigRange = Insert.first->getSecond();
659  if (std::equal(It + OrigRange.StartingIndex,
660  It + OrigRange.StartingIndex + OrigRange.Length,
661  It + FileRange.StartingIndex,
662  It + FileRange.StartingIndex + FileRange.Length))
663  // Map the new range to the original one.
664  FileRange = OrigRange;
665  else
666  // This is a hash collision. Mark the filenames ref invalid.
667  OrigRange.markInvalid();
668  }
669  }
670 
671  // We'll read the coverage mapping records in the loop below.
672  // This is a no-op in Version4 (coverage mappings are not affixed to the
673  // coverage header).
674  const char *MappingBuf = CovBuf;
675  if (Version >= CovMapVersion::Version4 && CoverageSize != 0)
676  return make_error<CoverageMapError>(coveragemap_error::malformed);
677  CovBuf += CoverageSize;
678  const char *MappingEnd = CovBuf;
679 
680  if (CovBuf > CovBufEnd)
681  return make_error<CoverageMapError>(coveragemap_error::malformed);
682 
684  // Read each function record.
685  if (Error E = readFunctionRecords(FuncRecBuf, FuncRecBufEnd, FileRange,
686  MappingBuf, MappingEnd))
687  return std::move(E);
688  }
689 
690  // Each coverage map has an alignment of 8, so we need to adjust alignment
691  // before reading the next map.
692  CovBuf += offsetToAlignedAddr(CovBuf, Align(8));
693 
694  return CovBuf;
695  }
696 
697  Error readFunctionRecords(const char *FuncRecBuf, const char *FuncRecBufEnd,
698  Optional<FilenameRange> OutOfLineFileRange,
699  const char *OutOfLineMappingBuf,
700  const char *OutOfLineMappingBufEnd) override {
701  auto CFR = reinterpret_cast<const FuncRecordType *>(FuncRecBuf);
702  while ((const char *)CFR < FuncRecBufEnd) {
703  // Validate the length of the coverage mapping for this function.
704  const char *NextMappingBuf;
705  const FuncRecordType *NextCFR;
706  std::tie(NextMappingBuf, NextCFR) =
707  CFR->template advanceByOne<Endian>(OutOfLineMappingBuf);
709  if (NextMappingBuf > OutOfLineMappingBufEnd)
710  return make_error<CoverageMapError>(coveragemap_error::malformed);
711 
712  // Look up the set of filenames associated with this function record.
713  Optional<FilenameRange> FileRange;
715  FileRange = OutOfLineFileRange;
716  } else {
717  uint64_t FilenamesRef = CFR->template getFilenamesRef<Endian>();
718  auto It = FileRangeMap.find(FilenamesRef);
719  if (It == FileRangeMap.end())
720  return make_error<CoverageMapError>(coveragemap_error::malformed);
721  else
722  FileRange = It->getSecond();
723  }
724 
725  // Now, read the coverage data.
726  if (FileRange && !FileRange->isInvalid()) {
727  StringRef Mapping =
728  CFR->template getCoverageMapping<Endian>(OutOfLineMappingBuf);
730  Mapping.data() + Mapping.size() > FuncRecBufEnd)
731  return make_error<CoverageMapError>(coveragemap_error::malformed);
732  if (Error Err = insertFunctionRecordIfNeeded(CFR, Mapping, *FileRange))
733  return Err;
734  }
735 
736  std::tie(OutOfLineMappingBuf, CFR) = std::tie(NextMappingBuf, NextCFR);
737  }
738  return Error::success();
739  }
740 };
741 
742 } // end anonymous namespace
743 
744 template <class IntPtrT, support::endianness Endian>
747  std::vector<BinaryCoverageReader::ProfileMappingRecord> &R, StringRef D,
748  std::vector<std::string> &F) {
749  using namespace coverage;
750 
751  switch (Version) {
753  return std::make_unique<VersionedCovMapFuncRecordReader<
754  CovMapVersion::Version1, IntPtrT, Endian>>(P, R, D, F);
760  // Decompress the name data.
761  if (Error E = P.create(P.getNameData()))
762  return std::move(E);
764  return std::make_unique<VersionedCovMapFuncRecordReader<
765  CovMapVersion::Version2, IntPtrT, Endian>>(P, R, D, F);
766  else if (Version == CovMapVersion::Version3)
767  return std::make_unique<VersionedCovMapFuncRecordReader<
768  CovMapVersion::Version3, IntPtrT, Endian>>(P, R, D, F);
769  else if (Version == CovMapVersion::Version4)
770  return std::make_unique<VersionedCovMapFuncRecordReader<
771  CovMapVersion::Version4, IntPtrT, Endian>>(P, R, D, F);
772  else if (Version == CovMapVersion::Version5)
773  return std::make_unique<VersionedCovMapFuncRecordReader<
774  CovMapVersion::Version5, IntPtrT, Endian>>(P, R, D, F);
775  else if (Version == CovMapVersion::Version6)
776  return std::make_unique<VersionedCovMapFuncRecordReader<
777  CovMapVersion::Version6, IntPtrT, Endian>>(P, R, D, F);
778  }
779  llvm_unreachable("Unsupported version");
780 }
781 
782 template <typename T, support::endianness Endian>
784  InstrProfSymtab &ProfileNames, StringRef CovMap, StringRef FuncRecords,
785  std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
786  StringRef CompilationDir, std::vector<std::string> &Filenames) {
787  using namespace coverage;
788 
789  // Read the records in the coverage data section.
790  auto CovHeader =
791  reinterpret_cast<const CovMapHeader *>(CovMap.data());
792  CovMapVersion Version = (CovMapVersion)CovHeader->getVersion<Endian>();
794  return make_error<CoverageMapError>(coveragemap_error::unsupported_version);
796  CovMapFuncRecordReader::get<T, Endian>(Version, ProfileNames, Records,
797  CompilationDir, Filenames);
798  if (Error E = ReaderExpected.takeError())
799  return E;
800  auto Reader = std::move(ReaderExpected.get());
801  const char *CovBuf = CovMap.data();
802  const char *CovBufEnd = CovBuf + CovMap.size();
803  const char *FuncRecBuf = FuncRecords.data();
804  const char *FuncRecBufEnd = FuncRecords.data() + FuncRecords.size();
805  while (CovBuf < CovBufEnd) {
806  // Read the current coverage header & filename data.
807  //
808  // Prior to Version4, this also reads all function records affixed to the
809  // header.
810  //
811  // Return a pointer to the next coverage header.
812  auto NextOrErr = Reader->readCoverageHeader(CovBuf, CovBufEnd);
813  if (auto E = NextOrErr.takeError())
814  return E;
815  CovBuf = NextOrErr.get();
816  }
817  // In Version4, function records are not affixed to coverage headers. Read
818  // the records from their dedicated section.
820  return Reader->readFunctionRecords(FuncRecBuf, FuncRecBufEnd, std::nullopt,
821  nullptr, nullptr);
822  return Error::success();
823 }
824 
825 static const char *TestingFormatMagic = "llvmcovmtestdata";
826 
829  StringRef Coverage, FuncRecordsStorage &&FuncRecords,
830  InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress,
831  support::endianness Endian, StringRef CompilationDir) {
832  std::unique_ptr<BinaryCoverageReader> Reader(
833  new BinaryCoverageReader(std::move(FuncRecords)));
834  Reader->ProfileNames = std::move(ProfileNames);
835  StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
836  if (BytesInAddress == 4 && Endian == support::endianness::little) {
837  if (Error E =
838  readCoverageMappingData<uint32_t, support::endianness::little>(
839  Reader->ProfileNames, Coverage, FuncRecordsRef,
840  Reader->MappingRecords, CompilationDir, Reader->Filenames))
841  return std::move(E);
842  } else if (BytesInAddress == 4 && Endian == support::endianness::big) {
843  if (Error E = readCoverageMappingData<uint32_t, support::endianness::big>(
844  Reader->ProfileNames, Coverage, FuncRecordsRef,
845  Reader->MappingRecords, CompilationDir, Reader->Filenames))
846  return std::move(E);
847  } else if (BytesInAddress == 8 && Endian == support::endianness::little) {
848  if (Error E =
849  readCoverageMappingData<uint64_t, support::endianness::little>(
850  Reader->ProfileNames, Coverage, FuncRecordsRef,
851  Reader->MappingRecords, CompilationDir, Reader->Filenames))
852  return std::move(E);
853  } else if (BytesInAddress == 8 && Endian == support::endianness::big) {
854  if (Error E = readCoverageMappingData<uint64_t, support::endianness::big>(
855  Reader->ProfileNames, Coverage, FuncRecordsRef,
856  Reader->MappingRecords, CompilationDir, Reader->Filenames))
857  return std::move(E);
858  } else
859  return make_error<CoverageMapError>(coveragemap_error::malformed);
860  return std::move(Reader);
861 }
862 
864 loadTestingFormat(StringRef Data, StringRef CompilationDir) {
865  uint8_t BytesInAddress = 8;
867 
869  if (Data.empty())
870  return make_error<CoverageMapError>(coveragemap_error::truncated);
871  unsigned N = 0;
872  uint64_t ProfileNamesSize = decodeULEB128(Data.bytes_begin(), &N);
873  if (N > Data.size())
874  return make_error<CoverageMapError>(coveragemap_error::malformed);
875  Data = Data.substr(N);
876  if (Data.empty())
877  return make_error<CoverageMapError>(coveragemap_error::truncated);
878  N = 0;
879  uint64_t Address = decodeULEB128(Data.bytes_begin(), &N);
880  if (N > Data.size())
881  return make_error<CoverageMapError>(coveragemap_error::malformed);
882  Data = Data.substr(N);
883  if (Data.size() < ProfileNamesSize)
884  return make_error<CoverageMapError>(coveragemap_error::malformed);
885  InstrProfSymtab ProfileNames;
886  if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
887  return std::move(E);
888  Data = Data.substr(ProfileNamesSize);
889  // Skip the padding bytes because coverage map data has an alignment of 8.
890  size_t Pad = offsetToAlignedAddr(Data.data(), Align(8));
891  if (Data.size() < Pad)
892  return make_error<CoverageMapError>(coveragemap_error::malformed);
893  Data = Data.substr(Pad);
894  if (Data.size() < sizeof(CovMapHeader))
895  return make_error<CoverageMapError>(coveragemap_error::malformed);
896  auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
897  Data.substr(0, sizeof(CovMapHeader)).data());
899  (CovMapVersion)CovHeader->getVersion<support::endianness::little>();
904  if (CoverageMapping.empty())
905  return make_error<CoverageMapError>(coveragemap_error::truncated);
906  CoverageRecords = MemoryBuffer::getMemBuffer("");
907  } else {
908  uint32_t FilenamesSize =
909  CovHeader->getFilenamesSize<support::endianness::little>();
910  uint32_t CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
911  CoverageMapping = Data.substr(0, CoverageMappingSize);
912  if (CoverageMapping.empty())
913  return make_error<CoverageMapError>(coveragemap_error::truncated);
914  Data = Data.substr(CoverageMappingSize);
915  // Skip the padding bytes because coverage records data has an alignment
916  // of 8.
917  Pad = offsetToAlignedAddr(Data.data(), Align(8));
918  if (Data.size() < Pad)
919  return make_error<CoverageMapError>(coveragemap_error::malformed);
920  CoverageRecords = MemoryBuffer::getMemBuffer(Data.substr(Pad));
921  if (CoverageRecords->getBufferSize() == 0)
922  return make_error<CoverageMapError>(coveragemap_error::truncated);
923  }
925  CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),
926  BytesInAddress, Endian, CompilationDir);
927 }
928 
929 /// Find all sections that match \p Name. There may be more than one if comdats
930 /// are in use, e.g. for the __llvm_covfun section on ELF.
932  StringRef Name) {
933  // On COFF, the object file section name may end in "$M". This tells the
934  // linker to sort these sections between "$A" and "$Z". The linker removes the
935  // dollar and everything after it in the final binary. Do the same to match.
936  bool IsCOFF = isa<COFFObjectFile>(OF);
937  auto stripSuffix = [IsCOFF](StringRef N) {
938  return IsCOFF ? N.split('$').first : N;
939  };
940  Name = stripSuffix(Name);
941 
942  std::vector<SectionRef> Sections;
943  for (const auto &Section : OF.sections()) {
944  Expected<StringRef> NameOrErr = Section.getName();
945  if (!NameOrErr)
946  return NameOrErr.takeError();
947  if (stripSuffix(*NameOrErr) == Name)
948  Sections.push_back(Section);
949  }
950  if (Sections.empty())
951  return make_error<CoverageMapError>(coveragemap_error::no_data_found);
952  return Sections;
953 }
954 
956 loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
957  StringRef CompilationDir = "") {
958  std::unique_ptr<ObjectFile> OF;
959  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
960  // If we have a universal binary, try to look up the object for the
961  // appropriate architecture.
962  auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch);
963  if (!ObjectFileOrErr)
964  return ObjectFileOrErr.takeError();
965  OF = std::move(ObjectFileOrErr.get());
966  } else if (isa<ObjectFile>(Bin.get())) {
967  // For any other object file, upcast and take ownership.
968  OF.reset(cast<ObjectFile>(Bin.release()));
969  // If we've asked for a particular arch, make sure they match.
970  if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
971  return errorCodeToError(object_error::arch_not_found);
972  } else
973  // We can only handle object files.
974  return make_error<CoverageMapError>(coveragemap_error::malformed);
975 
976  // The coverage uses native pointer sizes for the object it's written in.
977  uint8_t BytesInAddress = OF->getBytesInAddress();
978  support::endianness Endian = OF->isLittleEndian()
981 
982  // Look for the sections that we are interested in.
983  auto ObjFormat = OF->getTripleObjectFormat();
984  auto NamesSection =
985  lookupSections(*OF, getInstrProfSectionName(IPSK_name, ObjFormat,
986  /*AddSegmentInfo=*/false));
987  if (auto E = NamesSection.takeError())
988  return std::move(E);
989  auto CoverageSection =
990  lookupSections(*OF, getInstrProfSectionName(IPSK_covmap, ObjFormat,
991  /*AddSegmentInfo=*/false));
992  if (auto E = CoverageSection.takeError())
993  return std::move(E);
994  std::vector<SectionRef> CoverageSectionRefs = *CoverageSection;
995  if (CoverageSectionRefs.size() != 1)
996  return make_error<CoverageMapError>(coveragemap_error::malformed);
997  auto CoverageMappingOrErr = CoverageSectionRefs.back().getContents();
998  if (!CoverageMappingOrErr)
999  return CoverageMappingOrErr.takeError();
1000  StringRef CoverageMapping = CoverageMappingOrErr.get();
1001 
1002  InstrProfSymtab ProfileNames;
1003  std::vector<SectionRef> NamesSectionRefs = *NamesSection;
1004  if (NamesSectionRefs.size() != 1)
1005  return make_error<CoverageMapError>(coveragemap_error::malformed);
1006  if (Error E = ProfileNames.create(NamesSectionRefs.back()))
1007  return std::move(E);
1008 
1009  // Look for the coverage records section (Version4 only).
1010  auto CoverageRecordsSections =
1011  lookupSections(*OF, getInstrProfSectionName(IPSK_covfun, ObjFormat,
1012  /*AddSegmentInfo=*/false));
1013 
1015  if (auto E = CoverageRecordsSections.takeError()) {
1017  FuncRecords = MemoryBuffer::getMemBuffer("");
1018  } else {
1019  // Compute the FuncRecordsBuffer of the buffer, taking into account the
1020  // padding between each record, and making sure the first block is aligned
1021  // in memory to maintain consistency between buffer address and size
1022  // alignment.
1023  const Align RecordAlignment(8);
1024  uint64_t FuncRecordsSize = 0;
1025  for (SectionRef Section : *CoverageRecordsSections) {
1026  auto CoverageRecordsOrErr = Section.getContents();
1027  if (!CoverageRecordsOrErr)
1028  return CoverageRecordsOrErr.takeError();
1029  FuncRecordsSize += alignTo(CoverageRecordsOrErr->size(), RecordAlignment);
1030  }
1031  auto WritableBuffer =
1033  char *FuncRecordsBuffer = WritableBuffer->getBufferStart();
1034  assert(isAddrAligned(RecordAlignment, FuncRecordsBuffer) &&
1035  "Allocated memory is correctly aligned");
1036 
1037  for (SectionRef Section : *CoverageRecordsSections) {
1038  auto CoverageRecordsOrErr = Section.getContents();
1039  if (!CoverageRecordsOrErr)
1040  return CoverageRecordsOrErr.takeError();
1041  const auto &CoverageRecords = CoverageRecordsOrErr.get();
1042  FuncRecordsBuffer = std::copy(CoverageRecords.begin(),
1043  CoverageRecords.end(), FuncRecordsBuffer);
1044  FuncRecordsBuffer =
1045  std::fill_n(FuncRecordsBuffer,
1046  alignAddr(FuncRecordsBuffer, RecordAlignment) -
1047  (uintptr_t)FuncRecordsBuffer,
1048  '\0');
1049  }
1050  assert(FuncRecordsBuffer == WritableBuffer->getBufferEnd() &&
1051  "consistent init");
1052  FuncRecords = std::move(WritableBuffer);
1053  }
1054 
1056  CoverageMapping, std::move(FuncRecords), std::move(ProfileNames),
1057  BytesInAddress, Endian, CompilationDir);
1058 }
1059 
1060 /// Determine whether \p Arch is invalid or empty, given \p Bin.
1061 static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch) {
1062  // If we have a universal binary and Arch doesn't identify any of its slices,
1063  // it's user error.
1064  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin)) {
1065  for (auto &ObjForArch : Universal->objects())
1066  if (Arch == ObjForArch.getArchFlagName())
1067  return false;
1068  return true;
1069  }
1070  return false;
1071 }
1072 
1075  MemoryBufferRef ObjectBuffer, StringRef Arch,
1076  SmallVectorImpl<std::unique_ptr<MemoryBuffer>> &ObjectFileBuffers,
1077  StringRef CompilationDir) {
1078  std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
1079 
1080  if (ObjectBuffer.getBuffer().startswith(TestingFormatMagic)) {
1081  // This is a special format used for testing.
1082  auto ReaderOrErr =
1083  loadTestingFormat(ObjectBuffer.getBuffer(), CompilationDir);
1084  if (!ReaderOrErr)
1085  return ReaderOrErr.takeError();
1086  Readers.push_back(std::move(ReaderOrErr.get()));
1087  return std::move(Readers);
1088  }
1089 
1090  auto BinOrErr = createBinary(ObjectBuffer);
1091  if (!BinOrErr)
1092  return BinOrErr.takeError();
1093  std::unique_ptr<Binary> Bin = std::move(BinOrErr.get());
1094 
1095  if (isArchSpecifierInvalidOrMissing(Bin.get(), Arch))
1096  return make_error<CoverageMapError>(
1098 
1099  // MachO universal binaries which contain archives need to be treated as
1100  // archives, not as regular binaries.
1101  if (auto *Universal = dyn_cast<MachOUniversalBinary>(Bin.get())) {
1102  for (auto &ObjForArch : Universal->objects()) {
1103  // Skip slices within the universal binary which target the wrong arch.
1104  std::string ObjArch = ObjForArch.getArchFlagName();
1105  if (Arch != ObjArch)
1106  continue;
1107 
1108  auto ArchiveOrErr = ObjForArch.getAsArchive();
1109  if (!ArchiveOrErr) {
1110  // If this is not an archive, try treating it as a regular object.
1111  consumeError(ArchiveOrErr.takeError());
1112  break;
1113  }
1114 
1116  ArchiveOrErr.get()->getMemoryBufferRef(), Arch, ObjectFileBuffers,
1117  CompilationDir);
1118  }
1119  }
1120 
1121  // Load coverage out of archive members.
1122  if (auto *Ar = dyn_cast<Archive>(Bin.get())) {
1123  Error Err = Error::success();
1124  for (auto &Child : Ar->children(Err)) {
1125  Expected<MemoryBufferRef> ChildBufOrErr = Child.getMemoryBufferRef();
1126  if (!ChildBufOrErr)
1127  return ChildBufOrErr.takeError();
1128 
1129  auto ChildReadersOrErr = BinaryCoverageReader::create(
1130  ChildBufOrErr.get(), Arch, ObjectFileBuffers, CompilationDir);
1131  if (!ChildReadersOrErr)
1132  return ChildReadersOrErr.takeError();
1133  for (auto &Reader : ChildReadersOrErr.get())
1134  Readers.push_back(std::move(Reader));
1135  }
1136  if (Err)
1137  return std::move(Err);
1138 
1139  // Thin archives reference object files outside of the archive file, i.e.
1140  // files which reside in memory not owned by the caller. Transfer ownership
1141  // to the caller.
1142  if (Ar->isThin())
1143  for (auto &Buffer : Ar->takeThinBuffers())
1144  ObjectFileBuffers.push_back(std::move(Buffer));
1145 
1146  return std::move(Readers);
1147  }
1148 
1149  auto ReaderOrErr = loadBinaryFormat(std::move(Bin), Arch, CompilationDir);
1150  if (!ReaderOrErr)
1151  return ReaderOrErr.takeError();
1152  Readers.push_back(std::move(ReaderOrErr.get()));
1153  return std::move(Readers);
1154 }
1155 
1157  if (CurrentRecord >= MappingRecords.size())
1158  return make_error<CoverageMapError>(coveragemap_error::eof);
1159 
1160  FunctionsFilenames.clear();
1161  Expressions.clear();
1162  MappingRegions.clear();
1163  auto &R = MappingRecords[CurrentRecord];
1164  auto F = makeArrayRef(Filenames).slice(R.FilenamesBegin, R.FilenamesSize);
1165  RawCoverageMappingReader Reader(R.CoverageMapping, F, FunctionsFilenames,
1166  Expressions, MappingRegions);
1167  if (auto Err = Reader.read())
1168  return Err;
1169 
1170  Record.FunctionName = R.FunctionName;
1171  Record.FunctionHash = R.FunctionHash;
1172  Record.Filenames = FunctionsFilenames;
1173  Record.Expressions = Expressions;
1174  Record.MappingRegions = MappingRegions;
1175 
1176  ++CurrentRecord;
1177  return Error::success();
1178 }
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:156
Compression.h
llvm::compression::zlib::decompress
Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
Definition: Compression.cpp:123
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FilenamesBegin
size_t FilenamesBegin
Definition: CoverageMappingReader.h:171
FilenameRange::Length
unsigned Length
Definition: CoverageMappingReader.cpp:485
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::coverage::Version4
@ Version4
Definition: CoverageMapping.h:1003
TestingFormatMagic
static const char * TestingFormatMagic
Definition: CoverageMappingReader.cpp:825
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:483
llvm::coverage::coveragemap_error::no_data_found
@ no_data_found
llvm::coverage::Version3
@ Version3
Definition: CoverageMapping.h:1001
llvm::coverage::CounterExpression::ExprKind
ExprKind
Definition: CoverageMapping.h:150
llvm::coverage::CovMapVersion
CovMapVersion
Definition: CoverageMapping.h:993
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
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
Statistic.h
llvm::IndexedInstrProf::ComputeHash
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:1060
Path.h
llvm::sys::path::is_absolute
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:671
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::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::coverage::BinaryCoverageReader::readNextRecord
Error readNextRecord(CoverageMappingRecord &Record) override
Definition: CoverageMappingReader.cpp:1156
FilenameRange::isInvalid
bool isInvalid() const
Definition: CoverageMappingReader.cpp:491
Error.h
isArchSpecifierInvalidOrMissing
static bool isArchSpecifierInvalidOrMissing(Binary *Bin, StringRef Arch)
Determine whether Arch is invalid or empty, given Bin.
Definition: CoverageMappingReader.cpp:1061
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:188
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:783
llvm::Optional
Definition: APInt.h:33
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::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::coverage::CounterExpression::Add
@ Add
Definition: CoverageMapping.h:150
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
STLExtras.h
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::WritableMemoryBuffer::getNewUninitMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="", Optional< Align > Alignment=std::nullopt)
Allocate a new MemoryBuffer of the specified size that is not initialized.
Definition: MemoryBuffer.cpp:296
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::CoverageMapping
StringRef CoverageMapping
Definition: CoverageMappingReader.h:170
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
llvm::coverage::coveragemap_error::unsupported_version
@ unsupported_version
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1042
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:828
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:265
llvm::lltok::equal
@ equal
Definition: LLToken.h:25
llvm::SubDirectoryType::Bin
@ Bin
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:456
llvm::dwarf::toStringRef
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:193
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::get
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Definition: PointerIntPair.h:234
readULEB128
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
Definition: WasmObjectFile.cpp:102
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
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:994
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord
Definition: CoverageMappingReader.h:166
llvm::coverage::CurrentVersion
@ CurrentVersion
Definition: CoverageMapping.h:1010
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:120
Error.h
llvm::coverage::RawCoverageMappingDummyChecker::isDummy
Expected< bool > isDummy()
Definition: CoverageMappingReader.cpp:418
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Definition: StringRef.h:260
llvm::coverage::Version5
@ Version5
Definition: CoverageMapping.h:1005
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:92
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:642
llvm::StringRef::data
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:1056
llvm::coverage::RawCoverageReader::readSize
Error readSize(uint64_t &Result)
Definition: CoverageMappingReader.cpp:84
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:137
llvm::coverage::BinaryCoverageReader::ProfileMappingRecord::FunctionHash
uint64_t FunctionHash
Definition: CoverageMappingReader.h:169
llvm::compression::zlib::isAvailable
bool isAvailable()
Definition: Compression.cpp:105
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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:1074
llvm::SmallString< 256 >
llvm::coverage::coveragemap_error::truncated
@ truncated
llvm::coverage::RawCoverageMappingReader
Reader for the raw coverage mapping data.
Definition: CoverageMappingReader.h:132
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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::sys::path::remove_dots
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
Definition: Path.cpp:715
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:915
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::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:971
ObjectFile.h
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
llvm::Record
Definition: Record.h:1574
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:1715
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:204
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::coverage::Counter::EncodingCounterTagAndExpansionRegionTagBits
static const unsigned EncodingCounterTagAndExpansionRegionTagBits
Definition: CoverageMapping.h:98
llvm::coverage::RawCoverageMappingReader::read
Error read()
Definition: CoverageMappingReader.cpp:350
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
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:566
isCoverageMappingDummy
static Expected< bool > isCoverageMappingDummy(uint64_t Hash, StringRef Mapping)
Definition: CoverageMappingReader.cpp:474
uint32_t
EncodingExpansionRegionBit
static const unsigned EncodingExpansionRegionBit
Definition: CoverageMappingReader.cpp:225
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:65
Archive.h
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 size_t size() const
size - Get the string size.
Definition: StringRef.h:137
llvm::coverage::CounterMappingRegion::RegionKind
RegionKind
Definition: CoverageMapping.h:209
llvm::coverage::RawCoverageReader::readIntMax
Error readIntMax(uint64_t &Result, uint64_t MaxPlus1)
Definition: CoverageMappingReader.cpp:76
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:446
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:84
FilenameRange::markInvalid
void markInvalid()
Definition: CoverageMappingReader.cpp:490
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::isAddrAligned
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition: Alignment.h:151
Casting.h
FilenameRange::FilenameRange
FilenameRange(unsigned StartingIndex, unsigned Length)
Definition: CoverageMappingReader.cpp:487
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:475
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:464
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:212
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:596
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:484
SmallVector.h
COFF.h
N
#define N
llvm::coverage::Version6
@ Version6
Definition: CoverageMapping.h:1008
Expressions
gvn Early GVN Hoisting of Expressions
Definition: GVNHoist.cpp:1265
llvm::coverage::Version2
@ Version2
Definition: CoverageMapping.h:998
llvm::coverage::Counter::EncodingTagMask
static const unsigned EncodingTagMask
Definition: CoverageMapping.h:97
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:45
llvm::support::endianness
endianness
Definition: Endian.h:27
loadTestingFormat
static Expected< std::unique_ptr< BinaryCoverageReader > > loadTestingFormat(StringRef Data, StringRef CompilationDir)
Definition: CoverageMappingReader.cpp:864
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
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:956
llvm::StringRef::str
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:221
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:931
readString
static StringRef readString(WasmObjectFile::ReadContext &Ctx)
Definition: WasmObjectFile.cpp:112
raw_ostream.h
llvm::coverage::RawCoverageFilenamesReader::read
Error read(CovMapVersion Version)
Definition: CoverageMappingReader.cpp:101
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:965
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:92