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: Minor regression from llvm 14 with single array access and lea #56742

Closed
jaskarth opened this issue Jul 26, 2022 · 5 comments
Closed

X86: Minor regression from llvm 14 with single array access and lea #56742

jaskarth opened this issue Jul 26, 2022 · 5 comments
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@jaskarth
Copy link

jaskarth commented Jul 26, 2022

Description

When arrays are accessed once in a function, llvm14 produces a mov instruction that references the array in memory. However, trunk produces a lea that moves the array into a register, then performs the mov. This change makes sense when the array is accessed multiple times as it removes the extraneous memory reference, but it adds an additional unneeded instruction when there is only a single access of the array.

Code

Compiling with: -O3
Godbolt: https://godbolt.org/z/W4saM9fha

constexpr const static int array[5] = {1, 2, 3, 4, 5};

int get(int a) {
    return array[a];
}

int get2(int a, int b) {
    return array[a] + array[b];
}
trunk codegen
get(int):                                # @get(int)
        movsxd  rax, edi
        lea     rcx, [rip + array]
        mov     eax, dword ptr [rcx + 4*rax]
        ret
get2(int, int):                              # @get2(int, int)
        movsxd  rcx, edi
        lea     rdx, [rip + array]
        movsxd  rax, esi
        mov     eax, dword ptr [rdx + 4*rax]
        add     eax, dword ptr [rdx + 4*rcx]
        ret
llvm 14 codegen
get(int):
        movsxd  rax, edi
        mov     eax, dword ptr [4*rax + array]
        ret
get2(int, int):
        movsxd  rcx, edi
        movsxd  rax, esi
        mov     eax, dword ptr [4*rax + array]
        add     eax, dword ptr [4*rcx + array]
        ret
@topperc
Copy link
Collaborator

topperc commented Jul 26, 2022

The use of rip in the address indicates that we've changed how the address of the array is being computed. Trunk is using a position independent address, but llvm-14 isn't. I'm guessing this is related to -fpie being made the default by https://reviews.llvm.org/D120305

Can you try with -fno-pie on the clang command line?

@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2022

@llvm/issue-subscribers-backend-x86

@jaskarth
Copy link
Author

Oh yep, that does seems to change it to what llvm 14 produces! Would this still qualify as a bug, then?

@topperc
Copy link
Collaborator

topperc commented Jul 26, 2022

Adding @MaskRay

@MaskRay
Copy link
Member

MaskRay commented Aug 30, 2022

This is not a regression. It is a -fno-pic vs -fpie code generation difference. See #57449 (comment) and compiler-explorer/gcc-cross-builder#33

@MaskRay MaskRay closed this as completed Aug 30, 2022
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

5 participants