Page MenuHomePhabricator

[RISCV] Teach translateSetCCForBranch to help improve constant materialization.
Changes PlannedPublic

Authored by craig.topper on Jul 25 2022, 12:05 PM.

Details

Summary

InstCombine and DAGCombine both canonicalize setge X, C to
setgt X, C-1. C might be easier to materialize than C.

This patch teaches translateSetCCForBranch to reverse the canonicalize
if it will give us a single 'addi' or 'lui' to materialize the constant.

This was inspired by https://github.com/llvm/llvm-project/issues/56666
and wondering if we should do something like getAArch64Cmp.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 25 2022, 12:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 25 2022, 12:05 PM
craig.topper requested review of this revision.Jul 25 2022, 12:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 25 2022, 12:05 PM
craig.topper planned changes to this revision.Jul 25 2022, 12:56 PM

Need to guard against adding 1 to INT64_MIN. Though I find it unlikely that would occur.

reames requested changes to this revision.Jul 25 2022, 2:53 PM
reames added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1409

Please land the refactor to introduce the switch and rebase.

1417

Can't we generalize this to be if RISCVMatInt::getIntMatCost(C+1) < RISCVMatInt::getIntMatCost(C)?

craig.topper added inline comments.Jul 26 2022, 12:58 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1417

I was trying to be a little conservative if the constant required multiple instructions and was used by other instructions. Changing it could introduce multiple slightly different constants. I figured creating an LUI or ADDI wasn't as bad.

Maybe I should check one use and/or opaque constant?

reames added inline comments.Jul 27 2022, 1:02 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1417

I'd be fine with either the one use check, or a comment that says there deliberately isn't a one-use check and that's why this is restricted to the single instruction forms.

I think you could also restrict the single instruction case in terms of the getIntMatCost interface too, but that's not required.