This is an archive of the discontinued LLVM Phabricator instance.

[RISCV]Optimize (riscvisd::select_cc x, 0, ne, x, 1)
ClosedPublic

Authored by liaolucy on Mar 15 2023, 1:32 AM.

Details

Summary

This patch reduces the number of unpredictable branches.

Diff Detail

Event Timeline

liaolucy created this revision.Mar 15 2023, 1:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2023, 1:32 AM
liaolucy requested review of this revision.Mar 15 2023, 1:32 AM
craig.topper added inline comments.Mar 15 2023, 1:53 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10914

This comment mixes SelectionDAG names and instruction names. Can we use SelectionDAG throughout?

10918

Use seteq LHS, 0. That’s canonical.

liaolucy updated this revision to Diff 505470.Mar 15 2023, 6:24 AM
liaolucy retitled this revision from [RISCV]Optimize select_cc x, 0, ne, x, 1 to [RISCV]Optimize (riscvisd::select_cc x, 0, ne, x, 1).

Address comments, thanks

craig.topper added inline comments.Mar 15 2023, 8:56 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10914

We need to handle the commuted case iscvisd::select_cc x, 0, eq, 1, x as well.

10917

I think you need to do
LHS = DAG.getFreeze(LHS);

to make this safe for undefined values.

liaolucy updated this revision to Diff 505682.Mar 15 2023, 7:14 PM

Address comments

craig.topper added inline comments.Mar 15 2023, 7:17 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10919

Are there any tests for the SETEQ case?

liaolucy added inline comments.Mar 15 2023, 7:29 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
10919

I do not find LLVM IR that would cover the SETEQ case, perhaps a mir could be constructed?

%c = call i64 @llvm.umax.i64(i64 %a, i64 1)
%c = call i64 @llvm.umax.i64(i64 1, i64 %a)

After Initial selection DAG, both are the same DAG. So both will be SETNE

craig.topper accepted this revision.Mar 15 2023, 7:36 PM

LGTM

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

Ok. I think maybe it gets adjusted in LowerSelect to put the constant in the false operand.

This revision is now accepted and ready to land.Mar 15 2023, 7:36 PM
This revision was landed with ongoing or failed builds.Mar 15 2023, 7:56 PM
This revision was automatically updated to reflect the committed changes.