diff mbox

[RTL-ifcvt] PR rtl-optimization/68435 Allow (c ? x++ : x--) form

Message ID 5653323E.4080708@arm.com
State Accepted
Commit 9b71b2ab84caafd7f32429772745f9b07d85321f
Headers show

Commit Message

Kyrylo Tkachov Nov. 23, 2015, 3:35 p.m. UTC
Hi all,

In this PR we fail to if-convert a case where in the expression x = c ? a : b;
'a' and 'b' are something like x + 1 and x - 1.
So x appears in a and b.
The code that checks that nothing from the else block modifies the registers used in a
rejects this case. It should accept when the modification is in the last insn of the block
i.e. insn_a or insn_b in the language of noce_try_cmove_arith because we will not be
emitting insn_a and insn_b verbatim, but rather their modified versions emit_a and emit_b
that have had their destinations modified to fresh pseudos, so no conflicts will arise.

Bootstrapped and tested on arm, aarch64, x86_64.
This improved if-conversion opportunities a bit across SPEC2006, not so much as to make
a difference, but definitely a small improvement.

Ok for trunk?

Thanks,
Kyrill

2015-11-23  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     PR rtl-optimization/68435
     * ifcvt.c (noce_try_cmove_arith): Skip final insn when checking
     for clonflicts between a, b and the set destinations.

2015-11-23  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     PR rtl-optimization/68435
     * gcc.dg/pr68435.c: New test.
diff mbox

Patch

commit c890c68e2862980731e95cb4b7982f10b29109b7
Author: Kyrylo Tkachov <kyrylo.tkachov@arm.com>
Date:   Fri Nov 20 09:13:04 2015 +0000

    [ifcvt] PR rtl-optimization/68435 Allow (c ? x++ : x--) form

diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c
index d721ec7..af7a3b9 100644
--- a/gcc/ifcvt.c
+++ b/gcc/ifcvt.c
@@ -2210,7 +2210,10 @@  noce_try_cmove_arith (struct noce_if_info *if_info)
   if (tmp_b && then_bb)
     {
       FOR_BB_INSNS (then_bb, tmp_insn)
-	if (modified_in_p (orig_b, tmp_insn))
+	/* Don't check inside insn_a.  We will have changed it to emit_a
+	   with a destination that doesn't conflict.  */
+	if (!(insn_a && tmp_insn == insn_a)
+	    && modified_in_p (orig_b, tmp_insn))
 	  {
 	    modified_in_a = true;
 	    break;
@@ -2223,7 +2226,10 @@  noce_try_cmove_arith (struct noce_if_info *if_info)
 	if (tmp_b && else_bb)
 	  {
 	    FOR_BB_INSNS (else_bb, tmp_insn)
-	      if (modified_in_p (orig_a, tmp_insn))
+	    /* Don't check inside insn_b.  We will have changed it to emit_b
+	       with a destination that doesn't conflict.  */
+	      if (!(insn_b && tmp_insn == insn_b)
+		  && modified_in_p (orig_a, tmp_insn))
 		{
 		  modified_in_b = true;
 		  break;
diff --git a/gcc/testsuite/gcc.dg/pr68435.c b/gcc/testsuite/gcc.dg/pr68435.c
new file mode 100644
index 0000000..765699a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr68435.c
@@ -0,0 +1,52 @@ 
+/* { dg-do compile { target aarch64*-*-* x86_64-*-* } } */
+/* { dg-options "-fdump-rtl-ce1 -O2 -w" } */
+
+typedef struct cpp_reader cpp_reader;
+enum cpp_ttype
+{
+  CPP_EQ =
+    0, CPP_NOT, CPP_GREATER, CPP_LESS, CPP_PLUS, CPP_MINUS, CPP_MULT, CPP_DIV,
+  CPP_MOD, CPP_AND, CPP_OR, CPP_XOR, CPP_RSHIFT, CPP_LSHIFT, CPP_MIN,
+  CPP_MAX, CPP_COMPL, CPP_AND_AND, CPP_OR_OR, CPP_QUERY, CPP_COLON,
+  CPP_COMMA, CPP_OPEN_PAREN, CPP_CLOSE_PAREN, CPP_EQ_EQ, CPP_NOT_EQ,
+  CPP_GREATER_EQ, CPP_LESS_EQ, CPP_PLUS_EQ, CPP_MINUS_EQ, CPP_MULT_EQ,
+  CPP_DIV_EQ, CPP_MOD_EQ, CPP_AND_EQ, CPP_OR_EQ, CPP_XOR_EQ, CPP_RSHIFT_EQ,
+  CPP_LSHIFT_EQ, CPP_MIN_EQ, CPP_MAX_EQ, CPP_HASH, CPP_PASTE,
+  CPP_OPEN_SQUARE, CPP_CLOSE_SQUARE, CPP_OPEN_BRACE, CPP_CLOSE_BRACE,
+  CPP_SEMICOLON, CPP_ELLIPSIS, CPP_PLUS_PLUS, CPP_MINUS_MINUS, CPP_DEREF,
+  CPP_DOT, CPP_SCOPE, CPP_DEREF_STAR, CPP_DOT_STAR, CPP_ATSIGN, CPP_NAME,
+  CPP_NUMBER, CPP_CHAR, CPP_WCHAR, CPP_OTHER, CPP_STRING, CPP_WSTRING,
+  CPP_HEADER_NAME, CPP_COMMENT, CPP_MACRO_ARG, CPP_PADDING, CPP_EOF,
+};
+
+static struct op lex (cpp_reader *, int);
+
+struct op
+{
+  enum cpp_ttype op;
+  long value;
+};
+
+int
+_cpp_parse_expr (pfile)
+{
+  struct op init_stack[20];
+  struct op *stack = init_stack;
+  struct op *top = stack + 1;
+  int skip_evaluation = 0;
+  for (;;)
+    {
+      struct op op;
+      op = lex (pfile, skip_evaluation);
+      switch (op.op)
+	{
+	case CPP_OR_OR:
+	  if (top->value)
+	    skip_evaluation++;
+	  else
+	    skip_evaluation--;
+	}
+    }
+}
+
+/* { dg-final { scan-rtl-dump "2 true changes made" "ce1" } } */