From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C48D4385802B; Tue, 24 Aug 2021 08:33:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C48D4385802B From: "vincent-gcc at vinc17 dot net" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 4.1.1 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: vincent-gcc at vinc17 dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Aug 2021 08:33:07 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D30484 --- Comment #16 from Vincent Lef=C3=A8vre -= -- 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 addit= ion 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.=