[llvm] [AArch64][ISel] Enable profile-aware branch condition merging (PR #201486)

Kunal Pathak via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 22 07:50:09 PDT 2026


https://github.com/kunalspathak updated https://github.com/llvm/llvm-project/pull/201486

>From ed2a2689689d224a6ee6c6432172eee43c0acb22 Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Wed, 3 Jun 2026 17:58:33 -0700
Subject: [PATCH 1/8] [AArch64][ISel] Enable profile-aware branch condition
 merging via getJumpConditionMergingParams

AArch64 previously inherited the default {-1, -1, -1} for
`getJumpConditionMergingParams``, causing `shouldKeepJumpConditionsTogether`
in SelectionDAGBuilder to always return false. This meant compound
branch conditions (br (and/or cond1, cond2)) were always split into
separate basic blocks at the DAG level, and profile data from
BranchProbabilityInfo was never consulted for the merge/split decision.

Override `getJumpConditionMergingParams`` in AArch64TargetLowering with
tunable cl::opt parameters matching the X86 structure. Since CCMP is
part of the base AArch64 ISA, the CCMP bias is applied unconditionally.
Default values: BaseCost=2, CcmpBias=6 (effective threshold 8),
LikelyBias=0, UnlikelyBias=-1.

This enables three improvements:
1. Profile-guided merge/split decisions using BranchProbabilityInfo
2. Smarter compare ordering at the DAG level (e.g., placing large
    immediates in CMP and small ones in CCMP to respect the 0-31
    immediate range)
3. Branch elimination in cases like (a == 5 || b == 32) where the
    post-ISel CCMP pass previously could not merge due to immediate
    range constraints

Godbolt:  https://godbolt.org/z/M8veW617q
---
 .../Target/AArch64/AArch64ISelLowering.cpp    |  53 ++++
 llvm/lib/Target/AArch64/AArch64ISelLowering.h |   4 +
 llvm/test/CodeGen/AArch64/arm64-ccmp.ll       | 235 ++++++++++++------
 3 files changed, 214 insertions(+), 78 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index ec52647135722..e9d0412e45472 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -157,6 +157,47 @@ cl::opt<bool> EnableSVEGISel(
 
 // TODO: This option should be removed once we switch to always using PTRADD in
 // the SelectionDAG.
+static cl::opt<int> BrMergingBaseCostThresh(
+    "aarch64-br-merging-base-cost", cl::init(2),
+    cl::desc(
+        "Sets the cost threshold for when multiple conditionals will be merged "
+        "into one branch versus be split in multiple branches. Merging "
+        "conditionals saves branches at the cost of additional instructions. "
+        "This value sets the instruction cost limit, below which conditionals "
+        "will be merged, and above which conditionals will be split. Set to -1 "
+        "to never merge branches."),
+    cl::Hidden);
+
+static cl::opt<int> BrMergingCcmpBias(
+    "aarch64-br-merging-ccmp-bias", cl::init(6),
+    cl::desc("Increases 'aarch64-br-merging-base-cost' to account for the "
+             "CCMP instruction, which is always available on AArch64 and "
+             "makes merging branch conditions cheaper."),
+    cl::Hidden);
+
+static cl::opt<int> BrMergingLikelyBias(
+    "aarch64-br-merging-likely-bias", cl::init(0),
+    cl::desc("Increases 'aarch64-br-merging-base-cost' in cases that it is "
+             "likely that all conditionals will be executed. For example for "
+             "merging the conditionals (a == b && c > d), if its known that "
+             "a == b is likely, then it is likely that if the conditionals are "
+             "split both sides will be executed, so it may be desirable to "
+             "increase the instruction cost threshold. Set to -1 to never "
+             "merge likely branches."),
+    cl::Hidden);
+
+static cl::opt<int> BrMergingUnlikelyBias(
+    "aarch64-br-merging-unlikely-bias", cl::init(-1),
+    cl::desc(
+        "Decreases 'aarch64-br-merging-base-cost' in cases that it is unlikely "
+        "that all conditionals will be executed. For example for merging "
+        "the conditionals (a == b && c > d), if its known that a == b is "
+        "unlikely, then it is unlikely that if the conditionals are split "
+        "both sides will be executed, so it may be desirable to decrease "
+        "the instruction cost threshold. Set to -1 to never merge unlikely "
+        "branches."),
+    cl::Hidden);
+
 static cl::opt<bool> UseFEATCPACodegen(
     "aarch64-use-featcpa-codegen", cl::Hidden,
     cl::desc("Generate ISD::PTRADD nodes for pointer arithmetic in "
@@ -31529,6 +31570,18 @@ bool AArch64TargetLowering::
   return X.getValueType().isScalarInteger() || NewShiftOpcode == ISD::SHL;
 }
 
+TargetLoweringBase::CondMergingParams
+AArch64TargetLowering::getJumpConditionMergingParams(
+    Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs) const {
+  int BaseCost = BrMergingBaseCostThresh.getValue();
+  // CCMP is always available on AArch64.
+  if (BaseCost >= 0)
+    BaseCost += BrMergingCcmpBias;
+
+  return {BaseCost, BrMergingLikelyBias.getValue(),
+          BrMergingUnlikelyBias.getValue()};
+}
+
 TargetLowering::ShiftLegalizationStrategy
 AArch64TargetLowering::preferredShiftLegalizationStrategy(
     SelectionDAG &DAG, SDNode *N, unsigned int ExpansionFactor) const {
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.h b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
index 7e4c4e1ba25ff..20c476d076b47 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.h
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
@@ -441,6 +441,10 @@ class AArch64TargetLowering : public TargetLowering {
   preferredShiftLegalizationStrategy(SelectionDAG &DAG, SDNode *N,
                                      unsigned ExpansionFactor) const override;
 
+  CondMergingParams
+  getJumpConditionMergingParams(Instruction::BinaryOps Opc, const Value *Lhs,
+                                const Value *Rhs) const override;
+
   bool shouldTransformSignedTruncationCheck(EVT XVT,
                                             unsigned KeptBits) const override {
     // For vectors, we don't have a preference..
diff --git a/llvm/test/CodeGen/AArch64/arm64-ccmp.ll b/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
index 71d26d25c0515..b3ed2cf27bd97 100644
--- a/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
@@ -4,18 +4,31 @@
 target triple = "arm64-apple-ios"
 
 define i32 @single_same(i32 %a, i32 %b) nounwind ssp {
-; CHECK-LABEL: single_same:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, #5
-; CHECK-NEXT:    ccmp w1, #17, #4, ne
-; CHECK-NEXT:    b.ne LBB0_2
-; CHECK-NEXT:  ; %bb.1: ; %if.then
-; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-NEXT:    bl _foo
-; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-NEXT:  LBB0_2: ; %if.end
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: single_same:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w1, #17
+; CHECK-SD-NEXT:    ccmp w0, #5, #4, ne
+; CHECK-SD-NEXT:    b.ne LBB0_2
+; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
+; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-SD-NEXT:    bl _foo
+; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-SD-NEXT:  LBB0_2: ; %if.end
+; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: single_same:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, #5
+; CHECK-GI-NEXT:    ccmp w1, #17, #4, ne
+; CHECK-GI-NEXT:    b.ne LBB0_2
+; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
+; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-GI-NEXT:    bl _foo
+; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-GI-NEXT:  LBB0_2: ; %if.end
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 5
   %cmp1 = icmp eq i32 %b, 17
@@ -34,9 +47,9 @@ if.end:
 define i32 @single_different(i32 %a, i32 %b) nounwind ssp {
 ; CHECK-SD-LABEL: single_different:
 ; CHECK-SD:       ; %bb.0: ; %entry
-; CHECK-SD-NEXT:    cmp w0, #6
-; CHECK-SD-NEXT:    ccmp w1, #17, #0, ge
-; CHECK-SD-NEXT:    b.eq LBB1_2
+; CHECK-SD-NEXT:    cmp w1, #17
+; CHECK-SD-NEXT:    ccmp w0, #5, #4, eq
+; CHECK-SD-NEXT:    b.gt LBB1_2
 ; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
 ; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
 ; CHECK-SD-NEXT:    bl _foo
@@ -276,17 +289,31 @@ if.end:
 
 ; Chain multiple compares.
 define void @multi_different(i32 %a, i32 %b, i32 %c) nounwind ssp {
-; CHECK-LABEL: multi_different:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, w1
-; CHECK-NEXT:    sdiv w8, w1, w0
-; CHECK-NEXT:    ccmp w8, #5, #0, gt
-; CHECK-NEXT:    ccmp w8, w2, #4, eq
-; CHECK-NEXT:    b.gt LBB6_2
-; CHECK-NEXT:  ; %bb.1: ; %if.end
-; CHECK-NEXT:    ret
-; CHECK-NEXT:  LBB6_2: ; %if.then
-; CHECK-NEXT:    b _foo
+; CHECK-SD-LABEL: multi_different:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w0, w1
+; CHECK-SD-NEXT:    b.le LBB6_3
+; CHECK-SD-NEXT:  ; %bb.1: ; %land.lhs.true
+; CHECK-SD-NEXT:    sdiv w8, w1, w0
+; CHECK-SD-NEXT:    cmp w8, w2
+; CHECK-SD-NEXT:    ccmp w8, #5, #0, gt
+; CHECK-SD-NEXT:    b.ne LBB6_3
+; CHECK-SD-NEXT:  ; %bb.2: ; %if.then
+; CHECK-SD-NEXT:    b _foo
+; CHECK-SD-NEXT:  LBB6_3: ; %if.end
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: multi_different:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, w1
+; CHECK-GI-NEXT:    sdiv w8, w1, w0
+; CHECK-GI-NEXT:    ccmp w8, #5, #0, gt
+; CHECK-GI-NEXT:    ccmp w8, w2, #4, eq
+; CHECK-GI-NEXT:    b.gt LBB6_2
+; CHECK-GI-NEXT:  ; %bb.1: ; %if.end
+; CHECK-GI-NEXT:    ret
+; CHECK-GI-NEXT:  LBB6_2: ; %if.then
+; CHECK-GI-NEXT:    b _foo
 entry:
   %cmp = icmp sgt i32 %a, %b
   br i1 %cmp, label %land.lhs.true, label %if.end
@@ -308,18 +335,31 @@ if.end:
 
 ; Convert a cbz in the head block.
 define i32 @cbz_head(i32 %a, i32 %b) nounwind ssp {
-; CHECK-LABEL: cbz_head:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, #0
-; CHECK-NEXT:    ccmp w1, #17, #0, ne
-; CHECK-NEXT:    b.eq LBB7_2
-; CHECK-NEXT:  ; %bb.1: ; %if.then
-; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-NEXT:    bl _foo
-; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-NEXT:  LBB7_2: ; %if.end
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: cbz_head:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w1, #17
+; CHECK-SD-NEXT:    ccmp w0, #0, #4, eq
+; CHECK-SD-NEXT:    b.ne LBB7_2
+; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
+; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-SD-NEXT:    bl _foo
+; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-SD-NEXT:  LBB7_2: ; %if.end
+; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: cbz_head:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, #0
+; CHECK-GI-NEXT:    ccmp w1, #17, #0, ne
+; CHECK-GI-NEXT:    b.eq LBB7_2
+; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
+; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-GI-NEXT:    bl _foo
+; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-GI-NEXT:  LBB7_2: ; %if.end
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 0
   %cmp1 = icmp ne i32 %b, 17
@@ -338,22 +378,35 @@ if.end:
 ; smaller range of immediates than subs/adds.
 ; The ccmp immediates must be in the range 0-31.
 define i32 @immediate_range(i32 %a, i32 %b) nounwind ssp {
-; CHECK-LABEL: immediate_range:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, #5
-; CHECK-NEXT:    b.eq LBB8_3
-; CHECK-NEXT:  ; %bb.1: ; %entry
-; CHECK-NEXT:    cmp w1, #32
-; CHECK-NEXT:    b.eq LBB8_3
-; CHECK-NEXT:  ; %bb.2: ; %if.end
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
-; CHECK-NEXT:  LBB8_3: ; %if.then
-; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-NEXT:    bl _foo
-; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: immediate_range:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w1, #32
+; CHECK-SD-NEXT:    ccmp w0, #5, #4, ne
+; CHECK-SD-NEXT:    b.ne LBB8_2
+; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
+; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-SD-NEXT:    bl _foo
+; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-SD-NEXT:  LBB8_2: ; %if.end
+; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: immediate_range:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, #5
+; CHECK-GI-NEXT:    b.eq LBB8_3
+; CHECK-GI-NEXT:  ; %bb.1: ; %entry
+; CHECK-GI-NEXT:    cmp w1, #32
+; CHECK-GI-NEXT:    b.eq LBB8_3
+; CHECK-GI-NEXT:  ; %bb.2: ; %if.end
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
+; CHECK-GI-NEXT:  LBB8_3: ; %if.then
+; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-GI-NEXT:    bl _foo
+; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 5
   %cmp1 = icmp eq i32 %b, 32
@@ -370,18 +423,31 @@ if.end:
 
 ; Convert a cbz in the second block.
 define i32 @cbz_second(i32 %a, i32 %b) nounwind ssp {
-; CHECK-LABEL: cbz_second:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, #0
-; CHECK-NEXT:    ccmp w1, #0, #0, ne
-; CHECK-NEXT:    b.eq LBB9_2
-; CHECK-NEXT:  ; %bb.1: ; %if.then
-; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-NEXT:    bl _foo
-; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-NEXT:  LBB9_2: ; %if.end
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: cbz_second:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w1, #0
+; CHECK-SD-NEXT:    ccmp w0, #0, #4, eq
+; CHECK-SD-NEXT:    b.ne LBB9_2
+; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
+; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-SD-NEXT:    bl _foo
+; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-SD-NEXT:  LBB9_2: ; %if.end
+; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: cbz_second:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, #0
+; CHECK-GI-NEXT:    ccmp w1, #0, #0, ne
+; CHECK-GI-NEXT:    b.eq LBB9_2
+; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
+; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-GI-NEXT:    bl _foo
+; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-GI-NEXT:  LBB9_2: ; %if.end
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 0
   %cmp1 = icmp ne i32 %b, 0
@@ -398,18 +464,31 @@ if.end:
 
 ; Convert a cbnz in the second block.
 define i32 @cbnz_second(i32 %a, i32 %b) nounwind ssp {
-; CHECK-LABEL: cbnz_second:
-; CHECK:       ; %bb.0: ; %entry
-; CHECK-NEXT:    cmp w0, #0
-; CHECK-NEXT:    ccmp w1, #0, #4, ne
-; CHECK-NEXT:    b.ne LBB10_2
-; CHECK-NEXT:  ; %bb.1: ; %if.then
-; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-NEXT:    bl _foo
-; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-NEXT:  LBB10_2: ; %if.end
-; CHECK-NEXT:    mov w0, #7 ; =0x7
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: cbnz_second:
+; CHECK-SD:       ; %bb.0: ; %entry
+; CHECK-SD-NEXT:    cmp w1, #0
+; CHECK-SD-NEXT:    ccmp w0, #0, #4, ne
+; CHECK-SD-NEXT:    b.ne LBB10_2
+; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
+; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-SD-NEXT:    bl _foo
+; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-SD-NEXT:  LBB10_2: ; %if.end
+; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: cbnz_second:
+; CHECK-GI:       ; %bb.0: ; %entry
+; CHECK-GI-NEXT:    cmp w0, #0
+; CHECK-GI-NEXT:    ccmp w1, #0, #4, ne
+; CHECK-GI-NEXT:    b.ne LBB10_2
+; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
+; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-GI-NEXT:    bl _foo
+; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-GI-NEXT:  LBB10_2: ; %if.end
+; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
+; CHECK-GI-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 0
   %cmp1 = icmp eq i32 %b, 0

>From 1d257a79fb7c7203f5108d1d49947f6f5dafce18 Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Wed, 3 Jun 2026 18:13:23 -0700
Subject: [PATCH 2/8] clang format

---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index e9d0412e45472..bf468f2967119 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -31571,8 +31571,9 @@ bool AArch64TargetLowering::
 }
 
 TargetLoweringBase::CondMergingParams
-AArch64TargetLowering::getJumpConditionMergingParams(
-    Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs) const {
+AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
+                                                     const Value *Lhs,
+                                                     const Value *Rhs) const {
   int BaseCost = BrMergingBaseCostThresh.getValue();
   // CCMP is always available on AArch64.
   if (BaseCost >= 0)

>From ee771e6019309581596176b035cd4674436cbd1d Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Thu, 4 Jun 2026 11:37:08 -0700
Subject: [PATCH 3/8] fix test cases

---
 llvm/test/CodeGen/AArch64/aarch64-tbz.ll      | 119 ++++++++----
 llvm/test/CodeGen/AArch64/andorbrcompare.ll   | 112 +++++------
 llvm/test/CodeGen/AArch64/arm64_32.ll         |   2 +-
 .../test/CodeGen/AArch64/br-cond-not-merge.ll |   4 +-
 .../CodeGen/AArch64/branch-cond-split-fcmp.ll |  69 +++----
 llvm/test/CodeGen/AArch64/csr-split.ll        |  18 +-
 llvm/test/CodeGen/AArch64/fcvt-i256.ll        | 182 ++++++++----------
 .../AArch64/lr-reserved-for-ra-live-in.ll     |  16 +-
 llvm/test/CodeGen/AArch64/machine_cse.ll      |   2 +-
 llvm/test/CodeGen/AArch64/peephole-and-tst.ll |  28 ++-
 llvm/test/CodeGen/AArch64/pr166870.ll         |  20 +-
 llvm/test/CodeGen/AArch64/ragreedy-csr.ll     | 180 ++++++++---------
 llvm/test/CodeGen/AArch64/tbl-loops.ll        | 176 +++++++++--------
 llvm/test/CodeGen/AArch64/tbz-tbnz.ll         |  20 +-
 .../umulo-128-legalisation-lowering.ll        |  10 +-
 .../AArch64/instr-ref-target-hooks.ll         |   2 +-
 16 files changed, 464 insertions(+), 496 deletions(-)

diff --git a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
index f895dabba5714..9cfc20b29ecce 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
+++ b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
@@ -5,15 +5,26 @@
 ; RUN: llc -verify-machineinstrs -mtriple=aarch64 -global-isel -O0 < %s | FileCheck %s --check-prefixes=CHECK-GIO0
 
 define void @test1(i64 %A, i64 %B) {
-; CHECK-LABEL: test1:
-; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    tbz w1, #3, .LBB0_3
-; CHECK-NEXT:  // %bb.1: // %entry
-; CHECK-NEXT:    tbz w0, #2, .LBB0_3
-; CHECK-NEXT:  // %bb.2: // %if.then2
-; CHECK-NEXT:    b foo
-; CHECK-NEXT:  .LBB0_3: // %if.end3
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: test1:
+; CHECK-SD:       // %bb.0: // %entry
+; CHECK-SD-NEXT:    tst x0, #0x4
+; CHECK-SD-NEXT:    and x8, x1, #0x8
+; CHECK-SD-NEXT:    ccmp x8, #0, #4, ne
+; CHECK-SD-NEXT:    b.ne .LBB0_2
+; CHECK-SD-NEXT:  // %bb.1: // %if.end3
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB0_2: // %if.then2
+; CHECK-SD-NEXT:    b foo
+;
+; CHECK-GI-LABEL: test1:
+; CHECK-GI:       // %bb.0: // %entry
+; CHECK-GI-NEXT:    tbz w1, #3, .LBB0_3
+; CHECK-GI-NEXT:  // %bb.1: // %entry
+; CHECK-GI-NEXT:    tbz w0, #2, .LBB0_3
+; CHECK-GI-NEXT:  // %bb.2: // %if.then2
+; CHECK-GI-NEXT:    b foo
+; CHECK-GI-NEXT:  .LBB0_3: // %if.end3
+; CHECK-GI-NEXT:    ret
 ;
 ; CHECK-GIO0-LABEL: test1:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -57,16 +68,27 @@ if.end3:                                          ; preds = %if.then2, %entry
 }
 
 define void @test2(i64 %A, ptr readonly %B) #0 {
-; CHECK-LABEL: test2:
-; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    cbz x1, .LBB1_3
-; CHECK-NEXT:  // %bb.1: // %entry
-; CHECK-NEXT:    tbz w0, #3, .LBB1_3
-; CHECK-NEXT:  // %bb.2: // %if.then2
-; CHECK-NEXT:    ldr x1, [x1]
-; CHECK-NEXT:    b foo
-; CHECK-NEXT:  .LBB1_3: // %if.end3
-; CHECK-NEXT:    ret
+; CHECK-SD-LABEL: test2:
+; CHECK-SD:       // %bb.0: // %entry
+; CHECK-SD-NEXT:    tst x0, #0x8
+; CHECK-SD-NEXT:    ccmp x1, #0, #4, ne
+; CHECK-SD-NEXT:    b.ne .LBB1_2
+; CHECK-SD-NEXT:  // %bb.1: // %if.end3
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB1_2: // %if.then2
+; CHECK-SD-NEXT:    ldr x1, [x1]
+; CHECK-SD-NEXT:    b foo
+;
+; CHECK-GI-LABEL: test2:
+; CHECK-GI:       // %bb.0: // %entry
+; CHECK-GI-NEXT:    cbz x1, .LBB1_3
+; CHECK-GI-NEXT:  // %bb.1: // %entry
+; CHECK-GI-NEXT:    tbz w0, #3, .LBB1_3
+; CHECK-GI-NEXT:  // %bb.2: // %if.then2
+; CHECK-GI-NEXT:    ldr x1, [x1]
+; CHECK-GI-NEXT:    b foo
+; CHECK-GI-NEXT:  .LBB1_3: // %if.end3
+; CHECK-GI-NEXT:    ret
 ;
 ; CHECK-GIO0-LABEL: test2:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -111,15 +133,25 @@ if.end3:                                          ; preds = %entry, %if.then2
 
 ; Make sure we use the W variant when log2(mask) is < 32.
 define void @test3(i64 %A, i64 %B) {
-; CHECK-LABEL: test3:
-; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    tbz w1, #3, .LBB2_3
-; CHECK-NEXT:  // %bb.1: // %entry
-; CHECK-NEXT:    tbz w0, #28, .LBB2_3
-; CHECK-NEXT:  // %bb.2: // %if.end3
-; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB2_3: // %if.then2
-; CHECK-NEXT:    b foo
+; CHECK-SD-LABEL: test3:
+; CHECK-SD:       // %bb.0: // %entry
+; CHECK-SD-NEXT:    ubfx x8, x0, #28, #1
+; CHECK-SD-NEXT:    and w8, w8, w1, lsr #3
+; CHECK-SD-NEXT:    tbnz w8, #0, .LBB2_2
+; CHECK-SD-NEXT:  // %bb.1: // %if.then2
+; CHECK-SD-NEXT:    b foo
+; CHECK-SD-NEXT:  .LBB2_2: // %if.end3
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: test3:
+; CHECK-GI:       // %bb.0: // %entry
+; CHECK-GI-NEXT:    tbz w1, #3, .LBB2_3
+; CHECK-GI-NEXT:  // %bb.1: // %entry
+; CHECK-GI-NEXT:    tbz w0, #28, .LBB2_3
+; CHECK-GI-NEXT:  // %bb.2: // %if.end3
+; CHECK-GI-NEXT:    ret
+; CHECK-GI-NEXT:  .LBB2_3: // %if.then2
+; CHECK-GI-NEXT:    b foo
 ;
 ; CHECK-GIO0-LABEL: test3:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -164,15 +196,25 @@ if.end3:                                          ; preds = %if.then2, %entry
 }
 
 define void @test4(i64 %A, i64 %B) {
-; CHECK-LABEL: test4:
-; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    tbz w1, #3, .LBB3_3
-; CHECK-NEXT:  // %bb.1: // %entry
-; CHECK-NEXT:    tbz x0, #35, .LBB3_3
-; CHECK-NEXT:  // %bb.2: // %if.end3
-; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB3_3: // %if.then2
-; CHECK-NEXT:    b foo
+; CHECK-SD-LABEL: test4:
+; CHECK-SD:       // %bb.0: // %entry
+; CHECK-SD-NEXT:    ubfx x8, x0, #35, #1
+; CHECK-SD-NEXT:    and w8, w8, w1, lsr #3
+; CHECK-SD-NEXT:    tbnz w8, #0, .LBB3_2
+; CHECK-SD-NEXT:  // %bb.1: // %if.then2
+; CHECK-SD-NEXT:    b foo
+; CHECK-SD-NEXT:  .LBB3_2: // %if.end3
+; CHECK-SD-NEXT:    ret
+;
+; CHECK-GI-LABEL: test4:
+; CHECK-GI:       // %bb.0: // %entry
+; CHECK-GI-NEXT:    tbz w1, #3, .LBB3_3
+; CHECK-GI-NEXT:  // %bb.1: // %entry
+; CHECK-GI-NEXT:    tbz x0, #35, .LBB3_3
+; CHECK-GI-NEXT:  // %bb.2: // %if.end3
+; CHECK-GI-NEXT:    ret
+; CHECK-GI-NEXT:  .LBB3_3: // %if.then2
+; CHECK-GI-NEXT:    b foo
 ;
 ; CHECK-GIO0-LABEL: test4:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -249,6 +291,3 @@ land.end:
 
 declare void @foo(i64, i64)
 
-;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; CHECK-GI: {{.*}}
-; CHECK-SD: {{.*}}
diff --git a/llvm/test/CodeGen/AArch64/andorbrcompare.ll b/llvm/test/CodeGen/AArch64/andorbrcompare.ll
index 5bc06ec5b65b2..61331eb52a274 100644
--- a/llvm/test/CodeGen/AArch64/andorbrcompare.ll
+++ b/llvm/test/CodeGen/AArch64/andorbrcompare.ll
@@ -9,17 +9,15 @@ define i32 @and_eq_ne_ult(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5,
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #0, ne
-; CHECK-SD-NEXT:    b.eq .LBB0_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.lo .LBB0_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB0_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #0, ne
+; CHECK-SD-NEXT:    b.hs .LBB0_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB0_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_eq_ne_ult:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -60,17 +58,15 @@ define i32 @and_ne_ult_ule(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5,
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #4, lo
-; CHECK-SD-NEXT:    b.ne .LBB1_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.ls .LBB1_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB1_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #0, eq
+; CHECK-SD-NEXT:    b.hi .LBB1_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB1_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_ne_ult_ule:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -111,17 +107,15 @@ define i32 @and_ult_ule_ugt(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #2, ls
-; CHECK-SD-NEXT:    b.lo .LBB2_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.hi .LBB2_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB2_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #2, hs
+; CHECK-SD-NEXT:    b.ls .LBB2_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB2_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_ult_ule_ugt:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -162,17 +156,15 @@ define i32 @and_ule_ugt_uge(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #2, hi
-; CHECK-SD-NEXT:    b.ls .LBB3_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.hs .LBB3_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB3_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #2, hi
+; CHECK-SD-NEXT:    b.lo .LBB3_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB3_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_ule_ugt_uge:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -213,17 +205,15 @@ define i32 @and_ugt_uge_slt(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #0, hs
-; CHECK-SD-NEXT:    b.hi .LBB4_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.lt .LBB4_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB4_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #8, ls
+; CHECK-SD-NEXT:    b.ge .LBB4_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB4_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_ugt_uge_slt:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -264,17 +254,15 @@ define i32 @and_uge_slt_sle(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #0, lt
-; CHECK-SD-NEXT:    b.hs .LBB5_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.le .LBB5_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB5_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #4, lo
+; CHECK-SD-NEXT:    b.gt .LBB5_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB5_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_uge_slt_sle:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -315,17 +303,15 @@ define i32 @and_slt_sle_sgt(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #0, le
-; CHECK-SD-NEXT:    b.lt .LBB6_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.gt .LBB6_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB6_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #0, ge
+; CHECK-SD-NEXT:    b.le .LBB6_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB6_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_slt_sle_sgt:
 ; CHECK-GI:       // %bb.0: // %entry
@@ -366,17 +352,15 @@ define i32 @and_sle_sgt_sge(i32 %s0, i32 %s1, i32 %s2, i32 %s3, i32 %s4, i32 %s5
 ; CHECK-SD:       // %bb.0: // %entry
 ; CHECK-SD-NEXT:    cmp w2, w3
 ; CHECK-SD-NEXT:    ccmp w0, w1, #0, gt
-; CHECK-SD-NEXT:    b.le .LBB7_3
-; CHECK-SD-NEXT:  // %bb.1: // %entry
-; CHECK-SD-NEXT:    cmp w4, w5
-; CHECK-SD-NEXT:    b.ge .LBB7_3
-; CHECK-SD-NEXT:  // %bb.2:
-; CHECK-SD-NEXT:    mov w0, wzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB7_3: // %if
+; CHECK-SD-NEXT:    ccmp w4, w5, #0, gt
+; CHECK-SD-NEXT:    b.lt .LBB7_2
+; CHECK-SD-NEXT:  // %bb.1: // %if
 ; CHECK-SD-NEXT:    mov w0, #1 // =0x1
 ; CHECK-SD-NEXT:    str w0, [x6]
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB7_2:
+; CHECK-SD-NEXT:    mov w0, wzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: and_sle_sgt_sge:
 ; CHECK-GI:       // %bb.0: // %entry
diff --git a/llvm/test/CodeGen/AArch64/arm64_32.ll b/llvm/test/CodeGen/AArch64/arm64_32.ll
index cddadcab9cde1..e08dfcd5def25 100644
--- a/llvm/test/CodeGen/AArch64/arm64_32.ll
+++ b/llvm/test/CodeGen/AArch64/arm64_32.ll
@@ -1,7 +1,7 @@
 ; RUN: llc -mtriple=arm64_32-apple-ios7.0 %s -filetype=obj -o - -disable-post-ra -frame-pointer=non-leaf | \
 ; RUN:     llvm-objdump --private-headers - | \
 ; RUN:     FileCheck %s --check-prefix=CHECK-MACHO
-; RUN: llc -mtriple=arm64_32-apple-ios7.0 %s -o - -aarch64-enable-atomic-cfg-tidy=0 -disable-post-ra -frame-pointer=non-leaf | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT
+; RUN: llc -mtriple=arm64_32-apple-ios7.0 %s -o - -aarch64-enable-atomic-cfg-tidy=0 -disable-post-ra -frame-pointer=non-leaf -aarch64-br-merging-base-cost=-1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OPT
 ; RUN: llc -mtriple=arm64_32-apple-ios7.0 %s -o - -fast-isel -aarch64-enable-atomic-cfg-tidy=0 -disable-post-ra -frame-pointer=non-leaf | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-FAST
 
 ; CHECK-MACHO: Mach header
diff --git a/llvm/test/CodeGen/AArch64/br-cond-not-merge.ll b/llvm/test/CodeGen/AArch64/br-cond-not-merge.ll
index e0ffd81717402..6f5de41287458 100644
--- a/llvm/test/CodeGen/AArch64/br-cond-not-merge.ll
+++ b/llvm/test/CodeGen/AArch64/br-cond-not-merge.ll
@@ -1,5 +1,5 @@
-; RUN: llc -mtriple=aarch64 -verify-machineinstrs < %s | FileCheck --check-prefix=CHECK --check-prefix=OPT %s
-; RUN: llc -mtriple=aarch64 -verify-machineinstrs -O0 -fast-isel=0 -global-isel=false < %s | FileCheck --check-prefix=CHECK --check-prefix=NOOPT %s
+; RUN: llc -mtriple=aarch64 -verify-machineinstrs -aarch64-br-merging-base-cost=-1 < %s | FileCheck --check-prefix=CHECK --check-prefix=OPT %s
+; RUN: llc -mtriple=aarch64 -verify-machineinstrs -aarch64-br-merging-base-cost=-1 -O0 -fast-isel=0 -global-isel=false < %s | FileCheck --check-prefix=CHECK --check-prefix=NOOPT %s
 
 declare void @foo()
 
diff --git a/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll b/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
index 10c958abc71d6..9ed2d29b91096 100644
--- a/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
+++ b/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
@@ -7,19 +7,20 @@ declare i64 @bar()
 define i64 @test_or(float %a, float %b) {
 ; CHECK-SD-LABEL: test_or:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    fcmp s0, #0.0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    b.ne .LBB0_3
-; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    movi d2, #0000000000000000
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    b.ne .LBB0_3
-; CHECK-SD-NEXT:  // %bb.2: // %bb4
+; CHECK-SD-NEXT:    fccmp s0, s2, #0, eq
+; CHECK-SD-NEXT:    cset w8, eq
+; CHECK-SD-NEXT:    tbnz w8, #0, .LBB0_2
+; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB0_2: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
-; CHECK-SD-NEXT:  .LBB0_3: // %common.ret
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_or:
@@ -56,19 +57,19 @@ bb4:
 define i64 @test_or_select(float %a, float %b) {
 ; CHECK-SD-LABEL: test_or_select:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    fcmp s0, #0.0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    b.ne .LBB1_3
-; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    movi d2, #0000000000000000
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    b.ne .LBB1_3
-; CHECK-SD-NEXT:  // %bb.2: // %bb4
+; CHECK-SD-NEXT:    fccmp s0, s2, #0, eq
+; CHECK-SD-NEXT:    b.eq .LBB1_2
+; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB1_2: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
-; CHECK-SD-NEXT:  .LBB1_3: // %common.ret
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_or_select:
@@ -105,23 +106,23 @@ bb4:
 define i64 @test_and(float %a, float %b) {
 ; CHECK-SD-LABEL: test_and:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    fcmp s0, #0.0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    b.mi .LBB2_2
-; CHECK-SD-NEXT:    b.gt .LBB2_2
-; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    movi d2, #0000000000000000
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    b.eq .LBB2_3
-; CHECK-SD-NEXT:    b.vs .LBB2_3
-; CHECK-SD-NEXT:  .LBB2_2: // %common.ret
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB2_3: // %bb4
+; CHECK-SD-NEXT:    fccmp s1, s2, #8, le
+; CHECK-SD-NEXT:    fccmp s0, s2, #0, pl
+; CHECK-SD-NEXT:    fccmp s0, s2, #8, le
+; CHECK-SD-NEXT:    cset w8, pl
+; CHECK-SD-NEXT:    tbz w8, #0, .LBB2_2
+; CHECK-SD-NEXT:  // %bb.1: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB2_2:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_and:
 ; CHECK-GI:       // %bb.0: // %bb1
@@ -160,23 +161,23 @@ bb4:
 define i64 @test_and_select(float %a, float %b) {
 ; CHECK-SD-LABEL: test_and_select:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    fcmp s0, #0.0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    b.mi .LBB3_2
-; CHECK-SD-NEXT:    b.gt .LBB3_2
-; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    movi d2, #0000000000000000
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    b.eq .LBB3_3
-; CHECK-SD-NEXT:    b.vs .LBB3_3
-; CHECK-SD-NEXT:  .LBB3_2: // %common.ret
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB3_3: // %bb4
+; CHECK-SD-NEXT:    fccmp s1, s2, #8, le
+; CHECK-SD-NEXT:    fccmp s0, s2, #0, pl
+; CHECK-SD-NEXT:    fccmp s0, s2, #8, le
+; CHECK-SD-NEXT:    cset w8, pl
+; CHECK-SD-NEXT:    tbz w8, #0, .LBB3_2
+; CHECK-SD-NEXT:  // %bb.1: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
 ; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB3_2:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_and_select:
 ; CHECK-GI:       // %bb.0: // %bb1
diff --git a/llvm/test/CodeGen/AArch64/csr-split.ll b/llvm/test/CodeGen/AArch64/csr-split.ll
index 7b092b00b9655..816900452ff90 100644
--- a/llvm/test/CodeGen/AArch64/csr-split.ll
+++ b/llvm/test/CodeGen/AArch64/csr-split.ll
@@ -83,13 +83,12 @@ declare signext i32 @callNonVoid(ptr) local_unnamed_addr
 define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-LABEL: test2:
 ; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    cbz x0, .LBB1_3
-; CHECK-NEXT:  // %bb.1: // %entry
 ; CHECK-NEXT:    adrp x8, a
 ; CHECK-NEXT:    ldrsw x8, [x8, :lo12:a]
 ; CHECK-NEXT:    cmp x8, x0
-; CHECK-NEXT:    b.ne .LBB1_3
-; CHECK-NEXT:  // %bb.2: // %if.then2
+; CHECK-NEXT:    ccmp x0, #0, #4, eq
+; CHECK-NEXT:    b.eq .LBB1_2
+; CHECK-NEXT:  // %bb.1: // %if.then2
 ; CHECK-NEXT:    stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w19, -8
@@ -102,21 +101,20 @@ define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-NEXT:    .cfi_restore w19
 ; CHECK-NEXT:    .cfi_restore w30
 ; CHECK-NEXT:    b callNonVoid
-; CHECK-NEXT:  .LBB1_3: // %return
+; CHECK-NEXT:  .LBB1_2: // %return
 ; CHECK-NEXT:    mov w0, wzr
 ; CHECK-NEXT:    ret
 ;
 ; CHECK-APPLE-LABEL: test2:
 ; CHECK-APPLE:       ; %bb.0: ; %entry
-; CHECK-APPLE-NEXT:    cbz x0, LBB1_3
-; CHECK-APPLE-NEXT:  ; %bb.1: ; %entry
 ; CHECK-APPLE-NEXT:  Lloh2:
 ; CHECK-APPLE-NEXT:    adrp x8, _a at PAGE
 ; CHECK-APPLE-NEXT:  Lloh3:
 ; CHECK-APPLE-NEXT:    ldrsw x8, [x8, _a at PAGEOFF]
 ; CHECK-APPLE-NEXT:    cmp x8, x0
-; CHECK-APPLE-NEXT:    b.ne LBB1_3
-; CHECK-APPLE-NEXT:  ; %bb.2: ; %if.then2
+; CHECK-APPLE-NEXT:    ccmp x0, #0, #4, eq
+; CHECK-APPLE-NEXT:    b.eq LBB1_2
+; CHECK-APPLE-NEXT:  ; %bb.1: ; %if.then2
 ; CHECK-APPLE-NEXT:    stp x20, x19, [sp, #-32]! ; 16-byte Folded Spill
 ; CHECK-APPLE-NEXT:    .cfi_def_cfa_offset 32
 ; CHECK-APPLE-NEXT:    stp x29, x30, [sp, #16] ; 16-byte Folded Spill
@@ -135,7 +133,7 @@ define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-APPLE-NEXT:    .cfi_restore w19
 ; CHECK-APPLE-NEXT:    .cfi_restore w20
 ; CHECK-APPLE-NEXT:    b _callNonVoid
-; CHECK-APPLE-NEXT:  LBB1_3: ; %return
+; CHECK-APPLE-NEXT:  LBB1_2: ; %return
 ; CHECK-APPLE-NEXT:    mov w0, wzr
 ; CHECK-APPLE-NEXT:    ret
 ; CHECK-APPLE-NEXT:    .loh AdrpLdr Lloh2, Lloh3
diff --git a/llvm/test/CodeGen/AArch64/fcvt-i256.ll b/llvm/test/CodeGen/AArch64/fcvt-i256.ll
index 75ae703b1332e..7fc10f4574c76 100644
--- a/llvm/test/CodeGen/AArch64/fcvt-i256.ll
+++ b/llvm/test/CodeGen/AArch64/fcvt-i256.ll
@@ -1295,26 +1295,27 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-LABEL: f32_to_s256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fmov w8, s0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ubfx w10, w8, #23, #8
-; CHECK-SD-NEXT:    cmp w10, #127
-; CHECK-SD-NEXT:    b.lo .LBB8_5
-; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fcmp s0, s0
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    b.vs .LBB8_7
-; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    mov w9, #127 // =0x7f
+; CHECK-SD-NEXT:    ubfx w10, w8, #23, #8
+; CHECK-SD-NEXT:    ccmp w10, w9, #0, vc
+; CHECK-SD-NEXT:    b.hs .LBB8_2
+; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    mov x1, xzr
+; CHECK-SD-NEXT:    mov x2, xzr
+; CHECK-SD-NEXT:    mov x3, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB8_2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    cmp w10, #255
-; CHECK-SD-NEXT:    b.hs .LBB8_8
+; CHECK-SD-NEXT:    b.hs .LBB8_6
 ; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
 ; CHECK-SD-NEXT:    sbfx x9, x8, #31, #1
 ; CHECK-SD-NEXT:    mov w11, #8388608 // =0x800000
 ; CHECK-SD-NEXT:    cmp w10, #149
 ; CHECK-SD-NEXT:    bfxil w11, w8, #0, #23
 ; CHECK-SD-NEXT:    orr x8, x9, #0x1
-; CHECK-SD-NEXT:    b.hi .LBB8_6
+; CHECK-SD-NEXT:    b.hi .LBB8_5
 ; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
 ; CHECK-SD-NEXT:    mov w12, #150 // =0x96
 ; CHECK-SD-NEXT:    sub w10, w12, w10
@@ -1330,12 +1331,7 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-NEXT:    adcs x2, x15, x12
 ; CHECK-SD-NEXT:    adc x3, x11, x9
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_5:
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_6: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB8_5: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
@@ -1396,9 +1392,8 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-NEXT:    adds x2, x11, x12
 ; CHECK-SD-NEXT:    adc x3, x8, x9
 ; CHECK-SD-NEXT:    add sp, sp, #64
-; CHECK-SD-NEXT:  .LBB8_7: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_8: // %fp-to-i-if-saturate
+; CHECK-SD-NEXT:  .LBB8_6: // %fp-to-i-if-saturate
 ; CHECK-SD-NEXT:    // kill: def $w8 killed $w8 killed $x8 def $x8
 ; CHECK-SD-NEXT:    sbfx x8, x8, #31, #1
 ; CHECK-SD-NEXT:    mvn x0, x8
@@ -1572,46 +1567,41 @@ define i256 @f32_to_s256_sat(float %val) {
 define i256 @f32_to_u256_sat(float %val) {
 ; CHECK-SD-LABEL: f32_to_u256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
-; CHECK-SD-NEXT:    fmov w9, s0
+; CHECK-SD-NEXT:    fmov w10, s0
+; CHECK-SD-NEXT:    mov w9, #127 // =0x7f
+; CHECK-SD-NEXT:    cmp w10, #0
+; CHECK-SD-NEXT:    ubfx w8, w10, #23, #8
+; CHECK-SD-NEXT:    fccmp s0, s0, #1, pl
+; CHECK-SD-NEXT:    ccmp w8, w9, #0, vc
+; CHECK-SD-NEXT:    b.hs .LBB9_2
+; CHECK-SD-NEXT:  // %bb.1:
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ubfx w8, w9, #23, #8
-; CHECK-SD-NEXT:    cmp w8, #127
-; CHECK-SD-NEXT:    b.lo .LBB9_6
-; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
-; CHECK-SD-NEXT:    fcmp s0, s0
-; CHECK-SD-NEXT:    b.vs .LBB9_6
-; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-entry
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    tbnz w9, #31, .LBB9_8
-; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    mov x1, xzr
+; CHECK-SD-NEXT:    mov x2, xzr
+; CHECK-SD-NEXT:    mov x3, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB9_2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    cmp w8, #254
-; CHECK-SD-NEXT:    b.hi .LBB9_9
-; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-check.exp.size
-; CHECK-SD-NEXT:    mov w10, #8388608 // =0x800000
+; CHECK-SD-NEXT:    b.hi .LBB9_6
+; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
+; CHECK-SD-NEXT:    mov w9, #8388608 // =0x800000
 ; CHECK-SD-NEXT:    cmp w8, #149
-; CHECK-SD-NEXT:    bfxil w10, w9, #0, #23
-; CHECK-SD-NEXT:    b.hi .LBB9_7
-; CHECK-SD-NEXT:  // %bb.5: // %fp-to-i-if-exp.small
-; CHECK-SD-NEXT:    mov w9, #150 // =0x96
+; CHECK-SD-NEXT:    bfxil w9, w10, #0, #23
+; CHECK-SD-NEXT:    b.hi .LBB9_5
+; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
+; CHECK-SD-NEXT:    mov w10, #150 // =0x96
 ; CHECK-SD-NEXT:    mov x1, xzr
 ; CHECK-SD-NEXT:    mov x2, xzr
-; CHECK-SD-NEXT:    sub w8, w9, w8
+; CHECK-SD-NEXT:    sub w8, w10, w8
 ; CHECK-SD-NEXT:    mov x3, xzr
-; CHECK-SD-NEXT:    lsr w0, w10, w8
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB9_6:
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    lsr w0, w9, w8
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB9_7: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB9_5: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
 ; CHECK-SD-NEXT:    sub w8, w8, #150
-; CHECK-SD-NEXT:    str x10, [sp, #32]
+; CHECK-SD-NEXT:    str x9, [sp, #32]
 ; CHECK-SD-NEXT:    lsr x9, x8, #3
 ; CHECK-SD-NEXT:    mov x10, sp
 ; CHECK-SD-NEXT:    str xzr, [sp, #56]
@@ -1640,9 +1630,8 @@ define i256 @f32_to_u256_sat(float %val) {
 ; CHECK-SD-NEXT:    orr x2, x10, x15
 ; CHECK-SD-NEXT:    orr x1, x11, x13
 ; CHECK-SD-NEXT:    add sp, sp, #64
-; CHECK-SD-NEXT:  .LBB9_8: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB9_9:
+; CHECK-SD-NEXT:  .LBB9_6:
 ; CHECK-SD-NEXT:    mov x0, #-1 // =0xffffffffffffffff
 ; CHECK-SD-NEXT:    mov x1, #-1 // =0xffffffffffffffff
 ; CHECK-SD-NEXT:    mov x2, #-1 // =0xffffffffffffffff
@@ -1740,26 +1729,27 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-LABEL: f64_to_s256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fmov x9, d0
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ubfx x10, x9, #52, #11
-; CHECK-SD-NEXT:    cmp x10, #1023
-; CHECK-SD-NEXT:    b.lo .LBB10_5
-; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fcmp d0, d0
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    b.vs .LBB10_7
-; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    mov w8, #1023 // =0x3ff
+; CHECK-SD-NEXT:    ubfx x10, x9, #52, #11
+; CHECK-SD-NEXT:    ccmp x10, x8, #0, vc
+; CHECK-SD-NEXT:    b.hs .LBB10_2
+; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    mov x1, xzr
+; CHECK-SD-NEXT:    mov x2, xzr
+; CHECK-SD-NEXT:    mov x3, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB10_2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    asr x8, x9, #63
 ; CHECK-SD-NEXT:    cmp x10, #1278
-; CHECK-SD-NEXT:    b.hs .LBB10_8
+; CHECK-SD-NEXT:    b.hs .LBB10_6
 ; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
 ; CHECK-SD-NEXT:    mov x11, #4503599627370496 // =0x10000000000000
 ; CHECK-SD-NEXT:    cmp x10, #1074
 ; CHECK-SD-NEXT:    bfxil x11, x9, #0, #52
 ; CHECK-SD-NEXT:    orr x9, x8, #0x1
-; CHECK-SD-NEXT:    b.hi .LBB10_6
+; CHECK-SD-NEXT:    b.hi .LBB10_5
 ; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
 ; CHECK-SD-NEXT:    mov w12, #1075 // =0x433
 ; CHECK-SD-NEXT:    sub x10, x12, x10
@@ -1774,12 +1764,7 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-NEXT:    adcs x2, x11, x8
 ; CHECK-SD-NEXT:    adc x3, x12, x11
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_5:
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_6: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB10_5: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
@@ -1840,9 +1825,8 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-NEXT:    adds x2, x11, x12
 ; CHECK-SD-NEXT:    adc x3, x9, x8
 ; CHECK-SD-NEXT:    add sp, sp, #64
-; CHECK-SD-NEXT:  .LBB10_7: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_8: // %fp-to-i-if-saturate
+; CHECK-SD-NEXT:  .LBB10_6: // %fp-to-i-if-saturate
 ; CHECK-SD-NEXT:    mvn x0, x8
 ; CHECK-SD-NEXT:    eor x3, x8, #0x7fffffffffffffff
 ; CHECK-SD-NEXT:    mov x1, x0
@@ -2013,46 +1997,41 @@ define i256 @f64_to_s256_sat(double %val) {
 define i256 @f64_to_u256_sat(double %val) {
 ; CHECK-SD-LABEL: f64_to_u256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
-; CHECK-SD-NEXT:    fmov x9, d0
+; CHECK-SD-NEXT:    fmov x10, d0
+; CHECK-SD-NEXT:    mov w9, #1023 // =0x3ff
+; CHECK-SD-NEXT:    cmp x10, #0
+; CHECK-SD-NEXT:    ubfx x8, x10, #52, #11
+; CHECK-SD-NEXT:    fccmp d0, d0, #1, pl
+; CHECK-SD-NEXT:    ccmp x8, x9, #0, vc
+; CHECK-SD-NEXT:    b.hs .LBB11_2
+; CHECK-SD-NEXT:  // %bb.1:
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ubfx x8, x9, #52, #11
-; CHECK-SD-NEXT:    cmp x8, #1023
-; CHECK-SD-NEXT:    b.lo .LBB11_6
-; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
-; CHECK-SD-NEXT:    fcmp d0, d0
-; CHECK-SD-NEXT:    b.vs .LBB11_6
-; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-entry
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
-; CHECK-SD-NEXT:    tbnz x9, #63, .LBB11_8
-; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    mov x1, xzr
+; CHECK-SD-NEXT:    mov x2, xzr
+; CHECK-SD-NEXT:    mov x3, xzr
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB11_2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    cmp x8, #1278
-; CHECK-SD-NEXT:    b.hi .LBB11_9
-; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-check.exp.size
-; CHECK-SD-NEXT:    mov x10, #4503599627370496 // =0x10000000000000
+; CHECK-SD-NEXT:    b.hi .LBB11_6
+; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
+; CHECK-SD-NEXT:    mov x9, #4503599627370496 // =0x10000000000000
 ; CHECK-SD-NEXT:    cmp x8, #1074
-; CHECK-SD-NEXT:    bfxil x10, x9, #0, #52
-; CHECK-SD-NEXT:    b.hi .LBB11_7
-; CHECK-SD-NEXT:  // %bb.5: // %fp-to-i-if-exp.small
-; CHECK-SD-NEXT:    mov w9, #1075 // =0x433
+; CHECK-SD-NEXT:    bfxil x9, x10, #0, #52
+; CHECK-SD-NEXT:    b.hi .LBB11_5
+; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
+; CHECK-SD-NEXT:    mov w10, #1075 // =0x433
 ; CHECK-SD-NEXT:    mov x1, xzr
 ; CHECK-SD-NEXT:    mov x2, xzr
-; CHECK-SD-NEXT:    sub x8, x9, x8
+; CHECK-SD-NEXT:    sub x8, x10, x8
 ; CHECK-SD-NEXT:    mov x3, xzr
-; CHECK-SD-NEXT:    lsr x0, x10, x8
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB11_6:
-; CHECK-SD-NEXT:    mov x1, x0
-; CHECK-SD-NEXT:    mov x2, x0
-; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    lsr x0, x9, x8
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB11_7: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB11_5: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
 ; CHECK-SD-NEXT:    sub x8, x8, #1075
-; CHECK-SD-NEXT:    str x10, [sp, #32]
+; CHECK-SD-NEXT:    str x9, [sp, #32]
 ; CHECK-SD-NEXT:    lsr x9, x8, #3
 ; CHECK-SD-NEXT:    mov x10, sp
 ; CHECK-SD-NEXT:    str xzr, [sp, #56]
@@ -2081,9 +2060,8 @@ define i256 @f64_to_u256_sat(double %val) {
 ; CHECK-SD-NEXT:    orr x2, x10, x15
 ; CHECK-SD-NEXT:    orr x1, x11, x13
 ; CHECK-SD-NEXT:    add sp, sp, #64
-; CHECK-SD-NEXT:  .LBB11_8: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB11_9:
+; CHECK-SD-NEXT:  .LBB11_6:
 ; CHECK-SD-NEXT:    mov x0, #-1 // =0xffffffffffffffff
 ; CHECK-SD-NEXT:    mov x1, #-1 // =0xffffffffffffffff
 ; CHECK-SD-NEXT:    mov x2, #-1 // =0xffffffffffffffff
diff --git a/llvm/test/CodeGen/AArch64/lr-reserved-for-ra-live-in.ll b/llvm/test/CodeGen/AArch64/lr-reserved-for-ra-live-in.ll
index 708ba621c26d8..09107860c802b 100644
--- a/llvm/test/CodeGen/AArch64/lr-reserved-for-ra-live-in.ll
+++ b/llvm/test/CodeGen/AArch64/lr-reserved-for-ra-live-in.ll
@@ -8,23 +8,17 @@ declare void @spam()
 define i32 @check_lr_liveness(ptr %arg) #1 {
   ; CHECK-LABEL: name: check_lr_liveness
   ; CHECK: bb.0.bb:
-  ; CHECK-NEXT:   successors: %bb.4(0x20000000), %bb.1(0x60000000)
+  ; CHECK-NEXT:   successors: %bb.1, %bb.2
   ; CHECK-NEXT:   liveins: $x0, $lr
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   renamable $x8 = COPY $x0
-  ; CHECK-NEXT:   renamable $w0 = MOVi32imm -536870206
-  ; CHECK-NEXT:   CBNZX killed renamable $x8, %bb.1
+  ; CHECK-NEXT:   CBNZX killed renamable $x0, %bb.2
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.4:
-  ; CHECK-NEXT:   liveins: $w0, $lr
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   liveins: $lr
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $w0 = MOVi32imm -536870206
   ; CHECK-NEXT:   B %bb.3
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.1.bb:
-  ; CHECK-NEXT:   liveins: $w0, $lr
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   B %bb.2
-  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.bb1:
   ; CHECK-NEXT:   liveins: $lr
   ; CHECK-NEXT: {{  $}}
diff --git a/llvm/test/CodeGen/AArch64/machine_cse.ll b/llvm/test/CodeGen/AArch64/machine_cse.ll
index 6478f5a37f782..e8672bc47d2b2 100644
--- a/llvm/test/CodeGen/AArch64/machine_cse.ll
+++ b/llvm/test/CodeGen/AArch64/machine_cse.ll
@@ -1,4 +1,4 @@
-; RUN: llc < %s -mtriple=aarch64-linux-gnuabi -O2 -tail-dup-placement=0 | FileCheck %s
+; RUN: llc < %s -mtriple=aarch64-linux-gnuabi -O2 -tail-dup-placement=0 -aarch64-br-merging-base-cost=-1 | FileCheck %s
 ; -tail-dup-placement causes tail duplication during layout. This breaks the
 ; assumptions of the test case as written (specifically, it creates an
 ; additional cmp instruction, creating a false positive), so we pass
diff --git a/llvm/test/CodeGen/AArch64/peephole-and-tst.ll b/llvm/test/CodeGen/AArch64/peephole-and-tst.ll
index 74b0e69d1b05b..6449f5d5f07d3 100644
--- a/llvm/test/CodeGen/AArch64/peephole-and-tst.ll
+++ b/llvm/test/CodeGen/AArch64/peephole-and-tst.ll
@@ -13,27 +13,23 @@ define i32 @test_func_i32_two_uses(i32 %in, i32 %bit, i32 %mask) {
 ; CHECK-SD-NEXT:    ldr x8, [x8, :got_lo12:ptr_wrapper]
 ; CHECK-SD-NEXT:    ldr x9, [x8]
 ; CHECK-SD-NEXT:    mov w8, wzr
-; CHECK-SD-NEXT:    b .LBB0_3
-; CHECK-SD-NEXT:  .LBB0_1: // in Loop: Header=BB0_3 Depth=1
-; CHECK-SD-NEXT:    str xzr, [x9, #8]
-; CHECK-SD-NEXT:  .LBB0_2: // in Loop: Header=BB0_3 Depth=1
+; CHECK-SD-NEXT:    b .LBB0_2
+; CHECK-SD-NEXT:  .LBB0_1: // in Loop: Header=BB0_2 Depth=1
 ; CHECK-SD-NEXT:    lsl w1, w1, #1
-; CHECK-SD-NEXT:    cbz w1, .LBB0_6
-; CHECK-SD-NEXT:  .LBB0_3: // %do.body
+; CHECK-SD-NEXT:    cbz w1, .LBB0_4
+; CHECK-SD-NEXT:  .LBB0_2: // %do.body
 ; CHECK-SD-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-SD-NEXT:    ands w10, w1, w0
-; CHECK-SD-NEXT:    and w11, w2, w0
+; CHECK-SD-NEXT:    orr w11, w2, w10
 ; CHECK-SD-NEXT:    cinc w8, w8, ne
-; CHECK-SD-NEXT:    cmp w10, w11
-; CHECK-SD-NEXT:    b.eq .LBB0_1
-; CHECK-SD-NEXT:  // %bb.4: // %do.body
-; CHECK-SD-NEXT:    // in Loop: Header=BB0_3 Depth=1
-; CHECK-SD-NEXT:    cbnz w2, .LBB0_1
-; CHECK-SD-NEXT:  // %bb.5: // %do.body
-; CHECK-SD-NEXT:    // in Loop: Header=BB0_3 Depth=1
-; CHECK-SD-NEXT:    cbz w10, .LBB0_2
+; CHECK-SD-NEXT:    cmp w11, #0
+; CHECK-SD-NEXT:    and w11, w2, w0
+; CHECK-SD-NEXT:    ccmp w10, w11, #4, eq
+; CHECK-SD-NEXT:    b.ne .LBB0_1
+; CHECK-SD-NEXT:  // %bb.3: // in Loop: Header=BB0_2 Depth=1
+; CHECK-SD-NEXT:    str xzr, [x9, #8]
 ; CHECK-SD-NEXT:    b .LBB0_1
-; CHECK-SD-NEXT:  .LBB0_6: // %do.end
+; CHECK-SD-NEXT:  .LBB0_4: // %do.end
 ; CHECK-SD-NEXT:    mov w0, w8
 ; CHECK-SD-NEXT:    ret
 ;
diff --git a/llvm/test/CodeGen/AArch64/pr166870.ll b/llvm/test/CodeGen/AArch64/pr166870.ll
index 6f54b0465fbfd..4b3daef3b233d 100644
--- a/llvm/test/CodeGen/AArch64/pr166870.ll
+++ b/llvm/test/CodeGen/AArch64/pr166870.ll
@@ -22,21 +22,18 @@ define i32 @widget(i32 %arg, i32 %arg1, i1 %arg2, ptr %arg3, i1 %arg4) #0 nounwi
 ; CHECK-NEXT:    stp x22, x21, [sp, #16] // 16-byte Folded Spill
 ; CHECK-NEXT:    stp x20, x19, [sp, #32] // 16-byte Folded Spill
 ; CHECK-NEXT:    mov x19, x3
-; CHECK-NEXT:    mov x20, x0
-; CHECK-NEXT:    mov x21, x1
+; CHECK-NEXT:    mov x21, x0
+; CHECK-NEXT:    mov x22, x1
 ; CHECK-NEXT:    bl baz
-; CHECK-NEXT:    mov w0, #0 // =0x0
-; CHECK-NEXT:    mov w10, #1 // =0x1
-; CHECK-NEXT:    cbnz w10, .LBB0_10
+; CHECK-NEXT:    mov w8, #1 // =0x1
+; CHECK-NEXT:    cbnz w8, .LBB0_9
 ; CHECK-NEXT:  // %bb.5: // %bb7
-; CHECK-NEXT:    cbnz w10, .LBB0_9
+; CHECK-NEXT:    cbnz w8, .LBB0_9
 ; CHECK-NEXT:  // %bb.6: // %bb8
-; CHECK-NEXT:    mov x8, x21
-; CHECK-NEXT:    mov x9, x20
 ; CHECK-NEXT:    mov w20, #0 // =0x0
-; CHECK-NEXT:    mov w9, w9
-; CHECK-NEXT:    mov x21, x9
-; CHECK-NEXT:    mov w8, w8
+; CHECK-NEXT:    mov w8, w21
+; CHECK-NEXT:    mov x21, x8
+; CHECK-NEXT:    mov w8, w22
 ; CHECK-NEXT:    mov x22, x8
 ; CHECK-NEXT:  .LBB0_7: // %bb10
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
@@ -48,7 +45,6 @@ define i32 @widget(i32 %arg, i32 %arg1, i1 %arg2, ptr %arg3, i1 %arg4) #0 nounwi
 ; CHECK-NEXT:    cbnz x22, .LBB0_7
 ; CHECK-NEXT:  .LBB0_9:
 ; CHECK-NEXT:    mov w0, #0 // =0x0
-; CHECK-NEXT:  .LBB0_10:
 ; CHECK-NEXT:    ldp x20, x19, [sp, #32] // 16-byte Folded Reload
 ; CHECK-NEXT:    ldp x22, x21, [sp, #16] // 16-byte Folded Reload
 ; CHECK-NEXT:    ldr x30, [sp], #48 // 8-byte Folded Reload
diff --git a/llvm/test/CodeGen/AArch64/ragreedy-csr.ll b/llvm/test/CodeGen/AArch64/ragreedy-csr.ll
index 31f004e8d72b7..4ac7d0f1c34e6 100644
--- a/llvm/test/CodeGen/AArch64/ragreedy-csr.ll
+++ b/llvm/test/CodeGen/AArch64/ragreedy-csr.ll
@@ -24,7 +24,7 @@ define fastcc i32 @prune_match(ptr nocapture readonly %a, ptr nocapture readonly
 ; CHECK-NEXT:    ldrh w8, [x0]
 ; CHECK-NEXT:    ldrh w9, [x1]
 ; CHECK-NEXT:    cmp w8, w9
-; CHECK-NEXT:    b.ne LBB0_47
+; CHECK-NEXT:    b.ne LBB0_42
 ; CHECK-NEXT:  ; %bb.1: ; %if.end
 ; CHECK-NEXT:    sub sp, sp, #64
 ; CHECK-NEXT:    stp x29, x30, [sp, #48] ; 16-byte Folded Spill
@@ -80,7 +80,7 @@ define fastcc i32 @prune_match(ptr nocapture readonly %a, ptr nocapture readonly
 ; CHECK-NEXT:    ldrb w8, [x9, x11]
 ; CHECK-NEXT:    ldrb w15, [x10, x11]
 ; CHECK-NEXT:    cmp w8, w15
-; CHECK-NEXT:    b.ne LBB0_42
+; CHECK-NEXT:    b.ne LBB0_25
 ; CHECK-NEXT:  ; %bb.7: ; %if.end17
 ; CHECK-NEXT:    add x11, x11, #1
 ; CHECK-NEXT:    ldrsb x8, [x9, x11]
@@ -108,158 +108,144 @@ define fastcc i32 @prune_match(ptr nocapture readonly %a, ptr nocapture readonly
 ; CHECK-NEXT:    cbnz w8, LBB0_6
 ; CHECK-NEXT:  LBB0_9: ; %while.end
 ; CHECK-NEXT:    orr w8, w13, w12
-; CHECK-NEXT:    cbnz w8, LBB0_24
+; CHECK-NEXT:    cbnz w8, LBB0_23
 ; CHECK-NEXT:  ; %bb.10: ; %if.then23
 ; CHECK-NEXT:    ldr x12, [x0, #16]
 ; CHECK-NEXT:    ldrb w8, [x9, x11]
 ; CHECK-NEXT:    ldrb w13, [x12]
 ; CHECK-NEXT:    cmp w13, #83
-; CHECK-NEXT:    b.eq LBB0_19
+; CHECK-NEXT:    b.eq LBB0_18
 ; CHECK-NEXT:  LBB0_11: ; %while.cond59.preheader
-; CHECK-NEXT:    cbz w8, LBB0_23
+; CHECK-NEXT:    cbz w8, LBB0_22
 ; CHECK-NEXT:  LBB0_12: ; %land.rhs.preheader
 ; CHECK-NEXT:    add x12, x9, x11
 ; CHECK-NEXT:    add x9, x10, x11
+; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    add x10, x12, #1
 ; CHECK-NEXT:  LBB0_13: ; %land.rhs
 ; CHECK-NEXT:    ; =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ldrb w11, [x9], #1
-; CHECK-NEXT:    cbz w11, LBB0_23
+; CHECK-NEXT:    cbz w11, LBB0_22
 ; CHECK-NEXT:  ; %bb.14: ; %while.body66
 ; CHECK-NEXT:    ; in Loop: Header=BB0_13 Depth=1
 ; CHECK-NEXT:    cmp w8, #42
-; CHECK-NEXT:    b.eq LBB0_18
+; CHECK-NEXT:    b.eq LBB0_17
 ; CHECK-NEXT:  ; %bb.15: ; %while.body66
 ; CHECK-NEXT:    ; in Loop: Header=BB0_13 Depth=1
 ; CHECK-NEXT:    cmp w11, #42
-; CHECK-NEXT:    b.eq LBB0_18
+; CHECK-NEXT:    b.eq LBB0_17
 ; CHECK-NEXT:  ; %bb.16: ; %lor.lhs.false74
 ; CHECK-NEXT:    ; in Loop: Header=BB0_13 Depth=1
-; CHECK-NEXT:    cmp w8, w11
-; CHECK-NEXT:    mov w0, wzr
-; CHECK-NEXT:    b.ne LBB0_43
-; CHECK-NEXT:  ; %bb.17: ; %lor.lhs.false74
-; CHECK-NEXT:    ; in Loop: Header=BB0_13 Depth=1
 ; CHECK-NEXT:    cmp w8, #94
-; CHECK-NEXT:    b.eq LBB0_43
-; CHECK-NEXT:  LBB0_18: ; %if.then83
+; CHECK-NEXT:    ccmp w8, w11, #0, ne
+; CHECK-NEXT:    b.ne LBB0_25
+; CHECK-NEXT:  LBB0_17: ; %if.then83
 ; CHECK-NEXT:    ; in Loop: Header=BB0_13 Depth=1
 ; CHECK-NEXT:    ldrb w8, [x10], #1
-; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    cbnz w8, LBB0_13
-; CHECK-NEXT:    b LBB0_43
-; CHECK-NEXT:  LBB0_19: ; %land.lhs.true28
-; CHECK-NEXT:    cbz w8, LBB0_23
-; CHECK-NEXT:  ; %bb.20: ; %land.lhs.true28
+; CHECK-NEXT:    b LBB0_26
+; CHECK-NEXT:  LBB0_18: ; %land.lhs.true28
+; CHECK-NEXT:    cbz w8, LBB0_22
+; CHECK-NEXT:  ; %bb.19: ; %land.lhs.true28
 ; CHECK-NEXT:    cmp w8, #112
 ; CHECK-NEXT:    b.ne LBB0_12
-; CHECK-NEXT:  ; %bb.21: ; %land.lhs.true35
+; CHECK-NEXT:  ; %bb.20: ; %land.lhs.true35
 ; CHECK-NEXT:    ldrb w13, [x10, x11]
 ; CHECK-NEXT:    cmp w13, #112
 ; CHECK-NEXT:    b.ne LBB0_12
-; CHECK-NEXT:  ; %bb.22: ; %land.lhs.true43
+; CHECK-NEXT:  ; %bb.21: ; %land.lhs.true43
 ; CHECK-NEXT:    sub x12, x9, x12
 ; CHECK-NEXT:    add x12, x12, x11
 ; CHECK-NEXT:    cmp x12, #1
-; CHECK-NEXT:    b.ne LBB0_44
-; CHECK-NEXT:  LBB0_23:
+; CHECK-NEXT:    b.ne LBB0_39
+; CHECK-NEXT:  LBB0_22:
 ; CHECK-NEXT:    mov w0, #1 ; =0x1
-; CHECK-NEXT:    b LBB0_43
-; CHECK-NEXT:  LBB0_24: ; %if.else88
-; CHECK-NEXT:    cmp w12, #1
-; CHECK-NEXT:    b.ne LBB0_33
-; CHECK-NEXT:  ; %bb.25: ; %if.else88
+; CHECK-NEXT:    b LBB0_26
+; CHECK-NEXT:  LBB0_23: ; %if.else88
 ; CHECK-NEXT:    cmp w13, #2
-; CHECK-NEXT:    b.ne LBB0_33
-; CHECK-NEXT:  ; %bb.26: ; %while.cond95.preheader
+; CHECK-NEXT:    ccmp w12, #1, #0, eq
+; CHECK-NEXT:    b.eq LBB0_27
+; CHECK-NEXT:  ; %bb.24: ; %if.else123
+; CHECK-NEXT:    cmp w12, #2
+; CHECK-NEXT:    ccmp w13, #1, #0, eq
+; CHECK-NEXT:    b.eq LBB0_34
+; CHECK-NEXT:  LBB0_25:
+; CHECK-NEXT:    mov w0, wzr
+; CHECK-NEXT:  LBB0_26:
+; CHECK-NEXT:    ldp x29, x30, [sp, #48] ; 16-byte Folded Reload
+; CHECK-NEXT:    add sp, sp, #64
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  LBB0_27: ; %while.cond95.preheader
 ; CHECK-NEXT:    ldrb w12, [x9, x11]
-; CHECK-NEXT:    cbz w12, LBB0_23
-; CHECK-NEXT:  ; %bb.27: ; %land.rhs99.preheader
+; CHECK-NEXT:    cbz w12, LBB0_22
+; CHECK-NEXT:  ; %bb.28: ; %land.rhs99.preheader
 ; CHECK-NEXT:    mov x8, xzr
 ; CHECK-NEXT:    mov w0, #1 ; =0x1
-; CHECK-NEXT:    b LBB0_29
-; CHECK-NEXT:  LBB0_28: ; %if.then117
-; CHECK-NEXT:    ; in Loop: Header=BB0_29 Depth=1
+; CHECK-NEXT:    b LBB0_30
+; CHECK-NEXT:  LBB0_29: ; %if.then117
+; CHECK-NEXT:    ; in Loop: Header=BB0_30 Depth=1
 ; CHECK-NEXT:    add x12, x9, x8
 ; CHECK-NEXT:    add x8, x8, #1
 ; CHECK-NEXT:    add x12, x12, x11
 ; CHECK-NEXT:    ldrb w12, [x12, #1]
-; CHECK-NEXT:    cbz w12, LBB0_43
-; CHECK-NEXT:  LBB0_29: ; %land.rhs99
+; CHECK-NEXT:    cbz w12, LBB0_26
+; CHECK-NEXT:  LBB0_30: ; %land.rhs99
 ; CHECK-NEXT:    ; =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    add x13, x10, x8
 ; CHECK-NEXT:    ldrb w13, [x13, x11]
-; CHECK-NEXT:    cbz w13, LBB0_23
-; CHECK-NEXT:  ; %bb.30: ; %while.body104
-; CHECK-NEXT:    ; in Loop: Header=BB0_29 Depth=1
-; CHECK-NEXT:    cmp w12, w13
-; CHECK-NEXT:    b.eq LBB0_28
+; CHECK-NEXT:    cbz w13, LBB0_22
 ; CHECK-NEXT:  ; %bb.31: ; %while.body104
-; CHECK-NEXT:    ; in Loop: Header=BB0_29 Depth=1
-; CHECK-NEXT:    cmp w12, #42
-; CHECK-NEXT:    b.eq LBB0_28
+; CHECK-NEXT:    ; in Loop: Header=BB0_30 Depth=1
+; CHECK-NEXT:    cmp w12, w13
+; CHECK-NEXT:    b.eq LBB0_29
 ; CHECK-NEXT:  ; %bb.32: ; %while.body104
-; CHECK-NEXT:    ; in Loop: Header=BB0_29 Depth=1
+; CHECK-NEXT:    ; in Loop: Header=BB0_30 Depth=1
+; CHECK-NEXT:    cmp w12, #42
+; CHECK-NEXT:    b.eq LBB0_29
+; CHECK-NEXT:  ; %bb.33: ; %while.body104
+; CHECK-NEXT:    ; in Loop: Header=BB0_30 Depth=1
 ; CHECK-NEXT:    cmp w13, #94
-; CHECK-NEXT:    b.eq LBB0_28
-; CHECK-NEXT:    b LBB0_42
-; CHECK-NEXT:  LBB0_33: ; %if.else123
-; CHECK-NEXT:    cmp w13, #1
-; CHECK-NEXT:    mov w0, wzr
-; CHECK-NEXT:    b.ne LBB0_43
-; CHECK-NEXT:  ; %bb.34: ; %if.else123
-; CHECK-NEXT:    cmp w12, #2
-; CHECK-NEXT:    b.ne LBB0_43
-; CHECK-NEXT:  ; %bb.35: ; %while.cond130.preheader
-; CHECK-NEXT:    ldrb w8, [x9, x11]
-; CHECK-NEXT:    cbz w8, LBB0_23
-; CHECK-NEXT:  ; %bb.36: ; %land.rhs134.preheader
-; CHECK-NEXT:    mov x12, xzr
+; CHECK-NEXT:    b.eq LBB0_29
+; CHECK-NEXT:    b LBB0_25
+; CHECK-NEXT:  LBB0_34: ; %while.cond130.preheader
+; CHECK-NEXT:    ldrb w12, [x9, x11]
+; CHECK-NEXT:    cbz w12, LBB0_22
+; CHECK-NEXT:  ; %bb.35: ; %land.rhs134.preheader
+; CHECK-NEXT:    mov x8, xzr
+; CHECK-NEXT:    mov w13, #42 ; =0x2a
 ; CHECK-NEXT:    mov w0, #1 ; =0x1
-; CHECK-NEXT:    b LBB0_38
-; CHECK-NEXT:  LBB0_37: ; %if.then152
-; CHECK-NEXT:    ; in Loop: Header=BB0_38 Depth=1
-; CHECK-NEXT:    add x8, x9, x12
-; CHECK-NEXT:    add x12, x12, #1
-; CHECK-NEXT:    add x8, x8, x11
-; CHECK-NEXT:    ldrb w8, [x8, #1]
-; CHECK-NEXT:    cbz w8, LBB0_43
-; CHECK-NEXT:  LBB0_38: ; %land.rhs134
+; CHECK-NEXT:  LBB0_36: ; %land.rhs134
 ; CHECK-NEXT:    ; =>This Inner Loop Header: Depth=1
-; CHECK-NEXT:    add x13, x10, x12
-; CHECK-NEXT:    ldrb w13, [x13, x11]
-; CHECK-NEXT:    cbz w13, LBB0_23
-; CHECK-NEXT:  ; %bb.39: ; %while.body139
-; CHECK-NEXT:    ; in Loop: Header=BB0_38 Depth=1
-; CHECK-NEXT:    cmp w8, w13
-; CHECK-NEXT:    b.eq LBB0_37
-; CHECK-NEXT:  ; %bb.40: ; %while.body139
-; CHECK-NEXT:    ; in Loop: Header=BB0_38 Depth=1
-; CHECK-NEXT:    cmp w13, #42
-; CHECK-NEXT:    b.eq LBB0_37
-; CHECK-NEXT:  ; %bb.41: ; %while.body139
-; CHECK-NEXT:    ; in Loop: Header=BB0_38 Depth=1
-; CHECK-NEXT:    cmp w8, #94
-; CHECK-NEXT:    b.eq LBB0_37
-; CHECK-NEXT:  LBB0_42:
-; CHECK-NEXT:    mov w0, wzr
-; CHECK-NEXT:  LBB0_43:
-; CHECK-NEXT:    ldp x29, x30, [sp, #48] ; 16-byte Folded Reload
-; CHECK-NEXT:    add sp, sp, #64
-; CHECK-NEXT:    ret
-; CHECK-NEXT:  LBB0_44: ; %lor.lhs.false47
+; CHECK-NEXT:    add x14, x10, x8
+; CHECK-NEXT:    ldrb w14, [x14, x11]
+; CHECK-NEXT:    cbz w14, LBB0_22
+; CHECK-NEXT:  ; %bb.37: ; %while.body139
+; CHECK-NEXT:    ; in Loop: Header=BB0_36 Depth=1
+; CHECK-NEXT:    cmp w12, #94
+; CHECK-NEXT:    ccmp w14, w13, #4, ne
+; CHECK-NEXT:    ccmp w12, w14, #4, ne
+; CHECK-NEXT:    b.ne LBB0_25
+; CHECK-NEXT:  ; %bb.38: ; %if.then152
+; CHECK-NEXT:    ; in Loop: Header=BB0_36 Depth=1
+; CHECK-NEXT:    add x12, x9, x8
+; CHECK-NEXT:    add x8, x8, #1
+; CHECK-NEXT:    add x12, x12, x11
+; CHECK-NEXT:    ldrb w12, [x12, #1]
+; CHECK-NEXT:    cbnz w12, LBB0_36
+; CHECK-NEXT:    b LBB0_26
+; CHECK-NEXT:  LBB0_39: ; %lor.lhs.false47
 ; CHECK-NEXT:    cmp x12, #2
 ; CHECK-NEXT:    b.ne LBB0_11
-; CHECK-NEXT:  ; %bb.45: ; %land.lhs.true52
+; CHECK-NEXT:  ; %bb.40: ; %land.lhs.true52
 ; CHECK-NEXT:    add x12, x9, x11
 ; CHECK-NEXT:    mov w0, #1 ; =0x1
 ; CHECK-NEXT:    ldurb w12, [x12, #-1]
 ; CHECK-NEXT:    cmp w12, #73
-; CHECK-NEXT:    b.eq LBB0_43
-; CHECK-NEXT:  ; %bb.46: ; %land.lhs.true52
-; CHECK-NEXT:    cbz w8, LBB0_43
+; CHECK-NEXT:    b.eq LBB0_26
+; CHECK-NEXT:  ; %bb.41: ; %land.lhs.true52
+; CHECK-NEXT:    cbz w8, LBB0_26
 ; CHECK-NEXT:    b LBB0_12
-; CHECK-NEXT:  LBB0_47:
+; CHECK-NEXT:  LBB0_42:
 ; CHECK-NEXT:    mov w0, wzr
 ; CHECK-NEXT:    ret
 ; CHECK-NEXT:    .loh AdrpLdrGot Lloh0, Lloh1
diff --git a/llvm/test/CodeGen/AArch64/tbl-loops.ll b/llvm/test/CodeGen/AArch64/tbl-loops.ll
index 84af8596a0e99..25bceea3fe2b8 100644
--- a/llvm/test/CodeGen/AArch64/tbl-loops.ll
+++ b/llvm/test/CodeGen/AArch64/tbl-loops.ll
@@ -145,30 +145,28 @@ define void @loop2(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-LABEL: loop2:
 ; CHECK:       // %bb.0: // %entry
 ; CHECK-NEXT:    subs w8, w2, #1
-; CHECK-NEXT:    b.lt .LBB1_7
+; CHECK-NEXT:    b.lt .LBB1_6
 ; CHECK-NEXT:  // %bb.1: // %for.body.preheader
 ; CHECK-NEXT:    cmp w8, #2
-; CHECK-NEXT:    b.ls .LBB1_4
+; CHECK-NEXT:    b.ls .LBB1_3
 ; CHECK-NEXT:  // %bb.2: // %vector.memcheck
 ; CHECK-NEXT:    ubfiz x9, x8, #1, #32
 ; CHECK-NEXT:    add x9, x9, #2
-; CHECK-NEXT:    add x10, x1, x9, lsl #2
-; CHECK-NEXT:    cmp x10, x0
-; CHECK-NEXT:    b.ls .LBB1_8
-; CHECK-NEXT:  // %bb.3: // %vector.memcheck
-; CHECK-NEXT:    add x9, x0, x9
-; CHECK-NEXT:    cmp x9, x1
-; CHECK-NEXT:    b.ls .LBB1_8
-; CHECK-NEXT:  .LBB1_4:
+; CHECK-NEXT:    add x10, x0, x9
+; CHECK-NEXT:    add x9, x1, x9, lsl #2
+; CHECK-NEXT:    cmp x10, x1
+; CHECK-NEXT:    ccmp x9, x0, #0, hi
+; CHECK-NEXT:    b.ls .LBB1_7
+; CHECK-NEXT:  .LBB1_3:
 ; CHECK-NEXT:    mov w10, wzr
 ; CHECK-NEXT:    mov x8, x1
 ; CHECK-NEXT:    mov x9, x0
-; CHECK-NEXT:  .LBB1_5: // %for.body.preheader1
+; CHECK-NEXT:  .LBB1_4: // %for.body.preheader1
 ; CHECK-NEXT:    movi d0, #0000000000000000
 ; CHECK-NEXT:    mov w11, #1132396544 // =0x437f0000
 ; CHECK-NEXT:    sub w10, w2, w10
 ; CHECK-NEXT:    fmov s1, w11
-; CHECK-NEXT:  .LBB1_6: // %for.body
+; CHECK-NEXT:  .LBB1_5: // %for.body
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ldp s2, s3, [x8], #8
 ; CHECK-NEXT:    fcmp s2, s1
@@ -185,10 +183,10 @@ define void @loop2(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    fcvtzs s3, s3
 ; CHECK-NEXT:    stur b3, [x9, #1]
 ; CHECK-NEXT:    add x9, x9, #2
-; CHECK-NEXT:    b.ne .LBB1_6
-; CHECK-NEXT:  .LBB1_7: // %for.cond.cleanup
+; CHECK-NEXT:    b.ne .LBB1_5
+; CHECK-NEXT:  .LBB1_6: // %for.cond.cleanup
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB1_8: // %vector.ph
+; CHECK-NEXT:  .LBB1_7: // %vector.ph
 ; CHECK-NEXT:    add x11, x8, #1
 ; CHECK-NEXT:    mov w8, #1132396544 // =0x437f0000
 ; CHECK-NEXT:    and x10, x11, #0x1fffffffc
@@ -196,7 +194,7 @@ define void @loop2(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    and x12, x11, #0x1fffffffc
 ; CHECK-NEXT:    add x8, x1, x10, lsl #3
 ; CHECK-NEXT:    add x9, x0, x10, lsl #1
-; CHECK-NEXT:  .LBB1_9: // %vector.body
+; CHECK-NEXT:  .LBB1_8: // %vector.body
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ld2 { v1.4s, v2.4s }, [x1], #32
 ; CHECK-NEXT:    subs x12, x12, #4
@@ -214,11 +212,11 @@ define void @loop2(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    xtn v1.4h, v1.4s
 ; CHECK-NEXT:    trn1 v1.8b, v2.8b, v1.8b
 ; CHECK-NEXT:    str d1, [x0], #8
-; CHECK-NEXT:    b.ne .LBB1_9
-; CHECK-NEXT:  // %bb.10: // %middle.block
+; CHECK-NEXT:    b.ne .LBB1_8
+; CHECK-NEXT:  // %bb.9: // %middle.block
 ; CHECK-NEXT:    cmp x11, x10
-; CHECK-NEXT:    b.ne .LBB1_5
-; CHECK-NEXT:    b .LBB1_7
+; CHECK-NEXT:    b.ne .LBB1_4
+; CHECK-NEXT:    b .LBB1_6
 entry:
   %cmp19 = icmp sgt i32 %width, 0
   br i1 %cmp19, label %for.body.preheader, label %for.cond.cleanup
@@ -320,19 +318,56 @@ define void @loop3(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-LABEL: loop3:
 ; CHECK:       // %bb.0: // %entry
 ; CHECK-NEXT:    subs w8, w2, #1
-; CHECK-NEXT:    b.lt .LBB2_9
+; CHECK-NEXT:    b.lt .LBB2_6
 ; CHECK-NEXT:  // %bb.1: // %for.body.preheader
 ; CHECK-NEXT:    cmp w8, #2
-; CHECK-NEXT:    b.ls .LBB2_6
+; CHECK-NEXT:    b.ls .LBB2_3
 ; CHECK-NEXT:  // %bb.2: // %vector.memcheck
 ; CHECK-NEXT:    add x9, x8, w8, uxtw #1
 ; CHECK-NEXT:    add x9, x9, #3
-; CHECK-NEXT:    add x10, x1, x9, lsl #2
-; CHECK-NEXT:    add x9, x0, x9
-; CHECK-NEXT:    cmp x10, x0
-; CHECK-NEXT:    ccmp x9, x1, #0, hi
-; CHECK-NEXT:    b.hi .LBB2_6
-; CHECK-NEXT:  // %bb.3: // %vector.ph
+; CHECK-NEXT:    add x10, x0, x9
+; CHECK-NEXT:    add x9, x1, x9, lsl #2
+; CHECK-NEXT:    cmp x10, x1
+; CHECK-NEXT:    ccmp x9, x0, #0, hi
+; CHECK-NEXT:    b.ls .LBB2_7
+; CHECK-NEXT:  .LBB2_3:
+; CHECK-NEXT:    mov w10, wzr
+; CHECK-NEXT:    mov x8, x1
+; CHECK-NEXT:    mov x9, x0
+; CHECK-NEXT:  .LBB2_4: // %for.body.preheader1
+; CHECK-NEXT:    movi d0, #0000000000000000
+; CHECK-NEXT:    mov w11, #1132396544 // =0x437f0000
+; CHECK-NEXT:    sub w10, w2, w10
+; CHECK-NEXT:    fmov s1, w11
+; CHECK-NEXT:  .LBB2_5: // %for.body
+; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    ldp s2, s3, [x8]
+; CHECK-NEXT:    fcmp s2, s1
+; CHECK-NEXT:    fcsel s4, s1, s2, gt
+; CHECK-NEXT:    fcmp s2, #0.0
+; CHECK-NEXT:    fcsel s2, s0, s4, mi
+; CHECK-NEXT:    fcmp s3, s1
+; CHECK-NEXT:    fcsel s4, s1, s3, gt
+; CHECK-NEXT:    fcmp s3, #0.0
+; CHECK-NEXT:    ldr s3, [x8, #8]
+; CHECK-NEXT:    fcvtzs s2, s2
+; CHECK-NEXT:    add x8, x8, #12
+; CHECK-NEXT:    fcsel s4, s0, s4, mi
+; CHECK-NEXT:    fcmp s3, s1
+; CHECK-NEXT:    str b2, [x9]
+; CHECK-NEXT:    fcsel s5, s1, s3, gt
+; CHECK-NEXT:    fcmp s3, #0.0
+; CHECK-NEXT:    fcvtzs s4, s4
+; CHECK-NEXT:    fcsel s3, s0, s5, mi
+; CHECK-NEXT:    subs w10, w10, #1
+; CHECK-NEXT:    stur b4, [x9, #1]
+; CHECK-NEXT:    fcvtzs s3, s3
+; CHECK-NEXT:    stur b3, [x9, #2]
+; CHECK-NEXT:    add x9, x9, #3
+; CHECK-NEXT:    b.ne .LBB2_5
+; CHECK-NEXT:  .LBB2_6: // %for.cond.cleanup
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB2_7: // %vector.ph
 ; CHECK-NEXT:    add x11, x8, #1
 ; CHECK-NEXT:    mov w8, #1132396544 // =0x437f0000
 ; CHECK-NEXT:    adrp x12, .LCPI2_0
@@ -343,7 +378,7 @@ define void @loop3(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    and x12, x11, #0x1fffffffc
 ; CHECK-NEXT:    add x8, x1, x9, lsl #2
 ; CHECK-NEXT:    add x9, x0, x9
-; CHECK-NEXT:  .LBB2_4: // %vector.body
+; CHECK-NEXT:  .LBB2_8: // %vector.body
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ld3 { v2.4s, v3.4s, v4.4s }, [x1], #48
 ; CHECK-NEXT:    subs x12, x12, #4
@@ -370,48 +405,11 @@ define void @loop3(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    str d2, [x0]
 ; CHECK-NEXT:    str s3, [x0, #8]
 ; CHECK-NEXT:    add x0, x0, #12
-; CHECK-NEXT:    b.ne .LBB2_4
-; CHECK-NEXT:  // %bb.5: // %middle.block
-; CHECK-NEXT:    cmp x11, x10
-; CHECK-NEXT:    b.ne .LBB2_7
-; CHECK-NEXT:    b .LBB2_9
-; CHECK-NEXT:  .LBB2_6:
-; CHECK-NEXT:    mov w10, wzr
-; CHECK-NEXT:    mov x8, x1
-; CHECK-NEXT:    mov x9, x0
-; CHECK-NEXT:  .LBB2_7: // %for.body.preheader1
-; CHECK-NEXT:    movi d0, #0000000000000000
-; CHECK-NEXT:    mov w11, #1132396544 // =0x437f0000
-; CHECK-NEXT:    sub w10, w2, w10
-; CHECK-NEXT:    fmov s1, w11
-; CHECK-NEXT:  .LBB2_8: // %for.body
-; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
-; CHECK-NEXT:    ldp s2, s3, [x8]
-; CHECK-NEXT:    fcmp s2, s1
-; CHECK-NEXT:    fcsel s4, s1, s2, gt
-; CHECK-NEXT:    fcmp s2, #0.0
-; CHECK-NEXT:    fcsel s2, s0, s4, mi
-; CHECK-NEXT:    fcmp s3, s1
-; CHECK-NEXT:    fcsel s4, s1, s3, gt
-; CHECK-NEXT:    fcmp s3, #0.0
-; CHECK-NEXT:    ldr s3, [x8, #8]
-; CHECK-NEXT:    fcvtzs s2, s2
-; CHECK-NEXT:    add x8, x8, #12
-; CHECK-NEXT:    fcsel s4, s0, s4, mi
-; CHECK-NEXT:    fcmp s3, s1
-; CHECK-NEXT:    str b2, [x9]
-; CHECK-NEXT:    fcsel s5, s1, s3, gt
-; CHECK-NEXT:    fcmp s3, #0.0
-; CHECK-NEXT:    fcvtzs s4, s4
-; CHECK-NEXT:    fcsel s3, s0, s5, mi
-; CHECK-NEXT:    subs w10, w10, #1
-; CHECK-NEXT:    stur b4, [x9, #1]
-; CHECK-NEXT:    fcvtzs s3, s3
-; CHECK-NEXT:    stur b3, [x9, #2]
-; CHECK-NEXT:    add x9, x9, #3
 ; CHECK-NEXT:    b.ne .LBB2_8
-; CHECK-NEXT:  .LBB2_9: // %for.cond.cleanup
-; CHECK-NEXT:    ret
+; CHECK-NEXT:  // %bb.9: // %middle.block
+; CHECK-NEXT:    cmp x11, x10
+; CHECK-NEXT:    b.ne .LBB2_4
+; CHECK-NEXT:    b .LBB2_6
 entry:
   %cmp29 = icmp sgt i32 %width, 0
   br i1 %cmp29, label %for.body.preheader, label %for.cond.cleanup
@@ -530,30 +528,28 @@ define void @loop4(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-LABEL: loop4:
 ; CHECK:       // %bb.0: // %entry
 ; CHECK-NEXT:    subs w8, w2, #1
-; CHECK-NEXT:    b.lt .LBB3_7
+; CHECK-NEXT:    b.lt .LBB3_6
 ; CHECK-NEXT:  // %bb.1: // %for.body.preheader
 ; CHECK-NEXT:    cmp w8, #2
-; CHECK-NEXT:    b.ls .LBB3_4
+; CHECK-NEXT:    b.ls .LBB3_3
 ; CHECK-NEXT:  // %bb.2: // %vector.memcheck
 ; CHECK-NEXT:    ubfiz x9, x8, #2, #32
 ; CHECK-NEXT:    add x9, x9, #4
-; CHECK-NEXT:    add x10, x1, x9, lsl #2
-; CHECK-NEXT:    cmp x10, x0
-; CHECK-NEXT:    b.ls .LBB3_8
-; CHECK-NEXT:  // %bb.3: // %vector.memcheck
-; CHECK-NEXT:    add x9, x0, x9
-; CHECK-NEXT:    cmp x9, x1
-; CHECK-NEXT:    b.ls .LBB3_8
-; CHECK-NEXT:  .LBB3_4:
+; CHECK-NEXT:    add x10, x0, x9
+; CHECK-NEXT:    add x9, x1, x9, lsl #2
+; CHECK-NEXT:    cmp x10, x1
+; CHECK-NEXT:    ccmp x9, x0, #0, hi
+; CHECK-NEXT:    b.ls .LBB3_7
+; CHECK-NEXT:  .LBB3_3:
 ; CHECK-NEXT:    mov w10, wzr
 ; CHECK-NEXT:    mov x8, x1
 ; CHECK-NEXT:    mov x9, x0
-; CHECK-NEXT:  .LBB3_5: // %for.body.preheader1
+; CHECK-NEXT:  .LBB3_4: // %for.body.preheader1
 ; CHECK-NEXT:    movi d0, #0000000000000000
 ; CHECK-NEXT:    mov w11, #1132396544 // =0x437f0000
 ; CHECK-NEXT:    sub w10, w2, w10
 ; CHECK-NEXT:    fmov s1, w11
-; CHECK-NEXT:  .LBB3_6: // %for.body
+; CHECK-NEXT:  .LBB3_5: // %for.body
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ldp s2, s3, [x8]
 ; CHECK-NEXT:    fcmp s2, s1
@@ -584,10 +580,10 @@ define void @loop4(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    fcvtzs s5, s5
 ; CHECK-NEXT:    stur b5, [x9, #3]
 ; CHECK-NEXT:    add x9, x9, #4
-; CHECK-NEXT:    b.ne .LBB3_6
-; CHECK-NEXT:  .LBB3_7: // %for.cond.cleanup
+; CHECK-NEXT:    b.ne .LBB3_5
+; CHECK-NEXT:  .LBB3_6: // %for.cond.cleanup
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB3_8: // %vector.ph
+; CHECK-NEXT:  .LBB3_7: // %vector.ph
 ; CHECK-NEXT:    add x11, x8, #1
 ; CHECK-NEXT:    mov w8, #1132396544 // =0x437f0000
 ; CHECK-NEXT:    adrp x12, .LCPI3_0
@@ -597,7 +593,7 @@ define void @loop4(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    add x8, x1, x10, lsl #4
 ; CHECK-NEXT:    add x9, x0, x10, lsl #2
 ; CHECK-NEXT:    and x12, x11, #0x1fffffffc
-; CHECK-NEXT:  .LBB3_9: // %vector.body
+; CHECK-NEXT:  .LBB3_8: // %vector.body
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    ld4 { v2.4s, v3.4s, v4.4s, v5.4s }, [x1], #64
 ; CHECK-NEXT:    subs x12, x12, #4
@@ -627,11 +623,11 @@ define void @loop4(ptr noalias nocapture noundef writeonly %dst, ptr nocapture n
 ; CHECK-NEXT:    xtn v19.4h, v2.4s
 ; CHECK-NEXT:    tbl v2.16b, { v16.16b, v17.16b, v18.16b, v19.16b }, v1.16b
 ; CHECK-NEXT:    str q2, [x0], #16
-; CHECK-NEXT:    b.ne .LBB3_9
-; CHECK-NEXT:  // %bb.10: // %middle.block
+; CHECK-NEXT:    b.ne .LBB3_8
+; CHECK-NEXT:  // %bb.9: // %middle.block
 ; CHECK-NEXT:    cmp x11, x10
-; CHECK-NEXT:    b.ne .LBB3_5
-; CHECK-NEXT:    b .LBB3_7
+; CHECK-NEXT:    b.ne .LBB3_4
+; CHECK-NEXT:    b .LBB3_6
 entry:
   %cmp39 = icmp sgt i32 %width, 0
   br i1 %cmp39, label %for.body.preheader, label %for.cond.cleanup
diff --git a/llvm/test/CodeGen/AArch64/tbz-tbnz.ll b/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
index 5e51676056811..55954db64f8d6 100644
--- a/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
+++ b/llvm/test/CodeGen/AArch64/tbz-tbnz.ll
@@ -199,20 +199,20 @@ if.end:
 define void @test8(i64 %val1, i64 %val2, i64 %val3) {
 ; CHECK-SD-LABEL: test8:
 ; CHECK-SD:       // %bb.0:
-; CHECK-SD-NEXT:    tst x0, x1
-; CHECK-SD-NEXT:    b.pl .LBB7_3
-; CHECK-SD-NEXT:  // %bb.1:
 ; CHECK-SD-NEXT:    and x8, x1, x2
-; CHECK-SD-NEXT:    tbnz x8, #63, .LBB7_3
-; CHECK-SD-NEXT:  // %bb.2: // %if.then2
+; CHECK-SD-NEXT:    and x9, x0, x1
+; CHECK-SD-NEXT:    cmn x8, #1
+; CHECK-SD-NEXT:    ccmp x9, #0, #0, gt
+; CHECK-SD-NEXT:    b.pl .LBB7_2
+; CHECK-SD-NEXT:  // %bb.1: // %if.then2
 ; CHECK-SD-NEXT:    tst x0, x1, lsl #63
-; CHECK-SD-NEXT:    b.mi .LBB7_4
-; CHECK-SD-NEXT:  .LBB7_3: // %if.end
+; CHECK-SD-NEXT:    b.mi .LBB7_3
+; CHECK-SD-NEXT:  .LBB7_2: // %if.end
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB7_4: // %if.then3
+; CHECK-SD-NEXT:  .LBB7_3: // %if.then3
 ; CHECK-SD-NEXT:    tst x0, x1, lsl #62
-; CHECK-SD-NEXT:    b.mi .LBB7_3
-; CHECK-SD-NEXT:  // %bb.5: // %if.then4
+; CHECK-SD-NEXT:    b.mi .LBB7_2
+; CHECK-SD-NEXT:  // %bb.4: // %if.then4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
diff --git a/llvm/test/CodeGen/AArch64/umulo-128-legalisation-lowering.ll b/llvm/test/CodeGen/AArch64/umulo-128-legalisation-lowering.ll
index ace0c83e63c7c..d7348ea154b24 100644
--- a/llvm/test/CodeGen/AArch64/umulo-128-legalisation-lowering.ll
+++ b/llvm/test/CodeGen/AArch64/umulo-128-legalisation-lowering.ll
@@ -75,18 +75,18 @@ define i128 @__muloti4(i128 %0, i128 %1, ptr nocapture nonnull writeonly align 4
 ; AARCH-NEXT:    cmp x9, x11
 ; AARCH-NEXT:    ccmp x10, x11, #0, eq
 ; AARCH-NEXT:    cset w9, ne
-; AARCH-NEXT:    tbnz x1, #63, .LBB1_3
-; AARCH-NEXT:    b .LBB1_4
+; AARCH-NEXT:    b .LBB1_3
 ; AARCH-NEXT:  .LBB1_2: // %overflow.no
 ; AARCH-NEXT:    smulh x8, x0, x2
 ; AARCH-NEXT:    mov w9, wzr
 ; AARCH-NEXT:    mul x0, x0, x2
-; AARCH-NEXT:    tbz x1, #63, .LBB1_4
 ; AARCH-NEXT:  .LBB1_3: // %overflow.res
 ; AARCH-NEXT:    eor x10, x3, #0x8000000000000000
 ; AARCH-NEXT:    orr x10, x2, x10
-; AARCH-NEXT:    cbz x10, .LBB1_5
-; AARCH-NEXT:  .LBB1_4: // %Else2
+; AARCH-NEXT:    cmp x10, #0
+; AARCH-NEXT:    ccmp x1, #0, #0, eq
+; AARCH-NEXT:    b.mi .LBB1_5
+; AARCH-NEXT:  // %bb.4: // %Else2
 ; AARCH-NEXT:    cbz w9, .LBB1_6
 ; AARCH-NEXT:  .LBB1_5: // %Then7
 ; AARCH-NEXT:    mov w9, #1 // =0x1
diff --git a/llvm/test/DebugInfo/AArch64/instr-ref-target-hooks.ll b/llvm/test/DebugInfo/AArch64/instr-ref-target-hooks.ll
index 78c328deff697..f61ff75d7171f 100644
--- a/llvm/test/DebugInfo/AArch64/instr-ref-target-hooks.ll
+++ b/llvm/test/DebugInfo/AArch64/instr-ref-target-hooks.ll
@@ -1,7 +1,7 @@
 ; Test to ensure that variable "__last" is properly recovered at the end of the livedebugvalues pass when Instruction Referencing-based LiveDebugValues is used.
 ; This testcase was obtained by looking at FileCheck.cpp and reducing it down via llvm-reduce.
 
-; RUN: llc -mtriple=aarch64-apple-darwin -o - %s -stop-after=livedebugvalues -O2 -experimental-debug-variable-locations | FileCheck %s
+; RUN: llc -mtriple=aarch64-apple-darwin -o - %s -stop-after=livedebugvalues -O2 -experimental-debug-variable-locations -aarch64-br-merging-base-cost=-1 | FileCheck %s
 
 ; CHECK: ![[LOC:[0-9]+]] = !DILocalVariable(name: "__last",
 ; CHECK: DBG_VALUE_LIST ![[LOC]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_plus_uconst, 8, DW_OP_deref), $sp

>From d96fcd05b9ce4b1ee2893580e49ecae5db493ce3 Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Mon, 15 Jun 2026 14:26:54 -0700
Subject: [PATCH 4/8] Handle CBZ/CBNZ/TBZ/TBNZ instructions

---
 .../Target/AArch64/AArch64ISelLowering.cpp    | 57 ++++++++++++++++++-
 llvm/test/CodeGen/AArch64/aarch64-tbz.ll      | 56 ++++++------------
 llvm/test/CodeGen/AArch64/csr-split.ll        | 18 +++---
 3 files changed, 84 insertions(+), 47 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index bf468f2967119..6b32e9a8a2889 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -175,6 +175,16 @@ static cl::opt<int> BrMergingCcmpBias(
              "makes merging branch conditions cheaper."),
     cl::Hidden);
 
+static cl::opt<int> BrMergingCbzTbnzBias(
+    "aarch64-br-merging-cbz-tbnz-bias", cl::init(6),
+    cl::desc("Decreases 'aarch64-br-merging-base-cost' when a condition can be "
+             "lowered to a single CBZ/CBNZ or TBZ/TBNZ compare-and-branch. The "
+             "split form needs no separate compare, so the CCMP merging "
+             "discount does not apply; the default cancels it. Set to 0 to "
+             "disable, or higher than 'aarch64-br-merging-ccmp-bias' to make "
+             "such conditions never merge."),
+    cl::Hidden);
+
 static cl::opt<int> BrMergingLikelyBias(
     "aarch64-br-merging-likely-bias", cl::init(0),
     cl::desc("Increases 'aarch64-br-merging-base-cost' in cases that it is "
@@ -31574,11 +31584,56 @@ TargetLoweringBase::CondMergingParams
 AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
                                                      const Value *Lhs,
                                                      const Value *Rhs) const {
+  using namespace llvm::PatternMatch;
+
+  // Returns true if \p V is a branch condition that AArch64 can lower to a
+  // single compare-and-branch (CBZ/CBNZ) or test-bit-and-branch (TBZ/TBNZ),
+  // i.e. without materializing a separate compare. Merging such a condition
+  // into a CMP/CCMP chain removes that fused form and tends to add
+  // instructions, so it is less likely to be profitable.
+  auto IsCbzTbnzCandidate = [](const Value *V) {
+    // A truncation to i1 feeding a branch tests bit #0 -> TBZ/TBNZ.
+    if (match(V, m_Trunc(m_Value())))
+      return true;
+    const auto *Cmp = dyn_cast<ICmpInst>(V);
+    if (!Cmp)
+      return false;
+    ICmpInst::Predicate P = Cmp->getPredicate();
+    // icmp eq/ne X, 0 -> CBZ/CBNZ. This also subsumes the bit-test form
+    // icmp eq/ne (and X, Pow2), 0, which AArch64 folds to TBZ/TBNZ.
+    if (ICmpInst::isEquality(P) && match(Cmp->getOperand(1), m_Zero()))
+      return true;
+    // Sign-bit tests lower to TBZ/TBNZ on the MSB: (X s< 0) and (X s> -1).
+    if (P == ICmpInst::ICMP_SLT && match(Cmp->getOperand(1), m_Zero()))
+      return true;
+    if (P == ICmpInst::ICMP_SGT && match(Cmp->getOperand(1), m_AllOnes()))
+      return true;
+    return false;
+  };
+
   int BaseCost = BrMergingBaseCostThresh.getValue();
-  // CCMP is always available on AArch64.
+  // CCMP is always available on AArch64 and folds the second compare and the
+  // branch into a single 1-cycle op (e.g. V2Write_1c_1F_1Flg on Neoverse V2),
+  // so merging is worth tolerating extra speculated work on the RHS dependency
+  // chain. The bias is that tolerance measured in TTI latency units: it stands
+  // in for the amortized cost of the branch the merge eliminates, i.e. roughly
+  // MispredictPenalty (~10 cycles on modern Arm cores) weighted by the branch's
+  // misprediction probability. With no profile, a branch is ~50/50, so the
+  // budget is ~MispredictPenalty/2; the default of 6 sits in the
+  // MispredictPenalty * [1/2 .. 3/4] band used elsewhere for this same tradeoff
+  // (see EarlyIfConversion and AArch64ConditionalCompares). The likely/unlikely
+  // biases below refine the misprediction estimate from BranchProbabilityInfo.
   if (BaseCost >= 0)
     BaseCost += BrMergingCcmpBias;
 
+  // Withdraw the CCMP discount when either unmerged condition would already be a
+  // single CBZ/CBNZ or TBZ/TBNZ: the split form needs no separate compare, so
+  // merging into a CMP/CCMP chain tends to add instructions rather than save
+  // them.
+  if (BaseCost >= 0 && BrMergingCbzTbnzBias > 0 &&
+      (IsCbzTbnzCandidate(Lhs) || IsCbzTbnzCandidate(Rhs)))
+    BaseCost -= BrMergingCbzTbnzBias;
+
   return {BaseCost, BrMergingLikelyBias.getValue(),
           BrMergingUnlikelyBias.getValue()};
 }
diff --git a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
index 9cfc20b29ecce..1ba8ea3d3101d 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
+++ b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
@@ -133,25 +133,15 @@ if.end3:                                          ; preds = %entry, %if.then2
 
 ; Make sure we use the W variant when log2(mask) is < 32.
 define void @test3(i64 %A, i64 %B) {
-; CHECK-SD-LABEL: test3:
-; CHECK-SD:       // %bb.0: // %entry
-; CHECK-SD-NEXT:    ubfx x8, x0, #28, #1
-; CHECK-SD-NEXT:    and w8, w8, w1, lsr #3
-; CHECK-SD-NEXT:    tbnz w8, #0, .LBB2_2
-; CHECK-SD-NEXT:  // %bb.1: // %if.then2
-; CHECK-SD-NEXT:    b foo
-; CHECK-SD-NEXT:  .LBB2_2: // %if.end3
-; CHECK-SD-NEXT:    ret
-;
-; CHECK-GI-LABEL: test3:
-; CHECK-GI:       // %bb.0: // %entry
-; CHECK-GI-NEXT:    tbz w1, #3, .LBB2_3
-; CHECK-GI-NEXT:  // %bb.1: // %entry
-; CHECK-GI-NEXT:    tbz w0, #28, .LBB2_3
-; CHECK-GI-NEXT:  // %bb.2: // %if.end3
-; CHECK-GI-NEXT:    ret
-; CHECK-GI-NEXT:  .LBB2_3: // %if.then2
-; CHECK-GI-NEXT:    b foo
+; CHECK-LABEL: test3:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    tbz w1, #3, .LBB2_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    tbz w0, #28, .LBB2_3
+; CHECK-NEXT:  // %bb.2: // %if.end3
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB2_3: // %if.then2
+; CHECK-NEXT:    b foo
 ;
 ; CHECK-GIO0-LABEL: test3:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -196,25 +186,15 @@ if.end3:                                          ; preds = %if.then2, %entry
 }
 
 define void @test4(i64 %A, i64 %B) {
-; CHECK-SD-LABEL: test4:
-; CHECK-SD:       // %bb.0: // %entry
-; CHECK-SD-NEXT:    ubfx x8, x0, #35, #1
-; CHECK-SD-NEXT:    and w8, w8, w1, lsr #3
-; CHECK-SD-NEXT:    tbnz w8, #0, .LBB3_2
-; CHECK-SD-NEXT:  // %bb.1: // %if.then2
-; CHECK-SD-NEXT:    b foo
-; CHECK-SD-NEXT:  .LBB3_2: // %if.end3
-; CHECK-SD-NEXT:    ret
-;
-; CHECK-GI-LABEL: test4:
-; CHECK-GI:       // %bb.0: // %entry
-; CHECK-GI-NEXT:    tbz w1, #3, .LBB3_3
-; CHECK-GI-NEXT:  // %bb.1: // %entry
-; CHECK-GI-NEXT:    tbz x0, #35, .LBB3_3
-; CHECK-GI-NEXT:  // %bb.2: // %if.end3
-; CHECK-GI-NEXT:    ret
-; CHECK-GI-NEXT:  .LBB3_3: // %if.then2
-; CHECK-GI-NEXT:    b foo
+; CHECK-LABEL: test4:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    tbz w1, #3, .LBB3_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    tbz x0, #35, .LBB3_3
+; CHECK-NEXT:  // %bb.2: // %if.end3
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB3_3: // %if.then2
+; CHECK-NEXT:    b foo
 ;
 ; CHECK-GIO0-LABEL: test4:
 ; CHECK-GIO0:       // %bb.0: // %entry
diff --git a/llvm/test/CodeGen/AArch64/csr-split.ll b/llvm/test/CodeGen/AArch64/csr-split.ll
index 816900452ff90..7b092b00b9655 100644
--- a/llvm/test/CodeGen/AArch64/csr-split.ll
+++ b/llvm/test/CodeGen/AArch64/csr-split.ll
@@ -83,12 +83,13 @@ declare signext i32 @callNonVoid(ptr) local_unnamed_addr
 define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-LABEL: test2:
 ; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    cbz x0, .LBB1_3
+; CHECK-NEXT:  // %bb.1: // %entry
 ; CHECK-NEXT:    adrp x8, a
 ; CHECK-NEXT:    ldrsw x8, [x8, :lo12:a]
 ; CHECK-NEXT:    cmp x8, x0
-; CHECK-NEXT:    ccmp x0, #0, #4, eq
-; CHECK-NEXT:    b.eq .LBB1_2
-; CHECK-NEXT:  // %bb.1: // %if.then2
+; CHECK-NEXT:    b.ne .LBB1_3
+; CHECK-NEXT:  // %bb.2: // %if.then2
 ; CHECK-NEXT:    stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w19, -8
@@ -101,20 +102,21 @@ define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-NEXT:    .cfi_restore w19
 ; CHECK-NEXT:    .cfi_restore w30
 ; CHECK-NEXT:    b callNonVoid
-; CHECK-NEXT:  .LBB1_2: // %return
+; CHECK-NEXT:  .LBB1_3: // %return
 ; CHECK-NEXT:    mov w0, wzr
 ; CHECK-NEXT:    ret
 ;
 ; CHECK-APPLE-LABEL: test2:
 ; CHECK-APPLE:       ; %bb.0: ; %entry
+; CHECK-APPLE-NEXT:    cbz x0, LBB1_3
+; CHECK-APPLE-NEXT:  ; %bb.1: ; %entry
 ; CHECK-APPLE-NEXT:  Lloh2:
 ; CHECK-APPLE-NEXT:    adrp x8, _a at PAGE
 ; CHECK-APPLE-NEXT:  Lloh3:
 ; CHECK-APPLE-NEXT:    ldrsw x8, [x8, _a at PAGEOFF]
 ; CHECK-APPLE-NEXT:    cmp x8, x0
-; CHECK-APPLE-NEXT:    ccmp x0, #0, #4, eq
-; CHECK-APPLE-NEXT:    b.eq LBB1_2
-; CHECK-APPLE-NEXT:  ; %bb.1: ; %if.then2
+; CHECK-APPLE-NEXT:    b.ne LBB1_3
+; CHECK-APPLE-NEXT:  ; %bb.2: ; %if.then2
 ; CHECK-APPLE-NEXT:    stp x20, x19, [sp, #-32]! ; 16-byte Folded Spill
 ; CHECK-APPLE-NEXT:    .cfi_def_cfa_offset 32
 ; CHECK-APPLE-NEXT:    stp x29, x30, [sp, #16] ; 16-byte Folded Spill
@@ -133,7 +135,7 @@ define dso_local signext i32 @test2(ptr %p1) local_unnamed_addr uwtable  {
 ; CHECK-APPLE-NEXT:    .cfi_restore w19
 ; CHECK-APPLE-NEXT:    .cfi_restore w20
 ; CHECK-APPLE-NEXT:    b _callNonVoid
-; CHECK-APPLE-NEXT:  LBB1_2: ; %return
+; CHECK-APPLE-NEXT:  LBB1_3: ; %return
 ; CHECK-APPLE-NEXT:    mov w0, wzr
 ; CHECK-APPLE-NEXT:    ret
 ; CHECK-APPLE-NEXT:    .loh AdrpLdr Lloh2, Lloh3

>From 11f58c3118fe3cffeac15745b22d73b7d1b1cb21 Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Mon, 15 Jun 2026 17:12:33 -0700
Subject: [PATCH 5/8] clang format

---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 6b32e9a8a2889..8a3a1dca5ed0d 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -31626,8 +31626,8 @@ AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
   if (BaseCost >= 0)
     BaseCost += BrMergingCcmpBias;
 
-  // Withdraw the CCMP discount when either unmerged condition would already be a
-  // single CBZ/CBNZ or TBZ/TBNZ: the split form needs no separate compare, so
+  // Withdraw the CCMP discount when either unmerged condition would already be
+  // a single CBZ/CBNZ or TBZ/TBNZ: the split form needs no separate compare, so
   // merging into a CMP/CCMP chain tends to add instructions rather than save
   // them.
   if (BaseCost >= 0 && BrMergingCbzTbnzBias > 0 &&

>From dc92628a69685f9451868a9239ce5a3491edeb3b Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Tue, 16 Jun 2026 08:42:25 -0700
Subject: [PATCH 6/8] Readjust the fused compares for LHS and RHS

---
 .../Target/AArch64/AArch64ISelLowering.cpp    | 46 ++++++++----
 llvm/test/CodeGen/AArch64/aarch64-tbz.ll      | 63 ++++++----------
 llvm/test/CodeGen/AArch64/arm64-ccmp.ll       | 74 ++++++-------------
 3 files changed, 76 insertions(+), 107 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 8a3a1dca5ed0d..d315cafbaeecc 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -177,12 +177,13 @@ static cl::opt<int> BrMergingCcmpBias(
 
 static cl::opt<int> BrMergingCbzTbnzBias(
     "aarch64-br-merging-cbz-tbnz-bias", cl::init(6),
-    cl::desc("Decreases 'aarch64-br-merging-base-cost' when a condition can be "
-             "lowered to a single CBZ/CBNZ or TBZ/TBNZ compare-and-branch. The "
-             "split form needs no separate compare, so the CCMP merging "
-             "discount does not apply; the default cancels it. Set to 0 to "
-             "disable, or higher than 'aarch64-br-merging-ccmp-bias' to make "
-             "such conditions never merge."),
+    cl::desc(
+        "Adjusts 'aarch64-br-merging-base-cost' for conditions that lower "
+        "to a single CBZ/CBNZ or TBZ/TBNZ compare-and-branch, which need "
+        "no separate compare. When both conditions are such candidates the "
+        "split form is two fused branches and they are never merged; when "
+        "only one is, the base cost is decreased by this amount to bias "
+        "toward splitting. Set to 0 to disable."),
     cl::Hidden);
 
 static cl::opt<int> BrMergingLikelyBias(
@@ -31620,19 +31621,32 @@ AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
   // MispredictPenalty (~10 cycles on modern Arm cores) weighted by the branch's
   // misprediction probability. With no profile, a branch is ~50/50, so the
   // budget is ~MispredictPenalty/2; the default of 6 sits in the
-  // MispredictPenalty * [1/2 .. 3/4] band used elsewhere for this same tradeoff
-  // (see EarlyIfConversion and AArch64ConditionalCompares). The likely/unlikely
-  // biases below refine the misprediction estimate from BranchProbabilityInfo.
+  // MispredictPenalty * [1/2 .. 1] band used elsewhere for this same tradeoff
+  // (EarlyIfConversion uses /2 or full, AArch64ConditionalCompares uses 3/4).
+  // The comparison is approximate -- those passes count scheduler cycles while
+  // this sums TTI latency units. The likely/unlikely biases below refine the
+  // misprediction estimate from BranchProbabilityInfo.
   if (BaseCost >= 0)
     BaseCost += BrMergingCcmpBias;
 
-  // Withdraw the CCMP discount when either unmerged condition would already be
-  // a single CBZ/CBNZ or TBZ/TBNZ: the split form needs no separate compare, so
-  // merging into a CMP/CCMP chain tends to add instructions rather than save
-  // them.
-  if (BaseCost >= 0 && BrMergingCbzTbnzBias > 0 &&
-      (IsCbzTbnzCandidate(Lhs) || IsCbzTbnzCandidate(Rhs)))
-    BaseCost -= BrMergingCbzTbnzBias;
+  if (BaseCost >= 0 && BrMergingCbzTbnzBias > 0) {
+    bool LhsIsFusedBranch = IsCbzTbnzCandidate(Lhs);
+    bool RhsIsFusedBranch = IsCbzTbnzCandidate(Rhs);
+    // If both conditions would each lower to a single CBZ/CBNZ or TBZ/TBNZ, the
+    // split form is two fused compare-and-branches with no separate compares --
+    // at worst code-size-neutral versus a CMP/CCMP chain, and it keeps the
+    // short-circuit. The dependency-chain cost below would price each compare
+    // as a real instruction (cost ~1) and merge them anyway, so force the
+    // split.
+    if (LhsIsFusedBranch && RhsIsFusedBranch)
+      return {-1, -1, -1};
+    // If only one side is a fused branch, merging still costs it its fused
+    // form, so withdraw the CCMP discount to bias toward splitting; the other
+    // side may still be worth a CMP/CCMP, so leave that to the dependency-chain
+    // cost.
+    if (LhsIsFusedBranch || RhsIsFusedBranch)
+      BaseCost -= BrMergingCbzTbnzBias;
+  }
 
   return {BaseCost, BrMergingLikelyBias.getValue(),
           BrMergingUnlikelyBias.getValue()};
diff --git a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
index 1ba8ea3d3101d..f895dabba5714 100644
--- a/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
+++ b/llvm/test/CodeGen/AArch64/aarch64-tbz.ll
@@ -5,26 +5,15 @@
 ; RUN: llc -verify-machineinstrs -mtriple=aarch64 -global-isel -O0 < %s | FileCheck %s --check-prefixes=CHECK-GIO0
 
 define void @test1(i64 %A, i64 %B) {
-; CHECK-SD-LABEL: test1:
-; CHECK-SD:       // %bb.0: // %entry
-; CHECK-SD-NEXT:    tst x0, #0x4
-; CHECK-SD-NEXT:    and x8, x1, #0x8
-; CHECK-SD-NEXT:    ccmp x8, #0, #4, ne
-; CHECK-SD-NEXT:    b.ne .LBB0_2
-; CHECK-SD-NEXT:  // %bb.1: // %if.end3
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB0_2: // %if.then2
-; CHECK-SD-NEXT:    b foo
-;
-; CHECK-GI-LABEL: test1:
-; CHECK-GI:       // %bb.0: // %entry
-; CHECK-GI-NEXT:    tbz w1, #3, .LBB0_3
-; CHECK-GI-NEXT:  // %bb.1: // %entry
-; CHECK-GI-NEXT:    tbz w0, #2, .LBB0_3
-; CHECK-GI-NEXT:  // %bb.2: // %if.then2
-; CHECK-GI-NEXT:    b foo
-; CHECK-GI-NEXT:  .LBB0_3: // %if.end3
-; CHECK-GI-NEXT:    ret
+; CHECK-LABEL: test1:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    tbz w1, #3, .LBB0_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    tbz w0, #2, .LBB0_3
+; CHECK-NEXT:  // %bb.2: // %if.then2
+; CHECK-NEXT:    b foo
+; CHECK-NEXT:  .LBB0_3: // %if.end3
+; CHECK-NEXT:    ret
 ;
 ; CHECK-GIO0-LABEL: test1:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -68,27 +57,16 @@ if.end3:                                          ; preds = %if.then2, %entry
 }
 
 define void @test2(i64 %A, ptr readonly %B) #0 {
-; CHECK-SD-LABEL: test2:
-; CHECK-SD:       // %bb.0: // %entry
-; CHECK-SD-NEXT:    tst x0, #0x8
-; CHECK-SD-NEXT:    ccmp x1, #0, #4, ne
-; CHECK-SD-NEXT:    b.ne .LBB1_2
-; CHECK-SD-NEXT:  // %bb.1: // %if.end3
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB1_2: // %if.then2
-; CHECK-SD-NEXT:    ldr x1, [x1]
-; CHECK-SD-NEXT:    b foo
-;
-; CHECK-GI-LABEL: test2:
-; CHECK-GI:       // %bb.0: // %entry
-; CHECK-GI-NEXT:    cbz x1, .LBB1_3
-; CHECK-GI-NEXT:  // %bb.1: // %entry
-; CHECK-GI-NEXT:    tbz w0, #3, .LBB1_3
-; CHECK-GI-NEXT:  // %bb.2: // %if.then2
-; CHECK-GI-NEXT:    ldr x1, [x1]
-; CHECK-GI-NEXT:    b foo
-; CHECK-GI-NEXT:  .LBB1_3: // %if.end3
-; CHECK-GI-NEXT:    ret
+; CHECK-LABEL: test2:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    cbz x1, .LBB1_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    tbz w0, #3, .LBB1_3
+; CHECK-NEXT:  // %bb.2: // %if.then2
+; CHECK-NEXT:    ldr x1, [x1]
+; CHECK-NEXT:    b foo
+; CHECK-NEXT:  .LBB1_3: // %if.end3
+; CHECK-NEXT:    ret
 ;
 ; CHECK-GIO0-LABEL: test2:
 ; CHECK-GIO0:       // %bb.0: // %entry
@@ -271,3 +249,6 @@ land.end:
 
 declare void @foo(i64, i64)
 
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; CHECK-GI: {{.*}}
+; CHECK-SD: {{.*}}
diff --git a/llvm/test/CodeGen/AArch64/arm64-ccmp.ll b/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
index b3ed2cf27bd97..54d05c581bf2c 100644
--- a/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-ccmp.ll
@@ -423,31 +423,18 @@ if.end:
 
 ; Convert a cbz in the second block.
 define i32 @cbz_second(i32 %a, i32 %b) nounwind ssp {
-; CHECK-SD-LABEL: cbz_second:
-; CHECK-SD:       ; %bb.0: ; %entry
-; CHECK-SD-NEXT:    cmp w1, #0
-; CHECK-SD-NEXT:    ccmp w0, #0, #4, eq
-; CHECK-SD-NEXT:    b.ne LBB9_2
-; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
-; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-SD-NEXT:    bl _foo
-; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-SD-NEXT:  LBB9_2: ; %if.end
-; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
-; CHECK-SD-NEXT:    ret
-;
-; CHECK-GI-LABEL: cbz_second:
-; CHECK-GI:       ; %bb.0: ; %entry
-; CHECK-GI-NEXT:    cmp w0, #0
-; CHECK-GI-NEXT:    ccmp w1, #0, #0, ne
-; CHECK-GI-NEXT:    b.eq LBB9_2
-; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
-; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-GI-NEXT:    bl _foo
-; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-GI-NEXT:  LBB9_2: ; %if.end
-; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
-; CHECK-GI-NEXT:    ret
+; CHECK-LABEL: cbz_second:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w1, #0, #0, ne
+; CHECK-NEXT:    b.eq LBB9_2
+; CHECK-NEXT:  ; %bb.1: ; %if.then
+; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-NEXT:    bl _foo
+; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-NEXT:  LBB9_2: ; %if.end
+; CHECK-NEXT:    mov w0, #7 ; =0x7
+; CHECK-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 0
   %cmp1 = icmp ne i32 %b, 0
@@ -464,31 +451,18 @@ if.end:
 
 ; Convert a cbnz in the second block.
 define i32 @cbnz_second(i32 %a, i32 %b) nounwind ssp {
-; CHECK-SD-LABEL: cbnz_second:
-; CHECK-SD:       ; %bb.0: ; %entry
-; CHECK-SD-NEXT:    cmp w1, #0
-; CHECK-SD-NEXT:    ccmp w0, #0, #4, ne
-; CHECK-SD-NEXT:    b.ne LBB10_2
-; CHECK-SD-NEXT:  ; %bb.1: ; %if.then
-; CHECK-SD-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-SD-NEXT:    bl _foo
-; CHECK-SD-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-SD-NEXT:  LBB10_2: ; %if.end
-; CHECK-SD-NEXT:    mov w0, #7 ; =0x7
-; CHECK-SD-NEXT:    ret
-;
-; CHECK-GI-LABEL: cbnz_second:
-; CHECK-GI:       ; %bb.0: ; %entry
-; CHECK-GI-NEXT:    cmp w0, #0
-; CHECK-GI-NEXT:    ccmp w1, #0, #4, ne
-; CHECK-GI-NEXT:    b.ne LBB10_2
-; CHECK-GI-NEXT:  ; %bb.1: ; %if.then
-; CHECK-GI-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
-; CHECK-GI-NEXT:    bl _foo
-; CHECK-GI-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
-; CHECK-GI-NEXT:  LBB10_2: ; %if.end
-; CHECK-GI-NEXT:    mov w0, #7 ; =0x7
-; CHECK-GI-NEXT:    ret
+; CHECK-LABEL: cbnz_second:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w1, #0, #4, ne
+; CHECK-NEXT:    b.ne LBB10_2
+; CHECK-NEXT:  ; %bb.1: ; %if.then
+; CHECK-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-NEXT:    bl _foo
+; CHECK-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-NEXT:  LBB10_2: ; %if.end
+; CHECK-NEXT:    mov w0, #7 ; =0x7
+; CHECK-NEXT:    ret
 entry:
   %cmp = icmp eq i32 %a, 0
   %cmp1 = icmp eq i32 %b, 0

>From 8c8d8781d7c0c945475435f8b326691193fabedb Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Mon, 22 Jun 2026 07:42:37 -0700
Subject: [PATCH 7/8] Add condition to avoid expensive fccmp

---
 .../Target/AArch64/AArch64ISelLowering.cpp    |  50 ++--
 .../AArch64/br-cond-merging-cbz-tbnz.ll       | 268 ++++++++++++++++++
 .../CodeGen/AArch64/br-cond-merging-fccmp.ll  | 100 +++++++
 .../CodeGen/AArch64/branch-cond-split-fcmp.ll |  69 +++--
 llvm/test/CodeGen/AArch64/fcvt-i256.ll        |  70 +++--
 5 files changed, 465 insertions(+), 92 deletions(-)
 create mode 100644 llvm/test/CodeGen/AArch64/br-cond-merging-cbz-tbnz.ll
 create mode 100644 llvm/test/CodeGen/AArch64/br-cond-merging-fccmp.ll

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index d315cafbaeecc..be83e35ff18e5 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -160,12 +160,10 @@ cl::opt<bool> EnableSVEGISel(
 static cl::opt<int> BrMergingBaseCostThresh(
     "aarch64-br-merging-base-cost", cl::init(2),
     cl::desc(
-        "Sets the cost threshold for when multiple conditionals will be merged "
-        "into one branch versus be split in multiple branches. Merging "
-        "conditionals saves branches at the cost of additional instructions. "
-        "This value sets the instruction cost limit, below which conditionals "
-        "will be merged, and above which conditionals will be split. Set to -1 "
-        "to never merge branches."),
+        "Cost threshold for merging multiple conditionals into one branch "
+        "versus splitting into multiple branches: conditionals are merged when "
+        "their instruction cost is below this limit and split above it. Set to "
+        "-1 to never merge branches."),
     cl::Hidden);
 
 static cl::opt<int> BrMergingCcmpBias(
@@ -177,36 +175,24 @@ static cl::opt<int> BrMergingCcmpBias(
 
 static cl::opt<int> BrMergingCbzTbnzBias(
     "aarch64-br-merging-cbz-tbnz-bias", cl::init(6),
-    cl::desc(
-        "Adjusts 'aarch64-br-merging-base-cost' for conditions that lower "
-        "to a single CBZ/CBNZ or TBZ/TBNZ compare-and-branch, which need "
-        "no separate compare. When both conditions are such candidates the "
-        "split form is two fused branches and they are never merged; when "
-        "only one is, the base cost is decreased by this amount to bias "
-        "toward splitting. Set to 0 to disable."),
+    cl::desc("Decreases 'aarch64-br-merging-base-cost' when a condition can "
+             "lower to a single CBZ/CBNZ or TBZ/TBNZ compare-and-branch, to "
+             "bias toward splitting. Set to 0 to disable."),
     cl::Hidden);
 
 static cl::opt<int> BrMergingLikelyBias(
     "aarch64-br-merging-likely-bias", cl::init(0),
-    cl::desc("Increases 'aarch64-br-merging-base-cost' in cases that it is "
-             "likely that all conditionals will be executed. For example for "
-             "merging the conditionals (a == b && c > d), if its known that "
-             "a == b is likely, then it is likely that if the conditionals are "
-             "split both sides will be executed, so it may be desirable to "
-             "increase the instruction cost threshold. Set to -1 to never "
-             "merge likely branches."),
+    cl::desc("Increases 'aarch64-br-merging-base-cost' when all conditionals "
+             "are likely to be executed, biasing toward merging. Set to -1 to "
+             "never merge likely branches."),
     cl::Hidden);
 
 static cl::opt<int> BrMergingUnlikelyBias(
     "aarch64-br-merging-unlikely-bias", cl::init(-1),
     cl::desc(
-        "Decreases 'aarch64-br-merging-base-cost' in cases that it is unlikely "
-        "that all conditionals will be executed. For example for merging "
-        "the conditionals (a == b && c > d), if its known that a == b is "
-        "unlikely, then it is unlikely that if the conditionals are split "
-        "both sides will be executed, so it may be desirable to decrease "
-        "the instruction cost threshold. Set to -1 to never merge unlikely "
-        "branches."),
+        "Decreases 'aarch64-br-merging-base-cost' when all conditionals are "
+        "unlikely to be executed, biasing toward splitting. Set to -1 to never "
+        "merge unlikely branches."),
     cl::Hidden);
 
 static cl::opt<bool> UseFEATCPACodegen(
@@ -31587,6 +31573,16 @@ AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
                                                      const Value *Rhs) const {
   using namespace llvm::PatternMatch;
 
+  // Do not fold floating-point conditions into an FCMP/FCCMP chain. FCCMP is
+  // never cheaper than the FCMP it would replace and is far costlier on some
+  // cores (e.g. 9 vs 3 cycles, and 3 pipes vs 1, on Ampere1), and unordered
+  // predicates expand a single merge into several FCCMPs (see
+  // branch-cond-split-fcmp.ll). The dependency-chain heuristic below cannot see
+  // that expansion, so keep floating-point conditions split into separate
+  // compares-and-branches, as the generic target default does.
+  if (isa<FCmpInst>(Lhs) || isa<FCmpInst>(Rhs))
+    return {-1, -1, -1};
+
   // Returns true if \p V is a branch condition that AArch64 can lower to a
   // single compare-and-branch (CBZ/CBNZ) or test-bit-and-branch (TBZ/TBNZ),
   // i.e. without materializing a separate compare. Merging such a condition
diff --git a/llvm/test/CodeGen/AArch64/br-cond-merging-cbz-tbnz.ll b/llvm/test/CodeGen/AArch64/br-cond-merging-cbz-tbnz.ll
new file mode 100644
index 0000000000000..b225881ce1609
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/br-cond-merging-cbz-tbnz.ll
@@ -0,0 +1,268 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=aarch64-linux-gnu | FileCheck %s --check-prefixes=SPLIT
+; RUN: llc < %s -mtriple=aarch64-linux-gnu -aarch64-br-merging-cbz-tbnz-bias=0 | FileCheck %s --check-prefixes=MERGE
+
+; When one side of a merged branch condition would lower to a single CBZ/CBNZ or
+; TBZ/TBNZ compare-and-branch, getJumpConditionMergingParams withdraws the CCMP
+; merging discount so the fused branch is kept (and the expensive RHS is
+; short-circuited) instead of folded into a CMP/CCMP chain. The second RUN line
+; disables the carve-out (bias=0) and recovers the merged CCMP form.
+
+declare void @sink_a()
+declare void @sink_b()
+
+; icmp eq ptr, null -> CBZ.
+define void @cbz_eq_zero(ptr %p, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: cbz_eq_zero:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    cbz x0, .LBB0_3
+; SPLIT-NEXT:  // %bb.1: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    b.gt .LBB0_3
+; SPLIT-NEXT:  // %bb.2: // %fall
+; SPLIT-NEXT:    b sink_b
+; SPLIT-NEXT:  .LBB0_3: // %taken
+; SPLIT-NEXT:    b sink_a
+;
+; MERGE-LABEL: cbz_eq_zero:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    ccmp x0, #0, #4, le
+; MERGE-NEXT:    b.ne .LBB0_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB0_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %c0 = icmp eq ptr %p, null
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %or = or i1 %c0, %c1
+  br i1 %or, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; icmp slt i32, 0 -> TBNZ on the sign bit.
+define void @tbnz_slt_zero(i32 %s, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: tbnz_slt_zero:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    tbz w0, #31, .LBB1_3
+; SPLIT-NEXT:  // %bb.1: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    b.le .LBB1_3
+; SPLIT-NEXT:  // %bb.2: // %taken
+; SPLIT-NEXT:    b sink_a
+; SPLIT-NEXT:  .LBB1_3: // %fall
+; SPLIT-NEXT:    b sink_b
+;
+; MERGE-LABEL: tbnz_slt_zero:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    ccmn w0, #1, #0, gt
+; MERGE-NEXT:    b.gt .LBB1_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB1_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %c0 = icmp slt i32 %s, 0
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; icmp sgt i32, -1 (i.e. s >= 0) -> TBZ on the sign bit.
+define void @tbz_sgt_allones(i32 %s, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: tbz_sgt_allones:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    tbnz w0, #31, .LBB2_3
+; SPLIT-NEXT:  // %bb.1: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    b.le .LBB2_3
+; SPLIT-NEXT:  // %bb.2: // %taken
+; SPLIT-NEXT:    b sink_a
+; SPLIT-NEXT:  .LBB2_3: // %fall
+; SPLIT-NEXT:    b sink_b
+;
+; MERGE-LABEL: tbz_sgt_allones:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    ccmp w0, #0, #8, gt
+; MERGE-NEXT:    b.mi .LBB2_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB2_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %c0 = icmp sgt i32 %s, -1
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; icmp eq (and X, pow2), 0 -> TBZ on a single bit.
+define void @tbz_and_pow2(i32 %s, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: tbz_and_pow2:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    tbz w0, #3, .LBB3_3
+; SPLIT-NEXT:  // %bb.1: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    b.gt .LBB3_3
+; SPLIT-NEXT:  // %bb.2: // %fall
+; SPLIT-NEXT:    b sink_b
+; SPLIT-NEXT:  .LBB3_3: // %taken
+; SPLIT-NEXT:    b sink_a
+;
+; MERGE-LABEL: tbz_and_pow2:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    cset w8, le
+; MERGE-NEXT:    and w8, w8, w0, lsr #3
+; MERGE-NEXT:    tbnz w8, #0, .LBB3_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB3_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %m = and i32 %s, 8
+  %c0 = icmp eq i32 %m, 0
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %or = or i1 %c0, %c1
+  br i1 %or, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; trunc i32 to i1 tests bit #0 -> TBNZ.
+define void @tbnz_trunc(i32 %s, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: tbnz_trunc:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    tbz w0, #0, .LBB4_3
+; SPLIT-NEXT:  // %bb.1: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    b.le .LBB4_3
+; SPLIT-NEXT:  // %bb.2: // %taken
+; SPLIT-NEXT:    b sink_a
+; SPLIT-NEXT:  .LBB4_3: // %fall
+; SPLIT-NEXT:    b sink_b
+;
+; MERGE-LABEL: tbnz_trunc:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    cset w8, le
+; MERGE-NEXT:    orn w8, w8, w0
+; MERGE-NEXT:    tbnz w8, #0, .LBB4_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB4_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %c0 = trunc i32 %s to i1
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; Negative control: neither side is a CBZ/TBNZ candidate, so the carve-out does
+; not apply and the conditions still merge into a CCMP chain under both RUN
+; lines.
+define void @no_carveout_merges(i32 %s, i32 %x, i32 %y, i32 %z, i32 %k) {
+; SPLIT-LABEL: no_carveout_merges:
+; SPLIT:       // %bb.0: // %entry
+; SPLIT-NEXT:    add w8, w1, w2
+; SPLIT-NEXT:    madd w8, w8, w3, w1
+; SPLIT-NEXT:    cmp w8, w4
+; SPLIT-NEXT:    ccmp w0, #8, #8, gt
+; SPLIT-NEXT:    b.lt .LBB5_2
+; SPLIT-NEXT:  // %bb.1: // %taken
+; SPLIT-NEXT:    b sink_a
+; SPLIT-NEXT:  .LBB5_2: // %fall
+; SPLIT-NEXT:    b sink_b
+;
+; MERGE-LABEL: no_carveout_merges:
+; MERGE:       // %bb.0: // %entry
+; MERGE-NEXT:    add w8, w1, w2
+; MERGE-NEXT:    madd w8, w8, w3, w1
+; MERGE-NEXT:    cmp w8, w4
+; MERGE-NEXT:    ccmp w0, #8, #8, gt
+; MERGE-NEXT:    b.lt .LBB5_2
+; MERGE-NEXT:  // %bb.1: // %taken
+; MERGE-NEXT:    b sink_a
+; MERGE-NEXT:  .LBB5_2: // %fall
+; MERGE-NEXT:    b sink_b
+entry:
+  %c0 = icmp sgt i32 %s, 7
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %a3 = add i32 %a2, %x
+  %c1 = icmp sgt i32 %a3, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
diff --git a/llvm/test/CodeGen/AArch64/br-cond-merging-fccmp.ll b/llvm/test/CodeGen/AArch64/br-cond-merging-fccmp.ll
new file mode 100644
index 0000000000000..edd7aca564047
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/br-cond-merging-fccmp.ll
@@ -0,0 +1,100 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=aarch64-linux-gnu | FileCheck %s
+
+; getJumpConditionMergingParams never folds a floating-point condition into an
+; FCMP/FCCMP chain: FCCMP is never cheaper than the FCMP it replaces (and is far
+; costlier on some cores), so floating-point conditions are kept split into
+; separate compares-and-branches. Integer conditions are unaffected and still
+; merge into a CMP/CCMP chain.
+
+declare void @sink_a()
+declare void @sink_b()
+
+; fp && fp -> kept split, no FCCMP.
+define void @fp_and_split(double %s, double %t, double %x, double %y, double %z, double %k) {
+; CHECK-LABEL: fp_and_split:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    fcmp d0, d1
+; CHECK-NEXT:    b.pl .LBB0_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    fadd d0, d2, d3
+; CHECK-NEXT:    fmul d0, d0, d4
+; CHECK-NEXT:    fcmp d0, d5
+; CHECK-NEXT:    b.le .LBB0_3
+; CHECK-NEXT:  // %bb.2: // %taken
+; CHECK-NEXT:    b sink_a
+; CHECK-NEXT:  .LBB0_3: // %fall
+; CHECK-NEXT:    b sink_b
+entry:
+  %c0 = fcmp olt double %s, %t
+  %a1 = fadd double %x, %y
+  %a2 = fmul double %a1, %z
+  %c1 = fcmp ogt double %a2, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; fp || fp -> kept split, no FCCMP.
+define void @fp_or_split(double %s, double %t, double %x, double %y, double %z, double %k) {
+; CHECK-LABEL: fp_or_split:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    fcmp d0, d1
+; CHECK-NEXT:    b.mi .LBB1_3
+; CHECK-NEXT:  // %bb.1: // %entry
+; CHECK-NEXT:    fadd d0, d2, d3
+; CHECK-NEXT:    fmul d0, d0, d4
+; CHECK-NEXT:    fcmp d0, d5
+; CHECK-NEXT:    b.gt .LBB1_3
+; CHECK-NEXT:  // %bb.2: // %fall
+; CHECK-NEXT:    b sink_b
+; CHECK-NEXT:  .LBB1_3: // %taken
+; CHECK-NEXT:    b sink_a
+entry:
+  %c0 = fcmp olt double %s, %t
+  %a1 = fadd double %x, %y
+  %a2 = fmul double %a1, %z
+  %c1 = fcmp ogt double %a2, %k
+  %or = or i1 %c0, %c1
+  br i1 %or, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
+
+; Integer control: the same shape with integer compares still folds to a
+; CMP/CCMP chain, confirming the carve-out is floating-point specific.
+define void @int_and_merges(i32 %s, i32 %t, i32 %x, i32 %y, i32 %z, i32 %k) {
+; CHECK-LABEL: int_and_merges:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    add w8, w2, w3
+; CHECK-NEXT:    mul w8, w8, w4
+; CHECK-NEXT:    cmp w8, w5
+; CHECK-NEXT:    ccmp w0, w1, #0, gt
+; CHECK-NEXT:    b.ge .LBB2_2
+; CHECK-NEXT:  // %bb.1: // %taken
+; CHECK-NEXT:    b sink_a
+; CHECK-NEXT:  .LBB2_2: // %fall
+; CHECK-NEXT:    b sink_b
+entry:
+  %c0 = icmp slt i32 %s, %t
+  %a1 = add i32 %x, %y
+  %a2 = mul i32 %a1, %z
+  %c1 = icmp sgt i32 %a2, %k
+  %and = and i1 %c0, %c1
+  br i1 %and, label %taken, label %fall
+taken:
+  tail call void @sink_a()
+  ret void
+fall:
+  tail call void @sink_b()
+  ret void
+}
diff --git a/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll b/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
index 9ed2d29b91096..10c958abc71d6 100644
--- a/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
+++ b/llvm/test/CodeGen/AArch64/branch-cond-split-fcmp.ll
@@ -7,20 +7,19 @@ declare i64 @bar()
 define i64 @test_or(float %a, float %b) {
 ; CHECK-SD-LABEL: test_or:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    movi d2, #0000000000000000
-; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    fccmp s0, s2, #0, eq
-; CHECK-SD-NEXT:    cset w8, eq
-; CHECK-SD-NEXT:    tbnz w8, #0, .LBB0_2
-; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    fcmp s0, #0.0
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB0_2: // %bb4
+; CHECK-SD-NEXT:    b.ne .LBB0_3
+; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    fcmp s1, #0.0
+; CHECK-SD-NEXT:    b.ne .LBB0_3
+; CHECK-SD-NEXT:  // %bb.2: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-SD-NEXT:  .LBB0_3: // %common.ret
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_or:
@@ -57,19 +56,19 @@ bb4:
 define i64 @test_or_select(float %a, float %b) {
 ; CHECK-SD-LABEL: test_or_select:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    movi d2, #0000000000000000
-; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    fccmp s0, s2, #0, eq
-; CHECK-SD-NEXT:    b.eq .LBB1_2
-; CHECK-SD-NEXT:  // %bb.1:
+; CHECK-SD-NEXT:    fcmp s0, #0.0
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB1_2: // %bb4
+; CHECK-SD-NEXT:    b.ne .LBB1_3
+; CHECK-SD-NEXT:  // %bb.1: // %bb1
+; CHECK-SD-NEXT:    fcmp s1, #0.0
+; CHECK-SD-NEXT:    b.ne .LBB1_3
+; CHECK-SD-NEXT:  // %bb.2: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-SD-NEXT:  .LBB1_3: // %common.ret
 ; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_or_select:
@@ -106,23 +105,23 @@ bb4:
 define i64 @test_and(float %a, float %b) {
 ; CHECK-SD-LABEL: test_and:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    movi d2, #0000000000000000
+; CHECK-SD-NEXT:    fcmp s0, #0.0
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    b.mi .LBB2_2
+; CHECK-SD-NEXT:    b.gt .LBB2_2
+; CHECK-SD-NEXT:  // %bb.1: // %bb1
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    fccmp s1, s2, #8, le
-; CHECK-SD-NEXT:    fccmp s0, s2, #0, pl
-; CHECK-SD-NEXT:    fccmp s0, s2, #8, le
-; CHECK-SD-NEXT:    cset w8, pl
-; CHECK-SD-NEXT:    tbz w8, #0, .LBB2_2
-; CHECK-SD-NEXT:  // %bb.1: // %bb4
+; CHECK-SD-NEXT:    b.eq .LBB2_3
+; CHECK-SD-NEXT:    b.vs .LBB2_3
+; CHECK-SD-NEXT:  .LBB2_2: // %common.ret
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB2_3: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB2_2:
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_and:
 ; CHECK-GI:       // %bb.0: // %bb1
@@ -161,23 +160,23 @@ bb4:
 define i64 @test_and_select(float %a, float %b) {
 ; CHECK-SD-LABEL: test_and_select:
 ; CHECK-SD:       // %bb.0: // %bb1
-; CHECK-SD-NEXT:    movi d2, #0000000000000000
+; CHECK-SD-NEXT:    fcmp s0, #0.0
+; CHECK-SD-NEXT:    mov x0, xzr
+; CHECK-SD-NEXT:    b.mi .LBB3_2
+; CHECK-SD-NEXT:    b.gt .LBB3_2
+; CHECK-SD-NEXT:  // %bb.1: // %bb1
 ; CHECK-SD-NEXT:    fcmp s1, #0.0
-; CHECK-SD-NEXT:    fccmp s1, s2, #8, le
-; CHECK-SD-NEXT:    fccmp s0, s2, #0, pl
-; CHECK-SD-NEXT:    fccmp s0, s2, #8, le
-; CHECK-SD-NEXT:    cset w8, pl
-; CHECK-SD-NEXT:    tbz w8, #0, .LBB3_2
-; CHECK-SD-NEXT:  // %bb.1: // %bb4
+; CHECK-SD-NEXT:    b.eq .LBB3_3
+; CHECK-SD-NEXT:    b.vs .LBB3_3
+; CHECK-SD-NEXT:  .LBB3_2: // %common.ret
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB3_3: // %bb4
 ; CHECK-SD-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-SD-NEXT:    .cfi_offset w30, -16
 ; CHECK-SD-NEXT:    bl bar
 ; CHECK-SD-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB3_2:
-; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    ret
 ;
 ; CHECK-GI-LABEL: test_and_select:
 ; CHECK-GI:       // %bb.0: // %bb1
diff --git a/llvm/test/CodeGen/AArch64/fcvt-i256.ll b/llvm/test/CodeGen/AArch64/fcvt-i256.ll
index 7fc10f4574c76..3158b0ea560db 100644
--- a/llvm/test/CodeGen/AArch64/fcvt-i256.ll
+++ b/llvm/test/CodeGen/AArch64/fcvt-i256.ll
@@ -1295,27 +1295,26 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-LABEL: f32_to_s256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fmov w8, s0
-; CHECK-SD-NEXT:    fcmp s0, s0
-; CHECK-SD-NEXT:    mov w9, #127 // =0x7f
-; CHECK-SD-NEXT:    ubfx w10, w8, #23, #8
-; CHECK-SD-NEXT:    ccmp w10, w9, #0, vc
-; CHECK-SD-NEXT:    b.hs .LBB8_2
-; CHECK-SD-NEXT:  // %bb.1:
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    mov x1, xzr
-; CHECK-SD-NEXT:    mov x2, xzr
-; CHECK-SD-NEXT:    mov x3, xzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_2: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    ubfx w10, w8, #23, #8
+; CHECK-SD-NEXT:    cmp w10, #127
+; CHECK-SD-NEXT:    b.lo .LBB8_5
+; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
+; CHECK-SD-NEXT:    fcmp s0, s0
+; CHECK-SD-NEXT:    mov x1, x0
+; CHECK-SD-NEXT:    mov x2, x0
+; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    b.vs .LBB8_7
+; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    cmp w10, #255
-; CHECK-SD-NEXT:    b.hs .LBB8_6
+; CHECK-SD-NEXT:    b.hs .LBB8_8
 ; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
 ; CHECK-SD-NEXT:    sbfx x9, x8, #31, #1
 ; CHECK-SD-NEXT:    mov w11, #8388608 // =0x800000
 ; CHECK-SD-NEXT:    cmp w10, #149
 ; CHECK-SD-NEXT:    bfxil w11, w8, #0, #23
 ; CHECK-SD-NEXT:    orr x8, x9, #0x1
-; CHECK-SD-NEXT:    b.hi .LBB8_5
+; CHECK-SD-NEXT:    b.hi .LBB8_6
 ; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
 ; CHECK-SD-NEXT:    mov w12, #150 // =0x96
 ; CHECK-SD-NEXT:    sub w10, w12, w10
@@ -1331,7 +1330,12 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-NEXT:    adcs x2, x15, x12
 ; CHECK-SD-NEXT:    adc x3, x11, x9
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_5: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB8_5:
+; CHECK-SD-NEXT:    mov x1, x0
+; CHECK-SD-NEXT:    mov x2, x0
+; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB8_6: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
@@ -1392,8 +1396,9 @@ define i256 @f32_to_s256_sat(float %val) {
 ; CHECK-SD-NEXT:    adds x2, x11, x12
 ; CHECK-SD-NEXT:    adc x3, x8, x9
 ; CHECK-SD-NEXT:    add sp, sp, #64
+; CHECK-SD-NEXT:  .LBB8_7: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB8_6: // %fp-to-i-if-saturate
+; CHECK-SD-NEXT:  .LBB8_8: // %fp-to-i-if-saturate
 ; CHECK-SD-NEXT:    // kill: def $w8 killed $w8 killed $x8 def $x8
 ; CHECK-SD-NEXT:    sbfx x8, x8, #31, #1
 ; CHECK-SD-NEXT:    mvn x0, x8
@@ -1729,27 +1734,26 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-LABEL: f64_to_s256_sat:
 ; CHECK-SD:       // %bb.0: // %fp-to-i-entry
 ; CHECK-SD-NEXT:    fmov x9, d0
-; CHECK-SD-NEXT:    fcmp d0, d0
-; CHECK-SD-NEXT:    mov w8, #1023 // =0x3ff
-; CHECK-SD-NEXT:    ubfx x10, x9, #52, #11
-; CHECK-SD-NEXT:    ccmp x10, x8, #0, vc
-; CHECK-SD-NEXT:    b.hs .LBB10_2
-; CHECK-SD-NEXT:  // %bb.1:
 ; CHECK-SD-NEXT:    mov x0, xzr
-; CHECK-SD-NEXT:    mov x1, xzr
-; CHECK-SD-NEXT:    mov x2, xzr
-; CHECK-SD-NEXT:    mov x3, xzr
-; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_2: // %fp-to-i-if-check.saturate
+; CHECK-SD-NEXT:    ubfx x10, x9, #52, #11
+; CHECK-SD-NEXT:    cmp x10, #1023
+; CHECK-SD-NEXT:    b.lo .LBB10_5
+; CHECK-SD-NEXT:  // %bb.1: // %fp-to-i-entry
+; CHECK-SD-NEXT:    fcmp d0, d0
+; CHECK-SD-NEXT:    mov x1, x0
+; CHECK-SD-NEXT:    mov x2, x0
+; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    b.vs .LBB10_7
+; CHECK-SD-NEXT:  // %bb.2: // %fp-to-i-if-check.saturate
 ; CHECK-SD-NEXT:    asr x8, x9, #63
 ; CHECK-SD-NEXT:    cmp x10, #1278
-; CHECK-SD-NEXT:    b.hs .LBB10_6
+; CHECK-SD-NEXT:    b.hs .LBB10_8
 ; CHECK-SD-NEXT:  // %bb.3: // %fp-to-i-if-check.exp.size
 ; CHECK-SD-NEXT:    mov x11, #4503599627370496 // =0x10000000000000
 ; CHECK-SD-NEXT:    cmp x10, #1074
 ; CHECK-SD-NEXT:    bfxil x11, x9, #0, #52
 ; CHECK-SD-NEXT:    orr x9, x8, #0x1
-; CHECK-SD-NEXT:    b.hi .LBB10_5
+; CHECK-SD-NEXT:    b.hi .LBB10_6
 ; CHECK-SD-NEXT:  // %bb.4: // %fp-to-i-if-exp.small
 ; CHECK-SD-NEXT:    mov w12, #1075 // =0x433
 ; CHECK-SD-NEXT:    sub x10, x12, x10
@@ -1764,7 +1768,12 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-NEXT:    adcs x2, x11, x8
 ; CHECK-SD-NEXT:    adc x3, x12, x11
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_5: // %fp-to-i-if-exp.large
+; CHECK-SD-NEXT:  .LBB10_5:
+; CHECK-SD-NEXT:    mov x1, x0
+; CHECK-SD-NEXT:    mov x2, x0
+; CHECK-SD-NEXT:    mov x3, x0
+; CHECK-SD-NEXT:    ret
+; CHECK-SD-NEXT:  .LBB10_6: // %fp-to-i-if-exp.large
 ; CHECK-SD-NEXT:    sub sp, sp, #64
 ; CHECK-SD-NEXT:    .cfi_def_cfa_offset 64
 ; CHECK-SD-NEXT:    movi v0.2d, #0000000000000000
@@ -1825,8 +1834,9 @@ define i256 @f64_to_s256_sat(double %val) {
 ; CHECK-SD-NEXT:    adds x2, x11, x12
 ; CHECK-SD-NEXT:    adc x3, x9, x8
 ; CHECK-SD-NEXT:    add sp, sp, #64
+; CHECK-SD-NEXT:  .LBB10_7: // %fp-to-i-cleanup
 ; CHECK-SD-NEXT:    ret
-; CHECK-SD-NEXT:  .LBB10_6: // %fp-to-i-if-saturate
+; CHECK-SD-NEXT:  .LBB10_8: // %fp-to-i-if-saturate
 ; CHECK-SD-NEXT:    mvn x0, x8
 ; CHECK-SD-NEXT:    eor x3, x8, #0x7fffffffffffffff
 ; CHECK-SD-NEXT:    mov x1, x0

>From 6c94a952de2952f8b37d2eda571843ab3c4e5c59 Mon Sep 17 00:00:00 2001
From: Kunal Pathak <kupathak at fb.com>
Date: Mon, 22 Jun 2026 07:49:47 -0700
Subject: [PATCH 8/8] Trim comments a little bit

---
 .../Target/AArch64/AArch64ISelLowering.cpp    | 30 +++++++------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index be83e35ff18e5..d0722466bcb53 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -31573,13 +31573,10 @@ AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
                                                      const Value *Rhs) const {
   using namespace llvm::PatternMatch;
 
-  // Do not fold floating-point conditions into an FCMP/FCCMP chain. FCCMP is
-  // never cheaper than the FCMP it would replace and is far costlier on some
-  // cores (e.g. 9 vs 3 cycles, and 3 pipes vs 1, on Ampere1), and unordered
-  // predicates expand a single merge into several FCCMPs (see
-  // branch-cond-split-fcmp.ll). The dependency-chain heuristic below cannot see
-  // that expansion, so keep floating-point conditions split into separate
-  // compares-and-branches, as the generic target default does.
+  // Keep floating-point conditions split rather than folding them into an
+  // FCMP/FCCMP chain: FCCMP is never cheaper than the FCMP it replaces, and
+  // unordered predicates expand one merge into several FCCMPs that the cost
+  // heuristic below cannot see (regressed branch-cond-split-fcmp.ll).
   if (isa<FCmpInst>(Lhs) || isa<FCmpInst>(Rhs))
     return {-1, -1, -1};
 
@@ -31609,19 +31606,12 @@ AArch64TargetLowering::getJumpConditionMergingParams(Instruction::BinaryOps Opc,
   };
 
   int BaseCost = BrMergingBaseCostThresh.getValue();
-  // CCMP is always available on AArch64 and folds the second compare and the
-  // branch into a single 1-cycle op (e.g. V2Write_1c_1F_1Flg on Neoverse V2),
-  // so merging is worth tolerating extra speculated work on the RHS dependency
-  // chain. The bias is that tolerance measured in TTI latency units: it stands
-  // in for the amortized cost of the branch the merge eliminates, i.e. roughly
-  // MispredictPenalty (~10 cycles on modern Arm cores) weighted by the branch's
-  // misprediction probability. With no profile, a branch is ~50/50, so the
-  // budget is ~MispredictPenalty/2; the default of 6 sits in the
-  // MispredictPenalty * [1/2 .. 1] band used elsewhere for this same tradeoff
-  // (EarlyIfConversion uses /2 or full, AArch64ConditionalCompares uses 3/4).
-  // The comparison is approximate -- those passes count scheduler cycles while
-  // this sums TTI latency units. The likely/unlikely biases below refine the
-  // misprediction estimate from BranchProbabilityInfo.
+  // CCMP folds the second compare and the branch into a single cheap op, so
+  // merging is worth tolerating extra speculated work on the RHS dependency
+  // chain. The bias budgets that tolerance in TTI latency units, standing in
+  // for the amortized cost of the eliminated branch (~MispredictPenalty weighted
+  // by misprediction probability; ~50/50 with no profile, hence the default 6
+  // ~= MispredictPenalty/2). The likely/unlikely biases below refine that.
   if (BaseCost >= 0)
     BaseCost += BrMergingCcmpBias;
 



More information about the llvm-commits mailing list