[Bug middle-end/87654] New: Information about constants from condition is not propagated

antoshkka at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Oct 19 09:51:00 GMT 2018


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

            Bug ID: 87654
           Summary: Information about constants from condition is not
                    propagated
           Product: gcc
           Version: 9.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 following example:

unsigned align_func1(bool big, unsigned value) {
    const unsigned mx_ = (big ? 1 << 14 : 1 << 12);
    return value / mx_ * mx_;
}

It is equivalent to the following code:

unsigned align_func2(bool big, unsigned value) {
    if (big) {
        const unsigned mx_ = 1 << 14;
        return value / mx_ * mx_;
    }

    const unsigned mx_ = 1 << 12;
    return value / mx_ * mx_;
}

Assembly for the align_func2 seems optimal:
  mov eax, esi
  and esi, -4096
  and eax, -16384
  test dil, dil
  cmove eax, esi
  ret

While the assembly for the first function is far from optimal:
align_func1(bool, unsigned int):
  cmp dil, 1
  mov eax, esi
  sbb ecx, ecx
  xor edx, edx
  and ecx, -12288
  add ecx, 16384
  div ecx         ; <=== too bad
  imul eax, ecx
  ret


More information about the Gcc-bugs mailing list