[llvm] [AArch64] Add should be adjusted to zero for GlobalISel (PR #195941)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed May 6 03:53:32 PDT 2026


================
@@ -604,6 +604,35 @@ void applyVAshrLshrImm(MachineInstr &MI, MachineRegisterInfo &MRI,
   MI.eraseFromParent();
 }
 
+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());
+}
+
+/// Adjust compares against 1/-1 to a compare against 0.
+static bool shouldBeAdjustedToZero(Register LHS, const APInt &C,
+                                   CmpInst::Predicate &P,
+                                   const MachineRegisterInfo &MRI) {
+  MachineInstr *LHSDef = getDefIgnoringCopies(LHS, MRI);
+  const bool IsAndLHS = LHSDef && LHSDef->getOpcode() == TargetOpcode::G_AND;
+
+  if (C.isOne() && (P == CmpInst::ICMP_SLT || P == CmpInst::ICMP_SGE) &&
+      IsAndLHS) {
+    P = (P == CmpInst::ICMP_SLT) ? CmpInst::ICMP_SLE : CmpInst::ICMP_SGT;
+    return true;
+  }
+
+  if (!MRI.hasOneNonDBGUse(LHS) && !IsAndLHS)
----------------
arsenm wrote:

```suggestion
  if (!IsAndLHS && !MRI.hasOneNonDBGUse(LHS))
```

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


More information about the llvm-commits mailing list