Skip to content

[MacroFusion][RISCV] Make commutable instructions fusible as possible #85214

@wangpc-pp

Description

@wangpc-pp

In #82738, the user raised an issue about commutable instructions.
I have created a PR (#82751) to fix it, but it comes another problem: what if the μ-arch can fuse the instruction pair only if rs1 matches?
For example, for slli+add fusion (see https://godbolt.org/z/xo6xEoPKe):

slli r0, r0, 1
add r0, r0, r1 // fusible

slli r0, r0, 1
add r0, r1, r0 // not fusible

The μ-arch may not check rs2, but add is commutable, we can't guarantee that r0 will always be in rs1.
In InstCombine, we have InstCombinerImpl::SimplifyAssociativeOrCommutative:

/// This performs a few simplifications for operators that are associative or
/// commutative:
///
///  Commutative operators:
///
///  1. Order operands such that they are listed from right (least complex) to
///     left (most complex).  This puts constants before unary operators before
///     binary operators.

But do we need or have a way in backend to change the operands order of second instruction when it's commutable?
cc @dtcxzyw @topperc

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