This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Return true in hasBitTest when Zbs is enabled and update BEXTI pattern for resulting canonicalisation
ClosedPublic

Authored by asb on Aug 9 2022, 3:49 AM.

Details

Summary

As the Zbs extension includes bext[i] for bit extract, we can
unconditionally return true from this hook. This hook causes the DAG
combiner to perform the following canonicalisation:

and (not (srl X, C)), 1 --> (and X, 1<<C) == 0
and (srl (not X), C)), 1 --> (and X, 1<<C) == 0

As simply changing the hook causes a codegen regression, this patch also
modifies a BEXTI pattern to match this canonicalised form.

As BSETINVMask is now used for BEXT as well as BSET and BINV, it has
been renamed to the more generic SingleBitSetMask.

There is one codegen change in bittest.ll for bittest_31_i64 (NOT+BEXTI
rather than NOT+SRLIW). This is neutral in terms of code quality.

Diff Detail

Event Timeline

asb created this revision.Aug 9 2022, 3:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2022, 3:49 AM
asb requested review of this revision.Aug 9 2022, 3:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2022, 3:49 AM
reames requested changes to this revision.Aug 9 2022, 8:09 AM
reames added inline comments.
llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
896

I think this needs to use BSETINVMask. If I follow the existing logic right, we're preferring and/xor for immediate masks which fit within 12 bits.

902

This pattern looks to be matching the old canonicalization, but oddly, not using the BINV.

This revision now requires changes to proceed.Aug 9 2022, 8:09 AM
craig.topper added inline comments.Aug 9 2022, 8:44 AM
llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
896

BINVI just toggles the bit in place. It doesn’t produce 0 or 1.

902

We can’t use binvi. It doesn’t return a 0 or 1 value.

asb planned changes to this revision.Aug 9 2022, 8:48 AM
asb added inline comments.
llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
902

Oops, that's a silly mistake on my part. I'll respin this patch.

asb updated this revision to Diff 451384.Aug 10 2022, 1:45 AM
asb marked 4 inline comments as done.
asb edited the summary of this revision. (Show Details)

Fix error in first version of this patch, update patch description.

asb retitled this revision from [RISCV] Return true in hasBitTest for Zbs and add new BINVI pattern for resulting canonicalisation to [RISCV] Return true in hasBitTest when Zbs is enabled and update BEXTI pattern for resulting canonicalisation.Aug 10 2022, 1:46 AM
reames accepted this revision.Aug 10 2022, 7:31 AM

LGTM

Glancing at the pattern, I find myself wondering if we can generalize further. You have:

def : Pat<(seteq (and GPR:$rs1, SingleBitSetMask:$mask), 0),
          (BEXTI (XORI GPR:$rs1, -1), SingleBitSetMask:$mask)>;

Could we do something like:

def : Pat<(and GPR:$rs1, SingleBitSetMask:$mask),
          (BEXTI GPR:$rs1, SingleBitSetMask:$mask)>;

Follow up only to be clear.

This revision is now accepted and ready to land.Aug 10 2022, 7:31 AM

LGTM

Glancing at the pattern, I find myself wondering if we can generalize further. You have:

def : Pat<(seteq (and GPR:$rs1, SingleBitSetMask:$mask), 0),
          (BEXTI (XORI GPR:$rs1, -1), SingleBitSetMask:$mask)>;

Could we do something like:

def : Pat<(and GPR:$rs1, SingleBitSetMask:$mask),
          (BEXTI GPR:$rs1, SingleBitSetMask:$mask)>;

Follow up only to be clear.

That does not work. BEXTI returns 0 or 1. An And keeps the bit in the same place.

LGTM

Glancing at the pattern, I find myself wondering if we can generalize further. You have:

def : Pat<(seteq (and GPR:$rs1, SingleBitSetMask:$mask), 0),
          (BEXTI (XORI GPR:$rs1, -1), SingleBitSetMask:$mask)>;

Could we do something like:

def : Pat<(and GPR:$rs1, SingleBitSetMask:$mask),
          (BEXTI GPR:$rs1, SingleBitSetMask:$mask)>;

Follow up only to be clear.

That does not work. BEXTI returns 0 or 1. An And keeps the bit in the same place.

Doh. And the same mistake I already made once in this review as well. Oops.

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