From: "Yangfei (Felix)" <felix.yang@huawei.com>
To: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Cc: "Zhanghaijian (A)" <z.zhanghaijian@huawei.com>
Subject: [PATCH PR94026] combine missed opportunity to simplify comparisons with zero
Date: Wed, 04 Mar 2020 08:39:00 -0000 [thread overview]
Message-ID: <DA41BE1DDCA941489001C7FBD7A8820EE7D88351@dggeml527-mbx.china.huawei.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 941 bytes --]
Hi,
This is a simple fix for PR94026.
With this fix, combine will try make an extraction if we are in a equality comparison and this is an AND
with a constant which is power of two minus one. Shift here should be an constant. For example, combine
will transform (compare (and (lshiftrt x 8) 6) 0) to (compare (zero_extract (x 2 9)) 0).
Added one test case for this. Bootstrap and tested on both x86_64 and aarch64 Linux platform.
Any suggestion?
Thanks,
Felix
gcc:
+2020-03-04 Felix Yang <felix.yang@huawei.com>
+
+ PR rtl-optimization/94026
+ * combine.c (make_compound_operation_int): Make an extraction
+ if we are in a equality comparison and this is an AND with a
+ constant which is power of two minus one.
+
gcc/testsuite:
+2020-03-04 Felix Yang <felix.yang@huawei.com>
+
+ PR rtl-optimization/94026
+ * gcc.dg/pr94026.c: New test.
+
[-- Attachment #2: pr94026-v0.txt --]
[-- Type: text/plain, Size: 5277 bytes --]
diff --git a/gcc/combine.c b/gcc/combine.c
index 58366a6d331..c05064fc333 100644
--- a/gcc/combine.c
+++ b/gcc/combine.c
@@ -8170,14 +8170,31 @@ make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
if (!CONST_INT_P (XEXP (x, 1)))
break;
+ HOST_WIDE_INT pos;
+ unsigned HOST_WIDE_INT len;
+ pos = get_pos_from_mask (UINTVAL (XEXP (x, 1)), &len);
+
/* If the constant is a power of two minus one and the first operand
- is a logical right shift, make an extraction. */
+ is a logical right shift, make an extraction.
+ If we are in a equality comparison and this is an AND with a constant
+ which is power of two minus one, also make an extraction. */
if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
- && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
+ && (pos == 0 || (pos > 0 && equality_comparison
+ && CONST_INT_P (XEXP (XEXP (x, 0), 1)))))
{
new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
- new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
- i, 1, 0, in_code == COMPARE);
+ if (pos == 0)
+ {
+ new_rtx = make_extraction (mode, new_rtx, 0,
+ XEXP (XEXP (x, 0), 1), len, 1, 0,
+ in_code == COMPARE);
+ }
+ else
+ {
+ int real_pos = pos + UINTVAL (XEXP (XEXP (x, 0), 1));
+ new_rtx = make_extraction (mode, new_rtx, real_pos, NULL_RTX,
+ len, 1, 0, in_code == COMPARE);
+ }
}
/* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
@@ -8186,13 +8203,25 @@ make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
&& is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
&inner_mode)
&& GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
- && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
+ && (pos == 0
+ || (pos > 0 && equality_comparison
+ && CONST_INT_P (XEXP (SUBREG_REG (XEXP (x, 0)), 1)))))
{
rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
- new_rtx = make_extraction (inner_mode, new_rtx, 0,
- XEXP (inner_x0, 1),
- i, 1, 0, in_code == COMPARE);
+ if (pos == 0)
+ {
+ new_rtx = make_extraction (inner_mode, new_rtx, 0,
+ XEXP (inner_x0, 1),
+ len, 1, 0, in_code == COMPARE);
+ }
+ else
+ {
+ int real_pos = pos + UINTVAL (XEXP (inner_x0, 1));
+ new_rtx = make_extraction (inner_mode, new_rtx, real_pos,
+ NULL_RTX, len, 1, 0,
+ in_code == COMPARE);
+ }
/* If we narrowed the mode when dropping the subreg, then we lose. */
if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
@@ -8200,10 +8229,10 @@ make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
/* If that didn't give anything, see if the AND simplifies on
its own. */
- if (!new_rtx && i >= 0)
+ if (!new_rtx)
{
new_rtx = make_compound_operation (XEXP (x, 0), next_code);
- new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
+ new_rtx = make_extraction (mode, new_rtx, pos, NULL_RTX, len, 1,
0, in_code == COMPARE);
}
}
@@ -8212,7 +8241,7 @@ make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
|| GET_CODE (XEXP (x, 0)) == IOR)
&& GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
- && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
+ && (pos == 0 || (pos > 0 && equality_comparison)))
{
/* Apply the distributive law, and then try to make extractions. */
new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
@@ -8228,14 +8257,14 @@ make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
else if (GET_CODE (XEXP (x, 0)) == ROTATE
&& CONST_INT_P (XEXP (XEXP (x, 0), 1))
- && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
- && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
+ && (pos == 0 || (pos > 0 && equality_comparison))
+ && pos + len <= UINTVAL (XEXP (XEXP (x, 0), 1)))
{
new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
new_rtx = make_extraction (mode, new_rtx,
(GET_MODE_PRECISION (mode)
- - INTVAL (XEXP (XEXP (x, 0), 1))),
- NULL_RTX, i, 1, 0, in_code == COMPARE);
+ - UINTVAL (XEXP (XEXP (x, 0), 1)) + pos),
+ NULL_RTX, len, 1, 0, in_code == COMPARE);
}
/* On machines without logical shifts, if the operand of the AND is
diff --git a/gcc/testsuite/gcc.dg/pr94026.c b/gcc/testsuite/gcc.dg/pr94026.c
new file mode 100644
index 00000000000..7b5184dfb2c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94026.c
@@ -0,0 +1,20 @@
+/* { dg-do compile { target aarch64*-*-* i?86-*-* x86_64-*-* } } */
+/* { dg-options "-O2 -fdump-rtl-combine" } */
+
+int
+foo (int c)
+{
+ int a = (c >> 8) & 7;
+
+ if (a >= 2) {
+ return 1;
+ }
+
+ return 0;
+}
+
+/* The combine phase should transform (compare (and (lshiftrt x 8) 6) 0)
+ to (compare (zero_extract (x 2 9)) 0). We look for the *attempt*
+ to match this RTL pattern, regardless of whether an actual insn
+ may be found on the platform. */
+/* { dg-final { scan-rtl-dump "\\(zero_extract" "combine" } } */
next reply other threads:[~2020-03-04 8:39 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-04 8:39 Yangfei (Felix) [this message]
2020-03-05 15:37 ` Jeff Law
2020-03-06 1:01 ` Yangfei (Felix)
2020-03-12 23:50 ` Segher Boessenkool
2020-03-13 3:21 ` Yangfei (Felix)
2020-03-13 16:07 ` Segher Boessenkool
2020-03-16 6:29 ` Yangfei (Felix)
2020-03-16 17:58 ` Segher Boessenkool
2020-03-17 2:05 ` Yangfei (Felix)
2020-03-18 23:51 ` Segher Boessenkool
2020-03-19 1:43 ` Yangfei (Felix)
2020-03-20 1:38 ` Segher Boessenkool
2020-03-23 7:46 ` Yangfei (Felix)
2020-03-23 12:09 ` Segher Boessenkool
2020-03-24 6:30 ` Yangfei (Felix)
2020-03-24 14:58 ` Segher Boessenkool
2020-05-06 8:57 ` Yangfei (Felix)
2020-05-07 16:51 ` Segher Boessenkool
2020-05-23 14:57 ` Segher Boessenkool
2020-05-25 2:59 ` Yangfei (Felix)
2020-05-25 16:26 ` Segher Boessenkool
2020-05-26 3:45 ` Yangfei (Felix)
2020-05-26 15:31 ` Segher Boessenkool
2020-05-27 3:51 ` Yangfei (Felix)
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=DA41BE1DDCA941489001C7FBD7A8820EE7D88351@dggeml527-mbx.china.huawei.com \
--to=felix.yang@huawei.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=z.zhanghaijian@huawei.com \
/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).