Skip to content

Commit e7d9182

Browse files
committed
Enable constexpr on BITREVERSE builtin intrinsics (PR47249)
This enables us to use the __builtin_bitreverse 8/16/32/64 intrinsics inside constexpr code. Differential Revision: https://reviews.llvm.org/D86339
1 parent 2ceac91 commit e7d9182

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ Query for this feature with ``__has_builtin(__builtin_convertvector)``.
19841984
19851985
The '``__builtin_bitreverse``' family of builtins is used to reverse
19861986
the bitpattern of an integer value; for example ``0b10110110`` becomes
1987-
``0b01101101``.
1987+
``0b01101101``. These builtins can be used within constant expressions.
19881988
19891989
``__builtin_rotateleft``
19901990
------------------------

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Improvements to Clang's diagnostics
5656
Non-comprehensive list of changes in this release
5757
-------------------------------------------------
5858

59+
- The builtin intrinsics ``__builtin_bitreverse8``, ``__builtin_bitreverse16``,
60+
``__builtin_bitreverse32`` and ``__builtin_bitreverse64`` may now be used
61+
within constant expressions.
62+
5963
- The builtin intrinsics ``__builtin_rotateleft8``, ``__builtin_rotateleft16``,
6064
``__builtin_rotateleft32`` and ``__builtin_rotateleft64`` may now be used
6165
within constant expressions.

clang/lib/AST/ExprConstant.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11180,6 +11180,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1118011180
return Success(AlignedVal, E);
1118111181
}
1118211182

11183+
case Builtin::BI__builtin_bitreverse8:
11184+
case Builtin::BI__builtin_bitreverse16:
11185+
case Builtin::BI__builtin_bitreverse32:
11186+
case Builtin::BI__builtin_bitreverse64: {
11187+
APSInt Val;
11188+
if (!EvaluateInteger(E->getArg(0), Val, Info))
11189+
return false;
11190+
11191+
return Success(Val.reverseBits(), E);
11192+
}
11193+
1118311194
case Builtin::BI__builtin_bswap16:
1118411195
case Builtin::BI__builtin_bswap32:
1118511196
case Builtin::BI__builtin_bswap64: {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ 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 bitreverse1[__builtin_bitreverse8(0x01) == 0x80 ? 1 : -1];
173+
char bitreverse2[__builtin_bitreverse16(0x3C48) == 0x123C ? 1 : -1];
174+
char bitreverse3[__builtin_bitreverse32(0x12345678) == 0x1E6A2C48 ? 1 : -1];
175+
char bitreverse4[__builtin_bitreverse64(0x0123456789ABCDEFULL) == 0xF7B3D591E6A2C480 ? 1 : -1];
176+
172177
char rotateleft1[__builtin_rotateleft8(0x01, 5) == 0x20 ? 1 : -1];
173178
char rotateleft2[__builtin_rotateleft16(0x3210, 11) == 0x8190 ? 1 : -1];
174179
char rotateleft2[__builtin_rotateleft32(0x76543210, 22) == 0x841D950C ? 1 : -1];

0 commit comments

Comments
 (0)