http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47004

Geert Bosch <bosch at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bosch at gcc dot gnu.org

--- Comment #1 from Geert Bosch <bosch at gcc dot gnu.org> 2010-12-18 19:22:13 
UTC ---
The compiler gets really close in being able to optimize this testcase.
In fact, adding an extra always-true expression, as I did in lenzero4,
causes the compiler to optimize correctly.

int lenzero3 (int f, int l) {
  return (l < f ? 0 == 0 : l - f + 1 == 0);
}
int lenzero4 (int f, int l) {
  return (l < f ? 0 == 0 : (l - f >= 0) && (l - f + 1 == 0));
}

The first generates:
_lenzero3:
0000000000000040    cmpl    %edi,%esi
0000000000000042    movl    $0x00000001,%eax
0000000000000047    jl    0x00000053
0000000000000049    subl    %edi,%esi
000000000000004b    xorl    %eax,%eax
000000000000004d    cmpl    $0xff,%esi
0000000000000050    sete    %al
0000000000000053    repz/ret

The second:
_lenzero4:
0000000000000060    xorl    %eax,%eax
0000000000000062    cmpl    %edi,%esi
0000000000000064    setl    %al
0000000000000067    ret

The extra condition (l - f >= 0), provides the compiler with the right
hint to do the optimization. It can prove this added condition is always
true, and it then can derive that the condition (l - f + 1 == 0) is
always false.

So, the compiler *can* prove l < f implies l - f >= 0, it just doesn't
know it should prove it in order to optimize the expression in lenzero3.

  -Geert

Reply via email to