[llvm-bugs] [Bug 41255] New: Modulo to cmov conversion

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 27 03:46:16 PDT 2019


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

            Bug ID: 41255
           Summary: Modulo to cmov conversion
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

Hi,

int g = 0;
void cnt1(int n) {
    g++;
    g %= n;
}

void cnt2(int n) {
    g++;
    if (g==n) g=0;
}

int main(int argc, char**argv) {
    int e = atoi(argv[1]);
    for (int i = 0; i < e; ++i) {
        cnt1(7);
    }
    printf("%d", g);
}


CNT1 version on Haswell with -O3:
time ./a.out 65384546
3
real    0m0.294s
user    0m0.294s
sys     0m0.000s

CNT1 with GCC-9 -O3 is much faster. -O3 -fno-unroll-loops for Clang doesn't fix
it:
time ./a.out 65384546
3
real    0m0.247s
user    0m0.247s
sys     0m0.000s


GCC:
.L9:
        lea     eax, [rsi+1]
        add     ecx, 1
        movsx   rsi, eax
        cdq
        imul    rsi, rsi, -1840700269
        shr     rsi, 32
        add     esi, eax
        sar     esi, 2
        sub     esi, edx
        lea     edx, [0+rsi*8]
        sub     edx, esi
        sub     eax, edx
        mov     esi, eax
        cmp     ecx, edi
        jne     .L9
        mov     DWORD PTR g[rip], eax

Clang:
.LBB2_1:                           
        lea     ecx, [rsi + 1]
        movsxd  rcx, ecx
        imul    rcx, rcx, -1840700269
        shr     rcx, 32
        lea     ecx, [rcx + rsi]
        add     ecx, 1
        mov     edx, ecx
        shr     edx, 31
        sar     ecx, 2
        add     ecx, edx
        lea     edx, [8*rcx]
        sub     ecx, edx
        lea     esi, [rsi + rcx]
        add     esi, 1
        add     eax, -1
        jne     .LBB2_1
        mov     dword ptr [rip + g], esi

Small differences in codegen, I don't see a reason why this code is
slower/faster.

CNT2 version is the best (unrolling and generated cmov in Clang helps a lot,
GCC is slower since it does not use cmov):
time ./a.out 65384546
3
real    0m0.031s
user    0m0.031s
sys     0m0.000s

Maybe is it worth to do this transformation?

-- 
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/20190327/fa3870a7/attachment.html>


More information about the llvm-bugs mailing list