https://gcc.gnu.org/g:44c27171c36a9128da970fb35d4790de42a82b4d
commit r16-4735-g44c27171c36a9128da970fb35d4790de42a82b4d Author: Artemiy Volkov <[email protected]> Date: Thu Oct 30 07:42:03 2025 -0600 [PATCH][PR tree-optimization/122478] match.pd: fix simplify pattern for view_convert (BIT_FIELD_REF) The pattern introduced in r16-4682-g5eafa8d16be873 couldn't handle conversion from <unnamed-unsigned:1> to unsigned char, which ended up causing a gimple checking failure reported in PR122478. This patch fixes the pattern by prohibiting widening integral conversions in addition to the narrowing ones, or equivalently, requiring that the converted-to and converted-from types of the VCE both have precision equal to their size. Since type_has_mode_precision_p () does not apply to vector types, filter them out by adding a !INTEGRAL_TYPE_P () check on TREE_TYPE (@0). Bootstrapped and regtested on aarch64 and x86_64, regtested on i386 and riscv64, one GIMPLE test added. PR tree-optimization/122478 gcc/ChangeLog: * match.pd: Fix the view_convert (BIT_FIELD_REF) pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr122478.c: New test. Diff: --- gcc/match.pd | 6 ++++-- gcc/testsuite/gcc.dg/tree-ssa/pr122478.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 00493d6ad993..b65adece8dc6 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5658,10 +5658,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Squash view_converts of BFRs if no precision is lost. */ (simplify - (view_convert (BIT_FIELD_REF @1 @2 @3)) + (view_convert (BIT_FIELD_REF@0 @1 @2 @3)) (if (is_gimple_reg_type (type) && (!INTEGRAL_TYPE_P (type) - || type_has_mode_precision_p (type))) + || !INTEGRAL_TYPE_P (TREE_TYPE (@0)) + || (type_has_mode_precision_p (type) + && type_has_mode_precision_p (TREE_TYPE (@0))))) (BIT_FIELD_REF:type @1 @2 @3))) /* For integral conversions with the same precision or pointer diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr122478.c b/gcc/testsuite/gcc.dg/tree-ssa/pr122478.c new file mode 100644 index 000000000000..bc63f71274a9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr122478.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-additional-options "-fgimple" } */ + +unsigned char __GIMPLE (ssa) +foo (unsigned short mask__701) +{ + _Bool _19; + unsigned char _180; + +__BB(2): + _19 = __BIT_FIELD_REF <_Bool> (mask__701, 1, 12); + _180 = __VIEW_CONVERT<unsigned char>(_19); + return _180; +} + +/* { dg-final { scan-tree-dump-times "VIEW_CONVERT_EXPR" "optimized" 1 } } */
