public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: Alexander Monakov <amonakov@ispras.ru>, stefan@franke.ms
Cc: gcc-help@gcc.gnu.org
Subject: Re: optimizer discards sign information
Date: Wed, 10 Apr 2024 17:19:54 +0800	[thread overview]
Message-ID: <5379650ad2582ae97c40ef1a78ded354033d2f71.camel@xry111.site> (raw)
In-Reply-To: <d49ac68a-aee4-907b-3a86-100c62b2d805@ispras.ru>

On Wed, 2024-04-10 at 12:16 +0300, Alexander Monakov wrote:
> 
> On Wed, 10 Apr 2024, stefan@franke.ms wrote:
> 
> > Hi all,
> > 
> > I just stumbled over an issue, which is present in almost all gcc versions.
> > I worked around using inline assembly…
> > Maybe gcc behaves correct and I am wrong? Here is the code:
> > 
> > https://godbolt.org/z/cW8jcdh56
> > 
> > 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;
> > }
> > 
> > And on gcc 13.2 x86.64 you get
> > 
> > foo:
> >         movzx   esi, si
> >         movzx   edi, di
> >         imul    edi, esi
> >         movsx   rax, edi
> >         ret
> > 
> > 
> > There is a sign extension! The optimizer step discards the information
> > 
> > 	 x_6 = (u32) _3;
> > 
> > and uses _3 directly instead, which is signed.
> > 
> > Am I wrong or is it gcc?
> 
> GCC is not wrong. When your code computes x:
> 
>     u32 x = a * b;
> 
> 'a' and 'b' are first promoted to int according to C language rules, and
> the multiplication happens in the signed int type, with UB on overflow.
> The compiler deduces the range of signed int temporary holding the result
> of the multiplication is [0, 0x7fffffff], which allows to propagate it
> to the assignment of 'r' (which in the end produces a sign extension,
> as you observed, so the propagation did not turn out to be useful).
> 
> u16 * u16 is a famous footgun for sure. I'd suggest 'x = 1u * a * b'
> as a fix for the code.

Also note that -fsanitize=undefined detects the issue properly:

$ cat t.c
typedef unsigned long long int u64;
typedef unsigned int u32;
typedef unsigned short u16;

__attribute__((noipa))
u64 foo(u16 a, u16 b) {
    u32 x = a * b;
    u64 r = x;
    return r;
}

int main()
{
	__builtin_printf("%llx\n", foo(65535, 65535));
}
$ 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

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2024-04-10  9:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-10  8:52 stefan
2024-04-10  9:16 ` Alexander Monakov
2024-04-10  9:19   ` Xi Ruoyao [this message]
2024-04-10  9:40     ` LIU Hao
2024-04-10  9:44       ` Xi Ruoyao
2024-04-10  9:51         ` LIU Hao
2024-04-10  9:52           ` Xi Ruoyao
2024-04-10 10:07             ` LIU Hao
2024-04-10 10:17               ` Xi Ruoyao
2024-04-10 10:03           ` AW: " stefan
2024-04-10 10:34             ` Xi Ruoyao
2024-04-10  9:24   ` stefan
2024-04-10  9:49     ` stefan
2024-04-10  9:54       ` Xi Ruoyao
2024-04-10  9:57       ` LIU Hao
2024-04-10 10:03         ` Xi Ruoyao
2024-04-10 11:52     ` David Brown
2024-04-10 14:25       ` Stefan Franke
2024-04-10 16:51         ` David Brown
2024-04-11  0:32         ` Oleg Endo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5379650ad2582ae97c40ef1a78ded354033d2f71.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=amonakov@ispras.ru \
    --cc=gcc-help@gcc.gnu.org \
    --cc=stefan@franke.ms \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).