Skip to content

[AArch64] Inefficient loading of some immediates #140787

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

Open
AZero13 opened this issue May 20, 2025 · 7 comments
Open

[AArch64] Inefficient loading of some immediates #140787

AZero13 opened this issue May 20, 2025 · 7 comments

Comments

@AZero13
Copy link
Contributor

AZero13 commented May 20, 2025

https://godbolt.org/z/Yo55Gz8eP

We decompose many immediates, which is a good thing. However, there are some that we decompose that we can just avoid with the mov instruction. This is one such example.

@AZero13 AZero13 changed the title Inefficient loading of some immediates [AArch64] Inefficient loading of some immediates May 20, 2025
@AZero13
Copy link
Contributor Author

AZero13 commented May 20, 2025

https://godbolt.org/z/anGPGEeWh More simplified.

@AZero13
Copy link
Contributor Author

AZero13 commented May 20, 2025

Basically:

check1:
mov w8, #65534
movk w8, #65279, lsl #16
cmp w0, w8
cset w0, hi
ret

But we can do:
check1:
mov w1, -16777217
cmp w0, w1
cset w0, cs
ret

@llvmbot
Copy link
Member

llvmbot commented May 20, 2025

@llvm/issue-subscribers-backend-aarch64

Author: AZero13 (AZero13)

https://godbolt.org/z/Yo55Gz8eP

We decompose many immediates, which is a good thing. However, there are some that we decompose that we can just avoid with the mov instruction. This is one such example.

@pinskia
Copy link

pinskia commented May 20, 2025

Note GCC implemented this in GCC 9 [generic non-aarch64 specific path] (and GCC 15 for the ccmp path):
https://gcc.gnu.org/r9-2428 and https://gcc.gnu.org/r15-4759 .

@efriedma-quic
Copy link
Collaborator

We have code for adjusting condition codes to simplify cmp immediates; see https://github.com/llvm/llvm-project/blob/26d9cb17a6e655993c991b66b21d5c378256d79b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp#L4077 . We currently don't try to do any adjustments unless the adjustment results in a legal cmp immediate.

gcc apparently misses the adjustment for the following.

int check1 (int x)
{
  return x > 0xaa9fff;
}

@pinskia
Copy link

pinskia commented May 20, 2025

gcc apparently misses the adjustment for the following.

int check1 (int x)
{
  return x > 0xaa9fff;
}

I see the issue, the problem is how GCC calculates the cost here. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120372 (fix included which I will be submitting soon).

@AZero13
Copy link
Contributor Author

AZero13 commented May 21, 2025

We have code for adjusting condition codes to simplify cmp immediates; see https://github.com/llvm/llvm-project/blob/26d9cb17a6e655993c991b66b21d5c378256d79b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp#L4077 . We currently don't try to do any adjustments unless the adjustment results in a legal cmp immediate.

gcc apparently misses the adjustment for the following.

int check1 (int x)
{
  return x > 0xaa9fff;
}

fixing that in particular is not an easy task because by the time the ccmp is generated, so is the cmp, which is another bug I meant to report but forgot to.

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

5 participants