public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1
@ 2021-07-31  7:05 pinskia at gcc dot gnu.org
  2023-06-23  0:50 ` [Bug tree-optimization/101703] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-31  7:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101703
           Summary: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be
                    optimized to bool0 ^ bool1
           Product: gcc
           Version: 12.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)
{
    int t = a;
    int t1 = b;
    return (t + t1) & 1;
}
bool fa(bool a, bool b)
{
    int t = a;
    int t1 = b;
    return (t + t1)==1;
}
bool fb(bool a, bool b)
{
    return a!=b;
}
bool fc(bool a, bool b)
{
    return a^b;
}

These three should produce the same code gen.  Right now fb and fc do but f and
fa needs to handled.

the for fa, == 1 can be converted into & 1 as the range is [0,2]:
  # RANGE [0, 2] NONZERO 3
  _1 = t_3 + t1_5;
  _6 = _1 == 1;

and only 1 can be if & 1 is true. And the rest just follows through.

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

* [Bug tree-optimization/101703] (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1
  2021-07-31  7:05 [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1 pinskia at gcc dot gnu.org
@ 2023-06-23  0:50 ` pinskia at gcc dot gnu.org
  2023-06-23  1:19 ` 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-06-23  0:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-06-23
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
For most see PR 104292 but we need one more:
(simplify
  (bit_and (plus zero_one_valued_p@0 zero_one_valued_p@1) integer_onep)
  (bit_xor @0 @1)))

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

* [Bug tree-optimization/101703] (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1
  2021-07-31  7:05 [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1 pinskia at gcc dot gnu.org
  2023-06-23  0:50 ` [Bug tree-optimization/101703] " pinskia at gcc dot gnu.org
@ 2023-06-23  1:19 ` pinskia at gcc dot gnu.org
  2023-06-23  5:31 ` pinskia at gcc dot gnu.org
  2024-06-07 16:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-23  1:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
bool f(bool a, bool b)
{
    int t = a;
    int t1 = b;
    return (t + t1) & 2;
}
bool f1(bool a, bool b)
{
    int t = a;
    int t1 = b;
    return (t & t1);
}

This is needed too:
```
(simplify
  (ne (bit_and (plus zero_one_valued_p@0 zero_one_valued_p@1) INTEGER_CST@2)
integer_zerop)
  (if (wi::to_wide (@2) == 2)
    (convert (bit_and @0 @1))
    (if (wi::to_wide (@2) == 1)
     (convert (bit_xor @0 @1)))
  )
)
```

As we don't want to convert `(bool+bool)&2` into `(bool^bool)<<1`

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

* [Bug tree-optimization/101703] (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1
  2021-07-31  7:05 [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1 pinskia at gcc dot gnu.org
  2023-06-23  0:50 ` [Bug tree-optimization/101703] " pinskia at gcc dot gnu.org
  2023-06-23  1:19 ` pinskia at gcc dot gnu.org
@ 2023-06-23  5:31 ` pinskia at gcc dot gnu.org
  2024-06-07 16:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-23  5:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
A few more things to optimize:
```
bool minusne(bool a, bool b)
{
  return (a - b) != 1;
}
bool ornot(bool a, bool b)
{
  return !a | b; // or a <= b
}

bool minusne0(bool a, bool b)
{
  return (a - b) != -1;
}
bool ornot0(bool a, bool b)
{
  return a | !b; // or b <= a
}


bool minuseq(bool a, bool b)
{
  return (a - b) == 1;
}
bool andnot(bool a, bool b)
{
  return a & !b; // or b < a
}

bool minuseq0(bool a, bool b)
{
  return (a - b) == -1;
}
bool andnot0(bool a, bool b)
{
  return !a & b; // or a < b
}
```
minusne{,0} should be optimized into ornot{,0}.
minuseq{,0} should be optimized into andnot{,0}.

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

* [Bug tree-optimization/101703] (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1
  2021-07-31  7:05 [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1 pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-06-23  5:31 ` pinskia at gcc dot gnu.org
@ 2024-06-07 16:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-07 16:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://github.com/llvm/llv
                   |                            |m-project/issues/94737

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Another one:
```
unsigned f(bool a, bool b)
{
        return (a + b)>>1;
}
```

This is just `a & b`.
(this is from https://github.com/llvm/llvm-project/issues/94737 ).

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

end of thread, other threads:[~2024-06-07 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-31  7:05 [Bug tree-optimization/101703] New: (bool0 + bool1) & 1 and (bool0 + bool1) == 1 can be optimized to bool0 ^ bool1 pinskia at gcc dot gnu.org
2023-06-23  0:50 ` [Bug tree-optimization/101703] " pinskia at gcc dot gnu.org
2023-06-23  1:19 ` pinskia at gcc dot gnu.org
2023-06-23  5:31 ` pinskia at gcc dot gnu.org
2024-06-07 16:26 ` 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).