* [x86 PATCH] Improved pre-reload split of double word comparison against -1.
@ 2022-08-02 11:31 Roger Sayle
2022-08-02 17:24 ` Uros Bizjak
0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2022-08-02 11:31 UTC (permalink / raw)
To: 'GCC Patches'
[-- Attachment #1: Type: text/plain, Size: 1346 bytes --]
This patch adds an extra optimization to *cmp<dwi>_doubleword to improve
the code generated for comparisons against -1. Hypothetically, if a
comparison against -1 reached this splitter we'd currently generate code
that looks like:
notq %rdx ; 3 bytes
notq %rax ; 3 bytes
orq %rdx, %rax ; 3 bytes
setne %al
With this patch we would instead generate the superior:
andq %rdx, %rax ; 3 bytes
cmpq $-1, %rax ; 4 bytes
setne %al
which is both faster and smaller, and also what's currently generated
thanks to the middle-end splitting double word comparisons against
zero and minus one during RTL expansion. Should that change, this would
become a missed-optimization regression, but this patch also (potentially)
helps suitable comparisons created by CSE and combine.
This patch has been tested on x86_64-pc-linux-gnu, on its own and in
combination with a middle-end patch tweaking RTL expansion, both with
and without --target-board=unix{-m32}, with no new failures.
Ok for mainline?
2022-08-02 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
* config/i386/i386.md (*cmp<dwi>_doubleword): Add a special case
to split comparisons against -1 using AND and CMP -1 instructions.
Thanks again,
Roger
--
[-- Attachment #2: patchta4.txt --]
[-- Type: text/plain, Size: 707 bytes --]
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index f1158e1..e8f3851 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -1526,6 +1526,15 @@
operands[i] = force_reg (<MODE>mode, operands[i]);
operands[4] = gen_reg_rtx (<MODE>mode);
+
+ /* Special case comparisons against -1. */
+ if (operands[1] == constm1_rtx && operands[3] == constm1_rtx)
+ {
+ emit_insn (gen_and<mode>3 (operands[4], operands[0], operands[2]));
+ emit_insn (gen_cmp_1 (<MODE>mode, operands[4], constm1_rtx));
+ DONE;
+ }
+
if (operands[1] == const0_rtx)
emit_move_insn (operands[4], operands[0]);
else if (operands[0] == const0_rtx)
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [x86 PATCH] Improved pre-reload split of double word comparison against -1.
2022-08-02 11:31 [x86 PATCH] Improved pre-reload split of double word comparison against -1 Roger Sayle
@ 2022-08-02 17:24 ` Uros Bizjak
0 siblings, 0 replies; 2+ messages in thread
From: Uros Bizjak @ 2022-08-02 17:24 UTC (permalink / raw)
To: Roger Sayle; +Cc: GCC Patches
On Tue, Aug 2, 2022 at 1:31 PM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
>
> This patch adds an extra optimization to *cmp<dwi>_doubleword to improve
> the code generated for comparisons against -1. Hypothetically, if a
> comparison against -1 reached this splitter we'd currently generate code
> that looks like:
>
> notq %rdx ; 3 bytes
> notq %rax ; 3 bytes
> orq %rdx, %rax ; 3 bytes
> setne %al
>
> With this patch we would instead generate the superior:
>
> andq %rdx, %rax ; 3 bytes
> cmpq $-1, %rax ; 4 bytes
> setne %al
>
> which is both faster and smaller, and also what's currently generated
> thanks to the middle-end splitting double word comparisons against
> zero and minus one during RTL expansion. Should that change, this would
> become a missed-optimization regression, but this patch also (potentially)
> helps suitable comparisons created by CSE and combine.
>
> This patch has been tested on x86_64-pc-linux-gnu, on its own and in
> combination with a middle-end patch tweaking RTL expansion, both with
> and without --target-board=unix{-m32}, with no new failures.
> Ok for mainline?
>
>
> 2022-08-02 Roger Sayle <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
> * config/i386/i386.md (*cmp<dwi>_doubleword): Add a special case
> to split comparisons against -1 using AND and CMP -1 instructions.
OK.
Thanks,
Uros.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-08-02 17:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 11:31 [x86 PATCH] Improved pre-reload split of double word comparison against -1 Roger Sayle
2022-08-02 17:24 ` Uros Bizjak
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).