public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug target/30484] INT_MIN % -1 is well defined for -fwrapv Date: Tue, 24 Aug 2021 08:33:07 +0000 [thread overview] Message-ID: <bug-30484-4-icRM353d7C@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-30484-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30484 --- Comment #16 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> --- The issue is that the source code assuming -fno-wrapv may be more complex, thus giving slower generated code. Here's an example, which consists in adding 3 signed integers, for which the user knows that the sum is representable, so that the only issue is a potential integer overflow in the first addition. I've used GCC 11.2.0 on x86_64. With -fwrapv, the integer overflow is well-defined as wrapping, so that the user can write: int f (int a, int b, int c) { return a + b + c; } The generated code with -O3 -fwrapv has 2 instructions (the 2 additions): addl %edx, %esi leal (%rsi,%rdi), %eax But without -fwrapv, one needs to make sure that one doesn't get any integer overflow. Assume that the user knows that there is a single negative number among the 3 integers, so that using this negative number in the first addition will avoid an integer overflow. So the user can write: int f (int a, int b, int c) { if (b < 0) return a + b + c; else return a + c + b; } The generated code with -O3 has 6 instructions: leal (%rdi,%rdx), %eax addl %esi, %edi addl %edx, %edi addl %esi, %eax testl %esi, %esi cmovs %edi, %eax In theory, the compiler could normally optimize to produce the same code as with the source that assumes -fwrapv (here, a + b + c and a + c + b are obviously equivalent on a typical processor), but in practice, this is often not the case as shown above.
next prev parent reply other threads:[~2021-08-24 8:33 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top [not found] <bug-30484-4@http.gcc.gnu.org/bugzilla/> 2021-08-22 23:54 ` vincent-gcc at vinc17 dot net 2021-08-23 19:44 ` joseph at codesourcery dot com 2021-08-23 21:06 ` vincent-gcc at vinc17 dot net 2021-08-24 6:50 ` rguenther at suse dot de 2021-08-24 8:33 ` vincent-gcc at vinc17 dot net [this message] 2021-08-24 8:38 ` rguenther at suse dot de 2021-08-24 8:47 ` vincent-gcc at vinc17 dot net 2021-08-24 8:49 ` vincent-gcc at vinc17 dot net 2023-09-18 7:36 ` [Bug target/30484] INT_MIN % -1 is well defined for -fwrapv but traps on x86 rguenth at gcc dot gnu.org 2023-09-18 8:47 ` rguenth at gcc dot gnu.org 2023-09-18 8:47 ` rguenth at gcc dot gnu.org 2023-09-19 12:21 ` rguenth at gcc dot gnu.org 2023-09-19 12:28 ` rguenth at gcc dot gnu.org
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=bug-30484-4-icRM353d7C@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@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: linkBe 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).