FPEnv: convert Optional to std::optional

This commit is contained in:
Krzysztof Parzyszek 2022-12-03 13:54:35 -06:00
parent 059a23c0f0
commit ab672e9173
10 changed files with 42 additions and 33 deletions

View File

@ -16,8 +16,8 @@
#define LLVM_IR_FPENV_H
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/ADT/Optional.h"
#include "llvm/IR/FMF.h"
#include <optional>
namespace llvm {
class StringRef;
@ -46,19 +46,19 @@ enum ExceptionBehavior : uint8_t {
/// Returns a valid RoundingMode enumerator when given a string
/// that is valid as input in constrained intrinsic rounding mode
/// metadata.
Optional<RoundingMode> convertStrToRoundingMode(StringRef);
std::optional<RoundingMode> convertStrToRoundingMode(StringRef);
/// For any RoundingMode enumerator, returns a string valid as input in
/// constrained intrinsic rounding mode metadata.
Optional<StringRef> convertRoundingModeToStr(RoundingMode);
std::optional<StringRef> convertRoundingModeToStr(RoundingMode);
/// Returns a valid ExceptionBehavior enumerator when given a string
/// valid as input in constrained intrinsic exception behavior metadata.
Optional<fp::ExceptionBehavior> convertStrToExceptionBehavior(StringRef);
std::optional<fp::ExceptionBehavior> convertStrToExceptionBehavior(StringRef);
/// For any ExceptionBehavior enumerator, returns a string valid as
/// input in constrained intrinsic exception behavior metadata.
Optional<StringRef> convertExceptionBehaviorToStr(fp::ExceptionBehavior);
std::optional<StringRef> convertExceptionBehaviorToStr(fp::ExceptionBehavior);
/// Returns true if the exception handling behavior and rounding mode
/// match what is used in the default floating point environment.

View File

@ -46,6 +46,7 @@
#include <cassert>
#include <cstdint>
#include <functional>
#include <optional>
#include <utility>
namespace llvm {
@ -307,7 +308,8 @@ public:
/// Set the exception handling to be used with constrained floating point
void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
#ifndef NDEBUG
Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(NewExcept);
std::optional<StringRef> ExceptStr =
convertExceptionBehaviorToStr(NewExcept);
assert(ExceptStr && "Garbage strict exception behavior!");
#endif
DefaultConstrainedExcept = NewExcept;
@ -316,7 +318,8 @@ public:
/// Set the rounding mode handling to be used with constrained floating point
void setDefaultConstrainedRounding(RoundingMode NewRounding) {
#ifndef NDEBUG
Optional<StringRef> RoundingStr = convertRoundingModeToStr(NewRounding);
std::optional<StringRef> RoundingStr =
convertRoundingModeToStr(NewRounding);
assert(RoundingStr && "Garbage strict rounding mode!");
#endif
DefaultConstrainedRounding = NewRounding;
@ -1198,7 +1201,8 @@ private:
if (Rounding)
UseRounding = Rounding.value();
Optional<StringRef> RoundingStr = convertRoundingModeToStr(UseRounding);
std::optional<StringRef> RoundingStr =
convertRoundingModeToStr(UseRounding);
assert(RoundingStr && "Garbage strict rounding mode!");
auto *RoundingMDS = MDString::get(Context, RoundingStr.value());
@ -1211,7 +1215,8 @@ private:
if (Except)
UseExcept = Except.value();
Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(UseExcept);
std::optional<StringRef> ExceptStr =
convertExceptionBehaviorToStr(UseExcept);
assert(ExceptStr && "Garbage strict exception behavior!");
auto *ExceptMDS = MDString::get(Context, ExceptStr.value());

View File

@ -35,6 +35,7 @@
#include "llvm/Support/Casting.h"
#include <cassert>
#include <cstdint>
#include <optional>
namespace llvm {
@ -594,8 +595,8 @@ class ConstrainedFPIntrinsic : public IntrinsicInst {
public:
bool isUnaryOp() const;
bool isTernaryOp() const;
Optional<RoundingMode> getRoundingMode() const;
Optional<fp::ExceptionBehavior> getExceptionBehavior() const;
std::optional<RoundingMode> getRoundingMode() const;
std::optional<fp::ExceptionBehavior> getExceptionBehavior() const;
bool isDefaultFPEnvironment() const;
// Methods for support type inquiry through isa, cast, and dyn_cast:

View File

@ -1906,8 +1906,8 @@ static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
/// \param St Exception flags raised during constant evaluation.
static bool mayFoldConstrained(ConstrainedFPIntrinsic *CI,
APFloat::opStatus St) {
Optional<RoundingMode> ORM = CI->getRoundingMode();
Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
std::optional<RoundingMode> ORM = CI->getRoundingMode();
std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
// If the operation does not change exception status flags, it is safe
// to fold.
@ -1932,7 +1932,7 @@ static bool mayFoldConstrained(ConstrainedFPIntrinsic *CI,
/// Returns the rounding mode that should be used for constant evaluation.
static RoundingMode
getEvaluationRoundingMode(const ConstrainedFPIntrinsic *CI) {
Optional<RoundingMode> ORM = CI->getRoundingMode();
std::optional<RoundingMode> ORM = CI->getRoundingMode();
if (!ORM || *ORM == RoundingMode::Dynamic)
// Even if the rounding mode is unknown, try evaluating the operation.
// If it does not raise inexact exception, rounding was not applied,
@ -2133,7 +2133,7 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
// Rounding operations (floor, trunc, ceil, round and nearbyint) do not
// raise FP exceptions, unless the argument is signaling NaN.
Optional<APFloat::roundingMode> RM;
std::optional<APFloat::roundingMode> RM;
switch (IntrinsicID) {
default:
break;
@ -2164,12 +2164,12 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
APFloat::opStatus St = U.roundToIntegral(*RM);
if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
St == APFloat::opInexact) {
Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
if (EB && *EB == fp::ebStrict)
return nullptr;
}
} else if (U.isSignaling()) {
Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
if (EB && *EB != fp::ebIgnore)
return nullptr;
U = APFloat::getQNaN(U.getSemantics());

View File

@ -84,6 +84,7 @@
#include <cassert>
#include <cstdint>
#include <iterator>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@ -2304,7 +2305,7 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
// Convert the metadata argument to a constant integer
Metadata *MD = cast<MetadataAsValue>(CI.getArgOperand(1))->getMetadata();
Optional<RoundingMode> RoundMode =
std::optional<RoundingMode> RoundMode =
convertStrToRoundingMode(cast<MDString>(MD)->getString());
// Add the Rounding mode as an integer

View File

@ -6398,7 +6398,7 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
// Get the last argument, the metadata and convert it to an integer in the
// call
Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
Optional<RoundingMode> RoundMode =
std::optional<RoundingMode> RoundMode =
convertStrToRoundingMode(cast<MDString>(MD)->getString());
EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());

View File

@ -17,13 +17,14 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include <optional>
namespace llvm {
Optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
std::optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
// For dynamic rounding mode, we use round to nearest but we will set the
// 'exact' SDNodeFlag so that the value will not be rounded.
return StringSwitch<Optional<RoundingMode>>(RoundingArg)
return StringSwitch<std::optional<RoundingMode>>(RoundingArg)
.Case("round.dynamic", RoundingMode::Dynamic)
.Case("round.tonearest", RoundingMode::NearestTiesToEven)
.Case("round.tonearestaway", RoundingMode::NearestTiesToAway)
@ -33,8 +34,8 @@ Optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
.Default(std::nullopt);
}
Optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
Optional<StringRef> RoundingStr;
std::optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
std::optional<StringRef> RoundingStr;
switch (UseRounding) {
case RoundingMode::Dynamic:
RoundingStr = "round.dynamic";
@ -60,18 +61,18 @@ Optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
return RoundingStr;
}
Optional<fp::ExceptionBehavior>
std::optional<fp::ExceptionBehavior>
convertStrToExceptionBehavior(StringRef ExceptionArg) {
return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg)
return StringSwitch<std::optional<fp::ExceptionBehavior>>(ExceptionArg)
.Case("fpexcept.ignore", fp::ebIgnore)
.Case("fpexcept.maytrap", fp::ebMayTrap)
.Case("fpexcept.strict", fp::ebStrict)
.Default(std::nullopt);
}
Optional<StringRef>
std::optional<StringRef>
convertExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
Optional<StringRef> ExceptStr;
std::optional<StringRef> ExceptStr;
switch (UseExcept) {
case fp::ebStrict:
ExceptStr = "fpexcept.strict";

View File

@ -275,7 +275,7 @@ Value *InstrProfIncrementInst::getStep() const {
return ConstantInt::get(Type::getInt64Ty(Context), 1);
}
Optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
unsigned NumOperands = arg_size();
Metadata *MD = nullptr;
auto *MAV = dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2));
@ -286,7 +286,7 @@ Optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
return convertStrToRoundingMode(cast<MDString>(MD)->getString());
}
Optional<fp::ExceptionBehavior>
std::optional<fp::ExceptionBehavior>
ConstrainedFPIntrinsic::getExceptionBehavior() const {
unsigned NumOperands = arg_size();
Metadata *MD = nullptr;
@ -299,13 +299,13 @@ ConstrainedFPIntrinsic::getExceptionBehavior() const {
}
bool ConstrainedFPIntrinsic::isDefaultFPEnvironment() const {
Optional<fp::ExceptionBehavior> Except = getExceptionBehavior();
std::optional<fp::ExceptionBehavior> Except = getExceptionBehavior();
if (Except) {
if (Except.value() != fp::ebIgnore)
return false;
}
Optional<RoundingMode> Rounding = getRoundingMode();
std::optional<RoundingMode> Rounding = getRoundingMode();
if (Rounding) {
if (Rounding.value() != RoundingMode::NearestTiesToEven)
return false;

View File

@ -5025,7 +5025,7 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
" (the operand should be a string)"),
MD);
Optional<RoundingMode> RoundMode =
std::optional<RoundingMode> RoundMode =
convertStrToRoundingMode(cast<MDString>(MD)->getString());
Check(RoundMode && *RoundMode != RoundingMode::Dynamic,
"unsupported rounding mode argument", Call);

View File

@ -489,7 +489,8 @@ bool llvm::wouldInstructionBeTriviallyDead(Instruction *I,
}
if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I)) {
Optional<fp::ExceptionBehavior> ExBehavior = FPI->getExceptionBehavior();
std::optional<fp::ExceptionBehavior> ExBehavior =
FPI->getExceptionBehavior();
return *ExBehavior != fp::ebStrict;
}
}