https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114722
Bug ID: 114722
Summary: Missed optimization: !e*d*e=>0, affected by useless
instructions
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
(!e*d*e=>0), but gcc -O3 -fwrapv missed it. It looks like it's affected by some
useless instructions.
https://godbolt.org/z/5vPqcbGWr
extern int x;
extern int y;
void func1(int a, int b, int c, int d, int e) {
a = c;
a = b;
a = d;
x = c;
y = !e * d * e;
}
GCC -O3 -fwrapv:
func1(int, int, int, int, int):
xor eax, eax
test r8d, r8d
mov DWORD PTR x[rip], edx
cmovne ecx, eax
imul ecx, r8d
mov DWORD PTR y[rip], ecx
ret
Expected code (when we remove 'a=c; a=b; a=d;'):
func1(int, int, int, int, int):
mov DWORD PTR x[rip], edx
mov DWORD PTR y[rip], 0
ret
By the way, when the exact same fun1 and func2 appear together, func2 gets the
expected result:
extern int x;
extern int y;
void func1(int a, int b, int c, int d, int e) {
a = c;
a = b;
a = d;
x = c;
y = !e * d * e;
}
void func2(int a, int b, int c, int d, int e) {
a = c;
a = b;
a = d;
x = c;
y = !e * d * e;
}
GCC -O3 -fwrapv:
func1(int, int, int, int, int):
xor eax, eax
test r8d, r8d
mov DWORD PTR x[rip], edx
cmovne ecx, eax
imul ecx, r8d
mov DWORD PTR y[rip], ecx
ret
func2(int, int, int, int, int):
mov DWORD PTR x[rip], edx
mov DWORD PTR y[rip], 0
ret
Thank you very much for your time and effort! We look forward to hearing from
you.