public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code
@ 2022-01-30 18:34 avi at scylladb dot com
  2022-01-30 18:36 ` [Bug tree-optimization/104292] " avi at scylladb dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: avi at scylladb dot com @ 2022-01-30 18:34 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104292
           Summary: [missed optimization] boolean addition generates
                    suboptimal code
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: avi at scylladb dot com
  Target Milestone: ---

bool bool_or(bool b1, bool b2) {
    return b1 + b2;
}

generates

bool_or(bool, bool):
        movzbl  %dil, %edi
        movzbl  %sil, %esi
        addl    %esi, %edi
        setne   %al
        ret

Whereas it could generate

bool_or(bool, bool):
        or   %edi, %esi
        mov  %esi, %eax
        ret

For a net gain of two instructions (and the final mov can often be elided, so
up to three).

I encountered this while using std::plus<> with boolean types. It could be
optimized as a specialization of std::plus, but I think a magic transformation
in the optimizer would make it apply in more places.

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

* [Bug tree-optimization/104292] [missed optimization] boolean addition generates suboptimal code
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
@ 2022-01-30 18:36 ` avi at scylladb dot com
  2022-01-30 20:02 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: avi at scylladb dot com @ 2022-01-30 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Avi Kivity <avi at scylladb dot com> ---
btw, I see that the equivalent bool_and generates optimal code.

bool_and(bool, bool):
        movl    %esi, %eax
        andl    %edi, %eax
        ret

Perhaps bool is written with the expectation that any non-zero value is true?
But aren't non-zero values other than 1 undefined behavior?

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

* [Bug tree-optimization/104292] [missed optimization] boolean addition generates suboptimal code
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
  2022-01-30 18:36 ` [Bug tree-optimization/104292] " avi at scylladb dot com
@ 2022-01-30 20:02 ` pinskia at gcc dot gnu.org
  2022-01-30 20:05 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-30 20:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization

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

* [Bug tree-optimization/104292] [missed optimization] boolean addition generates suboptimal code
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
  2022-01-30 18:36 ` [Bug tree-optimization/104292] " avi at scylladb dot com
  2022-01-30 20:02 ` pinskia at gcc dot gnu.org
@ 2022-01-30 20:05 ` pinskia at gcc dot gnu.org
  2022-01-30 22:29 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-30 20:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-30
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, I thought I saw one which was asking about bool + bool == 2 also ...

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

* [Bug tree-optimization/104292] [missed optimization] boolean addition generates suboptimal code
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
                   ` (2 preceding siblings ...)
  2022-01-30 20:05 ` pinskia at gcc dot gnu.org
@ 2022-01-30 22:29 ` pinskia at gcc dot gnu.org
  2023-06-23  0:45 ` [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1 pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-30 22:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=101703

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Found it, PR 101703.

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

* [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
                   ` (3 preceding siblings ...)
  2022-01-30 22:29 ` pinskia at gcc dot gnu.org
@ 2023-06-23  0:45 ` pinskia at gcc dot gnu.org
  2023-06-23  0:45 ` pinskia at gcc dot gnu.org
  2024-05-09 14:56 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-23  0:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This should do it I think:

(for neeq (ne eq)
 (simplify
  (neeq (plus zero_one_valued_p@0 zero_one_valued_p@1) INTEGER_CST@2)
  (with {
    tree_code newcode = ERROR_MARK;
    bool notatend = false;
    tree cst1 = build_one_cst (type);
    wide_int cst = wi::to_wide (@2);
    // a + b == 2 -> a & b
    // a + b != 2 -> ~(a & b)
    if (cst == 2)
      newcode = BIT_AND_EXPR, notatend = neeq == NE_EXPR;
    // a + b == 1 -> a ^ b
    // a + b != 1 -> ~(a ^ b) -> a == b
    else if (cst == 1)
      newcode = BIT_XOR_EXPR, notatend = neeq == NE_EXPR;
    // a + b == 0 -> ~(a | b)
    // a + b != 0 -> a | b
    else if (cst == 0)
      newcode = BIT_IOR_EXPR, notatend = neeq == EQ_EXPR;
   }
  (if (notatend)
   (switch
    (if (newcode == BIT_IOR_EXPR)
     (bit_xor (bit_ior @0 @1) { cst1; }))
    (if (newcode == BIT_AND_EXPR)
     (bit_xor (bit_and @0 @1) { cst1; }))
    (if (newcode == BIT_XOR_EXPR)
     (eq @0 @1)))
   (switch
    (if (newcode == BIT_IOR_EXPR)
     (bit_ior @0 @1))
    (if (newcode == BIT_AND_EXPR)
     (bit_and @0 @1))
    (if (newcode == BIT_XOR_EXPR)
     (bit_xor @0 @1)))
  )
 )
)

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

* [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
                   ` (4 preceding siblings ...)
  2023-06-23  0:45 ` [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1 pinskia at gcc dot gnu.org
@ 2023-06-23  0:45 ` pinskia at gcc dot gnu.org
  2024-05-09 14:56 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-23  0:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

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

* [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1
  2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
                   ` (5 preceding siblings ...)
  2023-06-23  0:45 ` pinskia at gcc dot gnu.org
@ 2024-05-09 14:56 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 14:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note LLVM started to do this "trick" in 17.1.0.

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

end of thread, other threads:[~2024-05-09 14:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-30 18:34 [Bug tree-optimization/104292] New: [missed optimization] boolean addition generates suboptimal code avi at scylladb dot com
2022-01-30 18:36 ` [Bug tree-optimization/104292] " avi at scylladb dot com
2022-01-30 20:02 ` pinskia at gcc dot gnu.org
2022-01-30 20:05 ` pinskia at gcc dot gnu.org
2022-01-30 22:29 ` pinskia at gcc dot gnu.org
2023-06-23  0:45 ` [Bug tree-optimization/104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1 pinskia at gcc dot gnu.org
2023-06-23  0:45 ` pinskia at gcc dot gnu.org
2024-05-09 14:56 ` 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).