Skip to content

Commit 8e2641a

Browse files
authored
[RISCV] Add ORC_B to SimplifyDemandedBitsForTargetNode. (#141975)
1 parent dce490e commit 8e2641a

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20591,7 +20591,8 @@ void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2059120591
// control value of 7 is equivalent to brev8 and orc.b.
2059220592
Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
2059320593
bool IsGORC = Op.getOpcode() == RISCVISD::ORC_B;
20594-
// To compute zeros, we need to invert the value and invert it back after.
20594+
// To compute zeros for ORC_B, we need to invert the value and invert it
20595+
// back after. This inverting is harmless for BREV8.
2059520596
Known.Zero =
2059620597
~computeGREVOrGORC(~Known.Zero.getZExtValue(), 7, IsGORC);
2059720598
Known.One = computeGREVOrGORC(Known.One.getZExtValue(), 7, IsGORC);
@@ -20741,19 +20742,24 @@ bool RISCVTargetLowering::SimplifyDemandedBitsForTargetNode(
2074120742
unsigned BitWidth = OriginalDemandedBits.getBitWidth();
2074220743

2074320744
switch (Op.getOpcode()) {
20744-
case RISCVISD::BREV8: {
20745+
case RISCVISD::BREV8:
20746+
case RISCVISD::ORC_B: {
2074520747
KnownBits Known2;
20748+
bool IsGORC = Op.getOpcode() == RISCVISD::ORC_B;
20749+
// For BREV8, we need to do BREV8 on the demanded bits.
20750+
// For ORC_B, any bit in the output demandeds all bits from the same byte.
20751+
// So we need to do ORC_B on the demanded bits.
2074620752
APInt DemandedBits =
2074720753
APInt(BitWidth, computeGREVOrGORC(OriginalDemandedBits.getZExtValue(),
20748-
7, /*IsGORC=*/false));
20754+
7, IsGORC));
2074920755
if (SimplifyDemandedBits(Op.getOperand(0), DemandedBits,
2075020756
OriginalDemandedElts, Known2, TLO, Depth + 1))
2075120757
return true;
2075220758

20753-
Known.Zero =
20754-
computeGREVOrGORC(Known2.Zero.getZExtValue(), 7, /*IsGORC=*/false);
20755-
Known.One =
20756-
computeGREVOrGORC(Known2.One.getZExtValue(), 7, /*IsGORC=*/false);
20759+
// To compute zeros for ORC_B, we need to invert the value and invert it
20760+
// back after. This inverting is harmless for BREV8.
20761+
Known.Zero = ~computeGREVOrGORC(~Known2.Zero.getZExtValue(), 7, IsGORC);
20762+
Known.One = computeGREVOrGORC(Known2.One.getZExtValue(), 7, IsGORC);
2075720763
return false;
2075820764
}
2075920765
}

llvm/test/CodeGen/RISCV/rv32zbb-intrinsic.ll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ define i32 @orcb(i32 %a) nounwind {
1313
ret i32 %tmp
1414
}
1515

16-
; Second and+or are redundant with the first, make sure we remove it.
16+
; Second and+or are redundant with the first, make sure we remove one of the
17+
; ands and one of the ors.
1718
define i32 @orcb_knownbits(i32 %a) nounwind {
1819
; RV32ZBB-LABEL: orcb_knownbits:
1920
; RV32ZBB: # %bb.0:
2021
; RV32ZBB-NEXT: lui a1, 1044480
2122
; RV32ZBB-NEXT: and a0, a0, a1
22-
; RV32ZBB-NEXT: lui a1, 2048
23-
; RV32ZBB-NEXT: addi a1, a1, 1
24-
; RV32ZBB-NEXT: or a0, a0, a1
23+
; RV32ZBB-NEXT: lui a1, 4080
2524
; RV32ZBB-NEXT: orc.b a0, a0
25+
; RV32ZBB-NEXT: addi a1, a1, 255
26+
; RV32ZBB-NEXT: or a0, a0, a1
2627
; RV32ZBB-NEXT: ret
2728
%tmp = and i32 %a, 4278190080 ; 0xFF000000
2829
%tmp2 = or i32 %tmp, 8388609 ; 0x800001

llvm/test/CodeGen/RISCV/rv64zbb-intrinsic.ll

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ define signext i32 @orcb32_knownbits(i32 signext %a) nounwind {
2929
; RV64ZBB: # %bb.0:
3030
; RV64ZBB-NEXT: lui a1, 1044480
3131
; RV64ZBB-NEXT: and a0, a0, a1
32-
; RV64ZBB-NEXT: lui a1, 2048
33-
; RV64ZBB-NEXT: addi a1, a1, 1
34-
; RV64ZBB-NEXT: or a0, a0, a1
32+
; RV64ZBB-NEXT: lui a1, 4080
3533
; RV64ZBB-NEXT: orc.b a0, a0
34+
; RV64ZBB-NEXT: addi a1, a1, 255
35+
; RV64ZBB-NEXT: or a0, a0, a1
3636
; RV64ZBB-NEXT: sext.w a0, a0
3737
; RV64ZBB-NEXT: ret
3838
%tmp = and i32 %a, 4278190080 ; 0xFF000000
@@ -54,19 +54,20 @@ define i64 @orcb64(i64 %a) nounwind {
5454
ret i64 %tmp
5555
}
5656

57-
; Second and+or is redundant with the first, make sure we remove them.
57+
; Second and+or is redundant with the first, make sure we remove one of the ands
58+
; and one of the ors.
5859
define i64 @orcb64_knownbits(i64 %a) nounwind {
5960
; RV64ZBB-LABEL: orcb64_knownbits:
6061
; RV64ZBB: # %bb.0:
6162
; RV64ZBB-NEXT: lui a1, 65535
62-
; RV64ZBB-NEXT: lui a2, 256
63+
; RV64ZBB-NEXT: lui a2, 4080
6364
; RV64ZBB-NEXT: slli a1, a1, 12
64-
; RV64ZBB-NEXT: addiw a2, a2, 8
65+
; RV64ZBB-NEXT: addiw a2, a2, 255
6566
; RV64ZBB-NEXT: and a0, a0, a1
66-
; RV64ZBB-NEXT: slli a1, a2, 42
67+
; RV64ZBB-NEXT: slli a1, a2, 40
68+
; RV64ZBB-NEXT: orc.b a0, a0
6769
; RV64ZBB-NEXT: add a1, a2, a1
6870
; RV64ZBB-NEXT: or a0, a0, a1
69-
; RV64ZBB-NEXT: orc.b a0, a0
7071
; RV64ZBB-NEXT: ret
7172
%tmp = and i64 %a, 1099494850560 ; 0x000000ffff000000
7273
%tmp2 = or i64 %tmp, 4611721202800525320 ; 0x4000200000100008

0 commit comments

Comments
 (0)