AW: optimizer discards sign information
Xi Ruoyao
xry111@xry111.site
Wed Apr 10 10:03:43 GMT 2024
On Wed, 2024-04-10 at 17:57 +0800, LIU Hao via Gcc-help wrote:
> 在 2024-04-10 17:49, stefan@franke.ms 写道:
> > But I keep considering this as a bug. And clang behaves correctly!
>
> Yes there have been many reports [1]. It's a missed optimization.
Note that for this specific case:
typedef unsigned long long int u64;
typedef unsigned int u32;
typedef unsigned short u16;
u64 foo(u16 *a, u16 *b) {
u32 x = *a * *b;
u64 r = x;
return r >> 31;
}
gcc yields
foo:
xor eax, eax
ret
clang yields
foo: # @foo
movzx ecx, word ptr [rdi]
movzx eax, word ptr [rsi]
imul eax, ecx
shr eax, 31
ret
It's actually a missed-optimization of **clang**. Optimizing this
function to always return 0 **is** correct.
But for the general case:
u64 foo(u16 a, u16 b) {
u32 x = a * b;
u64 r = x;
return r;
}
there is a missed-optimization of GCC (redundant sign extension).
> You may work around it by using 32-bit parameters, or casting either
> operand to u32; casting the result will not help.
Indeed.
--
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list