-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
@llvm/pr-subscribers-backend-risc-v Author: Craig Topper (topperc) ChangesFull diff: https://github.com/llvm/llvm-project/pull/141898.diff 3 Files Affected:
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 0a849f49116ee..f6cdd0d30aaf7 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -20575,8 +20575,7 @@ void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
bool IsGORC = Op.getOpcode() == RISCVISD::ORC_B;
// To compute zeros, we need to invert the value and invert it back after.
- Known.Zero =
- ~computeGREVOrGORC(~Known.Zero.getZExtValue(), 7, IsGORC);
+ Known.Zero = ~computeGREVOrGORC(~Known.Zero.getZExtValue(), 7, IsGORC);
Known.One = computeGREVOrGORC(Known.One.getZExtValue(), 7, IsGORC);
break;
}
@@ -20717,6 +20716,35 @@ 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;
+
+ // To compute zeros, we need to invert the value and invert it back after.
+ 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 {
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index 78f2044ba83a7..ed27e1ea5ef6d 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -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,
diff --git a/llvm/test/CodeGen/RISCV/bswap-bitreverse.ll b/llvm/test/CodeGen/RISCV/bswap-bitreverse.ll
index 40a5772142345..1afb03b346a1a 100644
--- a/llvm/test/CodeGen/RISCV/bswap-bitreverse.ll
+++ b/llvm/test/CodeGen/RISCV/bswap-bitreverse.ll
@@ -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
|
@@ -20575,8 +20575,7 @@ void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, | |||
Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); | |||
bool IsGORC = Op.getOpcode() == RISCVISD::ORC_B; | |||
// To compute zeros, we need to invert the value and invert it back after. | |||
Known.Zero = | |||
~computeGREVOrGORC(~Known.Zero.getZExtValue(), 7, IsGORC); | |||
Known.Zero = ~computeGREVOrGORC(~Known.Zero.getZExtValue(), 7, IsGORC); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this line re-formatted? I don't see any change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. I must have done something by accident when I copied this code into the end of my new code.
OriginalDemandedElts, Known2, TLO, Depth + 1)) | ||
return true; | ||
|
||
// To compute zeros, we need to invert the value and invert it back after. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not getting this comment. This just a bit reverse in byte, if we have xxx0xx00, that's just 0xxx00xx isn't it? With the Zero form of that being 000F00FF, the result is still F000FF00 right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment was copied from computeKnownBits. It's needed for ORC_B, but not BREV8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it and removed the comment. I'll put it back when I add ORC_B as a follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.