diff mbox series

ranger: Fix up REALPART_EXPR/IMAGPART_EXPR handling [PR104604]

Message ID YhO6ZgEZnPvzhHk2@tucnak
State New
Headers show
Series ranger: Fix up REALPART_EXPR/IMAGPART_EXPR handling [PR104604] | expand

Commit Message

Jakub Jelinek Feb. 21, 2022, 4:14 p.m. UTC
Hi!

The following testcase is miscompiled since r12-3328.
That change assumed that if rhs1 of a GIMPLE_ASSIGN is COMPLEX_CST, then
that is the value of the lhs of the stmt, but that is not the case always,
only if it is a GIMPLE_SINGLE_RHS stmt.  If it is e.g.
GIMPLE_UNARY_RHS or GIMPLE_BINARY_RHS (the latter happens in the testcase),
then it can be e.g.
__complex__ (3, 0) / var
and the REALPART_EXPR of that isn't 3, but the realpart of the division.
I assume once the ranger can do complex numbers adjust_*part_expr will just
fetch one or the other range from a underlying complex range, but until
then, we should limit this to what r12-3328 meant to do.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-02-21  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/104604
	* gimple-range-fold.cc (adjust_imagpart_expr, adjust_realpart_expr):
	Only check if gimple_assign_rhs1 is COMPLEX_CST if
	gimple_assign_rhs_code is COMPLEX_CST.

	* gcc.c-torture/execute/pr104604.c: New test.


	Jakub

Comments

Richard Biener Feb. 21, 2022, 5:20 p.m. UTC | #1
> Am 21.02.2022 um 17:15 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>:
> 
> Hi!
> 
> The following testcase is miscompiled since r12-3328.
> That change assumed that if rhs1 of a GIMPLE_ASSIGN is COMPLEX_CST, then
> that is the value of the lhs of the stmt, but that is not the case always,
> only if it is a GIMPLE_SINGLE_RHS stmt.  If it is e.g.
> GIMPLE_UNARY_RHS or GIMPLE_BINARY_RHS (the latter happens in the testcase),
> then it can be e.g.
> __complex__ (3, 0) / var
> and the REALPART_EXPR of that isn't 3, but the realpart of the division.
> I assume once the ranger can do complex numbers adjust_*part_expr will just
> fetch one or the other range from a underlying complex range, but until
> then, we should limit this to what r12-3328 meant to do.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok

> 2022-02-21  Jakub Jelinek  <jakub@redhat.com>
> 
>    PR tree-optimization/104604
>    * gimple-range-fold.cc (adjust_imagpart_expr, adjust_realpart_expr):
>    Only check if gimple_assign_rhs1 is COMPLEX_CST if
>    gimple_assign_rhs_code is COMPLEX_CST.
> 
>    * gcc.c-torture/execute/pr104604.c: New test.
> 
> --- gcc/gimple-range-fold.cc.jj    2022-02-16 09:19:15.741557607 +0100
> +++ gcc/gimple-range-fold.cc    2022-02-21 13:50:56.524481955 +0100
> @@ -397,7 +397,8 @@ adjust_imagpart_expr (irange &res, const
>    }
>       return;
>     }
> -  if (is_gimple_assign (def_stmt))
> +  if (is_gimple_assign (def_stmt)
> +      && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
>     {
>       tree cst = gimple_assign_rhs1 (def_stmt);
>       if (TREE_CODE (cst) == COMPLEX_CST)
> @@ -422,7 +423,8 @@ adjust_realpart_expr (irange &res, const
>   if (!SSA_NAME_DEF_STMT (name))
>     return;
> 
> -  if (is_gimple_assign (def_stmt))
> +  if (is_gimple_assign (def_stmt)
> +      && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
>     {
>       tree cst = gimple_assign_rhs1 (def_stmt);
>       if (TREE_CODE (cst) == COMPLEX_CST)
> --- gcc/testsuite/gcc.c-torture/execute/pr104604.c.jj    2022-02-21 14:02:01.190303071 +0100
> +++ gcc/testsuite/gcc.c-torture/execute/pr104604.c    2022-02-21 14:01:25.941789588 +0100
> @@ -0,0 +1,34 @@
> +/* PR tree-optimization/104604 */
> +
> +unsigned char g;
> +
> +__attribute__((noipa))
> +unsigned char
> +foo (_Complex unsigned c)
> +{
> +  unsigned char v = g;
> +  _Complex unsigned t = 3;
> +  t /= c;
> +  return v + t;
> +}
> +
> +__attribute__((noipa))
> +unsigned char
> +bar (_Complex unsigned c)
> +{
> +  unsigned char v = g;
> +  _Complex unsigned t = 42;
> +  t /= c;
> +  return v + t;
> +}
> +
> +int
> +main ()
> +{
> +  unsigned char x = foo (7);
> +  if (x)
> +    __builtin_abort ();
> +  if (bar (7) != 6)
> +    __builtin_abort ();
> +  return 0;
> +}
> 
>    Jakub
>
diff mbox series

Patch

--- gcc/gimple-range-fold.cc.jj	2022-02-16 09:19:15.741557607 +0100
+++ gcc/gimple-range-fold.cc	2022-02-21 13:50:56.524481955 +0100
@@ -397,7 +397,8 @@  adjust_imagpart_expr (irange &res, const
 	}
       return;
     }
-  if (is_gimple_assign (def_stmt))
+  if (is_gimple_assign (def_stmt)
+      && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
     {
       tree cst = gimple_assign_rhs1 (def_stmt);
       if (TREE_CODE (cst) == COMPLEX_CST)
@@ -422,7 +423,8 @@  adjust_realpart_expr (irange &res, const
   if (!SSA_NAME_DEF_STMT (name))
     return;
 
-  if (is_gimple_assign (def_stmt))
+  if (is_gimple_assign (def_stmt)
+      && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
     {
       tree cst = gimple_assign_rhs1 (def_stmt);
       if (TREE_CODE (cst) == COMPLEX_CST)
--- gcc/testsuite/gcc.c-torture/execute/pr104604.c.jj	2022-02-21 14:02:01.190303071 +0100
+++ gcc/testsuite/gcc.c-torture/execute/pr104604.c	2022-02-21 14:01:25.941789588 +0100
@@ -0,0 +1,34 @@ 
+/* PR tree-optimization/104604 */
+
+unsigned char g;
+
+__attribute__((noipa))
+unsigned char
+foo (_Complex unsigned c)
+{
+  unsigned char v = g;
+  _Complex unsigned t = 3;
+  t /= c;
+  return v + t;
+}
+
+__attribute__((noipa))
+unsigned char
+bar (_Complex unsigned c)
+{
+  unsigned char v = g;
+  _Complex unsigned t = 42;
+  t /= c;
+  return v + t;
+}
+
+int
+main ()
+{
+  unsigned char x = foo (7);
+  if (x)
+    __builtin_abort ();
+  if (bar (7) != 6)
+    __builtin_abort ();
+  return 0;
+}