diff --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h --- a/llvm/include/llvm/Support/Alignment.h +++ b/llvm/include/llvm/Support/Alignment.h @@ -21,9 +21,9 @@ #ifndef LLVM_SUPPORT_ALIGNMENT_H_ #define LLVM_SUPPORT_ALIGNMENT_H_ -#include "llvm/ADT/Optional.h" #include "llvm/Support/MathExtras.h" #include +#include #ifndef NDEBUG #include #endif // NDEBUG @@ -93,13 +93,13 @@ } /// Allow constructions of constexpr Align. - template constexpr static LogValue Constant() { + template constexpr static Align Constant() { return LogValue{static_cast(CTLog2())}; } /// Allow constructions of constexpr Align from types. /// Compile time equivalent to Align(alignof(T)). - template constexpr static LogValue Of() { + template constexpr static Align Of() { return Constant::value>(); } @@ -114,9 +114,9 @@ /// This struct is a compact representation of a valid (power of two) or /// undefined (0) alignment. -struct MaybeAlign : public llvm::Optional { +struct MaybeAlign : public std::optional { private: - using UP = llvm::Optional; + using UP = std::optional; public: /// Default is undefined. @@ -128,9 +128,8 @@ MaybeAlign(MaybeAlign &&Other) = default; MaybeAlign &operator=(MaybeAlign &&Other) = default; - /// Use llvm::Optional constructor. - using UP::UP; - + constexpr MaybeAlign(std::nullopt_t None) : UP(None) {} + constexpr MaybeAlign(Align Value) : UP(Value) {} explicit MaybeAlign(uint64_t Value) { assert((Value == 0 || llvm::isPowerOf2_64(Value)) && "Alignment is neither 0 nor a power of 2"); @@ -292,6 +291,22 @@ bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; +// Allow equality comparisons between Align and MaybeAlign. +inline bool operator==(MaybeAlign Lhs, Align Rhs) { return Lhs && *Lhs == Rhs; } +inline bool operator!=(MaybeAlign Lhs, Align Rhs) { return !(Lhs == Rhs); } +inline bool operator==(Align Lhs, MaybeAlign Rhs) { return Rhs == Lhs; } +inline bool operator!=(Align Lhs, MaybeAlign Rhs) { return !(Rhs == Lhs); } +// Allow equality comparisons with MaybeAlign. +inline bool operator==(MaybeAlign Lhs, MaybeAlign Rhs) { + return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs); +} +inline bool operator!=(MaybeAlign Lhs, MaybeAlign Rhs) { return !(Lhs == Rhs); } +// Allow equality comparisons with std::nullopt. +inline bool operator==(MaybeAlign Lhs, std::nullopt_t) { return !bool(Lhs); } +inline bool operator!=(MaybeAlign Lhs, std::nullopt_t) { return bool(Lhs); } +inline bool operator==(std::nullopt_t, MaybeAlign Rhs) { return !bool(Rhs); } +inline bool operator!=(std::nullopt_t, MaybeAlign Rhs) { return bool(Rhs); } + #ifndef NDEBUG // For usage in LLVM_DEBUG macros. inline std::string DebugStr(const Align &A) { diff --git a/llvm/lib/Support/OptimizedStructLayout.cpp b/llvm/lib/Support/OptimizedStructLayout.cpp --- a/llvm/lib/Support/OptimizedStructLayout.cpp +++ b/llvm/lib/Support/OptimizedStructLayout.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/OptimizedStructLayout.h" +#include "llvm/ADT/Optional.h" using namespace llvm; diff --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp --- a/llvm/unittests/Support/AlignmentTest.cpp +++ b/llvm/unittests/Support/AlignmentTest.cpp @@ -235,7 +235,7 @@ std::vector getNonPowerOfTwo() { return {3, 10, 15}; } TEST(AlignmentDeathTest, CantConvertUnsetMaybe) { - EXPECT_DEATH((*MaybeAlign(0)), ".*"); + EXPECT_DEATH((MaybeAlign(0).value()), ".*"); } TEST(AlignmentDeathTest, InvalidCTors) {