Skip to content

Commit 0e28fd7

Browse files
authored
[RISCV] Check the types are the same for folding (sub 0, (setcc x, 0, setlt)) to (sra x, xlen - 1) (#158179)
We should check the type of x is the same as `sub` operation. Otherwise the shift amount xlen -1 will exceed the bit size of x. Fixes #158121.
1 parent 7ee4909 commit 0e28fd7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15827,7 +15827,8 @@ static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
1582715827
SDValue N1 = N->getOperand(1);
1582815828
// fold (sub 0, (setcc x, 0, setlt)) -> (sra x, xlen - 1)
1582915829
if (isNullConstant(N0) && N1.getOpcode() == ISD::SETCC && N1.hasOneUse() &&
15830-
isNullConstant(N1.getOperand(1))) {
15830+
isNullConstant(N1.getOperand(1)) &&
15831+
N1.getValueType() == N1.getOperand(0).getValueType()) {
1583115832
ISD::CondCode CCVal = cast<CondCodeSDNode>(N1.getOperand(2))->get();
1583215833
if (CCVal == ISD::SETLT) {
1583315834
SDLoc DL(N);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s
3+
4+
define i64 @f(ptr %p) {
5+
; CHECK-LABEL: f:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: lb a0, 0(a0)
8+
; CHECK-NEXT: srai a0, a0, 63
9+
; CHECK-NEXT: ret
10+
%load = load i8, ptr %p, align 1
11+
%conv1 = zext i8 %load to i32
12+
%cmp = icmp ult i32 127, %conv1
13+
%conv2 = zext i1 %cmp to i32
14+
%sub = sub nsw i32 0, %conv2
15+
%conv3 = sext i32 %sub to i64
16+
ret i64 %conv3
17+
}

0 commit comments

Comments
 (0)