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

            Bug ID: 115095
           Summary: [missed optimization] fixed processing on constant
                    string
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yann at droneaud dot fr
  Target Milestone: ---

Created attachment 58208
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58208&action=edit
source code of https://godbolt.org/z/Meqvbj8GG

I've found clang was able to compute the result of hashing a constant string at
compile time. I would have hope GCC -O3 would be able to optimize such
computation as well:

    static inline unsigned int hash(unsigned int h, const char *s)
    {
        while (*s) {
            h += *s;
            h *= *s++;
        }

        return h;
    }

    #define LOCATION() hash(hash(0, __FILE__), __func__)

    unsigned int location(void)
    {
        return LOCATION();
    }

is translated by clang to

    location:
        movl    $1418535820, %eax
        retq

but not by GCC, which doesn't compute the value of LOCATION() at compile time
but emit code that compute the value at runtime.

At first, I thought it was an issue with handling __FILE__ or __func__, but
trying with other string constants, GCC is not computing the value at compile
time.

See https://godbolt.org/z/Meqvbj8GG

Reply via email to