Index: Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- Transforms/InstCombine/InstCombineCalls.cpp +++ Transforms/InstCombine/InstCombineCalls.cpp @@ -2336,6 +2336,37 @@ return II; break; } + case Intrinsic::x86_avx512_mask_cmp_pd_128: + case Intrinsic::x86_avx512_mask_cmp_pd_256: + case Intrinsic::x86_avx512_mask_cmp_pd_512: + case Intrinsic::x86_avx512_mask_cmp_ps_128: + case Intrinsic::x86_avx512_mask_cmp_ps_256: + case Intrinsic::x86_avx512_mask_cmp_ps_512: { + // Checks for two options and select the first + // Opt1: Cmp(a-b,0)->Cmp(a,b) + // Opt2: Cmp(0,a-b)->Cmp(b,a) + for (int i = 0; i < 2; i++) { + // Folding cmp(sub(a,b),0) into cmp(a,b) + if (auto *I = dyn_cast(II->getArgOperand(i))) { + if (I->getOpcode() == Instruction::FSub && I->hasOneUse()) { + // This fold requires only the NINF(not +/- inf) since inf minus + // inf is nan. + // NSZ(No Signed Zeros) is not needed because zeros of any sign are + // equal for both compares. + // NNAN is not needed because nans compare the same for both compares. + // The compare intrinsic uses the above assumptions and therefore + // doesn't require additional flags. + FastMathFlags FMFs = I->getFastMathFlags(); + if (FMFs.noInfs() && isa((II->getArgOperand((i + 1) % 2)))) { + II->setArgOperand(0, I->getOperand(i % 2)); + II->setArgOperand(1, I->getOperand((i + 1) % 2)); + return II; + } + } + } + } + break; + } case Intrinsic::x86_avx512_mask_add_ps_512: case Intrinsic::x86_avx512_mask_div_ps_512: Index: Transforms/InstCombine/X86FsubCmpCombine.ll =================================================================== --- /dev/null +++ Transforms/InstCombine/X86FsubCmpCombine.ll @@ -0,0 +1,181 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +; The test checks the folding of cmp(sub(a,b),0) into cmp(a,b). + +define i8 @sub_compare_foldingPD128_safe(<2 x double> %a, <2 x double> %aa){ +; CHECK-LABEL: @sub_compare_foldingPD128_safe( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[SUB_SAFE:%.*]] = fsub <2 x double> [[A:%.*]], [[AA:%.*]] +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> [[SUB_SAFE]], <2 x double> zeroinitializer, i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.safe = fsub <2 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> %sub.safe , <2 x double> zeroinitializer, i32 5, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_foldingPD128(<2 x double> %a, <2 x double> %aa){ +; CHECK-LABEL: @sub_compare_foldingPD128( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> [[A:%.*]], <2 x double> [[AA:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <2 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> %sub.i , <2 x double> zeroinitializer, i32 5, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_foldingPD256(<4 x double> %a, <4 x double> %aa){ +; CHECK-LABEL: @sub_compare_foldingPD256( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> [[A:%.*]], <4 x double> [[AA:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i1 = fsub ninf <4 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> %sub.i1, <4 x double> zeroinitializer, i32 5, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_foldingPD512(<8 x double> %a, <8 x double> %aa){ +; CHECK-LABEL: @sub_compare_foldingPD512( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.512(<8 x double> [[A:%.*]], <8 x double> [[AA:%.*]], i32 11, i8 -1, i32 4) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i2 = fsub ninf <8 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.512(<8 x double> %sub.i2, <8 x double> zeroinitializer, i32 11, i8 -1, i32 4) + ret i8 %0 +} + + +define i8 @sub_compare_foldingPS128(<4 x float> %a, <4 x float> %aa){ +; CHECK-LABEL: @sub_compare_foldingPS128( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.ps.128(<4 x float> [[A:%.*]], <4 x float> [[AA:%.*]], i32 12, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i3 = fsub ninf <4 x float> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.ps.128(<4 x float> %sub.i3, <4 x float> zeroinitializer, i32 12, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_foldingPS256(<8 x float> %a, <8 x float> %aa){ +; CHECK-LABEL: @sub_compare_foldingPS256( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.ps.256(<8 x float> [[A:%.*]], <8 x float> [[AA:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i4 = fsub ninf <8 x float> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.ps.256(<8 x float> %sub.i4, <8 x float> zeroinitializer, i32 5, i8 -1) + ret i8 %0 +} + + +define i16 @sub_compare_foldingPS512(<16 x float> %a, <16 x float> %aa){ +; CHECK-LABEL: @sub_compare_foldingPS512( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i16 @llvm.x86.avx512.mask.cmp.ps.512(<16 x float> [[A:%.*]], <16 x float> [[AA:%.*]], i32 11, i16 -1, i32 4) +; CHECK-NEXT: ret i16 [[TMP0]] +; +entry: + %sub.i5 = fsub ninf <16 x float> %a, %aa + %0 = tail call i16 @llvm.x86.avx512.mask.cmp.ps.512(<16 x float> %sub.i5, <16 x float> zeroinitializer, i32 11, i16 -1, i32 4) + ret i16 %0 +} + + + +define i8 @sub_compare_folding_swapPD128(<2 x double> %a, <2 x double> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPD128( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> [[AA:%.*]], <2 x double> [[A:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <2 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double> zeroinitializer, <2 x double> %sub.i, i32 5, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_folding_swapPD256(<4 x double> %a, <4 x double> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPD256( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> [[AA:%.*]], <4 x double> [[A:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <4 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> zeroinitializer, <4 x double> %sub.i, i32 5, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_folding_swapPD512(<8 x double> %a, <8 x double> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPD512( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.pd.512(<8 x double> [[AA:%.*]], <8 x double> [[A:%.*]], i32 11, i8 -1, i32 4) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <8 x double> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.pd.512(<8 x double> zeroinitializer, <8 x double> %sub.i, i32 11, i8 -1, i32 4) + ret i8 %0 +} + + +define i8 @sub_compare_folding_swapPS128(<4 x float> %a, <4 x float> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPS128( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.ps.128(<4 x float> [[AA:%.*]], <4 x float> [[A:%.*]], i32 12, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <4 x float> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.ps.128(<4 x float> zeroinitializer, <4 x float> %sub.i, i32 12, i8 -1) + ret i8 %0 +} + + +define i8 @sub_compare_folding_swapPS256(<8 x float> %a, <8 x float> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPS256( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i8 @llvm.x86.avx512.mask.cmp.ps.256(<8 x float> [[AA:%.*]], <8 x float> [[A:%.*]], i32 5, i8 -1) +; CHECK-NEXT: ret i8 [[TMP0]] +; +entry: + %sub.i = fsub ninf <8 x float> %a, %aa + %0 = tail call i8 @llvm.x86.avx512.mask.cmp.ps.256(<8 x float> zeroinitializer, <8 x float> %sub.i, i32 5, i8 -1) + ret i8 %0 +} + + +define i16 @sub_compare_folding_swapPS512(<16 x float> %a, <16 x float> %aa){ +; CHECK-LABEL: @sub_compare_folding_swapPS512( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = tail call i16 @llvm.x86.avx512.mask.cmp.ps.512(<16 x float> [[AA:%.*]], <16 x float> [[A:%.*]], i32 11, i16 -1, i32 4) +; CHECK-NEXT: ret i16 [[TMP0]] +; +entry: + %sub.i = fsub ninf <16 x float> %a, %aa + %0 = tail call i16 @llvm.x86.avx512.mask.cmp.ps.512(<16 x float> zeroinitializer, <16 x float> %sub.i, i32 11, i16 -1, i32 4) + ret i16 %0 +} + +declare i8 @llvm.x86.avx512.mask.cmp.pd.128(<2 x double>, <2 x double>, i32, i8) +declare i8 @llvm.x86.avx512.mask.cmp.pd.256(<4 x double>, <4 x double>, i32, i8) +declare i8 @llvm.x86.avx512.mask.cmp.pd.512(<8 x double>, <8 x double>, i32, i8, i32) +declare i8 @llvm.x86.avx512.mask.cmp.ps.128(<4 x float>, <4 x float>, i32, i8) +declare i8 @llvm.x86.avx512.mask.cmp.ps.256(<8 x float>, <8 x float>, i32, i8) +declare i16 @llvm.x86.avx512.mask.cmp.ps.512(<16 x float>, <16 x float>, i32, i16, i32)