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

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 02:02:01 PDT 2024


================
@@ -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);
----------------
c8ef wrote:

> 1. Use `Builder.CreateSelect` instead.

Done.

> 2. Use `II->getType()` instead of `Type::getFloatTy`.

Done.

> 3. Please provide alive2 proofs

It seems that alive2 is having trouble emulating the `ldexp` operation. Am I using it incorrectly?

See also:
- https://alive2.llvm.org/ce/z/4Y9XK4

> 4. Please add double tests and vector tests.

Done.


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


More information about the llvm-commits mailing list