1 //===- CoverageMapping.h - Code coverage mapping support --------*- C++ -*-===//
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 // Code coverage mapping data is generated by clang and read by
10 // llvm-cov to show code coverage statistics for a file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
15 #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/iterator.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/ProfileData/InstrProf.h"
26 #include "llvm/Support/Alignment.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
42 namespace llvm {
43
44 class IndexedInstrProfReader;
45
46 namespace coverage {
47
48 class CoverageMappingReader;
49 struct CoverageMappingRecord;
50
51 enum class coveragemap_error {
52 success = 0,
53 eof,
54 no_data_found,
55 unsupported_version,
56 truncated,
57 malformed,
58 decompression_failed,
59 invalid_or_missing_arch_specifier
60 };
61
62 const std::error_category &coveragemap_category();
63
make_error_code(coveragemap_error E)64 inline std::error_code make_error_code(coveragemap_error E) {
65 return std::error_code(static_cast<int>(E), coveragemap_category());
66 }
67
68 class CoverageMapError : public ErrorInfo<CoverageMapError> {
69 public:
CoverageMapError(coveragemap_error Err)70 CoverageMapError(coveragemap_error Err) : Err(Err) {
71 assert(Err != coveragemap_error::success && "Not an error");
72 }
73
74 std::string message() const override;
75
log(raw_ostream & OS)76 void log(raw_ostream &OS) const override { OS << message(); }
77
convertToErrorCode()78 std::error_code convertToErrorCode() const override {
79 return make_error_code(Err);
80 }
81
get()82 coveragemap_error get() const { return Err; }
83
84 static char ID;
85
86 private:
87 coveragemap_error Err;
88 };
89
90 /// A Counter is an abstract value that describes how to compute the
91 /// execution count for a region of code using the collected profile count data.
92 struct Counter {
93 enum CounterKind { Zero, CounterValueReference, Expression };
94 static const unsigned EncodingTagBits = 2;
95 static const unsigned EncodingTagMask = 0x3;
96 static const unsigned EncodingCounterTagAndExpansionRegionTagBits =
97 EncodingTagBits + 1;
98
99 private:
100 CounterKind Kind = Zero;
101 unsigned ID = 0;
102
CounterCounter103 Counter(CounterKind Kind, unsigned ID) : Kind(Kind), ID(ID) {}
104
105 public:
106 Counter() = default;
107
getKindCounter108 CounterKind getKind() const { return Kind; }
109
isZeroCounter110 bool isZero() const { return Kind == Zero; }
111
isExpressionCounter112 bool isExpression() const { return Kind == Expression; }
113
getCounterIDCounter114 unsigned getCounterID() const { return ID; }
115
getExpressionIDCounter116 unsigned getExpressionID() const { return ID; }
117
118 friend bool operator==(const Counter &LHS, const Counter &RHS) {
119 return LHS.Kind == RHS.Kind && LHS.ID == RHS.ID;
120 }
121
122 friend bool operator!=(const Counter &LHS, const Counter &RHS) {
123 return !(LHS == RHS);
124 }
125
126 friend bool operator<(const Counter &LHS, const Counter &RHS) {
127 return std::tie(LHS.Kind, LHS.ID) < std::tie(RHS.Kind, RHS.ID);
128 }
129
130 /// Return the counter that represents the number zero.
getZeroCounter131 static Counter getZero() { return Counter(); }
132
133 /// Return the counter that corresponds to a specific profile counter.
getCounterCounter134 static Counter getCounter(unsigned CounterId) {
135 return Counter(CounterValueReference, CounterId);
136 }
137
138 /// Return the counter that corresponds to a specific addition counter
139 /// expression.
getExpressionCounter140 static Counter getExpression(unsigned ExpressionId) {
141 return Counter(Expression, ExpressionId);
142 }
143 };
144
145 /// A Counter expression is a value that represents an arithmetic operation
146 /// with two counters.
147 struct CounterExpression {
148 enum ExprKind { Subtract, Add };
149 ExprKind Kind;
150 Counter LHS, RHS;
151
CounterExpressionCounterExpression152 CounterExpression(ExprKind Kind, Counter LHS, Counter RHS)
153 : Kind(Kind), LHS(LHS), RHS(RHS) {}
154 };
155
156 /// A Counter expression builder is used to construct the counter expressions.
157 /// It avoids unnecessary duplication and simplifies algebraic expressions.
158 class CounterExpressionBuilder {
159 /// A list of all the counter expressions
160 std::vector<CounterExpression> Expressions;
161
162 /// A lookup table for the index of a given expression.
163 DenseMap<CounterExpression, unsigned> ExpressionIndices;
164
165 /// Return the counter which corresponds to the given expression.
166 ///
167 /// If the given expression is already stored in the builder, a counter
168 /// that references that expression is returned. Otherwise, the given
169 /// expression is added to the builder's collection of expressions.
170 Counter get(const CounterExpression &E);
171
172 /// Represents a term in a counter expression tree.
173 struct Term {
174 unsigned CounterID;
175 int Factor;
176
TermTerm177 Term(unsigned CounterID, int Factor)
178 : CounterID(CounterID), Factor(Factor) {}
179 };
180
181 /// Gather the terms of the expression tree for processing.
182 ///
183 /// This collects each addition and subtraction referenced by the counter into
184 /// a sequence that can be sorted and combined to build a simplified counter
185 /// expression.
186 void extractTerms(Counter C, int Sign, SmallVectorImpl<Term> &Terms);
187
188 /// Simplifies the given expression tree
189 /// by getting rid of algebraically redundant operations.
190 Counter simplify(Counter ExpressionTree);
191
192 public:
getExpressions()193 ArrayRef<CounterExpression> getExpressions() const { return Expressions; }
194
195 /// Return a counter that represents the expression that adds LHS and RHS.
196 Counter add(Counter LHS, Counter RHS);
197
198 /// Return a counter that represents the expression that subtracts RHS from
199 /// LHS.
200 Counter subtract(Counter LHS, Counter RHS);
201 };
202
203 using LineColPair = std::pair<unsigned, unsigned>;
204
205 /// A Counter mapping region associates a source range with a specific counter.
206 struct CounterMappingRegion {
207 enum RegionKind {
208 /// A CodeRegion associates some code with a counter
209 CodeRegion,
210
211 /// An ExpansionRegion represents a file expansion region that associates
212 /// a source range with the expansion of a virtual source file, such as
213 /// for a macro instantiation or #include file.
214 ExpansionRegion,
215
216 /// A SkippedRegion represents a source range with code that was skipped
217 /// by a preprocessor or similar means.
218 SkippedRegion,
219
220 /// A GapRegion is like a CodeRegion, but its count is only set as the
221 /// line execution count when its the only region in the line.
222 GapRegion
223 };
224
225 Counter Count;
226 unsigned FileID, ExpandedFileID;
227 unsigned LineStart, ColumnStart, LineEnd, ColumnEnd;
228 RegionKind Kind;
229
CounterMappingRegionCounterMappingRegion230 CounterMappingRegion(Counter Count, unsigned FileID, unsigned ExpandedFileID,
231 unsigned LineStart, unsigned ColumnStart,
232 unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind)
233 : Count(Count), FileID(FileID), ExpandedFileID(ExpandedFileID),
234 LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd),
235 ColumnEnd(ColumnEnd), Kind(Kind) {}
236
237 static CounterMappingRegion
makeRegionCounterMappingRegion238 makeRegion(Counter Count, unsigned FileID, unsigned LineStart,
239 unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
240 return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
241 LineEnd, ColumnEnd, CodeRegion);
242 }
243
244 static CounterMappingRegion
makeExpansionCounterMappingRegion245 makeExpansion(unsigned FileID, unsigned ExpandedFileID, unsigned LineStart,
246 unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
247 return CounterMappingRegion(Counter(), FileID, ExpandedFileID, LineStart,
248 ColumnStart, LineEnd, ColumnEnd,
249 ExpansionRegion);
250 }
251
252 static CounterMappingRegion
makeSkippedCounterMappingRegion253 makeSkipped(unsigned FileID, unsigned LineStart, unsigned ColumnStart,
254 unsigned LineEnd, unsigned ColumnEnd) {
255 return CounterMappingRegion(Counter(), FileID, 0, LineStart, ColumnStart,
256 LineEnd, ColumnEnd, SkippedRegion);
257 }
258
259 static CounterMappingRegion
makeGapRegionCounterMappingRegion260 makeGapRegion(Counter Count, unsigned FileID, unsigned LineStart,
261 unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) {
262 return CounterMappingRegion(Count, FileID, 0, LineStart, ColumnStart,
263 LineEnd, (1U << 31) | ColumnEnd, GapRegion);
264 }
265
startLocCounterMappingRegion266 inline LineColPair startLoc() const {
267 return LineColPair(LineStart, ColumnStart);
268 }
269
endLocCounterMappingRegion270 inline LineColPair endLoc() const { return LineColPair(LineEnd, ColumnEnd); }
271 };
272
273 /// Associates a source range with an execution count.
274 struct CountedRegion : public CounterMappingRegion {
275 uint64_t ExecutionCount;
276
CountedRegionCountedRegion277 CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
278 : CounterMappingRegion(R), ExecutionCount(ExecutionCount) {}
279 };
280
281 /// A Counter mapping context is used to connect the counters, expressions
282 /// and the obtained counter values.
283 class CounterMappingContext {
284 ArrayRef<CounterExpression> Expressions;
285 ArrayRef<uint64_t> CounterValues;
286
287 public:
288 CounterMappingContext(ArrayRef<CounterExpression> Expressions,
289 ArrayRef<uint64_t> CounterValues = None)
Expressions(Expressions)290 : Expressions(Expressions), CounterValues(CounterValues) {}
291
setCounts(ArrayRef<uint64_t> Counts)292 void setCounts(ArrayRef<uint64_t> Counts) { CounterValues = Counts; }
293
294 void dump(const Counter &C, raw_ostream &OS) const;
dump(const Counter & C)295 void dump(const Counter &C) const { dump(C, dbgs()); }
296
297 /// Return the number of times that a region of code associated with this
298 /// counter was executed.
299 Expected<int64_t> evaluate(const Counter &C) const;
300 };
301
302 /// Code coverage information for a single function.
303 struct FunctionRecord {
304 /// Raw function name.
305 std::string Name;
306 /// Mapping from FileID (i.e. vector index) to filename. Used to support
307 /// macro expansions within a function in which the macro and function are
308 /// defined in separate files.
309 ///
310 /// TODO: Uniquing filenames across all function records may be a performance
311 /// optimization.
312 std::vector<std::string> Filenames;
313 /// Regions in the function along with their counts.
314 std::vector<CountedRegion> CountedRegions;
315 /// The number of times this function was executed.
316 uint64_t ExecutionCount = 0;
317
FunctionRecordFunctionRecord318 FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
319 : Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
320
321 FunctionRecord(FunctionRecord &&FR) = default;
322 FunctionRecord &operator=(FunctionRecord &&) = default;
323
pushRegionFunctionRecord324 void pushRegion(CounterMappingRegion Region, uint64_t Count) {
325 if (CountedRegions.empty())
326 ExecutionCount = Count;
327 CountedRegions.emplace_back(Region, Count);
328 }
329 };
330
331 /// Iterator over Functions, optionally filtered to a single file.
332 class FunctionRecordIterator
333 : public iterator_facade_base<FunctionRecordIterator,
334 std::forward_iterator_tag, FunctionRecord> {
335 ArrayRef<FunctionRecord> Records;
336 ArrayRef<FunctionRecord>::iterator Current;
337 StringRef Filename;
338
339 /// Skip records whose primary file is not \c Filename.
340 void skipOtherFiles();
341
342 public:
343 FunctionRecordIterator(ArrayRef<FunctionRecord> Records_,
344 StringRef Filename = "")
Records(Records_)345 : Records(Records_), Current(Records.begin()), Filename(Filename) {
346 skipOtherFiles();
347 }
348
FunctionRecordIterator()349 FunctionRecordIterator() : Current(Records.begin()) {}
350
351 bool operator==(const FunctionRecordIterator &RHS) const {
352 return Current == RHS.Current && Filename == RHS.Filename;
353 }
354
355 const FunctionRecord &operator*() const { return *Current; }
356
357 FunctionRecordIterator &operator++() {
358 assert(Current != Records.end() && "incremented past end");
359 ++Current;
360 skipOtherFiles();
361 return *this;
362 }
363 };
364
365 /// Coverage information for a macro expansion or #included file.
366 ///
367 /// When covered code has pieces that can be expanded for more detail, such as a
368 /// preprocessor macro use and its definition, these are represented as
369 /// expansions whose coverage can be looked up independently.
370 struct ExpansionRecord {
371 /// The abstract file this expansion covers.
372 unsigned FileID;
373 /// The region that expands to this record.
374 const CountedRegion &Region;
375 /// Coverage for the expansion.
376 const FunctionRecord &Function;
377
ExpansionRecordExpansionRecord378 ExpansionRecord(const CountedRegion &Region,
379 const FunctionRecord &Function)
380 : FileID(Region.ExpandedFileID), Region(Region), Function(Function) {}
381 };
382
383 /// The execution count information starting at a point in a file.
384 ///
385 /// A sequence of CoverageSegments gives execution counts for a file in format
386 /// that's simple to iterate through for processing.
387 struct CoverageSegment {
388 /// The line where this segment begins.
389 unsigned Line;
390 /// The column where this segment begins.
391 unsigned Col;
392 /// The execution count, or zero if no count was recorded.
393 uint64_t Count;
394 /// When false, the segment was uninstrumented or skipped.
395 bool HasCount;
396 /// Whether this enters a new region or returns to a previous count.
397 bool IsRegionEntry;
398 /// Whether this enters a gap region.
399 bool IsGapRegion;
400
CoverageSegmentCoverageSegment401 CoverageSegment(unsigned Line, unsigned Col, bool IsRegionEntry)
402 : Line(Line), Col(Col), Count(0), HasCount(false),
403 IsRegionEntry(IsRegionEntry), IsGapRegion(false) {}
404
405 CoverageSegment(unsigned Line, unsigned Col, uint64_t Count,
406 bool IsRegionEntry, bool IsGapRegion = false)
LineCoverageSegment407 : Line(Line), Col(Col), Count(Count), HasCount(true),
408 IsRegionEntry(IsRegionEntry), IsGapRegion(IsGapRegion) {}
409
410 friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) {
411 return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry,
412 L.IsGapRegion) == std::tie(R.Line, R.Col, R.Count,
413 R.HasCount, R.IsRegionEntry,
414 R.IsGapRegion);
415 }
416 };
417
418 /// An instantiation group contains a \c FunctionRecord list, such that each
419 /// record corresponds to a distinct instantiation of the same function.
420 ///
421 /// Note that it's possible for a function to have more than one instantiation
422 /// (consider C++ template specializations or static inline functions).
423 class InstantiationGroup {
424 friend class CoverageMapping;
425
426 unsigned Line;
427 unsigned Col;
428 std::vector<const FunctionRecord *> Instantiations;
429
InstantiationGroup(unsigned Line,unsigned Col,std::vector<const FunctionRecord * > Instantiations)430 InstantiationGroup(unsigned Line, unsigned Col,
431 std::vector<const FunctionRecord *> Instantiations)
432 : Line(Line), Col(Col), Instantiations(std::move(Instantiations)) {}
433
434 public:
435 InstantiationGroup(const InstantiationGroup &) = delete;
436 InstantiationGroup(InstantiationGroup &&) = default;
437
438 /// Get the number of instantiations in this group.
size()439 size_t size() const { return Instantiations.size(); }
440
441 /// Get the line where the common function was defined.
getLine()442 unsigned getLine() const { return Line; }
443
444 /// Get the column where the common function was defined.
getColumn()445 unsigned getColumn() const { return Col; }
446
447 /// Check if the instantiations in this group have a common mangled name.
hasName()448 bool hasName() const {
449 for (unsigned I = 1, E = Instantiations.size(); I < E; ++I)
450 if (Instantiations[I]->Name != Instantiations[0]->Name)
451 return false;
452 return true;
453 }
454
455 /// Get the common mangled name for instantiations in this group.
getName()456 StringRef getName() const {
457 assert(hasName() && "Instantiations don't have a shared name");
458 return Instantiations[0]->Name;
459 }
460
461 /// Get the total execution count of all instantiations in this group.
getTotalExecutionCount()462 uint64_t getTotalExecutionCount() const {
463 uint64_t Count = 0;
464 for (const FunctionRecord *F : Instantiations)
465 Count += F->ExecutionCount;
466 return Count;
467 }
468
469 /// Get the instantiations in this group.
getInstantiations()470 ArrayRef<const FunctionRecord *> getInstantiations() const {
471 return Instantiations;
472 }
473 };
474
475 /// Coverage information to be processed or displayed.
476 ///
477 /// This represents the coverage of an entire file, expansion, or function. It
478 /// provides a sequence of CoverageSegments to iterate through, as well as the
479 /// list of expansions that can be further processed.
480 class CoverageData {
481 friend class CoverageMapping;
482
483 std::string Filename;
484 std::vector<CoverageSegment> Segments;
485 std::vector<ExpansionRecord> Expansions;
486
487 public:
488 CoverageData() = default;
489
CoverageData(StringRef Filename)490 CoverageData(StringRef Filename) : Filename(Filename) {}
491
492 /// Get the name of the file this data covers.
getFilename()493 StringRef getFilename() const { return Filename; }
494
495 /// Get an iterator over the coverage segments for this object. The segments
496 /// are guaranteed to be uniqued and sorted by location.
begin()497 std::vector<CoverageSegment>::const_iterator begin() const {
498 return Segments.begin();
499 }
500
end()501 std::vector<CoverageSegment>::const_iterator end() const {
502 return Segments.end();
503 }
504
empty()505 bool empty() const { return Segments.empty(); }
506
507 /// Expansions that can be further processed.
getExpansions()508 ArrayRef<ExpansionRecord> getExpansions() const { return Expansions; }
509 };
510
511 /// The mapping of profile information to coverage data.
512 ///
513 /// This is the main interface to get coverage information, using a profile to
514 /// fill out execution counts.
515 class CoverageMapping {
516 DenseMap<size_t, DenseSet<size_t>> RecordProvenance;
517 std::vector<FunctionRecord> Functions;
518 DenseMap<size_t, SmallVector<unsigned, 0>> FilenameHash2RecordIndices;
519 std::vector<std::pair<std::string, uint64_t>> FuncHashMismatches;
520
521 CoverageMapping() = default;
522
523 /// Add a function record corresponding to \p Record.
524 Error loadFunctionRecord(const CoverageMappingRecord &Record,
525 IndexedInstrProfReader &ProfileReader);
526
527 /// Look up the indices for function records which are at least partially
528 /// defined in the specified file. This is guaranteed to return a superset of
529 /// such records: extra records not in the file may be included if there is
530 /// a hash collision on the filename. Clients must be robust to collisions.
531 ArrayRef<unsigned>
532 getImpreciseRecordIndicesForFilename(StringRef Filename) const;
533
534 public:
535 CoverageMapping(const CoverageMapping &) = delete;
536 CoverageMapping &operator=(const CoverageMapping &) = delete;
537
538 /// Load the coverage mapping using the given readers.
539 static Expected<std::unique_ptr<CoverageMapping>>
540 load(ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
541 IndexedInstrProfReader &ProfileReader);
542
543 /// Load the coverage mapping from the given object files and profile. If
544 /// \p Arches is non-empty, it must specify an architecture for each object.
545 /// Ignores non-instrumented object files unless all are not instrumented.
546 static Expected<std::unique_ptr<CoverageMapping>>
547 load(ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
548 ArrayRef<StringRef> Arches = None);
549
550 /// The number of functions that couldn't have their profiles mapped.
551 ///
552 /// This is a count of functions whose profile is out of date or otherwise
553 /// can't be associated with any coverage information.
getMismatchedCount()554 unsigned getMismatchedCount() const { return FuncHashMismatches.size(); }
555
556 /// A hash mismatch occurs when a profile record for a symbol does not have
557 /// the same hash as a coverage mapping record for the same symbol. This
558 /// returns a list of hash mismatches, where each mismatch is a pair of the
559 /// symbol name and its coverage mapping hash.
getHashMismatches()560 ArrayRef<std::pair<std::string, uint64_t>> getHashMismatches() const {
561 return FuncHashMismatches;
562 }
563
564 /// Returns a lexicographically sorted, unique list of files that are
565 /// covered.
566 std::vector<StringRef> getUniqueSourceFiles() const;
567
568 /// Get the coverage for a particular file.
569 ///
570 /// The given filename must be the name as recorded in the coverage
571 /// information. That is, only names returned from getUniqueSourceFiles will
572 /// yield a result.
573 CoverageData getCoverageForFile(StringRef Filename) const;
574
575 /// Get the coverage for a particular function.
576 CoverageData getCoverageForFunction(const FunctionRecord &Function) const;
577
578 /// Get the coverage for an expansion within a coverage set.
579 CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion) const;
580
581 /// Gets all of the functions covered by this profile.
getCoveredFunctions()582 iterator_range<FunctionRecordIterator> getCoveredFunctions() const {
583 return make_range(FunctionRecordIterator(Functions),
584 FunctionRecordIterator());
585 }
586
587 /// Gets all of the functions in a particular file.
588 iterator_range<FunctionRecordIterator>
getCoveredFunctions(StringRef Filename)589 getCoveredFunctions(StringRef Filename) const {
590 return make_range(FunctionRecordIterator(Functions, Filename),
591 FunctionRecordIterator());
592 }
593
594 /// Get the list of function instantiation groups in a particular file.
595 ///
596 /// Every instantiation group in a program is attributed to exactly one file:
597 /// the file in which the definition for the common function begins.
598 std::vector<InstantiationGroup>
599 getInstantiationGroups(StringRef Filename) const;
600 };
601
602 /// Coverage statistics for a single line.
603 class LineCoverageStats {
604 uint64_t ExecutionCount;
605 bool HasMultipleRegions;
606 bool Mapped;
607 unsigned Line;
608 ArrayRef<const CoverageSegment *> LineSegments;
609 const CoverageSegment *WrappedSegment;
610
611 friend class LineCoverageIterator;
612 LineCoverageStats() = default;
613
614 public:
615 LineCoverageStats(ArrayRef<const CoverageSegment *> LineSegments,
616 const CoverageSegment *WrappedSegment, unsigned Line);
617
getExecutionCount()618 uint64_t getExecutionCount() const { return ExecutionCount; }
619
hasMultipleRegions()620 bool hasMultipleRegions() const { return HasMultipleRegions; }
621
isMapped()622 bool isMapped() const { return Mapped; }
623
getLine()624 unsigned getLine() const { return Line; }
625
getLineSegments()626 ArrayRef<const CoverageSegment *> getLineSegments() const {
627 return LineSegments;
628 }
629
getWrappedSegment()630 const CoverageSegment *getWrappedSegment() const { return WrappedSegment; }
631 };
632
633 /// An iterator over the \c LineCoverageStats objects for lines described by
634 /// a \c CoverageData instance.
635 class LineCoverageIterator
636 : public iterator_facade_base<
637 LineCoverageIterator, std::forward_iterator_tag, LineCoverageStats> {
638 public:
LineCoverageIterator(const CoverageData & CD)639 LineCoverageIterator(const CoverageData &CD)
640 : LineCoverageIterator(CD, CD.begin()->Line) {}
641
LineCoverageIterator(const CoverageData & CD,unsigned Line)642 LineCoverageIterator(const CoverageData &CD, unsigned Line)
643 : CD(CD), WrappedSegment(nullptr), Next(CD.begin()), Ended(false),
644 Line(Line), Segments(), Stats() {
645 this->operator++();
646 }
647
648 bool operator==(const LineCoverageIterator &R) const {
649 return &CD == &R.CD && Next == R.Next && Ended == R.Ended;
650 }
651
652 const LineCoverageStats &operator*() const { return Stats; }
653
654 LineCoverageStats &operator*() { return Stats; }
655
656 LineCoverageIterator &operator++();
657
getEnd()658 LineCoverageIterator getEnd() const {
659 auto EndIt = *this;
660 EndIt.Next = CD.end();
661 EndIt.Ended = true;
662 return EndIt;
663 }
664
665 private:
666 const CoverageData &CD;
667 const CoverageSegment *WrappedSegment;
668 std::vector<CoverageSegment>::const_iterator Next;
669 bool Ended;
670 unsigned Line;
671 SmallVector<const CoverageSegment *, 4> Segments;
672 LineCoverageStats Stats;
673 };
674
675 /// Get a \c LineCoverageIterator range for the lines described by \p CD.
676 static inline iterator_range<LineCoverageIterator>
getLineCoverageStats(const coverage::CoverageData & CD)677 getLineCoverageStats(const coverage::CoverageData &CD) {
678 auto Begin = LineCoverageIterator(CD);
679 auto End = Begin.getEnd();
680 return make_range(Begin, End);
681 }
682
683 // Coverage mappping data (V2) has the following layout:
684 // IPSK_covmap:
685 // [CoverageMapFileHeader]
686 // [ArrayStart]
687 // [CovMapFunctionRecordV2]
688 // [CovMapFunctionRecordV2]
689 // ...
690 // [ArrayEnd]
691 // [Encoded Filenames and Region Mapping Data]
692 //
693 // Coverage mappping data (V3) has the following layout:
694 // IPSK_covmap:
695 // [CoverageMapFileHeader]
696 // [Encoded Filenames]
697 // IPSK_covfun:
698 // [ArrayStart]
699 // odr_name_1: [CovMapFunctionRecordV3]
700 // odr_name_2: [CovMapFunctionRecordV3]
701 // ...
702 // [ArrayEnd]
703 //
704 // Both versions of the coverage mapping format encode the same information,
705 // but the V3 format does so more compactly by taking advantage of linkonce_odr
706 // semantics (it allows exactly 1 function record per name reference).
707
708 /// This namespace defines accessors shared by different versions of coverage
709 /// mapping records.
710 namespace accessors {
711
712 /// Return the structural hash associated with the function.
713 template <class FuncRecordTy, support::endianness Endian>
getFuncHash(const FuncRecordTy * Record)714 uint64_t getFuncHash(const FuncRecordTy *Record) {
715 return support::endian::byte_swap<uint64_t, Endian>(Record->FuncHash);
716 }
717
718 /// Return the coverage map data size for the function.
719 template <class FuncRecordTy, support::endianness Endian>
getDataSize(const FuncRecordTy * Record)720 uint64_t getDataSize(const FuncRecordTy *Record) {
721 return support::endian::byte_swap<uint32_t, Endian>(Record->DataSize);
722 }
723
724 /// Return the function lookup key. The value is considered opaque.
725 template <class FuncRecordTy, support::endianness Endian>
getFuncNameRef(const FuncRecordTy * Record)726 uint64_t getFuncNameRef(const FuncRecordTy *Record) {
727 return support::endian::byte_swap<uint64_t, Endian>(Record->NameRef);
728 }
729
730 /// Return the PGO name of the function. Used for formats in which the name is
731 /// a hash.
732 template <class FuncRecordTy, support::endianness Endian>
getFuncNameViaRef(const FuncRecordTy * Record,InstrProfSymtab & ProfileNames,StringRef & FuncName)733 Error getFuncNameViaRef(const FuncRecordTy *Record,
734 InstrProfSymtab &ProfileNames, StringRef &FuncName) {
735 uint64_t NameRef = getFuncNameRef<FuncRecordTy, Endian>(Record);
736 FuncName = ProfileNames.getFuncName(NameRef);
737 return Error::success();
738 }
739
740 /// Read coverage mapping out-of-line, from \p MappingBuf. This is used when the
741 /// coverage mapping is attached to the file header, instead of to the function
742 /// record.
743 template <class FuncRecordTy, support::endianness Endian>
getCoverageMappingOutOfLine(const FuncRecordTy * Record,const char * MappingBuf)744 StringRef getCoverageMappingOutOfLine(const FuncRecordTy *Record,
745 const char *MappingBuf) {
746 return {MappingBuf, size_t(getDataSize<FuncRecordTy, Endian>(Record))};
747 }
748
749 /// Advance to the next out-of-line coverage mapping and its associated
750 /// function record.
751 template <class FuncRecordTy, support::endianness Endian>
752 std::pair<const char *, const FuncRecordTy *>
advanceByOneOutOfLine(const FuncRecordTy * Record,const char * MappingBuf)753 advanceByOneOutOfLine(const FuncRecordTy *Record, const char *MappingBuf) {
754 return {MappingBuf + getDataSize<FuncRecordTy, Endian>(Record), Record + 1};
755 }
756
757 } // end namespace accessors
758
759 LLVM_PACKED_START
760 template <class IntPtrT>
761 struct CovMapFunctionRecordV1 {
762 using ThisT = CovMapFunctionRecordV1<IntPtrT>;
763
764 #define COVMAP_V1
765 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
766 #include "llvm/ProfileData/InstrProfData.inc"
767 #undef COVMAP_V1
768 CovMapFunctionRecordV1() = delete;
769
getFuncHashCovMapFunctionRecordV1770 template <support::endianness Endian> uint64_t getFuncHash() const {
771 return accessors::getFuncHash<ThisT, Endian>(this);
772 }
773
getDataSizeCovMapFunctionRecordV1774 template <support::endianness Endian> uint64_t getDataSize() const {
775 return accessors::getDataSize<ThisT, Endian>(this);
776 }
777
778 /// Return function lookup key. The value is consider opaque.
getFuncNameRefCovMapFunctionRecordV1779 template <support::endianness Endian> IntPtrT getFuncNameRef() const {
780 return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
781 }
782
783 /// Return the PGO name of the function.
784 template <support::endianness Endian>
getFuncNameCovMapFunctionRecordV1785 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
786 IntPtrT NameRef = getFuncNameRef<Endian>();
787 uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
788 FuncName = ProfileNames.getFuncName(NameRef, NameS);
789 if (NameS && FuncName.empty())
790 return make_error<CoverageMapError>(coveragemap_error::malformed);
791 return Error::success();
792 }
793
794 template <support::endianness Endian>
795 std::pair<const char *, const ThisT *>
advanceByOneCovMapFunctionRecordV1796 advanceByOne(const char *MappingBuf) const {
797 return accessors::advanceByOneOutOfLine<ThisT, Endian>(this, MappingBuf);
798 }
799
getFilenamesRefCovMapFunctionRecordV1800 template <support::endianness Endian> uint64_t getFilenamesRef() const {
801 llvm_unreachable("V1 function format does not contain a filenames ref");
802 }
803
804 template <support::endianness Endian>
getCoverageMappingCovMapFunctionRecordV1805 StringRef getCoverageMapping(const char *MappingBuf) const {
806 return accessors::getCoverageMappingOutOfLine<ThisT, Endian>(this,
807 MappingBuf);
808 }
809 };
810
811 struct CovMapFunctionRecordV2 {
812 using ThisT = CovMapFunctionRecordV2;
813
814 #define COVMAP_V2
815 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
816 #include "llvm/ProfileData/InstrProfData.inc"
817 #undef COVMAP_V2
818 CovMapFunctionRecordV2() = delete;
819
getFuncHashCovMapFunctionRecordV2820 template <support::endianness Endian> uint64_t getFuncHash() const {
821 return accessors::getFuncHash<ThisT, Endian>(this);
822 }
823
getDataSizeCovMapFunctionRecordV2824 template <support::endianness Endian> uint64_t getDataSize() const {
825 return accessors::getDataSize<ThisT, Endian>(this);
826 }
827
getFuncNameRefCovMapFunctionRecordV2828 template <support::endianness Endian> uint64_t getFuncNameRef() const {
829 return accessors::getFuncNameRef<ThisT, Endian>(this);
830 }
831
832 template <support::endianness Endian>
getFuncNameCovMapFunctionRecordV2833 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
834 return accessors::getFuncNameViaRef<ThisT, Endian>(this, ProfileNames,
835 FuncName);
836 }
837
838 template <support::endianness Endian>
839 std::pair<const char *, const ThisT *>
advanceByOneCovMapFunctionRecordV2840 advanceByOne(const char *MappingBuf) const {
841 return accessors::advanceByOneOutOfLine<ThisT, Endian>(this, MappingBuf);
842 }
843
getFilenamesRefCovMapFunctionRecordV2844 template <support::endianness Endian> uint64_t getFilenamesRef() const {
845 llvm_unreachable("V2 function format does not contain a filenames ref");
846 }
847
848 template <support::endianness Endian>
getCoverageMappingCovMapFunctionRecordV2849 StringRef getCoverageMapping(const char *MappingBuf) const {
850 return accessors::getCoverageMappingOutOfLine<ThisT, Endian>(this,
851 MappingBuf);
852 }
853 };
854
855 struct CovMapFunctionRecordV3 {
856 using ThisT = CovMapFunctionRecordV3;
857
858 #define COVMAP_V3
859 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
860 #include "llvm/ProfileData/InstrProfData.inc"
861 #undef COVMAP_V3
862 CovMapFunctionRecordV3() = delete;
863
getFuncHashCovMapFunctionRecordV3864 template <support::endianness Endian> uint64_t getFuncHash() const {
865 return accessors::getFuncHash<ThisT, Endian>(this);
866 }
867
getDataSizeCovMapFunctionRecordV3868 template <support::endianness Endian> uint64_t getDataSize() const {
869 return accessors::getDataSize<ThisT, Endian>(this);
870 }
871
getFuncNameRefCovMapFunctionRecordV3872 template <support::endianness Endian> uint64_t getFuncNameRef() const {
873 return accessors::getFuncNameRef<ThisT, Endian>(this);
874 }
875
876 template <support::endianness Endian>
getFuncNameCovMapFunctionRecordV3877 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
878 return accessors::getFuncNameViaRef<ThisT, Endian>(this, ProfileNames,
879 FuncName);
880 }
881
882 /// Get the filename set reference.
getFilenamesRefCovMapFunctionRecordV3883 template <support::endianness Endian> uint64_t getFilenamesRef() const {
884 return support::endian::byte_swap<uint64_t, Endian>(FilenamesRef);
885 }
886
887 /// Read the inline coverage mapping. Ignore the buffer parameter, it is for
888 /// out-of-line coverage mapping data only.
889 template <support::endianness Endian>
getCoverageMappingCovMapFunctionRecordV3890 StringRef getCoverageMapping(const char *) const {
891 return StringRef(&CoverageMapping, getDataSize<Endian>());
892 }
893
894 // Advance to the next inline coverage mapping and its associated function
895 // record. Ignore the out-of-line coverage mapping buffer.
896 template <support::endianness Endian>
897 std::pair<const char *, const CovMapFunctionRecordV3 *>
advanceByOneCovMapFunctionRecordV3898 advanceByOne(const char *) const {
899 assert(isAddrAligned(Align(8), this) && "Function record not aligned");
900 const char *Next = ((const char *)this) + sizeof(CovMapFunctionRecordV3) -
901 sizeof(char) + getDataSize<Endian>();
902 // Each function record has an alignment of 8, so we need to adjust
903 // alignment before reading the next record.
904 Next += offsetToAlignedAddr(Next, Align(8));
905 return {nullptr, reinterpret_cast<const CovMapFunctionRecordV3 *>(Next)};
906 }
907 };
908
909 // Per module coverage mapping data header, i.e. CoverageMapFileHeader
910 // documented above.
911 struct CovMapHeader {
912 #define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
913 #include "llvm/ProfileData/InstrProfData.inc"
getNRecordsCovMapHeader914 template <support::endianness Endian> uint32_t getNRecords() const {
915 return support::endian::byte_swap<uint32_t, Endian>(NRecords);
916 }
917
getFilenamesSizeCovMapHeader918 template <support::endianness Endian> uint32_t getFilenamesSize() const {
919 return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
920 }
921
getCoverageSizeCovMapHeader922 template <support::endianness Endian> uint32_t getCoverageSize() const {
923 return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
924 }
925
getVersionCovMapHeader926 template <support::endianness Endian> uint32_t getVersion() const {
927 return support::endian::byte_swap<uint32_t, Endian>(Version);
928 }
929 };
930
931 LLVM_PACKED_END
932
933 enum CovMapVersion {
934 Version1 = 0,
935 // Function's name reference from CovMapFuncRecord is changed from raw
936 // name string pointer to MD5 to support name section compression. Name
937 // section is also compressed.
938 Version2 = 1,
939 // A new interpretation of the columnEnd field is added in order to mark
940 // regions as gap areas.
941 Version3 = 2,
942 // Function records are named, uniqued, and moved to a dedicated section.
943 Version4 = 3,
944 // The current version is Version4.
945 CurrentVersion = INSTR_PROF_COVMAP_VERSION
946 };
947
948 template <int CovMapVersion, class IntPtrT> struct CovMapTraits {
949 using CovMapFuncRecordType = CovMapFunctionRecordV3;
950 using NameRefType = uint64_t;
951 };
952
953 template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version3, IntPtrT> {
954 using CovMapFuncRecordType = CovMapFunctionRecordV2;
955 using NameRefType = uint64_t;
956 };
957
958 template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version2, IntPtrT> {
959 using CovMapFuncRecordType = CovMapFunctionRecordV2;
960 using NameRefType = uint64_t;
961 };
962
963 template <class IntPtrT> struct CovMapTraits<CovMapVersion::Version1, IntPtrT> {
964 using CovMapFuncRecordType = CovMapFunctionRecordV1<IntPtrT>;
965 using NameRefType = IntPtrT;
966 };
967
968 } // end namespace coverage
969
970 /// Provide DenseMapInfo for CounterExpression
971 template<> struct DenseMapInfo<coverage::CounterExpression> {
972 static inline coverage::CounterExpression getEmptyKey() {
973 using namespace coverage;
974
975 return CounterExpression(CounterExpression::ExprKind::Subtract,
976 Counter::getCounter(~0U),
977 Counter::getCounter(~0U));
978 }
979
980 static inline coverage::CounterExpression getTombstoneKey() {
981 using namespace coverage;
982
983 return CounterExpression(CounterExpression::ExprKind::Add,
984 Counter::getCounter(~0U),
985 Counter::getCounter(~0U));
986 }
987
988 static unsigned getHashValue(const coverage::CounterExpression &V) {
989 return static_cast<unsigned>(
990 hash_combine(V.Kind, V.LHS.getKind(), V.LHS.getCounterID(),
991 V.RHS.getKind(), V.RHS.getCounterID()));
992 }
993
994 static bool isEqual(const coverage::CounterExpression &LHS,
995 const coverage::CounterExpression &RHS) {
996 return LHS.Kind == RHS.Kind && LHS.LHS == RHS.LHS && LHS.RHS == RHS.RHS;
997 }
998 };
999
1000 } // end namespace llvm
1001
1002 #endif // LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H
1003