diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp index 5d7c384774576..b5ce1aad7ea1e 100644 --- a/clang/lib/CodeGen/CodeGenPGO.cpp +++ b/clang/lib/CodeGen/CodeGenPGO.cpp @@ -165,8 +165,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { llvm::DenseMap &CounterMap; /// The next bitmap byte index to assign. unsigned NextMCDCBitmapIdx; - /// The map of statements to MC/DC bitmap coverage objects. - llvm::DenseMap &MCDCBitmapMap; + MCDC::State &MCDCState; /// Maximum number of supported MC/DC conditions in a boolean expression. unsigned MCDCMaxCond; /// The profile version. @@ -176,11 +175,11 @@ struct MapRegionCounters : public RecursiveASTVisitor { MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion, llvm::DenseMap &CounterMap, - llvm::DenseMap &MCDCBitmapMap, - unsigned MCDCMaxCond, DiagnosticsEngine &Diag) + MCDC::State &MCDCState, unsigned MCDCMaxCond, + DiagnosticsEngine &Diag) : NextCounter(0), Hash(HashVersion), CounterMap(CounterMap), - NextMCDCBitmapIdx(0), MCDCBitmapMap(MCDCBitmapMap), - MCDCMaxCond(MCDCMaxCond), ProfileVersion(ProfileVersion), Diag(Diag) {} + NextMCDCBitmapIdx(0), MCDCState(MCDCState), MCDCMaxCond(MCDCMaxCond), + ProfileVersion(ProfileVersion), Diag(Diag) {} // Blocks and lambdas are handled as separate functions, so we need not // traverse them in the parent context. @@ -309,7 +308,7 @@ struct MapRegionCounters : public RecursiveASTVisitor { // Otherwise, allocate the number of bytes required for the bitmap // based on the number of conditions. Must be at least 1-byte long. - MCDCBitmapMap[BinOp] = NextMCDCBitmapIdx; + MCDCState.BitmapMap[BinOp] = NextMCDCBitmapIdx; unsigned SizeInBits = std::max(1L << NumCond, CHAR_BIT); NextMCDCBitmapIdx += SizeInBits / CHAR_BIT; } @@ -987,10 +986,9 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) { unsigned MCDCMaxConditions = (CGM.getCodeGenOpts().MCDCCoverage) ? 6 : 0; RegionCounterMap.reset(new llvm::DenseMap); - RegionMCDCBitmapMap.reset(new llvm::DenseMap); + RegionMCDCState.reset(new MCDC::State); MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap, - *RegionMCDCBitmapMap, MCDCMaxConditions, - CGM.getDiags()); + *RegionMCDCState, MCDCMaxConditions, CGM.getDiags()); if (const FunctionDecl *FD = dyn_cast_or_null(D)) Walker.TraverseDecl(const_cast(FD)); else if (const ObjCMethodDecl *MD = dyn_cast_or_null(D)) @@ -1001,7 +999,7 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) { Walker.TraverseDecl(const_cast(CD)); assert(Walker.NextCounter > 0 && "no entry counter mapped for decl"); NumRegionCounters = Walker.NextCounter; - MCDCBitmapBytes = Walker.NextMCDCBitmapIdx; + RegionMCDCState->BitmapBytes = Walker.NextMCDCBitmapIdx; FunctionHash = Walker.Hash.finalize(); } @@ -1033,11 +1031,10 @@ void CodeGenPGO::emitCounterRegionMapping(const Decl *D) { std::string CoverageMapping; llvm::raw_string_ostream OS(CoverageMapping); - RegionCondIDMap.reset(new llvm::DenseMap); + RegionMCDCState->CondIDMap.clear(); CoverageMappingGen MappingGen( *CGM.getCoverageMapping(), CGM.getContext().getSourceManager(), - CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCBitmapMap.get(), - RegionCondIDMap.get()); + CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCState.get()); MappingGen.emitCounterMapping(D, OS); OS.flush(); @@ -1119,7 +1116,7 @@ bool CodeGenPGO::canEmitMCDCCoverage(const CGBuilderTy &Builder) { } void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) { - if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap) + if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState) return; auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext()); @@ -1129,7 +1126,7 @@ void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) { // anything. llvm::Value *Args[3] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), Builder.getInt64(FunctionHash), - Builder.getInt32(MCDCBitmapBytes)}; + Builder.getInt32(RegionMCDCState->BitmapBytes)}; Builder.CreateCall( CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_parameters), Args); } @@ -1137,13 +1134,13 @@ void CodeGenPGO::emitMCDCParameters(CGBuilderTy &Builder) { void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr) { - if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap) + if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState) return; S = S->IgnoreParens(); - auto ExprMCDCBitmapMapIterator = RegionMCDCBitmapMap->find(S); - if (ExprMCDCBitmapMapIterator == RegionMCDCBitmapMap->end()) + auto ExprMCDCBitmapMapIterator = RegionMCDCState->BitmapMap.find(S); + if (ExprMCDCBitmapMapIterator == RegionMCDCState->BitmapMap.end()) return; // Extract the ID of the global bitmap associated with this expression. @@ -1157,7 +1154,7 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, // index represents an executed test vector. llvm::Value *Args[5] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), Builder.getInt64(FunctionHash), - Builder.getInt32(MCDCBitmapBytes), + Builder.getInt32(RegionMCDCState->BitmapBytes), Builder.getInt32(MCDCTestVectorBitmapID), MCDCCondBitmapAddr.getPointer()}; Builder.CreateCall( @@ -1166,12 +1163,12 @@ void CodeGenPGO::emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, void CodeGenPGO::emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr) { - if (!canEmitMCDCCoverage(Builder) || !RegionMCDCBitmapMap) + if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState) return; S = S->IgnoreParens(); - if (RegionMCDCBitmapMap->find(S) == RegionMCDCBitmapMap->end()) + if (!RegionMCDCState->BitmapMap.contains(S)) return; // Emit intrinsic that resets a dedicated temporary value on the stack to 0. @@ -1181,7 +1178,7 @@ void CodeGenPGO::emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S, void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr, llvm::Value *Val) { - if (!canEmitMCDCCoverage(Builder) || !RegionCondIDMap) + if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState) return; // Even though, for simplicity, parentheses and unary logical-NOT operators @@ -1193,8 +1190,8 @@ void CodeGenPGO::emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, // also make debugging a bit easier. S = CodeGenFunction::stripCond(S); - auto ExprMCDCConditionIDMapIterator = RegionCondIDMap->find(S); - if (ExprMCDCConditionIDMapIterator == RegionCondIDMap->end()) + auto ExprMCDCConditionIDMapIterator = RegionMCDCState->CondIDMap.find(S); + if (ExprMCDCConditionIDMapIterator == RegionMCDCState->CondIDMap.end()) return; // Extract the ID of the condition we are setting in the bitmap. diff --git a/clang/lib/CodeGen/CodeGenPGO.h b/clang/lib/CodeGen/CodeGenPGO.h index 6596b6c352776..d3c2b277238fc 100644 --- a/clang/lib/CodeGen/CodeGenPGO.h +++ b/clang/lib/CodeGen/CodeGenPGO.h @@ -16,6 +16,7 @@ #include "CGBuilder.h" #include "CodeGenModule.h" #include "CodeGenTypes.h" +#include "MCDCState.h" #include "llvm/ProfileData/InstrProfReader.h" #include #include @@ -33,21 +34,18 @@ class CodeGenPGO { std::array NumValueSites; unsigned NumRegionCounters; - unsigned MCDCBitmapBytes; uint64_t FunctionHash; std::unique_ptr> RegionCounterMap; - std::unique_ptr> RegionMCDCBitmapMap; - std::unique_ptr> RegionCondIDMap; std::unique_ptr> StmtCountMap; std::unique_ptr ProfRecord; + std::unique_ptr RegionMCDCState; std::vector RegionCounts; uint64_t CurrentRegionCount; public: CodeGenPGO(CodeGenModule &CGModule) : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}), - NumRegionCounters(0), MCDCBitmapBytes(0), FunctionHash(0), - CurrentRegionCount(0) {} + NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {} /// Whether or not we have PGO region data for the current function. This is /// false both when we have no data at all and when our data has been diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp index 93fe76eb9903e..3b711c05e9275 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -689,8 +689,8 @@ struct MCDCCoverageBuilder { CodeGenModule &CGM; llvm::SmallVector DecisionStack; + MCDC::State &MCDCState; llvm::DenseMap &CondIDs; - llvm::DenseMap &MCDCBitmapMap; mcdc::ConditionID NextID = 1; bool NotMapped = false; @@ -703,12 +703,9 @@ struct MCDCCoverageBuilder { } public: - MCDCCoverageBuilder( - CodeGenModule &CGM, - llvm::DenseMap &CondIDMap, - llvm::DenseMap &MCDCBitmapMap) - : CGM(CGM), DecisionStack(1, DecisionStackSentinel), CondIDs(CondIDMap), - MCDCBitmapMap(MCDCBitmapMap) {} + MCDCCoverageBuilder(CodeGenModule &CGM, MCDC::State &MCDCState) + : CGM(CGM), DecisionStack(1, DecisionStackSentinel), MCDCState(MCDCState), + CondIDs(MCDCState.CondIDMap) {} /// 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 @@ -745,7 +742,8 @@ struct MCDCCoverageBuilder { return; // If binary expression is disqualified, don't do mapping. - if (!isBuilding() && !MCDCBitmapMap.contains(CodeGenFunction::stripCond(E))) + if (!isBuilding() && + !MCDCState.BitmapMap.contains(CodeGenFunction::stripCond(E))) NotMapped = true; // Don't go any further if we don't need to map condition IDs. @@ -818,8 +816,7 @@ struct CounterCoverageMappingBuilder /// The map of statements to count values. llvm::DenseMap &CounterMap; - /// The map of statements to bitmap coverage object values. - llvm::DenseMap &MCDCBitmapMap; + MCDC::State &MCDCState; /// A stack of currently live regions. llvm::SmallVector RegionStack; @@ -863,7 +860,7 @@ struct CounterCoverageMappingBuilder return Counter::getCounter(CounterMap[S]); } - unsigned getRegionBitmap(const Stmt *S) { return MCDCBitmapMap[S]; } + unsigned getRegionBitmap(const Stmt *S) { return MCDCState.BitmapMap[S]; } /// Push a region onto the stack. /// @@ -1341,12 +1338,9 @@ struct CounterCoverageMappingBuilder CounterCoverageMappingBuilder( CoverageMappingModuleGen &CVM, llvm::DenseMap &CounterMap, - llvm::DenseMap &MCDCBitmapMap, - llvm::DenseMap &CondIDMap, - SourceManager &SM, const LangOptions &LangOpts) + MCDC::State &MCDCState, SourceManager &SM, const LangOptions &LangOpts) : CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap), - MCDCBitmapMap(MCDCBitmapMap), - MCDCBuilder(CVM.getCodeGenModule(), CondIDMap, MCDCBitmapMap) {} + MCDCState(MCDCState), MCDCBuilder(CVM.getCodeGenModule(), MCDCState) {} /// Write the mapping data to the output stream void write(llvm::raw_ostream &OS) { @@ -2350,9 +2344,9 @@ unsigned CoverageMappingModuleGen::getFileID(FileEntryRef File) { void CoverageMappingGen::emitCounterMapping(const Decl *D, llvm::raw_ostream &OS) { - assert(CounterMap && MCDCBitmapMap); - CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCBitmapMap, - *CondIDMap, SM, LangOpts); + assert(CounterMap && MCDCState); + CounterCoverageMappingBuilder Walker(CVM, *CounterMap, *MCDCState, SM, + LangOpts); Walker.VisitDecl(D); Walker.write(OS); } diff --git a/clang/lib/CodeGen/CoverageMappingGen.h b/clang/lib/CodeGen/CoverageMappingGen.h index 62cea173c9fc9..f7c59c48c1839 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.h +++ b/clang/lib/CodeGen/CoverageMappingGen.h @@ -91,6 +91,10 @@ namespace CodeGen { class CodeGenModule; +namespace MCDC { +struct State; +} + /// Organizes the cross-function state that is used while generating /// code coverage mapping data. class CoverageMappingModuleGen { @@ -150,22 +154,20 @@ class CoverageMappingGen { SourceManager &SM; const LangOptions &LangOpts; llvm::DenseMap *CounterMap; - llvm::DenseMap *MCDCBitmapMap; - llvm::DenseMap *CondIDMap; + MCDC::State *MCDCState; public: CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts) : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(nullptr), - MCDCBitmapMap(nullptr), CondIDMap(nullptr) {} + MCDCState(nullptr) {} CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM, const LangOptions &LangOpts, llvm::DenseMap *CounterMap, - llvm::DenseMap *MCDCBitmapMap, - llvm::DenseMap *CondIDMap) + MCDC::State *MCDCState) : CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap), - MCDCBitmapMap(MCDCBitmapMap), CondIDMap(CondIDMap) {} + MCDCState(MCDCState) {} /// Emit the coverage mapping data which maps the regions of /// code to counters that will be used to find the execution diff --git a/clang/lib/CodeGen/MCDCState.h b/clang/lib/CodeGen/MCDCState.h new file mode 100644 index 0000000000000..dfb0d99921e87 --- /dev/null +++ b/clang/lib/CodeGen/MCDCState.h @@ -0,0 +1,32 @@ +//===---- MCDCState.h - Per-Function MC/DC state ----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Per-Function MC/DC state for PGO +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H +#define LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ProfileData/Coverage/MCDCTypes.h" + +namespace clang::CodeGen::MCDC { + +using namespace llvm::coverage::mcdc; + +/// Per-Function MC/DC state +struct State { + unsigned BitmapBytes = 0; + llvm::DenseMap BitmapMap; + llvm::DenseMap CondIDMap; +}; + +} // namespace clang::CodeGen::MCDC + +#endif // LLVM_CLANG_LIB_CODEGEN_MCDCSTATE_H