]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: simplify (view_convert (BIT_FIELD_REF))
authorArtemiy Volkov <artemiy.volkov@arm.com>
Mon, 27 Oct 2025 14:47:31 +0000 (14:47 +0000)
committerRichard Earnshaw <rearnsha@arm.com>
Tue, 28 Oct 2025 10:01:37 +0000 (10:01 +0000)
This patch adds a match.pd transformation to strip unnecessary
view_converts of BIT_FIELD_REFs.  This is only valid when the type of the
view_convert is a register type, and no loss of precision is involved when
casting to an integer type; both of these conditions are checked for in
the if clause of the new pattern.

The change survives bootstrap + regtest on aarch64 and x86_64, and one
additional test case has been added to gcc.dg/tree-ssa.

Changes since v1:
- Require is_gimple_reg_type (type) and remove conditions on @0
- Reword the commit message to reflect code changes

gcc/ChangeLog:

* match.pd: Add pattern to simplify view_convert (BIT_FIELD_REF).

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/forwprop-42.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c [new file with mode: 0644]

index eff70ad9bb4f78066028576dd406dca8683a07be..00493d6ad993ed3276f120a14c043a5e3ae0a493 100644 (file)
@@ -5656,6 +5656,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (view_convert (view_convert @0))
   (view_convert @0))
 
+/* Squash view_converts of BFRs if no precision is lost.  */
+(simplify
+  (view_convert (BIT_FIELD_REF @1 @2 @3))
+  (if (is_gimple_reg_type (type)
+       && (!INTEGRAL_TYPE_P (type)
+          || type_has_mode_precision_p (type)))
+   (BIT_FIELD_REF:type @1 @2 @3)))
+
 /* For integral conversions with the same precision or pointer
    conversions use a NOP_EXPR instead.  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c
new file mode 100644 (file)
index 0000000..5785e51
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1" } */
+
+typedef unsigned int vec2 __attribute__ ((vector_size (2 * sizeof (unsigned int))));
+typedef unsigned int vec1 __attribute__ ((vector_size (sizeof (unsigned int))));
+
+vec1 foo (vec2 x)
+{
+       return (vec1) x[1];
+}
+
+/* { dg-final { scan-tree-dump-not "VIEW_CONVERT_EXPR" "forwprop1" } } */