[llvm] [AArch64] Signed comparison using CMN is safe when the subtraction is nsw (PR #141993)

David Green via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 11 12:17:24 PDT 2025


================
@@ -3375,8 +3375,20 @@ bool isLegalCmpImmed(APInt C) {
   return isLegalArithImmed(C.abs().getZExtValue());
 }
 
-static bool cannotBeIntMin(SDValue CheckedVal, SelectionDAG &DAG) {
-  KnownBits KnownSrc = DAG.computeKnownBits(CheckedVal);
+static bool isSafeSignedCMN(SDValue Op, SelectionDAG &DAG) {
+  // 0 - INT_MIN sign wraps, so no signed wrap means cmn is safe.
+  if (Op->getFlags().hasNoSignedWrap())
+    return true;
+
+  // We can still figure out if the second operand is safe to use
+  // in a CMN instruction by checking if it is known to be not the minimum
+  // signed value. If it is not, then we can safely use CMN.
+  // FIXME: Can we can remove this check and simply rely on
+  // Op->getFlags().hasNoSignedWrap() once SelectionDAG/ISelLowering never
+  // creates subtract nodes, or SelectionDAG/ISelLowering consistently sets them
----------------
davemgreen wrote:

It currently says "Can we can remove". Maybe just remove the FIXME, if we don't have a plan to address it? Or turn it into a note if required.

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


More information about the llvm-commits mailing list