Skip to content

Commit 2ceac91

Browse files
committed
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
1 parent ec06b38 commit 2ceac91

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

clang/docs/LanguageExtensions.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,8 @@ the bits in the first argument by the amount in the second argument.
20162016
For example, ``0b10000110`` rotated left by 11 becomes ``0b00110100``.
20172017
The shift value is treated as an unsigned amount modulo the size of
20182018
the arguments. Both arguments and the result have the bitwidth specified
2019-
by the name of the builtin.
2019+
by the name of the builtin. These builtins can be used within constant
2020+
expressions.
20202021
20212022
``__builtin_rotateright``
20222023
-------------------------
@@ -2048,7 +2049,8 @@ the bits in the first argument by the amount in the second argument.
20482049
For example, ``0b10000110`` rotated right by 3 becomes ``0b11010000``.
20492050
The shift value is treated as an unsigned amount modulo the size of
20502051
the arguments. Both arguments and the result have the bitwidth specified
2051-
by the name of the builtin.
2052+
by the name of the builtin. These builtins can be used within constant
2053+
expressions.
20522054
20532055
``__builtin_unreachable``
20542056
-------------------------

clang/docs/ReleaseNotes.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ Improvements to Clang's diagnostics
5656
Non-comprehensive list of changes in this release
5757
-------------------------------------------------
5858

59-
- ...
59+
- The builtin intrinsics ``__builtin_rotateleft8``, ``__builtin_rotateleft16``,
60+
``__builtin_rotateleft32`` and ``__builtin_rotateleft64`` may now be used
61+
within constant expressions.
62+
63+
- The builtin intrinsics ``__builtin_rotateright8``, ``__builtin_rotateright16``,
64+
``__builtin_rotateright32`` and ``__builtin_rotateright64`` may now be used
65+
within constant expressions.
6066

6167
New Compiler Flags
6268
------------------

clang/lib/AST/ExprConstant.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -11347,6 +11347,30 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1134711347
return Success(Val.countPopulation(), E);
1134811348
}
1134911349

11350+
case Builtin::BI__builtin_rotateleft8:
11351+
case Builtin::BI__builtin_rotateleft16:
11352+
case Builtin::BI__builtin_rotateleft32:
11353+
case Builtin::BI__builtin_rotateleft64: {
11354+
APSInt Val, Amt;
11355+
if (!EvaluateInteger(E->getArg(0), Val, Info) ||
11356+
!EvaluateInteger(E->getArg(1), Amt, Info))
11357+
return false;
11358+
11359+
return Success(Val.rotl(Amt.urem(Val.getBitWidth())), E);
11360+
}
11361+
11362+
case Builtin::BI__builtin_rotateright8:
11363+
case Builtin::BI__builtin_rotateright16:
11364+
case Builtin::BI__builtin_rotateright32:
11365+
case Builtin::BI__builtin_rotateright64: {
11366+
APSInt Val, Amt;
11367+
if (!EvaluateInteger(E->getArg(0), Val, Info) ||
11368+
!EvaluateInteger(E->getArg(1), Amt, Info))
11369+
return false;
11370+
11371+
return Success(Val.rotr(Amt.urem(Val.getBitWidth())), E);
11372+
}
11373+
1135011374
case Builtin::BIstrlen:
1135111375
case Builtin::BIwcslen:
1135211376
// A call to strlen is not a constant expression.

clang/test/Sema/constant-builtins-2.c

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ char parity8[__builtin_parity(~0) == 0 ? 1 : -1];
169169
char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1];
170170
char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1];
171171

172+
char rotateleft1[__builtin_rotateleft8(0x01, 5) == 0x20 ? 1 : -1];
173+
char rotateleft2[__builtin_rotateleft16(0x3210, 11) == 0x8190 ? 1 : -1];
174+
char rotateleft2[__builtin_rotateleft32(0x76543210, 22) == 0x841D950C ? 1 : -1];
175+
char rotateleft2[__builtin_rotateleft64(0xFEDCBA9876543210ULL, 55) == 0x87F6E5D4C3B2A19ULL ? 1 : -1];
176+
177+
char rotateright1[__builtin_rotateright8(0x01, 5) == 0x08 ? 1 : -1];
178+
char rotateright2[__builtin_rotateright16(0x3210, 11) == 0x4206 ? 1 : -1];
179+
char rotateright2[__builtin_rotateright32(0x76543210, 22) == 0x50C841D9 ? 1 : -1];
180+
char rotateright2[__builtin_rotateright64(0xFEDCBA9876543210ULL, 55) == 0xB97530ECA86421FDULL ? 1 : -1];
181+
172182
char ffs1[__builtin_ffs(0) == 0 ? 1 : -1];
173183
char ffs2[__builtin_ffs(1) == 1 ? 1 : -1];
174184
char ffs3[__builtin_ffs(0xfbe71) == 1 ? 1 : -1];

0 commit comments

Comments
 (0)