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

            Bug ID: 91881
           Summary: Value range knowledge of higher bits not used in
                    optimizations
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:

unsigned long long sample2(unsigned long long m) {
    if (m >= 100) __builtin_unreachable();
    m *= 16;
    return m >> 3;
}

After the `if` statement we do know that the higher bits are set to 0. So
instead of generating the following assembly:

sample2(unsigned long long):
  mov rax, rdi
  sal rax, 4
  shr rax, 3
  ret

A more optimal assembly could be generated:

sample2(unsigned long long):
  lea rax, [rdi + rdi]
  ret


Godbolt playground: https://godbolt.org/z/1iSpTh

P.S.: that optimization is important for std::to_chars(..., double) like
functions, where a significant of a double is extracted into an unsigned long
long variable, so its upper bits are always zero.

Reply via email to