This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize immediate materialisation with BCLRI/BSETI
ClosedPublic

Authored by benshi001 on Oct 11 2021, 3:19 AM.

Details

Summary

Do the following optimization for immediate materialisation:

  1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000, first generate the lower 32-bit with Val|0x80000000 (which is expected be an int32), then emit (BCLRI r, 31).
  1. For values in range 0x80000000 ~ 0xffffffff, first generate the lower 32-bit with Val&~0x80000000 (which is expected to be an int32), then emit (BSETI r, 31).

Diff Detail

Event Timeline

benshi001 created this revision.Oct 11 2021, 3:19 AM
benshi001 requested review of this revision.Oct 11 2021, 3:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 11 2021, 3:19 AM
benshi001 updated this revision to Diff 378607.Oct 11 2021, 3:20 AM
benshi001 updated this revision to Diff 378617.Oct 11 2021, 3:48 AM
craig.topper added inline comments.Oct 11 2021, 4:36 AM
llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
191

Can this be Val | 0x80000000? That makes more sense to me since BCLRI only affects 1 bit.

benshi001 updated this revision to Diff 378656.Oct 11 2021, 7:16 AM
benshi001 marked an inline comment as done.Oct 11 2021, 7:18 AM
benshi001 added inline comments.
llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
191

If we change to Val | 0x80000000 instead of Val + 0x80000000,

we still need not to check NewVal != Val, and checking NewVal < 0 && isInt<32>(NewVal) is enough.

Since Val is not int32 while NewVal is.

benshi001 marked an inline comment as done.Oct 11 2021, 7:19 AM
This revision is now accepted and ready to land.Oct 11 2021, 9:39 AM
craig.topper requested changes to this revision.Oct 11 2021, 10:55 AM

Since this affects RISCVMatInt.cpp which is used by the assembler as well. We need MC layer tests like the "li" tests in rv64zba-aliases-valid.s

This revision now requires changes to proceed.Oct 11 2021, 10:55 AM
benshi001 updated this revision to Diff 378864.Oct 11 2021, 8:31 PM
benshi001 added a comment.EditedOct 11 2021, 8:34 PM

Since this affects RISCVMatInt.cpp which is used by the assembler as well. We need MC layer tests like the "li" tests in rv64zba-aliases-valid.s

Thanks. I have updated according to your advice.

  1. MC tests are added in rv64zbs-aliases-valid.s, since BLCRI/BSETI belong to Zbs not Zba.
  1. The similiar optimization is also performed against value in 0x80000000 ~ 0xffffffff, although we already optimized with the zext.w. But zext.w belongs to Zba, considering an extrame case that there is only Zbs but no Zba.
  1. I use Res = TmpSeq instead of directly return TmpSeq to enable further constrast with future optimization with Zba.
benshi001 updated this revision to Diff 378878.Oct 11 2021, 9:58 PM
benshi001 retitled this revision from [RISCV] Optimize immediate materialisation with BCLRI to [RISCV] Optimize immediate materialisation with BCLRI/BSETI.
benshi001 updated this revision to Diff 378970.Oct 12 2021, 4:31 AM

Can also test a constant like 0xffffffff7fffffff which we can now do as ADDI+BCLRI. Similarly test a constant that allows ADDI+BSETI.

Also please add a description to this patch.

benshi001 updated this revision to Diff 379220.Oct 12 2021, 4:51 PM
benshi001 edited the summary of this revision. (Show Details)

Can also test a constant like 0xffffffff7fffffff which we can now do as ADDI+BCLRI. Similarly test a constant that allows ADDI+BSETI.

Thanks. More tests of ADDI+BCLRI and ADDI+BSETI are added, and also, a short description is added.

This revision is now accepted and ready to land.Oct 12 2021, 4:57 PM
This revision was automatically updated to reflect the committed changes.