Skip to content

[AArch64] Suboptimal code for multiplication by certain constants #89430

Closed
@Kmeakin

Description

@Kmeakin

For some constants, GCC is able to generate sequences of add where LLVM generates mul. I have checked all constants between 1 and 100 (https://godbolt.org/z/rxej44fGj):

For all of the examples below(11, 13, 19, 21, 25, 27, 35, 37, 41, 49, 51, 69, 73, 81, 85), LLVM generates

mulK:
        mov     w8, K
        mul     w0, w0, w8
        ret
mul11:
        add     w1, w0, w0, lsl 2
        add     w0, w0, w1, lsl 1
        ret
mul13:
        add     w1, w0, w0, lsl 1
        add     w0, w0, w1, lsl 2
        ret
mul19:
        add     w1, w0, w0, lsl 3
        add     w0, w0, w1, lsl 1
        ret
mul21:
        add     w1, w0, w0, lsl 2
        add     w0, w0, w1, lsl 2
        ret
mul25:
        add     w0, w0, w0, lsl 2
        add     w0, w0, w0, lsl 2
        ret
mul27:
        add     w0, w0, w0, lsl 1
        add     w0, w0, w0, lsl 3
        ret
mul35:
        add     w1, w0, w0, lsl 4
        add     w0, w0, w1, lsl 1
        ret
mul37:
        add     w1, w0, w0, lsl 3
        add     w0, w0, w1, lsl 2
        ret
mul41:
        add     w1, w0, w0, lsl 2
        add     w0, w0, w1, lsl 3
        ret
mul49:
        add     w1, w0, w0, lsl 1
        add     w0, w0, w1, lsl 4
        ret
mul51:
        add     w0, w0, w0, lsl 1
        add     w0, w0, w0, lsl 4
        ret
mul69:
        add     w1, w0, w0, lsl 4
        add     w0, w0, w1, lsl 2
        ret
mul73:
        add     w1, w0, w0, lsl 3
        add     w0, w0, w1, lsl 3
        ret
mul81:
        add     w0, w0, w0, lsl 3
        add     w0, w0, w0, lsl 3
        ret
mul85:
        add     w0, w0, w0, lsl 2
        add     w0, w0, w0, lsl 4
        ret

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions