middle-end/106070 - bogus cond-expr folding

The following fixes up r13-469-g9a53101caadae1b5 by properly
implementing what operand_equal_for_comparison_p did.

2022-06-24  Richard Biener  <rguenther@suse.de>

	PR middle-end/106070
	* match.pd (a != b ? a : b): Fix translation of
	operand_equal_for_comparison_p.

	* gcc.dg/torture/pr106070.c: New testcase.
This commit is contained in:
Richard Biener 2022-06-24 13:37:22 +02:00
parent fa4e97907f
commit b36a1c964f
2 changed files with 27 additions and 4 deletions

View file

@ -4574,12 +4574,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
(convert3? @0) (convert4? @1))
(if (!HONOR_SIGNED_ZEROS (type)
&& ((INTEGRAL_TYPE_P (type)
/* Allow widening conversions of the data. */
&& (/* Allow widening conversions of the compare operands as data. */
(INTEGRAL_TYPE_P (type)
&& types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
&& types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
&& TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
&& TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
|| (tree_nop_conversion_p (type, TREE_TYPE (@0))
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))))
/* Or sign conversions for the comparison. */
|| (types_match (type, TREE_TYPE (@0))
&& types_match (type, TREE_TYPE (@1)))))
(switch
(if (cmp == EQ_EXPR)
(if (VECTOR_TYPE_P (type))

View file

@ -0,0 +1,20 @@
/* { dg-do run } */
unsigned int var_2 = 1;
int var_4 = -1;
int var_10 = 4;
unsigned long arr_252;
void __attribute__((noipa)) test() {
for (int a = 0; a < var_10; a += 2)
arr_252 = var_2 != (int)var_4 ? (unsigned long)var_4 : (unsigned long)var_2;
}
void test();
int main()
{
test();
if (arr_252 != 0xffffffffffffffff)
__builtin_abort();
return 0;
}