CoverageMapping.h 38 KB

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