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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 9 16:39:48 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
----------------
AZero13 wrote:

Okay, so what I meant is that we do not update flags when making nodes, so that information is lost. If we carried that, then knownbits would not be needed. I worded it badly. 

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


More information about the llvm-commits mailing list