public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Allow some NOP conversions in (X+CST1)+CST2 in match.pd
@ 2017-05-21 19:45 Marc Glisse
  2017-05-24  8:47 ` Richard Biener
  2017-05-27  8:30 ` Jakub Jelinek
  0 siblings, 2 replies; 4+ messages in thread
From: Marc Glisse @ 2017-05-21 19:45 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: TEXT/PLAIN, Size: 602 bytes --]

Hello,

generalizing a bit one transformation, to avoid a regression with another 
patch I am working on. Handling conversions always gets messy :-( It would 
have been easier to stick to scalars and wide_int, but since the existing 
transformation handles vectors, I didn't want to regress.

Bootstrap+testsuite on powerpc64le-unknown-linux-gnu.

2017-05-22  Marc Glisse  <marc.glisse@inria.fr>

gcc/
 	* match.pd ((A +- CST1) +- CST2): Allow some conversions.
 	* tree.c (drop_tree_overflow): Handle COMPLEX_CST and VECTOR_CST.

gcc/testsuite/
 	* gcc.dg/tree-ssa/addadd.c: New file.

-- 
Marc Glisse

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=conv.patch, Size: 5260 bytes --]

Index: match.pd
===================================================================
--- match.pd	(revision 248312)
+++ match.pd	(working copy)
@@ -1265,29 +1265,53 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (simplify
    (minus @0 (plus:c @0 @1))
    (negate @1))
   (simplify
    (minus @0 (minus @0 @1))
    @1)
 
   /* (A +- CST1) +- CST2 -> A + CST3  */
   (for outer_op (plus minus)
    (for inner_op (plus minus)
+	neg_inner_op (minus plus)
     (simplify
-     (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 = const_binop (outer_op == inner_op
-				     ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
-      (if (cst && !TREE_OVERFLOW (cst))
-       (inner_op @0 { cst; } ))))))
+     (outer_op (convert? (inner_op @0 CONSTANT_CLASS_P@1)) CONSTANT_CLASS_P@2)
+     (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
+      /* If one of the types wraps, use that one.  */
+      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
+       (if (outer_op == PLUS_EXPR)
+	(plus (convert @0) (inner_op @2 (convert @1)))
+	(minus (convert @0) (neg_inner_op @2 (convert @1))))
+       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
+	    || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+	(if (outer_op == PLUS_EXPR)
+	 (convert (plus @0 (inner_op (convert @2) @1)))
+	 (convert (minus @0 (neg_inner_op (convert @2) @1))))
+	/* If the constant operation overflows we cannot do the transform
+	   directly as we would introduce undefined overflow, for example
+	   with (a - 1) + INT_MIN.  */
+	(if (types_match (type, @0))
+	 (with { tree cst = const_binop (outer_op == inner_op
+					 ? PLUS_EXPR : MINUS_EXPR,
+					 type, @1, @2); }
+	  (if (cst && !TREE_OVERFLOW (cst))
+	   (inner_op @0 { cst; } )
+	   /* X+INT_MAX+1 is X-INT_MIN.  */
+	   (if (INTEGRAL_TYPE_P (type) && cst
+		&& wi::eq_p (cst, wi::min_value (type)))
+	    (neg_inner_op @0 { wide_int_to_tree (type, cst); })
+	    /* Last resort, use some unsigned type.  */
+	    (with { tree utype = unsigned_type_for (type); }
+	     (convert (inner_op
+		       (convert:utype @0)
+		       (convert:utype
+			{ drop_tree_overflow (cst); }))))))))))))))
 
   /* (CST1 - A) +- CST2 -> CST3 - A  */
   (for outer_op (plus minus)
    (simplify
     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
     (with { tree cst = const_binop (outer_op, type, @1, @2); }
      (if (cst && !TREE_OVERFLOW (cst))
       (minus { cst; } @0)))))
 
   /* CST1 - (CST2 - A) -> CST3 + A  */
Index: testsuite/gcc.dg/tree-ssa/addadd.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/addadd.c	(nonexistent)
+++ testsuite/gcc.dg/tree-ssa/addadd.c	(working copy)
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+
+int f(unsigned x){
+  x += 123;
+  int y = x;
+  y -= 99;
+  return y;
+}
+unsigned g(int x){
+  x += 123;
+  unsigned y = x;
+  y -= 99;
+  return y;
+}
+int h(int x){
+  x += __INT_MAX__;
+  x += 1;
+  return x;
+}
+int i(int x){
+  x += __INT_MAX__;
+  x += __INT_MAX__;
+  return x;
+}
+typedef int S __attribute__((vector_size(16)));
+void j(S*x){
+  *x += __INT_MAX__;
+  *x += __INT_MAX__;
+}
+
+/* { dg-final { scan-tree-dump-times " \\+ 24;" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\\(unsigned int\\)" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "2147483647" "optimized" } } */
Index: tree.c
===================================================================
--- tree.c	(revision 248312)
+++ tree.c	(working copy)
@@ -13131,20 +13131,39 @@ drop_tree_overflow (tree t)
   gcc_checking_assert (TREE_OVERFLOW (t));
 
   /* For tree codes with a sharing machinery re-build the result.  */
   if (TREE_CODE (t) == INTEGER_CST)
     return wide_int_to_tree (TREE_TYPE (t), t);
 
   /* Otherwise, as all tcc_constants are possibly shared, copy the node
      and drop the flag.  */
   t = copy_node (t);
   TREE_OVERFLOW (t) = 0;
+
+  /* For constants that contain nested constants, drop the flag
+     from those as well.  */
+  if (TREE_CODE (t) == COMPLEX_CST)
+    {
+      if (TREE_OVERFLOW (TREE_REALPART (t)))
+	TREE_REALPART (t) = drop_tree_overflow (TREE_REALPART (t));
+      if (TREE_OVERFLOW (TREE_IMAGPART (t)))
+	TREE_IMAGPART (t) = drop_tree_overflow (TREE_IMAGPART (t));
+    }
+  if (TREE_CODE (t) == VECTOR_CST)
+    {
+      for (unsigned i = 0; i < VECTOR_CST_NELTS (t); ++i)
+	{
+	  tree& elt = VECTOR_CST_ELT (t, i);
+	  if (TREE_OVERFLOW (elt))
+	    elt = drop_tree_overflow (elt);
+	}
+    }
   return t;
 }
 
 /* Given a memory reference expression T, return its base address.
    The base address of a memory reference expression is the main
    object being referenced.  For instance, the base address for
    'array[i].fld[j]' is 'array'.  You can think of this as stripping
    away the offset part from a memory address.
 
    This function calls handled_component_p to strip away all the inner

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Allow some NOP conversions in (X+CST1)+CST2 in match.pd
  2017-05-21 19:45 Allow some NOP conversions in (X+CST1)+CST2 in match.pd Marc Glisse
@ 2017-05-24  8:47 ` Richard Biener
  2017-05-27  8:30 ` Jakub Jelinek
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Biener @ 2017-05-24  8:47 UTC (permalink / raw)
  To: Marc Glisse; +Cc: GCC Patches

On Sun, May 21, 2017 at 9:22 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> Hello,
>
> generalizing a bit one transformation, to avoid a regression with another
> patch I am working on. Handling conversions always gets messy :-( It would
> have been easier to stick to scalars and wide_int, but since the existing
> transformation handles vectors, I didn't want to regress.

The pattern looks a bit unwieldly now ;)  I pondered a bit but
couldn't really find
a better way to handle things.

Thus, ok.

Richard.


> Bootstrap+testsuite on powerpc64le-unknown-linux-gnu.
>
> 2017-05-22  Marc Glisse  <marc.glisse@inria.fr>
>
> gcc/
>         * match.pd ((A +- CST1) +- CST2): Allow some conversions.
>         * tree.c (drop_tree_overflow): Handle COMPLEX_CST and VECTOR_CST.
>
> gcc/testsuite/
>         * gcc.dg/tree-ssa/addadd.c: New file.
>
> --
> Marc Glisse

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Allow some NOP conversions in (X+CST1)+CST2 in match.pd
  2017-05-21 19:45 Allow some NOP conversions in (X+CST1)+CST2 in match.pd Marc Glisse
  2017-05-24  8:47 ` Richard Biener
@ 2017-05-27  8:30 ` Jakub Jelinek
  2017-05-27  8:41   ` Marc Glisse
  1 sibling, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2017-05-27  8:30 UTC (permalink / raw)
  To: Marc Glisse; +Cc: gcc-patches

On Sun, May 21, 2017 at 09:22:56PM +0200, Marc Glisse wrote:
> generalizing a bit one transformation, to avoid a regression with another
> patch I am working on. Handling conversions always gets messy :-( It would
> have been easier to stick to scalars and wide_int, but since the existing
> transformation handles vectors, I didn't want to regress.
> 
> Bootstrap+testsuite on powerpc64le-unknown-linux-gnu.
> 
> 2017-05-22  Marc Glisse  <marc.glisse@inria.fr>
> 
> gcc/
> 	* match.pd ((A +- CST1) +- CST2): Allow some conversions.
> 	* tree.c (drop_tree_overflow): Handle COMPLEX_CST and VECTOR_CST.
> 
> gcc/testsuite/
> 	* gcc.dg/tree-ssa/addadd.c: New file.

After discussions in PR80887, I've reverted the match.pd part and xfailed
the test, because it breaks bootstrap and will take a while to resolve
properly.

2017-05-27  Jakub Jelinek  <jakub@redhat.com>

	PR bootstrap/80887
	Revert:
	2017-05-25  Marc Glisse  <marc.glisse@inria.fr>

	* match.pd ((A +- CST1) +- CST2): Allow some conversions.

	* gcc.dg/tree-ssa/addadd.c: Xfail all scan-tree-dump*.

--- gcc/match.pd	(revision 248448)
+++ gcc/match.pd	(revision 248447)
@@ -1299,39 +1299,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   /* (A +- CST1) +- CST2 -> A + CST3  */
   (for outer_op (plus minus)
    (for inner_op (plus minus)
-	neg_inner_op (minus plus)
     (simplify
-     (outer_op (convert? (inner_op @0 CONSTANT_CLASS_P@1)) CONSTANT_CLASS_P@2)
-     (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
-      /* If one of the types wraps, use that one.  */
-      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
-       (if (outer_op == PLUS_EXPR)
-	(plus (convert @0) (inner_op @2 (convert @1)))
-	(minus (convert @0) (neg_inner_op @2 (convert @1))))
-       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-	    || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
-	(if (outer_op == PLUS_EXPR)
-	 (convert (plus @0 (inner_op (convert @2) @1)))
-	 (convert (minus @0 (neg_inner_op (convert @2) @1))))
-	/* If the constant operation overflows we cannot do the transform
-	   directly as we would introduce undefined overflow, for example
-	   with (a - 1) + INT_MIN.  */
-	(if (types_match (type, @0))
-	 (with { tree cst = const_binop (outer_op == inner_op
-					 ? PLUS_EXPR : MINUS_EXPR,
-					 type, @1, @2); }
-	  (if (cst && !TREE_OVERFLOW (cst))
-	   (inner_op @0 { cst; } )
-	   /* X+INT_MAX+1 is X-INT_MIN.  */
-	   (if (INTEGRAL_TYPE_P (type) && cst
-		&& wi::eq_p (cst, wi::min_value (type)))
-	    (neg_inner_op @0 { wide_int_to_tree (type, cst); })
-	    /* Last resort, use some unsigned type.  */
-	    (with { tree utype = unsigned_type_for (type); }
-	     (convert (inner_op
-		       (convert:utype @0)
-		       (convert:utype
-			{ drop_tree_overflow (cst); }))))))))))))))
+     (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 = const_binop (outer_op == inner_op
+				     ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
+      (if (cst && !TREE_OVERFLOW (cst))
+       (inner_op @0 { cst; } ))))))
 
   /* (CST1 - A) +- CST2 -> CST3 - A  */
   (for outer_op (plus minus)
--- gcc/testsuite/gcc.dg/tree-ssa/addadd.c	(revision 248532)
+++ gcc/testsuite/gcc.dg/tree-ssa/addadd.c	(working copy)
@@ -29,6 +29,6 @@ void j(S*x){
   *x += __INT_MAX__;
 }
 
-/* { dg-final { scan-tree-dump-times " \\+ 24;" 2 "optimized" } } */
-/* { dg-final { scan-tree-dump-times "\\(unsigned int\\)" 2 "optimized" } } */
-/* { dg-final { scan-tree-dump-not "2147483647" "optimized" } } */
+/* { dg-final { scan-tree-dump-times " \\+ 24;" 2 "optimized" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "\\(unsigned int\\)" 2 "optimized" { xfail *-*-* }  } } */
+/* { dg-final { scan-tree-dump-not "2147483647" "optimized" { xfail *-*-* }  } } */


	Jakub

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Allow some NOP conversions in (X+CST1)+CST2 in match.pd
  2017-05-27  8:30 ` Jakub Jelinek
@ 2017-05-27  8:41   ` Marc Glisse
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Glisse @ 2017-05-27  8:41 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Sat, 27 May 2017, Jakub Jelinek wrote:

> After discussions in PR80887, I've reverted the match.pd part and xfailed
> the test, because it breaks bootstrap and will take a while to resolve
> properly.

Thank you.

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-05-27  8:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-21 19:45 Allow some NOP conversions in (X+CST1)+CST2 in match.pd Marc Glisse
2017-05-24  8:47 ` Richard Biener
2017-05-27  8:30 ` Jakub Jelinek
2017-05-27  8:41   ` Marc Glisse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).