diff options
author | NAKAMURA Takumi <geek4civic@gmail.com> | 2024-02-15 16:24:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-15 16:24:37 +0900 |
commit | ab76e48ac2c2dbfc7d6a600b9b0dd0672e6d9439 (patch) | |
tree | 47f591cd0cc4743f4f8e8e84f0df17c653e3384e /llvm/lib/ProfileData | |
parent | 36adfec155de366d722f2bac8ff9162289dcf06c (diff) | |
download | llvm-ab76e48ac2c2dbfc7d6a600b9b0dd0672e6d9439.zip llvm-ab76e48ac2c2dbfc7d6a600b9b0dd0672e6d9439.tar.gz llvm-ab76e48ac2c2dbfc7d6a600b9b0dd0672e6d9439.tar.bz2 |
[MC/DC] Refactor: Let MCDCConditionID int16_t with zero-origin (#81257)
Also, Let `NumConditions` `uint16_t`.
It is smarter to handle the ID as signed.
Narrowing to `int16_t` will reduce costs of handling byvalue. (See also
#81221 and #81227)
External behavior doesn't change. They below handle values as internal
values plus 1.
* `-dump-coverage-mapping`
* `CoverageMappingReader.cpp`
* `CoverageMappingWriter.cpp`
Diffstat (limited to 'llvm/lib/ProfileData')
-rw-r--r-- | llvm/lib/ProfileData/Coverage/CoverageMapping.cpp | 26 | ||||
-rw-r--r-- | llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp | 21 | ||||
-rw-r--r-- | llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp | 12 |
3 files changed, 33 insertions, 26 deletions
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp index 9adeceb..ddce758 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -272,17 +272,17 @@ private: // Walk the binary decision diagram and try assigning both false and true to // each node. When a terminal node (ID == 0) is reached, fill in the value in // the truth table. - void buildTestVector(MCDCRecord::TestVector &TV, unsigned ID, + void buildTestVector(MCDCRecord::TestVector &TV, mcdc::ConditionID ID, unsigned Index) { - assert((Index & (1 << (ID - 1))) == 0); + assert((Index & (1 << ID)) == 0); for (auto MCDCCond : {MCDCRecord::MCDC_False, MCDCRecord::MCDC_True}) { static_assert(MCDCRecord::MCDC_False == 0); static_assert(MCDCRecord::MCDC_True == 1); - Index |= MCDCCond << (ID - 1); - TV[ID - 1] = MCDCCond; + Index |= MCDCCond << ID; + TV[ID] = MCDCCond; auto NextID = CondsMap[ID][MCDCCond]; - if (NextID > 0) { + if (NextID >= 0) { buildTestVector(TV, NextID, Index); continue; } @@ -299,17 +299,17 @@ private: } // Reset back to DontCare. - TV[ID - 1] = MCDCRecord::MCDC_DontCare; + TV[ID] = MCDCRecord::MCDC_DontCare; } /// Walk the bits in the bitmap. A bit set to '1' indicates that the test /// vector at the corresponding index was executed during a test run. void findExecutedTestVectors() { // Walk the binary decision diagram to enumerate all possible test vectors. - // We start at the root node (ID == 1) with all values being DontCare. + // We start at the root node (ID == 0) with all values being DontCare. // `Index` encodes the bitmask of true values and is initially 0. MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare); - buildTestVector(TV, 1, 0); + buildTestVector(TV, 0, 0); } // Find an independence pair for each condition: @@ -371,7 +371,7 @@ public: for (const auto *B : Branches) { const auto &BranchParams = B->getBranchParams(); CondsMap[BranchParams.ID] = BranchParams.Conds; - PosToID[I] = BranchParams.ID - 1; + PosToID[I] = BranchParams.ID; CondLoc[I] = B->startLoc(); Folded[I++] = (B->Count.isZero() && B->FalseCount.isZero()); } @@ -566,10 +566,10 @@ private: assert(Branch.Kind == CounterMappingRegion::MCDCBranchRegion); auto ConditionID = Branch.getBranchParams().ID; - assert(ConditionID > 0 && "ConditionID should begin with 1"); + assert(ConditionID >= 0 && "ConditionID should be positive"); if (ConditionIDs.contains(ConditionID) || - ConditionID > DecisionParams.NumConditions) + ConditionID >= DecisionParams.NumConditions) return NotProcessed; if (!this->dominates(Branch)) @@ -577,9 +577,9 @@ private: assert(MCDCBranches.size() < DecisionParams.NumConditions); - // Put `ID=1` in front of `MCDCBranches` for convenience + // Put `ID=0` in front of `MCDCBranches` for convenience // even if `MCDCBranches` is not topological. - if (ConditionID == 1) + if (ConditionID == 0) MCDCBranches.insert(MCDCBranches.begin(), &Branch); else MCDCBranches.push_back(&Branch); diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp index de7be52..d328460 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -244,7 +244,9 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( unsigned LineStart = 0; for (size_t I = 0; I < NumRegions; ++I) { Counter C, C2; - uint64_t BIDX, NC, ID, TID, FID; + uint64_t BIDX, NC; + // They are stored as internal values plus 1 (min is -1) + uint64_t ID1, TID1, FID1; mcdc::Parameters Params; CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion; @@ -303,28 +305,29 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( return Err; if (auto Err = readCounter(C2)) return Err; - if (auto Err = readIntMax(ID, std::numeric_limits<unsigned>::max())) + if (auto Err = readIntMax(ID1, std::numeric_limits<int16_t>::max())) return Err; - if (auto Err = readIntMax(TID, std::numeric_limits<unsigned>::max())) + if (auto Err = readIntMax(TID1, std::numeric_limits<int16_t>::max())) return Err; - if (auto Err = readIntMax(FID, std::numeric_limits<unsigned>::max())) + if (auto Err = readIntMax(FID1, std::numeric_limits<int16_t>::max())) return Err; - if (ID == 0) + if (ID1 == 0) return make_error<CoverageMapError>( coveragemap_error::malformed, "MCDCConditionID shouldn't be zero"); Params = mcdc::BranchParameters{ - static_cast<unsigned>(ID), - {static_cast<unsigned>(FID), static_cast<unsigned>(TID)}}; + static_cast<int16_t>(static_cast<int16_t>(ID1) - 1), + {static_cast<int16_t>(static_cast<int16_t>(FID1) - 1), + static_cast<int16_t>(static_cast<int16_t>(TID1) - 1)}}; break; case CounterMappingRegion::MCDCDecisionRegion: Kind = CounterMappingRegion::MCDCDecisionRegion; if (auto Err = readIntMax(BIDX, std::numeric_limits<unsigned>::max())) return Err; - if (auto Err = readIntMax(NC, std::numeric_limits<unsigned>::max())) + if (auto Err = readIntMax(NC, std::numeric_limits<int16_t>::max())) return Err; Params = mcdc::DecisionParameters{static_cast<unsigned>(BIDX), - static_cast<unsigned>(NC)}; + static_cast<uint16_t>(NC)}; break; default: return make_error<CoverageMapError>(coveragemap_error::malformed, diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp index 6125cce..5036bde 100644 --- a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp @@ -253,12 +253,16 @@ void CoverageMappingWriter::write(raw_ostream &OS) { writeCounter(MinExpressions, Count, OS); writeCounter(MinExpressions, FalseCount, OS); { + // They are written as internal values plus 1. const auto &BranchParams = I->getBranchParams(); ParamsShouldBeNull = false; - assert(BranchParams.ID > 0); - encodeULEB128(static_cast<unsigned>(BranchParams.ID), OS); - encodeULEB128(static_cast<unsigned>(BranchParams.Conds[true]), OS); - encodeULEB128(static_cast<unsigned>(BranchParams.Conds[false]), OS); + assert(BranchParams.ID >= 0); + unsigned ID1 = BranchParams.ID + 1; + unsigned TID1 = BranchParams.Conds[true] + 1; + unsigned FID1 = BranchParams.Conds[false] + 1; + encodeULEB128(ID1, OS); + encodeULEB128(TID1, OS); + encodeULEB128(FID1, OS); } break; case CounterMappingRegion::MCDCDecisionRegion: |