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

Missed optimizations for comparison with multiplications #37612

Open davidbolvansky opened 5 years ago

davidbolvansky commented 5 years ago
Bugzilla Link 38264
Version trunk
OS Linux
CC @LebedevRI,@RKSimon,@nikic,@rotateright

Extended Description

Hello,

int f2(int x, int y, int z) { int tmp = y--; if (tmp x > x y) return 1; return 0; }

Current codegen: f2(int, int, int): # @​f2(int, int, int) lea ecx, [rsi - 1] imul esi, edi imul ecx, edi xor eax, eax cmp esi, ecx setg al ret

GCC: f2(int, int, int): xor eax, eax test edi, edi setg al ret

///////////////////// int f2(int x, int y, int z) { if ((y-1) x > x y) return 1; return 0; }

f2(int, int, int): # @​f2(int, int, int) lea ecx, [rsi - 1] imul ecx, edi imul esi, edi xor eax, eax cmp ecx, esi setg al ret

GCC: f2(int, int, int): mov eax, edi shr eax, 31 ret

For unsigned case - Clang: f2(unsigned int, unsigned int, unsigned int): # @​f2(unsigned int, unsigned int, unsigned int) lea ecx, [rsi - 1] imul ecx, edi imul esi, edi xor eax, eax cmp ecx, esi seta al ret

GCC (one multiplication is eliminated): f2(unsigned int, unsigned int, unsigned int): sub esi, 1 xor eax, eax imul esi, edi add esi, edi setc al ret

nikic commented 3 years ago

The fold from the last comment regressed in main because it's not poison-safe.

davidbolvansky commented 5 years ago

int test(int x, int n) { if (x) return n * x;

return 0;

}

Clang: test(int, int): # @​test(int, int) mov eax, esi imul eax, edi test edi, edi cmove eax, edi ret

GCC: _Z4testii: mov eax, edi imul eax, esi ret