[clang] [clang][Interp] Implement __builtin_bitreverse (PR #71687)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 16 04:47:45 PST 2023


================
@@ -59,13 +59,54 @@ static void pushInt(InterpState &S, int32_t Val) {
     llvm_unreachable("Int isn't 16 or 32 bit?");
 }
 
-static bool retInt(InterpState &S, CodePtr OpPC, APValue &Result) {
-  PrimType IntType = getIntPrimType(S);
-  if (IntType == PT_Sint32)
-    return Ret<PT_Sint32>(S, OpPC, Result);
-  else if (IntType == PT_Sint16)
-    return Ret<PT_Sint16>(S, OpPC, Result);
-  llvm_unreachable("Int isn't 16 or 32 bit?");
+static void pushAPSInt(InterpState &S, const APSInt &Val) {
+  bool Signed = Val.isSigned();
+
+  if (Signed) {
+    switch (Val.getBitWidth()) {
+    case 64:
+      S.Stk.push<Integral<64, true>>(
+          Integral<64, true>::from(Val.getSExtValue()));
+      break;
+    case 32:
+      S.Stk.push<Integral<32, true>>(
+          Integral<32, true>::from(Val.getSExtValue()));
+      break;
+    case 16:
+      S.Stk.push<Integral<16, true>>(
+          Integral<16, true>::from(Val.getSExtValue()));
+      break;
+    case 8:
+      S.Stk.push<Integral<8, true>>(
+          Integral<8, true>::from(Val.getSExtValue()));
+      break;
+    default:
+      llvm_unreachable("Invalid integer bitwidth");
----------------
AaronBallman wrote:

Yeah, that's the situation I'm worried about (not really specific to bitreverse as that only has hardcoded bit widths). However, now that I look at builtins.def more closely, most of the ones taking variadic arguments are doing so because they have custom type-checking that often disallows non-power-of-two-types. So I think we're fine for now, probably.

https://github.com/llvm/llvm-project/pull/71687


More information about the cfe-commits mailing list