From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28942 invoked by alias); 27 Oct 2015 06:34:40 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 28884 invoked by uid 48); 27 Oct 2015 06:34:35 -0000 From: "eggert at gnu dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/68110] __builtin_sub_overflow unsigned performance issue Date: Tue, 27 Oct 2015 06:34:00 -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: 5.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: minor X-Bugzilla-Who: eggert at gnu dot org 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: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-10/txt/msg02202.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68110 --- Comment #3 from Paul Eggert --- (In reply to Andrew Pinski from comment #2) > So the question is does anyone use this function without "a - b" later on? Not that I know of. The usual pattern for callers of the Gnulib macro is to use the macro to check whether there's overflow, and then to subtract if the result wouldn't overflow. So you're correct that my example is a microbenchmark and is not a good representative. That being said, and I'm just thinking aloud here, it may be helpful for GCC to try "a < b" as an alternative way to test for unsigned subtraction overflow, or conversely to have GCC use __builtin_sub_overflow as an alternative way to compute "a < b". Here's a slightly-more-realistic example: unsigned long sub1 (unsigned long a, unsigned long b) { if (a < b) return b - a; else return a - b; } unsigned long sub2 (unsigned long a, unsigned long b) { unsigned long c; if (__builtin_sub_overflow (a, b, &c)) return b - a; else return c; } On x86-64, gcc -O2 -S generates this: sub1: movq %rsi, %rdx movq %rdi, %rax subq %rdi, %rdx subq %rsi, %rax cmpq %rsi, %rdi cmovb %rdx, %rax ret sub2: movq %rdi, %rax subq %rsi, %rax cmpq %rdi, %rax ja .L13 rep ret .L13: movq %rsi, %rax subq %rdi, %rax ret The two functions have the same behavior. Presumably one implementation is better than the other, and could be used for both sub1 and sub2.