diff mbox series

reassoc: Handle OFFSET_TYPE like POINTER_TYPE in optimize_range_tests_cmp_bitwise [PR107029[

Message ID YzFjMj+hcggRdc8F@tucnak
State New
Headers show
Series reassoc: Handle OFFSET_TYPE like POINTER_TYPE in optimize_range_tests_cmp_bitwise [PR107029[ | expand

Commit Message

Jakub Jelinek Sept. 26, 2022, 8:30 a.m. UTC
Hi!

As the testcase shows, OFFSET_TYPE needs the same treatment as
POINTER_TYPE/REFERENCE_TYPE, otherwise we fail the same during the
newly added verification.  OFFSET_TYPE is signed though, so unlike
POINTER_TYPE/REFERENCE_TYPE it can also trigger with the
x < 0 && y < 0 && z < 0 to (x | y | z) < 0
optimization.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
ok for trunk?

2022-09-26  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/107029
	* tree-ssa-reassoc.cc (optimize_range_tests_cmp_bitwise): Treat
	OFFSET_TYPE like POINTER_TYPE, except that OFFSET_TYPE may be
	signed and so can trigger even the (b % 4) == 3 case.

	* g++.dg/torture/pr107029.C: New test.


	Jakub

Comments

Richard Biener Sept. 26, 2022, 10:58 a.m. UTC | #1
On Mon, 26 Sep 2022, Jakub Jelinek wrote:

> Hi!
> 
> As the testcase shows, OFFSET_TYPE needs the same treatment as
> POINTER_TYPE/REFERENCE_TYPE, otherwise we fail the same during the
> newly added verification.  OFFSET_TYPE is signed though, so unlike
> POINTER_TYPE/REFERENCE_TYPE it can also trigger with the
> x < 0 && y < 0 && z < 0 to (x | y | z) < 0
> optimization.
> 
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux,
> ok for trunk?

OK.

> 2022-09-26  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/107029
> 	* tree-ssa-reassoc.cc (optimize_range_tests_cmp_bitwise): Treat
> 	OFFSET_TYPE like POINTER_TYPE, except that OFFSET_TYPE may be
> 	signed and so can trigger even the (b % 4) == 3 case.
> 
> 	* g++.dg/torture/pr107029.C: New test.
> 
> --- gcc/tree-ssa-reassoc.cc.jj	2022-09-17 08:18:16.935880254 +0200
> +++ gcc/tree-ssa-reassoc.cc	2022-09-25 14:46:21.746367580 +0200
> @@ -3608,13 +3608,13 @@ optimize_range_tests_cmp_bitwise (enum t
>  	tree type2 = NULL_TREE;
>  	bool strict_overflow_p = false;
>  	candidates.truncate (0);
> -	if (POINTER_TYPE_P (type1))
> +	if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
>  	  type1 = pointer_sized_int_node;
>  	for (j = i; j; j = chains[j - 1])
>  	  {
>  	    tree type = TREE_TYPE (ranges[j - 1].exp);
>  	    strict_overflow_p |= ranges[j - 1].strict_overflow_p;
> -	    if (POINTER_TYPE_P (type))
> +	    if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
>  	      type = pointer_sized_int_node;
>  	    if ((b % 4) == 3)
>  	      {
> @@ -3646,7 +3646,7 @@ optimize_range_tests_cmp_bitwise (enum t
>  	    tree type = TREE_TYPE (ranges[j - 1].exp);
>  	    if (j == k)
>  	      continue;
> -	    if (POINTER_TYPE_P (type))
> +	    if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
>  	      type = pointer_sized_int_node;
>  	    if ((b % 4) == 3)
>  	      {
> @@ -3677,10 +3677,20 @@ optimize_range_tests_cmp_bitwise (enum t
>  		op = r->exp;
>  		continue;
>  	      }
> -	    if (id == l || POINTER_TYPE_P (TREE_TYPE (op)))
> +	    if (id == l
> +		|| POINTER_TYPE_P (TREE_TYPE (op))
> +		|| TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
>  	      {
>  		code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR;
>  		tree type3 = id >= l ? type1 : pointer_sized_int_node;
> +		if (code == BIT_NOT_EXPR
> +		    && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
> +		  {
> +		    g = gimple_build_assign (make_ssa_name (type3),
> +					     NOP_EXPR, op);
> +		    gimple_seq_add_stmt_without_update (&seq, g);
> +		    op = gimple_assign_lhs (g);
> +		  }
>  		g = gimple_build_assign (make_ssa_name (type3), code, op);
>  		gimple_seq_add_stmt_without_update (&seq, g);
>  		op = gimple_assign_lhs (g);
> @@ -3688,6 +3698,7 @@ optimize_range_tests_cmp_bitwise (enum t
>  	    tree type = TREE_TYPE (r->exp);
>  	    tree exp = r->exp;
>  	    if (POINTER_TYPE_P (type)
> +		|| TREE_CODE (type) == OFFSET_TYPE
>  		|| (id >= l && !useless_type_conversion_p (type1, type)))
>  	      {
>  		tree type3 = id >= l ? type1 : pointer_sized_int_node;
> @@ -3705,7 +3716,7 @@ optimize_range_tests_cmp_bitwise (enum t
>  	    op = gimple_assign_lhs (g);
>  	  }
>  	type1 = TREE_TYPE (ranges[k - 1].exp);
> -	if (POINTER_TYPE_P (type1))
> +	if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
>  	  {
>  	    gimple *g
>  	      = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op);
> --- gcc/testsuite/g++.dg/torture/pr107029.C.jj	2022-09-25 14:49:18.427954682 +0200
> +++ gcc/testsuite/g++.dg/torture/pr107029.C	2022-09-25 14:49:00.654197418 +0200
> @@ -0,0 +1,19 @@
> +// PR tree-optimization/107029
> +// { dg-do compile }
> +
> +struct S { long long a; int b; };
> +long long S::*a;
> +int S::*b;
> +struct A { void foo (bool, bool); void bar (); int c; };
> +
> +void
> +A::foo (bool a, bool b)
> +{
> +  c = a || b;
> +}
> +
> +void
> +A::bar()
> +{
> +  foo (a, b);
> +}
> 
> 	Jakub
> 
>
diff mbox series

Patch

--- gcc/tree-ssa-reassoc.cc.jj	2022-09-17 08:18:16.935880254 +0200
+++ gcc/tree-ssa-reassoc.cc	2022-09-25 14:46:21.746367580 +0200
@@ -3608,13 +3608,13 @@  optimize_range_tests_cmp_bitwise (enum t
 	tree type2 = NULL_TREE;
 	bool strict_overflow_p = false;
 	candidates.truncate (0);
-	if (POINTER_TYPE_P (type1))
+	if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
 	  type1 = pointer_sized_int_node;
 	for (j = i; j; j = chains[j - 1])
 	  {
 	    tree type = TREE_TYPE (ranges[j - 1].exp);
 	    strict_overflow_p |= ranges[j - 1].strict_overflow_p;
-	    if (POINTER_TYPE_P (type))
+	    if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
 	      type = pointer_sized_int_node;
 	    if ((b % 4) == 3)
 	      {
@@ -3646,7 +3646,7 @@  optimize_range_tests_cmp_bitwise (enum t
 	    tree type = TREE_TYPE (ranges[j - 1].exp);
 	    if (j == k)
 	      continue;
-	    if (POINTER_TYPE_P (type))
+	    if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
 	      type = pointer_sized_int_node;
 	    if ((b % 4) == 3)
 	      {
@@ -3677,10 +3677,20 @@  optimize_range_tests_cmp_bitwise (enum t
 		op = r->exp;
 		continue;
 	      }
-	    if (id == l || POINTER_TYPE_P (TREE_TYPE (op)))
+	    if (id == l
+		|| POINTER_TYPE_P (TREE_TYPE (op))
+		|| TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
 	      {
 		code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR;
 		tree type3 = id >= l ? type1 : pointer_sized_int_node;
+		if (code == BIT_NOT_EXPR
+		    && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
+		  {
+		    g = gimple_build_assign (make_ssa_name (type3),
+					     NOP_EXPR, op);
+		    gimple_seq_add_stmt_without_update (&seq, g);
+		    op = gimple_assign_lhs (g);
+		  }
 		g = gimple_build_assign (make_ssa_name (type3), code, op);
 		gimple_seq_add_stmt_without_update (&seq, g);
 		op = gimple_assign_lhs (g);
@@ -3688,6 +3698,7 @@  optimize_range_tests_cmp_bitwise (enum t
 	    tree type = TREE_TYPE (r->exp);
 	    tree exp = r->exp;
 	    if (POINTER_TYPE_P (type)
+		|| TREE_CODE (type) == OFFSET_TYPE
 		|| (id >= l && !useless_type_conversion_p (type1, type)))
 	      {
 		tree type3 = id >= l ? type1 : pointer_sized_int_node;
@@ -3705,7 +3716,7 @@  optimize_range_tests_cmp_bitwise (enum t
 	    op = gimple_assign_lhs (g);
 	  }
 	type1 = TREE_TYPE (ranges[k - 1].exp);
-	if (POINTER_TYPE_P (type1))
+	if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
 	  {
 	    gimple *g
 	      = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op);
--- gcc/testsuite/g++.dg/torture/pr107029.C.jj	2022-09-25 14:49:18.427954682 +0200
+++ gcc/testsuite/g++.dg/torture/pr107029.C	2022-09-25 14:49:00.654197418 +0200
@@ -0,0 +1,19 @@ 
+// PR tree-optimization/107029
+// { dg-do compile }
+
+struct S { long long a; int b; };
+long long S::*a;
+int S::*b;
+struct A { void foo (bool, bool); void bar (); int c; };
+
+void
+A::foo (bool a, bool b)
+{
+  c = a || b;
+}
+
+void
+A::bar()
+{
+  foo (a, b);
+}