CoverageMapping.h revision 321369
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/Hashing.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/StringSet.h"
24#include "llvm/ADT/iterator.h"
25#include "llvm/ADT/iterator_range.h"
26#include "llvm/ProfileData/InstrProf.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/Endian.h"
30#include "llvm/Support/Error.h"
31#include "llvm/Support/raw_ostream.h"
32#include <cassert>
33#include <cstdint>
34#include <iterator>
35#include <memory>
36#include <string>
37#include <system_error>
38#include <tuple>
39#include <utility>
40#include <vector>
41
42namespace llvm {
43
44class IndexedInstrProfReader;
45
46namespace coverage {
47
48class CoverageMappingReader;
49struct CoverageMappingRecord;
50
51enum class coveragemap_error {
52  success = 0,
53  eof,
54  no_data_found,
55  unsupported_version,
56  truncated,
57  malformed
58};
59
60const std::error_category &coveragemap_category();
61
62inline std::error_code make_error_code(coveragemap_error E) {
63  return std::error_code(static_cast<int>(E), coveragemap_category());
64}
65
66class CoverageMapError : public ErrorInfo<CoverageMapError> {
67public:
68  CoverageMapError(coveragemap_error Err) : Err(Err) {
69    assert(Err != coveragemap_error::success && "Not an error");
70  }
71
72  std::string message() const override;
73
74  void log(raw_ostream &OS) const override { OS << message(); }
75
76  std::error_code convertToErrorCode() const override {
77    return make_error_code(Err);
78  }
79
80  coveragemap_error get() const { return Err; }
81
82  static char ID;
83
84private:
85  coveragemap_error Err;
86};
87
88/// \brief A Counter is an abstract value that describes how to compute the
89/// execution count for a region of code using the collected profile count data.
90struct Counter {
91  enum CounterKind { Zero, CounterValueReference, Expression };
92  static const unsigned EncodingTagBits = 2;
93  static const unsigned EncodingTagMask = 0x3;
94  static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
95      EncodingTagBits + 1;
96
97private:
98  CounterKind Kind = Zero;
99  unsigned ID = 0;
100
101  Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
102
103public:
104  Counter() = default;
105
106  CounterKind getKind() const { return Kind; }
107
108  bool isZero() const { return Kind == Zero; }
109
110  bool isExpression() const { return Kind == Expression; }
111
112  unsigned getCounterID() const { return ID; }
113
114  unsigned getExpressionID() const { return ID; }
115
116  friend bool operator==(const Counter &LHS, const Counter &RHS) {
117    return LHS.Kind == RHS.Kind && LHS.ID == RHS.ID;
118  }
119
120  friend bool operator!=(const Counter &LHS, const Counter &RHS) {
121    return !(LHS == RHS);
122  }
123
124  friend bool operator<(const Counter &LHS, const Counter &RHS) {
125    return std::tie(LHS.Kind, LHS.ID) < std::tie(RHS.Kind, RHS.ID);
126  }
127
128  /// \brief Return the counter that represents the number zero.
129  static Counter getZero() { return Counter(); }
130
131  /// \brief Return the counter that corresponds to a specific profile counter.
132  static Counter getCounter(unsigned CounterId) {
133    return Counter(CounterValueReference, CounterId);
134  }
135
136  /// \brief Return the counter that corresponds to a specific
137  /// addition counter expression.
138  static Counter getExpression(unsigned ExpressionId) {
139    return Counter(Expression, ExpressionId);
140  }
141};
142
143/// \brief A Counter expression is a value that represents an arithmetic
144/// operation with two counters.
145struct CounterExpression {
146  enum ExprKind { Subtract, Add };
147  ExprKind Kind;
148  Counter LHS, RHS;
149
150  CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
151      : Kind(Kind), LHS(LHS), RHS(RHS) {}
152};
153
154/// \brief A Counter expression builder is used to construct the
155/// counter expressions. It avoids unnecessary duplication
156/// and simplifies algebraic expressions.
157class CounterExpressionBuilder {
158  /// \brief A list of all the counter expressions
159  std::vector<CounterExpression> Expressions;
160
161  /// \brief A lookup table for the index of a given expression.
162  DenseMap<CounterExpression, unsigned> ExpressionIndices;
163
164  /// \brief 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  /// \brief 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  /// \brief Simplifies the given expression tree
188  /// by getting rid of algebraically redundant operations.
189  Counter simplify(Counter ExpressionTree);
190
191public:
192  ArrayRef<CounterExpression> getExpressions() const { return Expressions; }
193
194  /// \brief Return a counter that represents the expression
195  /// that adds LHS and RHS.
196  Counter add(Counter LHS, Counter RHS);
197
198  /// \brief Return a counter that represents the expression
199  /// that subtracts RHS from LHS.
200  Counter subtract(Counter LHS, Counter RHS);
201};
202
203/// \brief A Counter mapping region associates a source range with
204/// a specific counter.
205struct CounterMappingRegion {
206  enum RegionKind {
207    /// \brief A CodeRegion associates some code with a counter
208    CodeRegion,
209
210    /// \brief An ExpansionRegion represents a file expansion region that
211    /// associates a source range with the expansion of a virtual source file,
212    /// such as for a macro instantiation or #include file.
213    ExpansionRegion,
214
215    /// \brief A SkippedRegion represents a source range with code that
216    /// was skipped by a preprocessor or similar means.
217    SkippedRegion
218  };
219
220  Counter Count;
221  unsigned FileID, ExpandedFileID;
222  unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
223  RegionKind Kind;
224
225  CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID,
226                       unsigned LineStart, unsigned ColumnStart,
227                       unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
228      : Count(Count), FileID(FileID), ExpandedFileID(ExpandedFileID),
229        LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd),
230        ColumnEnd(ColumnEnd), Kind(Kind) {}
231
232  static CounterMappingRegion
233  makeRegion(Counter Count, unsigned FileID, unsigned LineStart,
234             unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
235    return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
236                                LineEnd, ColumnEnd, CodeRegion);
237  }
238
239  static CounterMappingRegion
240  makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart,
241                unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
242    return CounterMappingRegion(Counter(), FileID, ExpandedFileID, LineStart,
243                                ColumnStart, LineEnd, ColumnEnd,
244                                ExpansionRegion);
245  }
246
247  static CounterMappingRegion
248  makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart,
249              unsigned LineEnd, unsigned ColumnEnd) {
250    return CounterMappingRegion(Counter(), FileID, 0, LineStart, ColumnStart,
251                                LineEnd, ColumnEnd, SkippedRegion);
252  }
253
254  inline std::pair<unsigned, unsigned> startLoc() const {
255    return std::pair<unsigned, unsigned>(LineStart, ColumnStart);
256  }
257
258  inline std::pair<unsigned, unsigned> endLoc() const {
259    return std::pair<unsigned, unsigned>(LineEnd, ColumnEnd);
260  }
261};
262
263/// \brief Associates a source range with an execution count.
264struct CountedRegion : public CounterMappingRegion {
265  uint64_t ExecutionCount;
266
267  CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
268      : CounterMappingRegion(R), ExecutionCount(ExecutionCount) {}
269};
270
271/// \brief A Counter mapping context is used to connect the counters,
272/// expressions and the obtained counter values.
273class CounterMappingContext {
274  ArrayRef<CounterExpression> Expressions;
275  ArrayRef<uint64_t> CounterValues;
276
277public:
278  CounterMappingContext(ArrayRef<CounterExpression> Expressions,
279                        ArrayRef<uint64_t> CounterValues = None)
280      : Expressions(Expressions), CounterValues(CounterValues) {}
281
282  void setCounts(ArrayRef<uint64_t> Counts) { CounterValues = Counts; }
283
284  void dump(const Counter &C, raw_ostream &OS) const;
285  void dump(const Counter &C) const { dump(C, dbgs()); }
286
287  /// \brief Return the number of times that a region of code associated with
288  /// this counter was executed.
289  Expected<int64_t> evaluate(const Counter &C) const;
290};
291
292/// \brief Code coverage information for a single function.
293struct FunctionRecord {
294  /// \brief Raw function name.
295  std::string Name;
296  /// \brief Associated files.
297  std::vector<std::string> Filenames;
298  /// \brief Regions in the function along with their counts.
299  std::vector<CountedRegion> CountedRegions;
300  /// \brief The number of times this function was executed.
301  uint64_t ExecutionCount;
302
303  FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
304      : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
305
306  FunctionRecord(FunctionRecord &&FR) = default;
307  FunctionRecord &operator=(FunctionRecord &&) = default;
308
309  void pushRegion(CounterMappingRegion Region, uint64_t Count) {
310    if (CountedRegions.empty())
311      ExecutionCount = Count;
312    CountedRegions.emplace_back(Region, Count);
313  }
314};
315
316/// \brief Iterator over Functions, optionally filtered to a single file.
317class FunctionRecordIterator
318    : public iterator_facade_base<FunctionRecordIterator,
319                                  std::forward_iterator_tag, FunctionRecord> {
320  ArrayRef<FunctionRecord> Records;
321  ArrayRef<FunctionRecord>::iterator Current;
322  StringRef Filename;
323
324  /// \brief Skip records whose primary file is not \c Filename.
325  void skipOtherFiles();
326
327public:
328  FunctionRecordIterator(ArrayRef<FunctionRecord> Records_,
329                         StringRef Filename = "")
330      : Records(Records_), Current(Records.begin()), Filename(Filename) {
331    skipOtherFiles();
332  }
333
334  FunctionRecordIterator() : Current(Records.begin()) {}
335
336  bool operator==(const FunctionRecordIterator &RHS) const {
337    return Current == RHS.Current && Filename == RHS.Filename;
338  }
339
340  const FunctionRecord &operator*() const { return *Current; }
341
342  FunctionRecordIterator &operator++() {
343    assert(Current != Records.end() && "incremented past end");
344    ++Current;
345    skipOtherFiles();
346    return *this;
347  }
348};
349
350/// \brief Coverage information for a macro expansion or #included file.
351///
352/// When covered code has pieces that can be expanded for more detail, such as a
353/// preprocessor macro use and its definition, these are represented as
354/// expansions whose coverage can be looked up independently.
355struct ExpansionRecord {
356  /// \brief The abstract file this expansion covers.
357  unsigned FileID;
358  /// \brief The region that expands to this record.
359  const CountedRegion &Region;
360  /// \brief Coverage for the expansion.
361  const FunctionRecord &Function;
362
363  ExpansionRecord(const CountedRegion &Region,
364                  const FunctionRecord &Function)
365      : FileID(Region.ExpandedFileID), Region(Region), Function(Function) {}
366};
367
368/// \brief The execution count information starting at a point in a file.
369///
370/// A sequence of CoverageSegments gives execution counts for a file in format
371/// that's simple to iterate through for processing.
372struct CoverageSegment {
373  /// \brief The line where this segment begins.
374  unsigned Line;
375  /// \brief The column where this segment begins.
376  unsigned Col;
377  /// \brief The execution count, or zero if no count was recorded.
378  uint64_t Count;
379  /// \brief When false, the segment was uninstrumented or skipped.
380  bool HasCount;
381  /// \brief Whether this enters a new region or returns to a previous count.
382  bool IsRegionEntry;
383
384  CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
385      : Line(Line), Col(Col), Count(0), HasCount(false),
386        IsRegionEntry(IsRegionEntry) {}
387
388  CoverageSegment(unsigned Line, unsigned Col, uint64_t Count,
389                  bool IsRegionEntry)
390      : Line(Line), Col(Col), Count(Count), HasCount(true),
391        IsRegionEntry(IsRegionEntry) {}
392
393  friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) {
394    return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry) ==
395           std::tie(R.Line, R.Col, R.Count, R.HasCount, R.IsRegionEntry);
396  }
397};
398
399/// \brief Coverage information to be processed or displayed.
400///
401/// This represents the coverage of an entire file, expansion, or function. It
402/// provides a sequence of CoverageSegments to iterate through, as well as the
403/// list of expansions that can be further processed.
404class CoverageData {
405  friend class CoverageMapping;
406
407  std::string Filename;
408  std::vector<CoverageSegment> Segments;
409  std::vector<ExpansionRecord> Expansions;
410
411public:
412  CoverageData() = default;
413
414  CoverageData(StringRef Filename) : Filename(Filename) {}
415
416  /// \brief Get the name of the file this data covers.
417  StringRef getFilename() const { return Filename; }
418
419  std::vector<CoverageSegment>::const_iterator begin() const {
420    return Segments.begin();
421  }
422
423  std::vector<CoverageSegment>::const_iterator end() const {
424    return Segments.end();
425  }
426
427  bool empty() const { return Segments.empty(); }
428
429  /// \brief Expansions that can be further processed.
430  ArrayRef<ExpansionRecord> getExpansions() const { return Expansions; }
431};
432
433/// \brief The mapping of profile information to coverage data.
434///
435/// This is the main interface to get coverage information, using a profile to
436/// fill out execution counts.
437class CoverageMapping {
438  StringSet<> FunctionNames;
439  std::vector<FunctionRecord> Functions;
440  unsigned MismatchedFunctionCount = 0;
441
442  CoverageMapping() = default;
443
444  /// \brief Add a function record corresponding to \p Record.
445  Error loadFunctionRecord(const CoverageMappingRecord &Record,
446                           IndexedInstrProfReader &ProfileReader);
447
448public:
449  CoverageMapping(const CoverageMapping &) = delete;
450  CoverageMapping &operator=(const CoverageMapping &) = delete;
451
452  /// \brief Load the coverage mapping using the given readers.
453  static Expected<std::unique_ptr<CoverageMapping>>
454  load(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
455       IndexedInstrProfReader &ProfileReader);
456
457  static Expected<std::unique_ptr<CoverageMapping>>
458  load(ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
459       StringRef Arch = StringRef());
460
461  /// \brief The number of functions that couldn't have their profiles mapped.
462  ///
463  /// This is a count of functions whose profile is out of date or otherwise
464  /// can't be associated with any coverage information.
465  unsigned getMismatchedCount() { return MismatchedFunctionCount; }
466
467  /// \brief Returns a lexicographically sorted, unique list of files that are
468  /// covered.
469  std::vector<StringRef> getUniqueSourceFiles() const;
470
471  /// \brief Get the coverage for a particular file.
472  ///
473  /// The given filename must be the name as recorded in the coverage
474  /// information. That is, only names returned from getUniqueSourceFiles will
475  /// yield a result.
476  CoverageData getCoverageForFile(StringRef Filename) const;
477
478  /// \brief Gets all of the functions covered by this profile.
479  iterator_range<FunctionRecordIterator> getCoveredFunctions() const {
480    return make_range(FunctionRecordIterator(Functions),
481                      FunctionRecordIterator());
482  }
483
484  /// \brief Gets all of the functions in a particular file.
485  iterator_range<FunctionRecordIterator>
486  getCoveredFunctions(StringRef Filename) const {
487    return make_range(FunctionRecordIterator(Functions, Filename),
488                      FunctionRecordIterator());
489  }
490
491  /// \brief Get the list of function instantiations in the file.
492  ///
493  /// Functions that are instantiated more than once, such as C++ template
494  /// specializations, have distinct coverage records for each instantiation.
495  std::vector<const FunctionRecord *>
496  getInstantiations(StringRef Filename) const;
497
498  /// \brief Get the coverage for a particular function.
499  CoverageData getCoverageForFunction(const FunctionRecord &Function) const;
500
501  /// \brief Get the coverage for an expansion within a coverage set.
502  CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const;
503};
504
505// Profile coverage map has the following layout:
506// [CoverageMapFileHeader]
507// [ArrayStart]
508//  [CovMapFunctionRecord]
509//  [CovMapFunctionRecord]
510//  ...
511// [ArrayEnd]
512// [Encoded Region Mapping Data]
513LLVM_PACKED_START
514template <class IntPtrT> struct CovMapFunctionRecordV1 {
515#define COVMAP_V1
516#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
517#include "llvm/ProfileData/InstrProfData.inc"
518#undef COVMAP_V1
519
520  // Return the structural hash associated with the function.
521  template <support::endianness Endian> uint64_t getFuncHash() const {
522    return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
523  }
524
525  // Return the coverage map data size for the funciton.
526  template <support::endianness Endian> uint32_t getDataSize() const {
527    return support::endian::byte_swap<uint32_t, Endian>(DataSize);
528  }
529
530  // Return function lookup key. The value is consider opaque.
531  template <support::endianness Endian> IntPtrT getFuncNameRef() const {
532    return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
533  }
534
535  // Return the PGO name of the function */
536  template <support::endianness Endian>
537  Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
538    IntPtrT NameRef = getFuncNameRef<Endian>();
539    uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
540    FuncName = ProfileNames.getFuncName(NameRef, NameS);
541    if (NameS && FuncName.empty())
542      return make_error<CoverageMapError>(coveragemap_error::malformed);
543    return Error::success();
544  }
545};
546
547struct CovMapFunctionRecord {
548#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
549#include "llvm/ProfileData/InstrProfData.inc"
550
551  // Return the structural hash associated with the function.
552  template <support::endianness Endian> uint64_t getFuncHash() const {
553    return support::endian::byte_swap<uint64_t, Endian>(FuncHash);
554  }
555
556  // Return the coverage map data size for the funciton.
557  template <support::endianness Endian> uint32_t getDataSize() const {
558    return support::endian::byte_swap<uint32_t, Endian>(DataSize);
559  }
560
561  // Return function lookup key. The value is consider opaque.
562  template <support::endianness Endian> uint64_t getFuncNameRef() const {
563    return support::endian::byte_swap<uint64_t, Endian>(NameRef);
564  }
565
566  // Return the PGO name of the function */
567  template <support::endianness Endian>
568  Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
569    uint64_t NameRef = getFuncNameRef<Endian>();
570    FuncName = ProfileNames.getFuncName(NameRef);
571    return Error::success();
572  }
573};
574
575// Per module coverage mapping data header, i.e. CoverageMapFileHeader
576// documented above.
577struct CovMapHeader {
578#define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
579#include "llvm/ProfileData/InstrProfData.inc"
580  template <support::endianness Endian> uint32_t getNRecords() const {
581    return support::endian::byte_swap<uint32_t, Endian>(NRecords);
582  }
583
584  template <support::endianness Endian> uint32_t getFilenamesSize() const {
585    return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
586  }
587
588  template <support::endianness Endian> uint32_t getCoverageSize() const {
589    return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
590  }
591
592  template <support::endianness Endian> uint32_t getVersion() const {
593    return support::endian::byte_swap<uint32_t, Endian>(Version);
594  }
595};
596
597LLVM_PACKED_END
598
599enum CovMapVersion {
600  Version1 = 0,
601  // Function's name reference from CovMapFuncRecord is changed from raw
602  // name string pointer to MD5 to support name section compression. Name
603  // section is also compressed.
604  Version2 = 1,
605  // The current version is Version2
606  CurrentVersion = INSTR_PROF_COVMAP_VERSION
607};
608
609template <int CovMapVersion, class IntPtrT> struct CovMapTraits {
610  using CovMapFuncRecordType = CovMapFunctionRecord;
611  using NameRefType = uint64_t;
612};
613
614template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version1, IntPtrT> {
615  using CovMapFuncRecordType = CovMapFunctionRecordV1<IntPtrT>;
616  using NameRefType = IntPtrT;
617};
618
619} // end namespace coverage
620
621/// \brief Provide DenseMapInfo for CounterExpression
622template<> struct DenseMapInfo<coverage::CounterExpression> {
623  static inline coverage::CounterExpression getEmptyKey() {
624    using namespace coverage;
625
626    return CounterExpression(CounterExpression::ExprKind::Subtract,
627                             Counter::getCounter(~0U),
628                             Counter::getCounter(~0U));
629  }
630
631  static inline coverage::CounterExpression getTombstoneKey() {
632    using namespace coverage;
633
634    return CounterExpression(CounterExpression::ExprKind::Add,
635                             Counter::getCounter(~0U),
636                             Counter::getCounter(~0U));
637  }
638
639  static unsigned getHashValue(const coverage::CounterExpression &V) {
640    return static_cast<unsigned>(
641        hash_combine(V.Kind, V.LHS.getKind(), V.LHS.getCounterID(),
642                     V.RHS.getKind(), V.RHS.getCounterID()));
643  }
644
645  static bool isEqual(const coverage::CounterExpression &LHS,
646                      const coverage::CounterExpression &RHS) {
647    return LHS.Kind == RHS.Kind && LHS.LHS == RHS.LHS && LHS.RHS == RHS.RHS;
648  }
649};
650
651} // end namespace llvm
652
653#endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
654