From c178ed33bd82151f21abc3c0015c671b33556999 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 12 Dec 2022 08:29:05 +0000 Subject: [PATCH] Transforms/Utils: llvm::Optional => std::optional --- .../llvm/Transforms/Scalar/LoopUnrollPass.h | 14 ++-- .../llvm/Transforms/Utils/BasicBlockUtils.h | 2 +- llvm/include/llvm/Transforms/Utils/LoopPeel.h | 4 +- .../include/llvm/Transforms/Utils/LoopUtils.h | 14 ++-- .../llvm/Transforms/Utils/PredicateInfo.h | 2 +- .../llvm/Transforms/Utils/SimplifyLibCalls.h | 6 +- .../llvm/Transforms/Utils/UnrollLoop.h | 7 +- llvm/lib/Transforms/Scalar/LoopDistribute.cpp | 2 +- .../Scalar/LoopUnrollAndJamPass.cpp | 8 +- llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 82 ++++++++++--------- llvm/lib/Transforms/Scalar/NewGVN.cpp | 2 +- .../Scalar/WarnMissedTransforms.cpp | 2 +- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 4 +- .../Transforms/Utils/BypassSlowDivision.cpp | 6 +- llvm/lib/Transforms/Utils/CodeMoverUtils.cpp | 15 ++-- llvm/lib/Transforms/Utils/Local.cpp | 6 +- llvm/lib/Transforms/Utils/LoopPeel.cpp | 18 ++-- llvm/lib/Transforms/Utils/LoopUnroll.cpp | 4 +- .../Transforms/Utils/LoopUnrollRuntime.cpp | 2 +- llvm/lib/Transforms/Utils/LoopUtils.cpp | 29 ++++--- llvm/lib/Transforms/Utils/PredicateInfo.cpp | 2 +- llvm/lib/Transforms/Utils/SCCPSolver.cpp | 3 +- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 4 +- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 9 +- .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 9 +- llvm/lib/Transforms/Utils/ValueMapper.cpp | 22 ++--- .../Transforms/Vectorize/LoopVectorize.cpp | 6 +- 27 files changed, 146 insertions(+), 138 deletions(-) diff --git a/llvm/include/llvm/Transforms/Scalar/LoopUnrollPass.h b/llvm/include/llvm/Transforms/Scalar/LoopUnrollPass.h index 6afe7ecd2a5de..8d8c2f254f02f 100644 --- a/llvm/include/llvm/Transforms/Scalar/LoopUnrollPass.h +++ b/llvm/include/llvm/Transforms/Scalar/LoopUnrollPass.h @@ -9,10 +9,10 @@ #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H -#include "llvm/ADT/Optional.h" #include "llvm/Analysis/LoopAnalysisManager.h" #include "llvm/IR/PassManager.h" #include "llvm/Support/CommandLine.h" +#include namespace llvm { @@ -59,12 +59,12 @@ class LoopFullUnrollPass : public PassInfoMixin { /// additional setters and then pass it to LoopUnrollPass. /// struct LoopUnrollOptions { - Optional AllowPartial; - Optional AllowPeeling; - Optional AllowRuntime; - Optional AllowUpperBound; - Optional AllowProfileBasedPeeling; - Optional FullUnrollMaxCount; + std::optional AllowPartial; + std::optional AllowPeeling; + std::optional AllowRuntime; + std::optional AllowUpperBound; + std::optional AllowProfileBasedPeeling; + std::optional FullUnrollMaxCount; int OptLevel; /// If false, use a cost model to determine whether unrolling of a loop is diff --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h index 9d921496ff107..3b2fe817c04cf 100644 --- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -581,7 +581,7 @@ BasicBlock *CreateControlFlowHub( DomTreeUpdater *DTU, SmallVectorImpl &GuardBlocks, const SetVector &Predecessors, const SetVector &Successors, const StringRef Prefix, - Optional MaxControlFlowBooleans = std::nullopt); + std::optional MaxControlFlowBooleans = std::nullopt); } // end namespace llvm diff --git a/llvm/include/llvm/Transforms/Utils/LoopPeel.h b/llvm/include/llvm/Transforms/Utils/LoopPeel.h index cd7499dfbf1b4..7dffaa4d3d9c3 100644 --- a/llvm/include/llvm/Transforms/Utils/LoopPeel.h +++ b/llvm/include/llvm/Transforms/Utils/LoopPeel.h @@ -26,8 +26,8 @@ bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE, TargetTransformInfo::PeelingPreferences gatherPeelingPreferences(Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, - Optional UserAllowPeeling, - Optional UserAllowProfileBasedPeeling, + std::optional UserAllowPeeling, + std::optional UserAllowProfileBasedPeeling, bool UnrollingSpecficValues = false); void computePeelCount(Loop *L, unsigned LoopSize, diff --git a/llvm/include/llvm/Transforms/Utils/LoopUtils.h b/llvm/include/llvm/Transforms/Utils/LoopUtils.h index 4f9c97708a5eb..0580cecfc7926 100644 --- a/llvm/include/llvm/Transforms/Utils/LoopUtils.h +++ b/llvm/include/llvm/Transforms/Utils/LoopUtils.h @@ -226,7 +226,7 @@ SmallVector findDefsUsedOutsideOfLoop(Loop *L); /// "llvm.loop.vectorize.scalable.enable") for a loop and use it to construct a /// ElementCount. If the metadata "llvm.loop.vectorize.width" cannot be found /// then std::nullopt is returned. -Optional +std::optional getOptionalElementCountLoopAttribute(const Loop *TheLoop); /// Create a new loop identifier for a loop created from a loop transformation. @@ -253,7 +253,7 @@ getOptionalElementCountLoopAttribute(const Loop *TheLoop); /// @p OrigLoopID: The original identifier can be reused. /// nullptr : The new loop has no attributes. /// MDNode* : A new unique loop identifier. -Optional +std::optional makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef FollowupAttrs, const char *InheritOptionsAttrsPrefix = "", bool AlwaysNew = false); @@ -311,7 +311,7 @@ void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, /// initialized with weight of loop's latch leading to the exit. /// Returns 0 when the count is estimated to be 0, or std::nullopt when a /// meaningful estimate can not be made. -Optional +std::optional getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight = nullptr); @@ -541,10 +541,10 @@ struct IVConditionInfo { /// If the branch condition of the header is partially invariant, return a pair /// containing the instructions to duplicate and a boolean Constant to update /// the condition in the loops created for the true or false successors. -Optional hasPartialIVCondition(const Loop &L, - unsigned MSSAThreshold, - const MemorySSA &MSSA, - AAResults &AA); +std::optional hasPartialIVCondition(const Loop &L, + unsigned MSSAThreshold, + const MemorySSA &MSSA, + AAResults &AA); } // end namespace llvm diff --git a/llvm/include/llvm/Transforms/Utils/PredicateInfo.h b/llvm/include/llvm/Transforms/Utils/PredicateInfo.h index e57e598b69180..d2224b61103c9 100644 --- a/llvm/include/llvm/Transforms/Utils/PredicateInfo.h +++ b/llvm/include/llvm/Transforms/Utils/PredicateInfo.h @@ -103,7 +103,7 @@ class PredicateBase : public ilist_node { } /// Fetch condition in the form of PredicateConstraint, if possible. - Optional getConstraint() const; + std::optional getConstraint() const; protected: PredicateBase(PredicateType PT, Value *Op, Value *Condition) diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h index 978339617c2ed..90a5ffc5b4fa0 100644 --- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -89,9 +89,9 @@ class FortifiedLibCallSimplifier { /// parameter. These are used by an implementation to opt-into stricter /// checking. bool isFortifiedCallFoldable(CallInst *CI, unsigned ObjSizeOp, - Optional SizeOp = std::nullopt, - Optional StrOp = std::nullopt, - Optional FlagsOp = std::nullopt); + std::optional SizeOp = std::nullopt, + std::optional StrOp = std::nullopt, + std::optional FlagsOp = std::nullopt); }; /// LibCallSimplifier - This class implements a collection of optimizations diff --git a/llvm/include/llvm/Transforms/Utils/UnrollLoop.h b/llvm/include/llvm/Transforms/Utils/UnrollLoop.h index b4f2c59f3f694..4f3010965b591 100644 --- a/llvm/include/llvm/Transforms/Utils/UnrollLoop.h +++ b/llvm/include/llvm/Transforms/Utils/UnrollLoop.h @@ -121,9 +121,10 @@ TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences( Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, llvm::OptimizationRemarkEmitter &ORE, int OptLevel, - Optional UserThreshold, Optional UserCount, - Optional UserAllowPartial, Optional UserRuntime, - Optional UserUpperBound, Optional UserFullUnrollMaxCount); + std::optional UserThreshold, std::optional UserCount, + std::optional UserAllowPartial, std::optional UserRuntime, + std::optional UserUpperBound, + std::optional UserFullUnrollMaxCount); InstructionCost ApproximateLoopSize(const Loop *L, unsigned &NumCalls, bool &NotDuplicatable, bool &Convergent, const TargetTransformInfo &TTI, diff --git a/llvm/lib/Transforms/Scalar/LoopDistribute.cpp b/llvm/lib/Transforms/Scalar/LoopDistribute.cpp index 434bee101ae75..7a2a34a8d595f 100644 --- a/llvm/lib/Transforms/Scalar/LoopDistribute.cpp +++ b/llvm/lib/Transforms/Scalar/LoopDistribute.cpp @@ -593,7 +593,7 @@ class InstPartitionContainer { /// Assign new LoopIDs for the partition's cloned loop. void setNewLoopID(MDNode *OrigLoopID, InstPartition *Part) { - Optional PartitionID = makeFollowupLoopID( + std::optional PartitionID = makeFollowupLoopID( OrigLoopID, {LLVMLoopDistributeFollowupAll, Part->hasDepCycle() ? LLVMLoopDistributeFollowupSequential diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp index 00d8b8866e36f..e51b24e849e8a 100644 --- a/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp @@ -368,7 +368,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, // To assign the loop id of the epilogue, assign it before unrolling it so it // is applied to every inner loop of the epilogue. We later apply the loop ID // for the jammed inner loop. - Optional NewInnerEpilogueLoopID = makeFollowupLoopID( + std::optional NewInnerEpilogueLoopID = makeFollowupLoopID( OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupRemainderInner}); if (NewInnerEpilogueLoopID) @@ -398,14 +398,14 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, // Assign new loop attributes. if (EpilogueOuterLoop) { - Optional NewOuterEpilogueLoopID = makeFollowupLoopID( + std::optional NewOuterEpilogueLoopID = makeFollowupLoopID( OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupRemainderOuter}); if (NewOuterEpilogueLoopID) EpilogueOuterLoop->setLoopID(NewOuterEpilogueLoopID.value()); } - Optional NewInnerLoopID = + std::optional NewInnerLoopID = makeFollowupLoopID(OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupInner}); if (NewInnerLoopID) @@ -414,7 +414,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, SubLoop->setLoopID(OrigSubLoopID); if (UnrollResult == LoopUnrollResult::PartiallyUnrolled) { - Optional NewOuterLoopID = makeFollowupLoopID( + std::optional NewOuterLoopID = makeFollowupLoopID( OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupOuter}); if (NewOuterLoopID) { diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp index 84705570dee39..cc0ca7ab9717c 100644 --- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -185,9 +185,10 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences( Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter &ORE, int OptLevel, - Optional UserThreshold, Optional UserCount, - Optional UserAllowPartial, Optional UserRuntime, - Optional UserUpperBound, Optional UserFullUnrollMaxCount) { + std::optional UserThreshold, std::optional UserCount, + std::optional UserAllowPartial, std::optional UserRuntime, + std::optional UserUpperBound, + std::optional UserFullUnrollMaxCount) { TargetTransformInfo::UnrollingPreferences UP; // Set up the defaults @@ -1119,17 +1120,20 @@ bool llvm::computeUnrollCount( return ExplicitUnroll; } -static LoopUnrollResult tryToUnrollLoop( - Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, - const TargetTransformInfo &TTI, AssumptionCache &AC, - OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, - ProfileSummaryInfo *PSI, bool PreserveLCSSA, int OptLevel, - bool OnlyWhenForced, bool ForgetAllSCEV, Optional ProvidedCount, - Optional ProvidedThreshold, Optional ProvidedAllowPartial, - Optional ProvidedRuntime, Optional ProvidedUpperBound, - Optional ProvidedAllowPeeling, - Optional ProvidedAllowProfileBasedPeeling, - Optional ProvidedFullUnrollMaxCount) { +static LoopUnrollResult +tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, + const TargetTransformInfo &TTI, AssumptionCache &AC, + OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, + ProfileSummaryInfo *PSI, bool PreserveLCSSA, int OptLevel, + bool OnlyWhenForced, bool ForgetAllSCEV, + std::optional ProvidedCount, + std::optional ProvidedThreshold, + std::optional ProvidedAllowPartial, + std::optional ProvidedRuntime, + std::optional ProvidedUpperBound, + std::optional ProvidedAllowPeeling, + std::optional ProvidedAllowProfileBasedPeeling, + std::optional ProvidedFullUnrollMaxCount) { LLVM_DEBUG(dbgs() << "Loop Unroll: F[" << L->getHeader()->getParent()->getName() << "] Loop %" << L->getHeader()->getName() << "\n"); @@ -1321,7 +1325,7 @@ static LoopUnrollResult tryToUnrollLoop( return LoopUnrollResult::Unmodified; if (RemainderLoop) { - Optional RemainderLoopID = + std::optional RemainderLoopID = makeFollowupLoopID(OrigLoopID, {LLVMLoopUnrollFollowupAll, LLVMLoopUnrollFollowupRemainder}); if (RemainderLoopID) @@ -1329,7 +1333,7 @@ static LoopUnrollResult tryToUnrollLoop( } if (UnrollResult != LoopUnrollResult::FullyUnrolled) { - Optional NewLoopID = + std::optional NewLoopID = makeFollowupLoopID(OrigLoopID, {LLVMLoopUnrollFollowupAll, LLVMLoopUnrollFollowupUnrolled}); if (NewLoopID) { @@ -1367,25 +1371,25 @@ class LoopUnroll : public LoopPass { /// Otherwise, forgetAllLoops and rebuild when needed next. bool ForgetAllSCEV; - Optional ProvidedCount; - Optional ProvidedThreshold; - Optional ProvidedAllowPartial; - Optional ProvidedRuntime; - Optional ProvidedUpperBound; - Optional ProvidedAllowPeeling; - Optional ProvidedAllowProfileBasedPeeling; - Optional ProvidedFullUnrollMaxCount; + std::optional ProvidedCount; + std::optional ProvidedThreshold; + std::optional ProvidedAllowPartial; + std::optional ProvidedRuntime; + std::optional ProvidedUpperBound; + std::optional ProvidedAllowPeeling; + std::optional ProvidedAllowProfileBasedPeeling; + std::optional ProvidedFullUnrollMaxCount; LoopUnroll(int OptLevel = 2, bool OnlyWhenForced = false, bool ForgetAllSCEV = false, - Optional Threshold = std::nullopt, - Optional Count = std::nullopt, - Optional AllowPartial = std::nullopt, - Optional Runtime = std::nullopt, - Optional UpperBound = std::nullopt, - Optional AllowPeeling = std::nullopt, - Optional AllowProfileBasedPeeling = std::nullopt, - Optional ProvidedFullUnrollMaxCount = std::nullopt) + std::optional Threshold = std::nullopt, + std::optional Count = std::nullopt, + std::optional AllowPartial = std::nullopt, + std::optional Runtime = std::nullopt, + std::optional UpperBound = std::nullopt, + std::optional AllowPeeling = std::nullopt, + std::optional AllowProfileBasedPeeling = std::nullopt, + std::optional ProvidedFullUnrollMaxCount = std::nullopt) : LoopPass(ID), OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced), ForgetAllSCEV(ForgetAllSCEV), ProvidedCount(std::move(Count)), ProvidedThreshold(Threshold), ProvidedAllowPartial(AllowPartial), @@ -1457,12 +1461,12 @@ Pass *llvm::createLoopUnrollPass(int OptLevel, bool OnlyWhenForced, // callers. return new LoopUnroll( OptLevel, OnlyWhenForced, ForgetAllSCEV, - Threshold == -1 ? std::nullopt : Optional(Threshold), - Count == -1 ? std::nullopt : Optional(Count), - AllowPartial == -1 ? std::nullopt : Optional(AllowPartial), - Runtime == -1 ? std::nullopt : Optional(Runtime), - UpperBound == -1 ? std::nullopt : Optional(UpperBound), - AllowPeeling == -1 ? std::nullopt : Optional(AllowPeeling)); + Threshold == -1 ? std::nullopt : std::optional(Threshold), + Count == -1 ? std::nullopt : std::optional(Count), + AllowPartial == -1 ? std::nullopt : std::optional(AllowPartial), + Runtime == -1 ? std::nullopt : std::optional(Runtime), + UpperBound == -1 ? std::nullopt : std::optional(UpperBound), + AllowPeeling == -1 ? std::nullopt : std::optional(AllowPeeling)); } Pass *llvm::createSimpleLoopUnrollPass(int OptLevel, bool OnlyWhenForced, @@ -1611,7 +1615,7 @@ PreservedAnalyses LoopUnrollPass::run(Function &F, // Check if the profile summary indicates that the profiled application // has a huge working set size, in which case we disable peeling to avoid // bloating it further. - Optional LocalAllowPeeling = UnrollOpts.AllowPeeling; + std::optional LocalAllowPeeling = UnrollOpts.AllowPeeling; if (PSI && PSI->hasHugeWorkingSetSize()) LocalAllowPeeling = false; std::string LoopName = std::string(L.getName()); diff --git a/llvm/lib/Transforms/Scalar/NewGVN.cpp b/llvm/lib/Transforms/Scalar/NewGVN.cpp index afa346ea320e8..1ec5bc1a39d86 100644 --- a/llvm/lib/Transforms/Scalar/NewGVN.cpp +++ b/llvm/lib/Transforms/Scalar/NewGVN.cpp @@ -1563,7 +1563,7 @@ NewGVN::performSymbolicPredicateInfoEvaluation(IntrinsicInst *I) const { LLVM_DEBUG(dbgs() << "Found predicate info from instruction !\n"); - const Optional &Constraint = PI->getConstraint(); + const std::optional &Constraint = PI->getConstraint(); if (!Constraint) return ExprResult::none(); diff --git a/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp b/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp index eeed11c0b533f..9e08954ef6433 100644 --- a/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp +++ b/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp @@ -48,7 +48,7 @@ static void warnAboutLeftoverTransformations(Loop *L, if (hasVectorizeTransformation(L) == TM_ForcedByUser) { LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n"); - Optional VectorizeWidth = + std::optional VectorizeWidth = getOptionalElementCountLoopAttribute(L); std::optional InterleaveCount = getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count"); diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index 9876681a19245..871d742e38135 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -1903,7 +1903,7 @@ convertToGuardPredicates(SmallVectorImpl &GuardBlocks, SmallVectorImpl &DeletionCandidates, const BBSetVector &Incoming, const BBSetVector &Outgoing, const StringRef Prefix, - Optional MaxControlFlowBooleans) { + std::optional MaxControlFlowBooleans) { BBPredicates GuardPredicates; auto F = Incoming.front()->getParent(); @@ -1928,7 +1928,7 @@ convertToGuardPredicates(SmallVectorImpl &GuardBlocks, BasicBlock *llvm::CreateControlFlowHub( DomTreeUpdater *DTU, SmallVectorImpl &GuardBlocks, const BBSetVector &Incoming, const BBSetVector &Outgoing, - const StringRef Prefix, Optional MaxControlFlowBooleans) { + const StringRef Prefix, std::optional MaxControlFlowBooleans) { if (Outgoing.size() < 2) return Outgoing.front(); diff --git a/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp b/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp index fcdf31db28f4d..60012a71ed10d 100644 --- a/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp +++ b/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp @@ -86,7 +86,7 @@ class FastDivInsertionTask { QuotRemPair createDivRemPhiNodes(QuotRemWithBB &LHS, QuotRemWithBB &RHS, BasicBlock *PhiBB); Value *insertOperandRuntimeCheck(Value *Op1, Value *Op2); - Optional insertFastDivAndRem(); + std::optional insertFastDivAndRem(); bool isSignedOp() { return SlowDivOrRem->getOpcode() == Instruction::SDiv || @@ -160,7 +160,7 @@ Value *FastDivInsertionTask::getReplacement(DivCacheTy &Cache) { if (CacheI == Cache.end()) { // If previous instance does not exist, try to insert fast div. - Optional OptResult = insertFastDivAndRem(); + std::optional OptResult = insertFastDivAndRem(); // Bail out if insertFastDivAndRem has failed. if (!OptResult) return nullptr; @@ -349,7 +349,7 @@ Value *FastDivInsertionTask::insertOperandRuntimeCheck(Value *Op1, Value *Op2) { /// Substitutes the div/rem instruction with code that checks the value of the /// operands and uses a shorter-faster div/rem instruction when possible. -Optional FastDivInsertionTask::insertFastDivAndRem() { +std::optional FastDivInsertionTask::insertFastDivAndRem() { Value *Dividend = SlowDivOrRem->getOperand(0); Value *Divisor = SlowDivOrRem->getOperand(1); diff --git a/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp b/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp index 2ff5aec8f8496..55bc74c00abd7 100644 --- a/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp +++ b/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp @@ -60,7 +60,7 @@ class ControlConditions { /// \p BB from \p Dominator. If \p MaxLookup is non-zero, it limits the /// number of conditions to collect. Return std::nullopt if not all conditions /// are collected successfully, or we hit the limit. - static const Optional + static const std::optional collectControlConditions(const BasicBlock &BB, const BasicBlock &Dominator, const DominatorTree &DT, const PostDominatorTree &PDT, @@ -105,9 +105,12 @@ static bool domTreeLevelBefore(DominatorTree *DT, const Instruction *InstA, return DA->getLevel() < DB->getLevel(); } -const Optional ControlConditions::collectControlConditions( - const BasicBlock &BB, const BasicBlock &Dominator, const DominatorTree &DT, - const PostDominatorTree &PDT, unsigned MaxLookup) { +const std::optional +ControlConditions::collectControlConditions(const BasicBlock &BB, + const BasicBlock &Dominator, + const DominatorTree &DT, + const PostDominatorTree &PDT, + unsigned MaxLookup) { assert(DT.dominates(&Dominator, &BB) && "Expecting Dominator to dominate BB"); ControlConditions Conditions; @@ -249,13 +252,13 @@ bool llvm::isControlFlowEquivalent(const BasicBlock &BB0, const BasicBlock &BB1, << " and " << BB1.getName() << " is " << CommonDominator->getName() << "\n"); - const Optional BB0Conditions = + const std::optional BB0Conditions = ControlConditions::collectControlConditions(BB0, *CommonDominator, DT, PDT); if (BB0Conditions == std::nullopt) return false; - const Optional BB1Conditions = + const std::optional BB1Conditions = ControlConditions::collectControlConditions(BB1, *CommonDominator, DT, PDT); if (BB1Conditions == std::nullopt) diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index af75fe11ad452..90e48f272655f 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -3030,9 +3030,9 @@ struct BitPart { /// /// Because we pass around references into \c BPS, we must use a container that /// does not invalidate internal references (std::map instead of DenseMap). -static const Optional & +static const std::optional & collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, - std::map> &BPS, int Depth, + std::map> &BPS, int Depth, bool &FoundRoot) { auto I = BPS.find(V); if (I != BPS.end()) @@ -3282,7 +3282,7 @@ bool llvm::recognizeBSwapOrBitReverseIdiom( // Try to find all the pieces corresponding to the bswap. bool FoundRoot = false; - std::map> BPS; + std::map> BPS; const auto &Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot); if (!Res) diff --git a/llvm/lib/Transforms/Utils/LoopPeel.cpp b/llvm/lib/Transforms/Utils/LoopPeel.cpp index 0c24c22c2d74e..d370d6c7fa214 100644 --- a/llvm/lib/Transforms/Utils/LoopPeel.cpp +++ b/llvm/lib/Transforms/Utils/LoopPeel.cpp @@ -161,7 +161,7 @@ class PhiAnalyzer { // Calculate the sufficient minimum number of iterations of the loop to peel // such that phi instructions become determined (subject to allowable limits) - Optional calculateIterationsToPeel(); + std::optional calculateIterationsToPeel(); protected: using PeelCounter = std::optional; @@ -253,7 +253,7 @@ PhiAnalyzer::PeelCounter PhiAnalyzer::calculate(const Value &V) { return Unknown; } -Optional PhiAnalyzer::calculateIterationsToPeel() { +std::optional PhiAnalyzer::calculateIterationsToPeel() { unsigned Iterations = 0; for (auto &PHI : L.getHeader()->phis()) { PeelCounter ToInvariance = calculate(PHI); @@ -265,7 +265,7 @@ Optional PhiAnalyzer::calculateIterationsToPeel() { } } assert((Iterations <= MaxIterations) && "bad result in phi analysis"); - return Iterations ? Optional(Iterations) : std::nullopt; + return Iterations ? std::optional(Iterations) : std::nullopt; } } // unnamed namespace @@ -570,7 +570,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize, if (L->getHeader()->getParent()->hasProfileData()) { if (violatesLegacyMultiExitLoopCheck(L)) return; - Optional EstimatedTripCount = getLoopEstimatedTripCount(L); + std::optional EstimatedTripCount = getLoopEstimatedTripCount(L); if (!EstimatedTripCount) return; @@ -808,10 +808,12 @@ static void cloneLoopBlocks( LVMap[KV.first] = KV.second; } -TargetTransformInfo::PeelingPreferences llvm::gatherPeelingPreferences( - Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, - Optional UserAllowPeeling, - Optional UserAllowProfileBasedPeeling, bool UnrollingSpecficValues) { +TargetTransformInfo::PeelingPreferences +llvm::gatherPeelingPreferences(Loop *L, ScalarEvolution &SE, + const TargetTransformInfo &TTI, + std::optional UserAllowPeeling, + std::optional UserAllowProfileBasedPeeling, + bool UnrollingSpecficValues) { TargetTransformInfo::PeelingPreferences PP; // Set the default values. diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp index da8d3756a00b2..984a0010bed01 100644 --- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp @@ -699,7 +699,7 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, }; auto WillExit = [&](const ExitInfo &Info, unsigned i, unsigned j, - bool IsLatch) -> Optional { + bool IsLatch) -> std::optional { if (CompletelyUnroll) { if (PreserveOnlyFirst) { if (i == 0) @@ -739,7 +739,7 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, // The branch destination. unsigned j = (i + 1) % e; bool IsLatch = Pair.first == LatchBlock; - Optional KnownWillExit = WillExit(Info, i, j, IsLatch); + std::optional KnownWillExit = WillExit(Info, i, j, IsLatch); if (!KnownWillExit) continue; diff --git a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp index 0d1ed73d1ed42..f063438c7c41f 100644 --- a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -400,7 +400,7 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder, if (UnrollRemainder) return NewLoop; - Optional NewLoopID = makeFollowupLoopID( + std::optional NewLoopID = makeFollowupLoopID( LoopID, {LLVMLoopUnrollFollowupAll, LLVMLoopUnrollFollowupRemainder}); if (NewLoopID) { NewLoop->setLoopID(NewLoopID.value()); diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 9ada3a2dbe2a5..0e4576f807351 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -247,7 +247,7 @@ void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD, TheLoop->setLoopID(NewLoopID); } -Optional +std::optional llvm::getOptionalElementCountLoopAttribute(const Loop *TheLoop) { std::optional Width = getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width"); @@ -261,7 +261,7 @@ llvm::getOptionalElementCountLoopAttribute(const Loop *TheLoop) { return std::nullopt; } -Optional llvm::makeFollowupLoopID( +std::optional llvm::makeFollowupLoopID( MDNode *OrigLoopID, ArrayRef FollowupOptions, const char *InheritOptionsExceptPrefix, bool AlwaysNew) { if (!OrigLoopID) { @@ -396,7 +396,7 @@ TransformationMode llvm::hasVectorizeTransformation(const Loop *L) { if (Enable == false) return TM_SuppressedByUser; - Optional VectorizeWidth = + std::optional VectorizeWidth = getOptionalElementCountLoopAttribute(L); std::optional InterleaveCount = getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count"); @@ -782,9 +782,9 @@ static BranchInst *getExpectedExitLoopLatchBranch(Loop *L) { /// Return the estimated trip count for any exiting branch which dominates /// the loop latch. -static Optional -getEstimatedTripCount(BranchInst *ExitingBranch, Loop *L, - uint64_t &OrigExitWeight) { +static std::optional getEstimatedTripCount(BranchInst *ExitingBranch, + Loop *L, + uint64_t &OrigExitWeight) { // To estimate the number of times the loop body was executed, we want to // know the number of times the backedge was taken, vs. the number of times // we exited the loop. @@ -808,7 +808,7 @@ getEstimatedTripCount(BranchInst *ExitingBranch, Loop *L, return ExitCount + 1; } -Optional +std::optional llvm::getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight) { // Currently we take the estimate exit count only from the loop latch, @@ -817,8 +817,8 @@ llvm::getLoopEstimatedTripCount(Loop *L, // TODO: incorporate information from other exits if (BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L)) { uint64_t ExitWeight; - if (Optional EstTripCount = - getEstimatedTripCount(LatchBranch, L, ExitWeight)) { + if (std::optional EstTripCount = + getEstimatedTripCount(LatchBranch, L, ExitWeight)) { if (EstimatedLoopInvocationWeight) *EstimatedLoopInvocationWeight = ExitWeight; return *EstTripCount; @@ -1477,7 +1477,7 @@ void llvm::setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop, // Get number of iterations in the original scalar loop. unsigned OrigLoopInvocationWeight = 0; - Optional OrigAverageTripCount = + std::optional OrigAverageTripCount = getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight); if (!OrigAverageTripCount) return; @@ -1704,10 +1704,9 @@ Value *llvm::addDiffRuntimeChecks( return MemoryRuntimeCheck; } -Optional llvm::hasPartialIVCondition(const Loop &L, - unsigned MSSAThreshold, - const MemorySSA &MSSA, - AAResults &AA) { +std::optional +llvm::hasPartialIVCondition(const Loop &L, unsigned MSSAThreshold, + const MemorySSA &MSSA, AAResults &AA) { auto *TI = dyn_cast(L.getHeader()->getTerminator()); if (!TI || !TI->isConditional()) return {}; @@ -1764,7 +1763,7 @@ Optional llvm::hasPartialIVCondition(const Loop &L, [&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate, MSSAThreshold](BasicBlock *Succ, BasicBlock *Header, SmallVector AccessesToCheck) - -> Optional { + -> std::optional { IVConditionInfo Info; // First, collect all blocks in the loop that are on a patch from Succ // to the header. diff --git a/llvm/lib/Transforms/Utils/PredicateInfo.cpp b/llvm/lib/Transforms/Utils/PredicateInfo.cpp index 9a1f33410a7b1..1f16ba78bdb08 100644 --- a/llvm/lib/Transforms/Utils/PredicateInfo.cpp +++ b/llvm/lib/Transforms/Utils/PredicateInfo.cpp @@ -783,7 +783,7 @@ PredicateInfo::~PredicateInfo() { } } -Optional PredicateBase::getConstraint() const { +std::optional PredicateBase::getConstraint() const { switch (Type) { case PT_Assume: case PT_Branch: { diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp index eed366c41deb5..437d2884e3f2f 100644 --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -1558,7 +1558,8 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) { const auto *PI = getPredicateInfoFor(&CB); assert(PI && "Missing predicate info for ssa.copy"); - const Optional &Constraint = PI->getConstraint(); + const std::optional &Constraint = + PI->getConstraint(); if (!Constraint) { mergeInValue(ValueState[&CB], &CB, CopyOfVal); return; diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index fecf2b109bb49..a9b883094ab20 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3139,7 +3139,7 @@ static ConstantInt *getKnownValueOnEdge(Value *V, BasicBlock *From, /// If we have a conditional branch on something for which we know the constant /// value in predecessors (e.g. a phi node in the current block), thread edges /// from the predecessor to their ultimate destination. -static Optional +static std::optional FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL, AssumptionCache *AC) { @@ -3283,7 +3283,7 @@ static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL, AssumptionCache *AC) { - Optional Result; + std::optional Result; bool EverChanged = false; do { // Note that None means "we changed things, but recurse further." diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index 9f0ad99f3220a..a7fe065c989f0 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -1035,12 +1035,13 @@ class WidenIV { // context. DenseMap PostIncRangeInfos; - Optional getPostIncRangeInfo(Value *Def, - Instruction *UseI) { + std::optional getPostIncRangeInfo(Value *Def, + Instruction *UseI) { DefUserPair Key(Def, UseI); auto It = PostIncRangeInfos.find(Key); - return It == PostIncRangeInfos.end() ? Optional(std::nullopt) - : Optional(It->second); + return It == PostIncRangeInfos.end() + ? std::optional(std::nullopt) + : std::optional(It->second); } void calculatePostIncRanges(PHINode *OrigPhi); diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 061ddd7432163..610d005db79a0 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -3746,12 +3746,9 @@ void LibCallSimplifier::eraseFromParent(Instruction *I) { // Fortified Library Call Optimizations //===----------------------------------------------------------------------===// -bool -FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI, - unsigned ObjSizeOp, - Optional SizeOp, - Optional StrOp, - Optional FlagOp) { +bool FortifiedLibCallSimplifier::isFortifiedCallFoldable( + CallInst *CI, unsigned ObjSizeOp, std::optional SizeOp, + std::optional StrOp, std::optional FlagOp) { // If this function takes a flag argument, the implementation may use it to // perform extra checks. Don't fold into the non-checking variant. if (FlagOp) { diff --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp index 0d2e415260242..341783b870430 100644 --- a/llvm/lib/Transforms/Utils/ValueMapper.cpp +++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp @@ -180,7 +180,7 @@ class Mapper { Value *mapBlockAddress(const BlockAddress &BA); /// Map metadata that doesn't require visiting operands. - Optional mapSimpleMetadata(const Metadata *MD); + std::optional mapSimpleMetadata(const Metadata *MD); Metadata *mapToMetadata(const Metadata *Key, Metadata *Val); Metadata *mapToSelf(const Metadata *MD); @@ -272,7 +272,7 @@ class MDNodeMapper { /// \return std::nullopt if \c Op is an unmapped uniqued \a MDNode. /// \post getMappedOp(Op) only returns std::nullopt if this returns /// std::nullopt. - Optional tryToMapOperand(const Metadata *Op); + std::optional tryToMapOperand(const Metadata *Op); /// Map a distinct node. /// @@ -284,7 +284,7 @@ class MDNodeMapper { MDNode *mapDistinctNode(const MDNode &N); /// Get a previously mapped node. - Optional getMappedOp(const Metadata *Op) const; + std::optional getMappedOp(const Metadata *Op) const; /// Create a post-order traversal of an unmapped uniqued node subgraph. /// @@ -558,11 +558,11 @@ Metadata *Mapper::mapToSelf(const Metadata *MD) { return mapToMetadata(MD, const_cast(MD)); } -Optional MDNodeMapper::tryToMapOperand(const Metadata *Op) { +std::optional MDNodeMapper::tryToMapOperand(const Metadata *Op) { if (!Op) return nullptr; - if (Optional MappedOp = M.mapSimpleMetadata(Op)) { + if (std::optional MappedOp = M.mapSimpleMetadata(Op)) { #ifndef NDEBUG if (auto *CMD = dyn_cast(Op)) assert((!*MappedOp || M.getVM().count(CMD->getValue()) || @@ -606,7 +606,7 @@ static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD, return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr; } -Optional MDNodeMapper::getMappedOp(const Metadata *Op) const { +std::optional MDNodeMapper::getMappedOp(const Metadata *Op) const { if (!Op) return nullptr; @@ -704,7 +704,7 @@ MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I, MDNode::op_iterator E, bool &HasChanged) { while (I != E) { Metadata *Op = *I++; // Increment even on early return. - if (Optional MappedOp = tryToMapOperand(Op)) { + if (std::optional MappedOp = tryToMapOperand(Op)) { // Check if the operand changes. HasChanged |= Op != *MappedOp; continue; @@ -757,7 +757,7 @@ void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) { // Clone the uniqued node and remap the operands. TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone(); remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) { - if (Optional MappedOp = getMappedOp(Old)) + if (std::optional MappedOp = getMappedOp(Old)) return *MappedOp; (void)D; assert(G.Info[Old].ID > D.ID && "Expected a forward reference"); @@ -796,7 +796,7 @@ Metadata *MDNodeMapper::map(const MDNode &N) { N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N); while (!DistinctWorklist.empty()) remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) { - if (Optional MappedOp = tryToMapOperand(Old)) + if (std::optional MappedOp = tryToMapOperand(Old)) return *MappedOp; return mapTopLevelUniquedNode(*cast(Old)); }); @@ -825,7 +825,7 @@ Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) { return *getMappedOp(&FirstN); } -Optional Mapper::mapSimpleMetadata(const Metadata *MD) { +std::optional Mapper::mapSimpleMetadata(const Metadata *MD) { // If the value already exists in the map, use it. if (std::optional NewMD = getVM().getMappedMD(MD)) return *NewMD; @@ -855,7 +855,7 @@ Metadata *Mapper::mapMetadata(const Metadata *MD) { assert(MD && "Expected valid metadata"); assert(!isa(MD) && "Unexpected local metadata"); - if (Optional NewMD = mapSimpleMetadata(MD)) + if (std::optional NewMD = mapSimpleMetadata(MD)) return *NewMD; return MDNodeMapper(*this).map(*cast(MD)); diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index f3d26507fc350..f366c31a5bbe6 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -410,7 +410,7 @@ static Optional getSmallBestKnownTC(ScalarEvolution &SE, Loop *L) { // Check if there is an expected trip count available from profile data. if (LoopVectorizeWithBlockFrequency) if (auto EstimatedTC = getLoopEstimatedTripCount(L)) - return EstimatedTC; + return *EstimatedTC; // Check if upper bound estimate is known. if (unsigned ExpectedTC = SE.getSmallConstantMaxTripCount(L)) @@ -7683,7 +7683,7 @@ void LoopVectorizationPlanner::executePlan(ElementCount BestVF, unsigned BestUF, // replace the vectorizer-specific hints below). MDNode *OrigLoopID = OrigLoop->getLoopID(); - Optional VectorizedLoopID = + std::optional VectorizedLoopID = makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll, LLVMLoopVectorizeFollowupVectorized}); @@ -10541,7 +10541,7 @@ bool LoopVectorizePass::processLoop(Loop *L) { checkMixedPrecision(L, ORE); } - Optional RemainderLoopID = + std::optional RemainderLoopID = makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll, LLVMLoopVectorizeFollowupEpilogue}); if (RemainderLoopID) {