[llvm] [RISCV] Transform fcmp to is.fpclass (PR #120242)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 17 07:59:23 PST 2024


================
@@ -196,6 +199,42 @@ bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) {
   return true;
 }
 
+// The 'fcmp uno/ord/oeq/une/ueq/one/ogt/oge/olt/ole x, 0.0' instructions are
+// equivalent to an FP class test. If the fcmp instruction would be custom
+// lowered or lowered to a libcall, use the is.fpclass intrinsic instead, which
+// is lowered by the back-end without a libcall.
+//
+// This basically reverts the transformations of
+// InstCombinerImpl::foldIntrinsicIsFPClass.
+bool RISCVCodeGenPrepare::visitFCmpInst(FCmpInst &Fcmp) {
+  const auto *TLI = ST->getTargetLowering();
+  const EVT VT = TLI->getValueType(*DL, Fcmp.getOperand(0)->getType());
+  const int ISDOpcode = TLI->InstructionOpcodeToISD(Fcmp.getOpcode());
+
+  auto LegalizeTypeAction = TLI->getTypeAction(Fcmp.getContext(), VT);
+  auto OperationAction = TLI->getOperationAction(ISDOpcode, VT);
+  if ((LegalizeTypeAction != TargetLoweringBase::TypeSoftenFloat &&
+       LegalizeTypeAction != TargetLoweringBase::TypeSoftPromoteHalf) ||
+      OperationAction == TargetLowering::Custom)
+    return false;
----------------
arsenm wrote:

I don't really like having it in codegenprepare in the first place. It really belongs in some combination of DAGCombiner or legalizer, depending on the purpose. The only nice thing is codegenprepare has access to better utilities, like an existing fcmpToClassTest helper and computeKnownFPClass. In principle those should be reimplemented in codegen 

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


More information about the llvm-commits mailing list