From 00af1fbb6311694303ff6e161d122cdf1dc8f646 Mon Sep 17 00:00:00 2001 From: c8ef Date: Sun, 9 Jun 2024 11:24:47 +0800 Subject: [PATCH 1/5] ldexp(x, zext(i1 y)) --- .../InstCombine/InstCombineCalls.cpp | 13 ++++++++++ .../test/Transforms/InstCombine/ldexp-zext.ll | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/ldexp-zext.ll diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 0632f3cfc6dd2..15c11799b949e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2618,6 +2618,19 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { } } + // ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0) + Value *ExtSrc; + if (match(Exp, m_ZExt(m_Value(ExtSrc))) && + ExtSrc->getType()->getScalarSizeInBits() == 1) { + Value *Cmp = Builder.CreateICmp( + ICmpInst::ICMP_NE, ExtSrc, Constant::getNullValue(ExtSrc->getType())); + SelectInst *Select = SelectInst::Create( + Cmp, ConstantFP::get(Type::getFloatTy(II->getContext()), 2.0), + ConstantFP::get(Type::getFloatTy(II->getContext()), 1.0)); + Builder.Insert(Select); + return BinaryOperator::CreateFMul(Src, Select); + } + break; } case Intrinsic::ptrauth_auth: diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-zext.ll new file mode 100644 index 0000000000000..998a8d321342c --- /dev/null +++ b/llvm/test/Transforms/InstCombine/ldexp-zext.ll @@ -0,0 +1,24 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +define float @ldexp_zext(float %x, i1 %bool) { +; CHECK-LABEL: @ldexp_zext( +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], float 2.000000e+00, float 1.000000e+00 +; CHECK-NEXT: [[LDEXP:%.*]] = fmul float [[TMP1]], [[X:%.*]] +; CHECK-NEXT: ret float [[LDEXP]] +; + %zext = zext i1 %bool to i32 + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %zext) + ret float %ldexp +} + +define float @ldexp_zext_negative(float %x, i8 %y) { +; CHECK-LABEL: @ldexp_zext_negative( +; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[Y:%.*]] to i32 +; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[ZEXT]]) +; CHECK-NEXT: ret float [[LDEXP]] +; + %zext = zext i8 %y to i32 + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %zext) + ret float %ldexp +} From fb274bb1b623bad167303f6630a110aac7d7b14e Mon Sep 17 00:00:00 2001 From: c8ef Date: Sun, 9 Jun 2024 16:58:54 +0800 Subject: [PATCH 2/5] address CR comments --- .../InstCombine/InstCombineCalls.cpp | 7 ++--- .../test/Transforms/InstCombine/ldexp-zext.ll | 30 ++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 15c11799b949e..e0310106cd5d7 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2624,10 +2624,9 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { ExtSrc->getType()->getScalarSizeInBits() == 1) { Value *Cmp = Builder.CreateICmp( ICmpInst::ICMP_NE, ExtSrc, Constant::getNullValue(ExtSrc->getType())); - SelectInst *Select = SelectInst::Create( - Cmp, ConstantFP::get(Type::getFloatTy(II->getContext()), 2.0), - ConstantFP::get(Type::getFloatTy(II->getContext()), 1.0)); - Builder.Insert(Select); + Value *Select = + Builder.CreateSelect(Cmp, ConstantFP::get(II->getType(), 2.0), + ConstantFP::get(II->getType(), 1.0)); return BinaryOperator::CreateFMul(Src, Select); } diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-zext.ll index 998a8d321342c..a89ccd7cca120 100644 --- a/llvm/test/Transforms/InstCombine/ldexp-zext.ll +++ b/llvm/test/Transforms/InstCombine/ldexp-zext.ll @@ -1,8 +1,8 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -passes=instcombine -S | FileCheck %s -define float @ldexp_zext(float %x, i1 %bool) { -; CHECK-LABEL: @ldexp_zext( +define float @ldexp_zext_float(float %x, i1 %bool) { +; CHECK-LABEL: @ldexp_zext_float( ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], float 2.000000e+00, float 1.000000e+00 ; CHECK-NEXT: [[LDEXP:%.*]] = fmul float [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret float [[LDEXP]] @@ -12,8 +12,8 @@ define float @ldexp_zext(float %x, i1 %bool) { ret float %ldexp } -define float @ldexp_zext_negative(float %x, i8 %y) { -; CHECK-LABEL: @ldexp_zext_negative( +define float @ldexp_zext_float_negative(float %x, i8 %y) { +; CHECK-LABEL: @ldexp_zext_float_negative( ; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[Y:%.*]] to i32 ; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[ZEXT]]) ; CHECK-NEXT: ret float [[LDEXP]] @@ -22,3 +22,25 @@ define float @ldexp_zext_negative(float %x, i8 %y) { %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %zext) ret float %ldexp } + +define double @ldexp_zext_double(double %x, i1 %bool) { +; CHECK-LABEL: @ldexp_zext_double( +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 2.000000e+00, double 1.000000e+00 +; CHECK-NEXT: [[LDEXP:%.*]] = fmul double [[TMP1]], [[X:%.*]] +; CHECK-NEXT: ret double [[LDEXP]] +; + %zext = zext i1 %bool to i32 + %ldexp = call double @llvm.ldexp.f32.i32(double %x, i32 %zext) + ret double %ldexp +} + +define <2 x float> @ldexp_zext_float_vector(<2 x float> %x, <2 x i1> %bool) { +; CHECK-LABEL: @ldexp_zext_float_vector( +; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[BOOL:%.*]], <2 x float> , <2 x float> +; CHECK-NEXT: [[LDEXP:%.*]] = fmul <2 x float> [[TMP1]], [[X:%.*]] +; CHECK-NEXT: ret <2 x float> [[LDEXP]] +; + %zext = zext <2 x i1> %bool to <2 x i32> + %ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %zext) + ret <2 x float> %ldexp +} From edc40e408d155ee28d713d9d5678ca6873ecd77f Mon Sep 17 00:00:00 2001 From: c8ef Date: Sun, 9 Jun 2024 20:39:35 +0800 Subject: [PATCH 3/5] remove useless icmp --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index e0310106cd5d7..38ff23dd1c08f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2622,10 +2622,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { Value *ExtSrc; if (match(Exp, m_ZExt(m_Value(ExtSrc))) && ExtSrc->getType()->getScalarSizeInBits() == 1) { - Value *Cmp = Builder.CreateICmp( - ICmpInst::ICMP_NE, ExtSrc, Constant::getNullValue(ExtSrc->getType())); Value *Select = - Builder.CreateSelect(Cmp, ConstantFP::get(II->getType(), 2.0), + Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0), ConstantFP::get(II->getType(), 1.0)); return BinaryOperator::CreateFMul(Src, Select); } From 270482c50d8c619ddbb18756f863a15045514240 Mon Sep 17 00:00:00 2001 From: c8ef Date: Sun, 9 Jun 2024 21:33:22 +0800 Subject: [PATCH 4/5] preserve fast math flag; add test about it --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 2 +- llvm/test/Transforms/InstCombine/ldexp-zext.ll | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 38ff23dd1c08f..4346a07e3a2cb 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2625,7 +2625,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { Value *Select = Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0), ConstantFP::get(II->getType(), 1.0)); - return BinaryOperator::CreateFMul(Src, Select); + return BinaryOperator::CreateFMulFMF(Src, Select, II); } break; diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-zext.ll index a89ccd7cca120..38465920fe0af 100644 --- a/llvm/test/Transforms/InstCombine/ldexp-zext.ll +++ b/llvm/test/Transforms/InstCombine/ldexp-zext.ll @@ -34,6 +34,17 @@ define double @ldexp_zext_double(double %x, i1 %bool) { ret double %ldexp } +define double @ldexp_zext_double_fast_math(double %x, i1 %bool) { +; CHECK-LABEL: @ldexp_zext_double_fast_math( +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 2.000000e+00, double 1.000000e+00 +; CHECK-NEXT: [[LDEXP:%.*]] = fmul fast double [[TMP1]], [[X:%.*]] +; CHECK-NEXT: ret double [[LDEXP]] +; + %zext = zext i1 %bool to i32 + %ldexp = call fast double @llvm.ldexp.f32.i32(double %x, i32 %zext) + ret double %ldexp +} + define <2 x float> @ldexp_zext_float_vector(<2 x float> %x, <2 x i1> %bool) { ; CHECK-LABEL: @ldexp_zext_float_vector( ; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[BOOL:%.*]], <2 x float> , <2 x float> From 5caee727edb86b0e9781fd02690e0a931a7eb9a7 Mon Sep 17 00:00:00 2001 From: c8ef Date: Mon, 10 Jun 2024 09:10:10 +0800 Subject: [PATCH 5/5] fix test --- llvm/test/Transforms/InstCombine/ldexp-zext.ll | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-zext.ll index 38465920fe0af..b6e4f12494059 100644 --- a/llvm/test/Transforms/InstCombine/ldexp-zext.ll +++ b/llvm/test/Transforms/InstCombine/ldexp-zext.ll @@ -30,18 +30,18 @@ define double @ldexp_zext_double(double %x, i1 %bool) { ; CHECK-NEXT: ret double [[LDEXP]] ; %zext = zext i1 %bool to i32 - %ldexp = call double @llvm.ldexp.f32.i32(double %x, i32 %zext) + %ldexp = call double @llvm.ldexp.f64.i32(double %x, i32 %zext) ret double %ldexp } define double @ldexp_zext_double_fast_math(double %x, i1 %bool) { ; CHECK-LABEL: @ldexp_zext_double_fast_math( ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 2.000000e+00, double 1.000000e+00 -; CHECK-NEXT: [[LDEXP:%.*]] = fmul fast double [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[LDEXP:%.*]] = fmul reassoc double [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret double [[LDEXP]] ; %zext = zext i1 %bool to i32 - %ldexp = call fast double @llvm.ldexp.f32.i32(double %x, i32 %zext) + %ldexp = call reassoc double @llvm.ldexp.f64.i32(double %x, i32 %zext) ret double %ldexp }