[llvm] [AArch64] Modify intra-block path to use tryOptimizePair (PR #188643)

Hussam Alhassan via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 25 16:47:03 PDT 2026


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

Update optimizeIntraBlock to use the tryOptimizePair method instead of attempting to optimise directly. This unifies optimisation logic between the intra- and cross-block paths and extends intra-block

>From 214c5eed5cb4c8e6c459048492862805b589d70f Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hsm.link at proton.me>
Date: Wed, 25 Mar 2026 21:50:08 +0000
Subject: [PATCH] [AArch64] Modify intra-block path to use tryOptimizePair

Update optimizeIntraBlock to use the tryOptimizePair method instead of
attempting to optimise directly. This unifies optimisation logic between
the intra- and cross-block paths and extends intra-block
---
 .../AArch64/AArch64ConditionOptimizer.cpp     | 49 ++++---------------
 .../AArch64/aarch64-condopt-unsigned.mir      | 31 ++++++++++++
 2 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
index 61fd34a2a25dd..1b9c19a298e0a 100644
--- a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
@@ -597,52 +597,21 @@ bool AArch64ConditionOptimizerImpl::optimizeIntraBlock(MachineBasicBlock &MBB) {
   }
 
   // Extract condition codes from both CSINCs (operand 3)
-  AArch64CC::CondCode FirstCond =
+  AArch64CC::CondCode FirstCondCode =
       (AArch64CC::CondCode)(int)FirstCSINC->getOperand(3).getImm();
-  AArch64CC::CondCode SecondCond =
+  AArch64CC::CondCode SecondCondCode =
       (AArch64CC::CondCode)(int)SecondCSINC->getOperand(3).getImm();
 
-  const int FirstImm = (int)FirstCmpMI->getOperand(2).getImm();
-  const int SecondImm = (int)SecondCmpMI->getOperand(2).getImm();
-
   LLVM_DEBUG(dbgs() << "Comparing intra-block CSINCs: "
-                    << AArch64CC::getCondCodeName(FirstCond) << " #" << FirstImm
-                    << " and " << AArch64CC::getCondCodeName(SecondCond) << " #"
-                    << SecondImm << '\n');
-
-  // Check if both conditions are the same (GT/GT, LT/LT, HI/HI, LO/LO)
-  // and immediates differ by 1.
-  if (FirstCond == SecondCond &&
-      (isGreaterThan(FirstCond) || isLessThan(FirstCond)) &&
-      std::abs(SecondImm - FirstImm) == 1) {
-    // Pick which comparison to adjust to match the other
-    // For GT/HI: adjust the one with smaller immediate
-    // For LT/LO: adjust the one with larger immediate
-    bool adjustFirst = (FirstImm < SecondImm);
-    if (isLessThan(FirstCond)) {
-      adjustFirst = !adjustFirst;
-    }
-
-    MachineInstr *CmpToAdjust = adjustFirst ? FirstCmpMI : SecondCmpMI;
-    MachineInstr *CSINCToAdjust = adjustFirst ? FirstCSINC : SecondCSINC;
-    AArch64CC::CondCode CondToAdjust = adjustFirst ? FirstCond : SecondCond;
-    int TargetImm = adjustFirst ? SecondImm : FirstImm;
-
-    CmpInfo Adj = getAdjustedCmpInfo(CmpToAdjust, CondToAdjust);
-
-    if (Adj.Imm == TargetImm &&
-        Adj.Opc == (adjustFirst ? SecondCmpMI : FirstCmpMI)->getOpcode()) {
-      LLVM_DEBUG(dbgs() << "Successfully optimizing intra-block CSINC pair\n");
+                    << AArch64CC::getCondCodeName(FirstCondCode) << " #"
+                    << FirstCmpMI->getOperand(2).getImm() << " and "
+                    << AArch64CC::getCondCodeName(SecondCondCode) << " #"
+                    << SecondCmpMI->getOperand(2).getImm() << '\n');
 
-      // Modify the selected CMP and CSINC
-      CmpCondPair ToChange{CmpToAdjust, CSINCToAdjust, CondToAdjust};
-      applyCmpAdjustment(ToChange, Adj);
+  CmpCondPair First{FirstCmpMI, FirstCSINC, FirstCondCode};
+  CmpCondPair Second{SecondCmpMI, SecondCSINC, SecondCondCode};
 
-      return true;
-    }
-  }
-
-  return false;
+  return tryOptimizePair(First, Second);
 }
 
 // Optimizes CMP+Bcc pairs across two basic blocks in the dominator tree.
diff --git a/llvm/test/CodeGen/AArch64/aarch64-condopt-unsigned.mir b/llvm/test/CodeGen/AArch64/aarch64-condopt-unsigned.mir
index 5fb2fa6c3b4eb..4269921e1b780 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-condopt-unsigned.mir
+++ b/llvm/test/CodeGen/AArch64/aarch64-condopt-unsigned.mir
@@ -278,6 +278,37 @@ body:             |
     RET undef $lr, implicit $w0
 ...
 
+---
+# CSINC/CSINC with HI/LO (unsigned > and <): imm differs by 2
+# First:  cmp w0, #10; csinc ..., hi  (w0 > 10 unsigned)
+# Second: cmp w0, #12; csinc ..., lo  (w0 < 12 unsigned)
+# Expected: Both adjusted to use immediate 11 with HS/LS respectively
+name:            csinc_hi_lo_intrablock
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0
+    ; CHECK-LABEL: name: csinc_hi_lo_intrablock
+    ; CHECK: liveins: $w0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32common = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY [[COPY]]
+    ; CHECK-NEXT: [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[COPY]], 11, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr [[COPY1]], $wzr, 2, implicit $nzcv
+    ; CHECK-NEXT: [[SUBSWri1:%[0-9]+]]:gpr32 = SUBSWri [[COPY]], 11, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[CSINCWr1:%[0-9]+]]:gpr32 = CSINCWr [[COPY1]], $wzr, 9, implicit $nzcv
+    ; CHECK-NEXT: $w0 = COPY [[CSINCWr1]]
+    ; CHECK-NEXT: RET undef $lr, implicit $w0
+    %0:gpr32common = COPY $w0
+    %1:gpr32 = COPY %0
+    %2:gpr32 = SUBSWri %0, 10, 0, implicit-def $nzcv
+    %3:gpr32 = CSINCWr %1, $wzr, 8, implicit $nzcv
+    %4:gpr32 = SUBSWri %0, 12, 0, implicit-def $nzcv
+    %5:gpr32 = CSINCWr %1, $wzr, 3, implicit $nzcv
+    $w0 = COPY %5
+    RET undef $lr, implicit $w0
+...
+
 ---
 # Test: HI with immediate 0 can be optimized by adjusting upward
 # Head: cmp w0, #0; b.hi  (w0 > 0 unsigned)



More information about the llvm-commits mailing list