[clang] [llvm] [LoongArch] [CodeGen] Add options for Clang to generate LoongArch-specific frecipe & frsqrte instructions (PR #109917)

Lu Weining via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 10 20:22:10 PDT 2024


================
@@ -5902,6 +5914,92 @@ Register LoongArchTargetLowering::getExceptionSelectorRegister(
   return LoongArch::R5;
 }
 
+//===----------------------------------------------------------------------===//
+// Target Optimization Hooks
+//===----------------------------------------------------------------------===//
+
+static int getEstimateRefinementSteps(EVT VT,
+                                      const LoongArchSubtarget &Subtarget) {
+  // Feature FRECIPE instrucions relative accuracy is 2^-14.
+  // IEEE float has 23 digits and double has 52 digits.
+  int RefinementSteps = VT.getScalarType() == MVT::f64 ? 2 : 1;
+  return RefinementSteps;
+}
+
+SDValue LoongArchTargetLowering::getSqrtEstimate(SDValue Operand,
+                                                 SelectionDAG &DAG, int Enabled,
+                                                 int &RefinementSteps,
+                                                 bool &UseOneConstNR,
+                                                 bool Reciprocal) const {
+  if (Subtarget.hasFrecipe()) {
+    SDLoc DL(Operand);
+    EVT VT = Operand.getValueType();
+    unsigned Opcode;
+
+    if (VT == MVT::f32) {
+      Opcode = LoongArchISD::FRSQRTE_S;
+    } else if (VT == MVT::f64 && Subtarget.hasBasicD()) {
+      Opcode = LoongArchISD::FRSQRTE_D;
+    } else if (VT == MVT::v4f32 && Subtarget.hasExtLSX()) {
+      Opcode = LoongArchISD::VFRSQRTE_S;
+    } else if (VT == MVT::v2f64 && Subtarget.hasExtLSX()) {
+      Opcode = LoongArchISD::VFRSQRTE_D;
+    } else if (VT == MVT::v8f32 && Subtarget.hasExtLASX()) {
+      Opcode = LoongArchISD::XVFRSQRTE_S;
+    } else if (VT == MVT::v4f64 && Subtarget.hasExtLASX()) {
+      Opcode = LoongArchISD::XVFRSQRTE_D;
+    } else {
+      return SDValue();
+    }
+
+    UseOneConstNR = false;
+    if (RefinementSteps == ReciprocalEstimate::Unspecified)
+      RefinementSteps = getEstimateRefinementSteps(VT, Subtarget);
+
+    SDValue Estimate = DAG.getNode(Opcode, DL, VT, Operand);
+    if (Reciprocal) {
----------------
SixWeining wrote:

Useless `{}`.

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


More information about the cfe-commits mailing list