[llvm] [AArch64] Use isKnownNonZero to optimize eligible compares to cmn and ccmn (PR #96349)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 07:05:11 PDT 2024


================
@@ -3566,13 +3574,21 @@ static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS,
       Opcode = AArch64ISD::CCMN;
       RHS = DAG.getConstant(Imm.abs(), DL, Const->getValueType(0));
     }
-  } else if (RHS.getOpcode() == ISD::SUB) {
-    SDValue SubOp0 = RHS.getOperand(0);
-    if (isNullConstant(SubOp0) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
-      // See emitComparison() on why we can only do this for SETEQ and SETNE.
-      Opcode = AArch64ISD::CCMN;
-      RHS = RHS.getOperand(1);
-    }
+  } else if (isCMN(RHS, CC, DAG)) {
+    Opcode = AArch64ISD::CCMN;
+    RHS = RHS.getOperand(1);
+  } else if (isCMN(LHS, CC, DAG) &&
+             (isIntEqualitySetCC(CC) ||
+              (isUnsignedIntSetCC(CC) && DAG.isKnownNeverZero(RHS)) ||
+              (isSignedIntSetCC(CC) && cannotBeIntMin(RHS, DAG)))) {
----------------
AtariDreams wrote:

> I think the and_ult_eq_s0s1_or case was mis-compiling because of
> 
> ```
>     // Swap LHS and RHS if it wasn't an equality comparison
>     // So we don't have to worry about changing the CC
>     // a < b -> -b < -a
>     std::swap(LHS, RHS);
> ```
> 
> The condition needed to be updated too.

The condition does not need to be updated because:

a < b -> -a > -b -> -b < -a

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


More information about the llvm-commits mailing list