[llvm] [NFC][AArch64] Add getRelaxedBranchCondition TII hook (PR #190850)

Hussam Alhassan via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 13:51:32 PDT 2026


https://github.com/hussam-alhassan created https://github.com/llvm/llvm-project/pull/190850

Add a getRelaxedBranchCondition hook to AArch64InstrInfo that returns the non-strict form of a strict branch condition (GT->GE, LT->LE, HI->HS, LO->LS), or -1 if the condition has no relaxed form. Update AArch64ConditionOptimizer to use it, and remove the local getAdjustedCmp helper.

>From 3984d08efa4d7d2d5dcea86a6bf37b5f037852d2 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hsm.link at proton.me>
Date: Tue, 7 Apr 2026 13:21:00 +0100
Subject: [PATCH] [NFC][AArch64] Add getRelaxedBranchCondition TII hook

Add a getRelaxedBranchCondition hook to AArch64InstrInfo that
returns the non-strict form of a strict branch condition (GT->GE,
LT->LE, HI->HS, LO->LS), or -1 if the condition has no relaxed form.
Update AArch64ConditionOptimizer to use it, and remove the local
getAdjustedCmp helper.
---
 .../AArch64/AArch64ConditionOptimizer.cpp     | 28 ++-----------------
 llvm/lib/Target/AArch64/AArch64InstrInfo.cpp  | 15 ++++++++++
 llvm/lib/Target/AArch64/AArch64InstrInfo.h    |  1 +
 3 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
index 4ca75d00d1e39..32423675010b3 100644
--- a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
@@ -309,30 +309,6 @@ static int getComplementOpc(int Opc) {
   }
 }
 
-// Changes form of comparison inclusive <-> exclusive.
-static AArch64CC::CondCode getAdjustedCmp(AArch64CC::CondCode Cmp) {
-  switch (Cmp) {
-  case AArch64CC::GT:
-    return AArch64CC::GE;
-  case AArch64CC::GE:
-    return AArch64CC::GT;
-  case AArch64CC::LT:
-    return AArch64CC::LE;
-  case AArch64CC::LE:
-    return AArch64CC::LT;
-  case AArch64CC::HI:
-    return AArch64CC::HS;
-  case AArch64CC::HS:
-    return AArch64CC::HI;
-  case AArch64CC::LO:
-    return AArch64CC::LS;
-  case AArch64CC::LS:
-    return AArch64CC::LO;
-  default:
-    llvm_unreachable("Unexpected condition code");
-  }
-}
-
 // Returns the adjusted immediate, opcode, and condition code for switching
 // between inclusive/exclusive forms (GT <-> GE, LT <-> LE).
 CmpInfo
@@ -371,7 +347,9 @@ AArch64ConditionOptimizerImpl::getAdjustedCmpInfo(MachineInstr *CmpMI,
     Opc = getComplementOpc(Opc);
   }
 
-  return {NewImm, Opc, getAdjustedCmp(Cmp)};
+  int RelaxedCC = TII->getRelaxedBranchCondition((int)Cmp);
+  assert(RelaxedCC != -1 && "Expected a strict condition code");
+  return {NewImm, Opc, (AArch64CC::CondCode)RelaxedCC};
 }
 
 // Modifies a comparison instruction's immediate and opcode.
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 4094526574d7a..68741daeec47a 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -653,6 +653,21 @@ bool AArch64InstrInfo::reverseBranchCondition(
   return false;
 }
 
+int AArch64InstrInfo::getRelaxedBranchCondition(int CC) const {
+  switch (static_cast<AArch64CC::CondCode>(CC)) {
+  case AArch64CC::GT:
+    return AArch64CC::GE;
+  case AArch64CC::LT:
+    return AArch64CC::LE;
+  case AArch64CC::HI:
+    return AArch64CC::HS;
+  case AArch64CC::LO:
+    return AArch64CC::LS;
+  default:
+    return -1;
+  }
+}
+
 unsigned AArch64InstrInfo::removeBranch(MachineBasicBlock &MBB,
                                         int *BytesRemoved) const {
   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
index a1ec4cbffdf02..bbbb76f88612c 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
@@ -420,6 +420,7 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
 
   bool
   reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
+  int getRelaxedBranchCondition(int CC) const;
   bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
                        Register, Register, Register, int &, int &,
                        int &) const override;



More information about the llvm-commits mailing list