public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select
@ 2020-11-06 10:58 gabravier at gmail dot com
  2020-11-06 11:03 ` [Bug middle-end/97743] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gabravier at gmail dot com @ 2020-11-06 10:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

            Bug ID: 97743
           Summary: Failure to optimize boolean multiplication to select
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

int f(bool b)
{
    return b * 743;
}

This can be optimized to `return b ? 743 : 0;`. This optimization is done by
LLVM, but not GCC.

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
@ 2020-11-06 11:03 ` rguenth at gcc dot gnu.org
  2020-11-06 11:17 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-11-06 11:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-11-06
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
          Component|tree-optimization           |middle-end
             Target|                            |x86_64-*-*

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
But is it really faster?

        movzbl  %dil, %eax
        imull   $743, %eax, %eax

vs.

        negb    %dil
        sbbl    %eax, %eax
        andl    $743, %eax

clang9 produces

        testl   %edi, %edi
        movl    $743, %eax              # imm = 0x2E7
        cmovel  %edi, %eax

more like a middle-end/RTL expansion transform, target dependent.

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
  2020-11-06 11:03 ` [Bug middle-end/97743] " rguenth at gcc dot gnu.org
@ 2020-11-06 11:17 ` jakub at gcc dot gnu.org
  2020-11-06 11:40 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-06 11:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Benchmarking shows at least on i9-7960X the multiplications to be faster -
0m0.493s for multiplication, 0m0.737s for ? 743 : 0 for 1 billion iterations in
a tight loop storing the result into memory.

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
  2020-11-06 11:03 ` [Bug middle-end/97743] " rguenth at gcc dot gnu.org
  2020-11-06 11:17 ` jakub at gcc dot gnu.org
@ 2020-11-06 11:40 ` rguenth at gcc dot gnu.org
  2020-11-06 12:11 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-11-06 11:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
imull on core & zen is 3-4 cycles latency (no idea if they special-case 0 or 1
values somehow, guess not).

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-06 11:40 ` rguenth at gcc dot gnu.org
@ 2020-11-06 12:11 ` pinskia at gcc dot gnu.org
  2020-11-06 12:12 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-11-06 12:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
What about:
(-B)&743
Is that faster?

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2020-11-06 12:11 ` pinskia at gcc dot gnu.org
@ 2020-11-06 12:12 ` pinskia at gcc dot gnu.org
  2021-08-18  6:00 ` pinskia at gcc dot gnu.org
  2021-08-18 21:13 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-11-06 12:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #4)
> What about:
> (-B)&743
> Is that faster?

Never mind I see it was not :)

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2020-11-06 12:12 ` pinskia at gcc dot gnu.org
@ 2021-08-18  6:00 ` pinskia at gcc dot gnu.org
  2021-08-18 21:13 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-18  6:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
What about:
        movzbl  %dil, %eax
        negl    %eax
        andl    $743, %eax

That would be 3 cycles, definitely better than the cmov case.  maybe one cycle
better than the imul case. It all depends on where the argument is coming from
really. If it was a set then we have these three choices:
int fm(int a, int d)
{
    int b = a == d;
    return b * 743;
}
int fc(int a, int d)
{
    return a==d ? 0 : 743;
}
int fand(int a, int d)
{
    int b = a == d;
    int t = -(b&1);
    return t & 743;
}

Producing:
fm:
        xorl    %eax, %eax
        cmpl    %esi, %edi
        sete    %al
        imull   $743, %eax, %eax
        ret
fc:
        xorl    %eax, %eax
        movl    $743, %edx
        cmpl    %esi, %edi
        cmovne  %edx, %eax
        ret
fand:
        xorl    %eax, %eax
        cmpl    %esi, %edi
        sete    %al
        negl    %eax
        andl    $743, %eax
        ret

For aarch64 we get:
fm:
        cmp     w0, w1
        mov     w0, 743
        cset    w1, eq
        mul     w0, w1, w0
        ret
fc:
        cmp     w0, w1
        mov     w0, 743
        csel    w0, w0, wzr, ne
        ret
fand:
        cmp     w0, w1
        mov     w0, 743
        csetm   w1, eq
        and     w0, w1, w0
        ret

For aarch64, the csel is faster as cset is just a csel but with increment.

For x86, It all depends on the cmov performance, I do think the neg/and case is
still faster than the imull case.

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

* [Bug middle-end/97743] Failure to optimize boolean multiplication to select
  2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2021-08-18  6:00 ` pinskia at gcc dot gnu.org
@ 2021-08-18 21:13 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-18 21:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97743

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is really a dup of bug 68557.

*** This bug has been marked as a duplicate of bug 68557 ***

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

end of thread, other threads:[~2021-08-18 21:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-06 10:58 [Bug tree-optimization/97743] New: Failure to optimize boolean multiplication to select gabravier at gmail dot com
2020-11-06 11:03 ` [Bug middle-end/97743] " rguenth at gcc dot gnu.org
2020-11-06 11:17 ` jakub at gcc dot gnu.org
2020-11-06 11:40 ` rguenth at gcc dot gnu.org
2020-11-06 12:11 ` pinskia at gcc dot gnu.org
2020-11-06 12:12 ` pinskia at gcc dot gnu.org
2021-08-18  6:00 ` pinskia at gcc dot gnu.org
2021-08-18 21:13 ` pinskia at gcc dot gnu.org

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