https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89029
Bug ID: 89029 Summary: __builtin_constant_p erroneously true Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pdimov at gmail dot com Target Milestone: --- In the following code: ``` inline bool assert_static_check( bool ev ) { return __builtin_constant_p( ev ) && !ev; } struct X1 { int v1_; }; inline bool operator==( X1 const& x1, X1 const& x2 ) { return x1.v1_ == x2.v1_; } int compare( X1 s1, X1 s2 ) { return assert_static_check( s1 == s2 ); } struct X2 { int v1_; int v2_; }; inline bool operator==( X2 const& x1, X2 const& x2 ) { return x1.v1_ == x2.v1_ && x1.v2_ == x2.v2_; } int compare( X2 s1, X2 s2 ) { return assert_static_check( s1 == s2 ); } ``` the generated code for the first compare function is, correctly, ``` compare(X1, X1): xor eax, eax ret ``` but for the second, it is ``` compare(X2, X2): mov eax, 1 cmp edi, esi je .L6 ret .L6: sar rdi, 32 sar rsi, 32 xor eax, eax cmp edi, esi setne al ret ``` which is incorrect, as `s1 == s2` isn't a constant here. (g++ -std=c++17 -O3, https://godbolt.org/z/nn0-4k) (This is simplified from an attempt to create a statically-diagnosed assert facility that would warn when the asserted expression is known to be false: https://godbolt.org/z/i5SXSd. Would've been cool if it worked.)