[llvm] [DAG][AArch64] Lower to FRINTZ instructions (PR #198477)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 05:02:12 PDT 2026


================
@@ -19959,13 +19959,23 @@ static SDValue foldFPToIntToFP(SDNode *N, const SDLoc &DL, SelectionDAG &DAG,
 
   // FIXME: We should be able to use node-level FMF here.
   EVT VT = N->getValueType(0);
-  if (!TLI.isOperationLegal(ISD::FTRUNC, VT))
+  if (!TLI.isOperationLegalOrCustom(ISD::FTRUNC, VT))
     return SDValue();
 
   bool IsUnsigned = N->getOpcode() == ISD::UINT_TO_FP;
   bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP;
   assert(IsSigned || IsUnsigned);
 
+  // Don't fold if the individual cast operations are already legal,
+  // as FTRUNC may have a more expensive custom expansion.
+  EVT IntVT = N->getOperand(0).getValueType();
+  EVT LegalIntVT = TLI.getTypeToTransformTo(*DAG.getContext(), IntVT);
+  unsigned FPToIntOp = IsUnsigned ? ISD::FP_TO_UINT : ISD::FP_TO_SINT;
+  unsigned IntToFPOp = N->getOpcode(); // UINT_TO_FP or SINT_TO_FP
+  if (TLI.isOperationLegal(FPToIntOp, LegalIntVT) &&
+      TLI.isOperationLegal(IntToFPOp, VT))
----------------
MacDue wrote:

I think we'd only want to do this when `FTRUNC` is not legal, right? If it's legal, then `frunc` is an improvement over a pair of to/from int. 
```suggestion
  if (!TLI.isOperationLegal(ISD::FTRUNC, VT) && 
      TLI.isOperationLegal(FPToIntOp, LegalIntVT) &&
      TLI.isOperationLegal(IntToFPOp, VT))
```

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


More information about the llvm-commits mailing list