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.
/* 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
--- /dev/null
+/* { 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 } } */