1 //=-- CoverageMapping.cpp - Code coverage mapping support ---------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for clang's and llvm's instrumentation based
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/CoverageMapping.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ProfileData/CoverageMappingReader.h"
21 #include "llvm/ProfileData/InstrProfReader.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
26 using namespace coverage;
28 #define DEBUG_TYPE "coverage-mapping"
30 CounterExpressionBuilder::CounterExpressionBuilder(unsigned NumCounterValues) {
31 Terms.resize(NumCounterValues);
34 Counter CounterExpressionBuilder::get(const CounterExpression &E) {
35 for (unsigned I = 0, S = Expressions.size(); I < S; ++I) {
36 if (Expressions[I] == E)
37 return Counter::getExpression(I);
39 Expressions.push_back(E);
40 return Counter::getExpression(Expressions.size() - 1);
43 void CounterExpressionBuilder::extractTerms(Counter C, int Sign) {
44 switch (C.getKind()) {
47 case Counter::CounterValueReference:
48 Terms[C.getCounterID()] += Sign;
50 case Counter::Expression:
51 const auto &E = Expressions[C.getExpressionID()];
52 extractTerms(E.LHS, Sign);
53 extractTerms(E.RHS, E.Kind == CounterExpression::Subtract ? -Sign : Sign);
58 Counter CounterExpressionBuilder::simplify(Counter ExpressionTree) {
59 // Gather constant terms.
62 extractTerms(ExpressionTree);
66 // Note: the additions are created first
67 // to avoid creation of a tree like ((0 - X) + Y) instead of (Y - X).
68 for (unsigned I = 0, S = Terms.size(); I < S; ++I) {
71 for (int J = 0; J < Terms[I]; ++J) {
73 C = Counter::getCounter(I);
75 C = get(CounterExpression(CounterExpression::Add, C,
76 Counter::getCounter(I)));
80 // Create subtractions.
81 for (unsigned I = 0, S = Terms.size(); I < S; ++I) {
84 for (int J = 0; J < (-Terms[I]); ++J)
85 C = get(CounterExpression(CounterExpression::Subtract, C,
86 Counter::getCounter(I)));
91 Counter CounterExpressionBuilder::add(Counter LHS, Counter RHS) {
92 return simplify(get(CounterExpression(CounterExpression::Add, LHS, RHS)));
95 Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS) {
97 get(CounterExpression(CounterExpression::Subtract, LHS, RHS)));
100 void CounterMappingContext::dump(const Counter &C,
101 llvm::raw_ostream &OS) const {
102 switch (C.getKind()) {
106 case Counter::CounterValueReference:
107 OS << '#' << C.getCounterID();
109 case Counter::Expression: {
110 if (C.getExpressionID() >= Expressions.size())
112 const auto &E = Expressions[C.getExpressionID()];
115 OS << (E.Kind == CounterExpression::Subtract ? " - " : " + ");
121 if (CounterValues.empty())
123 ErrorOr<int64_t> Value = evaluate(C);
126 OS << '[' << *Value << ']';
129 ErrorOr<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
130 switch (C.getKind()) {
133 case Counter::CounterValueReference:
134 if (C.getCounterID() >= CounterValues.size())
135 return std::make_error_code(std::errc::argument_out_of_domain);
136 return CounterValues[C.getCounterID()];
137 case Counter::Expression: {
138 if (C.getExpressionID() >= Expressions.size())
139 return std::make_error_code(std::errc::argument_out_of_domain);
140 const auto &E = Expressions[C.getExpressionID()];
141 ErrorOr<int64_t> LHS = evaluate(E.LHS);
144 ErrorOr<int64_t> RHS = evaluate(E.RHS);
147 return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
150 llvm_unreachable("Unhandled CounterKind");
153 ErrorOr<std::unique_ptr<CoverageMapping>>
154 CoverageMapping::load(ObjectFileCoverageMappingReader &CoverageReader,
155 IndexedInstrProfReader &ProfileReader) {
156 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping());
158 std::vector<uint64_t> Counts;
159 for (const auto &Record : CoverageReader) {
161 if (std::error_code EC = ProfileReader.getFunctionCounts(
162 Record.FunctionName, Record.FunctionHash, Counts)) {
163 if (EC != instrprof_error::hash_mismatch &&
164 EC != instrprof_error::unknown_function)
166 Coverage->MismatchedFunctionCount++;
170 assert(Counts.size() != 0 && "Function's counts are empty");
171 FunctionRecord Function(Record.FunctionName, Record.Filenames,
173 CounterMappingContext Ctx(Record.Expressions, Counts);
174 for (const auto &Region : Record.MappingRegions) {
175 ErrorOr<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
178 Function.CountedRegions.push_back(CountedRegion(Region, *ExecutionCount));
180 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
181 Coverage->MismatchedFunctionCount++;
185 Coverage->Functions.push_back(Function);
188 return std::move(Coverage);
191 ErrorOr<std::unique_ptr<CoverageMapping>>
192 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename) {
193 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
194 if (auto EC = CounterMappingBuff.getError())
196 ObjectFileCoverageMappingReader CoverageReader(CounterMappingBuff.get());
197 if (auto EC = CoverageReader.readHeader())
199 std::unique_ptr<IndexedInstrProfReader> ProfileReader;
200 if (auto EC = IndexedInstrProfReader::create(ProfileFilename, ProfileReader))
202 return load(CoverageReader, *ProfileReader);
206 /// \brief Distributes functions into instantiation sets.
208 /// An instantiation set is a collection of functions that have the same source
209 /// code, ie, template functions specializations.
210 class FunctionInstantiationSetCollector {
211 typedef DenseMap<std::pair<unsigned, unsigned>,
212 std::vector<const FunctionRecord *>> MapT;
213 MapT InstantiatedFunctions;
216 void insert(const FunctionRecord &Function, unsigned FileID) {
217 auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
218 while (I != E && I->FileID != FileID)
220 assert(I != E && "function does not cover the given file");
221 auto &Functions = InstantiatedFunctions[I->startLoc()];
222 Functions.push_back(&Function);
225 MapT::iterator begin() { return InstantiatedFunctions.begin(); }
227 MapT::iterator end() { return InstantiatedFunctions.end(); }
230 class SegmentBuilder {
231 std::vector<CoverageSegment> Segments;
232 SmallVector<const CountedRegion *, 8> ActiveRegions;
234 /// Start a segment with no count specified.
235 void startSegment(unsigned Line, unsigned Col) {
236 DEBUG(dbgs() << "Top level segment at " << Line << ":" << Col << "\n");
237 Segments.emplace_back(Line, Col, /*IsRegionEntry=*/false);
240 /// Start a segment with the given Region's count.
241 void startSegment(unsigned Line, unsigned Col, bool IsRegionEntry,
242 const CountedRegion &Region) {
243 if (Segments.empty())
244 Segments.emplace_back(Line, Col, IsRegionEntry);
245 CoverageSegment S = Segments.back();
246 // Avoid creating empty regions.
247 if (S.Line != Line || S.Col != Col) {
248 Segments.emplace_back(Line, Col, IsRegionEntry);
251 DEBUG(dbgs() << "Segment at " << Line << ":" << Col);
252 // Set this region's count.
253 if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion) {
254 DEBUG(dbgs() << " with count " << Region.ExecutionCount);
255 Segments.back().setCount(Region.ExecutionCount);
257 DEBUG(dbgs() << "\n");
260 /// Start a segment for the given region.
261 void startSegment(const CountedRegion &Region) {
262 startSegment(Region.LineStart, Region.ColumnStart, true, Region);
265 /// Pop the top region off of the active stack, starting a new segment with
266 /// the containing Region's count.
268 const CountedRegion *Active = ActiveRegions.back();
269 unsigned Line = Active->LineEnd, Col = Active->ColumnEnd;
270 ActiveRegions.pop_back();
271 if (ActiveRegions.empty())
272 startSegment(Line, Col);
274 startSegment(Line, Col, false, *ActiveRegions.back());
278 /// Build a list of CoverageSegments from a sorted list of Regions.
279 std::vector<CoverageSegment> buildSegments(ArrayRef<CountedRegion> Regions) {
280 for (const auto &Region : Regions) {
281 // Pop any regions that end before this one starts.
282 while (!ActiveRegions.empty() &&
283 ActiveRegions.back()->endLoc() <= Region.startLoc())
285 if (Segments.size() && Segments.back().Line == Region.LineStart &&
286 Segments.back().Col == Region.ColumnStart) {
287 if (Region.Kind != coverage::CounterMappingRegion::SkippedRegion)
288 Segments.back().addCount(Region.ExecutionCount);
290 // Add this region to the stack.
291 ActiveRegions.push_back(&Region);
292 startSegment(Region);
295 // Pop any regions that are left in the stack.
296 while (!ActiveRegions.empty())
303 std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() {
304 std::vector<StringRef> Filenames;
305 for (const auto &Function : getCoveredFunctions())
306 for (const auto &Filename : Function.Filenames)
307 Filenames.push_back(Filename);
308 std::sort(Filenames.begin(), Filenames.end());
309 auto Last = std::unique(Filenames.begin(), Filenames.end());
310 Filenames.erase(Last, Filenames.end());
314 static Optional<unsigned> findMainViewFileID(StringRef SourceFile,
315 const FunctionRecord &Function) {
316 llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
317 llvm::SmallVector<bool, 8> FilenameEquivalence(Function.Filenames.size(),
319 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
320 if (SourceFile == Function.Filenames[I])
321 FilenameEquivalence[I] = true;
322 for (const auto &CR : Function.CountedRegions)
323 if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
324 FilenameEquivalence[CR.FileID])
325 IsExpandedFile[CR.ExpandedFileID] = true;
326 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
327 if (FilenameEquivalence[I] && !IsExpandedFile[I])
332 static Optional<unsigned> findMainViewFileID(const FunctionRecord &Function) {
333 llvm::SmallVector<bool, 8> IsExpandedFile(Function.Filenames.size(), false);
334 for (const auto &CR : Function.CountedRegions)
335 if (CR.Kind == CounterMappingRegion::ExpansionRegion)
336 IsExpandedFile[CR.ExpandedFileID] = true;
337 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
338 if (!IsExpandedFile[I])
343 static SmallSet<unsigned, 8> gatherFileIDs(StringRef SourceFile,
344 const FunctionRecord &Function) {
345 SmallSet<unsigned, 8> IDs;
346 for (unsigned I = 0, E = Function.Filenames.size(); I < E; ++I)
347 if (SourceFile == Function.Filenames[I])
352 /// Sort a nested sequence of regions from a single file.
353 template <class It> static void sortNestedRegions(It First, It Last) {
354 std::sort(First, Last,
355 [](const CountedRegion &LHS, const CountedRegion &RHS) {
356 if (LHS.startLoc() == RHS.startLoc())
357 // When LHS completely contains RHS, we sort LHS first.
358 return RHS.endLoc() < LHS.endLoc();
359 return LHS.startLoc() < RHS.startLoc();
363 static bool isExpansion(const CountedRegion &R, unsigned FileID) {
364 return R.Kind == CounterMappingRegion::ExpansionRegion && R.FileID == FileID;
367 CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) {
368 CoverageData FileCoverage(Filename);
369 std::vector<coverage::CountedRegion> Regions;
371 for (const auto &Function : Functions) {
372 auto MainFileID = findMainViewFileID(Filename, Function);
375 auto FileIDs = gatherFileIDs(Filename, Function);
376 for (const auto &CR : Function.CountedRegions)
377 if (FileIDs.count(CR.FileID)) {
378 Regions.push_back(CR);
379 if (isExpansion(CR, *MainFileID))
380 FileCoverage.Expansions.emplace_back(CR, Function);
384 sortNestedRegions(Regions.begin(), Regions.end());
385 FileCoverage.Segments = SegmentBuilder().buildSegments(Regions);
390 std::vector<const FunctionRecord *>
391 CoverageMapping::getInstantiations(StringRef Filename) {
392 FunctionInstantiationSetCollector InstantiationSetCollector;
393 for (const auto &Function : Functions) {
394 auto MainFileID = findMainViewFileID(Filename, Function);
397 InstantiationSetCollector.insert(Function, *MainFileID);
400 std::vector<const FunctionRecord *> Result;
401 for (const auto &InstantiationSet : InstantiationSetCollector) {
402 if (InstantiationSet.second.size() < 2)
404 for (auto Function : InstantiationSet.second)
405 Result.push_back(Function);
411 CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) {
412 auto MainFileID = findMainViewFileID(Function);
414 return CoverageData();
416 CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
417 std::vector<coverage::CountedRegion> Regions;
418 for (const auto &CR : Function.CountedRegions)
419 if (CR.FileID == *MainFileID) {
420 Regions.push_back(CR);
421 if (isExpansion(CR, *MainFileID))
422 FunctionCoverage.Expansions.emplace_back(CR, Function);
425 sortNestedRegions(Regions.begin(), Regions.end());
426 FunctionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
428 return FunctionCoverage;
432 CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) {
433 CoverageData ExpansionCoverage(
434 Expansion.Function.Filenames[Expansion.FileID]);
435 std::vector<coverage::CountedRegion> Regions;
436 for (const auto &CR : Expansion.Function.CountedRegions)
437 if (CR.FileID == Expansion.FileID) {
438 Regions.push_back(CR);
439 if (isExpansion(CR, Expansion.FileID))
440 ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function);
443 sortNestedRegions(Regions.begin(), Regions.end());
444 ExpansionCoverage.Segments = SegmentBuilder().buildSegments(Regions);
446 return ExpansionCoverage;