Skip to content

Commit 4a551ed

Browse files
committed
Preserve Fast-Math Flags
1 parent 4c989b9 commit 4a551ed

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,12 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {
19181918
if (!C1VecTy || !C2VecTy)
19191919
return false;
19201920

1921+
auto *Select0 = cast<Instruction>(I.getOperand(0));
1922+
if (auto *SI0FOp = dyn_cast<FPMathOperator>(Select0))
1923+
if (auto *SI1FOp = dyn_cast<FPMathOperator>((I.getOperand(1))))
1924+
if (SI0FOp->getFastMathFlags() != SI1FOp->getFastMathFlags())
1925+
return false;
1926+
19211927
auto SK = TargetTransformInfo::SK_PermuteTwoSrc;
19221928
auto SelOp = Instruction::Select;
19231929
InstructionCost OldCost = TTI.getCmpSelInstrCost(
@@ -1945,6 +1951,8 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {
19451951
Value *ShuffleTrue = Builder.CreateShuffleVector(T1, T2, Mask);
19461952
Value *ShuffleFalse = Builder.CreateShuffleVector(F1, F2, Mask);
19471953
Value *NewSel = Builder.CreateSelect(ShuffleCmp, ShuffleTrue, ShuffleFalse);
1954+
if (isa<FPMathOperator>(NewSel))
1955+
cast<Instruction>(NewSel)->setFastMathFlags(Select0->getFastMathFlags());
19481956

19491957
replaceValue(I, *NewSel);
19501958
return true;

llvm/test/Transforms/VectorCombine/X86/shuffle-of-selects.ll

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ define <4 x float> @src_v2tov4_float_nnan(<2 x i1> %a, <2 x i1> %b, <2 x float>
290290
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
291291
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
292292
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
293-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
293+
; SSE-NEXT: [[RES:%.*]] = select nnan <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
294294
; SSE-NEXT: ret <4 x float> [[RES]]
295295
;
296296
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_nnan(
297297
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
298298
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
299299
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
300300
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
301-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
301+
; AVX2-NEXT: [[RES:%.*]] = select nnan <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
302302
; AVX2-NEXT: ret <4 x float> [[RES]]
303303
;
304304
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_nnan(
@@ -320,15 +320,15 @@ define <4 x float> @src_v2tov4_float_ninf(<2 x i1> %a, <2 x i1> %b, <2 x float>
320320
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
321321
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
322322
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
323-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
323+
; SSE-NEXT: [[RES:%.*]] = select ninf <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
324324
; SSE-NEXT: ret <4 x float> [[RES]]
325325
;
326326
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_ninf(
327327
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
328328
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
329329
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
330330
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
331-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
331+
; AVX2-NEXT: [[RES:%.*]] = select ninf <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
332332
; AVX2-NEXT: ret <4 x float> [[RES]]
333333
;
334334
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_ninf(
@@ -350,15 +350,15 @@ define <4 x float> @src_v2tov4_float_nnan_ninf(<2 x i1> %a, <2 x i1> %b, <2 x fl
350350
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
351351
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
352352
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
353-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
353+
; SSE-NEXT: [[RES:%.*]] = select nnan ninf <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
354354
; SSE-NEXT: ret <4 x float> [[RES]]
355355
;
356356
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_nnan_ninf(
357357
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
358358
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
359359
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
360360
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
361-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
361+
; AVX2-NEXT: [[RES:%.*]] = select nnan ninf <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
362362
; AVX2-NEXT: ret <4 x float> [[RES]]
363363
;
364364
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_nnan_ninf(
@@ -380,15 +380,15 @@ define <4 x float> @src_v2tov4_float_nsz(<2 x i1> %a, <2 x i1> %b, <2 x float> %
380380
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
381381
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
382382
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
383-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
383+
; SSE-NEXT: [[RES:%.*]] = select nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
384384
; SSE-NEXT: ret <4 x float> [[RES]]
385385
;
386386
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_nsz(
387387
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
388388
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
389389
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
390390
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
391-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
391+
; AVX2-NEXT: [[RES:%.*]] = select nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
392392
; AVX2-NEXT: ret <4 x float> [[RES]]
393393
;
394394
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_nsz(
@@ -410,15 +410,15 @@ define <4 x float> @src_v2tov4_float_nnan_nsz(<2 x i1> %a, <2 x i1> %b, <2 x flo
410410
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
411411
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
412412
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
413-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
413+
; SSE-NEXT: [[RES:%.*]] = select nnan nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
414414
; SSE-NEXT: ret <4 x float> [[RES]]
415415
;
416416
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_nnan_nsz(
417417
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
418418
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
419419
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
420420
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
421-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
421+
; AVX2-NEXT: [[RES:%.*]] = select nnan nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
422422
; AVX2-NEXT: ret <4 x float> [[RES]]
423423
;
424424
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_nnan_nsz(
@@ -440,15 +440,15 @@ define <4 x float> @src_v2tov4_float_ninf_nsz(<2 x i1> %a, <2 x i1> %b, <2 x flo
440440
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
441441
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
442442
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
443-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
443+
; SSE-NEXT: [[RES:%.*]] = select ninf nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
444444
; SSE-NEXT: ret <4 x float> [[RES]]
445445
;
446446
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_ninf_nsz(
447447
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
448448
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
449449
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
450450
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
451-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
451+
; AVX2-NEXT: [[RES:%.*]] = select ninf nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
452452
; AVX2-NEXT: ret <4 x float> [[RES]]
453453
;
454454
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_ninf_nsz(
@@ -470,15 +470,15 @@ define <4 x float> @src_v2tov4_float_nnan_ninf_nsz(<2 x i1> %a, <2 x i1> %b, <2
470470
; SSE-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
471471
; SSE-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
472472
; SSE-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
473-
; SSE-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
473+
; SSE-NEXT: [[RES:%.*]] = select nnan ninf nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
474474
; SSE-NEXT: ret <4 x float> [[RES]]
475475
;
476476
; AVX2-LABEL: define <4 x float> @src_v2tov4_float_nnan_ninf_nsz(
477477
; AVX2-SAME: <2 x i1> [[A:%.*]], <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[Y:%.*]], <2 x float> [[Z:%.*]]) #[[ATTR0]] {
478478
; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <2 x i1> [[A]], <2 x i1> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
479479
; AVX2-NEXT: [[TMP2:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
480480
; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <2 x float> [[Z]], <2 x float> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
481-
; AVX2-NEXT: [[RES:%.*]] = select <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
481+
; AVX2-NEXT: [[RES:%.*]] = select nnan ninf nsz <4 x i1> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]
482482
; AVX2-NEXT: ret <4 x float> [[RES]]
483483
;
484484
; AVX512-LABEL: define <4 x float> @src_v2tov4_float_nnan_ninf_nsz(

0 commit comments

Comments
 (0)