public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
@ 2023-12-18 18:53 pinskia at gcc dot gnu.org
  2023-12-18 19:03 ` [Bug tree-optimization/113072] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 18:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113072
           Summary: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at
                    the gimple level
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: TREE
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int foo(int a, int b) { return (a ^ 4) & (~a ^ 4); }
```
This should be optimized to 0 but is missed at the gimple level.


RTL Combine catches it:
```
Trying 8, 7 -> 9:
    8: {r105:SI=r101:SI^0xfffffffffffffffb;clobber flags:CC;}
      REG_DEAD r101:SI
      REG_UNUSED flags:CC
    7: {r104:SI=r101:SI^0x4;clobber flags:CC;}
      REG_UNUSED flags:CC
    9: {r103:SI=r104:SI&r105:SI;clobber flags:CC;}
      REG_DEAD r105:SI
      REG_DEAD r104:SI
      REG_UNUSED flags:CC
Failed to match this instruction:
(parallel [
        (set (reg:SI 103)
            (const_int 0 [0]))
        (clobber (reg:CC 17 flags))
    ])
Successfully matched this instruction:
(set (reg:SI 103)
    (const_int 0 [0]))
allowing combination of insns 7, 8 and 9
original costs 4 + 4 + 4 = 12
replacement cost 4
```
via apply_distributive_law (I think) which does basically `(a ^ 4) & (a ^
(~4))` -> `(4 ^ (~4)) & (a ^ a)` which is 0 as `a ^ a` is 0.

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
@ 2023-12-18 19:03 ` pinskia at gcc dot gnu.org
  2023-12-18 19:03 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 19:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Wait this part is wrong `which does basically `(a ^ 4) & (a ^ (~4))` -> `(4 ^
(~4)) & (a ^ a)` which is 0 as `a ^ a` is 0.`

Anyways a pattern like:
```
(simplify
 (bit_and (bit_xor @0 @1) (bit_xor @0 @2))
 (with { bool wascmp; }
  (if (bitwise_inverted_equal_p (@1, @2, &wascmp))
   { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
```

Is needed for bool.

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
  2023-12-18 19:03 ` [Bug tree-optimization/113072] " pinskia at gcc dot gnu.org
@ 2023-12-18 19:03 ` pinskia at gcc dot gnu.org
  2023-12-18 19:09 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 19:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
  2023-12-18 19:03 ` [Bug tree-optimization/113072] " pinskia at gcc dot gnu.org
  2023-12-18 19:03 ` pinskia at gcc dot gnu.org
@ 2023-12-18 19:09 ` pinskia at gcc dot gnu.org
  2023-12-18 19:10 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 19:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks like bool sometimes produces != or ^ :)
```
bool foo(bool a, int b)
{
        bool b1 = b == 1;
        bool b2 = !b1;
        bool c = (a ^ b1);
        return  c & (a ^ b2);
}
```
Though that is PR 111149 .

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-12-18 19:09 ` pinskia at gcc dot gnu.org
@ 2023-12-18 19:10 ` pinskia at gcc dot gnu.org
  2023-12-18 19:40 ` pinskia at gcc dot gnu.org
  2024-01-01  0:33 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 19:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
here is the bool testcase which just allows the use of ^:
```
bool foo1(bool a, int b)
{
        bool b1 = b == 1;
        bool b2 = !b1;
        return  (a ^ b1) & (a ^ b2);
}
```

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-12-18 19:10 ` pinskia at gcc dot gnu.org
@ 2023-12-18 19:40 ` pinskia at gcc dot gnu.org
  2024-01-01  0:33 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-18 19:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this applies to `|` instead of `&` too:
```
int foo1(int a, int b)
{
        return  (a ^ 4) | (~a ^ 4);
}
```
This should be optimized to `-1`. Though this does NOT get optimized to `-1` on
the RTL level ...

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

* [Bug tree-optimization/113072] `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level
  2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-12-18 19:40 ` pinskia at gcc dot gnu.org
@ 2024-01-01  0:33 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-01  0:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> Looks like bool sometimes produces != or ^ :)
> ```
> bool foo(bool a, int b)
> {
>         bool b1 = b == 1;
>         bool b2 = !b1;
>         bool c = (a ^ b1);
>         return  c & (a ^ b2);
> }
> ```
> Though that is PR 111149 .

I filed a related testcase to the above as PR 113186 but it does not handle the
above I think.

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

end of thread, other threads:[~2024-01-01  0:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-18 18:53 [Bug tree-optimization/113072] New: `(a ^ CST0) & (~a ^ CST0)` is not optimized to 0 at the gimple level pinskia at gcc dot gnu.org
2023-12-18 19:03 ` [Bug tree-optimization/113072] " pinskia at gcc dot gnu.org
2023-12-18 19:03 ` pinskia at gcc dot gnu.org
2023-12-18 19:09 ` pinskia at gcc dot gnu.org
2023-12-18 19:10 ` pinskia at gcc dot gnu.org
2023-12-18 19:40 ` pinskia at gcc dot gnu.org
2024-01-01  0:33 ` 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).