diff mbox

[match-and-simplify] Minor fixes to match-plusminus.pd

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

Commit Message

Richard Biener Sept. 11, 2014, 12:09 p.m. UTC
More closely match-up to tree-ssa-forwprop.c code.

Committed.

Richard.

2014-09-11  Richard Biener  <rguenther@suse.de>

	* match-plusminus.pd: More closely match tree-ssa-forwprop.c code.

Comments

Marc Glisse Sept. 11, 2014, 12:36 p.m. UTC | #1
/* ~A + 1 -> -A */
   (simplify
    (plus (bit_not @0) integer_onep@1)
    (if (TREE_CODE (TREE_TYPE (@1)) != COMPLEX_TYPE
         || (TREE_CODE (@1) == COMPLEX_CST
             && integer_onep (TREE_REALPART (@1))
             && integer_onep (TREE_IMAGPART (@1))))
     (negate @0)))

the complex part cannot happen, since integer_onep already checks that the 
imaginary part is 0. I was thinking of adding a predicate: 
integer_each_onep (or other name) that would be equivalent to integer_onep 
except for complex where it would check for 1+i instead of 1. We already 
have 2 separate predicates for -1 that only differ for complex. And we 
would probably want a corresponding build_ function to produce such 
constants.
Richard Biener Sept. 11, 2014, 12:43 p.m. UTC | #2
On Thu, 11 Sep 2014, Marc Glisse wrote:

>   /* ~A + 1 -> -A */
>   (simplify
>    (plus (bit_not @0) integer_onep@1)
>    (if (TREE_CODE (TREE_TYPE (@1)) != COMPLEX_TYPE
>         || (TREE_CODE (@1) == COMPLEX_CST
>             && integer_onep (TREE_REALPART (@1))
>             && integer_onep (TREE_IMAGPART (@1))))
>     (negate @0)))
> 
> the complex part cannot happen, since integer_onep already checks that the
> imaginary part is 0. I was thinking of adding a predicate: integer_each_onep
> (or other name) that would be equivalent to integer_onep except for complex
> where it would check for 1+i instead of 1. We already have 2 separate
> predicates for -1 that only differ for complex. And we would probably want a
> corresponding build_ function to produce such constants.

Ah, indeed.  The forwprop code reads:

              else if ((TREE_CODE (TREE_TYPE (rhs2)) != COMPLEX_TYPE
                        && integer_onep (rhs2))
                       || (TREE_CODE (rhs2) == COMPLEX_CST
                           && integer_onep (TREE_REALPART (rhs2))
                           && integer_onep (TREE_IMAGPART (rhs2))))

but yes, a new predicate would be nice.  In the pattern we can fix
it by using CONSTANT_CLASS_P as the predicate for @1 (or no
predicate at all).

Richard.
diff mbox

Patch

Index: gcc/match-plusminus.pd
===================================================================
--- gcc/match-plusminus.pd	(revision 215163)
+++ gcc/match-plusminus.pd	(working copy)
@@ -61,21 +61,21 @@  along with GCC; see the file COPYING3.
   (for outer_op (plus minus)
    (for inner_op (plus minus)
     (simplify
-     (outer_op (inner_op @0 INTEGER_CST@1) INTEGER_CST@2)
+     (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
      /* If the constant operation overflows we cannot do the transform
 	as we would introduce undefined overflow, for example
 	with (a - 1) + INT_MIN.  */
-     (with { tree cst = int_const_binop (outer_op == inner_op
-					 ? PLUS_EXPR : MINUS_EXPR, @1, @2); }
-      (if (!TREE_OVERFLOW (cst))
+     (with { tree cst = fold_binary (outer_op == inner_op
+				     ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
+      (if (cst && !TREE_OVERFLOW (cst))
        (inner_op @0 { cst; } ))))))
 
   /* (CST - A) +- CST -> CST - A  */
   (for outer_op (plus minus)
    (simplify
-    (outer_op (minus INTEGER_CST@1 @0) INTEGER_CST@2)
-    (with { tree cst = int_const_binop (outer_op, @1, @2); }
-     (if (!TREE_OVERFLOW (cst))
+    (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
+    (with { tree cst = fold_binary (outer_op, type, @1, @2); }
+     (if (cst && !TREE_OVERFLOW (cst))
       (minus { cst; } @0)))))
 
   /* ~A + A -> -1 */
@@ -85,7 +85,7 @@  along with GCC; see the file COPYING3.
 
   /* ~A + 1 -> -A */
   (simplify
-   (plus (bit_not integral_op_p@0) integer_onep@1)
+   (plus (bit_not @0) integer_onep@1)
    (if (TREE_CODE (TREE_TYPE (@1)) != COMPLEX_TYPE
 	|| (TREE_CODE (@1) == COMPLEX_CST
 	    && integer_onep (TREE_REALPART (@1))
@@ -136,7 +136,9 @@  along with GCC; see the file COPYING3.
 (simplify
   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
   /* Conditionally look through a sign-changing conversion.  */
-  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)))
+  (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
+       && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))
+	    || (GENERIC && type == TREE_TYPE (@1)))))
    @1))
 
 /* From tree-ssa-forwprop.c:associate_pointerplus_align.  */