Skip to content

[AArch64] Correctness fix: Turn cmn 0 into cmp 0 #143965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jun 12, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 12, 2025

@llvm/pr-subscribers-backend-aarch64

Author: AZero13 (AZero13)

Changes

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 --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))) {

@AZero13 AZero13 force-pushed the cmn-compare branch 5 times, most recently from fdf863c to cc1fa2a Compare June 12, 2025 23:35
AZero13 added 2 commits June 13, 2025 11:14
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants