public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Andrew Pinski <pinskia@gmail.com>
Subject: [PATCH 3/3] MATCH: Add some more value_replacement simplifications to match
Date: Sun, 29 Oct 2023 09:40:49 -0700	[thread overview]
Message-ID: <20231029164049.994454-4-pinskia@gmail.com> (raw)
In-Reply-To: <20231029164049.994454-1-pinskia@gmail.com>

This moves a few more value_replacements simplifications to match.
/* a == 1 ? b : a * b -> a * b */
/* a == 1 ? b : b / a  -> b / a */
/* a == -1 ? b : a & b -> a & b */

Also adds a testcase to show can we catch these where value_replacement would not
(but other passes would).

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

gcc/ChangeLog:

	* match.pd (`a == 1 ? b : a OP b`): New pattern.
	(`a == -1 ? b : a & b`): New pattern.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/phi-opt-value-4.c: New test.
---
 gcc/match.pd                                  | 18 ++++++++++
 .../gcc.dg/tree-ssa/phi-opt-value-4.c         | 36 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 9bc945ccada..6efa97cc6ae 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4159,6 +4159,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
    @2))
 
+/* a == 1 ? b : b / a  -> b / a */
+(for op (trunc_div ceil_div floor_div round_div exact_div)
+ (simplify
+  (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
+   @2))
+
+/* a == 1 ? b : a * b -> a * b */
+(for op (mult)
+ (simplify
+  (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
+   @2))
+
+/* a == -1 ? b : a & b -> a & b */
+(for op (bit_and)
+ (simplify
+  (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
+   @2))
+
 /* PTR == 0 ? 0 : &PTR->field -> PTR if field offset was 0. */
 (simplify
  (cond (eq @0 integer_zerop) integer_zerop ADDR_EXPR@1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c
new file mode 100644
index 00000000000..380082cb463
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-fre3 -fdump-tree-phiopt1 -fdump-tree-optimized" } */
+
+[[gnu::const]]
+int constcall(int);
+
+int fdiv(int a, int b)
+{
+  int c = b/a;
+  int t = constcall(c);
+  int d;
+  if (a == 1) d = b; else d = c;
+  return constcall(d) + t;
+}
+int fmult(int a, int b)
+{
+  int c = b*a;
+  int t = constcall(c);
+  int d;
+  if (a == 1) d = b; else d = c;
+  return constcall(d) + t;
+}
+int fand(int a, int b)
+{
+  int c = b&a;
+  int t = constcall(c);
+  int d;
+  if (a == -1) d = b; else d = c;
+  return constcall(d) + t;
+}
+
+/* Should be able to optimize away the if statements in phiopt1. */
+/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
+/* fre3 should be optimize each function to just `return constcall(a OP b) * 2;`. */
+/* { dg-final { scan-tree-dump-times "constcall " 3 "fre3" } } */
+/* { dg-final { scan-tree-dump-times "constcall " 3 "optimized" } } */
-- 
2.39.3


  parent reply	other threads:[~2023-10-29 16:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-29 16:40 [PATCH 0/3] start of moving value replacement from phiopt " Andrew Pinski
2023-10-29 16:40 ` [PATCH 1/3] MATCH: first of the value replacement moving from phiopt Andrew Pinski
2023-10-30  9:32   ` Richard Biener
2023-10-29 16:40 ` [PATCH 2/3] MATCH: Move jump_function_from_stmt support to match.pd Andrew Pinski
2023-10-30  9:29   ` Richard Biener
2023-10-30 20:36     ` Andrew Pinski
2023-10-29 16:40 ` Andrew Pinski [this message]
2023-10-30  9:30   ` [PATCH 3/3] MATCH: Add some more value_replacement simplifications to match Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231029164049.994454-4-pinskia@gmail.com \
    --to=pinskia@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).