Skip to content

[InstCombine] fold ldexp(x, sext(i1 y)) to fmul x, (select y, 0.5, 1.0) #95073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2024

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Jun 11, 2024

Follow up of #94887.

Context: #94887 (review)

@c8ef c8ef requested a review from nikic as a code owner June 11, 2024 04:10
@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (c8ef)

Changes

Follow up of #94887.

Context: #94887 (review)


Full diff: https://github.com/llvm/llvm-project/pull/95073.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+8)
  • (renamed) llvm/test/Transforms/InstCombine/ldexp-ext.ll (+55)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 4346a07e3a2cb..436cdbff75669 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2619,6 +2619,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     }
 
     // ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0)
+    // ldexp(x, sext(i1 y)) -> fmul x, (select y, 0.5, 1.0)
     Value *ExtSrc;
     if (match(Exp, m_ZExt(m_Value(ExtSrc))) &&
         ExtSrc->getType()->getScalarSizeInBits() == 1) {
@@ -2627,6 +2628,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
                                ConstantFP::get(II->getType(), 1.0));
       return BinaryOperator::CreateFMulFMF(Src, Select, II);
     }
+    if (match(Exp, m_SExt(m_Value(ExtSrc))) &&
+        ExtSrc->getType()->getScalarSizeInBits() == 1) {
+      Value *Select =
+          Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 0.5),
+                               ConstantFP::get(II->getType(), 1.0));
+      return BinaryOperator::CreateFMulFMF(Src, Select, II);
+    }
 
     break;
   }
diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-ext.ll
similarity index 51%
rename from llvm/test/Transforms/InstCombine/ldexp-zext.ll
rename to llvm/test/Transforms/InstCombine/ldexp-ext.ll
index b6e4f12494059..4608553eb8874 100644
--- a/llvm/test/Transforms/InstCombine/ldexp-zext.ll
+++ b/llvm/test/Transforms/InstCombine/ldexp-ext.ll
@@ -55,3 +55,58 @@ define <2 x float> @ldexp_zext_float_vector(<2 x float> %x, <2 x i1> %bool) {
   %ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %zext)
   ret <2 x float> %ldexp
 }
+
+define float @ldexp_sext_float(float %x, i1 %bool) {
+; CHECK-LABEL: @ldexp_sext_float(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[BOOL:%.*]], float 5.000000e-01, float 1.000000e+00
+; CHECK-NEXT:    [[LDEXP:%.*]] = fmul float [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    ret float [[LDEXP]]
+;
+  %sext = sext i1 %bool to i32
+  %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
+  ret float %ldexp
+}
+
+define float @ldexp_sext_float_negative(float %x, i8 %y) {
+; CHECK-LABEL: @ldexp_sext_float_negative(
+; CHECK-NEXT:    [[SEXT:%.*]] = sext i8 [[Y:%.*]] to i32
+; CHECK-NEXT:    [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[SEXT]])
+; CHECK-NEXT:    ret float [[LDEXP]]
+;
+  %sext = sext i8 %y to i32
+  %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
+  ret float %ldexp
+}
+
+define double @ldexp_sext_double(double %x, i1 %bool) {
+; CHECK-LABEL: @ldexp_sext_double(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
+; CHECK-NEXT:    [[LDEXP:%.*]] = fmul double [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    ret double [[LDEXP]]
+;
+  %sext = sext i1 %bool to i32
+  %ldexp = call double @llvm.ldexp.f64.i32(double %x, i32 %sext)
+  ret double %ldexp
+}
+
+define double @ldexp_sext_double_fast_math(double %x, i1 %bool) {
+; CHECK-LABEL: @ldexp_sext_double_fast_math(
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
+; CHECK-NEXT:    [[LDEXP:%.*]] = fmul reassoc double [[TMP1]], [[X:%.*]]
+; CHECK-NEXT:    ret double [[LDEXP]]
+;
+  %sext = sext i1 %bool to i32
+  %ldexp = call reassoc double @llvm.ldexp.f64.i32(double %x, i32 %sext)
+  ret double %ldexp
+}
+
+define <2 x float> @ldexp_sext_float_vector(<2 x float> %x, <2 x i1> %bool) {
+; CHECK-LABEL: @ldexp_sext_float_vector(
+; CHECK-NEXT:    [[TMP1:%.*]] = select <2 x i1> [[BOOL:%.*]], <2 x float> <float 5.000000e-01, float 5.000000e-01>, <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]]
+;
+  %sext = sext <2 x i1> %bool to <2 x i32>
+  %ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %sext)
+  ret <2 x float> %ldexp
+}

@dtcxzyw dtcxzyw requested a review from arsenm June 11, 2024 04:11
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@arsenm arsenm merged commit e5bdb7a into llvm:main Jun 11, 2024
9 checks passed
@c8ef c8ef deleted the ldexp-sext branch June 11, 2024 09:43
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants