[llvm] GlobalISel needs fdiv 1 / sqrt(x) to rsq combine (PR #78673)

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 28 23:29:42 PST 2024


================
@@ -33,6 +33,14 @@ def rcp_sqrt_to_rsq : GICombineRule<
          [{ return matchRcpSqrtToRsq(*${rcp}, ${matchinfo}); }]),
   (apply [{ Helper.applyBuildFn(*${rcp}, ${matchinfo}); }])>;
 
+def fdiv_1_by_sqrt_to_rsq : GICombineRule<
+  (defs root:$root),
+  (match (G_FSQRT $sqrt, $x, (MIFlags FmContract)),
+         (G_FCONSTANT $one, $fpimm),
+         (G_FDIV $dst, $one, $sqrt, (MIFlags FmContract)):$root,
+         [{ return ${fpimm}.getFPImm()->isExactlyValue(1.0)
+            || ${fpimm}.getFPImm()->isExactlyValue(-1.0); }]),
----------------
Pierre-vh wrote:

`right_identity_one_fp` will take care of the `1 * rsq`, it seems like we don't have one for `-1.0` to `G_FNEG` though so you can do it in a follow up patch if you want. `X * 1` and `X * -1` are generic simplifications that work for anything in X

So no, you don't need to reimplement much, just make the matcher more generic (don't check for +-1.0) and in the apply, you always emit `rsq(x) * y`. Prefer to emit the `y` on the rhs, that way if it's a constant it's already the canonical form for mul (constants are always on the RHS for commutative ops IIRC)

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


More information about the llvm-commits mailing list