This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Branchless lowering for select (and (x , 0x1) == 0), y, (z ^ y) ) and select (and (x , 0x1) == 0), y, (z | y) )
ClosedPublic

Authored by reames on Sep 29 2022, 7:24 AM.

Details

Summary

This code is directly ported from the X86 backend which applies the same rewrite (along with several others). As noted in the test change, we are missing a canonicalization or inversion of this pattern for the != 1 case. Plan to address in following change. Also planning on looking more closely at the other branchless variants from x86 to see if any are worth porting.

Motivation here is the coremark crc8 routine from https://github.com/eembc/coremark/blob/main/core_util.c#L165. This patch significantly reduces the number of unpredictable branches in the workload.

Diff Detail

Event Timeline

reames created this revision.Sep 29 2022, 7:24 AM
reames requested review of this revision.Sep 29 2022, 7:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 29 2022, 7:24 AM
reames updated this revision to Diff 463952.Sep 29 2022, 10:12 AM

Restructure as a post-legalize combine on SELECT_CC. Doing this as a combine on SETCC before legalization turns out to be annoying as the forms for the inverse icmp looks very different in the DAG (select vs setcc, swapped operands, and a truncate instead of an and).

asb accepted this revision.Sep 30 2022, 2:29 AM

Left some tiny nits re formatting, but otherwise this LGTM.

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

clang-format is suggesting some different formatting for this line and the equivalent one in the branch below.

9059

Nit: we => We

9072

clang-format is aligning the comment with the line above for me.

This revision is now accepted and ready to land.Sep 30 2022, 2:29 AM
craig.topper added inline comments.Sep 30 2022, 8:16 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
9026

Use ISD::isIntEqualitySetCC(CCVal)?

9058

unsigned int -> unsigned

This revision was landed with ongoing or failed builds.Sep 30 2022, 8:24 AM
This revision was automatically updated to reflect the committed changes.