public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c
@ 2023-06-04  8:15 pinskia at gcc dot gnu.org
  2023-07-12  6:20 ` [Bug tree-optimization/110111] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-04  8:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110111
           Summary: bool patterns that should produce a?b:c
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
bool f(bool a, bool b, bool c)
{
        if (a)  return b;
        return c;
}
bool f1(bool a, bool b, bool c)
{
        return a & b | (!a & c);
}
bool f2(bool a, bool b, bool c)
{
        return a & b | (a < c);
}
```
All 3 should produce the same code.

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

* [Bug tree-optimization/110111] bool patterns that should produce a?b:c
  2023-06-04  8:15 [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c pinskia at gcc dot gnu.org
@ 2023-07-12  6:20 ` pinskia at gcc dot gnu.org
  2023-08-24  6:47 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-07-12
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I just ran into this while writing some code inside GCC even.
I originally wrote the code like:
(innereq && cst0) || (!innereq && cst1)

And then I was like that should just be `innereq ? cst0 : cst1`

The other function this should be done for is:
bool f3(bool a, bool b, bool c)
{
  return (a && b) || (!a && c);
}

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

* [Bug tree-optimization/110111] bool patterns that should produce a?b:c
  2023-06-04  8:15 [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c pinskia at gcc dot gnu.org
  2023-07-12  6:20 ` [Bug tree-optimization/110111] " pinskia at gcc dot gnu.org
@ 2023-08-24  6:47 ` pinskia at gcc dot gnu.org
  2023-08-29  5:27 ` pinskia at gcc dot gnu.org
  2023-08-29  8:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-24  6:47 UTC (permalink / raw)
  To: gcc-bugs

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

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
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=111126

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine.
I have a patch for f1 now.

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

* [Bug tree-optimization/110111] bool patterns that should produce a?b:c
  2023-06-04  8:15 [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c pinskia at gcc dot gnu.org
  2023-07-12  6:20 ` [Bug tree-optimization/110111] " pinskia at gcc dot gnu.org
  2023-08-24  6:47 ` pinskia at gcc dot gnu.org
@ 2023-08-29  5:27 ` pinskia at gcc dot gnu.org
  2023-08-29  8:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-29  5:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
f1:
  _6 = b_2(D) ^ c_3(D);
  _7 = a_1(D) & _6;
  _4 = c_3(D) ^ _7;

Which was done due to:
/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
(simplify
 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))

Note if we move this over to bitwise_inverted_equal_p (which we should), we
will lose also:
```
bool f(int a, int b, int t)
{
  bool x = a == 0;
  bool y = b == 1;
  bool m = t == 2;
  bool mp = !m;
  return (x & mp) | (y & m);
}
```
Which is currently handled.
We should check for `element_precision (type) == 1` too.

So something like:

(simplify
 (bit_ior (bit_and:c@and1 @0 @3) (bit_and:c@and2 @1 @2))
 (with { bool wascmp; }
  (if (bitwise_inverted_equal_p (@0, @2, wascmp))
   (switch
    /* For 1bit, wascmp can be true and we can just convert it into `m ? y : x`
*/
    (if (INTEGRAL_TYPE_P (type) && element_precision (type) == 1)
     (cond @3 @0 @1))
    (if (!wascmp && element_precision (type) != 1
         && single_use (@and1) && single_use (@and2))
     (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
    )
   )
  )
 )
)

/* 1bit `((x ^ y) & m) ^ x` should just be convert into `m ? y : x` early */
(simplify
 (bit_xor:c (bit_and:c (bit_xor:c @0 @1) @2) @0)
 (if (INTEGRAL_TYPE_P (type) && element_precision (type) == 1)
  (cond @2 @0 @1)))

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

* [Bug tree-optimization/110111] bool patterns that should produce a?b:c
  2023-06-04  8:15 [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-08-29  5:27 ` pinskia at gcc dot gnu.org
@ 2023-08-29  8:10 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-29  8:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>/* 1bit `((x ^ y) & m) ^ x` should just be convert into `m ? y : x` early */


Actually it is true for all zero_one_valued_p. Even more if m is just
zere_one_valued_p we could convert it to
(m ? y : x) & 1

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

end of thread, other threads:[~2023-08-29  8:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-04  8:15 [Bug tree-optimization/110111] New: bool patterns that should produce a?b:c pinskia at gcc dot gnu.org
2023-07-12  6:20 ` [Bug tree-optimization/110111] " pinskia at gcc dot gnu.org
2023-08-24  6:47 ` pinskia at gcc dot gnu.org
2023-08-29  5:27 ` pinskia at gcc dot gnu.org
2023-08-29  8:10 ` 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).