Skip to content

Commit 9c545a1

Browse files
committed
[ValueTracking] Add support for insertelement in isKnownNonZero
Inserts don't modify the data, so if all elements that end up in the destination are non-zero the result is non-zero. Closes llvm#87703
1 parent 8a28b9b commit 9c545a1

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,29 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
27632763
return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ);
27642764
});
27652765
}
2766+
case Instruction::InsertElement: {
2767+
if (isa<ScalableVectorType>(I->getType()))
2768+
break;
2769+
2770+
const Value *Vec = I->getOperand(0);
2771+
const Value *Elt = I->getOperand(1);
2772+
auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2773+
2774+
unsigned NumElts = DemandedElts.getBitWidth();
2775+
APInt DemandedVecElts = DemandedElts;
2776+
bool SkipElt = false;
2777+
// If we know the index we are inserting too, clear it from Vec check.
2778+
if (CIdx && CIdx->getValue().ult(NumElts)) {
2779+
DemandedVecElts.clearBit(CIdx->getZExtValue());
2780+
SkipElt = !DemandedElts[CIdx->getZExtValue()];
2781+
}
2782+
2783+
// Result is zero if Elt is non-zero and rest of the demanded elts in Vec
2784+
// are non-zero.
2785+
return (SkipElt || isKnownNonZero(Elt, Depth, Q)) &&
2786+
(DemandedVecElts.isZero() ||
2787+
isKnownNonZero(Vec, DemandedVecElts, Depth, Q));
2788+
}
27662789
case Instruction::ExtractElement:
27672790
if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
27682791
const Value *Vec = EEI->getVectorOperand();

llvm/test/Transforms/InstSimplify/known-non-zero.ll

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,7 @@ define <4 x i1> @shuf_nonzero_rhs2_fail(<4 x i8> %xx) {
296296

297297
define <2 x i1> @insert_nonzero0(<2 x i8> %xx, i8 %yy) {
298298
; CHECK-LABEL: @insert_nonzero0(
299-
; CHECK-NEXT: [[X:%.*]] = add nuw <2 x i8> [[XX:%.*]], <i8 1, i8 0>
300-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
301-
; CHECK-NEXT: [[INS:%.*]] = insertelement <2 x i8> [[X]], i8 [[Y]], i32 1
302-
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[INS]], zeroinitializer
303-
; CHECK-NEXT: ret <2 x i1> [[R]]
299+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
304300
;
305301
%x = add nuw <2 x i8> %xx, <i8 1, i8 0>
306302
%y = add nuw i8 %yy, 1
@@ -312,11 +308,7 @@ define <2 x i1> @insert_nonzero0(<2 x i8> %xx, i8 %yy) {
312308

313309
define <2 x i1> @insert_nonzero1(<2 x i8> %xx, i8 %yy) {
314310
; CHECK-LABEL: @insert_nonzero1(
315-
; CHECK-NEXT: [[X:%.*]] = add nuw <2 x i8> [[XX:%.*]], <i8 0, i8 1>
316-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
317-
; CHECK-NEXT: [[INS:%.*]] = insertelement <2 x i8> [[X]], i8 [[Y]], i32 0
318-
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[INS]], zeroinitializer
319-
; CHECK-NEXT: ret <2 x i1> [[R]]
311+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
320312
;
321313
%x = add nuw <2 x i8> %xx, <i8 0, i8 1>
322314
%y = add nuw i8 %yy, 1
@@ -360,11 +352,7 @@ define <2 x i1> @insert_nonzero_fail2(<2 x i8> %xx, i8 %yy) {
360352

361353
define <2 x i1> @insert_nonzero_any_idx(<2 x i8> %xx, i8 %yy, i32 %idx) {
362354
; CHECK-LABEL: @insert_nonzero_any_idx(
363-
; CHECK-NEXT: [[X:%.*]] = add nuw <2 x i8> [[XX:%.*]], <i8 1, i8 1>
364-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
365-
; CHECK-NEXT: [[INS:%.*]] = insertelement <2 x i8> [[X]], i8 [[Y]], i32 [[IDX:%.*]]
366-
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[INS]], zeroinitializer
367-
; CHECK-NEXT: ret <2 x i1> [[R]]
355+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
368356
;
369357
%x = add nuw <2 x i8> %xx, <i8 1, i8 1>
370358
%y = add nuw i8 %yy, 1

0 commit comments

Comments
 (0)