public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114204] New: Missed optimization: -(a*!a) => 0 when a=-b-c
@ 2024-03-02  6:13 652023330028 at smail dot nju.edu.cn
  2024-03-02  7:02 ` [Bug tree-optimization/114204] " pinskia at gcc dot gnu.org
  2024-03-02  8:01 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-03-02  6:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114204
           Summary: Missed optimization: -(a*!a) => 0 when a=-b-c
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we noticed that the code below can be optimized as stated in the title
(-(a*!a) => 0), but gcc -O3 -fwrapv missed it.

This issue and PR 113716 have different patterns.

https://godbolt.org/z/W75aP3sx4

int m;
void func(int b, int c){
    int a=-b-c;
    m=-(a*!a);
}
GCC -O3 -fwrapv:
func(int, int):
        lea     eax, [rdi+rsi]
        mov     edx, 0
        add     esi, edi
        cmovne  eax, edx
        mov     DWORD PTR m[rip], eax
        ret

The IR we get is:
  _1 = -b_5(D);
  _2 = _1 == c_6(D);
  _9 = b_5(D) + c_6(D);
  _4 = _2 ? _9 : 0;
  m = _4;


By the way, for the similar code below, although IR has no expected
optimizations, the final assembler code is expected.

int m;
void func2(int b, int c){
    int a=b-c;
    m=-(a*!a);
}

IR:
  _1 = b_4(D) == c_5(D);
  _8 = c_5(D) - b_4(D);
  _3 = _1 ? _8 : 0;
  m = _3;

GCC -O3 -fwrapv:
func2(int, int):
        mov     DWORD PTR m[rip], 0
        ret

Thank you very much for your time and effort! We look forward to hearing from
you.

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

* [Bug tree-optimization/114204] Missed optimization: -(a*!a) => 0 when a=-b-c
  2024-03-02  6:13 [Bug tree-optimization/114204] New: Missed optimization: -(a*!a) => 0 when a=-b-c 652023330028 at smail dot nju.edu.cn
@ 2024-03-02  7:02 ` pinskia at gcc dot gnu.org
  2024-03-02  8:01 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-02  7:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2024-03-02
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---

`A == B ? (A - B) : 0`

And `A == -B ? (A + B) : 0`

Can be handled easily in match.pd.

So mine.

I think both can be done for !have_nans & !HAVE_SIGNED_ZEROS.

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

* [Bug tree-optimization/114204] Missed optimization: -(a*!a) => 0 when a=-b-c
  2024-03-02  6:13 [Bug tree-optimization/114204] New: Missed optimization: -(a*!a) => 0 when a=-b-c 652023330028 at smail dot nju.edu.cn
  2024-03-02  7:02 ` [Bug tree-optimization/114204] " pinskia at gcc dot gnu.org
@ 2024-03-02  8:01 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-02  8:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

Should be both handled in phiopt basically.

Another one too:
```
int f1(int a, int b)
{
        if (a == b)
          return a / b;
        return 1;
}
```
Should be transformed into 1 also.

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

end of thread, other threads:[~2024-03-02  8:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-02  6:13 [Bug tree-optimization/114204] New: Missed optimization: -(a*!a) => 0 when a=-b-c 652023330028 at smail dot nju.edu.cn
2024-03-02  7:02 ` [Bug tree-optimization/114204] " pinskia at gcc dot gnu.org
2024-03-02  8:01 ` 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).