Skip to content

Commit 923eaea

Browse files
committed
Fix clipr and clipur according to the specification update.
pascalgouedo/cv32e40p@5c9a14d
1 parent 203b720 commit 923eaea

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,12 @@ def CV_HI5: SDNodeXForm<imm, [{
675675
N->getValueType(0));
676676
}]>;
677677

678-
def positiveGPR : ImmLeaf<XLenVT, [{return (uint32_t)Imm >> 31 == 0;}]>;
678+
def positive : PatFrags<(ops node:$value),
679+
[(abs node:$value), (and node:$value, 0x7FFFFFFF),
680+
(lshr (shl node:$value, (XLenVT 1)), (XLenVT 1)]>;
679681
def between : PatFrags<(ops node:$lowerBound, node:$upperBound, node:$value),
680682
[(smin (smax node:$value, node:$lowerBound), node:$upperBound),
681683
(smax (smin node:$value, node:$upperBound), node:$lowerBound)]>;
682-
683684
def betweenu : PatFrags<(ops node:$upperBound, node:$value),
684685
[(smin (smax node:$value, 0), node:$upperBound),
685686
(smax (smin node:$value, node:$upperBound), 0)]>;
@@ -789,11 +790,11 @@ let Predicates = [HasVendorXCValu, IsRV32], AddedComplexity = 1 in {
789790

790791
def : Pat<(clip powerOf2Minus1:$upperBound, (XLenVT GPR:$rs1)),
791792
(CV_CLIP GPR:$rs1, (trailing1sPlus1 imm:$upperBound))>;
792-
def : Pat<(between (not positiveGPR:$rs2), positiveGPR:$rs2, (XLenVT GPR:$rs1)),
793+
def : Pat<(between (not (positive GPR:$rs2)), (positive GPR:$rs2), (XLenVT GPR:$rs1)),
793794
(CV_CLIPR GPR:$rs1, GPR:$rs2)>;
794795
def : Pat<(betweenu powerOf2Minus1:$upperBound, (XLenVT GPR:$rs1)),
795796
(CV_CLIPU GPR:$rs1, (trailing1sPlus1 imm:$upperBound))>;
796-
def : Pat<(betweenu GPR:$rs2, (XLenVT GPR:$rs1)),
797+
def : Pat<(betweenu (positive GPR:$rs2), (XLenVT GPR:$rs1)),
797798
(CV_CLIPUR GPR:$rs1, GPR:$rs2)>;
798799

799800
def : Pat<(sra (add (XLenVT GPR:$rs1), (XLenVT GPR:$rs2)), uimm5:$imm5),

llvm/test/CodeGen/RISCV/xcvalu.ll

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,23 @@ define i32 @clip(i32 %a) {
124124
define i32 @clipr(i32 %a, i32 %b) {
125125
; CHECK-LABEL: clipr:
126126
; CHECK: # %bb.0:
127-
; CHECK-NEXT: cv.abs a1, a1
128-
; CHECK-NEXT: not a0, a1
129-
; CHECK-NEXT: cv.max a0, a1, a0
130-
; CHECK-NEXT: cv.min a0, a0, a1
127+
; CHECK-NEXT: cv.clipr a0, a0, a1
131128
; CHECK-NEXT: ret
132129
%1 = call i32 @llvm.abs.i32(i32 %b, i1 false)
133130
%2 = xor i32 %1, -1
134-
%3 = call i32 @llvm.smax.i32(i32 %1, i32 %2)
131+
%3 = call i32 @llvm.smax.i32(i32 %a, i32 %2)
132+
%4 = call i32 @llvm.smin.i32(i32 %3, i32 %1)
133+
ret i32 %4
134+
}
135+
136+
define i32 @clipr2(i32 %a, i32 %b) {
137+
; CHECK-LABEL: clipr2:
138+
; CHECK: # %bb.0:
139+
; CHECK-NEXT: cv.clipr a0, a0, a1
140+
; CHECK-NEXT: ret
141+
%1 = and i32 %b, 2147483647
142+
%2 = xor i32 %1, -1
143+
%3 = call i32 @llvm.smax.i32(i32 %a, i32 %2)
135144
%4 = call i32 @llvm.smin.i32(i32 %3, i32 %1)
136145
ret i32 %4
137146
}
@@ -151,9 +160,21 @@ define i32 @clipur(i32 %a, i32 %b) {
151160
; CHECK: # %bb.0:
152161
; CHECK-NEXT: cv.clipur a0, a0, a1
153162
; CHECK-NEXT: ret
154-
%1 = call i32 @llvm.smax.i32(i32 %a, i32 0)
155-
%2 = call i32 @llvm.smin.i32(i32 %1, i32 %b)
156-
ret i32 %2
163+
%1 = call i32 @llvm.abs.i32(i32 %b, i1 false)
164+
%2 = call i32 @llvm.smax.i32(i32 %a, i32 0)
165+
%3 = call i32 @llvm.smin.i32(i32 %2, i32 %1)
166+
ret i32 %3
167+
}
168+
169+
define i32 @clipur2(i32 %a, i32 %b) {
170+
; CHECK-LABEL: clipur2:
171+
; CHECK: # %bb.0:
172+
; CHECK-NEXT: cv.clipur a0, a0, a1
173+
; CHECK-NEXT: ret
174+
%1 = and i32 %b, 2147483647
175+
%2 = call i32 @llvm.smax.i32(i32 %a, i32 0)
176+
%3 = call i32 @llvm.smin.i32(i32 %2, i32 %1)
177+
ret i32 %3
157178
}
158179

159180
define i32 @addN(i32 %a, i32 %b) {

0 commit comments

Comments
 (0)