From ab76e48ac2c2dbfc7d6a600b9b0dd0672e6d9439 Mon Sep 17 00:00:00 2001 From: NAKAMURA Takumi Date: Thu, 15 Feb 2024 16:24:37 +0900 Subject: [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` --- clang/lib/CodeGen/CoverageMappingGen.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'clang/lib/CodeGen/CoverageMappingGen.cpp') diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp index 93c3c31..fdf821a 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -686,11 +686,12 @@ private: llvm::SmallVector DecisionStack; MCDC::State &MCDCState; llvm::DenseMap &CondIDs; - mcdc::ConditionID NextID = 1; + mcdc::ConditionID NextID = 0; bool NotMapped = false; - /// Represent a sentinel value of [0,0] for the bottom of DecisionStack. - static constexpr mcdc::ConditionIDs DecisionStackSentinel{0, 0}; + /// Represent a sentinel value as a pair of final decisions for the bottom + // of DecisionStack. + static constexpr mcdc::ConditionIDs DecisionStackSentinel{-1, -1}; /// Is this a logical-AND operation? bool isLAnd(const BinaryOperator *E) const { @@ -705,12 +706,12 @@ public: /// Return whether the build of the control flow map is at the top-level /// (root) of a logical operator nest in a boolean expression prior to the /// assignment of condition IDs. - bool isIdle() const { return (NextID == 1 && !NotMapped); } + bool isIdle() const { return (NextID == 0 && !NotMapped); } /// Return whether any IDs have been assigned in the build of the control /// flow map, indicating that the map is being generated for this boolean /// expression. - bool isBuilding() const { return (NextID > 1); } + bool isBuilding() const { return (NextID > 0); } /// Set the given condition's ID. void setCondID(const Expr *Cond, mcdc::ConditionID ID) { @@ -721,7 +722,7 @@ public: mcdc::ConditionID getCondID(const Expr *Cond) const { auto I = CondIDs.find(CodeGenFunction::stripCond(Cond)); if (I == CondIDs.end()) - return 0; + return -1; else return I->second; } @@ -789,15 +790,15 @@ public: // Reset state if not doing mapping. if (NotMapped) { NotMapped = false; - assert(NextID == 1); + assert(NextID == 0); return 0; } // Set number of conditions and reset. - unsigned TotalConds = NextID - 1; + unsigned TotalConds = NextID; // Reset ID back to beginning. - NextID = 1; + NextID = 0; return TotalConds; } @@ -889,7 +890,7 @@ struct CounterCoverageMappingBuilder return RegionStack.size() - 1; } - size_t pushRegion(unsigned BitmapIdx, unsigned Conditions, + size_t pushRegion(unsigned BitmapIdx, uint16_t Conditions, std::optional StartLoc = std::nullopt, std::optional EndLoc = std::nullopt) { @@ -1038,7 +1039,7 @@ struct CounterCoverageMappingBuilder if (CodeGenFunction::isInstrumentedCondition(C)) { mcdc::Parameters BranchParams; mcdc::ConditionID ID = MCDCBuilder.getCondID(C); - if (ID > 0) + if (ID >= 0) BranchParams = mcdc::BranchParameters{ID, Conds}; // If a condition can fold to true or false, the corresponding branch @@ -2125,8 +2126,9 @@ static void dump(llvm::raw_ostream &OS, StringRef FunctionName, if (const auto *BranchParams = std::get_if(&R.MCDCParams)) { - OS << " [" << BranchParams->ID << "," << BranchParams->Conds[true]; - OS << "," << BranchParams->Conds[false] << "] "; + OS << " [" << BranchParams->ID + 1 << "," + << BranchParams->Conds[true] + 1; + OS << "," << BranchParams->Conds[false] + 1 << "] "; } if (R.Kind == CounterMappingRegion::ExpansionRegion) -- cgit v1.1