[llvm] f26bc5f - [InstCombine] fold `ldexp(x, zext(i1 y))` to `fmul x, (select y, 2.0, 1.0)` (#94887)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 10 07:40:41 PDT 2024


Author: c8ef
Date: 2024-06-10T16:40:37+02:00
New Revision: f26bc5f0c421c7a01a1a611249dd7483b749bab5

URL: https://github.com/llvm/llvm-project/commit/f26bc5f0c421c7a01a1a611249dd7483b749bab5
DIFF: https://github.com/llvm/llvm-project/commit/f26bc5f0c421c7a01a1a611249dd7483b749bab5.diff

LOG: [InstCombine] fold `ldexp(x, zext(i1 y))` to `fmul x, (select y, 2.0, 1.0)` (#94887)

close: #92538

Added: 
    llvm/test/Transforms/InstCombine/ldexp-zext.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 0632f3cfc6dd2..4346a07e3a2cb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2618,6 +2618,16 @@ 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 *Select =
+          Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0),
+                               ConstantFP::get(II->getType(), 1.0));
+      return BinaryOperator::CreateFMulFMF(Src, Select, II);
+    }
+
     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..b6e4f12494059
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/ldexp-zext.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+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]]
+;
+  %zext = zext i1 %bool to i32
+  %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %zext)
+  ret float %ldexp
+}
+
+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]]
+;
+  %zext = zext i8 %y to i32
+  %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.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 reassoc double [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    ret double [[LDEXP]]
+;
+  %zext = zext i1 %bool to i32
+  %ldexp = call reassoc double @llvm.ldexp.f64.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> <float 2.000000e+00, float 2.000000e+00>, <2 x float> <float 1.000000e+00, float 1.000000e+00>
+; 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
+}


        


More information about the llvm-commits mailing list