Skip to content

[RISCV] failure to use mulw for 32 bit multiplication #144422

Closed as not planned
Closed as not planned
@tom-rein

Description

@tom-rein

LLVM doesn't use mulw, except when it otherwise would have to introduce a sext.w.

The following code could use only mulw, but LLVM uses mul instead (godbolt):

int cube(int x) {
    return x * x * x;
}

int squarep1(int x) {
    return x * x + 1;
}

int mulshift(int x) {
    return x * x << 4;
}

void store(int x, int &y) {
    y = x * x;
}

void store(short x, short &y) {
    y = x * x;
}

void store(unsigned char x, unsigned char &y) {
    y = x * x;
}

void store(signed char x, signed char &y) {
    y = x * x;
}

short square16(short x) {
    return x * x;
}

int square(short x) {
    return x * x;
}

unsigned short square16(unsigned short x) {
    return x * x;
}

unsigned char square8(unsigned char x) {
    return x * x;
}

signed char square8(signed char x) {
    return x * x;
}

int square(unsigned char x) {
    return x * x;
}

int square(signed char x) {
    return x * x;
}

long square(unsigned x) {
    return x * x;
}

LLVM generates this:

cube(int):
        mul     a1, a0, a0
        mulw    a0, a1, a0
        ret

squarep1(int):
        mul     a0, a0, a0
        addiw   a0, a0, 1
        ret

mulshift(int):
        mul     a0, a0, a0
        slliw   a0, a0, 4
        ret

store(int, int&):
        mul     a0, a0, a0
        sw      a0, 0(a1)
        ret

store(short, short&):
        mul     a0, a0, a0
        sh      a0, 0(a1)
        ret

store(unsigned char, unsigned char&):
        mul     a0, a0, a0
        sb      a0, 0(a1)
        ret

store(signed char, signed char&):
        mul     a0, a0, a0
        sb      a0, 0(a1)
        ret

square16(short):
        mul     a0, a0, a0
        sext.h  a0, a0
        ret

square(short):
        mul     a0, a0, a0
        ret

square16(unsigned short):
        mul     a0, a0, a0
        zext.h  a0, a0
        ret

square8(unsigned char):
        mul     a0, a0, a0
        zext.b  a0, a0
        ret

square8(signed char):
        mul     a0, a0, a0
        sext.b  a0, a0
        ret

square(unsigned char):
        mul     a0, a0, a0
        ret

square(signed char):
        mul     a0, a0, a0
        ret

square(unsigned int):
        mul     a0, a0, a0
        zext.w  a0, a0
        ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend:RISC-Vmissed-optimizationquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions