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

Rajveer Singh Bharadwaj via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 20 06:17:02 PDT 2024


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

Resolves #110591

>From 711b46bac66590f84c67ddee9e359435df7b93aa Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Sun, 20 Oct 2024 18:42:46 +0530
Subject: [PATCH] [InstCombine] Add missing fold for `fsqrt(select(b, c1, c2))
 => select(b, fsqrt(c1), fsqrt(c2))`

Resolves #110591
---
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c8b9f166b16020..911e5baba8a4de 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1699,6 +1699,14 @@ 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);
+    }
+  }
+
   // Test if a FCmpInst instruction is used exclusively by a select as
   // part of a minimum or maximum operation. If so, refrain from doing
   // any other folding. This helps out other analyses which understand



More information about the llvm-commits mailing list