Skip to content

Commit

Permalink
[Transforms/Scalar] llvm::Optional => std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Dec 13, 2022
1 parent dbfe446 commit 3152156
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Scalar/Float2Int.h
Expand Up @@ -42,7 +42,7 @@ class Float2IntPass : public PassInfoMixin<Float2IntPass> {
ConstantRange badRange();
ConstantRange unknownRange();
ConstantRange validateRange(ConstantRange R);
Optional<ConstantRange> calcRange(Instruction *I);
std::optional<ConstantRange> calcRange(Instruction *I);
void walkBackwards();
void walkForwards();
bool validateAndTransform();
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Transforms/Scalar/LoopPassManager.h
Expand Up @@ -188,7 +188,7 @@ class PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
/// PassInstrumentation's BeforePass returns false. Otherwise, returns the
/// preserved analyses of the pass.
template <typename IRUnitT, typename PassT>
Optional<PreservedAnalyses>
std::optional<PreservedAnalyses>
runSinglePass(IRUnitT &IR, PassT &Pass, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U,
PassInstrumentation &PI);
Expand Down Expand Up @@ -394,7 +394,7 @@ class LPMUpdater {
};

template <typename IRUnitT, typename PassT>
Optional<PreservedAnalyses> LoopPassManager::runSinglePass(
std::optional<PreservedAnalyses> LoopPassManager::runSinglePass(
IRUnitT &IR, PassT &Pass, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U, PassInstrumentation &PI) {
// Get the loop in case of Loop pass and outermost loop in case of LoopNest
Expand Down
6 changes: 3 additions & 3 deletions llvm/include/llvm/Transforms/Scalar/Scalarizer.h
Expand Up @@ -17,8 +17,8 @@
#ifndef LLVM_TRANSFORMS_SCALAR_SCALARIZER_H
#define LLVM_TRANSFORMS_SCALAR_SCALARIZER_H

#include "llvm/ADT/Optional.h"
#include "llvm/IR/PassManager.h"
#include <optional>

namespace llvm {

Expand All @@ -30,8 +30,8 @@ struct ScalarizerPassOptions {
// Scalarizer.cpp. When the cl::opt are specified, they take precedence.
// When the cl::opt are not specified, the present optional booleans allow to
// override the cl::opt's default values.
llvm::Optional<bool> ScalarizeVariableInsertExtract;
llvm::Optional<bool> ScalarizeLoadStore;
std::optional<bool> ScalarizeVariableInsertExtract;
std::optional<bool> ScalarizeLoadStore;
};

class ScalarizerPass : public PassInfoMixin<ScalarizerPass> {
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
Expand Up @@ -35,7 +35,6 @@
#include "llvm/Transforms/Scalar/ConstantHoisting.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
Expand Down Expand Up @@ -532,8 +531,9 @@ void ConstantHoistingPass::collectConstantCandidates(Function &Fn) {
// bit widths (APInt Operator- does not like that). If the value cannot be
// represented in uint64 we return an "empty" APInt. This is then interpreted
// as the value is not in range.
static Optional<APInt> calculateOffsetDiff(const APInt &V1, const APInt &V2) {
Optional<APInt> Res;
static std::optional<APInt> calculateOffsetDiff(const APInt &V1,
const APInt &V2) {
std::optional<APInt> Res;
unsigned BW = V1.getBitWidth() > V2.getBitWidth() ?
V1.getBitWidth() : V2.getBitWidth();
uint64_t LimVal1 = V1.getLimitedValue();
Expand Down Expand Up @@ -605,9 +605,8 @@ ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S,
LLVM_DEBUG(dbgs() << "Cost: " << Cost << "\n");

for (auto C2 = S; C2 != E; ++C2) {
Optional<APInt> Diff = calculateOffsetDiff(
C2->ConstInt->getValue(),
ConstCand->ConstInt->getValue());
std::optional<APInt> Diff = calculateOffsetDiff(
C2->ConstInt->getValue(), ConstCand->ConstInt->getValue());
if (Diff) {
const InstructionCost ImmCosts =
TTI->getIntImmCodeSizeCost(Opcode, OpndIdx, Diff.value(), Ty);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Expand Up @@ -1296,7 +1296,7 @@ struct DSEState {
// there is no such MemoryDef, return std::nullopt. The returned value may not
// (completely) overwrite \p KillingLoc. Currently we bail out when we
// encounter an aliasing MemoryUse (read).
Optional<MemoryAccess *>
std::optional<MemoryAccess *>
getDomMemoryDef(MemoryDef *KillingDef, MemoryAccess *StartAccess,
const MemoryLocation &KillingLoc, const Value *KillingUndObj,
unsigned &ScanLimit, unsigned &WalkerStepLimit,
Expand Down Expand Up @@ -2082,7 +2082,7 @@ static bool eliminateDeadStores(Function &F, AliasAnalysis &AA, MemorySSA &MSSA,
if (State.SkipStores.count(Current))
continue;

Optional<MemoryAccess *> MaybeDeadAccess = State.getDomMemoryDef(
std::optional<MemoryAccess *> MaybeDeadAccess = State.getDomMemoryDef(
KillingDef, Current, KillingLoc, KillingUndObj, ScanLimit,
WalkerStepLimit, IsMemTerm, PartialLimit);

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/Float2Int.cpp
Expand Up @@ -236,7 +236,7 @@ void Float2IntPass::walkBackwards() {

// Calculate result range from operand ranges.
// Return std::nullopt if the range cannot be calculated yet.
Optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
std::optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
SmallVector<ConstantRange, 4> OpRanges;
for (Value *O : I->operands()) {
if (Instruction *OI = dyn_cast<Instruction>(O)) {
Expand Down Expand Up @@ -335,7 +335,7 @@ void Float2IntPass::walkForwards() {
Instruction *I = Worklist.back();
Worklist.pop_back();

if (Optional<ConstantRange> Range = calcRange(I))
if (std::optional<ConstantRange> Range = calcRange(I))
seen(I, *Range);
else
Worklist.push_front(I); // Reprocess later.
Expand Down
11 changes: 7 additions & 4 deletions llvm/lib/Transforms/Scalar/GVNSink.cpp
Expand Up @@ -599,7 +599,7 @@ class GVNSink {
/// The main heuristic function. Analyze the set of instructions pointed to by
/// LRI and return a candidate solution if these instructions can be sunk, or
/// std::nullopt otherwise.
Optional<SinkingInstructionCandidate> analyzeInstructionForSinking(
std::optional<SinkingInstructionCandidate> analyzeInstructionForSinking(
LockstepReverseIterator &LRI, unsigned &InstNum, unsigned &MemoryInstNum,
ModelledPHISet &NeededPHIs, SmallPtrSetImpl<Value *> &PHIContents);

Expand Down Expand Up @@ -639,9 +639,12 @@ class GVNSink {
}
};

Optional<SinkingInstructionCandidate> GVNSink::analyzeInstructionForSinking(
LockstepReverseIterator &LRI, unsigned &InstNum, unsigned &MemoryInstNum,
ModelledPHISet &NeededPHIs, SmallPtrSetImpl<Value *> &PHIContents) {
std::optional<SinkingInstructionCandidate>
GVNSink::analyzeInstructionForSinking(LockstepReverseIterator &LRI,
unsigned &InstNum,
unsigned &MemoryInstNum,
ModelledPHISet &NeededPHIs,
SmallPtrSetImpl<Value *> &PHIContents) {
auto Insts = *LRI;
LLVM_DEBUG(dbgs() << " -- Analyzing instruction set: [\n"; for (auto *I
: Insts) {
Expand Down
40 changes: 20 additions & 20 deletions llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
Expand Up @@ -211,9 +211,9 @@ class InductiveRangeCheck {
/// Computes a range for the induction variable (IndVar) in which the range
/// check is redundant and can be constant-folded away. The induction
/// variable is not required to be the canonical {0,+,1} induction variable.
Optional<Range> computeSafeIterationSpace(ScalarEvolution &SE,
const SCEVAddRecExpr *IndVar,
bool IsLatchSigned) const;
std::optional<Range> computeSafeIterationSpace(ScalarEvolution &SE,
const SCEVAddRecExpr *IndVar,
bool IsLatchSigned) const;

/// Parse out a set of inductive range checks from \p BI and append them to \p
/// Checks.
Expand Down Expand Up @@ -503,8 +503,8 @@ struct LoopStructure {
return Result;
}

static Optional<LoopStructure> parseLoopStructure(ScalarEvolution &, Loop &,
const char *&);
static std::optional<LoopStructure> parseLoopStructure(ScalarEvolution &,
Loop &, const char *&);
};

/// This class is used to constrain loops to run within a given iteration space.
Expand Down Expand Up @@ -554,7 +554,7 @@ class LoopConstrainer {
// Compute a safe set of limits for the main loop to run in -- effectively the
// intersection of `Range' and the iteration space of the original loop.
// Return std::nullopt if unable to compute the set of subranges.
Optional<SubRanges> calculateSubRanges(bool IsSignedPredicate) const;
std::optional<SubRanges> calculateSubRanges(bool IsSignedPredicate) const;

// Clone `OriginalLoop' and return the result in CLResult. The IR after
// running `cloneLoop' is well formed except for the PHI nodes in CLResult --
Expand Down Expand Up @@ -747,7 +747,7 @@ static bool isSafeIncreasingBound(const SCEV *Start,
SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit));
}

Optional<LoopStructure>
std::optional<LoopStructure>
LoopStructure::parseLoopStructure(ScalarEvolution &SE, Loop &L,
const char *&FailureReason) {
if (!L.isLoopSimplifyForm()) {
Expand Down Expand Up @@ -1061,7 +1061,7 @@ static const SCEV *NoopOrExtend(const SCEV *S, Type *Ty, ScalarEvolution &SE,
return Signed ? SE.getNoopOrSignExtend(S, Ty) : SE.getNoopOrZeroExtend(S, Ty);
}

Optional<LoopConstrainer::SubRanges>
std::optional<LoopConstrainer::SubRanges>
LoopConstrainer::calculateSubRanges(bool IsSignedPredicate) const {
IntegerType *Ty = cast<IntegerType>(LatchTakenCount->getType());

Expand Down Expand Up @@ -1413,7 +1413,7 @@ bool LoopConstrainer::run() {
MainLoopPreheader = Preheader;

bool IsSignedPredicate = MainLoopStructure.IsSignedPredicate;
Optional<SubRanges> MaybeSR = calculateSubRanges(IsSignedPredicate);
std::optional<SubRanges> MaybeSR = calculateSubRanges(IsSignedPredicate);
if (!MaybeSR) {
LLVM_DEBUG(dbgs() << "irce: could not compute subranges\n");
return false;
Expand All @@ -1428,7 +1428,7 @@ bool LoopConstrainer::run() {
Instruction *InsertPt = OriginalPreheader->getTerminator();

// It would have been better to make `PreLoop' and `PostLoop'
// `Optional<ClonedLoop>'s, but `ValueToValueMapTy' does not have a copy
// `std::optional<ClonedLoop>'s, but `ValueToValueMapTy' does not have a copy
// constructor.
ClonedLoop PreLoop, PostLoop;
bool NeedsPreLoop =
Expand Down Expand Up @@ -1581,10 +1581,10 @@ bool LoopConstrainer::run() {
/// Computes and returns a range of values for the induction variable (IndVar)
/// in which the range check can be safely elided. If it cannot compute such a
/// range, returns std::nullopt.
Optional<InductiveRangeCheck::Range>
InductiveRangeCheck::computeSafeIterationSpace(
ScalarEvolution &SE, const SCEVAddRecExpr *IndVar,
bool IsLatchSigned) const {
std::optional<InductiveRangeCheck::Range>
InductiveRangeCheck::computeSafeIterationSpace(ScalarEvolution &SE,
const SCEVAddRecExpr *IndVar,
bool IsLatchSigned) const {
// We can deal when types of latch check and range checks don't match in case
// if latch check is more narrow.
auto *IVType = dyn_cast<IntegerType>(IndVar->getType());
Expand Down Expand Up @@ -1710,9 +1710,9 @@ InductiveRangeCheck::computeSafeIterationSpace(
return InductiveRangeCheck::Range(Begin, End);
}

static Optional<InductiveRangeCheck::Range>
static std::optional<InductiveRangeCheck::Range>
IntersectSignedRange(ScalarEvolution &SE,
const Optional<InductiveRangeCheck::Range> &R1,
const std::optional<InductiveRangeCheck::Range> &R1,
const InductiveRangeCheck::Range &R2) {
if (R2.isEmpty(SE, /* IsSigned */ true))
return std::nullopt;
Expand All @@ -1739,9 +1739,9 @@ IntersectSignedRange(ScalarEvolution &SE,
return Ret;
}

static Optional<InductiveRangeCheck::Range>
static std::optional<InductiveRangeCheck::Range>
IntersectUnsignedRange(ScalarEvolution &SE,
const Optional<InductiveRangeCheck::Range> &R1,
const std::optional<InductiveRangeCheck::Range> &R1,
const InductiveRangeCheck::Range &R2) {
if (R2.isEmpty(SE, /* IsSigned */ false))
return std::nullopt;
Expand Down Expand Up @@ -1928,7 +1928,7 @@ bool InductiveRangeCheckElimination::run(
PrintRecognizedRangeChecks(errs());

const char *FailureReason = nullptr;
Optional<LoopStructure> MaybeLoopStructure =
std::optional<LoopStructure> MaybeLoopStructure =
LoopStructure::parseLoopStructure(SE, *L, FailureReason);
if (!MaybeLoopStructure) {
LLVM_DEBUG(dbgs() << "irce: could not parse loop structure: "
Expand All @@ -1941,7 +1941,7 @@ bool InductiveRangeCheckElimination::run(
const SCEVAddRecExpr *IndVar =
cast<SCEVAddRecExpr>(SE.getMinusSCEV(SE.getSCEV(LS.IndVarBase), SE.getSCEV(LS.IndVarStep)));

Optional<InductiveRangeCheck::Range> SafeIterRange;
std::optional<InductiveRangeCheck::Range> SafeIterRange;
Instruction *ExprInsertPt = Preheader->getTerminator();

SmallVector<InductiveRangeCheck, 4> RangeChecksToEliminate;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/LoopDistribute.cpp
Expand Up @@ -892,7 +892,7 @@ class LoopDistributeForLoop {
/// If the optional has a value, it indicates whether distribution was forced
/// to be enabled (true) or disabled (false). If the optional has no value
/// distribution was not forced either way.
const Optional<bool> &isForced() const { return IsForced; }
const std::optional<bool> &isForced() const { return IsForced; }

private:
/// Filter out checks between pointers from the same partition.
Expand Down Expand Up @@ -963,7 +963,7 @@ class LoopDistributeForLoop {
/// If the optional has a value, it indicates whether distribution was forced
/// to be enabled (true) or disabled (false). If the optional has no value
/// distribution was not forced either way.
Optional<bool> IsForced;
std::optional<bool> IsForced;
};

} // end anonymous namespace
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/Scalar/LoopFuse.cpp
Expand Up @@ -701,7 +701,7 @@ struct LoopFuser {
/// have the same TripCount. The second is the difference in the two
/// TripCounts. This information can be used later to determine whether or not
/// peeling can be performed on either one of the candidates.
std::pair<bool, Optional<unsigned>>
std::pair<bool, std::optional<unsigned>>
haveIdenticalTripCounts(const FusionCandidate &FC0,
const FusionCandidate &FC1) const {
const SCEV *TripCount0 = SE.getBackedgeTakenCount(FC0.L);
Expand Down Expand Up @@ -743,7 +743,7 @@ struct LoopFuser {
return {false, std::nullopt};
}

Optional<unsigned> Difference;
std::optional<unsigned> Difference;
int Diff = TC0 - TC1;

if (Diff > 0)
Expand Down Expand Up @@ -860,10 +860,10 @@ struct LoopFuser {
// the loops (second value of pair). The difference is not equal to
// None iff the loops iterate a constant number of times, and have a
// single exit.
std::pair<bool, Optional<unsigned>> IdenticalTripCountRes =
std::pair<bool, std::optional<unsigned>> IdenticalTripCountRes =
haveIdenticalTripCounts(*FC0, *FC1);
bool SameTripCount = IdenticalTripCountRes.first;
Optional<unsigned> TCDifference = IdenticalTripCountRes.second;
std::optional<unsigned> TCDifference = IdenticalTripCountRes.second;

// Here we are checking that FC0 (the first loop) can be peeled, and
// both loops have different tripcounts.
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Scalar/LoopPassManager.cpp
Expand Up @@ -87,7 +87,7 @@ LoopPassManager::runWithLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
Loop *OuterMostLoop = &L;

for (size_t I = 0, E = IsLoopNestPass.size(); I != E; ++I) {
Optional<PreservedAnalyses> PassPA;
std::optional<PreservedAnalyses> PassPA;
if (!IsLoopNestPass[I]) {
// The `I`-th pass is a loop pass.
auto &Pass = LoopPasses[LoopPassIndex++];
Expand Down Expand Up @@ -157,7 +157,8 @@ LoopPassManager::runWithoutLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
// instrumenting callbacks for the passes later.
PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(L, AR);
for (auto &Pass : LoopPasses) {
Optional<PreservedAnalyses> PassPA = runSinglePass(L, Pass, AM, AR, U, PI);
std::optional<PreservedAnalyses> PassPA =
runSinglePass(L, Pass, AM, AR, U, PI);

// `PassPA` is `None` means that the before-pass callbacks in
// `PassInstrumentation` return false. The pass does not run in this case,
Expand Down

0 comments on commit 3152156

Please sign in to comment.