Issue 236085
Summary [X86] CRC loop clmul optimization performs extra GPR/XMM moves
Labels new issue
Assignees
Reporter tantei3
    Godbolt link: https://godbolt.org/z/qrx6vW1hY
```c
#include <stdio.h>
#include <stdint.h>

#define POLY 0x9A6C9329AC4BC9B5
uint64_t crc64(uint64_t data, uint64_t crc) {
  for (int i = 0; i < 64; i++) {
      if (((crc & 0x1) ^ (data & 0x1)))
	    crc = (crc >> 1) ^ POLY;
      else
	    crc = (crc >> 1);
      data >>= 1;
    }
  return crc;
}
```
When compiled with `-O2 -march=westmere` this gets expanded to:
```
crc64(unsigned long, unsigned long):
        xor     rdi, rsi
        movq    xmm0, rdi
 movabs  rax, 2876949357237608311
        movq    xmm1, rax
 pclmulqdq       xmm1, xmm0, 0
        movq    rcx, xmm1
        movabs rax, 3808117099328934763
        movq    xmm0, rax
        pclmulqdq xmm0, xmm1, 0
        pextrq  rax, xmm0, 1
        xor     rax, rcx
 ret
```

This seems to include 4 movq and 1 pextrq (and 2 movabs) which is slightly less efficient than GCC:
```
"crc64(unsigned long, unsigned long)":
        movdqa  xmm2, XMMWORD PTR .LC0[rip]
        xor     rsi, rdi
        movq    xmm0, rsi
        pclmulqdq       xmm0, xmm2, 0
 movdqa  xmm1, xmm0
        punpcklqdq      xmm0, xmm0
        pclmulqdq xmm1, xmm2, 16
        pxor    xmm0, xmm1
        pextrq  rax, xmm0, 1
 ret
.LC0:
        .quad   2876949357237608311
        .quad 3808117099328934762
```
It seems to load the 2 constants (movabs in clang) from a 16-byte read-only memory, and performs one movdqa for constants, one movq for input and one pextrq for the output, which seems optimal here.
 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to