public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8090] phiopt: Fix up debug handling in the (x != cst1 ? x : cst2) != cst3 opt [PR105218]
@ 2022-04-11 17:09 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-04-11 17:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:1774ab84863202f5db694914b67e3aef8c6dd807

commit r12-8090-g1774ab84863202f5db694914b67e3aef8c6dd807
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Apr 11 19:06:59 2022 +0200

    phiopt: Fix up debug handling in the (x != cst1 ? x : cst2) != cst3 opt [PR105218]
    
    In the PR104639 optimization, I've added code to emit
      # DEBUG D#1 => arg != carg ? arg : oarg
    instruction and replace debug uses of the phi with that debug
    temp, so that the debug info is still accurrate.
    Unfortunately, that is only correct if the middle-bb and
    phi bb contain 1 and 2 predecessors, i.e. the ones that
    we are using in the optimization (in particular middle-bb has
    cond-bb as pred and phi bb cond-bb and middle-bb).
    If that is not the case, then we can reach these from another bb
    and so the arg SSA_NAME might not be valid there (its definition
    doesn't dominate all incoming edges), or, even if it is valid,
    might be wrong-debug, e.g. phi argument from some unrelated other
    incoming edge might have the carg value that the debug stmt
    remaps to oarg.  In theory we could check for that case and
    if middle-bb doesn't have a single pred or phi bb 2 preds
    check if arg SSA_NAME dominates the phi bb and if all other
    phi arguments are expr_not_equal_to the carg value, but this patch
    just uses a simpler approach and resets already if we have some
    extra incoming edges.
    
    2022-04-11  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/105218
            * tree-ssa-phiopt.cc (value_replacement): If middle_bb has
            more than one predecessor or phi's bb more than 2 predecessors,
            reset phi result uses instead of adding a debug temp.
    
            * gcc.dg/pr105218.c: New test.

Diff:
---
 gcc/testsuite/gcc.dg/pr105218.c | 16 ++++++++++++++++
 gcc/tree-ssa-phiopt.cc          | 13 +++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/pr105218.c b/gcc/testsuite/gcc.dg/pr105218.c
new file mode 100644
index 00000000000..0070057c32d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr105218.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/105218 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -g" } */
+
+int a, c;
+void bar (void);
+
+void
+foo (void)
+{
+  int b = 131;
+  if (a)
+    b = c == 2 ? 1 : c;
+  while (b)
+    bar ();
+}
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 00c8f399e4c..3eda825672c 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -1454,6 +1454,7 @@ value_replacement (basic_block cond_bb, basic_block middle_bb,
 		  imm_use_iterator imm_iter;
 		  tree phires = gimple_phi_result (phi);
 		  tree temp = NULL_TREE;
+		  bool reset_p = false;
 
 		  /* Add # DEBUG D#1 => arg != carg ? arg : oarg.  */
 		  FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, phires)
@@ -1462,6 +1463,16 @@ value_replacement (basic_block cond_bb, basic_block middle_bb,
 			continue;
 		      if (temp == NULL_TREE)
 			{
+			  if (!single_pred_p (middle_bb)
+			      || EDGE_COUNT (gimple_bb (phi)->preds) != 2)
+			    {
+			      /* But only if middle_bb has a single
+				 predecessor and phi bb has two, otherwise
+				 we could use a SSA_NAME not usable in that
+				 place or wrong-debug.  */
+			      reset_p = true;
+			      break;
+			    }
 			  gimple_stmt_iterator gsi
 			    = gsi_after_labels (gimple_bb (phi));
 			  tree type = TREE_TYPE (phires);
@@ -1476,6 +1487,8 @@ value_replacement (basic_block cond_bb, basic_block middle_bb,
 			replace_exp (use_p, temp);
 		      update_stmt (use_stmt);
 		    }
+		  if (reset_p)
+		    reset_debug_uses (phi);
 		}
 	    }
 	  if (equal_p)


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

only message in thread, other threads:[~2022-04-11 17:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-11 17:09 [gcc r12-8090] phiopt: Fix up debug handling in the (x != cst1 ? x : cst2) != cst3 opt [PR105218] Jakub Jelinek

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