Convert XOR constant operand to outer_type in ifcombine
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap |
fail
|
Patch failed to apply
|
Commit Message
When decode_field_reference decodes an equality test of the form
(outer_type)(a ^ c) == 0 into a == c, it strips the conversion
outer_type on the first pass (decoding the left-hand operand 'a')
and stores outer_type for field sizing. On the second pass
(*pxorp == true, decoding the right-hand operand 'c'), if 'c' is an
INTEGER_CST, it was left with its original unconverted type and width.
This meant that if 'c' was wider than outer_type (e.g. a 16-bit constant
cast to uint8_t), 'c' retained non-zero bits in the upper positions.
In fold_truth_andor_for_ifcombine, because both operands' bitsizes had
been adjusted to outer_type, rl_bitsize < rr_bitsize was false, leaving
the constant unclipped. The subsequent check for non-zero bits outside
the field mask then erroneously concluded that the comparison was
tautologically false and optimized the condition to 0.
Fix this by converting exp to outer_type when decoding the right-hand
XOR operand if outer_type is set and exp is an INTEGER_CST.
Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
Richard.
PR tree-optimization/127447
PR tree-optimization/125444
PR tree-optimization/124937
* gimple-fold.cc (decode_field_reference): Convert exp to
outer_type when decoding an INTEGER_CST right-hand XOR operand.
* gcc.dg/field-merge-28.c: New test.
---
gcc/gimple-fold.cc | 2 ++
gcc/testsuite/gcc.dg/field-merge-28.c | 47 +++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/field-merge-28.c
@@ -8005,6 +8005,8 @@ decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
{
exp = res_ops[1];
gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
+ if (outer_type && TREE_CODE (exp) == INTEGER_CST)
+ exp = fold_convert (outer_type, exp);
}
else if (!pxor_cmp_op)
/* Not much we can do when xor appears in the right-hand compare
new file mode 100644
@@ -0,0 +1,47 @@
+/* { dg-do run } */
+/* { dg-options "-O1" } */
+
+unsigned short b1, b2;
+int c1 = 0, c2 = 0;
+
+__attribute__((noipa)) void m1 () { c1++; }
+__attribute__((noipa)) void m2 () { c2++; }
+
+__attribute__((noipa))
+void test1 ()
+{
+ for (int i = 0; i < 9; i++)
+ {
+ unsigned short val = (b1 ^= 65533);
+ unsigned char k = val;
+ if ((val & 240) || k)
+ ;
+ else
+ m1 ();
+ }
+}
+
+__attribute__((noipa))
+void test2 ()
+{
+ for (int i = 0; i < 9; i++)
+ {
+ unsigned short val = (b2 ^= 65533);
+ signed char k = val;
+ if ((val & 240) || k)
+ ;
+ else
+ m2 ();
+ }
+}
+
+int main ()
+{
+ test1 ();
+ if (c1 != 4)
+ __builtin_abort ();
+ test2 ();
+ if (c2 != 4)
+ __builtin_abort ();
+ return 0;
+}