From: Kyrylo Tkachov <[email protected]>

The AArch64 if-conversion hook intends to accept candidates outside
loops without applying its CSEL cost limit, because the branch predictor
has little history to train on there.  The check has never fired.  In
ce1 the loop tree is still there, but a block outside every loop belongs
to the root loop, whose header is the entry block, so testing for a null
header does not find it.  In ce2 and ce3 if_convert frees the loop tree
before it examines candidates, so current_loops is null.

Record natural-loop membership before the loop tree is freed and pass it
to the target in noce_if_info.  Use it in the AArch64 hook, and apply
the rule only when optimizing for speed, since it is about branch
prediction.  find_if_case_1 gives the jump block it creates the index of
the THEN block it deletes, so assert that this block has one successor
and therefore can never be the test block of a later conversion.

Add tests for the same dependent sequence inside and outside a loop, for
outside-loop acceptance with explicit low cost limits, and for -Os
keeping the branch.

Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill

gcc/ChangeLog:

        * ifcvt.h (noce_if_info): Add in_loop.
        * ifcvt.cc (bbs_in_loops): New bitmap.
        (noce_find_if_block): Set in_loop.
        (find_if_case_1): Assert that the replacement block has one
        successor.
        (if_convert): Record loop membership before finalizing loops.
        Release the bitmap at the end of the pass.
        * config/aarch64/aarch64.cc
        (aarch64_noce_conversion_profitable_p): Use in_loop, and only when
        optimizing for speed.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/if_conv_outside_loop_1.c: New test.
        * gcc.target/aarch64/if_conv_outside_loop_2.c: Likewise.
        * gcc.target/aarch64/if_conv_outside_loop_3.c: Likewise.

Signed-off-by: Kyrylo Tkachov <[email protected]>
---
 gcc/config/aarch64/aarch64.cc                 | 11 ++---
 gcc/ifcvt.cc                                  | 13 +++++
 gcc/ifcvt.h                                   |  3 ++
 .../aarch64/if_conv_outside_loop_1.c          | 47 +++++++++++++++++++
 .../aarch64/if_conv_outside_loop_2.c          | 33 +++++++++++++
 .../aarch64/if_conv_outside_loop_3.c          | 29 ++++++++++++
 6 files changed, 129 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_1.c
 create mode 100644 gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_2.c
 create mode 100644 gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_3.c

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 948967982dd..7a73a441f98 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -2405,13 +2405,10 @@ static bool
 aarch64_noce_conversion_profitable_p (rtx_insn *seq,
                                      struct noce_if_info *if_info)
 {
-  /* If not in a loop, just accept all if-conversion as the branch predictor
-     won't have anything to train on.  So assume sequences are essentially
-     unpredictable.  ce1 is in CFG mode still while ce2 is outside.  For ce2
-     accept limited if-conversion based on the shape of the instruction.  */
-  if (current_loops
-      && (!if_info->test_bb->loop_father
-         || !if_info->test_bb->loop_father->header))
+  /* Outside loops the branch predictor has little history to train on, so
+     treat the branch as unpredictable and accept the sequence when
+     optimizing for speed.  */
+  if (if_info->speed_p && !if_info->in_loop)
     return true;
 
   /* For now we only care about csel speciifcally.  */
diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc
index fad341ead6c..8cc8ab1c369 100644
--- a/gcc/ifcvt.cc
+++ b/gcc/ifcvt.cc
@@ -92,6 +92,9 @@ static int num_true_changes;
    successful conversion sets it, not only the conditional-execution one.  */
 static bool ifcvt_changed_p;
 
+/* Blocks that are inside a loop at the start of this pass.  */
+static bitmap_head bbs_in_loops;
+
 /* Forward references.  */
 static bool noce_operand_ok (const_rtx);
 static void merge_if_block (ce_if_block *);
@@ -5514,6 +5517,7 @@ noce_find_if_block (basic_block test_bb, edge then_edge, 
edge else_edge,
   if_info.jump = jump;
   if_info.then_else_reversed = then_else_reversed;
   if_info.speed_p = speed_p;
+  if_info.in_loop = bitmap_bit_p (&bbs_in_loops, test_bb->index);
   if_info.max_seq_cost
     = targetm.max_noce_ifcvt_seq_cost (then_edge);
   /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
@@ -6377,6 +6381,9 @@ find_if_case_1 (basic_block test_bb, edge then_edge, edge 
else_edge)
      block we removed.  */
   if (new_bb)
     {
+      /* NEW_BB takes the index, and so the bbs_in_loops bit, of THEN_BB.
+        That bit is only read for a block that ends in a conditional jump.  */
+      gcc_checking_assert (single_succ_p (new_bb));
       df_bb_replace (then_bb_index, new_bb);
       /* This should have been done above via force_nonfallthru_and_redirect
          (possibly called from redirect_edge_and_branch_force).  */
@@ -6810,6 +6817,10 @@ if_convert (ifcvt_phase phase)
 
   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
   mark_loop_exit_edges ();
+  bitmap_initialize (&bbs_in_loops, &bitmap_default_obstack);
+  FOR_EACH_BB_FN (bb, cfun)
+    if (bb_loop_depth (bb) != 0)
+      bitmap_set_bit (&bbs_in_loops, bb->index);
   loop_optimizer_finalize ();
   free_dominance_info (CDI_DOMINATORS);
 
@@ -6851,6 +6862,8 @@ if_convert (ifcvt_phase phase)
 
   free_dominance_info (CDI_POST_DOMINATORS);
 
+  bitmap_release (&bbs_in_loops);
+
   if (dump_file)
     fflush (dump_file);
 
diff --git a/gcc/ifcvt.h b/gcc/ifcvt.h
index 806ad47158e..8347e0e8bc8 100644
--- a/gcc/ifcvt.h
+++ b/gcc/ifcvt.h
@@ -120,6 +120,9 @@ struct noce_if_info
      we're optimizing for size.  */
   bool speed_p;
 
+  /* True if TEST_BB was inside a loop at the start of this pass.  */
+  bool in_loop;
+
   /* An estimate of the cost of the original, un-converted if-region.  It
      covers COND and JUMP plus the arms: both arms when optimizing for size,
      and their branch-probability-weighted average when optimizing for speed,
diff --git a/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_1.c 
b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_1.c
new file mode 100644
index 00000000000..c9bb3960aa6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_1.c
@@ -0,0 +1,47 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+/* The target accepts if-conversion outside loops without a cost limit.  */
+
+int
+outside_loop (int v, int threshold)
+{
+  int t = v;
+  if (v > threshold)
+    {
+      t = v * 3;
+      t += 7;
+      t ^= 0x55;
+      t *= 0x55;
+      t -= 5;
+      t &= 0xFE;
+      t ^= 0x55;
+    }
+  return t;
+}
+
+/* The same sequence inside a loop exceeds the cost limit.  */
+
+int
+inside_loop (int n, const int *a, int threshold)
+{
+  int s = 0;
+  for (int i = 0; i < n; i++)
+    {
+      int v = a[i], t = v;
+      if (v > threshold)
+       {
+         t = v * 3;
+         t += 7;
+         t ^= 0x55;
+         t *= 0x55;
+         t -= 5;
+         t &= 0xFE;
+         t ^= 0x55;
+       }
+      s += t;
+    }
+  return s;
+}
+
+/* { dg-final { scan-assembler-times "\\tcsel\\t" 1 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_2.c 
b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_2.c
new file mode 100644
index 00000000000..732d23ec6fb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_2.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mcpu=olympus 
--param=max-rtl-if-conversion-unpredictable-cost=1 
--param=max-rtl-if-conversion-predictable-cost=1 -fdump-rtl-ce1" } */
+
+/* Outside loops, the target accepts the sequence before checking its cost.  */
+
+volatile unsigned long output[2];
+
+void
+select_values (unsigned long c, unsigned long x, unsigned long y)
+{
+  unsigned long a, b;
+  if (c > 7)
+    {
+      a = x + 9;
+      a ^= y;
+      a >>= 2;
+      a += 5;
+      a ^= y;
+      a >>= 3;
+      a += 13;
+      a ^= y;
+      b = 2;
+    }
+  else
+    {
+      a = x + 9;
+      b = 3;
+    }
+  output[0] = a;
+  output[1] = b;
+}
+
+/* { dg-final { scan-rtl-dump-times "if-conversion succeeded through 
noce_convert_multiple_sets" 1 "ce1" } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_3.c 
b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_3.c
new file mode 100644
index 00000000000..0350c80eb34
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/if_conv_outside_loop_3.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-Os" } */
+
+/* Outside loops the target skips its cost check only when optimizing for
+   speed.  When optimizing for size the three selects are larger than the
+   branch they replace.  */
+
+void sink3 (long, long, long);
+
+void
+f (long c)
+{
+  long a, b, d;
+  if (c == 3)
+    {
+      a = 5;
+      b = 7;
+      d = 13;
+    }
+  else
+    {
+      a = 9;
+      b = 11;
+      d = 17;
+    }
+  sink3 (a, b, d);
+}
+
+/* { dg-final { scan-assembler-not "\\tcsel\\t" } } */
-- 
2.54.0 (Apple Git-157)

Reply via email to