Skip to content

Commit 1c10201

Browse files
committed
Update InstCombine to use undef matcher instead
This is a patch to use m_Undef() matcher instead of isa<UndefValue>(). As suggested in D100122, this update is separately committed.
1 parent 2813acb commit 1c10201

File tree

9 files changed

+32
-32
lines changed

9 files changed

+32
-32
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ Instruction *InstCombinerImpl::narrowBinOp(TruncInst &Trunc) {
675675
static Instruction *shrinkSplatShuffle(TruncInst &Trunc,
676676
InstCombiner::BuilderTy &Builder) {
677677
auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
678-
if (Shuf && Shuf->hasOneUse() && isa<UndefValue>(Shuf->getOperand(1)) &&
678+
if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) &&
679679
is_splat(Shuf->getShuffleMask()) &&
680680
Shuf->getType() == Shuf->getOperand(0)->getType()) {
681681
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Undef, SplatMask
@@ -708,7 +708,7 @@ static Instruction *shrinkInsertElt(CastInst &Trunc,
708708
Value *ScalarOp = InsElt->getOperand(1);
709709
Value *Index = InsElt->getOperand(2);
710710

711-
if (isa<UndefValue>(VecOp)) {
711+
if (match(VecOp, m_Undef())) {
712712
// trunc (inselt undef, X, Index) --> inselt undef, (trunc X), Index
713713
// fptrunc (inselt undef, X, Index) --> inselt undef, (fptrunc X), Index
714714
UndefValue *NarrowUndef = UndefValue::get(DestTy);
@@ -2698,7 +2698,7 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
26982698
ShufElts.getKnownMinValue() % 2 == 0 && Shuf->hasOneUse() &&
26992699
Shuf->isReverse()) {
27002700
assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask");
2701-
assert(isa<UndefValue>(ShufOp1) && "Unexpected shuffle op");
2701+
assert(match(ShufOp1, m_Undef()) && "Unexpected shuffle op");
27022702
Function *Bswap =
27032703
Intrinsic::getDeclaration(CI.getModule(), Intrinsic::bswap, DestTy);
27042704
Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy);

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ static Value *likeBitCastFromVector(InstCombinerImpl &IC, Value *V) {
10651065
return nullptr;
10661066
V = IV->getAggregateOperand();
10671067
}
1068-
if (!isa<UndefValue>(V) ||!U)
1068+
if (!match(V, m_Undef()) || !U)
10691069
return nullptr;
10701070

10711071
auto *UT = cast<VectorType>(U->getType());

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
25962596
// don't simplify it so loop unswitch can know the equality comparison
25972597
// may have an undef operand. This is a workaround for PR31652 caused by
25982598
// descrepancy about branch on undef between LoopUnswitch and GVN.
2599-
if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) {
2599+
if (match(TrueVal, m_Undef()) || match(FalseVal, m_Undef())) {
26002600
if (llvm::any_of(SI.users(), [&](User *U) {
26012601
ICmpInst *CI = dyn_cast<ICmpInst>(U);
26022602
if (CI && CI->isEquality())

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
10561056
APInt EltMask(APInt::getAllOnesValue(VWidth));
10571057
assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
10581058

1059-
if (isa<UndefValue>(V)) {
1059+
if (match(V, m_Undef())) {
10601060
// If the entire vector is undef or poison, just return this info.
10611061
UndefElts = EltMask;
10621062
return nullptr;
@@ -1157,7 +1157,7 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
11571157
// merge the undef bits here since gepping with either an undef base or
11581158
// index results in undef.
11591159
for (unsigned i = 0; i < I->getNumOperands(); i++) {
1160-
if (isa<UndefValue>(I->getOperand(i))) {
1160+
if (match(I->getOperand(i), m_Undef())) {
11611161
// If the entire vector is undefined, just return this info.
11621162
UndefElts = EltMask;
11631163
return nullptr;
@@ -1226,7 +1226,7 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
12261226
// operand.
12271227
if (all_of(Shuffle->getShuffleMask(), [](int Elt) { return Elt == 0; }) &&
12281228
DemandedElts.isAllOnesValue()) {
1229-
if (!isa<UndefValue>(I->getOperand(1))) {
1229+
if (!match(I->getOperand(1), m_Undef())) {
12301230
I->setOperand(1, UndefValue::get(I->getOperand(1)->getType()));
12311231
MadeChange = true;
12321232
}

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

+20-19
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
474474
"Invalid CollectSingleShuffleElements");
475475
unsigned NumElts = cast<FixedVectorType>(V->getType())->getNumElements();
476476

477-
if (isa<UndefValue>(V)) {
477+
if (match(V, m_Undef())) {
478478
Mask.assign(NumElts, -1);
479479
return true;
480480
}
@@ -630,7 +630,7 @@ static ShuffleOps collectShuffleElements(Value *V, SmallVectorImpl<int> &Mask,
630630
assert(V->getType()->isVectorTy() && "Invalid shuffle!");
631631
unsigned NumElts = cast<FixedVectorType>(V->getType())->getNumElements();
632632

633-
if (isa<UndefValue>(V)) {
633+
if (match(V, m_Undef())) {
634634
Mask.assign(NumElts, -1);
635635
return std::make_pair(
636636
PermittedRHS ? UndefValue::get(PermittedRHS->getType()) : V, nullptr);
@@ -1102,7 +1102,7 @@ static Instruction *foldInsSequenceIntoSplat(InsertElementInst &InsElt) {
11021102
// insert into every element.
11031103
// TODO: If the base vector is not undef, it might be better to create a splat
11041104
// and then a select-shuffle (blend) with the base vector.
1105-
if (!isa<UndefValue>(FirstIE->getOperand(0)))
1105+
if (!match(FirstIE->getOperand(0), m_Undef()))
11061106
if (!ElementPresent.all())
11071107
return nullptr;
11081108

@@ -1164,7 +1164,7 @@ static Instruction *foldInsEltIntoSplat(InsertElementInst &InsElt) {
11641164
static Instruction *foldInsEltIntoIdentityShuffle(InsertElementInst &InsElt) {
11651165
// Check if the vector operand of this insert is an identity shuffle.
11661166
auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0));
1167-
if (!Shuf || !isa<UndefValue>(Shuf->getOperand(1)) ||
1167+
if (!Shuf || !match(Shuf->getOperand(1), m_Undef()) ||
11681168
!(Shuf->isIdentityWithExtract() || Shuf->isIdentityWithPadding()))
11691169
return nullptr;
11701170

@@ -1633,7 +1633,7 @@ static Value *evaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) {
16331633
assert(V->getType()->isVectorTy() && "can't reorder non-vector elements");
16341634
Type *EltTy = V->getType()->getScalarType();
16351635
Type *I32Ty = IntegerType::getInt32Ty(V->getContext());
1636-
if (isa<UndefValue>(V))
1636+
if (match(V, m_Undef()))
16371637
return UndefValue::get(FixedVectorType::get(EltTy, Mask.size()));
16381638

16391639
if (isa<ConstantAggregateZero>(V))
@@ -1886,7 +1886,7 @@ static Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf,
18861886
// Canonicalize to choose from operand 0 first unless operand 1 is undefined.
18871887
// Commuting undef to operand 0 conflicts with another canonicalization.
18881888
unsigned NumElts = cast<FixedVectorType>(Shuf.getType())->getNumElements();
1889-
if (!isa<UndefValue>(Shuf.getOperand(1)) &&
1889+
if (!match(Shuf.getOperand(1), m_Undef()) &&
18901890
Shuf.getMaskValue(0) >= (int)NumElts) {
18911891
// TODO: Can we assert that both operands of a shuffle-select are not undef
18921892
// (otherwise, it would have been folded by instsimplify?
@@ -2083,7 +2083,7 @@ static Instruction *narrowVectorSelect(ShuffleVectorInst &Shuf,
20832083
/// Try to combine 2 shuffles into 1 shuffle by concatenating a shuffle mask.
20842084
static Instruction *foldIdentityExtractShuffle(ShuffleVectorInst &Shuf) {
20852085
Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1);
2086-
if (!Shuf.isIdentityWithExtract() || !isa<UndefValue>(Op1))
2086+
if (!Shuf.isIdentityWithExtract() || !match(Op1, m_Undef()))
20872087
return nullptr;
20882088

20892089
Value *X, *Y;
@@ -2231,10 +2231,10 @@ static Instruction *foldIdentityPaddedShuffles(ShuffleVectorInst &Shuf) {
22312231
!isPowerOf2_32(
22322232
cast<FixedVectorType>(Shuffle0->getType())->getNumElements()) ||
22332233
!isPowerOf2_32(cast<FixedVectorType>(X->getType())->getNumElements()) ||
2234-
isa<UndefValue>(X) || isa<UndefValue>(Y))
2234+
match(X, m_Undef()) || match(Y, m_Undef()))
22352235
return nullptr;
2236-
assert(isa<UndefValue>(Shuffle0->getOperand(1)) &&
2237-
isa<UndefValue>(Shuffle1->getOperand(1)) &&
2236+
assert(match(Shuffle0->getOperand(1), m_Undef()) &&
2237+
match(Shuffle1->getOperand(1), m_Undef()) &&
22382238
"Unexpected operand for identity shuffle");
22392239

22402240
// This is a shuffle of 2 widening shuffles. We can shuffle the narrow source
@@ -2342,7 +2342,8 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
23422342

23432343
// shuffle x, x, mask --> shuffle x, undef, mask'
23442344
if (LHS == RHS) {
2345-
assert(!isa<UndefValue>(RHS) && "Shuffle with 2 undef ops not simplified?");
2345+
assert(!match(RHS, m_Undef()) &&
2346+
"Shuffle with 2 undef ops not simplified?");
23462347
// Remap any references to RHS to use LHS.
23472348
SmallVector<int, 16> Elts;
23482349
for (unsigned i = 0; i != VWidth; ++i) {
@@ -2356,7 +2357,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
23562357
}
23572358

23582359
// shuffle undef, x, mask --> shuffle x, undef, mask'
2359-
if (isa<UndefValue>(LHS)) {
2360+
if (match(LHS, m_Undef())) {
23602361
SVI.commute();
23612362
return &SVI;
23622363
}
@@ -2391,7 +2392,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
23912392
if (Instruction *I = foldIdentityPaddedShuffles(SVI))
23922393
return I;
23932394

2394-
if (isa<UndefValue>(RHS) && canEvaluateShuffled(LHS, Mask)) {
2395+
if (match(RHS, m_Undef()) && canEvaluateShuffled(LHS, Mask)) {
23952396
Value *V = evaluateInDifferentElementOrder(LHS, Mask);
23962397
return replaceInstUsesWith(SVI, V);
23972398
}
@@ -2530,10 +2531,10 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
25302531
ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
25312532
ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
25322533
if (LHSShuffle)
2533-
if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
2534+
if (!match(LHSShuffle->getOperand(1), m_Undef()) && !match(RHS, m_Undef()))
25342535
LHSShuffle = nullptr;
25352536
if (RHSShuffle)
2536-
if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
2537+
if (!match(RHSShuffle->getOperand(1), m_Undef()))
25372538
RHSShuffle = nullptr;
25382539
if (!LHSShuffle && !RHSShuffle)
25392540
return MadeChange ? &SVI : nullptr;
@@ -2556,7 +2557,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
25562557
Value* newRHS = RHS;
25572558
if (LHSShuffle) {
25582559
// case 1
2559-
if (isa<UndefValue>(RHS)) {
2560+
if (match(RHS, m_Undef())) {
25602561
newLHS = LHSOp0;
25612562
newRHS = LHSOp1;
25622563
}
@@ -2614,7 +2615,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
26142615
//
26152616
// If the value selected is an undef value, explicitly specify it
26162617
// with a -1 mask value. (case 1)
2617-
if (isa<UndefValue>(RHS))
2618+
if (match(RHS, m_Undef()))
26182619
eltMask = -1;
26192620
// If RHS is going to be replaced (case 3 or 4), calculate the
26202621
// new mask value for the element.
@@ -2623,8 +2624,8 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
26232624
// If the value selected is an undef value, explicitly specify it
26242625
// with a -1 mask value.
26252626
if (eltMask >= (int)RHSOp0Width) {
2626-
assert(isa<UndefValue>(RHSShuffle->getOperand(1))
2627-
&& "should have been check above");
2627+
assert(match(RHSShuffle->getOperand(1), m_Undef()) &&
2628+
"should have been check above");
26282629
eltMask = -1;
26292630
}
26302631
} else

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
16821682
Constant *MaybeUndef =
16831683
ConstOp1 ? ConstantExpr::get(Opcode, UndefScalar, CElt)
16841684
: ConstantExpr::get(Opcode, CElt, UndefScalar);
1685-
if (!isa<UndefValue>(MaybeUndef)) {
1685+
if (!match(MaybeUndef, m_Undef())) {
16861686
MayChange = false;
16871687
break;
16881688
}

llvm/test/Transforms/InstCombine/vec_shuffle-inseltpoison.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ define <4 x float> @test7(<4 x float> %x) {
8585
; This should turn into a single shuffle.
8686
define <4 x float> @test8(<4 x float> %x, <4 x float> %y) {
8787
; CHECK-LABEL: @test8(
88-
; CHECK-NEXT: [[T132:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> poison, <4 x i32> <i32 1, i32 undef, i32 3, i32 undef>
89-
; CHECK-NEXT: [[T134:%.*]] = shufflevector <4 x float> [[T132]], <4 x float> [[Y:%.*]], <4 x i32> <i32 0, i32 undef, i32 2, i32 4>
88+
; CHECK-NEXT: [[T134:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> [[Y:%.*]], <4 x i32> <i32 1, i32 undef, i32 3, i32 4>
9089
; CHECK-NEXT: ret <4 x float> [[T134]]
9190
;
9291
%t4 = extractelement <4 x float> %x, i32 1

llvm/test/Transforms/PhaseOrdering/X86/scalarization.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ define <4 x i32> @square(<4 x i32> %num, i32 %y, i32 %x, i32 %h, i32 %k, i32 %w,
2727
; CHECK-NEXT: [[DOTSCALAR6:%.*]] = add i32 [[DOTSCALAR5]], [[DIV9]]
2828
; CHECK-NEXT: [[DOTSCALAR7:%.*]] = add i32 [[DOTSCALAR6]], [[MUL21]]
2929
; CHECK-NEXT: [[DOTSCALAR8:%.*]] = add i32 [[DOTSCALAR7]], 317425
30-
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <4 x i32> poison, i32 [[DOTSCALAR8]], i64 0
30+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <4 x i32> <i32 undef, i32 poison, i32 poison, i32 poison>, i32 [[DOTSCALAR8]], i64 0
3131
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> zeroinitializer
3232
; CHECK-NEXT: [[ADD29:%.*]] = add <4 x i32> [[TMP2]], [[NUM:%.*]]
3333
; CHECK-NEXT: ret <4 x i32> [[ADD29]]

llvm/test/Transforms/SLPVectorizer/X86/alternate-int.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ define <8 x i32> @sdiv_v8i32_undefs(<8 x i32> %a) {
425425
; CHECK-NEXT: [[AB5:%.*]] = sdiv i32 [[A5]], 4
426426
; CHECK-NEXT: [[AB6:%.*]] = sdiv i32 [[A6]], 8
427427
; CHECK-NEXT: [[AB7:%.*]] = sdiv i32 [[A7]], 16
428-
; CHECK-NEXT: [[R1:%.*]] = insertelement <8 x i32> <i32 poison, i32 poison, i32 poison, i32 poison, i32 undef, i32 poison, i32 poison, i32 poison>, i32 [[AB1]], i32 1
428+
; CHECK-NEXT: [[R1:%.*]] = insertelement <8 x i32> <i32 poison, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>, i32 [[AB1]], i32 1
429429
; CHECK-NEXT: [[R2:%.*]] = insertelement <8 x i32> [[R1]], i32 [[AB2]], i32 2
430430
; CHECK-NEXT: [[R3:%.*]] = insertelement <8 x i32> [[R2]], i32 [[AB3]], i32 3
431431
; CHECK-NEXT: [[R5:%.*]] = insertelement <8 x i32> [[R3]], i32 [[AB5]], i32 5

0 commit comments

Comments
 (0)