[llvm] [AArch64] Correctness fix: Turn cmn 0 into cmp 0 (PR #143965)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 13:37:25 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: AZero13 (AZero13)

<details>
<summary>Changes</summary>

When we change the condition, in the edge case of -1, we need to change this from cmn 1 to cmp 0. We were returning cmn 0, which is not the same as cmp 0.

---
Full diff: https://github.com/llvm/llvm-project/pull/143965.diff


1 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp (+7-1) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
index 4c9f8c2723493..f14a8e883447f 100644
--- a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
@@ -255,7 +255,13 @@ AArch64ConditionOptimizer::CmpInfo AArch64ConditionOptimizer::adjustCmp(
   const int OldImm = (int)CmpMI->getOperand(2).getImm();
   const int NewImm = std::abs(OldImm + Correction);
 
-  // Handle +0 -> -1 and -0 -> +1 (CMN with 0 immediate) transitions by
+  // Handle cmn 1 -> cmp 0, because we prefer CMP 0 over cmn 0.
+  if (OldImm == 1 && (Negative && Correction == 1)) {
+    // If we are adjusting from -1 to 0, we need to change the opcode.
+    Opc = getComplementOpc(Opc);
+  }
+
+  // Handle +0 -> -1 and -0 -> +1 (CMN with 0 immediate.) transitions by
   // adjusting compare instruction opcode.
   if (OldImm == 0 && ((Negative && Correction == 1) ||
                       (!Negative && Correction == -1))) {

``````````

</details>


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


More information about the llvm-commits mailing list