[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:22:04 PDT 2024


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

>From 8c0278c443d881894d6906f10917492d5e97c5d5 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
---
 .../Transforms/InstCombine/InstructionCombining.cpp   | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c8b9f166b16020..a4ca9315e4f26f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -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);
+    }
+  }
+
   // 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