This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add isel pattern to optimize (mul (and X, 0xffffffff), (and Y, 0xffffffff)) on RV64
ClosedPublic

Authored by craig.topper on Mar 20 2021, 12:44 PM.

Details

Summary

This patterns computes the full 64 bit product of a 32x32 unsigned
multiply. This requires a two pairs of SLLI+SRLI to zero the
upper 32 bits of the inputs.

We can do better than this by using two SLLI to move the lower
bits to the upper bits then use MULHU to compute the product. This
is the high half of a full 64x64 product. Since we put 32 0s in the lower
bits of the inputs we know the 128-bit product will have zeros in the
lower 64 bits. So the upper 64 bits, which MULHU computes, will contain
the original 64 bit product we were after.

The same trick would work for (mul (sext_inreg X, i32), (sext_inreg Y, i32)) using MULHS, but sext_inreg
is sext.w which is already one instruction so we wouldn't save anything.

Diff Detail

Event Timeline

craig.topper created this revision.Mar 20 2021, 12:44 PM
craig.topper requested review of this revision.Mar 20 2021, 12:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2021, 12:44 PM
Herald added a subscriber: MaskRay. · View Herald Transcript

I think the final line of your commit message is the wrong way round?

craig.topper edited the summary of this revision. (Show Details)Mar 20 2021, 12:57 PM

I think the final line of your commit message is the wrong way round?

Maybe it was poorly written. How does it look now?

I think the final line of your commit message is the wrong way round?

Maybe it was poorly written. How does it look now?

Ah that makes more sense, I misunderstood what your point was

jrtc27 accepted this revision.Mar 20 2021, 1:01 PM
This revision is now accepted and ready to land.Mar 20 2021, 1:01 PM

I think the final line of your commit message is the wrong way round?

Maybe it was poorly written. How does it look now?

Ah that makes more sense, I misunderstood what your point was

I guess maybe we shouldn’t do this with Zba which has zext.w. Unless we know the ‘and’ is only used by the mul.