This patch adds a match.pd transformation to strip unnecessary
view_converts of BIT_FIELD_REFs.  This should work for all combinations of
types except where loss of precision is involved, i.e. in narrowing
conversions between integer types; this is 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.

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                                |  8 ++++++++
 gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c | 12 ++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c

diff --git a/gcc/match.pd b/gcc/match.pd
index a4248a521cf..660f4ecb3cb 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5624,6 +5624,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@0 @1 @2 @3))
+    (if (!INTEGRAL_TYPE_P (type)
+        || !INTEGRAL_TYPE_P (TREE_TYPE (@0))
+        || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
+    (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
index 00000000000..5785e51c089
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c
@@ -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" } } */
-- 
2.43.0

Reply via email to