https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111036

            Bug ID: 111036
           Summary: Code generation error in handling __builtin_constant_p
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bmei at broadcom dot com
  Target Milestone: ---

Compile and run following code


#include <stdio.h>
#define __align(n) __attribute__((aligned(n)))

__attribute__((aligned(32))) static struct {

    unsigned long long available_cmd_ids_per_core[2];
} _rl2c_cmd_id_data;

static inline void __attribute__((always_inline))
foo (void *base, size_t length)
{
    unsigned long int p = (unsigned long int) base;
    if (__builtin_constant_p(p) && (p & 31) == 0) { printf("constant p &&
aligned to 32\n"); } 
    else if (__builtin_constant_p(length)) { printf("constant length\n");} 
    else { printf("else\n"); }
}

int main(int argc, char **argv)
{
    foo(&_rl2c_cmd_id_data, sizeof(*(&_rl2c_cmd_id_data)));
    return 0;
}


With gcc 12.1.0 & gcc 13.1.0,  I got segmentation fault. With 11.1.0 and below,
I got correct result. I examined the dumped tree IR. In einline pass, a
__builtin_unreachable is inserted for else if/else branches as the compiler
probably thinks __builtin_constant_p(p) & (p&31) is always true. But the later
passes think __builtin_constant_p(p) is always false. Therefore all code are
optimized away.

Reply via email to