Skip to content

Commit eb7ad88

Browse files
authored
[InstCombine] Remove some uses with replaceUndefsWith() (#89190)
Now that we don't accept undef splat in PatternMatch, we can remove some uses of replaceUndefsWith(). I believe in all these cases only poison splats are possible now, in which case no replacement is necessary.
1 parent 3f2371e commit eb7ad88

11 files changed

+26
-58
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4433,23 +4433,13 @@ Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
44334433
// ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
44344434
Constant *C;
44354435
if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
4436-
match(C, m_Negative())) {
4437-
// We matched a negative constant, so propagating undef is unsafe.
4438-
// Clamp undef elements to -1.
4439-
Type *EltTy = Ty->getScalarType();
4440-
C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
4436+
match(C, m_Negative()))
44414437
return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y);
4442-
}
44434438

44444439
// ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
44454440
if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
4446-
match(C, m_NonNegative())) {
4447-
// We matched a non-negative constant, so propagating undef is unsafe.
4448-
// Clamp undef elements to 0.
4449-
Type *EltTy = Ty->getScalarType();
4450-
C = Constant::replaceUndefsWith(C, ConstantInt::getNullValue(EltTy));
4441+
match(C, m_NonNegative()))
44514442
return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y);
4452-
}
44534443

44544444
// ~(X + C) --> ~C - X
44554445
if (match(NotVal, m_Add(m_Value(X), m_ImmConstant(C))))

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,25 +4322,6 @@ static Value *foldICmpWithLowBitMaskedVal(ICmpInst::Predicate Pred, Value *Op0,
43224322
if (!IsLowBitMask())
43234323
return nullptr;
43244324

4325-
// The mask value may be a vector constant that has undefined elements. But
4326-
// it may not be safe to propagate those undefs into the new compare, so
4327-
// replace those elements by copying an existing, defined, and safe scalar
4328-
// constant.
4329-
Type *OpTy = M->getType();
4330-
auto *VecC = dyn_cast<Constant>(M);
4331-
auto *OpVTy = dyn_cast<FixedVectorType>(OpTy);
4332-
if (OpVTy && VecC && VecC->containsUndefOrPoisonElement()) {
4333-
Constant *SafeReplacementConstant = nullptr;
4334-
for (unsigned i = 0, e = OpVTy->getNumElements(); i != e; ++i) {
4335-
if (!isa<UndefValue>(VecC->getAggregateElement(i))) {
4336-
SafeReplacementConstant = VecC->getAggregateElement(i);
4337-
break;
4338-
}
4339-
}
4340-
assert(SafeReplacementConstant && "Failed to find undef replacement");
4341-
M = Constant::replaceUndefsWith(VecC, SafeReplacementConstant);
4342-
}
4343-
43444325
return IC.Builder.CreateICmp(DstPred, X, M);
43454326
}
43464327

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,6 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
842842
isKnownNeverNaN(&I, /*Depth=*/0, SQ.getWithInstruction(&I)))) {
843843
if (FPC->isNegative())
844844
Op0 = Builder.CreateFNegFMF(Op0, &I);
845-
Op1 = Constant::replaceUndefsWith(
846-
cast<Constant>(Op1),
847-
ConstantFP::get(Op1->getType()->getScalarType(), *FPC));
848845
CallInst *CopySign = Builder.CreateIntrinsic(Intrinsic::copysign,
849846
{I.getType()}, {Op1, Op0}, &I);
850847
return replaceInstUsesWith(I, CopySign);

llvm/test/Transforms/InstCombine/binop-itofp.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ define <2 x float> @nonzero_check_on_constant_for_si_fmul_vec_w_poison(i1 %c, i1
10701070
; CHECK-NEXT: [[CONV_I_V:%.*]] = insertelement <2 x i16> poison, i16 [[CONV_I_S]], i64 0
10711071
; CHECK-NEXT: [[CONV_I:%.*]] = shufflevector <2 x i16> [[CONV_I_V]], <2 x i16> poison, <2 x i32> zeroinitializer
10721072
; CHECK-NEXT: [[CONV1_I:%.*]] = sitofp <2 x i16> [[CONV_I]] to <2 x float>
1073-
; CHECK-NEXT: [[MUL3_I_I:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> zeroinitializer, <2 x float> [[CONV1_I]])
1073+
; CHECK-NEXT: [[MUL3_I_I:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float poison, float 0.000000e+00>, <2 x float> [[CONV1_I]])
10741074
; CHECK-NEXT: store i32 [[SEL]], ptr [[G_2345:%.*]], align 4
10751075
; CHECK-NEXT: ret <2 x float> [[MUL3_I_I]]
10761076
;
@@ -1090,9 +1090,9 @@ define <2 x float> @nonzero_check_on_constant_for_si_fmul_nz_vec_w_poison(i1 %c,
10901090
; CHECK-NEXT: [[CONV_I_S:%.*]] = trunc nuw i32 [[SEL]] to i16
10911091
; CHECK-NEXT: [[CONV_I_V:%.*]] = insertelement <2 x i16> poison, i16 [[CONV_I_S]], i64 0
10921092
; CHECK-NEXT: [[CONV_I:%.*]] = shufflevector <2 x i16> [[CONV_I_V]], <2 x i16> poison, <2 x i32> zeroinitializer
1093-
; CHECK-NEXT: [[CONV1_I:%.*]] = sitofp <2 x i16> [[CONV_I]] to <2 x float>
1093+
; CHECK-NEXT: [[MUL3_I_I:%.*]] = sitofp <2 x i16> [[CONV_I]] to <2 x float>
10941094
; CHECK-NEXT: store i32 [[SEL]], ptr [[G_2345:%.*]], align 4
1095-
; CHECK-NEXT: ret <2 x float> [[CONV1_I]]
1095+
; CHECK-NEXT: ret <2 x float> [[MUL3_I_I]]
10961096
;
10971097
%sel = select i1 %c, i32 65529, i32 53264
10981098
%conv.i.s = trunc i32 %sel to i16
@@ -1112,7 +1112,7 @@ define <2 x float> @nonzero_check_on_constant_for_si_fmul_negz_vec_w_poison(i1 %
11121112
; CHECK-NEXT: [[CONV_I:%.*]] = shufflevector <2 x i16> [[CONV_I_V]], <2 x i16> poison, <2 x i32> zeroinitializer
11131113
; CHECK-NEXT: [[CONV1_I:%.*]] = sitofp <2 x i16> [[CONV_I]] to <2 x float>
11141114
; CHECK-NEXT: [[TMP1:%.*]] = fneg <2 x float> [[CONV1_I]]
1115-
; CHECK-NEXT: [[MUL3_I_I:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> zeroinitializer, <2 x float> [[TMP1]])
1115+
; CHECK-NEXT: [[MUL3_I_I:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> <float poison, float -0.000000e+00>, <2 x float> [[TMP1]])
11161116
; CHECK-NEXT: store i32 [[SEL]], ptr [[G_2345:%.*]], align 4
11171117
; CHECK-NEXT: ret <2 x float> [[MUL3_I_I]]
11181118
;

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ne-to-icmp-ugt.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ define <2 x i1> @p2_vec_nonsplat_edgecase1(<2 x i8> %x) {
8181

8282
define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
8383
; CHECK-LABEL: @p3_vec_splat_poison(
84-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 3, i8 3>
84+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 poison, i8 3>
8585
; CHECK-NEXT: ret <3 x i1> [[RET]]
8686
;
8787
%tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>
@@ -91,7 +91,7 @@ define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
9191

9292
define <3 x i1> @p3_vec_nonsplat_poison(<3 x i8> %x) {
9393
; CHECK-LABEL: @p3_vec_nonsplat_poison(
94-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 -1, i8 -1, i8 3>
94+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 -1, i8 poison, i8 3>
9595
; CHECK-NEXT: ret <3 x i1> [[RET]]
9696
;
9797
%tmp0 = and <3 x i8> %x, <i8 -1, i8 poison, i8 3>

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-sgt-to-icmp-sgt.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ define <2 x i1> @p2_vec_nonsplat_edgecase() {
7575
define <3 x i1> @p3_vec_splat_poison() {
7676
; CHECK-LABEL: @p3_vec_splat_poison(
7777
; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()
78-
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X]], <i8 3, i8 3, i8 3>
78+
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X]], <i8 3, i8 poison, i8 3>
7979
; CHECK-NEXT: ret <3 x i1> [[RET]]
8080
;
8181
%x = call <3 x i8> @gen3x8()
@@ -87,7 +87,7 @@ define <3 x i1> @p3_vec_splat_poison() {
8787
define <3 x i1> @p3_vec_nonsplat_poison() {
8888
; CHECK-LABEL: @p3_vec_nonsplat_poison(
8989
; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()
90-
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X]], <i8 15, i8 3, i8 15>
90+
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X]], <i8 15, i8 3, i8 poison>
9191
; CHECK-NEXT: ret <3 x i1> [[RET]]
9292
;
9393
%x = call <3 x i8> @gen3x8()

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-slt-to-icmp-sgt.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ define <2 x i1> @p2_vec_nonsplat_edgecase(<2 x i8> %x) {
6060

6161
define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
6262
; CHECK-LABEL: @p3_vec_splat_poison(
63-
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 3, i8 3, i8 3>
63+
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 3, i8 poison, i8 3>
6464
; CHECK-NEXT: ret <3 x i1> [[RET]]
6565
;
6666
%tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>
@@ -70,7 +70,7 @@ define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
7070

7171
define <3 x i1> @p3_vec_nonsplat_poison(<3 x i8> %x) {
7272
; CHECK-LABEL: @p3_vec_nonsplat_poison(
73-
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 15, i8 15, i8 3>
73+
; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 poison, i8 15, i8 3>
7474
; CHECK-NEXT: ret <3 x i1> [[RET]]
7575
;
7676
%tmp0 = and <3 x i8> %x, <i8 poison, i8 15, i8 3>

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ugt-to-icmp-ugt.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ define <2 x i1> @p2_vec_nonsplat_edgecase1() {
9898
define <3 x i1> @p3_vec_splat_poison() {
9999
; CHECK-LABEL: @p3_vec_splat_poison(
100100
; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()
101-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 3, i8 3>
101+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 poison, i8 3>
102102
; CHECK-NEXT: ret <3 x i1> [[RET]]
103103
;
104104
%x = call <3 x i8> @gen3x8()
@@ -110,7 +110,7 @@ define <3 x i1> @p3_vec_splat_poison() {
110110
define <3 x i1> @p3_vec_nonsplat_poison() {
111111
; CHECK-LABEL: @p3_vec_nonsplat_poison(
112112
; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()
113-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 3, i8 15>
113+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 poison, i8 15>
114114
; CHECK-NEXT: ret <3 x i1> [[RET]]
115115
;
116116
%x = call <3 x i8> @gen3x8()

llvm/test/Transforms/InstCombine/canonicalize-constant-low-bit-mask-and-icmp-ult-to-icmp-ugt.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ define <2 x i1> @p2_vec_nonsplat_edgecase1(<2 x i8> %x) {
8282

8383
define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
8484
; CHECK-LABEL: @p3_vec_splat_poison(
85-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 3, i8 3>
85+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 poison, i8 3>
8686
; CHECK-NEXT: ret <3 x i1> [[RET]]
8787
;
8888
%tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>
@@ -92,7 +92,7 @@ define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
9292

9393
define <3 x i1> @p3_vec_nonsplat_poison(<3 x i8> %x) {
9494
; CHECK-LABEL: @p3_vec_nonsplat_poison(
95-
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 7, i8 31, i8 7>
95+
; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 7, i8 31, i8 poison>
9696
; CHECK-NEXT: ret <3 x i1> [[RET]]
9797
;
9898
%tmp0 = and <3 x i8> %x, <i8 7, i8 31, i8 poison>

llvm/test/Transforms/InstCombine/fmul.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,11 @@ define float @log2half(float %x, float %y) {
633633

634634
define float @log2half_commute(float %x1, float %y) {
635635
; CHECK-LABEL: @log2half_commute(
636-
; CHECK-NEXT: [[X1:%.*]] = fmul fast float [[X2:%.*]], 0x3FC24924A0000000
636+
; CHECK-NEXT: [[X:%.*]] = fmul fast float [[X1:%.*]], 0x3FC24924A0000000
637637
; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.log2.f32(float [[Y:%.*]])
638-
; CHECK-NEXT: [[TMP2:%.*]] = fmul fast float [[TMP1]], [[X1]]
639-
; CHECK-NEXT: [[TMP3:%.*]] = fsub fast float [[TMP2]], [[X1]]
640-
; CHECK-NEXT: ret float [[TMP3]]
638+
; CHECK-NEXT: [[TMP2:%.*]] = fmul fast float [[TMP1]], [[X]]
639+
; CHECK-NEXT: [[MUL:%.*]] = fsub fast float [[TMP2]], [[X]]
640+
; CHECK-NEXT: ret float [[MUL]]
641641
;
642642
%x = fdiv fast float %x1, 7.0 ; thwart complexity-based canonicalization
643643
%halfy = fmul fast float %y, 0.5
@@ -687,8 +687,8 @@ define float @fdiv_constant_numerator_fmul_fast(float %x) {
687687
define float @fdiv_constant_numerator_fmul_precdiv(float %x) {
688688
; CHECK-LABEL: @fdiv_constant_numerator_fmul_precdiv(
689689
; CHECK-NEXT: [[T1:%.*]] = fdiv float 2.000000e+03, [[X:%.*]]
690-
; CHECK-NEXT: [[T4:%.*]] = fmul reassoc float [[T1]], 6.000000e+03
691-
; CHECK-NEXT: ret float [[T4]]
690+
; CHECK-NEXT: [[T3:%.*]] = fmul reassoc float [[T1]], 6.000000e+03
691+
; CHECK-NEXT: ret float [[T3]]
692692
;
693693
%t1 = fdiv float 2.0e+3, %x
694694
%t3 = fmul reassoc float %t1, 6.0e+3
@@ -1288,7 +1288,7 @@ define half @mul_zero_nnan(half %x) {
12881288

12891289
define <2 x float> @mul_zero_nnan_vec_poison(<2 x float> %x) {
12901290
; CHECK-LABEL: @mul_zero_nnan_vec_poison(
1291-
; CHECK-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.copysign.v2f32(<2 x float> zeroinitializer, <2 x float> [[X:%.*]])
1291+
; CHECK-NEXT: [[R:%.*]] = call nnan <2 x float> @llvm.copysign.v2f32(<2 x float> <float 0.000000e+00, float poison>, <2 x float> [[X:%.*]])
12921292
; CHECK-NEXT: ret <2 x float> [[R]]
12931293
;
12941294
%r = fmul nnan <2 x float> %x, <float 0.0, float poison>
@@ -1389,7 +1389,7 @@ define <3 x float> @mul_neg_zero_nnan_ninf_vec(<3 x float> nofpclass(inf nan) %a
13891389
; CHECK-LABEL: @mul_neg_zero_nnan_ninf_vec(
13901390
; CHECK-NEXT: entry:
13911391
; CHECK-NEXT: [[TMP0:%.*]] = fneg <3 x float> [[A:%.*]]
1392-
; CHECK-NEXT: [[RET:%.*]] = call <3 x float> @llvm.copysign.v3f32(<3 x float> zeroinitializer, <3 x float> [[TMP0]])
1392+
; CHECK-NEXT: [[RET:%.*]] = call <3 x float> @llvm.copysign.v3f32(<3 x float> <float -0.000000e+00, float poison, float poison>, <3 x float> [[TMP0]])
13931393
; CHECK-NEXT: ret <3 x float> [[RET]]
13941394
;
13951395
entry:

llvm/test/Transforms/InstCombine/vector-xor.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ define <4 x i32> @test_v4i32_not_ashr_negative_const(<4 x i32> %a0) {
140140

141141
define <4 x i32> @test_v4i32_not_ashr_negative_const_poison(<4 x i32> %a0) {
142142
; CHECK-LABEL: @test_v4i32_not_ashr_negative_const_poison(
143-
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> <i32 2, i32 4, i32 0, i32 8>, [[A0:%.*]]
143+
; CHECK-NEXT: [[TMP1:%.*]] = lshr <4 x i32> <i32 2, i32 4, i32 poison, i32 8>, [[A0:%.*]]
144144
; CHECK-NEXT: ret <4 x i32> [[TMP1]]
145145
;
146146
%1 = ashr <4 x i32> <i32 -3, i32 -5, i32 poison, i32 -9>, %a0
@@ -172,7 +172,7 @@ define <4 x i32> @test_v4i32_not_lshr_nonnegative_const(<4 x i32> %a0) {
172172

173173
define <4 x i32> @test_v4i32_not_lshr_nonnegative_const_poison(<4 x i32> %a0) {
174174
; CHECK-LABEL: @test_v4i32_not_lshr_nonnegative_const_poison(
175-
; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> <i32 -4, i32 -6, i32 -1, i32 -10>, [[A0:%.*]]
175+
; CHECK-NEXT: [[TMP1:%.*]] = ashr <4 x i32> <i32 -4, i32 -6, i32 poison, i32 -10>, [[A0:%.*]]
176176
; CHECK-NEXT: ret <4 x i32> [[TMP1]]
177177
;
178178
%1 = lshr <4 x i32> <i32 3, i32 5, i32 poison, i32 9>, %a0

0 commit comments

Comments
 (0)