CoverageMapping.h 38 KB

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