public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/38126]  New: suboptimal code for (a && b || !a && !b)
@ 2008-11-15  0:07 sebor at roguewave dot com
  2008-11-15  0:11 ` [Bug middle-end/38126] " pinskia at gcc dot gnu dot org
  2009-09-12 23:33 ` msebor at gmail dot com
  0 siblings, 2 replies; 7+ messages in thread
From: sebor at roguewave dot com @ 2008-11-15  0:07 UTC (permalink / raw)
  To: gcc-bugs

I would expect gcc to generate comparable code for both functions below, or
perhaps even better code for foo() than for bar() since the code in foo() is
likely to be more common than the equivalent code in bar(). However, the code
produced for foo() is suboptimal in comparison to the code for bar(). In my
timings on x86 with gcc 4.3.0 at -O2, foo() appears to run about 5% slower than
bar().

$ cat t.c && gcc -S -O2 t.c && cat t.s
int foo (int *a, int *b) { return a && b || !a && !b; }
int bar (int *a, int *b) { return !!a == !!b; }
        .file   "t.c"
        .text
        .p2align 4,,15
.globl foo
        .type   foo, @function
foo:
.LFB2:
        testq   %rdi, %rdi
        je      .L2
        testq   %rsi, %rsi
        movl    $1, %eax
        je      .L2
        rep
        ret
        .p2align 4,,10
        .p2align 3
.L2:
        testq   %rdi, %rdi
        sete    %al
        testq   %rsi, %rsi
        sete    %dl
        andl    %edx, %eax
        movzbl  %al, %eax
        ret
.LFE2:
        .size   foo, .-foo
        .p2align 4,,15
.globl bar
        .type   bar, @function
bar:
.LFB3:
        testq   %rdi, %rdi
        sete    %al
        testq   %rsi, %rsi
        setne   %dl
        xorl    %edx, %eax
        movzbl  %al, %eax
        ret
.LFE3:
        .size   bar, .-bar
        .section        .eh_frame,"a",@progbits
.Lframe1:
        .long   .LECIE1-.LSCIE1
.LSCIE1:
        .long   0x0
        .byte   0x1
        .string "zR"
        .uleb128 0x1
        .sleb128 -8
        .byte   0x10
        .uleb128 0x1
        .byte   0x3
        .byte   0xc
        .uleb128 0x7
        .uleb128 0x8
        .byte   0x90
        .uleb128 0x1
        .align 8
.LECIE1:
.LSFDE1:
        .long   .LEFDE1-.LASFDE1
.LASFDE1:
        .long   .LASFDE1-.Lframe1
        .long   .LFB2
        .long   .LFE2-.LFB2
        .uleb128 0x0
        .align 8
.LEFDE1:
.LSFDE3:
        .long   .LEFDE3-.LASFDE3
.LASFDE3:
        .long   .LASFDE3-.Lframe1
        .long   .LFB3
        .long   .LFE3-.LFB3
        .uleb128 0x0
        .align 8
.LEFDE3:
        .ident  "GCC: (GNU) 4.3.0 20080428 (Red Hat 4.3.0-8)"
        .section        .note.GNU-stack,"",@progbits


-- 
           Summary: suboptimal code for (a && b || !a && !b)
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sebor at roguewave dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126


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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
  2008-11-15  0:07 [Bug c/38126] New: suboptimal code for (a && b || !a && !b) sebor at roguewave dot com
@ 2008-11-15  0:11 ` pinskia at gcc dot gnu dot org
  2009-09-12 23:33 ` msebor at gmail dot com
  1 sibling, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-11-15  0:11 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|c                           |middle-end


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126


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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
  2008-11-15  0:07 [Bug c/38126] New: suboptimal code for (a && b || !a && !b) sebor at roguewave dot com
  2008-11-15  0:11 ` [Bug middle-end/38126] " pinskia at gcc dot gnu dot org
@ 2009-09-12 23:33 ` msebor at gmail dot com
  1 sibling, 0 replies; 7+ messages in thread
From: msebor at gmail dot com @ 2009-09-12 23:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from msebor at gmail dot com  2009-09-12 23:33 -------
Code involving bool variables is similarly suboptimal:

$ cat t.cpp && gcc -O2 -S t.cpp && cat t.s
bool foo (bool a, bool b) {
    return a && b || !a && !b;
}

bool bar (bool a, bool b) {
    return a == b;
}
        .file   "t.cpp"
        .text
        .p2align 4,,15
.globl _Z3foobb
        .type   _Z3foobb, @function
_Z3foobb:
.LFB0:
        .cfi_startproc
        .cfi_personality 0x3,__gxx_personality_v0
        movl    %esi, %edx
        movl    %esi, %eax
        xorl    $1, %edx
        testb   %dil, %dil
        cmove   %edx, %eax
        ret
        .cfi_endproc
.LFE0:
        .size   _Z3foobb, .-_Z3foobb
        .p2align 4,,15
.globl _Z3barbb
        .type   _Z3barbb, @function
_Z3barbb:
.LFB1:
        .cfi_startproc
        .cfi_personality 0x3,__gxx_personality_v0
        cmpb    %dil, %sil
        sete    %al
        ret
        .cfi_endproc
.LFE1:
        .size   _Z3barbb, .-_Z3barbb
        .ident  "GCC: (GNU) 4.4.1 20090725 (Red Hat 4.4.1-2)"
        .section        .note.GNU-stack,"",@progbits


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126


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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
       [not found] <bug-38126-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2012-02-02  6:12 ` svfuerst at gmail dot com
@ 2021-06-03  2:48 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-06-03  2:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
For the original testcase with GCC 7, we get the same(similar enough) code gen
for both functions now.
foo:
.LFB0:
        .cfi_startproc
        testq   %rdi, %rdi
        setne   %al
        testq   %rsi, %rsi
        sete    %dl
        xorl    %edx, %eax
        movzbl  %al, %eax
        ret
bar:
.LFB1:
        .cfi_startproc
        testq   %rdi, %rdi
        sete    %al
        testq   %rsi, %rsi
        setne   %dl
        xorl    %edx, %eax
        movzbl  %al, %eax
        ret

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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
       [not found] <bug-38126-4@http.gcc.gnu.org/bugzilla/>
  2012-02-01 22:38 ` pinskia at gcc dot gnu.org
  2012-02-02  5:26 ` svfuerst at gmail dot com
@ 2012-02-02  6:12 ` svfuerst at gmail dot com
  2021-06-03  2:48 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 7+ messages in thread
From: svfuerst at gmail dot com @ 2012-02-02  6:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126

--- Comment #4 from Steven Fuerst <svfuerst at gmail dot com> 2012-02-02 06:11:27 UTC ---
Two more cases for simple boolean logic optimizations.

gcc-4.7 produces with -O3 for

int test_and(long long x, long long y)
{
    return x && y;
}

    test   %rsi, %rsi
    setne  %dl
    xor    %eax, %eax
    test   %rdi, %rdi
    setne  %al
    and    %edx, %eax
    retq

Whereas this is faster:

    neg %rdi
    sbb %rdi, %rdi
    xor %eax, %eax
    and %rsi, %rdi
    setne %al
    retq

Also

int test_other(long long x, long long y)
{
    return !x && y; /* or !(x || !y) */
}

gives

    test   %rsi,%rsi
    setne  %dl
    xor    %eax,%eax
    test   %rdi,%rdi
    sete   %al
    and    %edx,%eax
    retq

when
    sub $1, %rsi
    sbb %rsi, %rsi
    xor %eax, %eax
    or %rdi, %rsi
    sete %al
    retq
is faster.


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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
       [not found] <bug-38126-4@http.gcc.gnu.org/bugzilla/>
  2012-02-01 22:38 ` pinskia at gcc dot gnu.org
@ 2012-02-02  5:26 ` svfuerst at gmail dot com
  2012-02-02  6:12 ` svfuerst at gmail dot com
  2021-06-03  2:48 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 7+ messages in thread
From: svfuerst at gmail dot com @ 2012-02-02  5:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126

Steven Fuerst <svfuerst at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |svfuerst at gmail dot com

--- Comment #3 from Steven Fuerst <svfuerst at gmail dot com> 2012-02-02 05:25:34 UTC ---
For the first pair of functions, this is even faster:

    neg %rdi
    sbb %eax, %eax
    neg %rsi
    adc $1, %eax
    and $1, %eax
    retq


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

* [Bug middle-end/38126] suboptimal code for (a && b || !a && !b)
       [not found] <bug-38126-4@http.gcc.gnu.org/bugzilla/>
@ 2012-02-01 22:38 ` pinskia at gcc dot gnu.org
  2012-02-02  5:26 ` svfuerst at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-02-01 22:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38126

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-02-01
     Ever Confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-02-01 22:37:56 UTC ---
Confirmed.


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

end of thread, other threads:[~2021-06-03  2:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-15  0:07 [Bug c/38126] New: suboptimal code for (a && b || !a && !b) sebor at roguewave dot com
2008-11-15  0:11 ` [Bug middle-end/38126] " pinskia at gcc dot gnu dot org
2009-09-12 23:33 ` msebor at gmail dot com
     [not found] <bug-38126-4@http.gcc.gnu.org/bugzilla/>
2012-02-01 22:38 ` pinskia at gcc dot gnu.org
2012-02-02  5:26 ` svfuerst at gmail dot com
2012-02-02  6:12 ` svfuerst at gmail dot com
2021-06-03  2:48 ` 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).