This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Branchless lowering for (select (x < 0), TrueConstant, FalseConstant) and (select (x >= 0), TrueConstant, FalseConstant)
ClosedPublic

Authored by liaolucy on Nov 14 2022, 7:20 AM.

Details

Summary

This patch reduces the number of unpredictable branches

(select (x < 0), y, z) -> x >> (XLEN - 1) & (y - z) + z
(select (x >= 0), y, z) -> x >> (XLEN - 1) & (z - y) + y

Diff Detail

Event Timeline

liaolucy created this revision.Nov 14 2022, 7:20 AM
liaolucy requested review of this revision.Nov 14 2022, 7:20 AM
reames requested changes to this revision.Nov 14 2022, 10:21 AM
reames added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9702

It looks like we can avoid the not here by swapping the operands.

select (x < 0), y, z -> (x >> (XLEN - 1) & (y - z) + z
9705

The code appears out of sync with the comment, not sure which is correct.

The MaskedValueIsZero check here allows rhs to be 0 or 1.

9711

You shouldn't need these casts.

This revision now requires changes to proceed.Nov 14 2022, 10:21 AM
liaolucy added inline comments.Nov 14 2022, 5:27 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9711

Is it open to all constants? There will be some code size increase

craig.topper added inline comments.Nov 14 2022, 6:48 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9711

I think he was referring to the casts to uint64_t inside the isInt calls.. isInt takes a int64_t.

liaolucy updated this revision to Diff 475334.Nov 14 2022, 8:08 PM
liaolucy marked 3 inline comments as done.

Address Philip Reames's comments

craig.topper added inline comments.Nov 16 2022, 9:44 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9702

What about (x > -1), z, y?

liaolucy updated this revision to Diff 476041.Nov 17 2022, 1:10 AM
liaolucy marked an inline comment as done.

Address Craig.topper's comments

Add (select (x >= 0), y, z) -> x >> (XLEN - 1) & (z - y) + y

Add (select (x > -1), y, z) to the testcase

liaolucy retitled this revision from [RISCV] Branchless lowering for select (x < 0), TrueConstant, FalseConstant) to [RISCV] Branchless lowering for (select (x < 0), y, z) and (select (x >= 0), y, z).Nov 17 2022, 1:10 AM
liaolucy edited the summary of this revision. (Show Details)
liaolucy retitled this revision from [RISCV] Branchless lowering for (select (x < 0), y, z) and (select (x >= 0), y, z) to [RISCV] Branchless lowering for (select (x < 0), TrueConstant, FalseConstant) and (select (x >= 0), TrueConstant, FalseConstant).
liaolucy updated this revision to Diff 476809.Nov 20 2022, 11:29 PM

Refactor, it feels clearer

craig.topper added inline comments.Nov 20 2022, 11:51 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9709

Can you share most of the code by adding

if (CCVal == ISD::CondCode::SETGE)
  std::swap(TrueImm, FalseImm)

And another std::swap for TrueV and FalseV if the match succeeds?

liaolucy updated this revision to Diff 476828.Nov 21 2022, 1:45 AM

Address craig.topper's comments, thanks

reames accepted this revision.Nov 22 2022, 8:54 AM

LGTM

This revision is now accepted and ready to land.Nov 22 2022, 8:54 AM
craig.topper requested changes to this revision.Nov 22 2022, 9:09 AM
craig.topper added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9708

If you swap TrueV and FalseV here you need to swap them back at the end.

9724

Was this supposed to be TrueV and FalseV?

This revision now requires changes to proceed.Nov 22 2022, 9:09 AM
reames added inline comments.Nov 22 2022, 9:19 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9724

That's what I read it as... good catch!

liaolucy updated this revision to Diff 477355.Nov 22 2022, 5:34 PM

Update std::swap(TrueSImm, FalseSImm) to std::swap(TrueV, FalseV), thanks.

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9724

Sorry, I've been a bit confused lately.

craig.topper added inline comments.Nov 22 2022, 5:41 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9724

I think this swap needs to only happen if you did the first swap. So you need to check CCVal is SETGE again

liaolucy updated this revision to Diff 477358.Nov 22 2022, 5:50 PM

Check if CCVal is SETGE at the end

This revision is now accepted and ready to land.Nov 24 2022, 11:16 PM