This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize multiplication with immediates
ClosedPublic

Authored by benshi001 on Apr 2 2023, 2:45 AM.

Details

Summary

The optimization of (mul x, c) to (ADD (SLLI x, i0), (SLLI x, i1))
is only enabled for i32 multiplication on rv64, maybe due
to the regression in i64 multiplication on rv32.

However we can change the condition to that c should only
be used once, then the above regression can also be avoided,
while ohter chances of optimization can be enabled.

Diff Detail

Event Timeline

benshi001 created this revision.Apr 2 2023, 2:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 2 2023, 2:45 AM
benshi001 requested review of this revision.Apr 2 2023, 2:45 AM

Please write a description

benshi001 added inline comments.Apr 2 2023, 2:51 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
15259

The reason why there is regression in the case the sub target has the M extension and the data size >= XLen, is that the immediate is used twice for i64 mul on rv32.

If we change the restriction from VT.getSizeInBits() >= Subtarget.getXLen() to ConstNode->hasOneUse(), then the regression introduced by the above situation can be avoided, but other chances of optimization can be enabled.

Please write a description

I have commented for my reason, do you think it would be better to write it to my commit message ?

benshi001 edited the summary of this revision. (Show Details)Apr 2 2023, 3:10 AM
benshi001 edited the summary of this revision. (Show Details)
benshi001 edited the summary of this revision. (Show Details)Apr 2 2023, 3:14 AM

ping ...

The optimization of (mul x, c) to (ADD (SLLI x, i0), (SLLI x, i1)) is only enabled for i32 multiplication on rv64, is due to the regression of i64 multiplication on rv32.

we can change the condition to that c should only be used once, then the above regression can also be avoided, and ohter chances of optimization can be enabled.

This revision is now accepted and ready to land.Apr 14 2023, 10:11 PM
This revision was automatically updated to reflect the committed changes.