LLVM  7.0.0svn
CoverageMapping.h
Go to the documentation of this file.
1 //===- CoverageMapping.h - Code coverage mapping support --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Code coverage mapping data is generated by clang and read by
11 // llvm-cov to show code coverage statistics for a file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
16 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/Hashing.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/ADT/iterator.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/Error.h"
33 #include <cassert>
34 #include <cstdint>
35 #include <iterator>
36 #include <memory>
37 #include <string>
38 #include <system_error>
39 #include <tuple>
40 #include <utility>
41 #include <vector>
42 
43 namespace llvm {
44 
45 class IndexedInstrProfReader;
46 
47 namespace coverage {
48 
51 
52 enum class coveragemap_error {
53  success = 0,
54  eof,
57  truncated,
58  malformed
59 };
60 
62 
63 inline std::error_code make_error_code(coveragemap_error E) {
64  return std::error_code(static_cast<int>(E), coveragemap_category());
65 }
66 
67 class CoverageMapError : public ErrorInfo<CoverageMapError> {
68 public:
70  assert(Err != coveragemap_error::success && "Not an error");
71  }
72 
73  std::string message() const override;
74 
75  void log(raw_ostream &OS) const override { OS << message(); }
76 
77  std::error_code convertToErrorCode() const override {
78  return make_error_code(Err);
79  }
80 
81  coveragemap_error get() const { return Err; }
82 
83  static char ID;
84 
85 private:
87 };
88 
89 /// A Counter is an abstract value that describes how to compute the
90 /// execution count for a region of code using the collected profile count data.
91 struct Counter {
92  enum CounterKind { Zero, CounterValueReference, Expression };
93  static const unsigned EncodingTagBits = 2;
94  static const unsigned EncodingTagMask = 0x3;
95  static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
96  EncodingTagBits + 1;
97 
98 private:
99  CounterKind Kind = Zero;
100  unsigned ID = 0;
101 
102  Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
103 
104 public:
105  Counter() = default;
106 
107  CounterKind getKind() const { return Kind; }
108 
109  bool isZero() const { return Kind == Zero; }
110 
111  bool isExpression() const { return Kind == Expression; }
112 
113  unsigned getCounterID() const { return ID; }
114 
115  unsigned getExpressionID() const { return ID; }
116 
117  friend bool operator==(const Counter &LHS, const Counter &RHS) {
118  return LHS.Kind == RHS.Kind && LHS.ID == RHS.ID;
119  }
120 
121  friend bool operator!=(const Counter &LHS, const Counter &RHS) {
122  return !(LHS == RHS);
123  }
124 
125  friend bool operator<(const Counter &LHS, const Counter &RHS) {
126  return std::tie(LHS.Kind, LHS.ID) < std::tie(RHS.Kind, RHS.ID);
127  }
128 
129  /// Return the counter that represents the number zero.
130  static Counter getZero() { return Counter(); }
131 
132  /// Return the counter that corresponds to a specific profile counter.
133  static Counter getCounter(unsigned CounterId) {
134  return Counter(CounterValueReference, CounterId);
135  }
136 
137  /// Return the counter that corresponds to a specific addition counter
138  /// expression.
139  static Counter getExpression(unsigned ExpressionId) {
140  return Counter(Expression, ExpressionId);
141  }
142 };
143 
144 /// A Counter expression is a value that represents an arithmetic operation
145 /// with two counters.
147  enum ExprKind { Subtract, Add };
149  Counter LHS, RHS;
150 
152  : Kind(Kind), LHS(LHS), RHS(RHS) {}
153 };
154 
155 /// A Counter expression builder is used to construct the counter expressions.
156 /// It avoids unnecessary duplication and simplifies algebraic expressions.
158  /// A list of all the counter expressions
159  std::vector<CounterExpression> Expressions;
160 
161  /// A lookup table for the index of a given expression.
162  DenseMap<CounterExpression, unsigned> ExpressionIndices;
163 
164  /// Return the counter which corresponds to the given expression.
165  ///
166  /// If the given expression is already stored in the builder, a counter
167  /// that references that expression is returned. Otherwise, the given
168  /// expression is added to the builder's collection of expressions.
169  Counter get(const CounterExpression &E);
170 
171  /// Represents a term in a counter expression tree.
172  struct Term {
173  unsigned CounterID;
174  int Factor;
175 
176  Term(unsigned CounterID, int Factor)
177  : CounterID(CounterID), Factor(Factor) {}
178  };
179 
180  /// Gather the terms of the expression tree for processing.
181  ///
182  /// This collects each addition and subtraction referenced by the counter into
183  /// a sequence that can be sorted and combined to build a simplified counter
184  /// expression.
185  void extractTerms(Counter C, int Sign, SmallVectorImpl<Term> &Terms);
186 
187  /// Simplifies the given expression tree
188  /// by getting rid of algebraically redundant operations.
189  Counter simplify(Counter ExpressionTree);
190 
191 public:
193 
194  /// Return a counter that represents the expression that adds LHS and RHS.
195  Counter add(Counter LHS, Counter RHS);
196 
197  /// Return a counter that represents the expression that subtracts RHS from
198  /// LHS.
199  Counter subtract(Counter LHS, Counter RHS);
200 };
201 
202 using LineColPair = std::pair<unsigned, unsigned>;
203 
204 /// A Counter mapping region associates a source range with a specific counter.
206  enum RegionKind {
207  /// A CodeRegion associates some code with a counter
209 
210  /// An ExpansionRegion represents a file expansion region that associates
211  /// a source range with the expansion of a virtual source file, such as
212  /// for a macro instantiation or #include file.
214 
215  /// A SkippedRegion represents a source range with code that was skipped
216  /// by a preprocessor or similar means.
218 
219  /// A GapRegion is like a CodeRegion, but its count is only set as the
220  /// line execution count when its the only region in the line.
221  GapRegion
222  };
223 
225  unsigned FileID, ExpandedFileID;
226  unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
228 
229  CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID,
230  unsigned LineStart, unsigned ColumnStart,
231  unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
232  : Count(Count), FileID(FileID), ExpandedFileID(ExpandedFileID),
233  LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd),
234  ColumnEnd(ColumnEnd), Kind(Kind) {}
235 
236  static CounterMappingRegion
237  makeRegion(Counter Count, unsigned FileID, unsigned LineStart,
238  unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
239  return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
240  LineEnd, ColumnEnd, CodeRegion);
241  }
242 
243  static CounterMappingRegion
244  makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart,
245  unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
246  return CounterMappingRegion(Counter(), FileID, ExpandedFileID, LineStart,
247  ColumnStart, LineEnd, ColumnEnd,
248  ExpansionRegion);
249  }
250 
251  static CounterMappingRegion
252  makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart,
253  unsigned LineEnd, unsigned ColumnEnd) {
254  return CounterMappingRegion(Counter(), FileID, 0, LineStart, ColumnStart,
255  LineEnd, ColumnEnd, SkippedRegion);
256  }
257 
258  static CounterMappingRegion
259  makeGapRegion(Counter Count, unsigned FileID, unsigned LineStart,
260  unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
261  return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
262  LineEnd, (1U << 31) | ColumnEnd, GapRegion);
263  }
264 
265  inline LineColPair startLoc() const {
266  return LineColPair(LineStart, ColumnStart);
267  }
268 
269  inline LineColPair endLoc() const { return LineColPair(LineEnd, ColumnEnd); }
270 };
271 
272 /// Associates a source range with an execution count.
274  uint64_t ExecutionCount;
275 
276  CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
277  : CounterMappingRegion(R), ExecutionCount(ExecutionCount) {}
278 };
279 
280 /// A Counter mapping context is used to connect the counters, expressions
281 /// and the obtained counter values.
284  ArrayRef<uint64_t> CounterValues;
285 
286 public:
288  ArrayRef<uint64_t> CounterValues = None)
289  : Expressions(Expressions), CounterValues(CounterValues) {}
290 
291  void setCounts(ArrayRef<uint64_t> Counts) { CounterValues = Counts; }
292 
293  void dump(const Counter &C, raw_ostream &OS) const;
294  void dump(const Counter &C) const { dump(C, dbgs()); }
295 
296  /// Return the number of times that a region of code associated with this
297  /// counter was executed.
298  Expected<int64_t> evaluate(const Counter &C) const;
299 };
300 
301 /// Code coverage information for a single function.
303  /// Raw function name.
304  std::string Name;
305  /// Associated files.
306  std::vector<std::string> Filenames;
307  /// Regions in the function along with their counts.
308  std::vector<CountedRegion> CountedRegions;
309  /// The number of times this function was executed.
310  uint64_t ExecutionCount;
311 
313  : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
314 
315  FunctionRecord(FunctionRecord &&FR) = default;
316  FunctionRecord &operator=(FunctionRecord &&) = default;
317 
318  void pushRegion(CounterMappingRegion Region, uint64_t Count) {
319  if (CountedRegions.empty())
320  ExecutionCount = Count;
321  CountedRegions.emplace_back(Region, Count);
322  }
323 };
324 
325 /// Iterator over Functions, optionally filtered to a single file.
327  : public iterator_facade_base<FunctionRecordIterator,
328  std::forward_iterator_tag, FunctionRecord> {
329  ArrayRef<FunctionRecord> Records;
331  StringRef Filename;
332 
333  /// Skip records whose primary file is not \c Filename.
334  void skipOtherFiles();
335 
336 public:
338  StringRef Filename = "")
339  : Records(Records_), Current(Records.begin()), Filename(Filename) {
340  skipOtherFiles();
341  }
342 
343  FunctionRecordIterator() : Current(Records.begin()) {}
344 
345  bool operator==(const FunctionRecordIterator &RHS) const {
346  return Current == RHS.Current && Filename == RHS.Filename;
347  }
348 
349  const FunctionRecord &operator*() const { return *Current; }
350 
352  assert(Current != Records.end() && "incremented past end");
353  ++Current;
354  skipOtherFiles();
355  return *this;
356  }
357 };
358 
359 /// Coverage information for a macro expansion or #included file.
360 ///
361 /// When covered code has pieces that can be expanded for more detail, such as a
362 /// preprocessor macro use and its definition, these are represented as
363 /// expansions whose coverage can be looked up independently.
365  /// The abstract file this expansion covers.
366  unsigned FileID;
367  /// The region that expands to this record.
369  /// Coverage for the expansion.
371 
373  const FunctionRecord &Function)
374  : FileID(Region.ExpandedFileID), Region(Region), Function(Function) {}
375 };
376 
377 /// The execution count information starting at a point in a file.
378 ///
379 /// A sequence of CoverageSegments gives execution counts for a file in format
380 /// that's simple to iterate through for processing.
382  /// The line where this segment begins.
383  unsigned Line;
384  /// The column where this segment begins.
385  unsigned Col;
386  /// The execution count, or zero if no count was recorded.
387  uint64_t Count;
388  /// When false, the segment was uninstrumented or skipped.
389  bool HasCount;
390  /// Whether this enters a new region or returns to a previous count.
392  /// Whether this enters a gap region.
394 
395  CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
396  : Line(Line), Col(Col), Count(0), HasCount(false),
397  IsRegionEntry(IsRegionEntry), IsGapRegion(false) {}
398 
399  CoverageSegment(unsigned Line, unsigned Col, uint64_t Count,
400  bool IsRegionEntry, bool IsGapRegion = false)
401  : Line(Line), Col(Col), Count(Count), HasCount(true),
402  IsRegionEntry(IsRegionEntry), IsGapRegion(IsGapRegion) {}
403 
404  friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) {
405  return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry,
406  L.IsGapRegion) == std::tie(R.Line, R.Col, R.Count,
407  R.HasCount, R.IsRegionEntry,
408  R.IsGapRegion);
409  }
410 };
411 
412 /// An instantiation group contains a \c FunctionRecord list, such that each
413 /// record corresponds to a distinct instantiation of the same function.
414 ///
415 /// Note that it's possible for a function to have more than one instantiation
416 /// (consider C++ template specializations or static inline functions).
418  friend class CoverageMapping;
419 
420  unsigned Line;
421  unsigned Col;
422  std::vector<const FunctionRecord *> Instantiations;
423 
424  InstantiationGroup(unsigned Line, unsigned Col,
425  std::vector<const FunctionRecord *> Instantiations)
426  : Line(Line), Col(Col), Instantiations(std::move(Instantiations)) {}
427 
428 public:
429  InstantiationGroup(const InstantiationGroup &) = delete;
431 
432  /// Get the number of instantiations in this group.
433  size_t size() const { return Instantiations.size(); }
434 
435  /// Get the line where the common function was defined.
436  unsigned getLine() const { return Line; }
437 
438  /// Get the column where the common function was defined.
439  unsigned getColumn() const { return Col; }
440 
441  /// Check if the instantiations in this group have a common mangled name.
442  bool hasName() const {
443  for (unsigned I = 1, E = Instantiations.size(); I < E; ++I)
444  if (Instantiations[I]->Name != Instantiations[0]->Name)
445  return false;
446  return true;
447  }
448 
449  /// Get the common mangled name for instantiations in this group.
450  StringRef getName() const {
451  assert(hasName() && "Instantiations don't have a shared name");
452  return Instantiations[0]->Name;
453  }
454 
455  /// Get the total execution count of all instantiations in this group.
456  uint64_t getTotalExecutionCount() const {
457  uint64_t Count = 0;
458  for (const FunctionRecord *F : Instantiations)
459  Count += F->ExecutionCount;
460  return Count;
461  }
462 
463  /// Get the instantiations in this group.
465  return Instantiations;
466  }
467 };
468 
469 /// Coverage information to be processed or displayed.
470 ///
471 /// This represents the coverage of an entire file, expansion, or function. It
472 /// provides a sequence of CoverageSegments to iterate through, as well as the
473 /// list of expansions that can be further processed.
475  friend class CoverageMapping;
476 
477  std::string Filename;
478  std::vector<CoverageSegment> Segments;
479  std::vector<ExpansionRecord> Expansions;
480 
481 public:
482  CoverageData() = default;
483 
484  CoverageData(StringRef Filename) : Filename(Filename) {}
485 
486  /// Get the name of the file this data covers.
487  StringRef getFilename() const { return Filename; }
488 
489  /// Get an iterator over the coverage segments for this object. The segments
490  /// are guaranteed to be uniqued and sorted by location.
491  std::vector<CoverageSegment>::const_iterator begin() const {
492  return Segments.begin();
493  }
494 
495  std::vector<CoverageSegment>::const_iterator end() const {
496  return Segments.end();
497  }
498 
499  bool empty() const { return Segments.empty(); }
500 
501  /// Expansions that can be further processed.
502  ArrayRef<ExpansionRecord> getExpansions() const { return Expansions; }
503 };
504 
505 /// The mapping of profile information to coverage data.
506 ///
507 /// This is the main interface to get coverage information, using a profile to
508 /// fill out execution counts.
510  DenseMap<size_t, DenseSet<size_t>> RecordProvenance;
511  std::vector<FunctionRecord> Functions;
512  std::vector<std::pair<std::string, uint64_t>> FuncHashMismatches;
513  std::vector<std::pair<std::string, uint64_t>> FuncCounterMismatches;
514 
515  CoverageMapping() = default;
516 
517  /// Add a function record corresponding to \p Record.
518  Error loadFunctionRecord(const CoverageMappingRecord &Record,
519  IndexedInstrProfReader &ProfileReader);
520 
521 public:
522  CoverageMapping(const CoverageMapping &) = delete;
523  CoverageMapping &operator=(const CoverageMapping &) = delete;
524 
525  /// Load the coverage mapping using the given readers.
527  load(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
528  IndexedInstrProfReader &ProfileReader);
529 
530  /// Load the coverage mapping from the given object files and profile. If
531  /// \p Arches is non-empty, it must specify an architecture for each object.
533  load(ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
534  ArrayRef<StringRef> Arches = None);
535 
536  /// The number of functions that couldn't have their profiles mapped.
537  ///
538  /// This is a count of functions whose profile is out of date or otherwise
539  /// can't be associated with any coverage information.
540  unsigned getMismatchedCount() const {
541  return FuncHashMismatches.size() + FuncCounterMismatches.size();
542  }
543 
544  /// A hash mismatch occurs when a profile record for a symbol does not have
545  /// the same hash as a coverage mapping record for the same symbol. This
546  /// returns a list of hash mismatches, where each mismatch is a pair of the
547  /// symbol name and its coverage mapping hash.
549  return FuncHashMismatches;
550  }
551 
552  /// A counter mismatch occurs when there is an error when evaluating the
553  /// counter expressions in a coverage mapping record. This returns a list of
554  /// counter mismatches, where each mismatch is a pair of the symbol name and
555  /// the number of valid evaluated counter expressions.
557  return FuncCounterMismatches;
558  }
559 
560  /// Returns a lexicographically sorted, unique list of files that are
561  /// covered.
562  std::vector<StringRef> getUniqueSourceFiles() const;
563 
564  /// Get the coverage for a particular file.
565  ///
566  /// The given filename must be the name as recorded in the coverage
567  /// information. That is, only names returned from getUniqueSourceFiles will
568  /// yield a result.
569  CoverageData getCoverageForFile(StringRef Filename) const;
570 
571  /// Get the coverage for a particular function.
572  CoverageData getCoverageForFunction(const FunctionRecord &Function) const;
573 
574  /// Get the coverage for an expansion within a coverage set.
575  CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const;
576 
577  /// Gets all of the functions covered by this profile.
579  return make_range(FunctionRecordIterator(Functions),
581  }
582 
583  /// Gets all of the functions in a particular file.
585  getCoveredFunctions(StringRef Filename) const {
586  return make_range(FunctionRecordIterator(Functions, Filename),
588  }
589 
590  /// Get the list of function instantiation groups in a particular file.
591  ///
592  /// Every instantiation group in a program is attributed to exactly one file:
593  /// the file in which the definition for the common function begins.
594  std::vector<InstantiationGroup>
595  getInstantiationGroups(StringRef Filename) const;
596 };
597 
598 /// Coverage statistics for a single line.
600  uint64_t ExecutionCount;
601  bool HasMultipleRegions;
602  bool Mapped;
603  unsigned Line;
605  const CoverageSegment *WrappedSegment;
606 
607  friend class LineCoverageIterator;
608  LineCoverageStats() = default;
609 
610 public:
612  const CoverageSegment *WrappedSegment, unsigned Line);
613 
614  uint64_t getExecutionCount() const { return ExecutionCount; }
615 
616  bool hasMultipleRegions() const { return HasMultipleRegions; }
617 
618  bool isMapped() const { return Mapped; }
619 
620  unsigned getLine() const { return Line; }
621 
623  return LineSegments;
624  }
625 
626  const CoverageSegment *getWrappedSegment() const { return WrappedSegment; }
627 };
628 
629 /// An iterator over the \c LineCoverageStats objects for lines described by
630 /// a \c CoverageData instance.
632  : public iterator_facade_base<
633  LineCoverageIterator, std::forward_iterator_tag, LineCoverageStats> {
634 public:
636  : LineCoverageIterator(CD, CD.begin()->Line) {}
637 
638  LineCoverageIterator(const CoverageData &CD, unsigned Line)
639  : CD(CD), WrappedSegment(nullptr), Next(CD.begin()), Ended(false),
640  Line(Line), Segments(), Stats() {
641  this->operator++();
642  }
643 
644  LineCoverageIterator &operator=(const LineCoverageIterator &R) = default;
645 
646  bool operator==(const LineCoverageIterator &R) const {
647  return &CD == &R.CD && Next == R.Next && Ended == R.Ended;
648  }
649 
650  const LineCoverageStats &operator*() const { return Stats; }
651 
653 
654  LineCoverageIterator &operator++();
655 
657  auto EndIt = *this;
658  EndIt.Next = CD.end();
659  EndIt.Ended = true;
660  return EndIt;
661  }
662 
663 private:
664  const CoverageData &CD;
665  const CoverageSegment *WrappedSegment;
666  std::vector<CoverageSegment>::const_iterator Next;
667  bool Ended;
668  unsigned Line;
671 };
672 
673 /// Get a \c LineCoverageIterator range for the lines described by \p CD.
676  auto Begin = LineCoverageIterator(CD);
677  auto End = Begin.getEnd();
678  return make_range(Begin, End);
679 }
680 
681 // Profile coverage map has the following layout:
682 // [CoverageMapFileHeader]
683 // [ArrayStart]
684 // [CovMapFunctionRecord]
685 // [CovMapFunctionRecord]
686 // ...
687 // [ArrayEnd]
688 // [Encoded Region Mapping Data]
690 template <class IntPtrT> struct CovMapFunctionRecordV1 {
691 #define COVMAP_V1
692 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
694 #undef COVMAP_V1
695 
696  // Return the structural hash associated with the function.
697  template <support::endianness Endian> uint64_t getFuncHash() const {
698  return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
699  }
700 
701  // Return the coverage map data size for the funciton.
702  template <support::endianness Endian> uint32_t getDataSize() const {
703  return support::endian::byte_swap<uint32_t, Endian>(DataSize);
704  }
705 
706  // Return function lookup key. The value is consider opaque.
707  template <support::endianness Endian> IntPtrT getFuncNameRef() const {
708  return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
709  }
710 
711  // Return the PGO name of the function */
712  template <support::endianness Endian>
713  Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
714  IntPtrT NameRef = getFuncNameRef<Endian>();
715  uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
716  FuncName = ProfileNames.getFuncName(NameRef, NameS);
717  if (NameS && FuncName.empty())
718  return make_error<CoverageMapError>(coveragemap_error::malformed);
719  return Error::success();
720  }
721 };
722 
724 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
726 
727  // Return the structural hash associated with the function.
728  template <support::endianness Endian> uint64_t getFuncHash() const {
729  return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
730  }
731 
732  // Return the coverage map data size for the funciton.
733  template <support::endianness Endian> uint32_t getDataSize() const {
734  return support::endian::byte_swap<uint32_t, Endian>(DataSize);
735  }
736 
737  // Return function lookup key. The value is consider opaque.
738  template <support::endianness Endian> uint64_t getFuncNameRef() const {
739  return support::endian::byte_swap<uint64_t, Endian>(NameRef);
740  }
741 
742  // Return the PGO name of the function */
743  template <support::endianness Endian>
744  Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
745  uint64_t NameRef = getFuncNameRef<Endian>();
746  FuncName = ProfileNames.getFuncName(NameRef);
747  return Error::success();
748  }
749 };
750 
751 // Per module coverage mapping data header, i.e. CoverageMapFileHeader
752 // documented above.
753 struct CovMapHeader {
754 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
756  template <support::endianness Endian> uint32_t getNRecords() const {
757  return support::endian::byte_swap<uint32_t, Endian>(NRecords);
758  }
759 
760  template <support::endianness Endian> uint32_t getFilenamesSize() const {
761  return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
762  }
763 
764  template <support::endianness Endian> uint32_t getCoverageSize() const {
765  return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
766  }
767 
768  template <support::endianness Endian> uint32_t getVersion() const {
769  return support::endian::byte_swap<uint32_t, Endian>(Version);
770  }
771 };
772 
774 
776  Version1 = 0,
777  // Function's name reference from CovMapFuncRecord is changed from raw
778  // name string pointer to MD5 to support name section compression. Name
779  // section is also compressed.
780  Version2 = 1,
781  // A new interpretation of the columnEnd field is added in order to mark
782  // regions as gap areas.
783  Version3 = 2,
784  // The current version is Version3
785  CurrentVersion = INSTR_PROF_COVMAP_VERSION
786 };
787 
788 template <int CovMapVersion, class IntPtrT> struct CovMapTraits {
790  using NameRefType = uint64_t;
791 };
792 
793 template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version1, IntPtrT> {
795  using NameRefType = IntPtrT;
796 };
797 
798 } // end namespace coverage
799 
800 /// Provide DenseMapInfo for CounterExpression
801 template<> struct DenseMapInfo<coverage::CounterExpression> {
803  using namespace coverage;
804 
805  return CounterExpression(CounterExpression::ExprKind::Subtract,
806  Counter::getCounter(~0U),
807  Counter::getCounter(~0U));
808  }
809 
811  using namespace coverage;
812 
813  return CounterExpression(CounterExpression::ExprKind::Add,
814  Counter::getCounter(~0U),
815  Counter::getCounter(~0U));
816  }
817 
818  static unsigned getHashValue(const coverage::CounterExpression &V) {
819  return static_cast<unsigned>(
821  V.RHS.getKind(), V.RHS.getCounterID()));
822  }
823 
824  static bool isEqual(const coverage::CounterExpression &LHS,
825  const coverage::CounterExpression &RHS) {
826  return LHS.Kind == RHS.Kind && LHS.LHS == RHS.LHS && LHS.RHS == RHS.RHS;
827  }
828 };
829 
830 } // end namespace llvm
831 
832 #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
const NoneType None
Definition: None.h:24
uint64_t CallInst * C
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:250
static coverage::CounterExpression getTombstoneKey()
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:411
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:241
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static Counter getZero()
Return the counter that represents the number zero.
unsigned getMismatchedCount() const
The number of functions that couldn&#39;t have their profiles mapped.
static CounterMappingRegion makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd)
void setCounts(ArrayRef< uint64_t > Counts)
CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static bool isEqual(const coverage::CounterExpression &LHS, const coverage::CounterExpression &RHS)
LineCoverageIterator(const CoverageData &CD)
std::error_code make_error_code(coveragemap_error E)
friend bool operator==(const Counter &LHS, const Counter &RHS)
const CoverageSegment * getWrappedSegment() const
ArrayRef< std::pair< std::string, uint64_t > > getCounterMismatches() const
A counter mismatch occurs when there is an error when evaluating the counter expressions in a coverag...
F(f)
void pushRegion(CounterMappingRegion Region, uint64_t Count)
unsigned getLine() const
Get the line where the common function was defined.
A Counter expression is a value that represents an arithmetic operation with two counters.
FunctionRecord(StringRef Name, ArrayRef< StringRef > Filenames)
uint64_t getTotalExecutionCount() const
Get the total execution count of all instantiations in this group.
ArrayRef< std::pair< std::string, uint64_t > > getHashMismatches() const
A hash mismatch occurs when a profile record for a symbol does not have the same hash as a coverage m...
void log(raw_ostream &OS) const override
Print an error message to an output stream.
friend bool operator!=(const Counter &LHS, const Counter &RHS)
friend bool operator<(const Counter &LHS, const Counter &RHS)
friend bool operator==(const CoverageSegment &L, const CoverageSegment &R)
gvn Early GVN Hoisting of Expressions
Definition: GVNHoist.cpp:1205
Iterator over Functions, optionally filtered to a single file.
const LineCoverageStats & operator*() const
Definition: BitVector.h:921
unsigned Col
The column where this segment begins.
ArrayRef< CounterExpression > getExpressions() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
An iterator over the LineCoverageStats objects for lines described by a CoverageData instance...
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
block placement Basic Block Placement Stats
CoverageSegment(unsigned Line, unsigned Col, uint64_t Count, bool IsRegionEntry, bool IsGapRegion=false)
const CountedRegion & Region
The region that expands to this record.
Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize)
Return function&#39;s PGO name from the function name&#39;s symbol address in the object file.
std::pair< unsigned, unsigned > LineColPair
#define LLVM_PACKED_START
Definition: Compiler.h:352
const FunctionRecord & operator*() const
Coverage information to be processed or displayed.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
ArrayRef< const FunctionRecord * > getInstantiations() const
Get the instantiations in this group.
CoverageMapError(coveragemap_error Err)
#define LLVM_PACKED_END
Definition: Compiler.h:353
static CounterMappingRegion makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd)
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
ArrayRef< const CoverageSegment * > getLineSegments() const
const std::error_category & coveragemap_category()
iterator_range< FunctionRecordIterator > getCoveredFunctions(StringRef Filename) const
Gets all of the functions in a particular file.
The execution count information starting at a point in a file.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
static CounterMappingRegion makeRegion(Counter Count, unsigned FileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd)
Coverage mapping information for a single function.
uint64_t ExecutionCount
The number of times this function was executed.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
An instantiation group contains a FunctionRecord list, such that each record corresponds to a distinc...
A CodeRegion associates some code with a counter.
A Counter mapping context is used to connect the counters, expressions and the obtained counter value...
A Counter mapping region associates a source range with a specific counter.
bool IsRegionEntry
Whether this enters a new region or returns to a previous count.
bool operator==(const FunctionRecordIterator &RHS) const
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
size_t size() const
Get the number of instantiations in this group.
CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
Associates a source range with an execution count.
static coverage::CounterExpression getEmptyKey()
static unsigned getHashValue(const coverage::CounterExpression &V)
CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
uint32_t getCoverageSize() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Code coverage information for a single function.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:321
hexagon bit simplify
iterator_range< FunctionRecordIterator > getCoveredFunctions() const
Gets all of the functions covered by this profile.
Coverage statistics for a single line.
FunctionRecordIterator(ArrayRef< FunctionRecord > Records_, StringRef Filename="")
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
bool operator==(const LineCoverageIterator &R) const
ExpansionRecord(const CountedRegion &Region, const FunctionRecord &Function)
iterator end() const
Definition: ArrayRef.h:138
uint64_t Count
The execution count, or zero if no count was recorded.
bool HasCount
When false, the segment was uninstrumented or skipped.
std::vector< CoverageSegment >::const_iterator begin() const
Get an iterator over the coverage segments for this object.
An ExpansionRegion represents a file expansion region that associates a source range with the expansi...
A SkippedRegion represents a source range with code that was skipped by a preprocessor or similar mea...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
A range adaptor for a pair of iterators.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const
std::vector< CoverageSegment >::const_iterator end() const
Base class for user error types.
Definition: Error.h:339
LineCoverageIterator getEnd() const
The mapping of profile information to coverage data.
unsigned Line
The line where this segment begins.
CoverageData(StringRef Filename)
Basic Alias true
void dump(const Counter &C) const
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
unsigned getExpressionID() const
CounterKind getKind() const
bool hasName() const
Check if the instantiations in this group have a common mangled name.
#define I(x, y, z)
Definition: MD5.cpp:58
CounterMappingContext(ArrayRef< CounterExpression > Expressions, ArrayRef< uint64_t > CounterValues=None)
std::vector< CountedRegion > CountedRegions
Regions in the function along with their counts.
std::string Name
Raw function name.
ArrayRef< ExpansionRecord > getExpansions() const
Expansions that can be further processed.
const unsigned Kind
unsigned FileID
The abstract file this expansion covers.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool IsGapRegion
Whether this enters a gap region.
unsigned getCounterID() const
std::vector< std::string > Filenames
Associated files.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef getName() const
Get the common mangled name for instantiations in this group.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
LineCoverageIterator(const CoverageData &CD, unsigned Line)
uint32_t getFilenamesSize() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Reader for the indexed binary instrprof format.
static Counter getExpression(unsigned ExpressionId)
Return the counter that corresponds to a specific addition counter expression.
A Counter expression builder is used to construct the counter expressions.
static iterator_range< LineCoverageIterator > getLineCoverageStats(const coverage::CoverageData &CD)
Get a LineCoverageIterator range for the lines described by CD.
A Counter is an abstract value that describes how to compute the execution count for a region of code...
unsigned getColumn() const
Get the column where the common function was defined.
const uint64_t Version
Definition: InstrProf.h:895
static CounterMappingRegion makeGapRegion(Counter Count, unsigned FileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd)
static Counter getCounter(unsigned CounterId)
Return the counter that corresponds to a specific profile counter.
const FunctionRecord & Function
Coverage for the expansion.
StringRef getFilename() const
Get the name of the file this data covers.
Coverage information for a macro expansion or #included file.
FunctionRecordIterator & operator++()