[llvm] [AArch64] Have fallback for getSwappedCondition(CC) in the Fold check (NFC) (PR #160753)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 25 11:42:07 PDT 2025


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/160753

>From 41af802963239941ca446c8009f498fc3331a833 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 25 Sep 2025 14:37:04 -0400
Subject: [PATCH 1/2] Have fallback for getSwappedCondition(CC)

NFC for now, but if we simplify nodes where we can remove a compare with 0 and we are stuck with condition codes MI or PL, we are in trouble, as you cannot swap that.
---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index a4c1e265f0e63..0e68d5b570c28 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -25764,8 +25764,11 @@ static SDValue reassociateCSELOperandsForCSE(SDNode *N, SelectionDAG &DAG) {
     // Try again with the operands of the SUBS instruction and the condition
     // swapped. Due to canonicalization, this only helps for non-constant
     // operands of the SUBS instruction.
+    auto NewCC = getSwappedCondition(CC);
+    if (NewCC == AArch64CC::AL)
+      return SDValue();
     std::swap(CmpOpToMatch, CmpOpOther);
-    if (SDValue R = Fold(getSwappedCondition(CC), CmpOpToMatch, CmpOpToMatch))
+    if (SDValue R = Fold(NewCC, CmpOpToMatch, CmpOpToMatch))
       return R;
     return SDValue();
   }

>From 48e7e4b133ae72f7c62af58fe9ea98c0bac0849c Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Thu, 25 Sep 2025 14:41:57 -0400
Subject: [PATCH 2/2] Update AArch64ISelLowering.cpp

---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 0e68d5b570c28..d4e7750b51915 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -25773,7 +25773,7 @@ static SDValue reassociateCSELOperandsForCSE(SDNode *N, SelectionDAG &DAG) {
     return SDValue();
   }
 
-  if ((CC == AArch64CC::EQ || CC == AArch64CC::NE) && !CmpOpConst->isZero())
+  if ((CC == AArch64CC::EQ || CC == AArch64CC::NE || CC == AArch64CC::MI || CC == AArch64CC::PL) && !CmpOpConst->isZero())
     return SDValue();
 
   // Next, search for a subtraction with a slightly different constant. By
@@ -25805,6 +25805,7 @@ static SDValue reassociateCSELOperandsForCSE(SDNode *N, SelectionDAG &DAG) {
   case AArch64CC::HS:
     return CheckedFold(!CmpOpConst->getAPIntValue().isZero(),
                        CmpOpConst->getAPIntValue() - 1, AArch64CC::HI);
+  case AArch64CC::MI:
   case AArch64CC::LT:
     return CheckedFold(!CmpOpConst->getAPIntValue().isMinSignedValue(),
                        CmpOpConst->getAPIntValue() - 1, AArch64CC::LE);
@@ -25814,6 +25815,7 @@ static SDValue reassociateCSELOperandsForCSE(SDNode *N, SelectionDAG &DAG) {
   case AArch64CC::GT:
     return CheckedFold(!CmpOpConst->getAPIntValue().isMaxSignedValue(),
                        CmpOpConst->getAPIntValue() + 1, AArch64CC::GE);
+  case AArch64CC::PL:
   case AArch64CC::GE:
     return CheckedFold(!CmpOpConst->getAPIntValue().isMinSignedValue(),
                        CmpOpConst->getAPIntValue() - 1, AArch64CC::GT);



More information about the llvm-commits mailing list