[llvm] Adjust shouldBeAdjustedToZero to also accept one-use cases for -1 (PR #199439)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 24 12:09:29 PDT 2026


https://github.com/LumioseSil created https://github.com/llvm/llvm-project/pull/199439

This allows folding to mi/pl.

>From 89095ec0abb59bcef544e7a9cfe43bf5fdae8e0e Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sun, 24 May 2026 15:05:57 -0400
Subject: [PATCH] Adjust shouldBeAdjustedToZero to also accept one-use cases
 for -1

This allows folding to mi/pl.
---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 08f3d4e0d30ac..b83c995b24bdb 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -3747,7 +3747,7 @@ static bool isLegalArithImmed(uint64_t C) {
   return IsLegal;
 }
 
-bool isLegalCmpImmed(APInt C) {
+bool isLegalCmpImmed(const APInt &C) {
   // Works for negative immediates too, as it can be written as an ADDS
   // instruction with a negated immediate.
   return isLegalArithImmed(C.abs().getZExtValue());
@@ -4218,16 +4218,19 @@ static unsigned getCmpOperandFoldingProfit(SDValue Op, bool AllowExtend) {
 // emitComparison() converts comparison with one or negative one to comparison
 // with 0. Note that this only works for signed comparisons because of how ANDS
 // works.
-static bool shouldBeAdjustedToZero(SDValue LHS, APInt C, ISD::CondCode &CC) {
-  // Only works for ANDS and AND.
-  if (LHS.getOpcode() != ISD::AND && LHS.getOpcode() != AArch64ISD::ANDS)
-    return false;
+static bool shouldBeAdjustedToZero(SDValue LHS, const APInt &C,
+                                   ISD::CondCode &CC) {
 
-  if (C.isOne() && (CC == ISD::SETLT || CC == ISD::SETGE)) {
+  const bool IsAndLHS =
+      LHS.getOpcode() == ISD::AND || LHS.getOpcode() == AArch64ISD::ANDS;
+  if (C.isOne() && (CC == ISD::SETLT || CC == ISD::SETGE) && IsAndLHS) {
     CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
     return true;
   }
 
+  if (!LHS.hasOneUse() && !IsAndLHS)
+    return false;
+
   if (C.isAllOnes() && (CC == ISD::SETLE || CC == ISD::SETGT)) {
     CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
     return true;



More information about the llvm-commits mailing list