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

            Bug ID: 121479
           Summary: The return value of __builtin_constant_p()  is
                    inconsistent at different optimization levels
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njuwy at smail dot nju.edu.cn
  Target Milestone: ---

gcc version:
gcc version 16.0.0 20250804 (experimental) (GCC) 

Compiler Explorer: https://godbolt.org/z/Gnxfvqvza

src:
#include <math.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int foo(double *data, size_t len) {
    int status = 0;
    double sum = 0.0;
    size_t i = 0;

    while (i < len) {
        double val = data[i];
        if (isinf(val) || isnan(val)) {
            errno = EDOM;
            status = -2;
            break;
        }
        sum += fabs(val);
        i++;
    }
    printf("In foo, __builtin_constant_p(len) = %d\n",
__builtin_constant_p(len));

    return status;
}
int main(void) {
    double values[] = { -1.5, 2.0, -3.25 };
    size_t len = sizeof(values) / sizeof(values[0]);
    int result = foo(values, len);
    printf("In main, __builtin_constant_p(len) = %d\n",
__builtin_constant_p(len));
    return abs(result);
}


issue:
at -O0, -O1, -Os, -O2, -O3
the results of __builtin_constant_p(len) in foo() are:
0,0,0,1,1
the results of __builtin_constant_p(len) in main() are:
0,1,1,1,1

Is this a expected bahavior or miscompilation bug?

Reply via email to