[llvm] [AArch64] Fold constants into their users after tail duplication (PR #231048)

Andrew Gaul via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 11 17:39:37 PDT 2026


https://github.com/gaul updated https://github.com/llvm/llvm-project/pull/231048

>From 023347c21b8455fd80e1aef507f91149b28c3bed Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Mon, 12 Oct 2026 06:43:15 +0900
Subject: [PATCH 1/2] [AArch64] Precommit tests for folding constants into
 their users (NFC)

Add tests showing the code block placement's tail duplication leaves
behind when a block is copied into the predecessor that materialized its
PHI constant: a MOV of a one-instruction constant immediately followed by
a compare, add, logical operation or copy that reads it.

Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01RN4gpjgX8QWXRuQoBzYace
---
 .../AArch64/fold-movimm-after-tail-dup.ll     |  88 ++++
 .../redundant-cond-branch-fold-imm.mir        | 435 ++++++++++++++++++
 2 files changed, 523 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
 create mode 100644 llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir

diff --git a/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll b/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
new file mode 100644
index 0000000000000..49f3e6b51a5e0
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
@@ -0,0 +1,88 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=aarch64 -O3 < %s | FileCheck %s
+
+; A constant reaching a compare through a PHI. Tail duplication copies the
+; compare into the predecessor that supplies the constant, and the copy should
+; compare against the immediate rather than a register holding it.
+define i64 @cmp_phi_zero(ptr %p, i64 %idx, ptr %q) {
+; CHECK-LABEL: cmp_phi_zero:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    ldr x8, [x0]
+; CHECK-NEXT:    cbz x8, .LBB0_3
+; CHECK-NEXT:  // %bb.1: // %tagged
+; CHECK-NEXT:    tst x8, #0x7
+; CHECK-NEXT:    b.eq .LBB0_5
+; CHECK-NEXT:  // %bb.2: // %outofline
+; CHECK-NEXT:    ldr x9, [x2]
+; CHECK-NEXT:    cmp x1, x9
+; CHECK-NEXT:    b.lo .LBB0_4
+; CHECK-NEXT:    b .LBB0_6
+; CHECK-NEXT:  .LBB0_3:
+; CHECK-NEXT:    cmp x1, xzr
+; CHECK-NEXT:    b.hs .LBB0_6
+; CHECK-NEXT:  .LBB0_4: // %load
+; CHECK-NEXT:    and x8, x8, #0xfffffffffffffff8
+; CHECK-NEXT:    ldr x0, [x8, x1, lsl #3]
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB0_5:
+; CHECK-NEXT:    mov w9, #1 // =0x1
+; CHECK-NEXT:    cmp x1, x9
+; CHECK-NEXT:    b.lo .LBB0_4
+; CHECK-NEXT:  .LBB0_6:
+; CHECK-NEXT:    mov x0, #-1 // =0xffffffffffffffff
+; CHECK-NEXT:    ret
+entry:
+  %info = load i64, ptr %p
+  %isnull = icmp eq i64 %info, 0
+  br i1 %isnull, label %join, label %tagged
+
+tagged:
+  %tag = and i64 %info, 7
+  %istag = icmp eq i64 %tag, 0
+  br i1 %istag, label %join, label %outofline
+
+outofline:
+  %cnt = load i64, ptr %q
+  br label %join
+
+join:
+  %n = phi i64 [ 0, %entry ], [ 1, %tagged ], [ %cnt, %outofline ]
+  %inbounds = icmp ult i64 %idx, %n
+  br i1 %inbounds, label %load, label %reject
+
+load:
+  %base = and i64 %info, -8
+  %bp = inttoptr i64 %base to ptr
+  %ptr = getelementptr i64, ptr %bp, i64 %idx
+  %v = load i64, ptr %ptr
+  ret i64 %v
+
+reject:
+  ret i64 -1
+}
+
+; The same with an OR of a single-bit flag.
+define i32 @or_phi_flag(ptr %p, i32 %x, i1 %c) {
+; CHECK-LABEL: or_phi_flag:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    tbz w2, #0, .LBB1_2
+; CHECK-NEXT:  // %bb.1: // %a
+; CHECK-NEXT:    ldr w8, [x0]
+; CHECK-NEXT:    orr w0, w1, w8
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB1_2:
+; CHECK-NEXT:    mov w8, #65536 // =0x10000
+; CHECK-NEXT:    orr w0, w1, w8
+; CHECK-NEXT:    ret
+entry:
+  br i1 %c, label %a, label %join
+
+a:
+  %f = load i32, ptr %p
+  br label %join
+
+join:
+  %flag = phi i32 [ 65536, %entry ], [ %f, %a ]
+  %r = or i32 %x, %flag
+  ret i32 %r
+}
diff --git a/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir b/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir
new file mode 100644
index 0000000000000..aff2876a87ee6
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir
@@ -0,0 +1,435 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
+# RUN: llc -mtriple=aarch64 -run-pass=aarch64-redundantcondbranch -verify-machineinstrs -o - %s | FileCheck %s
+
+# A constant materialized by a single MOV is folded into the first add/sub or
+# logical instruction reading it, and the MOV is removed once it is dead.
+---
+name: cmp_killed
+body: |
+  ; CHECK-LABEL: name: cmp_killed
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x40000000), %bb.2(0x40000000)
+  ; CHECK-NEXT:   liveins: $x1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   renamable $w9 = MOVZWi 1, 0, implicit-def $x9
+  ; CHECK-NEXT:   $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+  ; CHECK-NEXT:   Bcc 3, %bb.1, implicit $nzcv
+  ; CHECK-NEXT:   B %bb.2
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   liveins: $x1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   $x0 = COPY $x1
+  ; CHECK-NEXT:   RET_ReallyLR implicit $x0
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   $x0 = MOVi64imm -1
+  ; CHECK-NEXT:   RET_ReallyLR implicit $x0
+  bb.0:
+    liveins: $x1
+    renamable $w9 = MOVZWi 1, 0, implicit-def $x9
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    Bcc 3, %bb.1, implicit $nzcv
+    B %bb.2
+  bb.1:
+    liveins: $x1
+    $x0 = COPY $x1
+    RET_ReallyLR implicit $x0
+  bb.2:
+    $x0 = MOVi64imm -1
+    RET_ReallyLR implicit $x0
+...
+---
+name: cmp_mov_still_live
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: cmp_mov_still_live
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $x0 = CSELXr renamable $x1, renamable $x9, 3, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 5, 0
+    $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    renamable $x0 = CSELXr renamable $x1, renamable $x9, 3, implicit $nzcv
+    RET_ReallyLR implicit $x0
+...
+---
+name: cmp_dead_after_user_via_liveness
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: cmp_dead_after_user_via_liveness
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $x9 = CSINCXr $xzr, $xzr, 2, implicit $nzcv
+    ; CHECK-NEXT: $x0 = COPY $x9
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 5, 0
+    $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    renamable $x9 = CSINCXr $xzr, $xzr, 2, implicit $nzcv
+    $x0 = COPY $x9
+    RET_ReallyLR implicit $x0
+...
+---
+name: user_overwrites_constant_register
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: user_overwrites_constant_register
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
+    ; CHECK-NEXT: renamable $x9 = ADDXrs renamable $x1, renamable $x9, 0
+    ; CHECK-NEXT: $x0 = COPY $x9
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 5, 0
+    renamable $x9 = ADDXrs renamable $x1, renamable $x9, 0
+    $x0 = COPY $x9
+    RET_ReallyLR implicit $x0
+...
+---
+name: cmp_x_user_of_w_mov_negated
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: cmp_x_user_of_w_mov_negated
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $w9 = MOVNWi 0, 0, implicit-def $x9
+    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $w9 = MOVNWi 0, 0, implicit-def $x9
+    $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: cmp_w_user_of_x_mov
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: cmp_w_user_of_x_mov
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVNXi 0, 0
+    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVNXi 0, 0
+    $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: cmp_w_user_of_x_mov_negated_shifted
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: cmp_w_user_of_x_mov_negated_shifted
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVNXi 65535, 0
+    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVNXi 65535, 0
+    $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: add_shifted_imm_commuted
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: add_shifted_imm_commuted
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 16
+    ; CHECK-NEXT: renamable $x0 = ADDXrs killed renamable $x9, killed renamable $x1, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 1, 16
+    renamable $x0 = ADDXrs killed renamable $x9, killed renamable $x1, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: add_shifted_imm
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: add_shifted_imm
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 12
+    ; CHECK-NEXT: renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 1, 12
+    renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: add_shifted_register_operand
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: add_shifted_register_operand
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 0
+    ; CHECK-NEXT: renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 2
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 1, 0
+    renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 2
+    RET_ReallyLR implicit $x0
+...
+---
+name: sub_constant_is_rn
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: sub_constant_is_rn
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 64, 0
+    ; CHECK-NEXT: renamable $x0 = SUBXrs killed renamable $x9, killed renamable $x1, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 64, 0
+    renamable $x0 = SUBXrs killed renamable $x9, killed renamable $x1, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: add_zr_rn
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: add_zr_rn
+    ; CHECK: renamable $x9 = MOVZXi 64, 0
+    ; CHECK-NEXT: renamable $x0 = ADDXrs $xzr, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 64, 0
+    renamable $x0 = ADDXrs $xzr, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: orr_from_zr_constant
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: orr_from_zr_constant
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $w9 = ORRWri $wzr, 1543
+    ; CHECK-NEXT: renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $w9 = ORRWri $wzr, 1543
+    renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
+    RET_ReallyLR implicit $w0
+...
+---
+name: copy_of_constant
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: copy_of_constant
+    ; CHECK: renamable $w9 = MOVZWi 1, 0, implicit-def $x9
+    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $w9 = MOVZWi 1, 0, implicit-def $x9
+    renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: copy_of_negative_constant_narrowed
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: copy_of_negative_constant_narrowed
+    ; CHECK: renamable $x9 = MOVNXi 2, 0
+    ; CHECK-NEXT: renamable $w0 = ORRWrs $wzr, killed renamable $w9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVNXi 2, 0
+    renamable $w0 = ORRWrs $wzr, killed renamable $w9, 0
+    RET_ReallyLR implicit $w0
+...
+---
+name: copy_of_logical_imm_constant
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: copy_of_logical_imm_constant
+    ; CHECK: renamable $x9 = ORRXri $xzr, 56
+    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = ORRXri $xzr, 56
+    renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: copy_of_negative_constant_widened
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: copy_of_negative_constant_widened
+    ; CHECK: renamable $w9 = MOVNWi 1, 0, implicit-def $x9
+    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $w9 = MOVNWi 1, 0, implicit-def $x9
+    renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: copy_of_constant_needing_two_instructions
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: copy_of_constant_needing_two_instructions
+    ; CHECK: renamable $w9 = MOVNWi 2, 0, implicit-def $x9
+    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $w9 = MOVNWi 2, 0, implicit-def $x9
+    renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: eor_logical_imm
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: eor_logical_imm
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 16
+    ; CHECK-NEXT: renamable $x0 = EORXrs killed renamable $x1, killed renamable $x9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    renamable $x9 = MOVZXi 1, 16
+    renamable $x0 = EORXrs killed renamable $x1, killed renamable $x9, 0
+    RET_ReallyLR implicit $x0
+...
+---
+name: ands_logical_imm_zr_dst
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: ands_logical_imm_zr_dst
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $w9 = MOVZWi 255, 0, implicit-def $x9
+    ; CHECK-NEXT: $wzr = ANDSWrs killed renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $w9 = MOVZWi 255, 0, implicit-def $x9
+    $wzr = ANDSWrs killed renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: and_zero_not_logical_imm
+body: |
+  bb.0:
+    liveins: $w1
+    ; CHECK-LABEL: name: and_zero_not_logical_imm
+    ; CHECK: liveins: $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $w9 = MOVZWi 0, 0, implicit-def $x9
+    ; CHECK-NEXT: renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $w9 = MOVZWi 0, 0, implicit-def $x9
+    renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
+    RET_ReallyLR implicit $w0
+...
+---
+name: movk_chain_untouched
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: movk_chain_untouched
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 0
+    ; CHECK-NEXT: renamable $x9 = MOVKXi renamable $x9, 1, 16
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVZXi 1, 0
+    renamable $x9 = MOVKXi renamable $x9, 1, 16
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: user_after_unrelated_instructions
+body: |
+  bb.0:
+    liveins: $x1, $x2
+    ; CHECK-LABEL: name: user_after_unrelated_instructions
+    ; CHECK: liveins: $x1, $x2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 7, 0
+    ; CHECK-NEXT: renamable $x8 = LDRXui renamable $x2, 0
+    ; CHECK-NEXT: STRXui renamable $x8, renamable $x2, 1
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVZXi 7, 0
+    renamable $x8 = LDRXui renamable $x2, 0
+    STRXui renamable $x8, renamable $x2, 1
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: reg_redefined_before_user
+body: |
+  bb.0:
+    liveins: $x1, $x2
+    ; CHECK-LABEL: name: reg_redefined_before_user
+    ; CHECK: liveins: $x1, $x2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 7, 0
+    ; CHECK-NEXT: renamable $x9 = LDRXui renamable $x2, 0
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVZXi 7, 0
+    renamable $x9 = LDRXui renamable $x2, 0
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+name: first_use_not_foldable
+body: |
+  bb.0:
+    liveins: $x1, $x2
+    ; CHECK-LABEL: name: first_use_not_foldable
+    ; CHECK: liveins: $x1, $x2
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi 7, 0
+    ; CHECK-NEXT: STRXui renamable $x9, renamable $x2, 0
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVZXi 7, 0
+    STRXui renamable $x9, renamable $x2, 0
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...
+---
+# A MOVZ of a symbol (large code model, TLS, block addresses) is not a constant.
+name: movz_symbol
+body: |
+  bb.0:
+    liveins: $x1
+    ; CHECK-LABEL: name: movz_symbol
+    ; CHECK: liveins: $x1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: renamable $x9 = MOVZXi target-flags(aarch64-g0, aarch64-nc) <mcsymbol sym>, 0
+    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    ; CHECK-NEXT: RET_ReallyLR implicit $w0
+    renamable $x9 = MOVZXi target-flags(aarch64-g0, aarch64-nc) <mcsymbol sym>, 0
+    $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
+    RET_ReallyLR implicit $w0
+...

>From d0c21bb1002fb9045b5bb43e228cffdaa49c1b10 Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Mon, 12 Oct 2026 06:43:15 +0900
Subject: [PATCH 2/2] [AArch64] Fold constants into their users after tail
 duplication

Block placement duplicates small blocks into their predecessors, and a
PHI operand that was a constant then meets its user in the block that
materialized it:

  mov w9, #1
  cmp x1, x9
  b.lo .LBB0_4

ISel would have taken the constant as an immediate had it been an
operand, but by this point selection is long over and nothing folds it
back. The redundant conditional branch pass already runs after block
placement for the zero form of the same effect, CBZ of ZR, so extend it.
When a single-instruction constant (MOVZ, MOVN or ORR from ZR) is next
read by an add/sub or logical instruction that could take it as an
immediate, rewrite that instruction to the immediate form, negating the
constant under the opposite add/sub when that is what encodes, which
preserves the flags as cmn does for cmp. A plain copy of the constant is
rematerialized into its destination instead. The MOV is deleted once
nothing reads it.

Measured with llc -O3 over 1,073 translation units of LLVM and clang
(8.56M instructions of AArch64 code), this removes 3,421 instructions,
0.04%. 541 translation units shrink and none grow. About 1,800 compares
and 700 adds, subtracts and logical operations take the immediate, and
2,566 copies of a constant become the constant.

Found via armlint.

Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01RN4gpjgX8QWXRuQoBzYace
---
 .../AArch64RedundantCondBranchPass.cpp        | 255 +++++++++++++++++-
 .../AArch64/fold-movimm-after-tail-dup.ll     |   6 +-
 .../redundant-cond-branch-fold-imm.mir        |  50 ++--
 .../CodeGen/AArch64/typepromotion-cost.ll     |  10 +-
 4 files changed, 277 insertions(+), 44 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64RedundantCondBranchPass.cpp b/llvm/lib/Target/AArch64/AArch64RedundantCondBranchPass.cpp
index 9023881590888..4c26282e68597 100644
--- a/llvm/lib/Target/AArch64/AArch64RedundantCondBranchPass.cpp
+++ b/llvm/lib/Target/AArch64/AArch64RedundantCondBranchPass.cpp
@@ -11,10 +11,17 @@
 // simple pass looks at the terminators to a block, removing the redundant
 // instructions where necessary.
 //
+// The same tail duplication exposes other constant phi operands: the block that
+// supplied the constant now holds the duplicated user right after the MOV that
+// materializes it. Where the user could have taken the constant as an
+// immediate, rewrite it to the immediate form and drop the MOV once it is dead.
+//
 //===----------------------------------------------------------------------===//
 
 #include "AArch64.h"
+#include "AArch64ExpandImm.h"
 #include "AArch64InstrInfo.h"
+#include "MCTargetDesc/AArch64AddressingModes.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/TargetInstrInfo.h"
 
@@ -47,12 +54,258 @@ INITIALIZE_PASS(AArch64RedundantCondBranchLegacy, "aarch64-redundantcondbranch",
                 "AArch64 Redundant Conditional Branch Elimination pass", false,
                 false)
 
+// If \p MI materializes a constant in one instruction, return the value it
+// leaves in the 64-bit register.
+static std::optional<uint64_t> getSingleInstrConstant(const MachineInstr &MI) {
+  switch (MI.getOpcode()) {
+  default:
+    return std::nullopt;
+  case AArch64::MOVZWi:
+  case AArch64::MOVZXi:
+  case AArch64::MOVNWi:
+  case AArch64::MOVNXi: {
+    // The source may be a symbol with a :abs_gN: specifier (large code model,
+    // TLS, block addresses) rather than a constant.
+    if (!MI.getOperand(1).isImm())
+      return std::nullopt;
+    uint64_t Imm = (uint64_t)MI.getOperand(1).getImm()
+                   << MI.getOperand(2).getImm();
+    switch (MI.getOpcode()) {
+    case AArch64::MOVZWi:
+    case AArch64::MOVZXi:
+      return Imm;
+    case AArch64::MOVNWi:
+      return ~Imm & 0xffffffffULL;
+    default:
+      return ~Imm;
+    }
+  }
+  case AArch64::ORRWri:
+    if (MI.getOperand(1).getReg() != AArch64::WZR)
+      return std::nullopt;
+    return AArch64_AM::decodeLogicalImmediate(MI.getOperand(2).getImm(), 32);
+  case AArch64::ORRXri:
+    if (MI.getOperand(1).getReg() != AArch64::XZR)
+      return std::nullopt;
+    return AArch64_AM::decodeLogicalImmediate(MI.getOperand(2).getImm(), 64);
+  }
+}
+
+// Rewrite \p MI, a copy of the constant \p Value or an add/sub or logical
+// instruction taking it in register \p Reg, to materialize or take the constant
+// as an immediate. Returns false if it is not such an instruction or the
+// constant does not encode.
+static bool foldImmediateIntoUser(MachineInstr &MI, Register Reg,
+                                  uint64_t Value, const TargetInstrInfo &TII,
+                                  const TargetRegisterInfo &TRI) {
+  unsigned RIOpc, NegRIOpc = 0;
+  bool Is64Bit = false, IsCommutable = true, IsLogical = false,
+       SetsFlags = false;
+  switch (MI.getOpcode()) {
+  default:
+    return false;
+  case AArch64::ADDXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::ADDWrs:
+    RIOpc = Is64Bit ? AArch64::ADDXri : AArch64::ADDWri;
+    NegRIOpc = Is64Bit ? AArch64::SUBXri : AArch64::SUBWri;
+    break;
+  case AArch64::SUBXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::SUBWrs:
+    RIOpc = Is64Bit ? AArch64::SUBXri : AArch64::SUBWri;
+    NegRIOpc = Is64Bit ? AArch64::ADDXri : AArch64::ADDWri;
+    IsCommutable = false;
+    break;
+  case AArch64::ADDSXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::ADDSWrs:
+    RIOpc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
+    NegRIOpc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
+    SetsFlags = true;
+    break;
+  case AArch64::SUBSXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::SUBSWrs:
+    RIOpc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
+    NegRIOpc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
+    IsCommutable = false;
+    SetsFlags = true;
+    break;
+  case AArch64::ANDXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::ANDWrs:
+    RIOpc = Is64Bit ? AArch64::ANDXri : AArch64::ANDWri;
+    IsLogical = true;
+    break;
+  case AArch64::ANDSXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::ANDSWrs:
+    RIOpc = Is64Bit ? AArch64::ANDSXri : AArch64::ANDSWri;
+    IsLogical = SetsFlags = true;
+    break;
+  case AArch64::EORXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::EORWrs:
+    RIOpc = Is64Bit ? AArch64::EORXri : AArch64::EORWri;
+    IsLogical = true;
+    break;
+  case AArch64::ORRXrs:
+    Is64Bit = true;
+    [[fallthrough]];
+  case AArch64::ORRWrs:
+    RIOpc = Is64Bit ? AArch64::ORRXri : AArch64::ORRWri;
+    IsLogical = true;
+    break;
+  }
+  // Only the unshifted register operand holds the constant itself.
+  if (MI.getOperand(3).getImm() != 0)
+    return false;
+
+  unsigned ImmIdx;
+  if (TRI.isSuperOrSubRegisterEq(Reg, MI.getOperand(2).getReg()))
+    ImmIdx = 2;
+  else if (IsCommutable &&
+           TRI.isSuperOrSubRegisterEq(Reg, MI.getOperand(1).getReg()))
+    ImmIdx = 1;
+  else
+    return false;
+  const MachineOperand &Dst = MI.getOperand(0);
+  const MachineOperand &Src = MI.getOperand(ImmIdx == 2 ? 1 : 2);
+
+  // Register 31 is SP in Rn of the add/sub immediate forms and in Rd of the
+  // forms that do not set flags, where the register forms read ZR.
+  bool DstIsZR = Dst.getReg() == AArch64::WZR || Dst.getReg() == AArch64::XZR;
+  bool SrcIsZR = Src.getReg() == AArch64::WZR || Src.getReg() == AArch64::XZR;
+  if ((DstIsZR && !SetsFlags) || (SrcIsZR && !IsLogical))
+    return false;
+
+  unsigned RegSize = Is64Bit ? 64 : 32;
+  uint64_t Mask = Is64Bit ? ~0ULL : 0xffffffffULL;
+  uint64_t Imm = Value & Mask;
+
+  // A copy of the constant, ORR Rd, ZR, Rm, materializes it in Rd instead when
+  // that takes one instruction.
+  if (SrcIsZR && (MI.getOpcode() == AArch64::ORRWrs ||
+                  MI.getOpcode() == AArch64::ORRXrs)) {
+    SmallVector<AArch64_IMM::ImmInsnModel, 4> Insn;
+    AArch64_IMM::expandMOVImm(Imm, RegSize, Insn);
+    if (Insn.size() != 1)
+      return false;
+    MachineInstrBuilder MIB =
+        BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII.get(Insn[0].Opcode))
+            .add(Dst);
+    if (Insn[0].Opcode == AArch64::ORRWri || Insn[0].Opcode == AArch64::ORRXri)
+      MIB.addReg(Is64Bit ? AArch64::XZR : AArch64::WZR).addImm(*Insn[0].Op2);
+    else
+      MIB.addImm(*Insn[0].Op1).addImm(*Insn[0].Op2);
+    LLVM_DEBUG(dbgs() << "Rematerializing: " << MI
+                      << "            into: " << *MIB);
+    MI.eraseFromParent();
+    return true;
+  }
+
+  unsigned NewOpc = RIOpc;
+  uint64_t Enc;
+  unsigned Shift = 0;
+  if (IsLogical) {
+    if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
+      return false;
+    Enc = AArch64_AM::encodeLogicalImmediate(Imm, RegSize);
+  } else {
+    // A constant that does not encode may do so negated under the opposite
+    // operation, which leaves the flags unchanged, as cmn does for cmp.
+    if (AArch64_AM::isLegalArithImmed(Imm)) {
+      Enc = Imm;
+    } else if (AArch64_AM::isLegalArithImmed(-Imm & Mask)) {
+      Enc = -Imm & Mask;
+      NewOpc = NegRIOpc;
+    } else {
+      return false;
+    }
+    Shift = AArch64_AM::getArithImmedShift(Enc);
+    Enc >>= Shift;
+  }
+
+  MachineInstrBuilder MIB =
+      BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII.get(NewOpc))
+          .add(Dst)
+          .add(Src)
+          .addImm(Enc);
+  if (!IsLogical)
+    MIB.addImm(Shift);
+  if (SetsFlags) {
+    MachineOperand *OldNZCV = MI.findRegisterDefOperand(AArch64::NZCV, &TRI);
+    MachineOperand *NewNZCV = MIB->findRegisterDefOperand(AArch64::NZCV, &TRI);
+    if (OldNZCV && NewNZCV)
+      NewNZCV->setIsDead(OldNZCV->isDead());
+  }
+  LLVM_DEBUG(dbgs() << "Folding immediate: " << MI << "        into: " << *MIB);
+  MI.eraseFromParent();
+  return true;
+}
+
+// Fold the constants that single MOVs in \p MBB materialize into their first
+// user when it can take them as an immediate.
+static bool foldImmediates(MachineBasicBlock &MBB, const TargetInstrInfo &TII,
+                           const TargetRegisterInfo &TRI) {
+  bool Changed = false;
+  for (MachineBasicBlock::iterator It = MBB.begin(); It != MBB.end();) {
+    MachineInstr &MovMI = *It++;
+    std::optional<uint64_t> Value = getSingleInstrConstant(MovMI);
+    if (!Value || MovMI.isBundled())
+      continue;
+    Register Reg = MovMI.getOperand(0).getReg();
+    // Look at the 64-bit register so that both widths of the constant and of
+    // its user are covered.
+    if (AArch64::GPR32RegClass.contains(Reg))
+      Reg = TRI.getMatchingSuperReg(Reg, AArch64::sub_32,
+                                    &AArch64::GPR64RegClass);
+    if (!Reg)
+      continue;
+
+    // Find the first instruction touching the register.
+    MachineBasicBlock::iterator User = std::next(MovMI.getIterator());
+    for (; User != MBB.end(); ++User)
+      if (User->readsRegister(Reg, &TRI) || User->modifiesRegister(Reg, &TRI))
+        break;
+    if (User == MBB.end() || !User->readsRegister(Reg, &TRI) ||
+        User->isBundled())
+      continue;
+
+    // The MOV is dead once its register is killed or overwritten by the user,
+    // or is not read again afterwards.
+    bool Dead =
+        User->killsRegister(Reg, &TRI) || User->modifiesRegister(Reg, &TRI);
+    MachineBasicBlock::iterator Next = std::next(User);
+    if (!foldImmediateIntoUser(*User, Reg, *Value, TII, TRI))
+      continue;
+    Changed = true;
+    // The user, which may have been the next instruction, is replaced.
+    It = std::next(MovMI.getIterator());
+    if (Dead || MBB.computeRegisterLiveness(&TRI, Reg, Next, 16) ==
+                    MachineBasicBlock::LQR_Dead)
+      MovMI.eraseFromParent();
+  }
+  return Changed;
+}
+
 static bool runAArch64RedundantCondBranch(MachineFunction &MF) {
   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
+  const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
 
   bool Changed = false;
-  for (MachineBasicBlock &MBB : MF)
+  for (MachineBasicBlock &MBB : MF) {
+    Changed |= foldImmediates(MBB, TII, TRI);
     Changed |= optimizeTerminators(&MBB, TII);
+  }
   return Changed;
 }
 
diff --git a/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll b/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
index 49f3e6b51a5e0..1223c952e6260 100644
--- a/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
+++ b/llvm/test/CodeGen/AArch64/fold-movimm-after-tail-dup.ll
@@ -25,8 +25,7 @@ define i64 @cmp_phi_zero(ptr %p, i64 %idx, ptr %q) {
 ; CHECK-NEXT:    ldr x0, [x8, x1, lsl #3]
 ; CHECK-NEXT:    ret
 ; CHECK-NEXT:  .LBB0_5:
-; CHECK-NEXT:    mov w9, #1 // =0x1
-; CHECK-NEXT:    cmp x1, x9
+; CHECK-NEXT:    cmp x1, #1
 ; CHECK-NEXT:    b.lo .LBB0_4
 ; CHECK-NEXT:  .LBB0_6:
 ; CHECK-NEXT:    mov x0, #-1 // =0xffffffffffffffff
@@ -71,8 +70,7 @@ define i32 @or_phi_flag(ptr %p, i32 %x, i1 %c) {
 ; CHECK-NEXT:    orr w0, w1, w8
 ; CHECK-NEXT:    ret
 ; CHECK-NEXT:  .LBB1_2:
-; CHECK-NEXT:    mov w8, #65536 // =0x10000
-; CHECK-NEXT:    orr w0, w1, w8
+; CHECK-NEXT:    orr w0, w1, #0x10000
 ; CHECK-NEXT:    ret
 entry:
   br i1 %c, label %a, label %join
diff --git a/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir b/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir
index aff2876a87ee6..2ce557182517a 100644
--- a/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir
+++ b/llvm/test/CodeGen/AArch64/redundant-cond-branch-fold-imm.mir
@@ -11,8 +11,7 @@ body: |
   ; CHECK-NEXT:   successors: %bb.1(0x40000000), %bb.2(0x40000000)
   ; CHECK-NEXT:   liveins: $x1
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   renamable $w9 = MOVZWi 1, 0, implicit-def $x9
-  ; CHECK-NEXT:   $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+  ; CHECK-NEXT:   $xzr = SUBSXri renamable $x1, 1, 0, implicit-def $nzcv
   ; CHECK-NEXT:   Bcc 3, %bb.1, implicit $nzcv
   ; CHECK-NEXT:   B %bb.2
   ; CHECK-NEXT: {{  $}}
@@ -48,7 +47,7 @@ body: |
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
     ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
-    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $xzr = SUBSXri renamable $x1, 5, 0, implicit-def $nzcv
     ; CHECK-NEXT: renamable $x0 = CSELXr renamable $x1, renamable $x9, 3, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = MOVZXi 5, 0
@@ -64,8 +63,7 @@ body: |
     ; CHECK-LABEL: name: cmp_dead_after_user_via_liveness
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
-    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $xzr = SUBSXri renamable $x1, 5, 0, implicit-def $nzcv
     ; CHECK-NEXT: renamable $x9 = CSINCXr $xzr, $xzr, 2, implicit $nzcv
     ; CHECK-NEXT: $x0 = COPY $x9
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
@@ -83,8 +81,7 @@ body: |
     ; CHECK-LABEL: name: user_overwrites_constant_register
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 5, 0
-    ; CHECK-NEXT: renamable $x9 = ADDXrs renamable $x1, renamable $x9, 0
+    ; CHECK-NEXT: renamable $x9 = ADDXri renamable $x1, 5, 0
     ; CHECK-NEXT: $x0 = COPY $x9
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = MOVZXi 5, 0
@@ -100,8 +97,7 @@ body: |
     ; CHECK-LABEL: name: cmp_x_user_of_w_mov_negated
     ; CHECK: liveins: $w1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $w9 = MOVNWi 0, 0, implicit-def $x9
-    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $wzr = ADDSWri renamable $w1, 1, 0, implicit-def $nzcv
     ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $w9 = MOVNWi 0, 0, implicit-def $x9
@@ -117,8 +113,7 @@ body: |
     ; CHECK-LABEL: name: cmp_w_user_of_x_mov
     ; CHECK: liveins: $w1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVNXi 0, 0
-    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $wzr = ADDSWri renamable $w1, 1, 0, implicit-def $nzcv
     ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $x9 = MOVNXi 0, 0
@@ -134,8 +129,7 @@ body: |
     ; CHECK-LABEL: name: cmp_w_user_of_x_mov_negated_shifted
     ; CHECK: liveins: $w1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVNXi 65535, 0
-    ; CHECK-NEXT: $wzr = SUBSWrs renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $wzr = ADDSWri renamable $w1, 16, 12, implicit-def $nzcv
     ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $x9 = MOVNXi 65535, 0
@@ -151,8 +145,7 @@ body: |
     ; CHECK-LABEL: name: add_shifted_imm_commuted
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 16
-    ; CHECK-NEXT: renamable $x0 = ADDXrs killed renamable $x9, killed renamable $x1, 0
+    ; CHECK-NEXT: renamable $x0 = ADDXri killed renamable $x1, 16, 12
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = MOVZXi 1, 16
     renamable $x0 = ADDXrs killed renamable $x9, killed renamable $x1, 0
@@ -166,8 +159,7 @@ body: |
     ; CHECK-LABEL: name: add_shifted_imm
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 12
-    ; CHECK-NEXT: renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 0
+    ; CHECK-NEXT: renamable $x0 = ADDXri killed renamable $x1, 1, 12
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = MOVZXi 1, 12
     renamable $x0 = ADDXrs killed renamable $x1, killed renamable $x9, 0
@@ -223,8 +215,7 @@ body: |
     ; CHECK-LABEL: name: orr_from_zr_constant
     ; CHECK: liveins: $w1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $w9 = ORRWri $wzr, 1543
-    ; CHECK-NEXT: renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
+    ; CHECK-NEXT: renamable $w0 = ANDWri killed renamable $w1, 1543
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $w9 = ORRWri $wzr, 1543
     renamable $w0 = ANDWrs killed renamable $w1, killed renamable $w9, 0
@@ -235,8 +226,7 @@ name: copy_of_constant
 body: |
   bb.0:
     ; CHECK-LABEL: name: copy_of_constant
-    ; CHECK: renamable $w9 = MOVZWi 1, 0, implicit-def $x9
-    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK: renamable $x0 = MOVZXi 1, 0
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $w9 = MOVZWi 1, 0, implicit-def $x9
     renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
@@ -247,8 +237,7 @@ name: copy_of_negative_constant_narrowed
 body: |
   bb.0:
     ; CHECK-LABEL: name: copy_of_negative_constant_narrowed
-    ; CHECK: renamable $x9 = MOVNXi 2, 0
-    ; CHECK-NEXT: renamable $w0 = ORRWrs $wzr, killed renamable $w9, 0
+    ; CHECK: renamable $w0 = MOVNWi 2, 0
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $x9 = MOVNXi 2, 0
     renamable $w0 = ORRWrs $wzr, killed renamable $w9, 0
@@ -259,8 +248,7 @@ name: copy_of_logical_imm_constant
 body: |
   bb.0:
     ; CHECK-LABEL: name: copy_of_logical_imm_constant
-    ; CHECK: renamable $x9 = ORRXri $xzr, 56
-    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK: renamable $x0 = ORRXri $xzr, 56
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = ORRXri $xzr, 56
     renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
@@ -271,8 +259,7 @@ name: copy_of_negative_constant_widened
 body: |
   bb.0:
     ; CHECK-LABEL: name: copy_of_negative_constant_widened
-    ; CHECK: renamable $w9 = MOVNWi 1, 0, implicit-def $x9
-    ; CHECK-NEXT: renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
+    ; CHECK: renamable $x0 = ORRXri $xzr, 8158
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $w9 = MOVNWi 1, 0, implicit-def $x9
     renamable $x0 = ORRXrs $xzr, killed renamable $x9, 0
@@ -298,8 +285,7 @@ body: |
     ; CHECK-LABEL: name: eor_logical_imm
     ; CHECK: liveins: $x1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 1, 16
-    ; CHECK-NEXT: renamable $x0 = EORXrs killed renamable $x1, killed renamable $x9, 0
+    ; CHECK-NEXT: renamable $x0 = EORXri killed renamable $x1, 7168
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     renamable $x9 = MOVZXi 1, 16
     renamable $x0 = EORXrs killed renamable $x1, killed renamable $x9, 0
@@ -313,8 +299,7 @@ body: |
     ; CHECK-LABEL: name: ands_logical_imm_zr_dst
     ; CHECK: liveins: $w1
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $w9 = MOVZWi 255, 0, implicit-def $x9
-    ; CHECK-NEXT: $wzr = ANDSWrs killed renamable $w1, killed renamable $w9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $wzr = ANDSWri killed renamable $w1, 7, implicit-def $nzcv
     ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $w9 = MOVZWi 255, 0, implicit-def $x9
@@ -364,10 +349,9 @@ body: |
     ; CHECK-LABEL: name: user_after_unrelated_instructions
     ; CHECK: liveins: $x1, $x2
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: renamable $x9 = MOVZXi 7, 0
     ; CHECK-NEXT: renamable $x8 = LDRXui renamable $x2, 0
     ; CHECK-NEXT: STRXui renamable $x8, renamable $x2, 1
-    ; CHECK-NEXT: $xzr = SUBSXrs renamable $x1, killed renamable $x9, 0, implicit-def $nzcv
+    ; CHECK-NEXT: $xzr = SUBSXri renamable $x1, 7, 0, implicit-def $nzcv
     ; CHECK-NEXT: renamable $w0 = CSINCWr $wzr, $wzr, 1, implicit $nzcv
     ; CHECK-NEXT: RET_ReallyLR implicit $w0
     renamable $x9 = MOVZXi 7, 0
diff --git a/llvm/test/CodeGen/AArch64/typepromotion-cost.ll b/llvm/test/CodeGen/AArch64/typepromotion-cost.ll
index 3aed4cb671c02..736affcf46c20 100644
--- a/llvm/test/CodeGen/AArch64/typepromotion-cost.ll
+++ b/llvm/test/CodeGen/AArch64/typepromotion-cost.ll
@@ -11,10 +11,10 @@ define i32 @needless_promotion(ptr nocapture noundef readonly %S, i64 noundef %r
 ; CHECK-O2-NEXT:  // %bb.1: // %lor.rhs
 ; CHECK-O2-NEXT:    cbz x1, .LBB0_5
 ; CHECK-O2-NEXT:  // %bb.2:
-; CHECK-O2-NEXT:    mov w9, #2
+; CHECK-O2-NEXT:    mov w9, #2 // =0x2
 ; CHECK-O2-NEXT:    b .LBB0_4
 ; CHECK-O2-NEXT:  .LBB0_3:
-; CHECK-O2-NEXT:    mov w9, #1
+; CHECK-O2-NEXT:    mov w9, #1 // =0x1
 ; CHECK-O2-NEXT:  .LBB0_4: // %lor.end.sink.split
 ; CHECK-O2-NEXT:    cmp w8, w9
 ; CHECK-O2-NEXT:    cset w0, eq
@@ -30,13 +30,11 @@ define i32 @needless_promotion(ptr nocapture noundef readonly %S, i64 noundef %r
 ; CHECK-O3-NEXT:  // %bb.1: // %lor.rhs
 ; CHECK-O3-NEXT:    cbz x1, .LBB0_4
 ; CHECK-O3-NEXT:  // %bb.2:
-; CHECK-O3-NEXT:    mov w9, #2
-; CHECK-O3-NEXT:    cmp w8, w9
+; CHECK-O3-NEXT:    cmp w8, #2
 ; CHECK-O3-NEXT:    cset w0, eq
 ; CHECK-O3-NEXT:    ret
 ; CHECK-O3-NEXT:  .LBB0_3:
-; CHECK-O3-NEXT:    mov w9, #1
-; CHECK-O3-NEXT:    cmp w8, w9
+; CHECK-O3-NEXT:    cmp w8, #1
 ; CHECK-O3-NEXT:    cset w0, eq
 ; CHECK-O3-NEXT:    ret
 ; CHECK-O3-NEXT:  .LBB0_4:



More information about the llvm-commits mailing list