Skip to content

Commit 9b0be08

Browse files
committed
[ValueTracking] Implement computeKnownBits for llvm.vector.reduce.{add,mul}
1 parent d883817 commit 9b0be08

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,26 @@ static void computeKnownBitsFromOperator(const Operator *I,
16471647
} else
16481648
Known.One.clearAllBits();
16491649
break;
1650+
case Intrinsic::vector_reduce_mul:
1651+
case Intrinsic::vector_reduce_add:
1652+
// We compute the common bits for all elements then apply the reduce op
1653+
// NumEle times. This is mostly useful for known high zeros.
1654+
if (auto *VecTy =
1655+
dyn_cast<FixedVectorType>(I->getOperand(0)->getType())) {
1656+
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1657+
KnownBits SingleKnown = Known;
1658+
for (unsigned i = 1, e = VecTy->getNumElements(); i < e; ++i) {
1659+
if (Known.isUnknown())
1660+
break;
1661+
if (II->getIntrinsicID() == Intrinsic::vector_reduce_add)
1662+
Known = KnownBits::computeForAddSub(
1663+
/*Add=*/true, /*NSW=*/false, /*NUW=*/false, SingleKnown,
1664+
Known);
1665+
else
1666+
Known = KnownBits::mul(SingleKnown, Known);
1667+
}
1668+
}
1669+
break;
16501670
case Intrinsic::umin:
16511671
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
16521672
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);

llvm/test/Transforms/InstCombine/known-bits.ll

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,10 +1126,7 @@ define i8 @known_reduce_xor_odd_fail(<3 x i8> %xx) {
11261126

11271127
define i8 @known_reduce_add(<2 x i8> %xx) {
11281128
; CHECK-LABEL: @known_reduce_add(
1129-
; CHECK-NEXT: [[X:%.*]] = and <2 x i8> [[XX:%.*]], <i8 3, i8 3>
1130-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.add.v2i8(<2 x i8> [[X]])
1131-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 8
1132-
; CHECK-NEXT: ret i8 [[R]]
1129+
; CHECK-NEXT: ret i8 0
11331130
;
11341131
%x = and <2 x i8> %xx, <i8 3, i8 3>
11351132
%v = call i8 @llvm.vector.reduce.add(<2 x i8> %x)
@@ -1165,10 +1162,7 @@ define i8 @known_reduce_add_fail2(<4 x i8> %xx) {
11651162

11661163
define i8 @known_reduce_add2(<4 x i8> %xx) {
11671164
; CHECK-LABEL: @known_reduce_add2(
1168-
; CHECK-NEXT: [[X:%.*]] = and <4 x i8> [[XX:%.*]], <i8 3, i8 3, i8 3, i8 3>
1169-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.add.v4i8(<4 x i8> [[X]])
1170-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 32
1171-
; CHECK-NEXT: ret i8 [[R]]
1165+
; CHECK-NEXT: ret i8 0
11721166
;
11731167
%x = and <4 x i8> %xx, <i8 3, i8 3, i8 3, i8 3>
11741168
%v = call i8 @llvm.vector.reduce.add.v4i8(<4 x i8> %x)
@@ -1178,10 +1172,7 @@ define i8 @known_reduce_add2(<4 x i8> %xx) {
11781172

11791173
define i8 @known_reduce_add3(<2 x i8> %xx) {
11801174
; CHECK-LABEL: @known_reduce_add3(
1181-
; CHECK-NEXT: [[X:%.*]] = or <2 x i8> [[XX:%.*]], <i8 1, i8 1>
1182-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.add.v2i8(<2 x i8> [[X]])
1183-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
1184-
; CHECK-NEXT: ret i8 [[R]]
1175+
; CHECK-NEXT: ret i8 0
11851176
;
11861177
%x = or <2 x i8> %xx, <i8 1, i8 1>
11871178
%v = call i8 @llvm.vector.reduce.add(<2 x i8> %x)
@@ -1191,10 +1182,7 @@ define i8 @known_reduce_add3(<2 x i8> %xx) {
11911182

11921183
define i8 @known_reduce_add33(<3 x i8> %xx) {
11931184
; CHECK-LABEL: @known_reduce_add33(
1194-
; CHECK-NEXT: [[X:%.*]] = or <3 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1>
1195-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.add.v3i8(<3 x i8> [[X]])
1196-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
1197-
; CHECK-NEXT: ret i8 [[R]]
1185+
; CHECK-NEXT: ret i8 1
11981186
;
11991187
%x = or <3 x i8> %xx, <i8 1, i8 1, i8 1>
12001188
%v = call i8 @llvm.vector.reduce.add.v3i8(<3 x i8> %x)
@@ -1204,10 +1192,7 @@ define i8 @known_reduce_add33(<3 x i8> %xx) {
12041192

12051193
define i8 @known_reduce_add34(<4 x i8> %xx) {
12061194
; CHECK-LABEL: @known_reduce_add34(
1207-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1, i8 1>
1208-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.add.v4i8(<4 x i8> [[X]])
1209-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
1210-
; CHECK-NEXT: ret i8 [[R]]
1195+
; CHECK-NEXT: ret i8 0
12111196
;
12121197
%x = or <4 x i8> %xx, <i8 1, i8 1, i8 1, i8 1>
12131198
%v = call i8 @llvm.vector.reduce.add.v4i8(<4 x i8> %x)
@@ -1245,10 +1230,7 @@ define i8 @known_reduce_add4_fail(<2 x i8> %xx) {
12451230

12461231
define i8 @known_reduce_mul(<2 x i8> %xx) {
12471232
; CHECK-LABEL: @known_reduce_mul(
1248-
; CHECK-NEXT: [[X:%.*]] = and <2 x i8> [[XX:%.*]], <i8 3, i8 3>
1249-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.mul.v2i8(<2 x i8> [[X]])
1250-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 16
1251-
; CHECK-NEXT: ret i8 [[R]]
1233+
; CHECK-NEXT: ret i8 0
12521234
;
12531235
%x = and <2 x i8> %xx, <i8 3, i8 3>
12541236
%v = call i8 @llvm.vector.reduce.mul(<2 x i8> %x)
@@ -1284,10 +1266,7 @@ define i8 @known_reduce_mul_fail2(<3 x i8> %xx) {
12841266

12851267
define i8 @known_reduce_mul2(<3 x i8> %xx) {
12861268
; CHECK-LABEL: @known_reduce_mul2(
1287-
; CHECK-NEXT: [[X:%.*]] = and <3 x i8> [[XX:%.*]], <i8 3, i8 3, i8 3>
1288-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.mul.v3i8(<3 x i8> [[X]])
1289-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 64
1290-
; CHECK-NEXT: ret i8 [[R]]
1269+
; CHECK-NEXT: ret i8 0
12911270
;
12921271
%x = and <3 x i8> %xx, <i8 3, i8 3, i8 3>
12931272
%v = call i8 @llvm.vector.reduce.mul.v3i8(<3 x i8> %x)
@@ -1297,10 +1276,7 @@ define i8 @known_reduce_mul2(<3 x i8> %xx) {
12971276

12981277
define i8 @known_reduce_mul3(<2 x i8> %xx) {
12991278
; CHECK-LABEL: @known_reduce_mul3(
1300-
; CHECK-NEXT: [[X:%.*]] = or <2 x i8> [[XX:%.*]], <i8 1, i8 1>
1301-
; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.mul.v2i8(<2 x i8> [[X]])
1302-
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1
1303-
; CHECK-NEXT: ret i8 [[R]]
1279+
; CHECK-NEXT: ret i8 1
13041280
;
13051281
%x = or <2 x i8> %xx, <i8 1, i8 1>
13061282
%v = call i8 @llvm.vector.reduce.mul(<2 x i8> %x)

0 commit comments

Comments
 (0)