Skip to content

Commit b57ba8e

Browse files
authored
[RISCV] Use APInt in useInversedSetcc to prevent crashes when mask is larger than UINT64_MAX. (#81888)
There are no checks that the type is legal so we need to handle any type.
1 parent fc027e1 commit b57ba8e

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15000,8 +15000,8 @@ static SDValue useInversedSetcc(SDNode *N, SelectionDAG &DAG,
1500015000
ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1500115001
if (CC == ISD::SETEQ && LHS.getOpcode() == ISD::AND &&
1500215002
isa<ConstantSDNode>(LHS.getOperand(1)) && isNullConstant(RHS)) {
15003-
uint64_t MaskVal = LHS.getConstantOperandVal(1);
15004-
if (isPowerOf2_64(MaskVal) && !isInt<12>(MaskVal))
15003+
const APInt &MaskVal = LHS.getConstantOperandAPInt(1);
15004+
if (MaskVal.isPowerOf2() && !MaskVal.isSignedIntN(12))
1500515005
return DAG.getSelect(DL, VT,
1500615006
DAG.getSetCC(DL, CondVT, LHS, RHS, ISD::SETNE),
1500715007
False, True);

llvm/test/CodeGen/RISCV/condops.ll

+51
Original file line numberDiff line numberDiff line change
@@ -3719,3 +3719,54 @@ entry:
37193719
%cond = select i1 %tobool.not, i64 0, i64 %x
37203720
ret i64 %cond
37213721
}
3722+
3723+
; Test that we don't crash on types larger than 64 bits.
3724+
define i64 @single_bit3(i80 %x, i64 %y) {
3725+
; RV32I-LABEL: single_bit3:
3726+
; RV32I: # %bb.0: # %entry
3727+
; RV32I-NEXT: lw a0, 8(a0)
3728+
; RV32I-NEXT: slli a0, a0, 31
3729+
; RV32I-NEXT: srai a3, a0, 31
3730+
; RV32I-NEXT: and a0, a3, a1
3731+
; RV32I-NEXT: and a1, a3, a2
3732+
; RV32I-NEXT: ret
3733+
;
3734+
; RV64I-LABEL: single_bit3:
3735+
; RV64I: # %bb.0: # %entry
3736+
; RV64I-NEXT: slli a1, a1, 63
3737+
; RV64I-NEXT: srai a0, a1, 63
3738+
; RV64I-NEXT: and a0, a0, a2
3739+
; RV64I-NEXT: ret
3740+
;
3741+
; RV64XVENTANACONDOPS-LABEL: single_bit3:
3742+
; RV64XVENTANACONDOPS: # %bb.0: # %entry
3743+
; RV64XVENTANACONDOPS-NEXT: andi a1, a1, 1
3744+
; RV64XVENTANACONDOPS-NEXT: vt.maskc a0, a2, a1
3745+
; RV64XVENTANACONDOPS-NEXT: ret
3746+
;
3747+
; RV64XTHEADCONDMOV-LABEL: single_bit3:
3748+
; RV64XTHEADCONDMOV: # %bb.0: # %entry
3749+
; RV64XTHEADCONDMOV-NEXT: slli a1, a1, 63
3750+
; RV64XTHEADCONDMOV-NEXT: srai a0, a1, 63
3751+
; RV64XTHEADCONDMOV-NEXT: and a0, a0, a2
3752+
; RV64XTHEADCONDMOV-NEXT: ret
3753+
;
3754+
; RV32ZICOND-LABEL: single_bit3:
3755+
; RV32ZICOND: # %bb.0: # %entry
3756+
; RV32ZICOND-NEXT: lw a0, 8(a0)
3757+
; RV32ZICOND-NEXT: andi a3, a0, 1
3758+
; RV32ZICOND-NEXT: czero.eqz a0, a1, a3
3759+
; RV32ZICOND-NEXT: czero.eqz a1, a2, a3
3760+
; RV32ZICOND-NEXT: ret
3761+
;
3762+
; RV64ZICOND-LABEL: single_bit3:
3763+
; RV64ZICOND: # %bb.0: # %entry
3764+
; RV64ZICOND-NEXT: andi a1, a1, 1
3765+
; RV64ZICOND-NEXT: czero.eqz a0, a2, a1
3766+
; RV64ZICOND-NEXT: ret
3767+
entry:
3768+
%and = and i80 %x, 18446744073709551616 ; 1 << 64
3769+
%tobool.not = icmp eq i80 %and, 0
3770+
%cond = select i1 %tobool.not, i64 0, i64 %y
3771+
ret i64 %cond
3772+
}

0 commit comments

Comments
 (0)