On 10/23/25 5:50 AM, Richard Biener wrote:
On Thu, 23 Oct 2025, Artemiy Volkov wrote:

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)))

I think you want to ensure that you do not fold in size changes.
See tree-cfg.cc:verify_types_in_gimple_reference.  The view_convert
could convert to a variably-sized type.

I'm concerned there are many corner cases you can't think of.   So
instead of a negative list (aka !INTEGRAL_TYPE_P) I'm looking for
a positive-list we are sure will work.  I'd suggest to use
is_gimple_reg_type.  Also require type_has_mode_precision_p
on integral types.
Just an FYI. I went down this path in a prior life. There's certainly value in squashing out the useless V_C_E nodes. My recollection was it simplified the match.pd/forwprop patterns we were writing to improve extracting elements out of a vector. Sadly, never upstreamed and never saw the light of day.

Point being it may not help much in isolation, but it can simplify further work which obviously has value.
jeff

Reply via email to