20#include "llvm/IR/Intrinsics.h"
21#include "llvm/IR/MDBuilder.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/MD5.h"
31static llvm::cl::opt<bool>
33 llvm::cl::desc(
"Enable value profiling"),
34 llvm::cl::Hidden, llvm::cl::init(
false));
39void CodeGenPGO::setFuncName(StringRef Name,
40 llvm::GlobalValue::LinkageTypes
Linkage) {
41 llvm::IndexedInstrProfReader *PGOReader = CGM.
getPGOReader();
42 FuncName = llvm::getPGOFuncName(
44 PGOReader ? PGOReader->getVersion() : llvm::IndexedInstrProf::Version);
48 FuncNameVar = llvm::createPGOFuncNameVar(CGM.
getModule(),
Linkage, FuncName);
51void CodeGenPGO::setFuncName(llvm::Function *Fn) {
52 setFuncName(
Fn->getName(),
Fn->getLinkage());
54 llvm::createPGOFuncNameMetadata(*Fn, FuncName);
85 static const int NumBitsPerType = 6;
86 static const unsigned NumTypesPerWord =
sizeof(
uint64_t) * 8 / NumBitsPerType;
87 static const unsigned TooBig = 1u << NumBitsPerType;
97 enum HashType :
unsigned char {
104 ObjCForCollectionStmt,
114 BinaryConditionalOperator,
138 static_assert(LastHashType <= TooBig,
"Too many types in HashType");
141 : Working(0), Count(0), HashVersion(HashVersion) {}
142 void combine(HashType
Type);
146const int PGOHash::NumBitsPerType;
147const unsigned PGOHash::NumTypesPerWord;
148const unsigned PGOHash::TooBig;
151static PGOHashVersion getPGOHashVersion(llvm::IndexedInstrProfReader *PGOReader,
153 if (PGOReader->getVersion() <= 4)
155 if (PGOReader->getVersion() <= 5)
157 if (PGOReader->getVersion() <= 12)
163struct MapRegionCounters :
public RecursiveASTVisitor<MapRegionCounters> {
164 using Base = RecursiveASTVisitor<MapRegionCounters>;
167 unsigned NextCounter;
171 llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
173 MCDC::State &MCDCState;
175 unsigned MCDCMaxCond;
179 DiagnosticsEngine &
Diag;
181 MapRegionCounters(
PGOHashVersion HashVersion, uint64_t ProfileVersion,
182 llvm::DenseMap<const Stmt *, CounterPair> &CounterMap,
183 MCDC::State &MCDCState,
unsigned MCDCMaxCond,
184 DiagnosticsEngine &
Diag)
185 : NextCounter(0), Hash(HashVersion), CounterMap(CounterMap),
186 MCDCState(MCDCState), MCDCMaxCond(MCDCMaxCond),
187 ProfileVersion(ProfileVersion),
Diag(
Diag) {}
191 bool TraverseBlockExpr(BlockExpr *BE) {
return true; }
194 for (
auto C : zip(
LE->captures(),
LE->capture_inits()))
195 TraverseLambdaCapture(LE, &std::get<0>(
C), std::get<1>(
C));
198 bool TraverseCapturedStmt(CapturedStmt *CS) {
return true; }
200 bool VisitDecl(
const Decl *D) {
205 case Decl::CXXMethod:
206 case Decl::CXXConstructor:
207 case Decl::CXXDestructor:
208 case Decl::CXXConversion:
209 case Decl::ObjCMethod:
212 CounterMap[D->
getBody()] = NextCounter++;
220 PGOHash::HashType updateCounterMappings(Stmt *S) {
222 if (
Type != PGOHash::None)
223 CounterMap[S] = NextCounter++;
236 unsigned NumCond = 0;
237 bool SplitNestedLogicalOp =
false;
238 SmallVector<const Stmt *, 16> NonLogOpStack;
239 SmallVector<const BinaryOperator *, 16> LogOpStack;
242 bool dataTraverseStmtPre(Stmt *S) {
244 if (MCDCMaxCond == 0)
249 if (LogOpStack.empty()) {
251 SplitNestedLogicalOp =
false;
254 if (
const Expr *E = dyn_cast<Expr>(S)) {
255 if (
const auto *BinOp =
257 BinOp && BinOp->isLogicalOp()) {
265 SplitNestedLogicalOp = SplitNestedLogicalOp || !NonLogOpStack.empty();
267 LogOpStack.push_back(BinOp);
274 if (!LogOpStack.empty())
275 NonLogOpStack.push_back(S);
283 bool dataTraverseStmtPost(Stmt *S) {
285 if (MCDCMaxCond == 0)
288 if (
const Expr *E = dyn_cast<Expr>(S)) {
289 const BinaryOperator *BinOp =
292 assert(LogOpStack.back() == BinOp);
293 LogOpStack.pop_back();
296 if (LogOpStack.empty()) {
298 if (SplitNestedLogicalOp) {
304 if (NumCond > MCDCMaxCond) {
306 << NumCond << MCDCMaxCond;
317 if (!LogOpStack.empty())
318 NonLogOpStack.pop_back();
328 bool VisitBinaryOperator(BinaryOperator *S) {
334 if (ProfileVersion >= llvm::IndexedInstrProf::Version7)
335 CounterMap[S->
getRHS()] = NextCounter++;
340 return Base::VisitBinaryOperator(S);
344 bool VisitStmt(Stmt *S) {
345 auto Type = updateCounterMappings(S);
347 Type = getHashType(Hash.getHashVersion(), S);
348 if (
Type != PGOHash::None)
353 bool TraverseIfStmt(IfStmt *
If) {
356 return Base::TraverseIfStmt(
If);
361 for (Stmt *CS :
If->children()) {
362 if (!CS || NoSingleByteCoverage)
364 if (CS ==
If->getThen())
365 CounterMap[
If->getThen()] = NextCounter++;
366 else if (CS ==
If->getElse())
367 CounterMap[
If->getElse()] = NextCounter++;
373 for (Stmt *CS :
If->children()) {
376 if (CS ==
If->getThen())
377 Hash.combine(PGOHash::IfThenBranch);
378 else if (CS ==
If->getElse())
379 Hash.combine(PGOHash::IfElseBranch);
382 Hash.combine(PGOHash::EndOfScope);
386 bool TraverseWhileStmt(WhileStmt *While) {
390 for (Stmt *CS : While->
children()) {
391 if (!CS || NoSingleByteCoverage)
394 CounterMap[While->
getCond()] = NextCounter++;
395 else if (CS == While->
getBody())
396 CounterMap[While->
getBody()] = NextCounter++;
399 Base::TraverseWhileStmt(While);
401 Hash.combine(PGOHash::EndOfScope);
405 bool TraverseDoStmt(DoStmt *Do) {
410 if (!CS || NoSingleByteCoverage)
413 CounterMap[Do->
getCond()] = NextCounter++;
415 CounterMap[Do->
getBody()] = NextCounter++;
418 Base::TraverseDoStmt(Do);
420 Hash.combine(PGOHash::EndOfScope);
424 bool TraverseForStmt(ForStmt *For) {
429 if (!CS || NoSingleByteCoverage)
432 CounterMap[For->
getCond()] = NextCounter++;
433 else if (CS == For->
getInc())
434 CounterMap[For->
getInc()] = NextCounter++;
436 CounterMap[For->
getBody()] = NextCounter++;
439 Base::TraverseForStmt(For);
441 Hash.combine(PGOHash::EndOfScope);
445 bool TraverseCXXForRangeStmt(CXXForRangeStmt *ForRange) {
448 for (Stmt *CS : ForRange->
children()) {
449 if (!CS || NoSingleByteCoverage)
452 CounterMap[ForRange->
getBody()] = NextCounter++;
455 Base::TraverseCXXForRangeStmt(ForRange);
457 Hash.combine(PGOHash::EndOfScope);
464#define DEFINE_NESTABLE_TRAVERSAL(N) \
465 bool Traverse##N(N *S) { \
466 Base::Traverse##N(S); \
467 if (Hash.getHashVersion() != PGO_HASH_V1) \
468 Hash.combine(PGOHash::EndOfScope); \
481 case Stmt::LabelStmtClass:
482 return PGOHash::LabelStmt;
483 case Stmt::WhileStmtClass:
484 return PGOHash::WhileStmt;
485 case Stmt::DoStmtClass:
486 return PGOHash::DoStmt;
487 case Stmt::ForStmtClass:
488 return PGOHash::ForStmt;
489 case Stmt::CXXForRangeStmtClass:
490 return PGOHash::CXXForRangeStmt;
491 case Stmt::ObjCForCollectionStmtClass:
492 return PGOHash::ObjCForCollectionStmt;
493 case Stmt::SwitchStmtClass:
494 return PGOHash::SwitchStmt;
495 case Stmt::CaseStmtClass:
496 return PGOHash::CaseStmt;
497 case Stmt::DefaultStmtClass:
498 return PGOHash::DefaultStmt;
499 case Stmt::IfStmtClass:
500 return PGOHash::IfStmt;
501 case Stmt::CXXTryStmtClass:
502 return PGOHash::CXXTryStmt;
503 case Stmt::CXXCatchStmtClass:
504 return PGOHash::CXXCatchStmt;
505 case Stmt::ConditionalOperatorClass:
506 return PGOHash::ConditionalOperator;
507 case Stmt::BinaryConditionalOperatorClass:
508 return PGOHash::BinaryConditionalOperator;
509 case Stmt::BinaryOperatorClass: {
512 return PGOHash::BinaryOperatorLAnd;
514 return PGOHash::BinaryOperatorLOr;
520 return PGOHash::BinaryOperatorLT;
522 return PGOHash::BinaryOperatorGT;
524 return PGOHash::BinaryOperatorLE;
526 return PGOHash::BinaryOperatorGE;
528 return PGOHash::BinaryOperatorEQ;
530 return PGOHash::BinaryOperatorNE;
541 case Stmt::GotoStmtClass:
542 return PGOHash::GotoStmt;
543 case Stmt::IndirectGotoStmtClass:
544 return PGOHash::IndirectGotoStmt;
545 case Stmt::BreakStmtClass:
546 return PGOHash::BreakStmt;
547 case Stmt::ContinueStmtClass:
548 return PGOHash::ContinueStmt;
549 case Stmt::ReturnStmtClass:
550 return PGOHash::ReturnStmt;
551 case Stmt::CXXThrowExprClass:
552 return PGOHash::ThrowExpr;
553 case Stmt::UnaryOperatorClass: {
556 return PGOHash::UnaryOperatorLNot;
562 return PGOHash::None;
568struct ComputeRegionCounts :
public ConstStmtVisitor<ComputeRegionCounts> {
574 bool RecordNextStmtCount;
580 llvm::DenseMap<const Stmt *, uint64_t> &
CountMap;
583 struct BreakContinue {
586 BreakContinue() =
default;
588 SmallVector<BreakContinue, 8> BreakContinueStack;
590 ComputeRegionCounts(llvm::DenseMap<const Stmt *, uint64_t> &
CountMap,
594 void RecordStmtCount(
const Stmt *S) {
595 if (RecordNextStmtCount) {
597 RecordNextStmtCount =
false;
603 CurrentCount = Count;
607 void VisitStmt(
const Stmt *S) {
609 for (
const Stmt *Child : S->
children())
614 void VisitFunctionDecl(
const FunctionDecl *D) {
626 void VisitCapturedDecl(
const CapturedDecl *D) {
633 void VisitObjCMethodDecl(
const ObjCMethodDecl *D) {
640 void VisitBlockDecl(
const BlockDecl *D) {
647 void VisitReturnStmt(
const ReturnStmt *S) {
652 RecordNextStmtCount =
true;
655 void VisitCXXThrowExpr(
const CXXThrowExpr *E) {
660 RecordNextStmtCount =
true;
663 void VisitGotoStmt(
const GotoStmt *S) {
666 RecordNextStmtCount =
true;
669 void VisitLabelStmt(
const LabelStmt *S) {
670 RecordNextStmtCount =
false;
677 void VisitBreakStmt(
const BreakStmt *S) {
679 assert(!BreakContinueStack.empty() &&
"break not in a loop or switch!");
680 BreakContinueStack.back().BreakCount += CurrentCount;
682 RecordNextStmtCount =
true;
685 void VisitContinueStmt(
const ContinueStmt *S) {
687 assert(!BreakContinueStack.empty() &&
"continue stmt not in a loop!");
688 BreakContinueStack.back().ContinueCount += CurrentCount;
690 RecordNextStmtCount =
true;
693 void VisitWhileStmt(
const WhileStmt *S) {
695 uint64_t ParentCount = CurrentCount;
697 BreakContinueStack.push_back(BreakContinue());
703 uint64_t BackedgeCount = CurrentCount;
709 BreakContinue BC = BreakContinueStack.pop_back_val();
711 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
714 setCount(BC.BreakCount + CondCount - BodyCount);
715 RecordNextStmtCount =
true;
718 void VisitDoStmt(
const DoStmt *S) {
722 BreakContinueStack.push_back(BreakContinue());
724 uint64_t BodyCount = setCount(LoopCount + CurrentCount);
727 uint64_t BackedgeCount = CurrentCount;
729 BreakContinue BC = BreakContinueStack.pop_back_val();
732 uint64_t CondCount = setCount(BackedgeCount + BC.ContinueCount);
735 setCount(BC.BreakCount + CondCount - LoopCount);
736 RecordNextStmtCount =
true;
739 void VisitForStmt(
const ForStmt *S) {
744 uint64_t ParentCount = CurrentCount;
746 BreakContinueStack.push_back(BreakContinue());
752 uint64_t BackedgeCount = CurrentCount;
753 BreakContinue BC = BreakContinueStack.pop_back_val();
758 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
765 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
770 setCount(BC.BreakCount + CondCount - BodyCount);
771 RecordNextStmtCount =
true;
774 void VisitCXXForRangeStmt(
const CXXForRangeStmt *S) {
783 uint64_t ParentCount = CurrentCount;
784 BreakContinueStack.push_back(BreakContinue());
790 uint64_t BackedgeCount = CurrentCount;
791 BreakContinue BC = BreakContinueStack.pop_back_val();
795 uint64_t IncCount = setCount(BackedgeCount + BC.ContinueCount);
801 setCount(ParentCount + BackedgeCount + BC.ContinueCount);
804 setCount(BC.BreakCount + CondCount - BodyCount);
805 RecordNextStmtCount =
true;
808 void VisitObjCForCollectionStmt(
const ObjCForCollectionStmt *S) {
811 uint64_t ParentCount = CurrentCount;
812 BreakContinueStack.push_back(BreakContinue());
817 uint64_t BackedgeCount = CurrentCount;
818 BreakContinue BC = BreakContinueStack.pop_back_val();
820 setCount(BC.BreakCount + ParentCount + BackedgeCount + BC.ContinueCount -
822 RecordNextStmtCount =
true;
825 void VisitSwitchStmt(
const SwitchStmt *S) {
831 BreakContinueStack.push_back(BreakContinue());
834 BreakContinue BC = BreakContinueStack.pop_back_val();
835 if (!BreakContinueStack.empty())
836 BreakContinueStack.back().ContinueCount += BC.ContinueCount;
839 RecordNextStmtCount =
true;
842 void VisitSwitchCase(
const SwitchCase *S) {
843 RecordNextStmtCount =
false;
848 setCount(CurrentCount + CaseCount);
852 RecordNextStmtCount =
true;
856 void VisitIfStmt(
const IfStmt *S) {
866 uint64_t ParentCount = CurrentCount;
878 uint64_t ElseCount = ParentCount - ThenCount;
883 OutCount += CurrentCount;
885 OutCount += ElseCount;
887 RecordNextStmtCount =
true;
890 void VisitCXXTryStmt(
const CXXTryStmt *S) {
897 RecordNextStmtCount =
true;
900 void VisitCXXCatchStmt(
const CXXCatchStmt *S) {
901 RecordNextStmtCount =
false;
908 void VisitAbstractConditionalOperator(
const AbstractConditionalOperator *E) {
910 uint64_t ParentCount = CurrentCount;
920 uint64_t FalseCount = setCount(ParentCount - TrueCount);
923 OutCount += CurrentCount;
926 RecordNextStmtCount =
true;
929 void VisitBinLAnd(
const BinaryOperator *E) {
931 uint64_t ParentCount = CurrentCount;
937 setCount(ParentCount + RHSCount - CurrentCount);
938 RecordNextStmtCount =
true;
941 void VisitBinLOr(
const BinaryOperator *E) {
943 uint64_t ParentCount = CurrentCount;
949 setCount(ParentCount + RHSCount - CurrentCount);
950 RecordNextStmtCount =
true;
955void PGOHash::combine(HashType
Type) {
957 assert(
Type &&
"Hash is invalid: unexpected type 0");
958 assert(
unsigned(
Type) < TooBig &&
"Hash is invalid: too many types");
961 if (Count && Count % NumTypesPerWord == 0) {
962 using namespace llvm::support;
964 endian::byte_swap<uint64_t>(Working, llvm::endianness::little);
965 MD5.update(llvm::ArrayRef((uint8_t *)&Swapped,
sizeof(Swapped)));
971 Working = Working << NumBitsPerType |
Type;
976 if (Count <= NumTypesPerWord)
987 MD5.update({(uint8_t)Working});
989 using namespace llvm::support;
991 endian::byte_swap<uint64_t>(Working, llvm::endianness::little);
992 MD5.update(llvm::ArrayRef((uint8_t *)&Swapped,
sizeof(Swapped)));
997 llvm::MD5::MD5Result
Result;
1008 if (CGM.getLangOpts().CUDA && !CGM.getLangOpts().CUDAIsDevice &&
1012 bool InstrumentRegions = CGM.getCodeGenOpts().hasProfileClangInstr();
1013 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
1014 if (!InstrumentRegions && !PGOReader)
1021 if (CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1022 if (
const auto *CCD = dyn_cast<CXXConstructorDecl>(D))
1030 CGM.ClearUnusedCoverageMapping(D);
1031 if (Fn->hasFnAttribute(llvm::Attribute::NoProfile))
1033 if (Fn->hasFnAttribute(llvm::Attribute::SkipProfile))
1043 mapRegionCounters(D);
1044 if (CGM.getCodeGenOpts().CoverageMapping)
1045 emitCounterRegionMapping(D);
1047 loadRegionCounts(PGOReader,
SM.isInMainFile(D->
getLocation()));
1048 computeRegionCounts(D);
1049 applyFunctionAttributes(PGOReader, Fn);
1053void CodeGenPGO::mapRegionCounters(
const Decl *D) {
1057 uint64_t ProfileVersion = llvm::IndexedInstrProf::Version;
1059 HashVersion = getPGOHashVersion(PGOReader, CGM);
1060 ProfileVersion = PGOReader->getVersion();
1072 unsigned MCDCMaxConditions =
1076 RegionCounterMap.reset(
new llvm::DenseMap<const Stmt *, CounterPair>);
1078 MapRegionCounters Walker(HashVersion, ProfileVersion, *RegionCounterMap,
1079 *RegionMCDCState, MCDCMaxConditions, CGM.
getDiags());
1080 if (
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1082 else if (
const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1084 else if (
const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
1085 Walker.TraverseDecl(
const_cast<BlockDecl *
>(BD));
1086 else if (
const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
1088 assert(Walker.NextCounter > 0 &&
"no entry counter mapped for decl");
1089 NumRegionCounters = Walker.NextCounter;
1090 FunctionHash = Walker.Hash.finalize();
1092 FunctionHash &= llvm::NamedInstrProfRecord::FUNC_HASH_MASK;
1095bool CodeGenPGO::skipRegionMappingForDecl(
const Decl *D) {
1105 !D->
hasAttr<CUDAGlobalAttr>()) ||
1107 (D->
hasAttr<CUDAGlobalAttr>() ||
1108 (!D->
hasAttr<CUDAHostAttr>() && D->
hasAttr<CUDADeviceAttr>())))))
1117void CodeGenPGO::emitCounterRegionMapping(
const Decl *D) {
1118 if (skipRegionMappingForDecl(D))
1121 std::string CoverageMapping;
1122 llvm::raw_string_ostream
OS(CoverageMapping);
1123 RegionMCDCState->BranchByStmt.clear();
1124 CoverageMappingGen MappingGen(
1125 *CGM.getCoverageMapping(), CGM.getContext().getSourceManager(),
1126 CGM.getLangOpts(), RegionCounterMap.get(), RegionMCDCState.get());
1127 MappingGen.emitCounterMapping(D, OS);
1129 if (CoverageMapping.empty())
1133 unsigned MaxNumCounters = NumRegionCounters;
1134 for (
const auto &[_,
V] : *RegionCounterMap) {
1135 assert((!
V.Executed.hasValue() || MaxNumCounters >
V.Executed) &&
1136 "TrueCnt should not be reassigned");
1137 if (
V.Skipped.hasValue())
1138 MaxNumCounters = std::max(MaxNumCounters,
V.Skipped + 1);
1140 NumRegionCounters = MaxNumCounters;
1142 CGM.getCoverageMapping()->addFunctionMappingRecord(
1143 FuncNameVar, FuncName, FunctionHash, CoverageMapping);
1148 llvm::GlobalValue::LinkageTypes
Linkage) {
1149 if (skipRegionMappingForDecl(D))
1152 std::string CoverageMapping;
1153 llvm::raw_string_ostream OS(CoverageMapping);
1155 CGM.getContext().getSourceManager(),
1159 if (CoverageMapping.empty())
1163 CGM.getCoverageMapping()->addFunctionMappingRecord(
1164 FuncNameVar, FuncName, FunctionHash, CoverageMapping,
false);
1167void CodeGenPGO::computeRegionCounts(
const Decl *D) {
1168 StmtCountMap.reset(
new llvm::DenseMap<const Stmt *, uint64_t>);
1169 ComputeRegionCounts Walker(*StmtCountMap, *
this);
1170 if (
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1171 Walker.VisitFunctionDecl(FD);
1172 else if (
const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1173 Walker.VisitObjCMethodDecl(MD);
1174 else if (
const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
1175 Walker.VisitBlockDecl(BD);
1176 else if (
const CapturedDecl *CD = dyn_cast_or_null<CapturedDecl>(D))
1177 Walker.VisitCapturedDecl(
const_cast<CapturedDecl *
>(CD));
1181CodeGenPGO::applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
1182 llvm::Function *Fn) {
1187 Fn->setEntryCount(FunctionCount);
1191 if (!RegionCounterMap)
1192 return {
false,
false};
1194 auto I = RegionCounterMap->find(S);
1195 if (I == RegionCounterMap->end())
1196 return {
false,
false};
1198 return {I->second.Executed.hasValue(), I->second.Skipped.hasValue()};
1202 bool UseSkipPath,
bool UseBoth,
1203 llvm::Value *StepV) {
1204 if (!RegionCounterMap)
1208 const auto &TheCounterPair = (*RegionCounterMap)[S];
1210 if (!Builder.GetInsertBlock())
1214 (UseSkipPath ? TheCounterPair.Skipped : TheCounterPair.Executed);
1220 auto *NormalizedFuncNameVarPtr =
1221 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1222 FuncNameVar, llvm::PointerType::get(CGM.getLLVMContext(), 0));
1224 llvm::Value *Args[] = {
1225 NormalizedFuncNameVarPtr, Builder.getInt64(FunctionHash),
1226 Builder.getInt32(NumRegionCounters), Builder.getInt32(Counter), StepV};
1229 assert(!StepV &&
"StepV is not supported in single byte counter mode");
1230 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_cover),
1233 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment),
1237 CGM.getIntrinsic(llvm::Intrinsic::instrprof_increment_step), Args);
1240bool CodeGenPGO::canEmitMCDCCoverage(
const CGBuilderTy &Builder) {
1246 if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
1249 auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1254 llvm::Value *Args[3] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1255 Builder.getInt64(FunctionHash),
1256 Builder.getInt32(RegionMCDCState->BitmapBits)};
1258 CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_parameters), Args);
1262std::vector<Address *>
1264 std::vector<Address *>
Result;
1266 if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
1270 for (
auto &[_,
V] : RegionMCDCState->DecisionByStmt)
1272 SortedPair.emplace_back(
V.ID, &
V.MCDCCondBitmapAddr);
1274 llvm::sort(SortedPair);
1276 for (
auto &[_, MCDCCondBitmapAddr] : SortedPair)
1277 Result.push_back(MCDCCondBitmapAddr);
1285 if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
1290 auto DecisionStateIter = RegionMCDCState->DecisionByStmt.find(S);
1291 if (DecisionStateIter == RegionMCDCState->DecisionByStmt.end())
1294 auto &MCDCCondBitmapAddr = DecisionStateIter->second.MCDCCondBitmapAddr;
1295 if (!MCDCCondBitmapAddr.isValid())
1300 if (DecisionStateIter->second.Indices.size() == 0)
1304 unsigned MCDCTestVectorBitmapOffset = DecisionStateIter->second.BitmapIdx;
1305 auto *I8PtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
1312 llvm::Value *Args[4] = {llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1313 Builder.getInt64(FunctionHash),
1314 Builder.getInt32(MCDCTestVectorBitmapOffset),
1315 MCDCCondBitmapAddr.emitRawPointer(CGF)};
1317 CGM.getIntrinsic(llvm::Intrinsic::instrprof_mcdc_tvbitmap_update), Args);
1321 if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
1324 auto I = RegionMCDCState->DecisionByStmt.find(S->
IgnoreParens());
1325 if (I == RegionMCDCState->DecisionByStmt.end())
1328 auto &MCDCCondBitmapAddr = I->second.MCDCCondBitmapAddr;
1329 if (!MCDCCondBitmapAddr.isValid())
1333 Builder.CreateStore(Builder.getInt32(0), MCDCCondBitmapAddr);
1339 if (!canEmitMCDCCoverage(Builder) || !RegionMCDCState)
1351 auto BranchStateIter = RegionMCDCState->BranchByStmt.find(S);
1352 if (BranchStateIter == RegionMCDCState->BranchByStmt.end())
1356 const auto &Branch = BranchStateIter->second;
1357 assert(Branch.ID >= 0 &&
"Condition has no ID!");
1358 assert(Branch.DecisionStmt);
1361 const auto DecisionIter =
1362 RegionMCDCState->DecisionByStmt.find(Branch.DecisionStmt);
1363 if (DecisionIter == RegionMCDCState->DecisionByStmt.end())
1366 auto &MCDCCondBitmapAddr = DecisionIter->second.MCDCCondBitmapAddr;
1367 if (!MCDCCondBitmapAddr.isValid())
1370 const auto &TVIdxs = DecisionIter->second.Indices[Branch.ID];
1372 auto *CurTV = Builder.CreateLoad(MCDCCondBitmapAddr,
1373 "mcdc." + Twine(Branch.ID + 1) +
".cur");
1374 auto *NewTV = Builder.CreateAdd(CurTV, Builder.getInt32(TVIdxs[
true]));
1375 NewTV = Builder.CreateSelect(
1376 Val, NewTV, Builder.CreateAdd(CurTV, Builder.getInt32(TVIdxs[
false])));
1377 Builder.CreateStore(NewTV, MCDCCondBitmapAddr);
1381 if (CGM.getCodeGenOpts().hasProfileClangInstr())
1382 M.addModuleFlag(llvm::Module::Warning,
"EnableValueProfiling",
1387 if (CGM.getCodeGenOpts().hasProfileClangInstr() &&
1389 const StringRef VarName(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1390 llvm::Type *IntTy64 = llvm::Type::getInt64Ty(M.getContext());
1391 uint64_t ProfileVersion =
1392 (INSTR_PROF_RAW_VERSION | VARIANT_MASK_BYTE_COVERAGE);
1394 auto IRLevelVersionVariable =
new llvm::GlobalVariable(
1395 M, IntTy64,
true, llvm::GlobalValue::WeakAnyLinkage,
1396 llvm::Constant::getIntegerValue(IntTy64,
1397 llvm::APInt(64, ProfileVersion)),
1400 IRLevelVersionVariable->setVisibility(llvm::GlobalValue::HiddenVisibility);
1401 llvm::Triple TT(M.getTargetTriple());
1403 IRLevelVersionVariable->setVisibility(
1404 llvm::GlobalValue::ProtectedVisibility);
1405 if (TT.supportsCOMDAT()) {
1406 IRLevelVersionVariable->setLinkage(llvm::GlobalValue::ExternalLinkage);
1407 IRLevelVersionVariable->setComdat(M.getOrInsertComdat(VarName));
1409 IRLevelVersionVariable->setDSOLocal(
true);
1416 llvm::Instruction *ValueSite, llvm::Value *ValuePtr) {
1421 if (!ValuePtr || !ValueSite || !Builder.GetInsertBlock())
1427 bool InstrumentValueSites = CGM.getCodeGenOpts().hasProfileClangInstr();
1428 if (InstrumentValueSites && RegionCounterMap) {
1429 auto BuilderInsertPoint = Builder.saveIP();
1430 Builder.SetInsertPoint(ValueSite);
1431 llvm::Value *Args[5] = {
1433 Builder.getInt64(FunctionHash),
1434 Builder.CreatePtrToInt(ValuePtr, Builder.getInt64Ty()),
1435 Builder.getInt32(ValueKind),
1436 Builder.getInt32(NumValueSites[ValueKind]++)
1439 CGM.getIntrinsic(llvm::Intrinsic::instrprof_value_profile), Args);
1440 Builder.restoreIP(BuilderInsertPoint);
1444 llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
1452 if (NumValueSites[ValueKind] >= ProfRecord->getNumValueSites(ValueKind))
1455 llvm::annotateValueSite(CGM.getModule(), *ValueSite, *ProfRecord,
1456 (llvm::InstrProfValueKind)ValueKind,
1457 NumValueSites[ValueKind]);
1459 NumValueSites[ValueKind]++;
1463void CodeGenPGO::loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
1464 bool IsInMainFile) {
1466 RegionCounts.clear();
1467 auto RecordExpected = PGOReader->getInstrProfRecord(FuncName, FunctionHash);
1468 if (
auto E = RecordExpected.takeError()) {
1469 auto IPE = std::get<0>(llvm::InstrProfError::take(std::move(E)));
1470 if (IPE == llvm::instrprof_error::unknown_function)
1472 else if (IPE == llvm::instrprof_error::hash_mismatch)
1474 else if (IPE == llvm::instrprof_error::malformed)
1480 std::make_unique<llvm::InstrProfRecord>(std::move(RecordExpected.get()));
1481 RegionCounts = ProfRecord->Counts;
1489 return MaxWeight < UINT32_MAX ? 1 : MaxWeight / UINT32_MAX + 1;
1502 assert(Scale &&
"scale by 0?");
1503 uint64_t Scaled = Weight / Scale + 1;
1504 assert(Scaled <= UINT32_MAX &&
"overflow 32-bits");
1508llvm::MDNode *CodeGenFunction::createProfileWeights(uint64_t TrueCount,
1509 uint64_t FalseCount)
const {
1511 if (!TrueCount && !FalseCount)
1517 llvm::MDBuilder MDHelper(
CGM.getLLVMContext());
1523CodeGenFunction::createProfileWeights(ArrayRef<uint64_t> Weights)
const {
1525 if (Weights.size() < 2)
1529 uint64_t MaxWeight = *llvm::max_element(Weights);
1536 SmallVector<uint32_t, 16> ScaledWeights;
1537 ScaledWeights.reserve(Weights.size());
1538 for (uint64_t W : Weights)
1541 llvm::MDBuilder MDHelper(
CGM.getLLVMContext());
1542 return MDHelper.createBranchWeights(ScaledWeights);
1546CodeGenFunction::createProfileWeightsForLoop(
const Stmt *
Cond,
1547 uint64_t LoopCount)
const {
1548 if (!PGO->haveRegionCounts())
1550 std::optional<uint64_t> CondCount = PGO->getStmtCount(
Cond);
1551 if (!CondCount || *CondCount == 0)
1553 return createProfileWeights(LoopCount,
1554 std::max(*CondCount, LoopCount) - LoopCount);
1558 const Stmt *S,
bool UseBoth,
1559 llvm::Value *StepV) {
1560 if (
CGM.getCodeGenOpts().hasProfileClangInstr() &&
1561 !
CurFn->hasFnAttribute(llvm::Attribute::NoProfile) &&
1562 !
CurFn->hasFnAttribute(llvm::Attribute::SkipProfile)) {
1567 PGO->setCurrentStmt(S);
1571 return PGO->getIsCounterPair(S);
1574 PGO->markStmtAsUsed(Skipped, S);
1577 PGO->markStmtMaybeUsed(S);
1582 PGO->emitMCDCParameters(
Builder);
1586 for (
auto *MCDCCondBitmapAddr : PGO->getMCDCCondBitmapAddrArray(
Builder))
1587 *MCDCCondBitmapAddr =
1592 return PGO->isMCDCDecisionExpr(E);
1595 return PGO->isMCDCBranchExpr(E);
1599 PGO->emitMCDCCondBitmapReset(
Builder, E);
1600 PGO->setCurrentStmt(E);
1605 PGO->emitMCDCTestVectorBitmapUpdate(
Builder, E, *
this);
1606 PGO->setCurrentStmt(E);
1613 PGO->emitMCDCCondBitmapUpdate(
Builder, E, Val, *
this);
1614 PGO->setCurrentStmt(E);
1619 return PGO->getStmtCount(S).value_or(0);
1624 PGO->setCurrentRegionCount(Count);
1630 return PGO->getCurrentRegionCount();
llvm::ImmutableMap< CountKey, unsigned > CountMap
#define DEFINE_NESTABLE_TRAVERSAL(N)
static llvm::cl::opt< bool > EnableValueProfiling("enable-value-profiling", llvm::cl::desc("Enable value profiling"), llvm::cl::Hidden, llvm::cl::init(false))
PGOHashVersion
The version of the PGO hash algorithm.
static uint64_t calculateWeightScale(uint64_t MaxWeight)
Calculate what to divide by to scale weights.
static uint32_t scaleBranchWeight(uint64_t Weight, uint64_t Scale)
Scale an individual branch weight (and add 1).
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
SourceManager & getSourceManager()
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
A builtin binary operation expression such as "x + y" or "x <= y".
static bool isLogicalOp(Opcode Opc)
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
CXXCatchStmt - This represents a C++ catch block.
Stmt * getHandlerBlock() const
DeclStmt * getBeginStmt()
DeclStmt * getLoopVarStmt()
DeclStmt * getRangeStmt()
const Expr * getSubExpr() const
CXXTryStmt - A C++ try block, including all handlers.
CXXCatchStmt * getHandler(unsigned i)
unsigned getNumHandlers() const
CompoundStmt * getTryBlock()
Represents the body of a CapturedStmt, and serves as its DeclContext.
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
bool hasProfileClangInstr() const
Check if Clang profile instrumenation is on.
std::string MainFileName
The user provided name for the "main file", if non-empty.
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void setCurrentProfileCount(uint64_t Count)
Set the profiler's current count.
bool isBinaryLogicalOp(const Expr *E) const
CounterForIncrement
Used to specify which counter in a pair shall be incremented.
@ UseSkipPath
Skip (false)
RawAddress CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
void maybeUpdateMCDCTestVectorBitmap(const Expr *E)
Increment the profiler's counter for the given expression by StepV.
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor)
Checks whether the given constructor is a valid subject for the complete-to-base constructor delegati...
void maybeCreateMCDCCondBitmap()
Allocate a temp value on the stack that MCDC can use to track condition results.
static bool isInstrumentedCondition(const Expr *C)
isInstrumentedCondition - Determine whether the given condition is an instrumentable condition (i....
void maybeResetMCDCCondBitmap(const Expr *E)
Zero-init the MCDC temp value.
bool isMCDCCoverageEnabled() const
void maybeUpdateMCDCCondBitmap(const Expr *E, llvm::Value *Val)
Update the MCDC temp value with the condition's evaluated result.
ASTContext & getContext() const
bool isMCDCBranchExpr(const Expr *E) const
static const Expr * stripCond(const Expr *C)
Ignore parentheses and logical-NOT to track conditions consistently.
uint64_t getCurrentProfileCount()
Get the profiler's current count.
void markStmtMaybeUsed(const Stmt *S)
std::pair< bool, bool > getIsCounterPair(const Stmt *S) const
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
void markStmtAsUsed(bool Skipped, const Stmt *S)
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
bool isMCDCDecisionExpr(const Expr *E) const
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
DiagnosticsEngine & getDiags() const
const LangOptions & getLangOpts() const
llvm::IndexedInstrProfReader * getPGOReader() const
InstrProfStats & getPGOStats()
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
std::pair< bool, bool > getIsCounterPair(const Stmt *S) const
uint64_t getRegionCount(const Stmt *S)
Return the region count for the counter at the given index.
void setValueProfilingFlag(llvm::Module &M)
void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, llvm::Instruction *ValueSite, llvm::Value *ValuePtr)
void emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, llvm::Value *Val, CodeGenFunction &CGF)
void emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S)
std::vector< Address * > getMCDCCondBitmapAddrArray(CGBuilderTy &Builder)
Fill mcdc.addr order by ID.
void setProfileVersion(llvm::Module &M)
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, llvm::GlobalValue::LinkageTypes Linkage)
Emit a coverage mapping range with a counter zero for an unused declaration.
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S, bool UseFalsePath, bool UseBoth, llvm::Value *StepV)
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S, CodeGenFunction &CGF)
void emitMCDCParameters(CGBuilderTy &Builder)
bool haveRegionCounts() const
Whether or not we have PGO region data for the current function.
Organizes the per-function state that is used while generating code coverage mapping data.
void emitEmptyMapping(const Decl *D, llvm::raw_ostream &OS)
Emit the coverage mapping data for an unused function.
void addMissing(bool MainFile)
Record that a function we've visited has no profile data.
void addMismatched(bool MainFile)
Record that a function we've visited has mismatched profile data.
void addVisited(bool MainFile)
Record that we've visited a function and whether or not that function was in the main source file.
Decl - This represents one declaration (or definition), e.g.
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
SourceLocation getLocation() const
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Represents a function declaration or definition.
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
GlobalDecl - represents a global declaration.
CXXCtorType getCtorType() const
CXXDtorType getDtorType() const
const Decl * getDecl() const
bool isNegatedConsteval() const
Represents Objective-C's collection statement.
ObjCMethodDecl - Represents an instance or class method declaration.
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
StmtClass getStmtClass() const
SourceLocation getBeginLoc() const LLVM_READONLY
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool LE(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.
@ Ctor_Base
Base object ctor.
bool isa(CodeGen::Address addr)
@ If
'if' clause, allowed on all the Compute Constructs, Data Constructs, Executable Constructs,...
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
@ Result
The result type of a method or function.
@ Dtor_Base
Base object dtor.
@ Type
The name was classified as a type.
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
U cast(CodeGen::Address addr)
@ None
The alignment was not explicit in code.
cl::opt< bool > SystemHeadersCoverage
Diagnostic wrappers for TextAPI types for error reporting.
cl::opt< bool > EnableSingleByteCoverage
Per-Function MC/DC state.
llvm::DenseMap< const Stmt *, Decision > DecisionByStmt