Skip to content

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

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 5 commits into from
Jun 10, 2024

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Jun 9, 2024

close: #92538

@c8ef c8ef requested a review from nikic as a code owner June 9, 2024 03:27
@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (c8ef)

Changes

close: #92538


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+13)
  • (added) llvm/test/Transforms/InstCombine/ldexp-zext.ll (+24)
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
+}

@dtcxzyw dtcxzyw requested a review from arsenm June 9, 2024 08:22
@c8ef c8ef requested a review from dtcxzyw June 9, 2024 09:02
@nikic nikic added the floating-point Floating-point math label Jun 9, 2024
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

In a follow up could also handle sext -> 1.0 or 0.5

@c8ef c8ef requested review from arsenm and dtcxzyw June 9, 2024 13:59
; CHECK-NEXT: ret double [[LDEXP]]
;
%zext = zext i1 %bool to i32
%ldexp = call fast double @llvm.ldexp.f32.i32(double %x, i32 %zext)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@c8ef c8ef requested review from arsenm and dtcxzyw June 10, 2024 01:38
@arsenm arsenm merged commit f26bc5f into llvm:main Jun 10, 2024
7 checks passed
@c8ef c8ef deleted the i-92538 branch June 10, 2024 14:49
arsenm pushed a commit that referenced this pull request Jun 11, 2024
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
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
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InstCombine should fold ldexp(x, zext(i1 y)) to fmul x, (select y, 2.0, 1.0)
5 participants