From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 469A43858C55; Tue, 8 Nov 2022 00:00:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 469A43858C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667865642; bh=Yn/UUfpT9yMHl42/176mvBrurbzveDTpb38+3QYn5bk=; h=From:To:Subject:Date:From; b=s3/QuTj6u9c5dn4bLuzlI4OBfnRpvK4sschaVV7/SFezLrfsTDZQsP7Q5PMWOYtMn 1fniTeLa7HN1cOWagraw4tEpdidGc1vZQRNTMvauHvt+BhDkuBoEXVDL1D6TrOXCOK G7yODiCeMDkxTkrMsXUx6Iz51FLcHRCmh6zwQP90= From: "cassio.neri at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/107564] New: Fail to recognize overflow check for addition of __uint128_t operands Date: Tue, 08 Nov 2022 00:00:40 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: cassio.neri at gmail dot com 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107564 Bug ID: 107564 Summary: Fail to recognize overflow check for addition of __uint128_t operands Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: cassio.neri at gmail dot com Target Milestone: --- Consider: char f128(__uint128_t m, __uint128_t n) { #if !defined(USE_BUILTIN_ADD_OVERFLOW) m +=3D n; return m < n; #else __uint128_t r; return __builtin_add_overflow(m, n, &r); #endif } When USE_BUILTIN_ADD_OVERFLOW is undefined, GCC fails to recognise this is = an overflow check and with -O3 generates this: mov r8, rdi mov rax, rsi mov rdi, rax mov rsi, r8 mov rax, rdx mov rdx, rcx add rsi, rax adc rdi, rcx cmp rsi, rax mov rcx, rdi sbb rcx, rdx setc al ret When USE_BUILTIN_ADD_OVERFLOW is defined, it generates better code but still suboptimal: mov r8, rdi mov rax, rsi mov rsi, r8 mov rdi, rax add rsi, rdx adc rdi, rcx setc al ret For other unsigned integer types GCC generates the same optimal code for bo= th methods. For instance for uint64_t: add rdi, rsi setc al ret https://godbolt.org/z/bj4M5no4j=