]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
if-to-switch: Don't skip the first condition bb when find_conditions in if-to-switch...
authorXionghu Luo <xionghuluo@tencent.com>
Thu, 9 Jun 2022 07:46:30 +0000 (15:46 +0800)
committerXionghu Luo <xionghuluo@tencent.com>
Tue, 21 Jun 2022 09:26:45 +0000 (17:26 +0800)
The if condition is at last of first bb, so side effect statement in first BB
doesn't matter, then the first if condition could also be folded to switch
table.

gcc/ChangeLog:

PR target/105740
* gimple-if-to-switch.cc (find_conditions): Don't skip the first
condition bb.

gcc/testsuite/ChangeLog:

PR target/105740
* gcc.dg/tree-ssa/if-to-switch-11.c: New test.

Signed-off-by: Xionghu Luo <xionghuluo@tencent.com>
gcc/gimple-if-to-switch.cc
gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-11.c [new file with mode: 0644]

index 5dcfe5b77e0a90357d03918fe038cb6011e1a20f..ca276d790c322f62619437588b93f81106ce420c 100644 (file)
@@ -389,7 +389,9 @@ find_conditions (basic_block bb,
   if (cond == NULL)
     return;
 
-  if (!no_side_effect_bb (bb))
+  /* An empty conditions_in_bbs indicates we are processing the first
+     basic-block then no need check side effect.  */
+  if (!conditions_in_bbs->is_empty () && !no_side_effect_bb (bb))
     return;
 
   tree lhs = gimple_cond_lhs (cond);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-11.c b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-11.c
new file mode 100644 (file)
index 0000000..3dffee0
--- /dev/null
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-iftoswitch-optimized" } */
+
+struct f {
+  int len;
+  int arr[4];
+};
+
+int
+test (struct f const *const f)
+{
+  if (f->arr[3] == 1) {
+    return 12;
+  } else if (f->arr[3] == 2) {
+    return 27;
+  } else if (f->arr[3] == 3) {
+    return 38;
+  } else if (f->arr[3] == 4) {
+    return 18;
+  } else if (f->arr[3] == 5) {
+    return 58;
+  } else if (f->arr[3] == 6) {
+    return 68;
+  }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "Canonical GIMPLE case clusters: 1 2 3 4 5 6" "iftoswitch" } } */