[llvm-bugs] [Bug 46809] New: Missed optimization to use the carry flag after subtracting

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 22 10:19:40 PDT 2020


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

            Bug ID: 46809
           Summary: Missed optimization to use the carry flag after
                    subtracting
           Product: libraries
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: josephcsible at gmail.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, spatel+llvm at rotateright.com

Consider these two C functions:

unsigned f1(unsigned x, unsigned y) {
    unsigned z = x - y;
    if (x < y) {
        z += 100;
    }
    return z;
}
unsigned f2(unsigned x, unsigned y) {
    unsigned z;
    if (__builtin_usub_overflow(x, y, &z)) {
        z += 100;
    }
    return z;
}

At -O3 or -Os, they both generate the same assembly:

        movl    %edi, %eax
        movl    %edi, %ecx
        subl    %esi, %ecx
        addl    $100, %ecx
        subl    %esi, %eax
        cmovbl  %ecx, %eax
        retq

https://godbolt.org/z/MevEM9

That's suboptimal. They should have instead used lea to save a move, save a
duplicate subtraction, and avoid clobbering the carry flag, like this:

        subl    %esi, %edi
        leal    100(%rdi), %eax
        cmovael %edi, %eax
        retq

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200722/f430c500/attachment-0001.html>


More information about the llvm-bugs mailing list