[llvm] [missed-opt][X86] Optimize fptosi+select down to a single cvttsd2si Instruction on X86 (PR #172710)

via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 28 20:42:47 PST 2025


================
@@ -47915,6 +47894,47 @@ static SDValue combineSelect(SDNode *N, SelectionDAG &DAG,
   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
   bool CondConstantVector = ISD::isBuildVectorOfConstantSDNodes(Cond.getNode());
 
+  // select in presence of fp_to_sint can be replaced with just fp_to_sint
+  // fold (SELECT (SETCC (FABS X), MAXFLOAT), (FP_TO_SINT X), INT_MIN)
+  // -> (FP_TO_SINT X)
+  using namespace SDPatternMatch;
+  SDValue T;
+  SDValue FloatConst;
+  if (sd_match(Cond, m_SetCC(m_FAbs(m_Value(T)), m_Value(FloatConst),
+                             m_SpecificCondCode(ISD::SETOLT)))) {
+    if (FloatConst.getOpcode() != ISD::ConstantFP)
+      return SDValue();
+    SDValue FpToInt = LHS;
+    SDValue ConstNode = RHS;
+    if (FpToInt.getOpcode() != ISD::FP_TO_SINT) {
+      std::swap(FpToInt, ConstNode);
+    }
+
+    if (FpToInt.getOpcode() != ISD::FP_TO_SINT) {
+      return SDValue();
+    }
+
+    if (!DAG.isConstantValueOfAnyType(ConstNode)) {
+      return SDValue();
+    }
+
+    if (T != FpToInt.getOperand(0)) {
+      return SDValue();
+    }
+
+    EVT IntVT = FpToInt.getValueType();
+    APInt IntMin = APInt::getSignedMinValue(IntVT.getSizeInBits());
+
+    auto *C = cast<ConstantSDNode>(ConstNode);
+    if (C->getAPIntValue() != IntMin) {
+      return SDValue();
+    }
+
+    // check if the Maxfloat value is matching the value of CmpConst
+    return DAG.getNode(ISD::FP_TO_SINT, DL, FpToInt.getValueType(),
----------------
Bhuvan1527 wrote:

Hi @RKSimon . I tried replacing with target specific node here, however it is throwing the following error when  when running llc on the example test file, 
```
LLVM ERROR: Cannot select: 0x5c3269d7b610: i32 = X86ISD::CVTTS2SI 0x5c3269d7b0d0
  0x5c3269d7b0d0: f64,ch = CopyFromReg 0x5c3269d37ac0, Register:f64 %0
In function: X86ConvF64ValToI32
```
Requesting a suggestion on next steps. 

 

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


More information about the llvm-commits mailing list