[llvm] [AArch64] G_BITCAST should not change bitwidths (PR #81031)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 11:46:28 PST 2024


https://github.com/AtariDreams created https://github.com/llvm/llvm-project/pull/81031

G_BITCAST is not allowed to change the bitwidths, but it has for far too long.

There does not seem to be any tests that rely on this behavior anymore, so it seems right to make this change now.

>From 14a06757f3b7b29796355b2623b51579ddf81684 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Wed, 7 Feb 2024 14:22:44 -0500
Subject: [PATCH] [AArch64] G_BITCAST should not change bitwidths

G_BITCAST is not allowed to change the bitwidths, but it has for far too long.

There does not seem to be any tests that rely on this behavior anymore, so it seems right to make this change now.
---
 .../AArch64/GISel/AArch64LegalizerInfo.cpp    | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
index cbf5655706e694..b4e6323dcf34c1 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
@@ -743,13 +743,18 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
 
   // Casts for 32 and 64-bit width type are just copies.
   // Same for 128-bit width type, except they are on the FPR bank.
-  getActionDefinitionsBuilder(G_BITCAST)
-      // FIXME: This is wrong since G_BITCAST is not allowed to change the
-      // number of bits but it's what the previous code described and fixing
-      // it breaks tests.
-      .legalForCartesianProduct({s8, s16, s32, s64, s128, v16s8, v8s8, v4s8,
-                                 v8s16, v4s16, v2s16, v4s32, v2s32, v2s64,
-                                 v2p0});
+  getActionDefinitionsBuilder(G_BITCAST).legalIf(
+      [=](const LegalityQuery &Query) {
+        const LLT &DstTy = Query.Types[0];
+        const LLT &SrcTy = Query.Types[1];
+        // Bitcast needs to stick to the same bit-width
+        if (DstTy.getSizeInBits() != SrcTy.getSizeInBits())
+          return false;
+        return llvm::is_contained({s8, s16, s32, s64, s128, v16s8, v8s8, v4s8,
+                                   v8s16, v4s16, v2s16, v4s32, v2s32, v2s64,
+                                   v2p0},
+                                  DstTy);
+      });
 
   getActionDefinitionsBuilder(G_VASTART).legalFor({p0});
 



More information about the llvm-commits mailing list