public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
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:


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




With the patch attatched:


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


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