Skip to content

Commit cafdeb1

Browse files
committedOct 3, 2018
[InstCombine] allow SimplifyDemandedVectorElts to work with FP binops
We're a long way from D50992 and D51553, but this is where we have to start. We weren't back-propagating undefs into binop constant values for anything but add/sub/mul/and/or/xor. This is likely because we have to be careful about not introducing UB/poison with div/rem/shift. But I suspect we already are getting the poison part wrong for add/sub/mul (although it may not be possible to expose the bug currently because we use SimplifyDemandedVectorElts from a limited set of opcodes). See the discussion/implementation from D48987 and D49047. This patch just enables functionality for FP ops because those do not have UB/poison potential. llvm-svn: 343727
1 parent 0e99f56 commit cafdeb1

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -1378,24 +1378,6 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
13781378
}
13791379
break;
13801380
}
1381-
case Instruction::And:
1382-
case Instruction::Or:
1383-
case Instruction::Xor:
1384-
case Instruction::Add:
1385-
case Instruction::Sub:
1386-
case Instruction::Mul:
1387-
// div/rem demand all inputs, because they don't want divide by zero.
1388-
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1389-
Depth + 1);
1390-
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1391-
TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1392-
UndefElts2, Depth + 1);
1393-
if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1394-
1395-
// Output elements are undefined if both are undefined. Consider things
1396-
// like undef&0. The result is known zero, not undef.
1397-
UndefElts &= UndefElts2;
1398-
break;
13991381
case Instruction::FPTrunc:
14001382
case Instruction::FPExt:
14011383
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
@@ -1647,5 +1629,25 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
16471629
break;
16481630
}
16491631
}
1632+
1633+
// TODO: We bail completely on integer div/rem and shifts because they have
1634+
// UB/poison potential, but that should be refined.
1635+
BinaryOperator *BO;
1636+
if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) {
1637+
TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, UndefElts,
1638+
Depth + 1);
1639+
if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1640+
TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1641+
UndefElts2, Depth + 1);
1642+
if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1643+
1644+
// TODO: If this is a potentially poison-producing instruction, we need
1645+
// to drop the wrapping/exact flags?
1646+
1647+
// Output elements are undefined if both are undefined. Consider things
1648+
// like undef&0. The result is known zero, not undef.
1649+
UndefElts &= UndefElts2;
1650+
}
1651+
16501652
return MadeChange ? I : nullptr;
16511653
}

‎llvm/test/Transforms/InstCombine/X86/x86-avx512.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ define double @test_mask3_vfmsub_sd_1(<2 x double> %a, <2 x double> %b, <2 x dou
17091709

17101710
define <4 x float> @test_mask3_vfnmsub_ss(<4 x float> %a, <4 x float> %b, <4 x float> %c, i8 %mask) {
17111711
; CHECK-LABEL: @test_mask3_vfnmsub_ss(
1712-
; CHECK-NEXT: [[DOTRHS:%.*]] = extractelement <4 x float> [[A:%.*]], i32 0
1712+
; CHECK-NEXT: [[DOTRHS:%.*]] = extractelement <4 x float> [[A:%.*]], i64 0
17131713
; CHECK-NEXT: [[TMP1:%.*]] = fsub float -0.000000e+00, [[DOTRHS]]
17141714
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x float> [[B:%.*]], i32 0
17151715
; CHECK-NEXT: [[DOTRHS1:%.*]] = extractelement <4 x float> [[C:%.*]], i64 0

‎llvm/test/Transforms/InstCombine/shuffle_select.ll

+7-7
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ define <4 x float> @fadd(<4 x float> %v) {
348348

349349
define <4 x double> @fsub(<4 x double> %v) {
350350
; CHECK-LABEL: @fsub(
351-
; CHECK-NEXT: [[B:%.*]] = fsub <4 x double> <double 4.100000e+01, double 4.200000e+01, double 4.300000e+01, double 4.400000e+01>, [[V:%.*]]
351+
; CHECK-NEXT: [[B:%.*]] = fsub <4 x double> <double undef, double undef, double 4.300000e+01, double 4.400000e+01>, [[V:%.*]]
352352
; CHECK-NEXT: [[S:%.*]] = shufflevector <4 x double> [[V]], <4 x double> [[B]], <4 x i32> <i32 undef, i32 1, i32 6, i32 7>
353353
; CHECK-NEXT: ret <4 x double> [[S]]
354354
;
@@ -371,7 +371,7 @@ define <4 x float> @fmul(<4 x float> %v) {
371371

372372
define <4 x double> @fdiv_constant_op0(<4 x double> %v) {
373373
; CHECK-LABEL: @fdiv_constant_op0(
374-
; CHECK-NEXT: [[B:%.*]] = fdiv fast <4 x double> <double 4.100000e+01, double 4.200000e+01, double 4.300000e+01, double 4.400000e+01>, [[V:%.*]]
374+
; CHECK-NEXT: [[B:%.*]] = fdiv fast <4 x double> <double undef, double undef, double 4.300000e+01, double 4.400000e+01>, [[V:%.*]]
375375
; CHECK-NEXT: [[S:%.*]] = shufflevector <4 x double> [[V]], <4 x double> [[B]], <4 x i32> <i32 undef, i32 1, i32 6, i32 7>
376376
; CHECK-NEXT: ret <4 x double> [[S]]
377377
;
@@ -392,7 +392,7 @@ define <4 x double> @fdiv_constant_op1(<4 x double> %v) {
392392

393393
define <4 x double> @frem(<4 x double> %v) {
394394
; CHECK-LABEL: @frem(
395-
; CHECK-NEXT: [[B:%.*]] = frem <4 x double> <double 4.100000e+01, double 4.200000e+01, double 4.300000e+01, double 4.400000e+01>, [[V:%.*]]
395+
; CHECK-NEXT: [[B:%.*]] = frem <4 x double> <double 4.100000e+01, double 4.200000e+01, double undef, double undef>, [[V:%.*]]
396396
; CHECK-NEXT: [[S:%.*]] = shufflevector <4 x double> [[B]], <4 x double> [[V]], <4 x i32> <i32 0, i32 1, i32 6, i32 7>
397397
; CHECK-NEXT: ret <4 x double> [[S]]
398398
;
@@ -791,8 +791,8 @@ define <4 x double> @fdiv_fdiv(<4 x double> %v0) {
791791

792792
define <4 x double> @frem_frem(<4 x double> %v0) {
793793
; CHECK-LABEL: @frem_frem(
794-
; CHECK-NEXT: [[T1:%.*]] = frem <4 x double> <double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00>, [[V0:%.*]]
795-
; CHECK-NEXT: [[T2:%.*]] = frem <4 x double> [[V0]], <double 5.000000e+00, double 6.000000e+00, double 7.000000e+00, double 8.000000e+00>
794+
; CHECK-NEXT: [[T1:%.*]] = frem <4 x double> <double 1.000000e+00, double 2.000000e+00, double undef, double undef>, [[V0:%.*]]
795+
; CHECK-NEXT: [[T2:%.*]] = frem <4 x double> [[V0]], <double undef, double undef, double 7.000000e+00, double 8.000000e+00>
796796
; CHECK-NEXT: [[T3:%.*]] = shufflevector <4 x double> [[T1]], <4 x double> [[T2]], <4 x i32> <i32 0, i32 1, i32 6, i32 7>
797797
; CHECK-NEXT: ret <4 x double> [[T3]]
798798
;
@@ -1284,8 +1284,8 @@ define <4 x double> @frem_2_vars(<4 x double> %v0, <4 x double> %v1) {
12841284

12851285
define <4 x double> @fdiv_2_vars(<4 x double> %v0, <4 x double> %v1) {
12861286
; CHECK-LABEL: @fdiv_2_vars(
1287-
; CHECK-NEXT: [[T1:%.*]] = fdiv <4 x double> <double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00>, [[V0:%.*]]
1288-
; CHECK-NEXT: [[T2:%.*]] = fdiv <4 x double> [[V1:%.*]], <double 5.000000e+00, double 6.000000e+00, double 7.000000e+00, double 8.000000e+00>
1287+
; CHECK-NEXT: [[T1:%.*]] = fdiv <4 x double> <double 1.000000e+00, double 2.000000e+00, double undef, double undef>, [[V0:%.*]]
1288+
; CHECK-NEXT: [[T2:%.*]] = fdiv <4 x double> [[V1:%.*]], <double undef, double undef, double 7.000000e+00, double 8.000000e+00>
12891289
; CHECK-NEXT: [[T3:%.*]] = shufflevector <4 x double> [[T1]], <4 x double> [[T2]], <4 x i32> <i32 0, i32 1, i32 6, i32 7>
12901290
; CHECK-NEXT: ret <4 x double> [[T3]]
12911291
;

‎llvm/test/Transforms/InstCombine/vec_demanded_elts.ll

+7-7
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ define <3 x i8> @shuf_urem_const_op1(<3 x i8> %x) {
421421

422422
define <3 x float> @shuf_fadd(<3 x float> %x) {
423423
; CHECK-LABEL: @shuf_fadd(
424-
; CHECK-NEXT: [[BO:%.*]] = fadd <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>
424+
; CHECK-NEXT: [[BO:%.*]] = fadd <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float undef>
425425
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 1, i32 0>
426426
; CHECK-NEXT: ret <3 x float> [[R]]
427427
;
@@ -432,7 +432,7 @@ define <3 x float> @shuf_fadd(<3 x float> %x) {
432432

433433
define <3 x float> @shuf_fsub(<3 x float> %x) {
434434
; CHECK-LABEL: @shuf_fsub(
435-
; CHECK-NEXT: [[BO:%.*]] = fsub fast <3 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>, [[X:%.*]]
435+
; CHECK-NEXT: [[BO:%.*]] = fsub fast <3 x float> <float 1.000000e+00, float undef, float 3.000000e+00>, [[X:%.*]]
436436
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 0, i32 2>
437437
; CHECK-NEXT: ret <3 x float> [[R]]
438438
;
@@ -443,7 +443,7 @@ define <3 x float> @shuf_fsub(<3 x float> %x) {
443443

444444
define <3 x float> @shuf_fmul(<3 x float> %x) {
445445
; CHECK-LABEL: @shuf_fmul(
446-
; CHECK-NEXT: [[BO:%.*]] = fmul reassoc <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>
446+
; CHECK-NEXT: [[BO:%.*]] = fmul reassoc <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float undef>
447447
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 1, i32 0>
448448
; CHECK-NEXT: ret <3 x float> [[R]]
449449
;
@@ -454,7 +454,7 @@ define <3 x float> @shuf_fmul(<3 x float> %x) {
454454

455455
define <3 x float> @shuf_fdiv_const_op0(<3 x float> %x) {
456456
; CHECK-LABEL: @shuf_fdiv_const_op0(
457-
; CHECK-NEXT: [[BO:%.*]] = fdiv reassoc ninf <3 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>, [[X:%.*]]
457+
; CHECK-NEXT: [[BO:%.*]] = fdiv reassoc ninf <3 x float> <float 1.000000e+00, float undef, float 3.000000e+00>, [[X:%.*]]
458458
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 0, i32 2>
459459
; CHECK-NEXT: ret <3 x float> [[R]]
460460
;
@@ -465,7 +465,7 @@ define <3 x float> @shuf_fdiv_const_op0(<3 x float> %x) {
465465

466466
define <3 x float> @shuf_fdiv_const_op1(<3 x float> %x) {
467467
; CHECK-LABEL: @shuf_fdiv_const_op1(
468-
; CHECK-NEXT: [[BO:%.*]] = fdiv nnan ninf <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>
468+
; CHECK-NEXT: [[BO:%.*]] = fdiv nnan ninf <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float undef>
469469
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 1, i32 0>
470470
; CHECK-NEXT: ret <3 x float> [[R]]
471471
;
@@ -476,7 +476,7 @@ define <3 x float> @shuf_fdiv_const_op1(<3 x float> %x) {
476476

477477
define <3 x float> @shuf_frem_const_op0(<3 x float> %x) {
478478
; CHECK-LABEL: @shuf_frem_const_op0(
479-
; CHECK-NEXT: [[BO:%.*]] = frem nnan <3 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>, [[X:%.*]]
479+
; CHECK-NEXT: [[BO:%.*]] = frem nnan <3 x float> <float 1.000000e+00, float undef, float 3.000000e+00>, [[X:%.*]]
480480
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 undef, i32 2, i32 0>
481481
; CHECK-NEXT: ret <3 x float> [[R]]
482482
;
@@ -487,7 +487,7 @@ define <3 x float> @shuf_frem_const_op0(<3 x float> %x) {
487487

488488
define <3 x float> @shuf_frem_const_op1(<3 x float> %x) {
489489
; CHECK-LABEL: @shuf_frem_const_op1(
490-
; CHECK-NEXT: [[BO:%.*]] = frem reassoc ninf <3 x float> [[X:%.*]], <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>
490+
; CHECK-NEXT: [[BO:%.*]] = frem reassoc ninf <3 x float> [[X:%.*]], <float undef, float 2.000000e+00, float 3.000000e+00>
491491
; CHECK-NEXT: [[R:%.*]] = shufflevector <3 x float> [[BO]], <3 x float> undef, <3 x i32> <i32 1, i32 undef, i32 2>
492492
; CHECK-NEXT: ret <3 x float> [[R]]
493493
;

‎llvm/test/Transforms/InstCombine/vec_shuffle.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ define <2 x float> @fsub_splat_constant0(<2 x float> %x) {
952952

953953
define <2 x float> @fsub_splat_constant1(<2 x float> %x) {
954954
; CHECK-LABEL: @fsub_splat_constant1(
955-
; CHECK-NEXT: [[TMP1:%.*]] = fadd <2 x float> [[X:%.*]], <float -4.200000e+01, float 0x7FF8000000000000>
955+
; CHECK-NEXT: [[TMP1:%.*]] = fadd <2 x float> [[X:%.*]], <float -4.200000e+01, float undef>
956956
; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> undef, <2 x i32> zeroinitializer
957957
; CHECK-NEXT: ret <2 x float> [[R]]
958958
;

0 commit comments

Comments
 (0)
Please sign in to comment.