From: "xionghuluo(罗雄虎)" <xionghuluo@tencent.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] if-to-switch: Don't skip the first condition bb when find_conditions in if-to-switch [PR105740]
Date: Tue, 21 Jun 2022 11:05:03 +0800 [thread overview]
Message-ID: <tencent_5B86F0F91457C4BE38ACDDAA@qq.com> (raw)
[-- 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
next reply other threads:[~2022-06-21 3:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-21 3:05 xionghuluo(罗雄虎) [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_5B86F0F91457C4BE38ACDDAA@qq.com \
--to=xionghuluo@tencent.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).