Skip to content

Commit 41c5221

Browse files
committed
[ValueTracking] Add support for vector_reduce_{s,u}{min,max} in computeKnownBits
Previously missing. We compute by just applying the reduce function on the knownbits of each element. Closes llvm#88169
1 parent 77d6684 commit 41c5221

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,14 @@ static void computeKnownBitsFromOperator(const Operator *I,
16211621
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
16221622
Known = KnownBits::ssub_sat(Known, Known2);
16231623
break;
1624+
// for min/max reduce, any bit common to each element in the input vec
1625+
// is set in the output.
1626+
case Intrinsic::vector_reduce_umax:
1627+
case Intrinsic::vector_reduce_umin:
1628+
case Intrinsic::vector_reduce_smax:
1629+
case Intrinsic::vector_reduce_smin:
1630+
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1631+
break;
16241632
case Intrinsic::umin:
16251633
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
16261634
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);

llvm/test/Transforms/InstCombine/vector-reduce-min-max-known.ll

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ define i1 @vec_reduce_smin_non_zero_fail(<4 x i8> %xx) {
130130

131131
define i8 @vec_reduce_umax_known0(<4 x i8> %xx) {
132132
; CHECK-LABEL: @vec_reduce_umax_known0(
133-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1, i8 1>
134-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]])
135-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
136-
; CHECK-NEXT: ret i8 [[R]]
133+
; CHECK-NEXT: ret i8 1
137134
;
138135
%x = or <4 x i8> %xx, <i8 1, i8 1, i8 1, i8 1>
139136
%v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x)
@@ -182,10 +179,7 @@ define i8 @vec_reduce_umax_known_fail1(<4 x i8> %xx) {
182179

183180
define i8 @vec_reduce_umin_known0(<4 x i8> %xx) {
184181
; CHECK-LABEL: @vec_reduce_umin_known0(
185-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1, i8 1>
186-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umin.v4i8(<4 x i8> [[X]])
187-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
188-
; CHECK-NEXT: ret i8 [[R]]
182+
; CHECK-NEXT: ret i8 1
189183
;
190184
%x = or <4 x i8> %xx, <i8 1, i8 1, i8 1, i8 1>
191185
%v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x)
@@ -235,10 +229,7 @@ define i8 @vec_reduce_umin_known_fail1(<4 x i8> %xx) {
235229

236230
define i8 @vec_reduce_smax_known(<4 x i8> %xx) {
237231
; CHECK-LABEL: @vec_reduce_smax_known(
238-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 4, i8 4, i8 4, i8 5>
239-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smax.v4i8(<4 x i8> [[X]])
240-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 4
241-
; CHECK-NEXT: ret i8 [[R]]
232+
; CHECK-NEXT: ret i8 4
242233
;
243234
%x = or <4 x i8> %xx, <i8 4, i8 4, i8 4, i8 5>
244235
%v = call i8 @llvm.vector.reduce.smax(<4 x i8> %x)
@@ -261,10 +252,7 @@ define i8 @vec_reduce_smax_known_fail(<4 x i8> %xx) {
261252

262253
define i8 @vec_reduce_smin_known(<4 x i8> %xx) {
263254
; CHECK-LABEL: @vec_reduce_smin_known(
264-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 8, i8 24, i8 56, i8 9>
265-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smin.v4i8(<4 x i8> [[X]])
266-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 8
267-
; CHECK-NEXT: ret i8 [[R]]
255+
; CHECK-NEXT: ret i8 8
268256
;
269257
%x = or <4 x i8> %xx, <i8 8, i8 24, i8 56, i8 9>
270258
%v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x)

0 commit comments

Comments
 (0)