[llvm] 161bddf - [ADT] Make BitmaskEnum operations constant expressions

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 11 02:12:48 PST 2022


Author: Pavel Labath
Date: 2022-03-11T11:11:55+01:00
New Revision: 161bddf3af09aa883c9414d793de79c89e51636a

URL: https://github.com/llvm/llvm-project/commit/161bddf3af09aa883c9414d793de79c89e51636a
DIFF: https://github.com/llvm/llvm-project/commit/161bddf3af09aa883c9414d793de79c89e51636a.diff

LOG: [ADT] Make BitmaskEnum operations constant expressions

This avoids runtime initialization (a global constructor) whenever they appear
in the initializer.

The patch just adds the constexpr keyword to a couple of functions.

Differential Revision: https://reviews.llvm.org/D121281

Added: 
    

Modified: 
    llvm/include/llvm/ADT/BitmaskEnum.h
    llvm/include/llvm/Support/MathExtras.h
    llvm/unittests/ADT/BitmaskEnumTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/BitmaskEnum.h b/llvm/include/llvm/ADT/BitmaskEnum.h
index 89e5508e08e16..205da1240d44e 100644
--- a/llvm/include/llvm/ADT/BitmaskEnum.h
+++ b/llvm/include/llvm/ADT/BitmaskEnum.h
@@ -77,7 +77,7 @@ namespace BitmaskEnumDetail {
 
 /// Get a bitmask with 1s in all places up to the high-order bit of E's largest
 /// value.
-template <typename E> std::underlying_type_t<E> Mask() {
+template <typename E> constexpr std::underlying_type_t<E> Mask() {
   // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
   // subtracting 1 gives us the mask with all bits set, like we want.
   return NextPowerOf2(static_cast<std::underlying_type_t<E>>(
@@ -87,7 +87,7 @@ template <typename E> std::underlying_type_t<E> Mask() {
 
 /// Check that Val is in range for E, and return Val cast to E's underlying
 /// type.
-template <typename E> std::underlying_type_t<E> Underlying(E Val) {
+template <typename E> constexpr std::underlying_type_t<E> Underlying(E Val) {
   auto U = static_cast<std::underlying_type_t<E>>(Val);
   assert(U >= 0 && "Negative enum values are not allowed.");
   assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
@@ -99,22 +99,22 @@ constexpr unsigned bitWidth(uint64_t Value) {
 }
 
 template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator~(E Val) {
+constexpr E operator~(E Val) {
   return static_cast<E>(~Underlying(Val) & Mask<E>());
 }
 
 template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator|(E LHS, E RHS) {
+constexpr E operator|(E LHS, E RHS) {
   return static_cast<E>(Underlying(LHS) | Underlying(RHS));
 }
 
 template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator&(E LHS, E RHS) {
+constexpr E operator&(E LHS, E RHS) {
   return static_cast<E>(Underlying(LHS) & Underlying(RHS));
 }
 
 template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
-E operator^(E LHS, E RHS) {
+constexpr E operator^(E LHS, E RHS) {
   return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
 }
 

diff  --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 604d709ba0058..8c2f2af1c49e2 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -707,7 +707,7 @@ constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
 
 /// Returns the next power of two (in 64-bits) that is strictly greater than A.
 /// Returns zero on overflow.
-inline uint64_t NextPowerOf2(uint64_t A) {
+constexpr inline uint64_t NextPowerOf2(uint64_t A) {
   A |= (A >> 1);
   A |= (A >> 2);
   A |= (A >> 4);

diff  --git a/llvm/unittests/ADT/BitmaskEnumTest.cpp b/llvm/unittests/ADT/BitmaskEnumTest.cpp
index f07e20330aec2..266a3f6310a31 100644
--- a/llvm/unittests/ADT/BitmaskEnumTest.cpp
+++ b/llvm/unittests/ADT/BitmaskEnumTest.cpp
@@ -76,6 +76,17 @@ TEST(BitmaskEnumTest, BitwiseXorEquals) {
   EXPECT_EQ(F3, f);
 }
 
+TEST(BitmaskEnumTest, ConstantExpression) {
+  constexpr Flags f1 = ~F1;
+  constexpr Flags f2 = F1 | F2;
+  constexpr Flags f3 = F1 & F2;
+  constexpr Flags f4 = F1 ^ F2;
+  EXPECT_EQ(f1, ~F1);
+  EXPECT_EQ(f2, F1 | F2);
+  EXPECT_EQ(f3, F1 & F2);
+  EXPECT_EQ(f4, F1 ^ F2);
+}
+
 TEST(BitmaskEnumTest, BitwiseNot) {
   Flags f = ~F1;
   EXPECT_EQ(14, f); // Largest value for f is 15.


        


More information about the llvm-commits mailing list