https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109045
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 which involve function calls. Given that interfaces should use std::optional when the semantics matches this should mean code like this should be optimized: #include <optional> std::optional<long> g(long); long f(long a) { auto r = 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 The instructions from 1f to 28 including are not needed (and the initial load at 1b adjusted).