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

            Bug ID: 85237
           Summary: missed optimisation opportunity for large/negative
                    shifts
           Product: gcc
           Version: 8.0.1
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vegard.nossum at oracle dot com
                CC: segher at gcc dot gnu.org
  Target Milestone: ---

Input:

int f(int x)
{
    return 100 >> (10000 * (x == 1));
}

With -O3 I get:

f(int):
  cmpl $1, %edi
  movl $100, %edx
  movl $0, %eax
  cmovne %edx, %eax
  ret

However, (x == 1) must always be 0, since the shift would be too large (and
cause UB) otherwise. Clang is able to see this and always outputs:

f(int): # @f(int)
  movl $100, %eax
  retq

I believe a similar example would be simply:

int f(int x)
{
    return 100 >> (INT_MAX * x);
}

where again, the only valid (non-UB) value for x is 0.

Reply via email to