[clang] 2ceac91 - Enable constexpr on ROTATELEFT/ROTATERIGHT builtin intrinsics (PR47249)

Simon Pilgrim via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 22 06:44:10 PDT 2020


Author: Simon Pilgrim
Date: 2020-08-22T14:43:21+01:00
New Revision: 2ceac91ec0fc186ff503402e7b88e4576a69008d

URL: https://github.com/llvm/llvm-project/commit/2ceac91ec0fc186ff503402e7b88e4576a69008d
DIFF: https://github.com/llvm/llvm-project/commit/2ceac91ec0fc186ff503402e7b88e4576a69008d.diff

LOG: Enable constexpr on ROTATELEFT/ROTATERIGHT builtin intrinsics (PR47249)

This enables us to use the __builtin_rotateleft / __builtin_rotateright 8/16/32/64 intrinsics inside constexpr code.

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

Added: 
    

Modified: 
    clang/docs/LanguageExtensions.rst
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/ExprConstant.cpp
    clang/test/Sema/constant-builtins-2.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 518ab6385263..fd563b0d59ba 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -2016,7 +2016,8 @@ the bits in the first argument by the amount in the second argument.
 For example, ``0b10000110`` rotated left by 11 becomes ``0b00110100``.
 The shift value is treated as an unsigned amount modulo the size of
 the arguments. Both arguments and the result have the bitwidth specified
-by the name of the builtin.
+by the name of the builtin. These builtins can be used within constant
+expressions.
 
 ``__builtin_rotateright``
 -------------------------
@@ -2048,7 +2049,8 @@ the bits in the first argument by the amount in the second argument.
 For example, ``0b10000110`` rotated right by 3 becomes ``0b11010000``.
 The shift value is treated as an unsigned amount modulo the size of
 the arguments. Both arguments and the result have the bitwidth specified
-by the name of the builtin.
+by the name of the builtin. These builtins can be used within constant
+expressions.
 
 ``__builtin_unreachable``
 -------------------------

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b01408bba575..7a94039f9a26 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -56,7 +56,13 @@ Improvements to Clang's diagnostics
 Non-comprehensive list of changes in this release
 -------------------------------------------------
 
-- ...
+- The builtin intrinsics ``__builtin_rotateleft8``, ``__builtin_rotateleft16``,
+  ``__builtin_rotateleft32`` and ``__builtin_rotateleft64`` may now be used
+  within constant expressions.
+
+- The builtin intrinsics ``__builtin_rotateright8``, ``__builtin_rotateright16``,
+  ``__builtin_rotateright32`` and ``__builtin_rotateright64`` may now be used
+  within constant expressions.
 
 New Compiler Flags
 ------------------

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index b22797b5c3c0..09ef5629182a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11347,6 +11347,30 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
     return Success(Val.countPopulation(), E);
   }
 
+  case Builtin::BI__builtin_rotateleft8:
+  case Builtin::BI__builtin_rotateleft16:
+  case Builtin::BI__builtin_rotateleft32:
+  case Builtin::BI__builtin_rotateleft64: {
+    APSInt Val, Amt;
+    if (!EvaluateInteger(E->getArg(0), Val, Info) ||
+        !EvaluateInteger(E->getArg(1), Amt, Info))
+      return false;
+
+    return Success(Val.rotl(Amt.urem(Val.getBitWidth())), E);
+  }
+
+  case Builtin::BI__builtin_rotateright8:
+  case Builtin::BI__builtin_rotateright16:
+  case Builtin::BI__builtin_rotateright32:
+  case Builtin::BI__builtin_rotateright64: {
+    APSInt Val, Amt;
+    if (!EvaluateInteger(E->getArg(0), Val, Info) ||
+        !EvaluateInteger(E->getArg(1), Amt, Info))
+      return false;
+
+    return Success(Val.rotr(Amt.urem(Val.getBitWidth())), E);
+  }
+
   case Builtin::BIstrlen:
   case Builtin::BIwcslen:
     // A call to strlen is not a constant expression.

diff  --git a/clang/test/Sema/constant-builtins-2.c b/clang/test/Sema/constant-builtins-2.c
index 9bb5215b8dbd..b0f566e9eecf 100644
--- a/clang/test/Sema/constant-builtins-2.c
+++ b/clang/test/Sema/constant-builtins-2.c
@@ -169,6 +169,16 @@ char parity8[__builtin_parity(~0) == 0 ? 1 : -1];
 char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1];
 char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1];
 
+char rotateleft1[__builtin_rotateleft8(0x01, 5) == 0x20 ? 1 : -1];
+char rotateleft2[__builtin_rotateleft16(0x3210, 11) == 0x8190 ? 1 : -1];
+char rotateleft2[__builtin_rotateleft32(0x76543210, 22) == 0x841D950C ? 1 : -1];
+char rotateleft2[__builtin_rotateleft64(0xFEDCBA9876543210ULL, 55) == 0x87F6E5D4C3B2A19ULL ? 1 : -1];
+
+char rotateright1[__builtin_rotateright8(0x01, 5) == 0x08 ? 1 : -1];
+char rotateright2[__builtin_rotateright16(0x3210, 11) == 0x4206 ? 1 : -1];
+char rotateright2[__builtin_rotateright32(0x76543210, 22) == 0x50C841D9 ? 1 : -1];
+char rotateright2[__builtin_rotateright64(0xFEDCBA9876543210ULL, 55) == 0xB97530ECA86421FDULL ? 1 : -1];
+
 char ffs1[__builtin_ffs(0) == 0 ? 1 : -1];
 char ffs2[__builtin_ffs(1) == 1 ? 1 : -1];
 char ffs3[__builtin_ffs(0xfbe71) == 1 ? 1 : -1];


        


More information about the cfe-commits mailing list