public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] if-to-switch: Don't skip the first condition bb when find_conditions in if-to-switch [PR105740]
@ 2022-06-21  3:05 xionghuluo(罗雄虎)
  2022-06-21  3:44 ` Xionghu Luo
  2022-06-21  7:33 ` Richard Biener
  0 siblings, 2 replies; 10+ messages in thread
From: xionghuluo(罗雄虎) @ 2022-06-21  3:05 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 3076 bytes --]

Current GCC generates:


 test2:
.LFB0:
        .cfi_startproc
        xorl    %edx, %edx
        cmpl    $3, (%rdi)
        jle     .L1
        movl    16(%rdi), %eax
        cmpl    $1, %eax
        je      .L4
        subl    $2, %eax
        cmpl    $4, %eax
        ja      .L1
        movl    CSWTCH.1(,%rax,4), %edx
.L1:
        movl    %edx, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L4:
        movl    $12, %edx
        jmp     .L1
        .cfi_endproc
.LFE0:
        .size   test2, .-test2
        .section        .rodata
        .align 16
        .type   CSWTCH.1, @object
        .size   CSWTCH.1, 20
CSWTCH.1:
        .long   27
        .long   38
        .long   18
        .long   58
        .long   68




With the patch attatched:


 test2:
.LFB0:
        .cfi_startproc
        xorl    %edx, %edx
        cmpl    $3, (%rdi)
        jle     .L1
        movl    16(%rdi), %eax
        subl    $1, %eax
        cmpl    $5, %eax
        jbe     .L6
.L1:
        movl    %edx, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L6:
        movl    CSWTCH.1(,%rax,4), %edx
        movl    %edx, %eax
        ret
        .cfi_endproc
.LFE0:
        .size   test2, .-test2
        .section        .rodata
        .align 16
        .type   CSWTCH.1, @object
        .size   CSWTCH.1, 24
CSWTCH.1:
        .long   12
        .long   27
        .long   38
        .long   18
        .long   58
        .long   68


Bootstrap and regression tested pass on x86_64-linux-gnu, OK for master?

[-- Attachment #2: 0001-if-to-switch-Don-t-skip-the-first-condition-bb-when-.patch --]
[-- Type: application/octet-stream, Size: 2167 bytes --]

From 53b8cde013e976d18d6378726e7699b81b03c580 Mon Sep 17 00:00:00 2001
From: Xionghu Luo <xionghuluo@tencent.com>
Date: Thu, 9 Jun 2022 15:46:30 +0800
Subject: [PATCH] if-to-switch: Don't skip the first condition bb when
 find_conditions in if-to-switch [PR105740]

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:

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

gcc/testsuite/ChangeLog:

	* 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                    |  2 +-
 .../gcc.dg/tree-ssa/if-to-switch-11.c         | 28 +++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-11.c

diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc
index 5dcfe5b77e0..f7b0b02628b 100644
--- a/gcc/gimple-if-to-switch.cc
+++ b/gcc/gimple-if-to-switch.cc
@@ -389,7 +389,7 @@ find_conditions (basic_block bb,
   if (cond == NULL)
     return;
 
-  if (!no_side_effect_bb (bb))
+  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
index 00000000000..3dffee04812
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-11.c
@@ -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" } } */
-- 
2.35.1.415.g9a52fd993e


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-06-29 12:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-21  3:05 [PATCH] if-to-switch: Don't skip the first condition bb when find_conditions in if-to-switch [PR105740] xionghuluo(罗雄虎)
2022-06-21  3:44 ` Xionghu Luo
2022-06-21  7:28   ` Martin Liška
2022-06-21  7:33     ` Xi Ruoyao
2022-06-21  7:42       ` Martin Liška
2022-06-21 10:08         ` Xionghu Luo
2022-06-21  7:33 ` Richard Biener
2022-06-21 10:05   ` Xionghu Luo
2022-06-21 12:08     ` Richard Biener
2022-06-29 12:01       ` Martin Liška

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