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

[CodeGen][ARM] Missed optimizated load store pair instruction combine #133048

Open
hstk30-hw opened this issue Mar 26, 2025 · 8 comments
Open

[CodeGen][ARM] Missed optimizated load store pair instruction combine #133048

hstk30-hw opened this issue Mar 26, 2025 · 8 comments

Comments

@hstk30-hw
Copy link
Contributor

https://godbolt.org/z/db17KEWK1

typedef struct tagContext {
    unsigned long long int a;
    unsigned long long int b;
    unsigned long long int c;
} Context ;



void test(Context *context) {
    context->a = 0;
    context->b = 0;
    context->c = 0;
}

GCC output:

test:
        movs    r2, #0
        movs    r3, #0
        strd    r2, [r0]
        strd    r2, [r0, #8]
        strd    r2, [r0, #16]
        bx      lr

llvm output:

test:
        mov     r1, #0
        str     r1, [r0]
        str     r1, [r0, #4]
        str     r1, [r0, #8]
        str     r1, [r0, #12]
        str     r1, [r0, #16]
        str     r1, [r0, #20]
        bx      lr

ARMLoadStoreOptimizer Pass have Pre/post register allocation

@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/issue-subscribers-backend-arm

Author: None (hstk30-hw)

https://godbolt.org/z/db17KEWK1
typedef struct tagContext {
    unsigned long long int a;
    unsigned long long int b;
    unsigned long long int c;
} Context ;



void test(Context *context) {
    context->a = 0;
    context->b = 0;
    context->c = 0;
}

GCC output:

test:
        movs    r2, #<!-- -->0
        movs    r3, #<!-- -->0
        strd    r2, [r0]
        strd    r2, [r0, #<!-- -->8]
        strd    r2, [r0, #<!-- -->16]
        bx      lr

llvm output:

test:
        mov     r1, #<!-- -->0
        str     r1, [r0]
        str     r1, [r0, #<!-- -->4]
        str     r1, [r0, #<!-- -->8]
        str     r1, [r0, #<!-- -->12]
        str     r1, [r0, #<!-- -->16]
        str     r1, [r0, #<!-- -->20]
        bx      lr

ARMLoadStoreOptimizer Pass have Pre/post register allocation

@davemgreen
Copy link
Collaborator

GCC might be defaulting to -mthumb, which would allow it to use STRD on llvm: https://godbolt.org/z/cfKzYnPfh. Otherwise the A32 instruction requires consecutive registers so it more difficult to use.

@hstk30-hw
Copy link
Contributor Author

GCC after 9.3.0 default to -mthumb possibly. In 8.5.0 can still use STRD.
https://godbolt.org/z/4aE1d3dhj

@hstk30-hw
Copy link
Contributor Author

Hi, why llvm generate

strd    r1, r1, [r0]

I see the STRD need consecutive registers. Thumb not have this limit? I can't find thumb's document.

https://developer.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/STRD--register-

@davemgreen
Copy link
Collaborator

The immediate version has T1 and A1 versions: https://developer.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/STRD--immediate-

Yeah - the arm instruction needs consecutive instructions, the thumb variant does not. The register allocator does not know that it is beneficial to duplicate the mov to make use of the strd instruction.

Have you considered using -mthumb? Or is there a reason you cannot use it? The extra code-density itself can be good benefit. We should maybe be considering changing the default to -mthumb to match GCC.

@hstk30-hw
Copy link
Contributor Author

Emm, I can use -mthumb, but not sure how the performance.
In fact, I'm doing some performance improve work in A32, then find this difference between GCC : )

@pinskia
Copy link

pinskia commented Mar 26, 2025

GCC after 9.3.0 default to -mthumb possibly. In 8.5.0 can still use STRD. https://godbolt.org/z/4aE1d3dhj

Note it is not GCC defaults that change but rather godbolt's configuration of gcc that changed between the versions.

@hstk30-hw
Copy link
Contributor Author

Thanks for the correction.

Note it is not GCC defaults that change but rather godbolt's configuration of gcc that changed between the versions.

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