https://llvm.org/bugs/show_bug.cgi?id=31028

            Bug ID: 31028
           Summary: Too much division
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedb...@nondot.org
          Reporter: fil...@gmail.com
                CC: llvm-bugs@lists.llvm.org
    Classification: Unclassified

Mentioned by Tillmann Karras.

The x86 backend emits too many idiv instructions for this function:

#include <inttypes.h>
int32_t f(int32_t a, int32_t b) {
  if (a % b == 42)
    return a / b;
  return 3;
}

Codegen:
x86-64 (annotated):
patatino(int, int):                          # @patatino(int, int)
        mov     ecx, edi
        mov     eax, ecx
        cdq
        idiv    esi       # This gets us quotient in rax, remainder in rdx
        mov     eax, 3
        cmp     edx, 42
        jne     .LBB0_2
        mov     eax, ecx
        cdq
        idiv    esi       # This is useless since we already had the result
before
.LBB0_2:
        ret

arm64 target (Only one divide. Different trick ("multiply-subtract"
instruction: msub)):
patatino(int, int):                          // @patatino(int, int)
        mov      w8, w0
        sdiv    w0, w8, w1
        msub    w8, w0, w1, w8
        cmp       w8, #42         // =42
        b.eq    .LBB0_2
        orr     w0, wzr, #0x3
.LBB0_2:
        ret

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to