[COMMITTED] match: Improve `(a != b) ? (a + b) : (2 * a)` pattern [PR19832]

Message ID 20231225040034.251374-1-quic_apinski@quicinc.com
State Unresolved
Headers
Series [COMMITTED] match: Improve `(a != b) ? (a + b) : (2 * a)` pattern [PR19832] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Andrew Pinski (QUIC) Dec. 25, 2023, 4 a.m. UTC
  In the testcase provided, we would match f_plus but not g_plus
due to a missing `:c` on the plus operator. This fixes the oversight
there.

Note this was noted in https://github.com/llvm/llvm-project/issues/76318 .

Committed as obvious after bootstrap/test on x86_64-linux-gnu.

	PR tree-optimization/19832

gcc/ChangeLog:

	* match.pd (`(a != b) ? (a + b) : (2 * a)`): Add `:c`
	on the plus operator.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/phi-opt-same-2.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/match.pd                                  |  2 +-
 .../gcc.dg/tree-ssa/phi-opt-same-2.c          | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-2.c
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index d57e29bfe1d..a980c4d7e94 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5694,7 +5694,7 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
     @2)))
  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
  (simplify
-  (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
+  (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
    @2))
 )
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-2.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-2.c
new file mode 100644
index 00000000000..94fb6a92cea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-2.c
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-optimized" } */
+/* PR tree-optimization/19832 */
+
+int f_plus(int a, int b)
+{
+  if (a != b) return a + b;
+  return a + a;
+}
+
+int g_plus(int a, int b)
+{
+  if (b != a) return a + b;
+  return a + a;
+}
+
+/* All of the above function's if should have been optimized away even in phiopt1. */
+/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "optimized" } } */