[llvm] [GlobalISel][AArch6] Add G_FPTOSI_SAT/G_FPTOUI_SAT (PR #96297)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 05:06:19 PDT 2024


================
@@ -7012,6 +7063,102 @@ LegalizerHelper::LegalizeResult LegalizerHelper::lowerFPTOSI(MachineInstr &MI) {
   return Legalized;
 }
 
+LegalizerHelper::LegalizeResult
+LegalizerHelper::lowerFPTOINT_SAT(MachineInstr &MI) {
+  auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
+
+  bool IsSigned = MI.getOpcode() == TargetOpcode::G_FPTOSI_SAT;
+  unsigned SatWidth = DstTy.getScalarSizeInBits();
+
+  // Determine minimum and maximum integer values and their corresponding
+  // floating-point values.
+  APInt MinInt, MaxInt;
+  if (IsSigned) {
+    MinInt = APInt::getSignedMinValue(SatWidth);
+    MaxInt = APInt::getSignedMaxValue(SatWidth);
+  } else {
+    MinInt = APInt::getMinValue(SatWidth);
+    MaxInt = APInt::getMaxValue(SatWidth);
+  }
+
+  const fltSemantics &Semantics = getFltSemanticForLLT(SrcTy.getScalarType());
+  APFloat MinFloat(Semantics);
+  APFloat MaxFloat(Semantics);
+
+  APFloat::opStatus MinStatus =
+      MinFloat.convertFromAPInt(MinInt, IsSigned, APFloat::rmTowardZero);
+  APFloat::opStatus MaxStatus =
+      MaxFloat.convertFromAPInt(MaxInt, IsSigned, APFloat::rmTowardZero);
+  bool AreExactFloatBounds = !(MinStatus & APFloat::opStatus::opInexact) &&
+                             !(MaxStatus & APFloat::opStatus::opInexact);
+
+  // If the integer bounds are exactly representable as floats and min/max are
+  // legal, emit a min+max+fptoi sequence. Otherwise we have to use a sequence
+  // of comparisons and selects.
+  bool MinMaxLegal = LI.isLegal({TargetOpcode::G_FMINNUM, SrcTy}) &&
+                     LI.isLegal({TargetOpcode::G_FMAXNUM, SrcTy});
+  if (AreExactFloatBounds && MinMaxLegal) {
+    // Clamp Src by MinFloat from below. If Src is NaN the result is MinFloat.
+    auto Max = MIRBuilder.buildFMaxNum(
+        SrcTy, Src, MIRBuilder.buildFConstant(SrcTy, MinFloat));
+    // Clamp by MaxFloat from above. NaN cannot occur.
----------------
arsenm wrote:

Then add the nnan flag? 

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


More information about the llvm-commits mailing list