[llvm] [AArch64] Fold cmp #0 into its definition when the flags feed a CCMP (PR #221858)

Andrew Gaul via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 18:15:03 PDT 2026


https://github.com/gaul created https://github.com/llvm/llvm-project/pull/221858

The peephole that replaces `add/sub/and/bic Rd, ...; cmp Rd, #0` with the flag-setting form of the defining instruction, and the one that removes `cmp #0` after a `csinc`, analyze the users of the compare's flags with examineCFlagsUse. That analysis only knew branches and selects, so a compare whose flags feed a conditional compare was always left alone.

This is the normal shape of the first compare of a ccmp chain. The conditional compare pass replaces a CBZ/CBNZ head with `cmp Rn, #0`, and instruction selection emits the same compare for the ADD/SUB leaf of a conjunction. Both leave sequences such as

```
  and  w9, w8, #3
  cmp  w9, #0
  ccmp w8, #0, #4, ne
```

where the `and` could set the flags itself.

A conditional compare reads NZCV through its condition code operand exactly like a branch or a select, and the NZCV it defines afterwards is a new value that ends the live range of the compared flags. Teach the flag-use analysis to find the condition code of CCMP, CCMN and FCCMP so the existing safety checks (only N and Z may be used, V only with nsw or after AND/BIC) apply to them unchanged. The condition optimizer keeps using the narrower branch-or-select helper.

Scanning librustc_driver and Firefox's XUL found this sequence 489 and 408 times respectively.

>From 1cbe116f0dd644625621983cf67a97cf54d39979 Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Mon, 7 Sep 2026 17:42:16 -0700
Subject: [PATCH 1/2] [AArch64] Precommit tests for cmp #0 feeding a
 conditional compare (NFC)

The peephole that folds a compare with zero into the flag-setting form of
the instruction defining the compared register (or removes it after a
CSINC) gives up when the flags are consumed by a CCMP, CCMN or FCCMP.
Add tests for the current output: a MIR test that runs the peephole
directly, and an IR test where the compare comes from the conditional
compare pass (CBZ/CBNZ head) or from selecting the ADD/SUB leaf of a
conjunction.

Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01RN4gpjgX8QWXRuQoBzYace
---
 .../AArch64/ccmp-cmp-zero-flag-setting.ll     | 304 ++++++++++++++++++
 .../AArch64/peephole-cmp-zero-ccmp-user.mir   | 285 ++++++++++++++++
 2 files changed, 589 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
 create mode 100644 llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir

diff --git a/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll b/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
new file mode 100644
index 0000000000000..955309e5c1f82
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
@@ -0,0 +1,304 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=aarch64 -aarch64-enable-atomic-cfg-tidy=0 -aarch64-enable-ccmp -aarch64-stress-ccmp -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK,CHECK-SD
+; RUN: llc -mtriple=aarch64 -aarch64-enable-atomic-cfg-tidy=0 -aarch64-enable-ccmp -aarch64-stress-ccmp -verify-machineinstrs -global-isel < %s | FileCheck %s --check-prefixes=CHECK,CHECK-GI
+
+; When the conditional compare pass merges a block into a CBZ/CBNZ head, it
+; replaces the branch with `cmp Rn, #0`. If Rn is defined by an ADD, SUB, AND
+; or BIC in the same block, that compare folds into the flag-setting form of
+; the defining instruction.
+
+declare void @use32(i32)
+declare void @use64(i64)
+declare void @other()
+
+define void @and_live(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: and_live:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    and w0, w0, w1
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w2, #7, #4, ne
+; CHECK-NEXT:    b.ne .LBB0_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl use32
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB0_2: // %else
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = and i32 %x, %y
+  %c1 = icmp eq i32 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i32 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use32(i32 %a)
+  ret void
+
+else:
+  call void @other()
+  ret void
+}
+
+define void @and_dead(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: and_dead:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    and w8, w0, w1
+; CHECK-NEXT:    cmp w8, #0
+; CHECK-NEXT:    ccmp w2, #7, #4, eq
+; CHECK-NEXT:    b.ne .LBB1_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB1_2: // %else
+; CHECK-NEXT:    mov w0, w2
+; CHECK-NEXT:    bl use32
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = and i32 %x, %y
+  %c1 = icmp ne i32 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i32 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @other()
+  ret void
+
+else:
+  call void @use32(i32 %z)
+  ret void
+}
+
+define void @and_imm(i32 %x, i32 %z) {
+; CHECK-LABEL: and_imm:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    and w0, w0, #0xff
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w1, #7, #4, ne
+; CHECK-NEXT:    b.ne .LBB2_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl use32
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB2_2: // %else
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = and i32 %x, 255
+  %c1 = icmp eq i32 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i32 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use32(i32 %a)
+  ret void
+
+else:
+  call void @other()
+  ret void
+}
+
+define void @bic(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: bic:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    bic w0, w0, w1
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w2, #7, #4, ne
+; CHECK-NEXT:    b.ne .LBB3_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl use32
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB3_2: // %else
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %ny = xor i32 %y, -1
+  %a = and i32 %x, %ny
+  %c1 = icmp eq i32 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i32 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use32(i32 %a)
+  ret void
+
+else:
+  call void @other()
+  ret void
+}
+
+define void @add(i64 %x, i64 %y, i64 %z) {
+; CHECK-LABEL: add:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    add x0, x0, x1
+; CHECK-NEXT:    cmp x0, #0
+; CHECK-NEXT:    ccmp x2, #7, #4, ne
+; CHECK-NEXT:    b.ne .LBB4_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl use64
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB4_2: // %else
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = add i64 %x, %y
+  %c1 = icmp eq i64 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i64 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use64(i64 %a)
+  ret void
+
+else:
+  call void @other()
+  ret void
+}
+
+define void @sub_imm(i64 %x, i64 %z) {
+; CHECK-LABEL: sub_imm:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    sub x0, x0, #1
+; CHECK-NEXT:    cmp x0, #0
+; CHECK-NEXT:    ccmp x1, #7, #4, eq
+; CHECK-NEXT:    b.ne .LBB5_2
+; CHECK-NEXT:  // %bb.1: // %then
+; CHECK-NEXT:    bl use64
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB5_2: // %else
+; CHECK-NEXT:    bl other
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = sub i64 %x, 1
+  %c1 = icmp ne i64 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i64 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use64(i64 %a)
+  ret void
+
+else:
+  call void @other()
+  ret void
+}
+
+; The compare only folds into a definition in its own block.
+define void @and_other_block(i32 %x, i32 %y, i32 %z, i1 %p) {
+; CHECK-LABEL: and_other_block:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
+; CHECK-NEXT:    .cfi_def_cfa_offset 16
+; CHECK-NEXT:    .cfi_offset w30, -16
+; CHECK-NEXT:    and w0, w0, w1
+; CHECK-NEXT:    tbz w3, #0, .LBB6_2
+; CHECK-NEXT:  // %bb.1: // %head
+; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ccmp w2, #7, #4, ne
+; CHECK-NEXT:  .LBB6_2: // %else
+; CHECK-NEXT:    bl use32
+; CHECK-NEXT:    ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT:    ret
+entry:
+  %a = and i32 %x, %y
+  br i1 %p, label %head, label %else
+
+head:
+  %c1 = icmp eq i32 %a, 0
+  br i1 %c1, label %then, label %check2
+
+check2:
+  %c2 = icmp eq i32 %z, 7
+  br i1 %c2, label %then, label %else
+
+then:
+  call void @use32(i32 %a)
+  ret void
+
+else:
+  call void @use32(i32 %a)
+  ret void
+}
+
+; The same compare comes out of instruction selection when the ADD/SUB leaf is
+; the first compare of a conjunction.
+define i64 @add_or_leaf(i64 %x, i64 %y, i64 %z) {
+; CHECK-LABEL: add_or_leaf:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    add x8, x0, x1
+; CHECK-NEXT:    cmp x8, #0
+; CHECK-NEXT:    ccmp x2, #7, #4, ne
+; CHECK-NEXT:    csel x0, x8, x2, eq
+; CHECK-NEXT:    ret
+  %a = add i64 %x, %y
+  %c1 = icmp eq i64 %z, 7
+  %c2 = icmp eq i64 %a, 0
+  %o = or i1 %c1, %c2
+  %r = select i1 %o, i64 %a, i64 %z
+  ret i64 %r
+}
+
+define i64 @sub_and_leaf(i64 %x, i64 %y, i64 %z) {
+; CHECK-LABEL: sub_and_leaf:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    sub x8, x0, x1
+; CHECK-NEXT:    cmp x8, #0
+; CHECK-NEXT:    ccmp x2, #7, #2, ne
+; CHECK-NEXT:    csel x0, x8, x2, lo
+; CHECK-NEXT:    ret
+  %a = sub i64 %x, %y
+  %c1 = icmp ult i64 %z, 7
+  %c2 = icmp ne i64 %a, 0
+  %o = and i1 %c1, %c2
+  %r = select i1 %o, i64 %a, i64 %z
+  ret i64 %r
+}
+;; 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/peephole-cmp-zero-ccmp-user.mir b/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
new file mode 100644
index 0000000000000..b2b3771cda987
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
@@ -0,0 +1,285 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=aarch64 -run-pass=peephole-opt -verify-machineinstrs %s -o - | FileCheck %s
+# RUN: llc -mtriple=aarch64 -passes=peephole-opt -verify-machineinstrs %s -o - | FileCheck %s
+#
+# A compare with zero whose flags are consumed by a conditional compare
+# (CCMP, CCMN, FCCMP) can be folded into the flag-setting form of the
+# instruction defining the compared register, or removed after a CSINC, just
+# as when the consumer is a branch or a select. The conditional compare reads
+# NZCV through its condition code operand and then defines a new NZCV value.
+
+---
+name:            ands_from_and_rr_ccmp_wi
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1, $w2
+    ; CHECK-LABEL: name: ands_from_and_rr_ccmp_wi
+    ; CHECK: liveins: $w0, $w1, $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
+    ; CHECK-NEXT: [[ANDWrr:%[0-9]+]]:gpr32common = ANDWrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ANDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 4, 1, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDWrr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32 = COPY $w0
+    %1:gpr32 = COPY $w1
+    %2:gpr32 = COPY $w2
+    %3:gpr32common = ANDWrr %0, %1
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWi %2, 7, 4, 1, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+name:            ands_from_and_ri_ccmp_wr
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1, $w2
+    ; CHECK-LABEL: name: ands_from_and_ri_ccmp_wr
+    ; CHECK: liveins: $w0, $w1, $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
+    ; CHECK-NEXT: [[ANDWri:%[0-9]+]]:gpr32common = ANDWri [[COPY]], 7
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ANDWri]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWr [[COPY1]], [[COPY2]], 0, 0, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDWri]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32 = COPY $w0
+    %1:gpr32 = COPY $w1
+    %2:gpr32 = COPY $w2
+    %3:gpr32common = ANDWri %0, 7
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWr %1, %2, 0, 0, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+name:            bics_from_bic_rr_ccmn_xi
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $x0, $x1, $x2
+    ; CHECK-LABEL: name: bics_from_bic_rr_ccmn_xi
+    ; CHECK: liveins: $x0, $x1, $x2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64 = COPY $x2
+    ; CHECK-NEXT: [[BICXrr:%[0-9]+]]:gpr64common = BICXrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[BICXrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMNXi [[COPY2]], 5, 8, 0, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCXr:%[0-9]+]]:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[BICXrr]]
+    ; CHECK-NEXT: $x0 = COPY [[ADDXrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %0:gpr64 = COPY $x0
+    %1:gpr64 = COPY $x1
+    %2:gpr64 = COPY $x2
+    %3:gpr64common = BICXrr %0, %1
+    dead %4:gpr64 = SUBSXri %3, 0, 0, implicit-def $nzcv
+    CCMNXi %2, 5, 8, 0, implicit-def $nzcv, implicit $nzcv
+    %5:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
+    %6:gpr64 = ADDXrr %5, %3
+    $x0 = COPY %6
+    RET_ReallyLR implicit $x0
+...
+---
+name:            adds_from_add_rr_ccmp_xr
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $x0, $x1, $x2
+    ; CHECK-LABEL: name: adds_from_add_rr_ccmp_xr
+    ; CHECK: liveins: $x0, $x1, $x2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64 = COPY $x2
+    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64common = ADDXrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[ADDXrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPXr [[COPY2]], [[COPY]], 4, 1, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCXr:%[0-9]+]]:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDXrr1:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[ADDXrr]]
+    ; CHECK-NEXT: $x0 = COPY [[ADDXrr1]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %0:gpr64 = COPY $x0
+    %1:gpr64 = COPY $x1
+    %2:gpr64 = COPY $x2
+    %3:gpr64common = ADDXrr %0, %1
+    dead %4:gpr64 = SUBSXri %3, 0, 0, implicit-def $nzcv
+    CCMPXr %2, %0, 4, 1, implicit-def $nzcv, implicit $nzcv
+    %5:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
+    %6:gpr64 = ADDXrr %5, %3
+    $x0 = COPY %6
+    RET_ReallyLR implicit $x0
+...
+---
+name:            subs_from_sub_ri_fccmp
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $s0, $s1
+    ; CHECK-LABEL: name: subs_from_sub_ri_fccmp
+    ; CHECK: liveins: $w0, $s0, $s1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32common = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:fpr32 = COPY $s0
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:fpr32 = COPY $s1
+    ; CHECK-NEXT: [[SUBWri:%[0-9]+]]:gpr32common = SUBWri [[COPY]], 1, 0
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[SUBWri]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: FCCMPSrr [[COPY1]], [[COPY2]], 0, 1, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[SUBWri]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32common = COPY $w0
+    %1:fpr32 = COPY $s0
+    %2:fpr32 = COPY $s1
+    %3:gpr32common = SUBWri %0, 1, 0
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    FCCMPSrr %1, %2, 0, 1, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+# GE reads N and V; V from ADDS is only usable because the add is nsw.
+name:            adds_from_add_nsw_ccmp_ge
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1, $w2
+    ; CHECK-LABEL: name: adds_from_add_nsw_ccmp_ge
+    ; CHECK: liveins: $w0, $w1, $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32common = nsw ADDWrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ADDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 8, 10, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr1:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ADDWrr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr1]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32 = COPY $w0
+    %1:gpr32 = COPY $w1
+    %2:gpr32 = COPY $w2
+    %3:gpr32common = nsw ADDWrr %0, %1
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWi %2, 7, 8, 10, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+# HS reads C, which ANDS does not compute the way a compare with zero does.
+name:            keep_cmp_when_ccmp_reads_carry
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1, $w2
+    ; CHECK-LABEL: name: keep_cmp_when_ccmp_reads_carry
+    ; CHECK: liveins: $w0, $w1, $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
+    ; CHECK-NEXT: [[ANDWrr:%[0-9]+]]:gpr32common = ANDWrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ANDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 4, 2, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDWrr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32 = COPY $w0
+    %1:gpr32 = COPY $w1
+    %2:gpr32 = COPY $w2
+    %3:gpr32common = ANDWrr %0, %1
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWi %2, 7, 4, 2, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+# GE reads V, and without nsw the add may overflow.
+name:            keep_cmp_when_ccmp_reads_overflow
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1, $w2
+    ; CHECK-LABEL: name: keep_cmp_when_ccmp_reads_overflow
+    ; CHECK: liveins: $w0, $w1, $w2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32common = ADDWrr [[COPY]], [[COPY1]]
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ADDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 8, 10, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr1:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ADDWrr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr1]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32 = COPY $w0
+    %1:gpr32 = COPY $w1
+    %2:gpr32 = COPY $w2
+    %3:gpr32common = ADDWrr %0, %1
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWi %2, 7, 8, 10, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...
+---
+# The compare of a CSINC result with zero goes away and the conditional
+# compare tests the CSINC's flags directly with the inverted condition.
+name:            remove_cmp_after_csinc_ccmp
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $w0, $w1
+    ; CHECK-LABEL: name: remove_cmp_after_csinc_ccmp
+    ; CHECK: liveins: $w0, $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32common = COPY $w0
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
+    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[COPY]], 3, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32common = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: dead [[SUBSWri1:%[0-9]+]]:gpr32 = SUBSWri [[CSINCWr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY1]], 7, 4, 0, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: [[CSINCWr1:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr1]], [[CSINCWr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    %0:gpr32common = COPY $w0
+    %1:gpr32 = COPY $w1
+    dead %2:gpr32 = SUBSWri %0, 3, 0, implicit-def $nzcv
+    %3:gpr32common = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    dead %4:gpr32 = SUBSWri %3, 0, 0, implicit-def $nzcv
+    CCMPWi %1, 7, 4, 0, implicit-def $nzcv, implicit $nzcv
+    %5:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    %6:gpr32 = ADDWrr %5, %3
+    $w0 = COPY %6
+    RET_ReallyLR implicit $w0
+...

>From c2c6cc68dcc65a78262c16b6f0aa59a21ac89c98 Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Mon, 7 Sep 2026 17:44:39 -0700
Subject: [PATCH 2/2] [AArch64] Fold cmp #0 into its definition when the flags
 feed a CCMP

The peephole that replaces `add/sub/and/bic Rd, ...; cmp Rd, #0` with the
flag-setting form of the defining instruction, and the one that removes
`cmp #0` after a `csinc`, analyze the users of the compare's flags with
examineCFlagsUse. That analysis only knew branches and selects, so a
compare whose flags feed a conditional compare was always left alone.

This is the normal shape of the first compare of a ccmp chain. The
conditional compare pass replaces a CBZ/CBNZ head with `cmp Rn, #0`, and
instruction selection emits the same compare for the ADD/SUB leaf of a
conjunction. Both leave sequences such as

  and  w9, w8, #3
  cmp  w9, #0
  ccmp w8, #0, #4, ne

where the `and` could set the flags itself.

A conditional compare reads NZCV through its condition code operand
exactly like a branch or a select, and the NZCV it defines afterwards is
a new value that ends the live range of the compared flags. Teach the
flag-use analysis to find the condition code of CCMP, CCMN and FCCMP so
the existing safety checks (only N and Z may be used, V only with nsw or
after AND/BIC) apply to them unchanged. The condition optimizer keeps
using the narrower branch-or-select helper.

Scanning librustc_driver and Firefox's XUL found this sequence 489 and
408 times respectively.

Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01RN4gpjgX8QWXRuQoBzYace
---
 llvm/lib/Target/AArch64/AArch64InstrInfo.cpp  | 39 +++++++++++++++++--
 .../AArch64/ccmp-cmp-zero-flag-setting.ll     | 24 ++++--------
 .../AArch64/peephole-cmp-zero-ccmp-user.mir   | 37 +++++++-----------
 3 files changed, 59 insertions(+), 41 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 522b263df5de1..d19787e6d097a 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -2236,12 +2236,45 @@ int AArch64InstrInfo::findCondCodeUseOperandIdxForBranchOrSelect(
   }
 }
 
+/// \returns The condition code operand index for \p Instr if it is a branch,
+/// a select or a conditional compare, and -1 otherwise.
+///
+/// A conditional compare reads NZCV through its condition code operand just
+/// like a branch or a select does, so its use of the flags is analyzed the
+/// same way. It then defines a new NZCV value, which ends the range in which
+/// the flags of the compare being optimized are live.
+static int findCondCodeUseOperandIdx(const MachineInstr &Instr) {
+  switch (Instr.getOpcode()) {
+  default:
+    return AArch64InstrInfo::findCondCodeUseOperandIdxForBranchOrSelect(Instr);
+
+  case AArch64::CCMPWi:
+  case AArch64::CCMPXi:
+  case AArch64::CCMPWr:
+  case AArch64::CCMPXr:
+  case AArch64::CCMNWi:
+  case AArch64::CCMNXi:
+  case AArch64::CCMNWr:
+  case AArch64::CCMNXr:
+  case AArch64::FCCMPHrr:
+  case AArch64::FCCMPSrr:
+  case AArch64::FCCMPDrr:
+  case AArch64::FCCMPEHrr:
+  case AArch64::FCCMPESrr:
+  case AArch64::FCCMPEDrr: {
+    // $Rn, $Rm or $imm, $nzcv, $cond, implicit-def $nzcv, implicit $nzcv
+    int Idx = Instr.findRegisterUseOperandIdx(AArch64::NZCV, /*TRI=*/nullptr);
+    assert(Idx >= 2);
+    return Idx - 2;
+  }
+  }
+}
+
 /// Find a condition code used by the instruction.
 /// Returns AArch64CC::Invalid if either the instruction does not use condition
 /// codes or we don't optimize CmpInstr in the presence of such instructions.
 static AArch64CC::CondCode findCondCodeUsedByInstr(const MachineInstr &Instr) {
-  int CCIdx =
-      AArch64InstrInfo::findCondCodeUseOperandIdxForBranchOrSelect(Instr);
+  int CCIdx = findCondCodeUseOperandIdx(Instr);
   return CCIdx >= 0 ? static_cast<AArch64CC::CondCode>(
                           Instr.getOperand(CCIdx).getImm())
                     : AArch64CC::Invalid;
@@ -2542,7 +2575,7 @@ bool AArch64InstrInfo::removeCmpToZeroOrOne(
   if (IsInvertCC) {
     // Invert condition codes in CmpInstr CC users
     for (MachineInstr *CCUseInstr : CCUseInstrs) {
-      int Idx = findCondCodeUseOperandIdxForBranchOrSelect(*CCUseInstr);
+      int Idx = findCondCodeUseOperandIdx(*CCUseInstr);
       assert(Idx >= 0 && "Unexpected instruction using CC.");
       MachineOperand &CCOperand = CCUseInstr->getOperand(Idx);
       AArch64CC::CondCode CCUse = AArch64CC::getInvertedCondCode(
diff --git a/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll b/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
index 955309e5c1f82..80ac71522eb30 100644
--- a/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
+++ b/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
@@ -17,8 +17,7 @@ define void @and_live(i32 %x, i32 %y, i32 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    and w0, w0, w1
-; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ands w0, w0, w1
 ; CHECK-NEXT:    ccmp w2, #7, #4, ne
 ; CHECK-NEXT:    b.ne .LBB0_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -53,8 +52,7 @@ define void @and_dead(i32 %x, i32 %y, i32 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    and w8, w0, w1
-; CHECK-NEXT:    cmp w8, #0
+; CHECK-NEXT:    tst w0, w1
 ; CHECK-NEXT:    ccmp w2, #7, #4, eq
 ; CHECK-NEXT:    b.ne .LBB1_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -90,8 +88,7 @@ define void @and_imm(i32 %x, i32 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    and w0, w0, #0xff
-; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    ands w0, w0, #0xff
 ; CHECK-NEXT:    ccmp w1, #7, #4, ne
 ; CHECK-NEXT:    b.ne .LBB2_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -126,8 +123,7 @@ define void @bic(i32 %x, i32 %y, i32 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    bic w0, w0, w1
-; CHECK-NEXT:    cmp w0, #0
+; CHECK-NEXT:    bics w0, w0, w1
 ; CHECK-NEXT:    ccmp w2, #7, #4, ne
 ; CHECK-NEXT:    b.ne .LBB3_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -163,8 +159,7 @@ define void @add(i64 %x, i64 %y, i64 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    add x0, x0, x1
-; CHECK-NEXT:    cmp x0, #0
+; CHECK-NEXT:    adds x0, x0, x1
 ; CHECK-NEXT:    ccmp x2, #7, #4, ne
 ; CHECK-NEXT:    b.ne .LBB4_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -199,8 +194,7 @@ define void @sub_imm(i64 %x, i64 %z) {
 ; CHECK-NEXT:    str x30, [sp, #-16]! // 8-byte Folded Spill
 ; CHECK-NEXT:    .cfi_def_cfa_offset 16
 ; CHECK-NEXT:    .cfi_offset w30, -16
-; CHECK-NEXT:    sub x0, x0, #1
-; CHECK-NEXT:    cmp x0, #0
+; CHECK-NEXT:    subs x0, x0, #1
 ; CHECK-NEXT:    ccmp x1, #7, #4, eq
 ; CHECK-NEXT:    b.ne .LBB5_2
 ; CHECK-NEXT:  // %bb.1: // %then
@@ -271,8 +265,7 @@ else:
 define i64 @add_or_leaf(i64 %x, i64 %y, i64 %z) {
 ; CHECK-LABEL: add_or_leaf:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    add x8, x0, x1
-; CHECK-NEXT:    cmp x8, #0
+; CHECK-NEXT:    adds x8, x0, x1
 ; CHECK-NEXT:    ccmp x2, #7, #4, ne
 ; CHECK-NEXT:    csel x0, x8, x2, eq
 ; CHECK-NEXT:    ret
@@ -287,8 +280,7 @@ define i64 @add_or_leaf(i64 %x, i64 %y, i64 %z) {
 define i64 @sub_and_leaf(i64 %x, i64 %y, i64 %z) {
 ; CHECK-LABEL: sub_and_leaf:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    sub x8, x0, x1
-; CHECK-NEXT:    cmp x8, #0
+; CHECK-NEXT:    subs x8, x0, x1
 ; CHECK-NEXT:    ccmp x2, #7, #2, ne
 ; CHECK-NEXT:    csel x0, x8, x2, lo
 ; CHECK-NEXT:    ret
diff --git a/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir b/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
index b2b3771cda987..5d3b4716892cc 100644
--- a/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
+++ b/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
@@ -20,11 +20,10 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
-    ; CHECK-NEXT: [[ANDWrr:%[0-9]+]]:gpr32common = ANDWrr [[COPY]], [[COPY1]]
-    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ANDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[ANDSWrr:%[0-9]+]]:gpr32common = ANDSWrr [[COPY]], [[COPY1]], implicit-def $nzcv
     ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 4, 1, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDWrr]]
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDSWrr]]
     ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     %0:gpr32 = COPY $w0
@@ -50,11 +49,10 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
-    ; CHECK-NEXT: [[ANDWri:%[0-9]+]]:gpr32common = ANDWri [[COPY]], 7
-    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ANDWri]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[ANDSWri:%[0-9]+]]:gpr32common = ANDSWri [[COPY]], 7, implicit-def $nzcv
     ; CHECK-NEXT: CCMPWr [[COPY1]], [[COPY2]], 0, 0, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDWri]]
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ANDSWri]]
     ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     %0:gpr32 = COPY $w0
@@ -80,11 +78,10 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x1
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64 = COPY $x2
-    ; CHECK-NEXT: [[BICXrr:%[0-9]+]]:gpr64common = BICXrr [[COPY]], [[COPY1]]
-    ; CHECK-NEXT: dead [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[BICXrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[BICSXrr:%[0-9]+]]:gpr64common = BICSXrr [[COPY]], [[COPY1]], implicit-def $nzcv
     ; CHECK-NEXT: CCMNXi [[COPY2]], 5, 8, 0, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCXr:%[0-9]+]]:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[BICXrr]]
+    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[BICSXrr]]
     ; CHECK-NEXT: $x0 = COPY [[ADDXrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     %0:gpr64 = COPY $x0
@@ -110,12 +107,11 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x1
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64 = COPY $x2
-    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64common = ADDXrr [[COPY]], [[COPY1]]
-    ; CHECK-NEXT: dead [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[ADDXrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[ADDSXrr:%[0-9]+]]:gpr64common = ADDSXrr [[COPY]], [[COPY1]], implicit-def $nzcv
     ; CHECK-NEXT: CCMPXr [[COPY2]], [[COPY]], 4, 1, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCXr:%[0-9]+]]:gpr64 = CSINCXr $xzr, $xzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDXrr1:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[ADDXrr]]
-    ; CHECK-NEXT: $x0 = COPY [[ADDXrr1]]
+    ; CHECK-NEXT: [[ADDXrr:%[0-9]+]]:gpr64 = ADDXrr [[CSINCXr]], [[ADDSXrr]]
+    ; CHECK-NEXT: $x0 = COPY [[ADDXrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     %0:gpr64 = COPY $x0
     %1:gpr64 = COPY $x1
@@ -140,11 +136,10 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32common = COPY $w0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:fpr32 = COPY $s0
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:fpr32 = COPY $s1
-    ; CHECK-NEXT: [[SUBWri:%[0-9]+]]:gpr32common = SUBWri [[COPY]], 1, 0
-    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[SUBWri]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[SUBSWri:%[0-9]+]]:gpr32common = SUBSWri [[COPY]], 1, 0, implicit-def $nzcv
     ; CHECK-NEXT: FCCMPSrr [[COPY1]], [[COPY2]], 0, 1, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[SUBWri]]
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[SUBSWri]]
     ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     %0:gpr32common = COPY $w0
@@ -171,12 +166,11 @@ body:             |
     ; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32 = COPY $w0
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
     ; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr32 = COPY $w2
-    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32common = nsw ADDWrr [[COPY]], [[COPY1]]
-    ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[ADDWrr]], 0, 0, implicit-def $nzcv
+    ; CHECK-NEXT: [[ADDSWrr:%[0-9]+]]:gpr32common = nsw ADDSWrr [[COPY]], [[COPY1]], implicit-def $nzcv
     ; CHECK-NEXT: CCMPWi [[COPY2]], 7, 8, 10, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
-    ; CHECK-NEXT: [[ADDWrr1:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ADDWrr]]
-    ; CHECK-NEXT: $w0 = COPY [[ADDWrr1]]
+    ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr]], [[ADDSWrr]]
+    ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     %0:gpr32 = COPY $w0
     %1:gpr32 = COPY $w1
@@ -266,8 +260,7 @@ body:             |
     ; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $w1
     ; CHECK-NEXT: dead [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[COPY]], 3, 0, implicit-def $nzcv
     ; CHECK-NEXT: [[CSINCWr:%[0-9]+]]:gpr32common = CSINCWr $wzr, $wzr, 1, implicit $nzcv
-    ; CHECK-NEXT: dead [[SUBSWri1:%[0-9]+]]:gpr32 = SUBSWri [[CSINCWr]], 0, 0, implicit-def $nzcv
-    ; CHECK-NEXT: CCMPWi [[COPY1]], 7, 4, 0, implicit-def $nzcv, implicit $nzcv
+    ; CHECK-NEXT: CCMPWi [[COPY1]], 7, 4, 1, implicit-def $nzcv, implicit $nzcv
     ; CHECK-NEXT: [[CSINCWr1:%[0-9]+]]:gpr32 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: [[ADDWrr:%[0-9]+]]:gpr32 = ADDWrr [[CSINCWr1]], [[CSINCWr]]
     ; CHECK-NEXT: $w0 = COPY [[ADDWrr]]



More information about the llvm-commits mailing list