access-softek / llvm-project

Other
0 stars 0 forks source link

Inefficient code for adding two 32bit integers #4

Closed pftbest closed 11 months ago

pftbest commented 5 years ago

This simple program:

long foo(long a, long b) {
    return a + b;
}

Compiles to this strange sequence of instructions:

    add r15, r13
    add r14, r12
    cmp r14, r12
    mov access-softek/msp430-clang#1, r14
    bic r2, r14
    add r14, r13
    ret

While the output from gcc and clang-6.0 is much more reasonable:

        add.w   r14, r12
        addc.w  r15, r13
        ret
asl commented 5 years ago

There were some changes in add-with-carry in mainline... Probably we need to lower them better.

chbessonova commented 5 years ago

As I can see ADDC/SUBC was deprecated and default action was set to expand for them (see details https://reviews.llvm.org/D47422). We need to make them legal for msp430 to return the previous behavior.

pftbest commented 5 years ago

There is also one more case which is not optimal even on old clang:

long foo = 0;
void bar() {
    foo += 1;
}

While on gcc it is fine:

    ADD access-softek/msp430-clang#1, &foo
    ADDC    #0, &foo+2
    RET