llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
26.85k stars 11.02k forks source link

Modulo to cmov conversion #40600

Open davidbolvansky opened 5 years ago

davidbolvansky commented 5 years ago
Bugzilla Link 41255
Version trunk
OS Linux

Extended Description

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?

llvmbot commented 5 days ago

@llvm/issue-subscribers-backend-x86

Author: Dávid Bolvanský (davidbolvansky)

| | | | --- | --- | | Bugzilla Link | [41255](https://llvm.org/bz41255) | | Version | trunk | | OS | Linux | ## Extended Description 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?