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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 9 03:46:57 PDT 2024


================
@@ -2618,6 +2618,18 @@ 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()));
+      Value *Select =
+          Builder.CreateSelect(Cmp, ConstantFP::get(II->getType(), 2.0),
+                               ConstantFP::get(II->getType(), 1.0));
----------------
dtcxzyw wrote:

```suggestion
      Value *Select =
          Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 2.0),
                               ConstantFP::get(II->getType(), 1.0));
```

We always fold `icmp ne i1 X, false -> X`.


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


More information about the llvm-commits mailing list