Skip to content

Commit 4907e1c

Browse files
committed
[RISCV] Select (and (srl x, c2), c1) as (srli (srai x, c2-c3)).
If c1 is a mask with c3 leading zeros and c3 is larger than c2. Fixes regression reported in llvm#101751.
1 parent 533190a commit 4907e1c

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,31 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
14491449
}
14501450
}
14511451

1452+
// Turn (and (srl x, c2), c1) -> (srli (srai x, c2-c3)) if c1 is a mask with
1453+
// c3 leading zeros and c2 is larger than c3.
1454+
if (N0.getOpcode() == ISD::SRA && isa<ConstantSDNode>(N0.getOperand(1)) &&
1455+
N0.hasOneUse()) {
1456+
unsigned C2 = N0.getConstantOperandVal(1);
1457+
unsigned XLen = Subtarget->getXLen();
1458+
assert((C2 > 0 && C2 < XLen) && "Unexpected shift amount!");
1459+
1460+
SDValue X = N0.getOperand(0);
1461+
1462+
if (isMask_64(C1)) {
1463+
unsigned Leading = XLen - llvm::bit_width(C1);
1464+
if (C2 > Leading) {
1465+
SDNode *SRAI = CurDAG->getMachineNode(
1466+
RISCV::SRAI, DL, VT, X,
1467+
CurDAG->getTargetConstant(C2 - Leading, DL, VT));
1468+
SDNode *SRLI = CurDAG->getMachineNode(
1469+
RISCV::SRLI, DL, VT, SDValue(SRAI, 0),
1470+
CurDAG->getTargetConstant(Leading, DL, VT));
1471+
ReplaceNode(Node, SRLI);
1472+
return;
1473+
}
1474+
}
1475+
}
1476+
14521477
// If C1 masks off the upper bits only (but can't be formed as an
14531478
// ANDI), use an unsigned bitfield extract (e.g., th.extu), if
14541479
// available.

llvm/test/CodeGen/RISCV/lack-of-signed-truncation-check.ll

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@
2424
define i1 @shifts_necmp_i16_i8(i16 %x) nounwind {
2525
; RV32I-LABEL: shifts_necmp_i16_i8:
2626
; RV32I: # %bb.0:
27-
; RV32I-NEXT: lui a1, 16
28-
; RV32I-NEXT: addi a1, a1, -1
29-
; RV32I-NEXT: and a2, a0, a1
27+
; RV32I-NEXT: slli a1, a0, 16
28+
; RV32I-NEXT: srli a1, a1, 16
3029
; RV32I-NEXT: slli a0, a0, 24
31-
; RV32I-NEXT: srai a0, a0, 24
32-
; RV32I-NEXT: and a0, a0, a1
33-
; RV32I-NEXT: xor a0, a0, a2
30+
; RV32I-NEXT: srai a0, a0, 8
31+
; RV32I-NEXT: srli a0, a0, 16
32+
; RV32I-NEXT: xor a0, a0, a1
3433
; RV32I-NEXT: snez a0, a0
3534
; RV32I-NEXT: ret
3635
;
3736
; RV64I-LABEL: shifts_necmp_i16_i8:
3837
; RV64I: # %bb.0:
39-
; RV64I-NEXT: lui a1, 16
40-
; RV64I-NEXT: addiw a1, a1, -1
41-
; RV64I-NEXT: and a2, a0, a1
38+
; RV64I-NEXT: slli a1, a0, 48
39+
; RV64I-NEXT: srli a1, a1, 48
4240
; RV64I-NEXT: slli a0, a0, 56
43-
; RV64I-NEXT: srai a0, a0, 56
44-
; RV64I-NEXT: and a0, a0, a1
45-
; RV64I-NEXT: xor a0, a0, a2
41+
; RV64I-NEXT: srai a0, a0, 8
42+
; RV64I-NEXT: srli a0, a0, 48
43+
; RV64I-NEXT: xor a0, a0, a1
4644
; RV64I-NEXT: snez a0, a0
4745
; RV64I-NEXT: ret
4846
;

llvm/test/CodeGen/RISCV/signed-truncation-check.ll

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@
2424
define i1 @shifts_eqcmp_i16_i8(i16 %x) nounwind {
2525
; RV32I-LABEL: shifts_eqcmp_i16_i8:
2626
; RV32I: # %bb.0:
27-
; RV32I-NEXT: lui a1, 16
28-
; RV32I-NEXT: addi a1, a1, -1
29-
; RV32I-NEXT: and a2, a0, a1
27+
; RV32I-NEXT: slli a1, a0, 16
28+
; RV32I-NEXT: srli a1, a1, 16
3029
; RV32I-NEXT: slli a0, a0, 24
31-
; RV32I-NEXT: srai a0, a0, 24
32-
; RV32I-NEXT: and a0, a0, a1
33-
; RV32I-NEXT: xor a0, a0, a2
30+
; RV32I-NEXT: srai a0, a0, 8
31+
; RV32I-NEXT: srli a0, a0, 16
32+
; RV32I-NEXT: xor a0, a0, a1
3433
; RV32I-NEXT: seqz a0, a0
3534
; RV32I-NEXT: ret
3635
;
3736
; RV64I-LABEL: shifts_eqcmp_i16_i8:
3837
; RV64I: # %bb.0:
39-
; RV64I-NEXT: lui a1, 16
40-
; RV64I-NEXT: addiw a1, a1, -1
41-
; RV64I-NEXT: and a2, a0, a1
38+
; RV64I-NEXT: slli a1, a0, 48
39+
; RV64I-NEXT: srli a1, a1, 48
4240
; RV64I-NEXT: slli a0, a0, 56
43-
; RV64I-NEXT: srai a0, a0, 56
44-
; RV64I-NEXT: and a0, a0, a1
45-
; RV64I-NEXT: xor a0, a0, a2
41+
; RV64I-NEXT: srai a0, a0, 8
42+
; RV64I-NEXT: srli a0, a0, 48
43+
; RV64I-NEXT: xor a0, a0, a1
4644
; RV64I-NEXT: seqz a0, a0
4745
; RV64I-NEXT: ret
4846
;

0 commit comments

Comments
 (0)