1*e78d131aSEugene Zelenko //===- CoverageMappingWriter.cpp - Code coverage mapping writer -----------===// 2dc707122SEaswaran Raman // 3dc707122SEaswaran Raman // The LLVM Compiler Infrastructure 4dc707122SEaswaran Raman // 5dc707122SEaswaran Raman // This file is distributed under the University of Illinois Open Source 6dc707122SEaswaran Raman // License. See LICENSE.TXT for details. 7dc707122SEaswaran Raman // 8dc707122SEaswaran Raman //===----------------------------------------------------------------------===// 9dc707122SEaswaran Raman // 10dc707122SEaswaran Raman // This file contains support for writing coverage mapping data for 11dc707122SEaswaran Raman // instrumentation based coverage. 12dc707122SEaswaran Raman // 13dc707122SEaswaran Raman //===----------------------------------------------------------------------===// 14dc707122SEaswaran Raman 15*e78d131aSEugene Zelenko #include "llvm/ADT/ArrayRef.h" 16*e78d131aSEugene Zelenko #include "llvm/ADT/SmallVector.h" 17dc707122SEaswaran Raman #include "llvm/ProfileData/Coverage/CoverageMappingWriter.h" 18dc707122SEaswaran Raman #include "llvm/Support/LEB128.h" 19*e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h" 20*e78d131aSEugene Zelenko #include <algorithm> 21*e78d131aSEugene Zelenko #include <cassert> 22*e78d131aSEugene Zelenko #include <limits> 23*e78d131aSEugene Zelenko #include <vector> 24dc707122SEaswaran Raman 25dc707122SEaswaran Raman using namespace llvm; 26dc707122SEaswaran Raman using namespace coverage; 27dc707122SEaswaran Raman 2834e4e477SVedant Kumar void CoverageFilenamesSectionWriter::write(raw_ostream &OS) { 2934e4e477SVedant Kumar encodeULEB128(Filenames.size(), OS); 3034e4e477SVedant Kumar for (const auto &Filename : Filenames) { 3134e4e477SVedant Kumar encodeULEB128(Filename.size(), OS); 3234e4e477SVedant Kumar OS << Filename; 3334e4e477SVedant Kumar } 3434e4e477SVedant Kumar } 3534e4e477SVedant Kumar 36dc707122SEaswaran Raman namespace { 37*e78d131aSEugene Zelenko 38dc707122SEaswaran Raman /// \brief Gather only the expressions that are used by the mapping 39dc707122SEaswaran Raman /// regions in this function. 40dc707122SEaswaran Raman class CounterExpressionsMinimizer { 41dc707122SEaswaran Raman ArrayRef<CounterExpression> Expressions; 42*e78d131aSEugene Zelenko SmallVector<CounterExpression, 16> UsedExpressions; 43dc707122SEaswaran Raman std::vector<unsigned> AdjustedExpressionIDs; 44dc707122SEaswaran Raman 45dc707122SEaswaran Raman public: 46*e78d131aSEugene Zelenko CounterExpressionsMinimizer(ArrayRef<CounterExpression> Expressions, 47*e78d131aSEugene Zelenko ArrayRef<CounterMappingRegion> MappingRegions) 48*e78d131aSEugene Zelenko : Expressions(Expressions) { 49*e78d131aSEugene Zelenko AdjustedExpressionIDs.resize(Expressions.size(), 0); 50*e78d131aSEugene Zelenko for (const auto &I : MappingRegions) 51*e78d131aSEugene Zelenko mark(I.Count); 52*e78d131aSEugene Zelenko for (const auto &I : MappingRegions) 53*e78d131aSEugene Zelenko gatherUsed(I.Count); 54*e78d131aSEugene Zelenko } 55*e78d131aSEugene Zelenko 56dc707122SEaswaran Raman void mark(Counter C) { 57dc707122SEaswaran Raman if (!C.isExpression()) 58dc707122SEaswaran Raman return; 59dc707122SEaswaran Raman unsigned ID = C.getExpressionID(); 60dc707122SEaswaran Raman AdjustedExpressionIDs[ID] = 1; 61dc707122SEaswaran Raman mark(Expressions[ID].LHS); 62dc707122SEaswaran Raman mark(Expressions[ID].RHS); 63dc707122SEaswaran Raman } 64dc707122SEaswaran Raman 65dc707122SEaswaran Raman void gatherUsed(Counter C) { 66dc707122SEaswaran Raman if (!C.isExpression() || !AdjustedExpressionIDs[C.getExpressionID()]) 67dc707122SEaswaran Raman return; 68dc707122SEaswaran Raman AdjustedExpressionIDs[C.getExpressionID()] = UsedExpressions.size(); 69dc707122SEaswaran Raman const auto &E = Expressions[C.getExpressionID()]; 70dc707122SEaswaran Raman UsedExpressions.push_back(E); 71dc707122SEaswaran Raman gatherUsed(E.LHS); 72dc707122SEaswaran Raman gatherUsed(E.RHS); 73dc707122SEaswaran Raman } 74dc707122SEaswaran Raman 75dc707122SEaswaran Raman ArrayRef<CounterExpression> getExpressions() const { return UsedExpressions; } 76dc707122SEaswaran Raman 77dc707122SEaswaran Raman /// \brief Adjust the given counter to correctly transition from the old 78dc707122SEaswaran Raman /// expression ids to the new expression ids. 79dc707122SEaswaran Raman Counter adjust(Counter C) const { 80dc707122SEaswaran Raman if (C.isExpression()) 81dc707122SEaswaran Raman C = Counter::getExpression(AdjustedExpressionIDs[C.getExpressionID()]); 82dc707122SEaswaran Raman return C; 83dc707122SEaswaran Raman } 84dc707122SEaswaran Raman }; 85*e78d131aSEugene Zelenko 86*e78d131aSEugene Zelenko } // end anonymous namespace 87dc707122SEaswaran Raman 88dc707122SEaswaran Raman /// \brief Encode the counter. 89dc707122SEaswaran Raman /// 90dc707122SEaswaran Raman /// The encoding uses the following format: 91dc707122SEaswaran Raman /// Low 2 bits - Tag: 92dc707122SEaswaran Raman /// Counter::Zero(0) - A Counter with kind Counter::Zero 93dc707122SEaswaran Raman /// Counter::CounterValueReference(1) - A counter with kind 94dc707122SEaswaran Raman /// Counter::CounterValueReference 95dc707122SEaswaran Raman /// Counter::Expression(2) + CounterExpression::Subtract(0) - 96dc707122SEaswaran Raman /// A counter with kind Counter::Expression and an expression 97dc707122SEaswaran Raman /// with kind CounterExpression::Subtract 98dc707122SEaswaran Raman /// Counter::Expression(2) + CounterExpression::Add(1) - 99dc707122SEaswaran Raman /// A counter with kind Counter::Expression and an expression 100dc707122SEaswaran Raman /// with kind CounterExpression::Add 101dc707122SEaswaran Raman /// Remaining bits - Counter/Expression ID. 102dc707122SEaswaran Raman static unsigned encodeCounter(ArrayRef<CounterExpression> Expressions, 103dc707122SEaswaran Raman Counter C) { 104dc707122SEaswaran Raman unsigned Tag = unsigned(C.getKind()); 105dc707122SEaswaran Raman if (C.isExpression()) 106dc707122SEaswaran Raman Tag += Expressions[C.getExpressionID()].Kind; 107dc707122SEaswaran Raman unsigned ID = C.getCounterID(); 108dc707122SEaswaran Raman assert(ID <= 109dc707122SEaswaran Raman (std::numeric_limits<unsigned>::max() >> Counter::EncodingTagBits)); 110dc707122SEaswaran Raman return Tag | (ID << Counter::EncodingTagBits); 111dc707122SEaswaran Raman } 112dc707122SEaswaran Raman 113dc707122SEaswaran Raman static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C, 114dc707122SEaswaran Raman raw_ostream &OS) { 115dc707122SEaswaran Raman encodeULEB128(encodeCounter(Expressions, C), OS); 116dc707122SEaswaran Raman } 117dc707122SEaswaran Raman 118dc707122SEaswaran Raman void CoverageMappingWriter::write(raw_ostream &OS) { 119dc707122SEaswaran Raman // Sort the regions in an ascending order by the file id and the starting 120f3c8a9cfSIgor Kudrin // location. Sort by region kinds to ensure stable order for tests. 121f3c8a9cfSIgor Kudrin std::stable_sort( 122f3c8a9cfSIgor Kudrin MappingRegions.begin(), MappingRegions.end(), 123f3c8a9cfSIgor Kudrin [](const CounterMappingRegion &LHS, const CounterMappingRegion &RHS) { 124f3c8a9cfSIgor Kudrin if (LHS.FileID != RHS.FileID) 125f3c8a9cfSIgor Kudrin return LHS.FileID < RHS.FileID; 126f3c8a9cfSIgor Kudrin if (LHS.startLoc() != RHS.startLoc()) 127f3c8a9cfSIgor Kudrin return LHS.startLoc() < RHS.startLoc(); 128f3c8a9cfSIgor Kudrin return LHS.Kind < RHS.Kind; 129f3c8a9cfSIgor Kudrin }); 130dc707122SEaswaran Raman 131dc707122SEaswaran Raman // Write out the fileid -> filename mapping. 132dc707122SEaswaran Raman encodeULEB128(VirtualFileMapping.size(), OS); 133dc707122SEaswaran Raman for (const auto &FileID : VirtualFileMapping) 134dc707122SEaswaran Raman encodeULEB128(FileID, OS); 135dc707122SEaswaran Raman 136dc707122SEaswaran Raman // Write out the expressions. 137dc707122SEaswaran Raman CounterExpressionsMinimizer Minimizer(Expressions, MappingRegions); 138dc707122SEaswaran Raman auto MinExpressions = Minimizer.getExpressions(); 139dc707122SEaswaran Raman encodeULEB128(MinExpressions.size(), OS); 140dc707122SEaswaran Raman for (const auto &E : MinExpressions) { 141dc707122SEaswaran Raman writeCounter(MinExpressions, Minimizer.adjust(E.LHS), OS); 142dc707122SEaswaran Raman writeCounter(MinExpressions, Minimizer.adjust(E.RHS), OS); 143dc707122SEaswaran Raman } 144dc707122SEaswaran Raman 145dc707122SEaswaran Raman // Write out the mapping regions. 146dc707122SEaswaran Raman // Split the regions into subarrays where each region in a 147dc707122SEaswaran Raman // subarray has a fileID which is the index of that subarray. 148dc707122SEaswaran Raman unsigned PrevLineStart = 0; 149dc707122SEaswaran Raman unsigned CurrentFileID = ~0U; 150dc707122SEaswaran Raman for (auto I = MappingRegions.begin(), E = MappingRegions.end(); I != E; ++I) { 151dc707122SEaswaran Raman if (I->FileID != CurrentFileID) { 152dc707122SEaswaran Raman // Ensure that all file ids have at least one mapping region. 153dc707122SEaswaran Raman assert(I->FileID == (CurrentFileID + 1)); 154dc707122SEaswaran Raman // Find the number of regions with this file id. 155dc707122SEaswaran Raman unsigned RegionCount = 1; 156dc707122SEaswaran Raman for (auto J = I + 1; J != E && I->FileID == J->FileID; ++J) 157dc707122SEaswaran Raman ++RegionCount; 158dc707122SEaswaran Raman // Start a new region sub-array. 159dc707122SEaswaran Raman encodeULEB128(RegionCount, OS); 160dc707122SEaswaran Raman 161dc707122SEaswaran Raman CurrentFileID = I->FileID; 162dc707122SEaswaran Raman PrevLineStart = 0; 163dc707122SEaswaran Raman } 164dc707122SEaswaran Raman Counter Count = Minimizer.adjust(I->Count); 165dc707122SEaswaran Raman switch (I->Kind) { 166dc707122SEaswaran Raman case CounterMappingRegion::CodeRegion: 167dc707122SEaswaran Raman writeCounter(MinExpressions, Count, OS); 168dc707122SEaswaran Raman break; 169dc707122SEaswaran Raman case CounterMappingRegion::ExpansionRegion: { 170dc707122SEaswaran Raman assert(Count.isZero()); 171dc707122SEaswaran Raman assert(I->ExpandedFileID <= 172dc707122SEaswaran Raman (std::numeric_limits<unsigned>::max() >> 173dc707122SEaswaran Raman Counter::EncodingCounterTagAndExpansionRegionTagBits)); 174dc707122SEaswaran Raman // Mark an expansion region with a set bit that follows the counter tag, 175dc707122SEaswaran Raman // and pack the expanded file id into the remaining bits. 176dc707122SEaswaran Raman unsigned EncodedTagExpandedFileID = 177dc707122SEaswaran Raman (1 << Counter::EncodingTagBits) | 178dc707122SEaswaran Raman (I->ExpandedFileID 179dc707122SEaswaran Raman << Counter::EncodingCounterTagAndExpansionRegionTagBits); 180dc707122SEaswaran Raman encodeULEB128(EncodedTagExpandedFileID, OS); 181dc707122SEaswaran Raman break; 182dc707122SEaswaran Raman } 183dc707122SEaswaran Raman case CounterMappingRegion::SkippedRegion: 184dc707122SEaswaran Raman assert(Count.isZero()); 185dc707122SEaswaran Raman encodeULEB128(unsigned(I->Kind) 186dc707122SEaswaran Raman << Counter::EncodingCounterTagAndExpansionRegionTagBits, 187dc707122SEaswaran Raman OS); 188dc707122SEaswaran Raman break; 189dc707122SEaswaran Raman } 190dc707122SEaswaran Raman assert(I->LineStart >= PrevLineStart); 191dc707122SEaswaran Raman encodeULEB128(I->LineStart - PrevLineStart, OS); 192dc707122SEaswaran Raman encodeULEB128(I->ColumnStart, OS); 193dc707122SEaswaran Raman assert(I->LineEnd >= I->LineStart); 194dc707122SEaswaran Raman encodeULEB128(I->LineEnd - I->LineStart, OS); 195dc707122SEaswaran Raman encodeULEB128(I->ColumnEnd, OS); 196dc707122SEaswaran Raman PrevLineStart = I->LineStart; 197dc707122SEaswaran Raman } 198dc707122SEaswaran Raman // Ensure that all file ids have at least one mapping region. 199dc707122SEaswaran Raman assert(CurrentFileID == (VirtualFileMapping.size() - 1)); 200dc707122SEaswaran Raman } 201