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.

Reply via email to