[llvm] [SelectionDAG] Improve ucmp/scmp codegen (PR #190787)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 14:00:34 PDT 2026


================
@@ -11638,6 +11638,23 @@ SDValue TargetLowering::expandCMP(SDNode *Node, SelectionDAG &DAG) const {
   EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
   SDLoc dl(Node);
 
+  // fold scmp(x, 0) -> or(sra(x, bw-1), zext(setne(x, 0)))
+  // only when VT is a non-native integer type.
+  if (Opcode == ISD::SCMP && isNullConstant(RHS) && VT.isScalarInteger() &&
+      getTypeAction(*DAG.getContext(), VT) == TypeExpandInteger) {
+    SDValue ShiftAmt =
+        DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, dl);
+    SDValue SignMask = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
+    SDValue IsNZ = DAG.getSetCC(dl, BoolVT, LHS, RHS, ISD::SETNE);
+    SDValue IsNZExt = DAG.getZExtOrTrunc(IsNZ, dl, VT);
+    if (BoolVT.getScalarSizeInBits() != 1 &&
+        getBooleanContents(BoolVT) != ZeroOrOneBooleanContent)
+      IsNZExt =
+          DAG.getNode(ISD::AND, dl, VT, IsNZExt, DAG.getConstant(1, dl, VT));
----------------
arsenm wrote:

I think it's more typical to do something that will be sure to result in 0 or 1, like a select on the condition and let other combines worry about the boolean ext behavior. But this is fine I guess 

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


More information about the llvm-commits mailing list