From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 070973858C66; Mon, 6 Mar 2023 19:32:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 070973858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678131135; bh=zEFFA58rrhvBEo3ovGe+xdUT+HZmhYi/vRtE1keTo10=; h=From:To:Subject:Date:From; b=W7m+l+JM1gHHSjGwqXVDCFAdKK2N73SreLbxxG9BpomDZnUcsDUVwYehydn4gz+fD G6TrAD4VW+H92qq/SdK7CfYY85euWSDasPzIboHqLgAwUovwXbHUMGJLNvjxbSvuGR pGV40KboMChm3eOE84uleI0Jl4EQhfy00TipHtv8= From: "drepper.fsp+rhbz at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/109045] New: assume attribute and std::optional do not mix Date: Mon, 06 Mar 2023 19:32:14 +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: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: drepper.fsp+rhbz 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=3D109045 Bug ID: 109045 Summary: assume attribute and std::optional do not mix Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: drepper.fsp+rhbz at gmail dot com Target Milestone: --- The assume attribute is meant to help expressing more complex assumptions w= hich involve function calls. Given that interfaces should use std::optional when the semantics matches this should mean code like this should be optimized: #include std::optional g(long); long f(long a) { auto r =3D g(a); [[assume(!r || *r > 0)]]; return r.value_or(0) / 2; } The generated code should use the unsigned divide by two method but it does not. With today's gcc trunk version: 0000000000000000 <_Z1fl>: 0: 48 83 ec 18 sub $0x18,%rsp 4: e8 00 00 00 00 call 9 <_Z1fl+0x9> 9: 48 89 04 24 mov %rax,(%rsp) d: 48 89 54 24 08 mov %rdx,0x8(%rsp) 12: 31 c0 xor %eax,%eax 14: 80 7c 24 08 00 cmpb $0x0,0x8(%rsp) 19: 74 11 je 2c <_Z1fl+0x2c> 1b: 48 8b 14 24 mov (%rsp),%rdx 1f: 48 89 d0 mov %rdx,%rax 22: 48 c1 e8 3f shr $0x3f,%rax 26: 48 01 d0 add %rdx,%rax 29: 48 d1 f8 sar %rax 2c: 48 83 c4 18 add $0x18,%rsp 30: c3 ret=20=20=20=20 The instructions from 1f to 28 including are not needed (and the initial lo= ad at 1b adjusted).=