[llvm] [AArch64] Fold cmp #0 into its definition when the flags feed a CCMP (PR #221858)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 7 18:15:48 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-aarch64
Author: Andrew Gaul (gaul)
<details>
<summary>Changes</summary>
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.
---
Patch is 23.23 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/221858.diff
3 Files Affected:
- (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.cpp (+36-3)
- (added) llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll (+296)
- (added) llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir (+278)
``````````diff
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
new file mode 100644
index 0000000000000..80ac71522eb30
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/ccmp-cmp-zero-flag-setting.ll
@@ -0,0 +1,296 @@
+; 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: ands w0, w0, w1
+; 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: tst w0, w1
+; 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: ands w0, w0, #0xff
+; 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: bics w0, w0, w1
+; 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: adds x0, x0, x1
+; 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: subs x0, x0, #1
+; 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: adds x8, x0, x1
+; 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: subs x8, x0, x1
+; 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..5d3b4716892cc
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/peephole-cmp-zero-ccmp-user.mir
@@ -0,0 +1,278 @@
+# 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: [[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]], [[ANDSWrr]]
+ ; 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: [[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]], [[ANDSWri]]
+ ; 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: [[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]], [[BICSXrr]]
+ ; 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: [[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: [[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
+ %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: [[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]], [[SUBSWri]]
+ ; 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: [[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: [[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
+ %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...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/221858
More information about the llvm-commits
mailing list