diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -56,7 +56,9 @@ Non-comprehensive list of changes in this release ------------------------------------------------- -- ... +- The builtin intrinsics ``__builtin_bitreverse8``, ``__builtin_bitreverse``, + ``__builtin_bitreverse32`` and ``__builtin_bitreverse64`` may now be used + within constexpr expressions. New Compiler Flags ------------------ diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11180,6 +11180,17 @@ return Success(AlignedVal, E); } + case Builtin::BI__builtin_bitreverse8: + case Builtin::BI__builtin_bitreverse16: + case Builtin::BI__builtin_bitreverse32: + case Builtin::BI__builtin_bitreverse64: { + APSInt Val; + if (!EvaluateInteger(E->getArg(0), Val, Info)) + return false; + + return Success(Val.reverseBits(), E); + } + case Builtin::BI__builtin_bswap16: case Builtin::BI__builtin_bswap32: case Builtin::BI__builtin_bswap64: { diff --git a/clang/test/Sema/constant-builtins-2.c b/clang/test/Sema/constant-builtins-2.c --- a/clang/test/Sema/constant-builtins-2.c +++ b/clang/test/Sema/constant-builtins-2.c @@ -169,6 +169,11 @@ char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1]; char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1]; +char bitreverse1[__builtin_bitreverse8(0x01) == 0x80 ? 1 : -1]; +char bitreverse2[__builtin_bitreverse16(0x3C48) == 0x123C ? 1 : -1]; +char bitreverse3[__builtin_bitreverse32(0x12345678) == 0x1E6A2C48 ? 1 : -1]; +char bitreverse4[__builtin_bitreverse64(0x0123456789ABCDEFULL) == 0xF7B3D591E6A2C480 ? 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];