public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] match.pd: Add new abs pattern [PR94920]
@ 2022-07-21 21:28 Sam Feifer
  2022-07-21 21:34 ` Marek Polacek
  0 siblings, 1 reply; 2+ messages in thread
From: Sam Feifer @ 2022-07-21 21:28 UTC (permalink / raw)
  To: GCC Patches

This patch is intended to fix a missed optimization in match.pd. It optimizes (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) to just abs(x). Additionally, the pattern (x <= 0 ? -x : 0) now gets optimized to max(-x, 0), which helps with the other simplification rule.

Tests are also included to be added to the testsuite.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR tree-optimization/94920

gcc/ChangeLog:

	* match.pd (x >= 0 ? x : 0) + (x <= 0 ? -x : 0): New simplification.
	           (x <= 0 ? -x : 0): New simplification.

gcc/testsuite/ChangeLog:

	* g++.dg/pr94920-1.C: New test.
	* g++.dg/pr94920.C: New test.
	* gcc.dg/pr94920-2.c: New test.
---
 gcc/match.pd                     | 10 +++++
 gcc/testsuite/g++.dg/pr94920-1.C | 17 +++++++++
 gcc/testsuite/g++.dg/pr94920.C   | 63 ++++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr94920-2.c | 15 ++++++++
 4 files changed, 105 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/pr94920-1.C
 create mode 100644 gcc/testsuite/g++.dg/pr94920.C
 create mode 100644 gcc/testsuite/gcc.dg/pr94920-2.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 88a1a5aa9cc..9736393061a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -339,6 +339,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
   (COPYSIGN_ALL (negate @0) @1)))
 
+/* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
+(simplify
+  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
+  (abs @0))
+
 /* X * 1, X / 1 -> X.  */
 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
   (simplify
@@ -3425,6 +3430,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
   (cond (convert:boolean_type_node @2) @1 @0)))
 
+/* (x <= 0 ? -x : 0) -> max(-x, 0).  */
+(simplify
+  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
+  (max @2 @1))
+
 /* Simplifications of shift and rotates.  */
 
 (for rotate (lrotate rrotate)
diff --git a/gcc/testsuite/g++.dg/pr94920-1.C b/gcc/testsuite/g++.dg/pr94920-1.C
new file mode 100644
index 00000000000..6c6483eab2d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr94920-1.C
@@ -0,0 +1,17 @@
+/* PR tree-optimization/94920 */
+/* { dg-do run } */
+
+#include "pr94920.C"
+
+int main() {
+
+    if (foo(0) != 0
+        || foo(-42) != 42
+        || foo(42) != 42
+        || baz(-10) != 10
+        || baz(-10) != 10) {
+            __builtin_abort();
+        }
+    
+    return 0;
+}
diff --git a/gcc/testsuite/g++.dg/pr94920.C b/gcc/testsuite/g++.dg/pr94920.C
new file mode 100644
index 00000000000..925ec4f42f1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr94920.C
@@ -0,0 +1,63 @@
+/* PR tree-optimization/94920 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef int __attribute__((vector_size(4*sizeof(int)))) vint;
+
+/* Same form as PR.  */
+__attribute__((noipa)) unsigned int foo(int x) {
+    return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0);
+}
+
+/* Test for forward propogation.  */
+__attribute__((noipa)) unsigned int corge(int x) {
+    int w = (x >= 0 ? x : 0);
+    int y = -x;
+    int z = (y >= 0 ? y : 0);
+    return w + z;
+}
+
+/* Vector case.  */
+__attribute__((noipa)) vint thud(vint x) {
+    vint t = (x >= 0 ? x : 0) ;
+    vint xx = -x;
+    vint t1 =  (xx >= 0 ? xx : 0);
+    return t + t1;
+}
+
+/* Signed function.  */
+__attribute__((noipa)) int bar(int x) {
+    return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0);
+}
+
+/* Commutative property.  */
+__attribute__((noipa)) unsigned int baz(int x) {
+    return (x <= 0 ? -x : 0) + (x >= 0 ? x : 0);
+}
+
+/* Flipped order for max expressions.  */
+__attribute__((noipa)) unsigned int quux(int x) {
+    return (0 <= x ? x : 0) + (0 >= x ? -x : 0);
+}
+
+/* Not zero so should not optimize.  */
+__attribute__((noipa)) unsigned int waldo(int x) {
+    return (x >= 4 ? x : 4) + (x <= 4 ? -x : 4);
+}
+
+/* Not zero so should not optimize.  */
+__attribute__((noipa)) unsigned int fred(int x) {
+    return (x >= -4 ? x : -4) + (x <= -4 ? -x : -4);
+}
+
+/* Incorrect pattern.  */
+__attribute__((noipa)) unsigned int goo(int x) {
+    return (x <= 0 ? x : 0) + (x >= 0 ? -x : 0);
+}
+
+/* Incorrect pattern.  */
+__attribute__((noipa)) int qux(int x) {
+    return (x >= 0 ? x : 0) + (x >= 0 ? x : 0);
+}
+
+/* { dg-final {scan-tree-dump-times " ABS_EXPR " 6 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr94920-2.c b/gcc/testsuite/gcc.dg/pr94920-2.c
new file mode 100644
index 00000000000..a2d23324cfa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94920-2.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/94920 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Form from PR.  */
+__attribute__((noipa)) unsigned int foo(int x) {
+    return x <= 0 ? -x : 0;
+}
+
+/* Changed order.  */
+__attribute__((noipa)) unsigned int bar(int x) {
+    return 0 >= x ? -x : 0;
+}
+
+/* { dg-final {scan-tree-dump-times " MAX_EXPR " 2 "optimized" } } */

base-commit: 28be481cf47d52af8b11972d2394226bbaf87867
-- 
2.31.1


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

* Re: [pushed] match.pd: Add new abs pattern [PR94920]
  2022-07-21 21:28 [pushed] match.pd: Add new abs pattern [PR94920] Sam Feifer
@ 2022-07-21 21:34 ` Marek Polacek
  0 siblings, 0 replies; 2+ messages in thread
From: Marek Polacek @ 2022-07-21 21:34 UTC (permalink / raw)
  To: Sam Feifer; +Cc: GCC Patches

On Thu, Jul 21, 2022 at 05:28:34PM -0400, Sam Feifer via Gcc-patches wrote:
> This patch is intended to fix a missed optimization in match.pd. It optimizes (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) to just abs(x). Additionally, the pattern (x <= 0 ? -x : 0) now gets optimized to max(-x, 0), which helps with the other simplification rule.
> 
> Tests are also included to be added to the testsuite.
 
To clarify, this patch has been approved by Richi in an earlier
thread, so...

> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

...this line isn't really asking for a review.
 
> 	PR tree-optimization/94920
> 
> gcc/ChangeLog:
> 
> 	* match.pd (x >= 0 ? x : 0) + (x <= 0 ? -x : 0): New simplification.
> 	           (x <= 0 ? -x : 0): New simplification.

The second line ought to be formatted 8 spaces to the left, so that
it's aligned with '* match.pd'.
 
Marek


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

end of thread, other threads:[~2022-07-21 21:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-21 21:28 [pushed] match.pd: Add new abs pattern [PR94920] Sam Feifer
2022-07-21 21:34 ` Marek Polacek

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).