On Wed, 2024-04-10 at 17:40 +0800, LIU Hao wrote: > 在 2024-04-10 17:19, Xi Ruoyao via Gcc-help 写道: > > $ cc t.c -O2 -fsanitize=undefined > > $ ./a.out > > t.c:7:15: runtime error: signed integer overflow: 65535 * 65535 > > cannot be represented in type 'int' > > fffe0001 > > Undefined behavior is not a valid point, as it never happens. You only get a "different result" when an undefined behavior happens, thus it **is** a valid point to say there is no wrong-code issue. > It's a real bug. There are many PRs on bugzilla. You may argue it's a missed-optimization, but we were discussing about wrong-code or not. > The sign extension could have been eliminated completely: > > movzx eax, si > movzx edi, di > imul eax, edi > ret > > > And here is a similar issue: > > typedef unsigned long long int u64; > typedef unsigned int u32; > > u64 foo(u64 a) { > return (u32) __builtin_ctzll(a); > } > > which results in > > xor eax, eax > rep bsf rax, rdi // effectively `tzcnt rax, rdi` > cdqe // unnecessary sign-extension > ret -- Xi Ruoyao <xry111@xxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University