public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: <stefan@franke.ms>
To: <gcc-help@gcc.gnu.org>
Subject: AW: optimizer discards sign information
Date: Wed, 10 Apr 2024 12:03:05 +0200	[thread overview]
Message-ID: <01a301da8b2e$4805d830$d8118890$@franke.ms> (raw)
In-Reply-To: <2e2c1430-edfa-42ed-8713-cff56a2de64d@126.com>



> -----Ursprüngliche Nachricht-----
> Von: LIU Hao <lh_mouse@126.com>
> Gesendet: Mittwoch, 10. April 2024 11:51
> An: Xi Ruoyao <xry111@xry111.site>; Alexander Monakov
> <amonakov@ispras.ru>; stefan@franke.ms
> Cc: gcc-help@gcc.gnu.org
> Betreff: Re: optimizer discards sign information
> 
> 在 2024-04-10 17:44, Xi Ruoyao 写道:
> > 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.
> 
> Nobody in this thread has been thinking it's wrong code.
> 
> When there is no overflow, it's missed optimization. When there is an
> overflow, I don't care.
> 
> Do you agree?
> 
> 
> --
> Best regards,
> LIU Hao


Yes, there is an overflow when the value gets assigned to x

	u32 x = *a * *b;

And after that line of code, x is a valid unsigned int, no matter what value was assigned. And the compiler must not throw away that unsignedness.

Also an add can overflow:

u64 faa(int a, int b) {
    u32 x = a + b;
    u64 r = x;

And in this case the optimizer doesn't discard the variable x

With the multiplication the optimizer kills it:

int _5;

  _5 = _2 * _4;
  x_9 = (u32) _5;
  r_10 = (u64) x_9;

becomes:

  int _5;

  _5 = _2 * _4;
  r_10 = (u64) _5;

The cast to (u32) is an important information, which IMHO must not be discarded.


Regards

Stefan



  parent reply	other threads:[~2024-04-10 10:03 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
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           ` stefan [this message]
2024-04-10 10:34             ` AW: " 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='01a301da8b2e$4805d830$d8118890$@franke.ms' \
    --to=stefan@franke.ms \
    --cc=gcc-help@gcc.gnu.org \
    /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).