[llvm] [AArch64] Lower an equality branch against a logical immediate to EOR + CBZ/CBNZ (PR #223123)
Andrew Gaul via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 12 00:04:18 PDT 2026
https://github.com/gaul updated https://github.com/llvm/llvm-project/pull/223123
>From 81290e61cb0e79cf2c2dfa73e7608989c00e4435 Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Fri, 11 Sep 2026 21:52:07 -0700
Subject: [PATCH 1/2] [AArch64] Add tests for equality branches against logical
immediates (NFC)
Branches on x == C and x != C where C is not a legal CMP/CMN immediate:
constants that are logical immediates (INT64_MIN, the i32 sign bit,
0xffffff, 0x7fffffff), the same constant reused across a call, a loop
whose constant is hoisted into a callee-saved register, and the shapes
that must keep their current lowering -- a CMP or CMN immediate, a
constant that is neither kind of immediate, an ordered condition, a
value-producing compare, two tests merged into a conditional-compare
chain, a constant shared with a select, and a function under speculative
load hardening. The checks record the current codegen, which materializes
the constant for every compare.
Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01KZMjy58X6Z2sH8fd4SSRi4
---
.../CodeGen/AArch64/branch-eq-logical-imm.ll | 512 ++++++++++++++++++
1 file changed, 512 insertions(+)
create mode 100644 llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
diff --git a/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll b/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
new file mode 100644
index 0000000000000..72accb4b6dacb
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
@@ -0,0 +1,512 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=aarch64-linux-gnu < %s | FileCheck %s
+
+; Equality branches against constants that no CMP/CMN immediate encodes.
+; Those that are logical immediates can be tested as (x ^ C) == 0.
+
+declare void @g()
+declare void @h()
+
+define void @br_eq_int64_min(i64 %x) {
+; CHECK-LABEL: br_eq_int64_min:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.ne .LBB0_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB0_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, -9223372036854775808
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+define void @br_ne_int64_min(i64 %x) {
+; CHECK-LABEL: br_ne_int64_min:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.eq .LBB1_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB1_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp ne i64 %x, -9223372036854775808
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+define void @br_eq_i32_signbit(i32 %x) {
+; CHECK-LABEL: br_eq_i32_signbit:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov w8, #-2147483648 // =0x80000000
+; CHECK-NEXT: cmp w0, w8
+; CHECK-NEXT: b.ne .LBB2_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB2_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i32 %x, -2147483648
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+define void @br_ne_i32_ffffff(i32 %x) {
+; CHECK-LABEL: br_ne_i32_ffffff:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov w8, #16777215 // =0xffffff
+; CHECK-NEXT: cmp w0, w8
+; CHECK-NEXT: b.eq .LBB3_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB3_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp ne i32 %x, 16777215
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+define void @br_eq_i64_int32_max(i64 %x) {
+; CHECK-LABEL: br_eq_i64_int32_max:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov w8, #2147483647 // =0x7fffffff
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.ne .LBB4_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB4_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, 2147483647
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+; The constant is used twice, across a call.
+define void @br_two_uses(i64 %x, i64 %y) {
+; CHECK-LABEL: br_two_uses:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
+; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 32
+; CHECK-NEXT: .cfi_offset w19, -8
+; CHECK-NEXT: .cfi_offset w20, -16
+; CHECK-NEXT: .cfi_offset w30, -32
+; CHECK-NEXT: mov x20, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: mov x19, x1
+; CHECK-NEXT: cmp x0, x20
+; CHECK-NEXT: b.ne .LBB5_2
+; CHECK-NEXT: // %bb.1: // %callg
+; CHECK-NEXT: bl g
+; CHECK-NEXT: .LBB5_2: // %second
+; CHECK-NEXT: cmp x19, x20
+; CHECK-NEXT: b.ne .LBB5_4
+; CHECK-NEXT: // %bb.3: // %callh
+; CHECK-NEXT: bl h
+; CHECK-NEXT: .LBB5_4: // %exit
+; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
+; CHECK-NEXT: ldr x30, [sp], #32 // 8-byte Folded Reload
+; CHECK-NEXT: ret
+entry:
+ %cx = icmp eq i64 %x, -9223372036854775808
+ br i1 %cx, label %callg, label %second
+callg:
+ call void @g()
+ br label %second
+second:
+ %cy = icmp eq i64 %y, -9223372036854775808
+ br i1 %cy, label %callh, label %exit
+callh:
+ call void @h()
+ br label %exit
+exit:
+ ret void
+}
+
+; A CMP immediate keeps the compare.
+define void @br_eq_imm12(i64 %x) {
+; CHECK-LABEL: br_eq_imm12:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: cmp x0, #100
+; CHECK-NEXT: b.ne .LBB6_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB6_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, 100
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+; A negative constant whose magnitude encodes keeps CMN.
+define void @br_eq_neg5(i64 %x) {
+; CHECK-LABEL: br_eq_neg5:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: cmn x0, #5
+; CHECK-NEXT: b.ne .LBB7_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB7_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, -5
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+; Not a logical immediate: the constant is materialized.
+define void @br_eq_0x1234(i64 %x) {
+; CHECK-LABEL: br_eq_0x1234:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov w8, #4660 // =0x1234
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.ne .LBB8_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB8_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, 4660
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+; An ordered condition needs the subtraction.
+define void @br_sgt_int32_max(i64 %x) {
+; CHECK-LABEL: br_sgt_int32_max:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: mov w8, #-2147483648 // =0x80000000
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.lt .LBB9_2
+; CHECK-NEXT: // %bb.1: // %then
+; 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: bl g
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: .LBB9_2: // %exit
+; CHECK-NEXT: ret
+entry:
+ %c = icmp sgt i64 %x, 2147483647
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
+
+; Value-producing compares.
+define i1 @setcc_int64_min(i64 %x) {
+; CHECK-LABEL: setcc_int64_min:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: cset w0, eq
+; CHECK-NEXT: ret
+ %c = icmp eq i64 %x, -9223372036854775808
+ ret i1 %c
+}
+
+define i64 @select_int64_min(i64 %x, i64 %a, i64 %b) {
+; CHECK-LABEL: select_int64_min:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: csel x0, x1, x2, eq
+; CHECK-NEXT: ret
+ %c = icmp eq i64 %x, -9223372036854775808
+ %r = select i1 %c, i64 %a, i64 %b
+ ret i64 %r
+}
+
+; Three tests lowered from a switch and merged by the conditional-compare
+; pass: the compares' flags feed a CCMP chain, not a branch.
+define i64 @br_ccmp_chain(i64 %x0, i64 %n) {
+; CHECK-LABEL: br_ccmp_chain:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: stp x30, x25, [sp, #-64]! // 16-byte Folded Spill
+; CHECK-NEXT: stp x24, x23, [sp, #16] // 16-byte Folded Spill
+; CHECK-NEXT: stp x22, x21, [sp, #32] // 16-byte Folded Spill
+; CHECK-NEXT: stp x20, x19, [sp, #48] // 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 64
+; CHECK-NEXT: .cfi_offset w19, -8
+; CHECK-NEXT: .cfi_offset w20, -16
+; CHECK-NEXT: .cfi_offset w21, -24
+; CHECK-NEXT: .cfi_offset w22, -32
+; CHECK-NEXT: .cfi_offset w23, -40
+; CHECK-NEXT: .cfi_offset w24, -48
+; CHECK-NEXT: .cfi_offset w25, -56
+; CHECK-NEXT: .cfi_offset w30, -64
+; CHECK-NEXT: cmp x1, #1
+; CHECK-NEXT: mov x19, x0
+; CHECK-NEXT: b.lt .LBB12_5
+; CHECK-NEXT: // %bb.1: // %loop.preheader
+; CHECK-NEXT: mov x21, #32557 // =0x7f2d
+; CHECK-NEXT: mov x22, #33103 // =0x814f
+; CHECK-NEXT: mov x20, x1
+; CHECK-NEXT: movk x21, #19605, lsl #16
+; CHECK-NEXT: movk x22, #63335, lsl #16
+; CHECK-NEXT: mov w23, #65535 // =0xffff
+; CHECK-NEXT: movk x21, #62509, lsl #32
+; CHECK-NEXT: movk x22, #31614, lsl #32
+; CHECK-NEXT: mov x24, #9223372036854775807 // =0x7fffffffffffffff
+; CHECK-NEXT: movk x21, #22609, lsl #48
+; CHECK-NEXT: movk x22, #5125, lsl #48
+; CHECK-NEXT: mov w25, #-1 // =0xffffffff
+; CHECK-NEXT: b .LBB12_3
+; CHECK-NEXT: .LBB12_2: // %latch
+; CHECK-NEXT: // in Loop: Header=BB12_3 Depth=1
+; CHECK-NEXT: subs x20, x20, #1
+; CHECK-NEXT: b.eq .LBB12_5
+; CHECK-NEXT: .LBB12_3: // %loop
+; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: madd x19, x19, x21, x22
+; CHECK-NEXT: cmp x19, x23
+; CHECK-NEXT: ccmp x19, x24, #4, ne
+; CHECK-NEXT: ccmp x19, x25, #4, ne
+; CHECK-NEXT: b.ne .LBB12_2
+; CHECK-NEXT: // %bb.4: // %call
+; CHECK-NEXT: // in Loop: Header=BB12_3 Depth=1
+; CHECK-NEXT: bl g
+; CHECK-NEXT: b .LBB12_2
+; CHECK-NEXT: .LBB12_5: // %exit
+; CHECK-NEXT: mov x0, x19
+; CHECK-NEXT: ldp x20, x19, [sp, #48] // 16-byte Folded Reload
+; CHECK-NEXT: ldp x22, x21, [sp, #32] // 16-byte Folded Reload
+; CHECK-NEXT: ldp x24, x23, [sp, #16] // 16-byte Folded Reload
+; CHECK-NEXT: ldp x30, x25, [sp], #64 // 16-byte Folded Reload
+; CHECK-NEXT: ret
+entry:
+ %cmp0 = icmp sgt i64 %n, 0
+ br i1 %cmp0, label %loop, label %exit
+loop:
+ %i = phi i64 [ %inc, %latch ], [ 0, %entry ]
+ %x = phi i64 [ %next, %latch ], [ %x0, %entry ]
+ %mul = mul nsw i64 %x, 6364136223846793005
+ %next = add nsw i64 %mul, 1442695040888963407
+ switch i64 %next, label %latch [
+ i64 9223372036854775807, label %call
+ i64 4294967295, label %call
+ i64 65535, label %call
+ ]
+call:
+ call void @g()
+ br label %latch
+latch:
+ %inc = add nuw nsw i64 %i, 1
+ %done = icmp eq i64 %inc, %n
+ br i1 %done, label %exit, label %loop
+exit:
+ %r = phi i64 [ %x0, %entry ], [ %next, %latch ]
+ ret i64 %r
+}
+
+; The constant is shared with a select.
+define i64 @br_shared_with_select(i64 %x, i64 %y, i64 %a, i64 %b) {
+; CHECK-LABEL: br_shared_with_select:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: str x30, [sp, #-48]! // 8-byte Folded Spill
+; CHECK-NEXT: stp x22, x21, [sp, #16] // 16-byte Folded Spill
+; CHECK-NEXT: stp x20, x19, [sp, #32] // 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 48
+; CHECK-NEXT: .cfi_offset w19, -8
+; CHECK-NEXT: .cfi_offset w20, -16
+; CHECK-NEXT: .cfi_offset w21, -24
+; CHECK-NEXT: .cfi_offset w22, -32
+; CHECK-NEXT: .cfi_offset w30, -48
+; CHECK-NEXT: mov x22, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: mov x19, x3
+; CHECK-NEXT: mov x20, x2
+; CHECK-NEXT: cmp x0, x22
+; CHECK-NEXT: mov x21, x1
+; CHECK-NEXT: b.ne .LBB13_2
+; CHECK-NEXT: // %bb.1: // %then
+; CHECK-NEXT: bl g
+; CHECK-NEXT: .LBB13_2: // %exit
+; CHECK-NEXT: cmp x21, x22
+; CHECK-NEXT: ldp x22, x21, [sp, #16] // 16-byte Folded Reload
+; CHECK-NEXT: csel x0, x20, x19, eq
+; CHECK-NEXT: ldp x20, x19, [sp, #32] // 16-byte Folded Reload
+; CHECK-NEXT: ldr x30, [sp], #48 // 8-byte Folded Reload
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, -9223372036854775808
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ %d = icmp eq i64 %y, -9223372036854775808
+ %r = select i1 %d, i64 %a, i64 %b
+ ret i64 %r
+}
+
+; A loop containing a call: the constant is hoisted into a callee-saved
+; register.
+define void @br_loop(ptr %p, i64 %n) {
+; CHECK-LABEL: br_loop:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: cmp x1, #1
+; CHECK-NEXT: b.lt .LBB14_6
+; CHECK-NEXT: // %bb.1: // %loop.preheader
+; CHECK-NEXT: stp x30, x21, [sp, #-32]! // 16-byte Folded Spill
+; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 32
+; CHECK-NEXT: .cfi_offset w19, -8
+; CHECK-NEXT: .cfi_offset w20, -16
+; CHECK-NEXT: .cfi_offset w21, -24
+; CHECK-NEXT: .cfi_offset w30, -32
+; CHECK-NEXT: mov x19, x1
+; CHECK-NEXT: mov x20, x0
+; CHECK-NEXT: mov x21, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: b .LBB14_3
+; CHECK-NEXT: .LBB14_2: // %latch
+; CHECK-NEXT: // in Loop: Header=BB14_3 Depth=1
+; CHECK-NEXT: subs x19, x19, #1
+; CHECK-NEXT: b.eq .LBB14_5
+; CHECK-NEXT: .LBB14_3: // %loop
+; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: ldr x8, [x20], #8
+; CHECK-NEXT: cmp x8, x21
+; CHECK-NEXT: b.ne .LBB14_2
+; CHECK-NEXT: // %bb.4: // %call
+; CHECK-NEXT: // in Loop: Header=BB14_3 Depth=1
+; CHECK-NEXT: bl g
+; CHECK-NEXT: b .LBB14_2
+; CHECK-NEXT: .LBB14_5:
+; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
+; CHECK-NEXT: ldp x30, x21, [sp], #32 // 16-byte Folded Reload
+; CHECK-NEXT: .LBB14_6: // %exit
+; CHECK-NEXT: ret
+entry:
+ %cmp0 = icmp sgt i64 %n, 0
+ br i1 %cmp0, label %loop, label %exit
+loop:
+ %i = phi i64 [ 0, %entry ], [ %inc, %latch ]
+ %addr = getelementptr inbounds i64, ptr %p, i64 %i
+ %v = load i64, ptr %addr
+ %c = icmp eq i64 %v, -9223372036854775808
+ br i1 %c, label %call, label %latch
+call:
+ call void @g()
+ br label %latch
+latch:
+ %inc = add nuw nsw i64 %i, 1
+ %done = icmp eq i64 %inc, %n
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+; Speculative load hardening needs a flag-setting branch.
+define void @br_eq_int64_min_slh(i64 %x) speculative_load_hardening {
+; CHECK-LABEL: br_eq_int64_min_slh:
+; CHECK: // %bb.0: // %entry
+; CHECK-NEXT: cmp sp, #0
+; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: csetm x16, ne
+; CHECK-NEXT: cmp x0, x8
+; CHECK-NEXT: b.ne .LBB15_3
+; CHECK-NEXT: // %bb.1:
+; CHECK-NEXT: csel x16, x16, xzr, eq
+; CHECK-NEXT: // %bb.2: // %then
+; 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: mov x0, sp
+; CHECK-NEXT: and x0, x0, x16
+; CHECK-NEXT: mov sp, x0
+; CHECK-NEXT: bl g
+; CHECK-NEXT: cmp sp, #0
+; CHECK-NEXT: csetm x16, ne
+; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; CHECK-NEXT: and x30, x30, x16
+; CHECK-NEXT: csdb
+; CHECK-NEXT: b .LBB15_4
+; CHECK-NEXT: .LBB15_3:
+; CHECK-NEXT: csel x16, x16, xzr, ne
+; CHECK-NEXT: .LBB15_4: // %exit
+; CHECK-NEXT: mov x0, sp
+; CHECK-NEXT: and x0, x0, x16
+; CHECK-NEXT: mov sp, x0
+; CHECK-NEXT: ret
+entry:
+ %c = icmp eq i64 %x, -9223372036854775808
+ br i1 %c, label %then, label %exit
+then:
+ call void @g()
+ br label %exit
+exit:
+ ret void
+}
>From 3f00bb5c7d374c128ea44fca092823c9c5479e88 Mon Sep 17 00:00:00 2001
From: Andrew Gaul <andrew at gaul.org>
Date: Fri, 11 Sep 2026 23:47:49 -0700
Subject: [PATCH 2/2] [AArch64] Fold a compare-and-branch against a logical
immediate into EOR + CBZ/CBNZ
A pre-RA peephole for x == C and x != C feeding a branch, when C is a
logical immediate but not a legal CMP/CMN immediate:
mov x8, #-9223372036854775808
cmp x0, x8
b.ne .LBB0_2
becomes
eor x8, x0, #0x8000000000000000
cbnz x8, .LBB0_2
The rewrite is made only when every use of the materialized constant is
such a compare-and-branch, so the materialization disappears altogether.
That is the whole benefit: one instruction fewer per site, and no
register carrying the constant. When the same constant is compared on
both sides of a call, or inside a loop, LLVM currently parks it in a
callee-saved register and pays the spill; with the constant folded into
the EOR the loop in the test drops its x21 save and the two-use function
drops x20. A compare that shares a hoisted constant with another consumer
-- a CSEL, a CCMP chain -- is left alone, since converting it would trade
a fused compare-and-branch for two issue slots while removing nothing.
Doing this in AArch64MIPeepholeOpt rather than in ISel keeps it clear of
the conditional-compare pass, which runs earlier: a compare that became
part of a CCMP chain no longer feeds a branch and is never a candidate.
Rewriting the same shape in LowerBR_CC instead turns each leaf of a
merged condition into EOR + CBZ before that pass runs, and the pass then
converts the CBZ back into a compare against zero while keeping the EOR,
so a three-way test grows from four instructions to seven and slows by
12% on Apple M4 Max. examineCFlagsUse supplies the flag-consumer check
and refuses flags live into a successor; functions under speculative
load hardening keep their flag-setting branches.
The shape is common. rustc places the niches of an enum that has two or
more dataless variants beside a Vec or String payload at isize::MAX + 1
upward, so every test for such a variant compares against
0x8000000000000000; Gecko's TimeDuration keeps INT64_MIN as a sentinel;
0x7fffffff and 0xffffff are frequent limits. Firefox 155's libxul.so
contains 13,019 branch compares of this form, 11,262 of them against
INT64_MIN.
Found via armlint.
Co-Authored-By: Claude Fable 5.1 <noreply at anthropic.com>
Claude-Session: https://claude.ai/code/session_01KZMjy58X6Z2sH8fd4SSRi4
---
.../Target/AArch64/AArch64MIPeepholeOpt.cpp | 151 ++++++++++++++++++
.../CodeGen/AArch64/branch-eq-logical-imm.ll | 67 ++++----
2 files changed, 181 insertions(+), 37 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp b/llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
index 554d5938cf2cd..66ba62dd1965d 100644
--- a/llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
+++ b/llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
@@ -143,6 +143,8 @@ class AArch64MIPeepholeOptImpl {
bool visitFMOVDr(MachineInstr &MI);
bool visitUBFMXri(MachineInstr &MI);
bool visitCopy(MachineInstr &MI);
+ bool isEqBranchOnReg(MachineInstr &Cmp, Register ConstReg, MachineInstr *&Br);
+ bool visitBcc(MachineInstr &MI);
};
struct AArch64MIPeepholeOptLegacy : public MachineFunctionPass {
@@ -957,6 +959,152 @@ bool AArch64MIPeepholeOptImpl::visitCopy(MachineInstr &MI) {
return true;
}
+// Whether Cmp is a register-form SUBS comparing a virtual register against
+// ConstReg, with a dead result, whose flags reach exactly one B.cond with
+// condition EQ or NE and nothing else (in particular no CCMP, and no flags
+// live into a successor). Returns that branch in Br.
+bool AArch64MIPeepholeOptImpl::isEqBranchOnReg(MachineInstr &Cmp,
+ Register ConstReg,
+ MachineInstr *&Br) {
+ unsigned Opc = Cmp.getOpcode();
+ if (Opc != AArch64::SUBSWrr && Opc != AArch64::SUBSXrr)
+ return false;
+ Register Dst = Cmp.getOperand(0).getReg();
+ if (Dst.isVirtual() ? !MRI->use_nodbg_empty(Dst)
+ : (Dst != AArch64::WZR && Dst != AArch64::XZR))
+ return false;
+ Register Rn = Cmp.getOperand(1).getReg();
+ Register Rm = Cmp.getOperand(2).getReg();
+ if ((Rn != ConstReg && Rm != ConstReg) || Rn == Rm)
+ return false;
+ Register X = Rn == ConstReg ? Rm : Rn;
+ if (!X.isVirtual())
+ return false;
+ SmallVector<MachineInstr *, 4> Users;
+ std::optional<UsedNZCV> Used = examineCFlagsUse(Cmp, Cmp, *TRI, &Users);
+ if (!Used || Users.size() != 1 || Users[0]->getOpcode() != AArch64::Bcc)
+ return false;
+ auto CC = static_cast<AArch64CC::CondCode>(Users[0]->getOperand(0).getImm());
+ if (CC != AArch64CC::EQ && CC != AArch64CC::NE)
+ return false;
+ Br = Users[0];
+ return true;
+}
+
+// A compare against a constant that is a logical immediate but no compare
+// immediate, feeding an equality branch:
+// %c = MOVi64imm C (or SUBREG_TO_REG of a MOVi32imm)
+// %d = SUBSXrr %x, %c, implicit-def $nzcv ; %d dead
+// Bcc EQ|NE, %bb
+// tests x == C, which is (x ^ C) == 0, and CBZ/CBNZ tests that directly:
+// %e = EORXri %x, C
+// CBZX|CBNZX %e, %bb
+// One instruction fewer, and no register carrying the constant. The rewrite
+// requires every use of the constant to be such a compare-and-branch, so the
+// materialization disappears altogether: converting a compare that shares a
+// hoisted constant with other consumers would trade a fused compare-and-branch
+// for two issue slots while removing nothing. Running after the conditional
+// compare pass, a compare that became part of a CCMP chain is never a
+// candidate, since its flags feed the CCMP rather than a branch.
+bool AArch64MIPeepholeOptImpl::visitBcc(MachineInstr &MI) {
+ // Speculative load hardening relies on flag-setting branches.
+ if (MI.getMF()->getFunction().hasFnAttribute(
+ Attribute::SpeculativeLoadHardening))
+ return false;
+ auto CC = static_cast<AArch64CC::CondCode>(MI.getOperand(0).getImm());
+ if (CC != AArch64CC::EQ && CC != AArch64CC::NE)
+ return false;
+
+ // The compare the branch reads: the nearest NZCV writer above it.
+ MachineBasicBlock &MBB = *MI.getParent();
+ MachineInstr *Cmp = nullptr;
+ for (MachineBasicBlock::iterator It = MI.getIterator(); It != MBB.begin();) {
+ --It;
+ if (It->modifiesRegister(AArch64::NZCV, TRI)) {
+ Cmp = &*It;
+ break;
+ }
+ }
+ if (!Cmp)
+ return false;
+ unsigned CmpOpc = Cmp->getOpcode();
+ if (CmpOpc != AArch64::SUBSWrr && CmpOpc != AArch64::SUBSXrr)
+ return false;
+ bool Is64 = CmpOpc == AArch64::SUBSXrr;
+
+ // One operand materializes the constant, directly or through a
+ // SUBREG_TO_REG of a 32-bit materialization.
+ Register ConstReg;
+ MachineInstr *MovMI = nullptr, *SubregMI = nullptr;
+ for (unsigned Idx : {1u, 2u}) {
+ Register R = Cmp->getOperand(Idx).getReg();
+ if (!R.isVirtual())
+ continue;
+ MachineInstr *Def = MRI->getUniqueVRegDef(R), *Sub = nullptr;
+ if (Def && Def->getOpcode() == TargetOpcode::SUBREG_TO_REG) {
+ Sub = Def;
+ Def = MRI->getUniqueVRegDef(Def->getOperand(1).getReg());
+ }
+ if (!Def || (Def->getOpcode() != AArch64::MOVi32imm &&
+ Def->getOpcode() != AArch64::MOVi64imm))
+ continue;
+ if ((Def->getOpcode() == AArch64::MOVi32imm) != (!Is64 || Sub != nullptr))
+ continue;
+ ConstReg = R;
+ MovMI = Def;
+ SubregMI = Sub;
+ break;
+ }
+ if (!MovMI)
+ return false;
+ unsigned Bits = Is64 ? 64 : 32;
+ uint64_t Imm = MovMI->getOperand(1).getImm();
+ if (MovMI->getOpcode() == AArch64::MOVi32imm)
+ Imm &= 0xffffffffu;
+ // A constant CMP or CMN encodes is not this fold's business, and the EOR
+ // needs a logical immediate.
+ if (AArch64_AM::isLegalCmpImmed(APInt(Bits, Imm)) ||
+ !AArch64_AM::isLogicalImmediate(Imm, Bits))
+ return false;
+
+ // Every use of the constant must be such a compare-and-branch, this one
+ // included, so that the materialization goes away.
+ Register MovReg = MovMI->getOperand(0).getReg();
+ if (SubregMI && !MRI->hasOneNonDBGUse(MovReg))
+ return false;
+ MachineInstr *Br = nullptr;
+ for (MachineInstr &Use : MRI->use_nodbg_instructions(ConstReg))
+ if (!isEqBranchOnReg(Use, ConstReg, Br))
+ return false;
+ if (!isEqBranchOnReg(*Cmp, ConstReg, Br) || Br != &MI)
+ return false;
+
+ Register X = Cmp->getOperand(1).getReg() == ConstReg
+ ? Cmp->getOperand(2).getReg()
+ : Cmp->getOperand(1).getReg();
+ Register Eor = MRI->createVirtualRegister(Is64 ? &AArch64::GPR64RegClass
+ : &AArch64::GPR32RegClass);
+ BuildMI(MBB, *Cmp, Cmp->getDebugLoc(),
+ TII->get(Is64 ? AArch64::EORXri : AArch64::EORWri), Eor)
+ .addReg(X)
+ .addImm(AArch64_AM::encodeLogicalImmediate(Imm, Bits));
+ unsigned BrOpc = CC == AArch64CC::EQ
+ ? (Is64 ? AArch64::CBZX : AArch64::CBZW)
+ : (Is64 ? AArch64::CBNZX : AArch64::CBNZW);
+ BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BrOpc))
+ .addReg(Eor)
+ .addMBB(MI.getOperand(1).getMBB());
+ MI.eraseFromParent();
+ Cmp->eraseFromParent();
+ if (MRI->use_nodbg_empty(ConstReg)) {
+ if (SubregMI)
+ SubregMI->eraseFromParent();
+ if (MRI->use_nodbg_empty(MovReg))
+ MovMI->eraseFromParent();
+ }
+ return true;
+}
+
bool AArch64MIPeepholeOptImpl::run(MachineFunction &MF) {
TII = static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
TRI = static_cast<const AArch64RegisterInfo *>(
@@ -1070,6 +1218,9 @@ bool AArch64MIPeepholeOptImpl::run(MachineFunction &MF) {
case AArch64::COPY:
Changed |= visitCopy(MI);
break;
+ case AArch64::Bcc:
+ Changed |= visitBcc(MI);
+ break;
}
}
}
diff --git a/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll b/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
index 72accb4b6dacb..fb038d8e3a6a4 100644
--- a/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
+++ b/llvm/test/CodeGen/AArch64/branch-eq-logical-imm.ll
@@ -10,9 +10,8 @@ declare void @h()
define void @br_eq_int64_min(i64 %x) {
; CHECK-LABEL: br_eq_int64_min:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
-; CHECK-NEXT: cmp x0, x8
-; CHECK-NEXT: b.ne .LBB0_2
+; CHECK-NEXT: eor x8, x0, #0x8000000000000000
+; CHECK-NEXT: cbnz x8, .LBB0_2
; CHECK-NEXT: // %bb.1: // %then
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
@@ -34,9 +33,8 @@ exit:
define void @br_ne_int64_min(i64 %x) {
; CHECK-LABEL: br_ne_int64_min:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: mov x8, #-9223372036854775808 // =0x8000000000000000
-; CHECK-NEXT: cmp x0, x8
-; CHECK-NEXT: b.eq .LBB1_2
+; CHECK-NEXT: eor x8, x0, #0x8000000000000000
+; CHECK-NEXT: cbz x8, .LBB1_2
; CHECK-NEXT: // %bb.1: // %then
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
@@ -58,9 +56,8 @@ exit:
define void @br_eq_i32_signbit(i32 %x) {
; CHECK-LABEL: br_eq_i32_signbit:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: mov w8, #-2147483648 // =0x80000000
-; CHECK-NEXT: cmp w0, w8
-; CHECK-NEXT: b.ne .LBB2_2
+; CHECK-NEXT: eor w8, w0, #0x80000000
+; CHECK-NEXT: cbnz w8, .LBB2_2
; CHECK-NEXT: // %bb.1: // %then
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
@@ -82,9 +79,8 @@ exit:
define void @br_ne_i32_ffffff(i32 %x) {
; CHECK-LABEL: br_ne_i32_ffffff:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: mov w8, #16777215 // =0xffffff
-; CHECK-NEXT: cmp w0, w8
-; CHECK-NEXT: b.eq .LBB3_2
+; CHECK-NEXT: eor w8, w0, #0xffffff
+; CHECK-NEXT: cbz w8, .LBB3_2
; CHECK-NEXT: // %bb.1: // %then
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
@@ -106,9 +102,8 @@ exit:
define void @br_eq_i64_int32_max(i64 %x) {
; CHECK-LABEL: br_eq_i64_int32_max:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: mov w8, #2147483647 // =0x7fffffff
-; CHECK-NEXT: cmp x0, x8
-; CHECK-NEXT: b.ne .LBB4_2
+; CHECK-NEXT: eor x8, x0, #0x7fffffff
+; CHECK-NEXT: cbnz x8, .LBB4_2
; CHECK-NEXT: // %bb.1: // %then
; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 16
@@ -131,26 +126,26 @@ exit:
define void @br_two_uses(i64 %x, i64 %y) {
; CHECK-LABEL: br_two_uses:
; CHECK: // %bb.0: // %entry
-; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
-; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
-; CHECK-NEXT: .cfi_def_cfa_offset 32
+; CHECK-NEXT: stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 16
; CHECK-NEXT: .cfi_offset w19, -8
-; CHECK-NEXT: .cfi_offset w20, -16
-; CHECK-NEXT: .cfi_offset w30, -32
-; CHECK-NEXT: mov x20, #-9223372036854775808 // =0x8000000000000000
+; CHECK-NEXT: .cfi_offset w30, -16
+; CHECK-NEXT: eor x8, x0, #0x8000000000000000
; CHECK-NEXT: mov x19, x1
-; CHECK-NEXT: cmp x0, x20
-; CHECK-NEXT: b.ne .LBB5_2
-; CHECK-NEXT: // %bb.1: // %callg
+; CHECK-NEXT: cbz x8, .LBB5_3
+; CHECK-NEXT: // %bb.1: // %second
+; CHECK-NEXT: eor x8, x19, #0x8000000000000000
+; CHECK-NEXT: cbz x8, .LBB5_4
+; CHECK-NEXT: .LBB5_2: // %exit
+; CHECK-NEXT: ldp x30, x19, [sp], #16 // 16-byte Folded Reload
+; CHECK-NEXT: ret
+; CHECK-NEXT: .LBB5_3: // %callg
; CHECK-NEXT: bl g
-; CHECK-NEXT: .LBB5_2: // %second
-; CHECK-NEXT: cmp x19, x20
-; CHECK-NEXT: b.ne .LBB5_4
-; CHECK-NEXT: // %bb.3: // %callh
+; CHECK-NEXT: eor x8, x19, #0x8000000000000000
+; CHECK-NEXT: cbnz x8, .LBB5_2
+; CHECK-NEXT: .LBB5_4: // %callh
; CHECK-NEXT: bl h
-; CHECK-NEXT: .LBB5_4: // %exit
-; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
-; CHECK-NEXT: ldr x30, [sp], #32 // 8-byte Folded Reload
+; CHECK-NEXT: ldp x30, x19, [sp], #16 // 16-byte Folded Reload
; CHECK-NEXT: ret
entry:
%cx = icmp eq i64 %x, -9223372036854775808
@@ -420,16 +415,14 @@ define void @br_loop(ptr %p, i64 %n) {
; CHECK-NEXT: cmp x1, #1
; CHECK-NEXT: b.lt .LBB14_6
; CHECK-NEXT: // %bb.1: // %loop.preheader
-; CHECK-NEXT: stp x30, x21, [sp, #-32]! // 16-byte Folded Spill
+; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
; CHECK-NEXT: .cfi_def_cfa_offset 32
; CHECK-NEXT: .cfi_offset w19, -8
; CHECK-NEXT: .cfi_offset w20, -16
-; CHECK-NEXT: .cfi_offset w21, -24
; CHECK-NEXT: .cfi_offset w30, -32
; CHECK-NEXT: mov x19, x1
; CHECK-NEXT: mov x20, x0
-; CHECK-NEXT: mov x21, #-9223372036854775808 // =0x8000000000000000
; CHECK-NEXT: b .LBB14_3
; CHECK-NEXT: .LBB14_2: // %latch
; CHECK-NEXT: // in Loop: Header=BB14_3 Depth=1
@@ -438,15 +431,15 @@ define void @br_loop(ptr %p, i64 %n) {
; CHECK-NEXT: .LBB14_3: // %loop
; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
; CHECK-NEXT: ldr x8, [x20], #8
-; CHECK-NEXT: cmp x8, x21
-; CHECK-NEXT: b.ne .LBB14_2
+; CHECK-NEXT: eor x8, x8, #0x8000000000000000
+; CHECK-NEXT: cbnz x8, .LBB14_2
; CHECK-NEXT: // %bb.4: // %call
; CHECK-NEXT: // in Loop: Header=BB14_3 Depth=1
; CHECK-NEXT: bl g
; CHECK-NEXT: b .LBB14_2
; CHECK-NEXT: .LBB14_5:
; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
-; CHECK-NEXT: ldp x30, x21, [sp], #32 // 16-byte Folded Reload
+; CHECK-NEXT: ldr x30, [sp], #32 // 8-byte Folded Reload
; CHECK-NEXT: .LBB14_6: // %exit
; CHECK-NEXT: ret
entry:
More information about the llvm-commits
mailing list