[llvm] [InstCombine] Transform high latency, dependent FSQRT/FDIV into FMUL (PR #87474)

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 8 11:31:15 PST 2025


================
@@ -666,6 +667,90 @@ Instruction *InstCombinerImpl::foldPowiReassoc(BinaryOperator &I) {
   return nullptr;
 }
 
+// Check legality for transforming
+// x = 1.0/sqrt(a)
+// r1 = x * x;
+// r2 = a/sqrt(a);
+//
+// TO
+//
+// r1 = 1/a
+// r2 = sqrt(a)
+// x = r1 * r2
+// This transform works only when 'a' is known positive.
+static bool isFSqrtDivToFMulLegal(Instruction *X,
+                                  SmallPtrSetImpl<Instruction *> &R1,
+                                  SmallPtrSetImpl<Instruction *> &R2) {
+  BasicBlock *BBx = X->getParent();
+  BasicBlock *BBr1 = (*R1.begin())->getParent();
+  BasicBlock *BBr2 = (*R2.begin())->getParent();
+
+  CallInst *FSqrt = cast<CallInst>(X->getOperand(1));
----------------
andykaylor wrote:

I don't like the use of cast<> here. I understand that you've verified in getFSqrtDivOptPattern() that this will be a call, but the cast here creates a tight coupling between the functions that isn't enforced by the function semantics. That is, someone could call this function without having called the other. I think it makes more sense to combine them or to call getFSqrtDivOptPattern from here and not require it to be called separately by users of this function.

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


More information about the llvm-commits mailing list