From 3200de91bc70bfd6d4b7b40e769bc82756b9b555 Mon Sep 17 00:00:00 2001
From: Andrew Pinski <apinski@marvell.com>
Date: Mon, 15 Nov 2021 22:55:09 +0000
Subject: [PATCH] tree-optimization: [PR103245] Improve detection of abs
 pattern using multiplication

So while working on PR 103228 (and a few others), I noticed the testcase for PR 94785
was failing. The problem is that the nop_convert moved from being inside the IOR to be
outside of it. I also noticed the patch for PR 103228 was not needed to reproduce the
issue either.
This patch combines the two patterns together for the abs match when using multiplication
and adds a few places where nop_convert are optional.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/103245

gcc/ChangeLog:

	* match.pd: Combine the abs pattern matching using multiplication.
	Adding optional nop_convert too.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/pr103245-1.c: New test.
---
 gcc/match.pd                               | 22 +++++++++----------
 gcc/testsuite/gcc.dg/tree-ssa/pr103245-1.c | 25 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103245-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index a319aefa8081..758322da217f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1478,21 +1478,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (absu tree_expr_nonnegative_p@0)
  (convert @0))
 
-/* Simplify (-(X < 0) | 1) * X into abs (X).  */
+/* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
 (simplify
- (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
- (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
-  (abs @0)))
-
-/* Similarly (-(X < 0) | 1U) * X into absu (X).  */
-(simplify
- (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
-		  integer_onep) (nop_convert @0))
+ (mult:c (nop_convert1?
+	  (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
+		    integer_onep))
+	 (nop_convert3? @0))
  (if (INTEGRAL_TYPE_P (type)
-      && TYPE_UNSIGNED (type)
       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
-  (absu @0)))
+  (if (TYPE_UNSIGNED (type))
+   (absu @0)
+   (abs @0)
+  )
+ )
+)
 
 /* A few cases of fold-const.c negate_expr_p predicate.  */
 (match negate_expr_p
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103245-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103245-1.c
new file mode 100644
index 000000000000..68ddeadb7996
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103245-1.c
@@ -0,0 +1,25 @@
+/* PR tree-optimization/103245 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times " = ABSU_EXPR <v_\[0-9]*\\\(D\\\)>;" 1 "optimized" } } */
+
+unsigned
+f1 (int v)
+{
+  unsigned int d_6;
+  int b_5;
+  int a_4;
+  _Bool _1;
+  unsigned int v1_2;
+  unsigned int _7;
+  int _9;
+
+  _1 = v < 0;
+  a_4 = (int) _1;
+  b_5 = -a_4;
+  _9 = b_5 | 1;
+  d_6 = (unsigned int) _9;
+  v1_2 = (unsigned int) v;
+  _7 = v1_2 * d_6;
+  return _7;
+}
-- 
GitLab