This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Use register allocation hints to improve use of compressed instructions.
ClosedPublic

Authored by craig.topper on Nov 17 2022, 4:00 PM.

Details

Summary

Compressed instructions usually require one of the source registers
to also be the source register. The register allocator doesn't have
that bias on its own.

This patch adds register allocation hints to introduce this bias.
I've started with ADDI, ADDIW, and SLLI. These all have a 5-bit
field for the register. If the source and dest register are the
same they are guaranteed to compress as long as the immediate is
also 6 bits.

This code was inspired by similar code from the SystemZ target.

Diff Detail

Event Timeline

craig.topper created this revision.Nov 17 2022, 4:00 PM
craig.topper requested review of this revision.Nov 17 2022, 4:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 17 2022, 4:00 PM

I was assuming this would never increase code size but that didn't seem to be the case. Since it wasn't clear to me why that would be, I reduced a problematic case and the conclusion is... it's because it's incomplete. The new register allocation sometimes ends up being unfavorable for the compressible instructions that are not yet handled (obvious in hindsight). I presume that once this code is complete that will either go away or be much more unlikely. Still, even as it is, the typical result is to improve code size so it's a nice improvement!

I suggest fleshing out the compressibility test a bit more.

reames accepted this revision.Nov 21 2022, 8:52 AM

LGTM, basic premise make sense.

I'm wondering if we should go even further. For out of order cores with renaming, defaulting to reuse the source register if legal seems like something we could always do. I could see this being a problem for a core without renaming, do we have any such?

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
431

I believe Use is actually User here.

This revision is now accepted and ready to land.Nov 21 2022, 8:52 AM
craig.topper added inline comments.Nov 21 2022, 11:01 PM
llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
431

I'm going to use MI. It's just an instruction that has the register as an operand either as a Def or a Use.

Rename variable

Use operand iterator instead of instruction iterator. I'm not sure what order the instruction order
visits things. Since we're most interested in specific operands, using operand iterator made more
sense to me.

Reuse variable

This revision was landed with ongoing or failed builds.Nov 25 2022, 8:40 AM
This revision was automatically updated to reflect the committed changes.