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

            Bug ID: 89360
           Summary: GCC doesn't emit movcc instruction in some cases
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: SztfG at yandex dot ru
  Target Milestone: ---
            Target: x86_64-linux-gnu

Here are two function:

void sort2_ternary(int a, int b, int *pa, int *pb)
{
  *pa = a < b ? a : b;
  *pb = a < b ? b : a;
}

void sort2_if(int a, int b, int *pa, int *pb)
{
  if (a < b) {
    *pa = a;
    *pb = b;
  }
  else
  {
    *pa = b;
    *pb = a;
  }
}

GCC does not emit CMOVcc (conditional move) on GCC 8.2 if we compile it as C++
code

https://godbolt.org/z/ytE0Ix

sort2_ternary(int, int, int*, int*):
        cmp     edi, esi
        jge     .L2
        mov     eax, edi
        mov     edi, esi
        mov     esi, eax
.L2:
        mov     DWORD PTR [rdx], esi
        mov     DWORD PTR [rcx], edi
        ret
sort2_if(int, int, int*, int*):
        cmp     edi, esi
        jge     .L5
        mov     DWORD PTR [rdx], edi
        mov     DWORD PTR [rcx], esi
        ret
.L5:
        mov     DWORD PTR [rdx], esi
        mov     DWORD PTR [rcx], edi
        ret

but if compile it as C code, sort2_ternary have MOVcc:

sort2_ternary:
        cmp     esi, edi
        mov     eax, edi
        cmovle  eax, esi
        cmovl   esi, edi
        mov     DWORD PTR [rdx], eax
        mov     DWORD PTR [rcx], esi
        ret

but sort2_if remains same.


On GCC trunk there is no difference between compiling this as C or C++ for
sort2_ternary(), but sort2_if() still doesn't have CMOVcc instruction in it for
both case.


Measuring cmov vs branch-mov performance: https://github.com/xiadz/cmov

Reply via email to