Skip to content

[RISCV] Add BREV8 to SimplifyDemandedBitsForTargetNode. #141898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20717,6 +20717,34 @@ unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
return 1;
}

bool RISCVTargetLowering::SimplifyDemandedBitsForTargetNode(
SDValue Op, const APInt &OriginalDemandedBits,
const APInt &OriginalDemandedElts, KnownBits &Known, TargetLoweringOpt &TLO,
unsigned Depth) const {
unsigned BitWidth = OriginalDemandedBits.getBitWidth();

switch (Op.getOpcode()) {
case RISCVISD::BREV8: {
KnownBits Known2;
APInt DemandedBits =
APInt(BitWidth, computeGREVOrGORC(OriginalDemandedBits.getZExtValue(),
7, /*IsGORC=*/false));
if (SimplifyDemandedBits(Op.getOperand(0), DemandedBits,
OriginalDemandedElts, Known2, TLO, Depth + 1))
return true;

Known.Zero =
computeGREVOrGORC(Known2.Zero.getZExtValue(), 7, /*IsGORC=*/false);
Known.One =
computeGREVOrGORC(Known2.One.getZExtValue(), 7, /*IsGORC=*/false);
return false;
}
}

return TargetLowering::SimplifyDemandedBitsForTargetNode(
Op, OriginalDemandedBits, OriginalDemandedElts, Known, TLO, Depth);
}

bool RISCVTargetLowering::canCreateUndefOrPoisonForTargetNode(
SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const {
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class RISCVTargetLowering : public TargetLowering {
const SelectionDAG &DAG,
unsigned Depth) const override;

bool SimplifyDemandedBitsForTargetNode(SDValue Op, const APInt &DemandedBits,
const APInt &DemandedElts,
KnownBits &Known,
TargetLoweringOpt &TLO,
unsigned Depth) const override;

bool canCreateUndefOrPoisonForTargetNode(SDValue Op,
const APInt &DemandedElts,
const SelectionDAG &DAG,
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/RISCV/bswap-bitreverse.ll
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ define i8 @test_bitreverse_i8(i8 %a) nounwind {
;
; RV32ZBKB-LABEL: test_bitreverse_i8:
; RV32ZBKB: # %bb.0:
; RV32ZBKB-NEXT: rev8 a0, a0
; RV32ZBKB-NEXT: slli a0, a0, 24
; RV32ZBKB-NEXT: brev8 a0, a0
; RV32ZBKB-NEXT: srli a0, a0, 24
; RV32ZBKB-NEXT: ret
;
; RV64ZBKB-LABEL: test_bitreverse_i8:
; RV64ZBKB: # %bb.0:
; RV64ZBKB-NEXT: rev8 a0, a0
; RV64ZBKB-NEXT: slli a0, a0, 56
; RV64ZBKB-NEXT: brev8 a0, a0
; RV64ZBKB-NEXT: srli a0, a0, 56
; RV64ZBKB-NEXT: ret
Expand Down
Loading