[llvm] [InstCombine] Add missing fold for `fsqrt(select(b, c1, c2)) => select(b, fsqrt(c1), fsqrt(c2))` (PR #113084)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 20 06:41:34 PDT 2024


================
@@ -1699,6 +1699,17 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
   if (SI->getType()->isIntOrIntVectorTy(1))
     return nullptr;
 
+  if (auto *II = dyn_cast<IntrinsicInst>(&Op)) {
+    if (II->getIntrinsicID() == Intrinsic::sqrt) {
+      Value *NewTV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, TV, nullptr,
+                                                  "sqrt_fold");
+      Value *NewFV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, FV, nullptr,
+                                                  "sqrt_fold");
+      return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr,
+                                SI);
+    }
+  }
----------------
goldsteinn wrote:

As this currently exists, it will also fold if one of the arms is not constant.

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


More information about the llvm-commits mailing list