diff --git a/llvm/include/llvm/CodeGen/MachineSizeOpts.h b/llvm/include/llvm/CodeGen/MachineSizeOpts.h --- a/llvm/include/llvm/CodeGen/MachineSizeOpts.h +++ b/llvm/include/llvm/CodeGen/MachineSizeOpts.h @@ -23,14 +23,17 @@ class MachineFunction; /// Returns true if machine function \p MF is suggested to be size-optimized -/// base on the profile. -bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, - const MachineBlockFrequencyInfo *BFI); +/// base on the profile, false if not. Returns None if there is no info the +/// profile. +Optional shouldOptimizeForSize(const MachineFunction *MF, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *BFI); /// Returns true if machine basic block \p MBB is suggested to be size-optimized -/// base on the profile. -bool shouldOptimizeForSize(const MachineBasicBlock *MBB, - ProfileSummaryInfo *PSI, - const MachineBlockFrequencyInfo *MBFI); +/// base on the profile, false if not. Returns None if there is no info the +/// profile. +Optional shouldOptimizeForSize(const MachineBasicBlock *MBB, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI); } // end namespace llvm diff --git a/llvm/include/llvm/Transforms/Utils/SizeOpts.h b/llvm/include/llvm/Transforms/Utils/SizeOpts.h --- a/llvm/include/llvm/Transforms/Utils/SizeOpts.h +++ b/llvm/include/llvm/Transforms/Utils/SizeOpts.h @@ -33,16 +33,17 @@ class Function; class ProfileSummaryInfo; -template -bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, - BFIT *BFI) { +template +Optional shouldFuncOptimizeForSizeImpl(const FuncT *F, + ProfileSummaryInfo *PSI, + BFIT *BFI) { assert(F); if (!PSI || !BFI || !PSI->hasProfileSummary()) - return false; + return None; if (ForcePGSO) return true; if (!EnablePGSO) - return false; + return None; if (PGSOColdCodeOnly || (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize())) { // Even if the working set size isn't large, size-optimize cold code. @@ -53,16 +54,16 @@ F, PSI, *BFI); } -template -bool shouldOptimizeForSizeImpl(const BlockT *BB, ProfileSummaryInfo *PSI, - BFIT *BFI) { +template +Optional shouldOptimizeForSizeImpl(const BlockT *BB, + ProfileSummaryInfo *PSI, BFIT *BFI) { assert(BB); if (!PSI || !BFI || !PSI->hasProfileSummary()) - return false; + return None; if (ForcePGSO) return true; if (!EnablePGSO) - return false; + return None; if (PGSOColdCodeOnly || (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize())) { // Even if the working set size isn't large, size-optimize cold code. @@ -74,15 +75,15 @@ } /// Returns true if function \p F is suggested to be size-optimized base on the -/// profile. -bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI, - BlockFrequencyInfo *BFI); +/// profile, false if not. Returns None if there is no info the profile. +Optional shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI, + BlockFrequencyInfo *BFI); /// Returns true if basic block \p BB is suggested to be size-optimized base -/// on the profile. -bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI, - BlockFrequencyInfo *BFI); - +/// on the profile, false if not. Returns None if there is no info the profile. +Optional shouldOptimizeForSize(const BasicBlock *BB, + ProfileSummaryInfo *PSI, + BlockFrequencyInfo *BFI); } // end namespace llvm #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H diff --git a/llvm/lib/CodeGen/MachineSizeOpts.cpp b/llvm/lib/CodeGen/MachineSizeOpts.cpp --- a/llvm/lib/CodeGen/MachineSizeOpts.cpp +++ b/llvm/lib/CodeGen/MachineSizeOpts.cpp @@ -105,16 +105,16 @@ }; } // end anonymous namespace -bool llvm::shouldOptimizeForSize(const MachineFunction *MF, - ProfileSummaryInfo *PSI, - const MachineBlockFrequencyInfo *MBFI) { - return shouldFuncOptimizeForSizeImpl( - MF, PSI, MBFI); +Optional +llvm::shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + return shouldFuncOptimizeForSizeImpl(MF, PSI, + MBFI); } -bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB, - ProfileSummaryInfo *PSI, - const MachineBlockFrequencyInfo *MBFI) { - return shouldOptimizeForSizeImpl( - MBB, PSI, MBFI); +Optional +llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB, + ProfileSummaryInfo *PSI, + const MachineBlockFrequencyInfo *MBFI) { + return shouldOptimizeForSizeImpl(MBB, PSI, MBFI); } diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp --- a/llvm/lib/CodeGen/TargetLoweringBase.cpp +++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp @@ -1442,7 +1442,7 @@ // getEstimatedNumberOfCaseClusters() in BasicTTIImpl. const bool OptForSize = SI->getParent()->getParent()->hasOptSize() || - llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI); + llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI).getValueOr(false); const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize); const unsigned MaxJumpTableSize = getMaximumJumpTableSize(); diff --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp --- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp +++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp @@ -553,8 +553,9 @@ unsigned NumUses = 0; bool OptForSize = Entry->getParent()->hasOptSize() || - llvm::shouldOptimizeForSize(Entry->getParent(), PSI, BFI); - if (!OptForSize || std::distance(S,E) > 100) { + llvm::shouldOptimizeForSize(Entry->getParent(), PSI, BFI) + .getValueOr(false); + if (!OptForSize || std::distance(S, E) > 100) { for (auto ConstCand = S; ConstCand != E; ++ConstCand) { NumUses += ConstCand->Uses.size(); if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost) diff --git a/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp b/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp --- a/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp +++ b/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp @@ -544,8 +544,9 @@ auto *HeaderBB = L->getHeader(); auto *F = HeaderBB->getParent(); - bool OptForSize = F->hasOptSize() || - llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI); + bool OptForSize = + F->hasOptSize() || + llvm::shouldOptimizeForSize(HeaderBB, PSI, BFI).getValueOr(false); if (OptForSize) { LLVM_DEBUG( dbgs() << "Versioning is needed but not allowed when optimizing " diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp --- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -212,8 +212,9 @@ TTI.getUnrollingPreferences(L, SE, UP); // Apply size attributes - bool OptForSize = L->getHeader()->getParent()->hasOptSize() || - llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI); + bool OptForSize = + L->getHeader()->getParent()->hasOptSize() || + llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI).getValueOr(false); if (OptForSize) { UP.Threshold = UP.OptSizeThreshold; UP.PartialThreshold = UP.PartialOptSizeThreshold; diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2715,8 +2715,9 @@ // Don't rewrite fputs to fwrite when optimising for size because fwrite // requires more arguments and thus extra MOVs are required. - bool OptForSize = CI->getFunction()->hasOptSize() || - llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI); + bool OptForSize = + CI->getFunction()->hasOptSize() || + llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI).getValueOr(false); if (OptForSize) return nullptr; diff --git a/llvm/lib/Transforms/Utils/SizeOpts.cpp b/llvm/lib/Transforms/Utils/SizeOpts.cpp --- a/llvm/lib/Transforms/Utils/SizeOpts.cpp +++ b/llvm/lib/Transforms/Utils/SizeOpts.cpp @@ -69,12 +69,14 @@ }; } // end anonymous namespace -bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI, - BlockFrequencyInfo *BFI) { +Optional llvm::shouldOptimizeForSize(const Function *F, + ProfileSummaryInfo *PSI, + BlockFrequencyInfo *BFI) { return shouldFuncOptimizeForSizeImpl(F, PSI, BFI); } -bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI, - BlockFrequencyInfo *BFI) { +Optional llvm::shouldOptimizeForSize(const BasicBlock *BB, + ProfileSummaryInfo *PSI, + BlockFrequencyInfo *BFI) { return shouldOptimizeForSizeImpl(BB, PSI, BFI); } diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -7429,9 +7429,10 @@ ScalarEvolution *SE, DominatorTree *DT, const LoopAccessInfo *LAI) { ScalarEpilogueLowering SEL = CM_ScalarEpilogueAllowed; + Optional IsColdByProfile = + llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI); if (Hints.getForce() != LoopVectorizeHints::FK_Enabled && - (F->hasOptSize() || - llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI))) + (F->hasOptSize() || IsColdByProfile.getValueOr(false))) SEL = CM_ScalarEpilogueNotAllowedOptSize; else if (PreferPredicateOverEpilog || Hints.getPredicate() == LoopVectorizeHints::FK_Enabled || diff --git a/llvm/unittests/Target/X86/MachineSizeOptsTest.cpp b/llvm/unittests/Target/X86/MachineSizeOptsTest.cpp --- a/llvm/unittests/Target/X86/MachineSizeOptsTest.cpp +++ b/llvm/unittests/Target/X86/MachineSizeOptsTest.cpp @@ -113,13 +113,13 @@ ASSERT_TRUE(iter == BB0.succ_end()); MachineBasicBlock *BB3 = *BB1->succ_begin(); ASSERT_TRUE(BB3 == *BB2->succ_begin()); - EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, MBFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, MBFI_G)); - EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, MBFI_H)); - EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, MBFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, MBFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, MBFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, MBFI_F)); + EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, MBFI_F).getValueOr(false)); + EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, MBFI_G).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, MBFI_H).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, MBFI_F).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, MBFI_F).getValueOr(false)); + EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, MBFI_F).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, MBFI_F).getValueOr(false)); } const char* MachineSizeOptsTest::MIRString = R"MIR( diff --git a/llvm/unittests/Transforms/Utils/SizeOptsTest.cpp b/llvm/unittests/Transforms/Utils/SizeOptsTest.cpp --- a/llvm/unittests/Transforms/Utils/SizeOptsTest.cpp +++ b/llvm/unittests/Transforms/Utils/SizeOptsTest.cpp @@ -68,13 +68,13 @@ BasicBlock *BB3 = BB1->getSingleSuccessor(); EXPECT_TRUE(PSI.hasProfileSummary()); - EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, BFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, BFI_G)); - EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, BFI_H)); - EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, BFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, BFI_F)); - EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, BFI_F)); - EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, BFI_F)); + EXPECT_FALSE(shouldOptimizeForSize(F, &PSI, BFI_F).getValueOr(false)); + EXPECT_TRUE(shouldOptimizeForSize(G, &PSI, BFI_G).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(H, &PSI, BFI_H).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(&BB0, &PSI, BFI_F).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(BB1, &PSI, BFI_F).getValueOr(false)); + EXPECT_TRUE(shouldOptimizeForSize(BB2, &PSI, BFI_F).getValueOr(false)); + EXPECT_FALSE(shouldOptimizeForSize(BB3, &PSI, BFI_F).getValueOr(false)); } const char* SizeOptsTest::IRString = R"IR(