diff mbox

Fix PR64126

Message ID alpine.LSU.2.11.1412011400000.5894@zhemvz.fhfr.qr
State New
Headers show

Commit Message

Richard Biener Dec. 1, 2014, 1 p.m. UTC
The following fixes PR64126.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.

Richard.

2014-12-01  Richard Biener  <rguenther@suse.de>

	PR middle-end/64126
	* match.pd: Allow conversions in ~A + 1 -> -A, add -A - 1 -> ~A
	and -1 - A -> ~A.
	* fold-const.c (fold_binary_loc): Remove transforms here.

Comments

Marc Glisse Dec. 1, 2014, 9:20 p.m. UTC | #1
On Mon, 1 Dec 2014, Richard Biener wrote:

> The following fixes PR64126.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.
>
> Richard.
>
> 2014-12-01  Richard Biener  <rguenther@suse.de>
>
> 	PR middle-end/64126
> 	* match.pd: Allow conversions in ~A + 1 -> -A, add -A - 1 -> ~A
> 	and -1 - A -> ~A.
> 	* fold-const.c (fold_binary_loc): Remove transforms here.
>
> Index: gcc/match.pd
> ===================================================================
> --- gcc/match.pd	(revision 218144)
> +++ gcc/match.pd	(working copy)
> @@ -484,8 +522,22 @@ (define_operator_list inverted_tcc_compa
>
>   /* ~A + 1 -> -A */
>   (simplify
> -   (plus (bit_not @0) integer_each_onep)
> -   (negate @0))
> +   (plus (convert? (bit_not @0)) integer_each_onep)
> +   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
> +    (negate (convert @0))))
> +
> +  /* -A - 1 -> ~A */
> +  (simplify
> +   (minus (convert? (negate @0)) integer_each_onep)
> +   (if (!TYPE_OVERFLOW_TRAPS (type)

I don't understand why TYPE_OVERFLOW_TRAPS is tested for this one but not 
the others.

> +	&& tree_nop_conversion_p (type, TREE_TYPE (@0)))
> +    (bit_not (convert @0))))
> +
> +  /* -1 - A -> ~A */
> +  (simplify
> +   (minus integer_all_onesp @0)
> +   (if (TREE_CODE (type) != COMPLEX_TYPE)
> +    (bit_not @0)))

It should also be true for COMPLEX_TYPE where integer_all_onesp tests for 
-1-i.

(I know you are just copying from fold-const)
Richard Biener Dec. 2, 2014, 8:44 a.m. UTC | #2
On Mon, 1 Dec 2014, Marc Glisse wrote:

> On Mon, 1 Dec 2014, Richard Biener wrote:
> 
> > The following fixes PR64126.
> > 
> > Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.
> > 
> > Richard.
> > 
> > 2014-12-01  Richard Biener  <rguenther@suse.de>
> > 
> > 	PR middle-end/64126
> > 	* match.pd: Allow conversions in ~A + 1 -> -A, add -A - 1 -> ~A
> > 	and -1 - A -> ~A.
> > 	* fold-const.c (fold_binary_loc): Remove transforms here.
> > 
> > Index: gcc/match.pd
> > ===================================================================
> > --- gcc/match.pd	(revision 218144)
> > +++ gcc/match.pd	(working copy)
> > @@ -484,8 +522,22 @@ (define_operator_list inverted_tcc_compa
> > 
> >   /* ~A + 1 -> -A */
> >   (simplify
> > -   (plus (bit_not @0) integer_each_onep)
> > -   (negate @0))
> > +   (plus (convert? (bit_not @0)) integer_each_onep)
> > +   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
> > +    (negate (convert @0))))
> > +
> > +  /* -A - 1 -> ~A */
> > +  (simplify
> > +   (minus (convert? (negate @0)) integer_each_onep)
> > +   (if (!TYPE_OVERFLOW_TRAPS (type)
> 
> I don't understand why TYPE_OVERFLOW_TRAPS is tested for this one but not the
> others.

No idea - I just copied from fold-const.c.  The whole -ftrapv and
now -fsanitize=overflow stuff is just very inconsistently checked.

> > +	&& tree_nop_conversion_p (type, TREE_TYPE (@0)))
> > +    (bit_not (convert @0))))
> > +
> > +  /* -1 - A -> ~A */
> > +  (simplify
> > +   (minus integer_all_onesp @0)
> > +   (if (TREE_CODE (type) != COMPLEX_TYPE)
> > +    (bit_not @0)))
> 
> It should also be true for COMPLEX_TYPE where integer_all_onesp tests for
> -1-i.

Yeah, I have no idea why it was disabled for complex.  Maybe expansion
doesn't deal with ~complex properly (on some targets?).  Not sure.

Didn't bother to investigate as ...

> (I know you are just copying from fold-const)

... I was just copying from fold-const.c ;)

Richard.
diff mbox

Patch

Index: gcc/match.pd
===================================================================
--- gcc/match.pd	(revision 218144)
+++ gcc/match.pd	(working copy)
@@ -484,8 +522,22 @@  (define_operator_list inverted_tcc_compa
 
   /* ~A + 1 -> -A */
   (simplify
-   (plus (bit_not @0) integer_each_onep)
-   (negate @0))
+   (plus (convert? (bit_not @0)) integer_each_onep)
+   (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
+    (negate (convert @0))))
+
+  /* -A - 1 -> ~A */
+  (simplify
+   (minus (convert? (negate @0)) integer_each_onep)
+   (if (!TYPE_OVERFLOW_TRAPS (type)
+	&& tree_nop_conversion_p (type, TREE_TYPE (@0)))
+    (bit_not (convert @0))))
+
+  /* -1 - A -> ~A */
+  (simplify
+   (minus integer_all_onesp @0)
+   (if (TREE_CODE (type) != COMPLEX_TYPE)
+    (bit_not @0)))
 
   /* (T)(P + A) - (T)P -> (T) A */
   (for add (plus pointer_plus)
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c	(revision 218144)
+++ gcc/fold-const.c	(working copy)
@@ -10391,19 +10475,6 @@  fold_binary_loc (location_t loc,
 					      negate_expr (arg1)),
 			    fold_convert_loc (loc, type,
 					      TREE_OPERAND (arg0, 0)));
-      /* Convert -A - 1 to ~A.  */
-      if (TREE_CODE (arg0) == NEGATE_EXPR
-	  && integer_each_onep (arg1)
-	  && !TYPE_OVERFLOW_TRAPS (type))
-	return fold_build1_loc (loc, BIT_NOT_EXPR, type,
-			    fold_convert_loc (loc, type,
-					      TREE_OPERAND (arg0, 0)));
-
-      /* Convert -1 - A to ~A.  */
-      if (TREE_CODE (type) != COMPLEX_TYPE
-	  && integer_all_onesp (arg0))
-	return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
-
 
       /* X - (X / Y) * Y is X % Y.  */
       if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))