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

            Bug ID: 101240
           Summary: [missed optimization] Transitivity of less-than and
                    less-or-equal
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyrylo.bohdanenko at gmail dot com
  Target Milestone: ---

Consider the following C++ code

#define ALWAYS_TRUE(x) do { if (x) __builtin_unreachable(); } while (false)

int divide(int a, int b) {
  ALWAYS_TRUE(a == b);
  return a / b;
}

void test_array(unsigned (&arr)[3]) {
  ALWAYS_TRUE(a[0] < a[1] && a[1] < a[2]);
  return a[0] < a[2];
}

The first function is optimiozed away:

divide(int, int):
        mov     eax, 1
        ret

While the second still does the comparison:

test_array(unsigned int (&) [3]):
        mov     eax, DWORD PTR [rdi+8]
        cmp     DWORD PTR [rdi], eax
        setb    al
        ret

It would be nice if GCC could deduce that 

a < b && b < c --> a < c

And optimize that second function (provided no other UB is involved)

Reply via email to