public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/110491 - PHI-OPT and undefs
@ 2023-07-04 12:16 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-07-04 12:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: pinskia

The following makes sure to not make conditional undefs in PHI arguments
unconditional by folding cond ? arg1 : arg2.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

This might become redundant with the pending fix to reset
flow-sensitive info.

Richard.

	PR tree-optimization/110491
	* tree-ssa-phiopt.cc (match_simplify_replacement): Check
	whether the PHI args are possibly undefined before folding
	the COND_EXPR.

	* gcc.dg/torture/pr110491.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr110491.c | 29 +++++++++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                  |  7 ++++++
 2 files changed, 36 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr110491.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr110491.c b/gcc/testsuite/gcc.dg/torture/pr110491.c
new file mode 100644
index 00000000000..00b3bdfc122
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr110491.c
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+
+int a, c, d, e;
+short b;
+void f(int *g) { c &= *g; }
+void h(void);
+void i() {
+  a = 1;
+  h();
+  f(&a);
+}
+void h() {
+  int *j = &c;
+  *j = 5;
+k:
+  for (; 4 + b <= 0;)
+    ;
+  for (; d;) {
+    c = e == 0;
+    goto k;
+  }
+}
+int main()
+{
+  i();
+  if (c != 1)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 31a7c39e405..467c9fd108a 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -785,6 +785,13 @@ match_simplify_replacement (basic_block cond_bb, basic_block middle_bb,
       arg_false = arg0;
     }
 
+  /* Do not make conditional undefs unconditional.  */
+  if ((TREE_CODE (arg_true) == SSA_NAME
+       && ssa_name_maybe_undef_p (arg_true))
+      || (TREE_CODE (arg_false) == SSA_NAME
+	  && ssa_name_maybe_undef_p (arg_false)))
+    return false;
+
   tree type = TREE_TYPE (gimple_phi_result (phi));
   result = gimple_simplify_phiopt (early_p, type, stmt,
 				   arg_true, arg_false,
-- 
2.35.3

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-04 12:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-04 12:16 [PATCH] tree-optimization/110491 - PHI-OPT and undefs Richard Biener

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