[Bug tree-optimization/126763] New: a?__builtin_clz(a):__builtin_clz(31) -> clz(a|1)

"pinskia at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126763

            Bug ID: 126763
           Summary: a?__builtin_clz(a):__builtin_clz(31) -> clz(a|1)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int f(unsigned a)
{
    if (a)
      return __builtin_clz(a);
    return __builtin_clz(1);
}

int f0(unsigned a)
{
    return __builtin_clz(a|1);
}
int n(unsigned a)
{
    return __builtin_clzg(a,__builtin_clz(1)+1);
}
```

This all have well defined code and all return the same thing. f0 produces the
best results; 31 (assuming `sizeof(int)==4`).

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114032#c3 mentions we could add
support for CLZ for other than zero values than CLZ_VALUE_DEFINED_AT_ZERO, that
might be a good idea. For 31 (or 63) we could special case that in expand as
being `a|1` which should be able to support the value always.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.