Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X86] and/or/xor could use 8-bit immediate for more constant arguments #134474

Open
dzaima opened this issue Apr 5, 2025 · 1 comment
Open

[X86] and/or/xor could use 8-bit immediate for more constant arguments #134474

dzaima opened this issue Apr 5, 2025 · 1 comment

Comments

@dzaima
Copy link

dzaima commented Apr 5, 2025

The functions:

void test_or(long* dst, long x) {
    *dst = x | 200;
}
void test_and(long* dst, long x) {
    *dst = x & -200;
}
void test_xor(long* dst, long x) {
    *dst = x ^ 200;
}

for x86-64 with -O3 (or even -Oz) compile to:

or  rsi, 0xc8               ; 48 81 ce c8 00 00 00
and rsi, 0xffffffffffffff38 ; 48 81 e6 38 ff ff ff
xor rsi, 0xc8               ; 48 81 f6 c8 00 00 00

whereas they could be:

or  sil, 0xc8 ; 40 80 ce c8
and sil, 0x38 ; 40 80 e6 38
xor sil, 0xc8 ; 40 80 f6 c8

This apply:

  • for | for an immediate of 128..255;
  • for & with an immediate of -256..-129; (x & -256 can furthermore be xor sil,sil)
  • for ^ with an immediate of 128..255.

(uops.info says that the (R8l, I8) and (R64, I32) variants in question are largely equivalent)

compiler explorer

@dzaima dzaima changed the title [X86] and/or could use 8-bit immediate for more constant arguments [X86] and/or/xor could use 8-bit immediate for more constant arguments Apr 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 5, 2025

@llvm/issue-subscribers-backend-x86

Author: dzaima (dzaima)

The functions: ```c void test_or(long* dst, long x) { *dst = x | 200; } void test_and(long* dst, long x) { *dst = x & -200; } void test_xor(long* dst, long x) { *dst = x ^ 200; } ``` for x86-64 with `-O3` (or even `-Oz`) compile to: ```asm or rsi, 0xc8 ; 48 81 ce c8 00 00 00 and rsi, 0xffffffffffffff38 ; 48 81 e6 38 ff ff ff xor rsi, 0xc8 ; 48 81 f6 c8 00 00 00 ``` whereas they could be: ```asm or sil, 0xc8 ; 40 80 ce c8 and sil, 0x38 ; 40 80 e6 38 xor sil, 0xc8 ; 40 80 f6 c8 ``` This apply: - for `|` for an immediate of 128..255; - for `&` with an immediate of -256..-129; (`x & -256` can furthermore be `xor sil,sil`) - for `^` with an immediate of 128..255.

(uops.info says that the (R8l, I8) and (R64, I32) variants in question are largely equivalent)

compiler explorer

@abhishek-kaushik22 abhishek-kaushik22 self-assigned this Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants