diff mbox series

[2/2,V4] Add plusminus-with-convert pattern (PR 94234)

Message ID MWHPR01MB23043B9BDFA2D3BB6906B248F7200@MWHPR01MB2304.prod.exchangelabs.com
State New
Headers show
Series Ping: [PATCH 2/2 V3] Simplify plusminus-mult-with-convert expr in forwprop (PR 94234) | expand

Commit Message

Feng Xue OS Sept. 15, 2020, 10:14 a.m. UTC
Add a rule (T)(A) +- (T)(B) -> (T)(A +- B), which works only when (A +- B)
could be folded to a simple value. By this rule, a plusminus-mult-with-convert
expression could be handed over to the rule (A * C) +- (B * C) -> (A +- B).

Bootstrapped/regtested on x86_64-linux and aarch64-linux.

Feng
---
2020-09-15  Feng Xue  <fxue@os.amperecomputing.com>

gcc/
	PR tree-optimization/94234
	* match.pd (T)(A) +- (T)(B) -> (T)(A +- B): New simplification.

gcc/testsuite/
	PR tree-optimization/94234
 	* gcc.dg/pr94234-3.c: New test.

Comments

Richard Biener Sept. 15, 2020, 11:07 a.m. UTC | #1
On Tue, Sep 15, 2020 at 12:14 PM Feng Xue OS
<fxue@os.amperecomputing.com> wrote:
>
> Add a rule (T)(A) +- (T)(B) -> (T)(A +- B), which works only when (A +- B)
> could be folded to a simple value. By this rule, a plusminus-mult-with-convert
> expression could be handed over to the rule (A * C) +- (B * C) -> (A +- B).

Please use INTEGRAL_TYPE_P () instead of TREE_CODE == INTEGER_TYPE
in all three cases.  It's enough to check for INTEGRAL_TYPE_P on one operand,
the types_match will take care of the other.

OK with those changes.

Thanks,
Richard.

> Bootstrapped/regtested on x86_64-linux and aarch64-linux.
>
> Feng
> ---
> 2020-09-15  Feng Xue  <fxue@os.amperecomputing.com>
>
> gcc/
>         PR tree-optimization/94234
>         * match.pd (T)(A) +- (T)(B) -> (T)(A +- B): New simplification.
>
> gcc/testsuite/
>         PR tree-optimization/94234
>         * gcc.dg/pr94234-3.c: New test.
Feng Xue OS Sept. 15, 2020, 12:25 p.m. UTC | #2
>> Add a rule (T)(A) +- (T)(B) -> (T)(A +- B), which works only when (A +- B)
>> could be folded to a simple value. By this rule, a plusminus-mult-with-convert
>> expression could be handed over to the rule (A * C) +- (B * C) -> (A +- B).
>
>Please use INTEGRAL_TYPE_P () instead of TREE_CODE == INTEGER_TYPE
>in all three cases.  It's enough to check for INTEGRAL_TYPE_P on one operand,
>the types_match will take care of the other.

I would have considered using INTEGRAL_TYPE_P(), but if inner type is bool or
enum, can we do plus/minus operation on that?

Feng

>
>OK with those changes.
>
>Thanks,
>Richard.
>
>
> Bootstrapped/regtested on x86_64-linux and aarch64-linux.
>
> Feng
> ---
> 2020-09-15  Feng Xue  <fxue@os.amperecomputing.com>
>
> gcc/
>         PR tree-optimization/94234
>         * match.pd (T)(A) +- (T)(B) -> (T)(A +- B): New simplification.
>
> gcc/testsuite/
>         PR tree-optimization/94234
>         * gcc.dg/pr94234-3.c: New test.
Richard Biener Sept. 15, 2020, 12:30 p.m. UTC | #3
On Tue, Sep 15, 2020 at 2:25 PM Feng Xue OS <fxue@os.amperecomputing.com> wrote:
>
> >> Add a rule (T)(A) +- (T)(B) -> (T)(A +- B), which works only when (A +- B)
> >> could be folded to a simple value. By this rule, a plusminus-mult-with-convert
> >> expression could be handed over to the rule (A * C) +- (B * C) -> (A +- B).
> >
> >Please use INTEGRAL_TYPE_P () instead of TREE_CODE == INTEGER_TYPE
> >in all three cases.  It's enough to check for INTEGRAL_TYPE_P on one operand,
> >the types_match will take care of the other.
>
> I would have considered using INTEGRAL_TYPE_P(), but if inner type is bool or
> enum, can we do plus/minus operation on that?

Yes, the distinction doesn't really exist for the middle-end - they
are just integer
types with certain precision and signedness.

Richard.

> Feng
>
> >
> >OK with those changes.
> >
> >Thanks,
> >Richard.
> >
> >
> > Bootstrapped/regtested on x86_64-linux and aarch64-linux.
> >
> > Feng
> > ---
> > 2020-09-15  Feng Xue  <fxue@os.amperecomputing.com>
> >
> > gcc/
> >         PR tree-optimization/94234
> >         * match.pd (T)(A) +- (T)(B) -> (T)(A +- B): New simplification.
> >
> > gcc/testsuite/
> >         PR tree-optimization/94234
> >         * gcc.dg/pr94234-3.c: New test.
diff mbox series

Patch

From f7c7483bd61fe1e3d6888f84d718fb4be4ea9e14 Mon Sep 17 00:00:00 2001
From: Feng Xue <fxue@os.amperecomputing.com>
Date: Mon, 17 Aug 2020 23:00:35 +0800
Subject: [PATCH] tree-optimization/94234 - add plusminus-with-convert pattern

Add a rule (T)(A) +- (T)(B) -> (T)(A +- B), which works only when (A +- B)
could be folded to a simple value. By this rule, a plusminus-mult-with-convert
expression could be handed over to the rule (A * C) +- (B * C) -> (A +- B).

2020-09-15  Feng Xue  <fxue@os.amperecomputing.com>

gcc/
	PR tree-optimization/94234
	* match.pd (T)(A) +- (T)(B) -> (T)(A +- B): New simplification.

gcc/testsuite/
	PR tree-optimization/94234
 	* gcc.dg/pr94234-3.c: New test.
---
 gcc/match.pd                     | 16 ++++++++++++
 gcc/testsuite/gcc.dg/pr94234-3.c | 42 ++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr94234-3.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 46fd880bd37..d8c59fad9c1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2397,6 +2397,22 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        (plus (convert @0) (op @2 (convert @1))))))
 #endif
 
+/* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
+   to a simple value.  */
+#if GIMPLE
+  (for op (plus minus)
+   (simplify
+    (op (convert @0) (convert @1))
+     (if (TREE_CODE (type) == INTEGER_TYPE
+	  && TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
+	  && TREE_CODE (TREE_TYPE (@1)) == INTEGER_TYPE
+	  && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
+	  && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+	  && !TYPE_OVERFLOW_TRAPS (type)
+	  && !TYPE_OVERFLOW_SANITIZED (type))
+      (convert (op! @0 @1)))))
+#endif
+
   /* ~A + A -> -1 */
   (simplify
    (plus:c (bit_not @0) @0)
diff --git a/gcc/testsuite/gcc.dg/pr94234-3.c b/gcc/testsuite/gcc.dg/pr94234-3.c
new file mode 100644
index 00000000000..9bb9b46bd96
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94234-3.c
@@ -0,0 +1,42 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1" } */
+
+typedef __SIZE_TYPE__ size_t;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+
+ptrdiff_t foo1 (char *a, size_t n)
+{
+  char *b1 = a + 8 * n;
+  char *b2 = a + 8 * (n - 1);
+
+  return b1 - b2;
+}
+
+int use_ptr (char *a, char *b);
+
+ptrdiff_t foo2 (char *a, size_t n)
+{
+  char *b1 = a + 8 * (n - 1);
+  char *b2 = a + 8 * n;
+
+  use_ptr (b1, b2);
+
+  return b1 - b2;
+}
+
+int use_int (int i);
+
+unsigned goo (unsigned m_param, unsigned n_param)
+{
+  unsigned b1 = m_param * (n_param + 2);
+  unsigned b2 = m_param * (n_param + 1);
+  int r = (int)(b1) - (int)(b2);
+
+  use_int (r);
+
+  return r;
+}
+
+/* { dg-final { scan-tree-dump-times "return 8;" 1 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "return -8;" 1 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "return m_param" 1 "forwprop1" } } */
-- 
2.17.1