Skip to content

Commit 2549517

Browse files
authored
[X86][APX] Remove the EFLAGS def operand rather than the last one (#131430)
The last one may be an implict use, e.g., `IDIV32r %4:gr32, implicit-def dead $eax, implicit-def $edx, implicit-def dead $eflags, implicit $eax, implicit $edx` https://godbolt.org/z/KPKzj5c8K
1 parent 3b5413c commit 2549517

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5691,7 +5691,8 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
56915691
// Replace non-NF with NF instructions.
56925692
for (auto &Inst : InstsToUpdate) {
56935693
Inst.first->setDesc(get(Inst.second));
5694-
Inst.first->removeOperand(Inst.first->getNumOperands() - 1);
5694+
Inst.first->removeOperand(
5695+
Inst.first->findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
56955696
}
56965697

56975698
// Make sure Sub instruction defines EFLAGS and mark the def live.

llvm/test/CodeGen/X86/apx/ctest.ll

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2-
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp -verify-machineinstrs | FileCheck %s
3-
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+ndd -verify-machineinstrs | FileCheck %s --check-prefix=NDD
2+
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+nf -verify-machineinstrs | FileCheck %s
3+
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+ndd,+nf -verify-machineinstrs | FileCheck %s --check-prefix=NDD
44

55
define void @ctest8rr_zf(i8 noundef %a, i8 noundef %b) {
66
; CHECK-LABEL: ctest8rr_zf:
@@ -936,4 +936,52 @@ if.end: ; preds = %entry, %if.then
936936
ret void
937937
}
938938

939+
define void @cmp_srem(ptr %p, i32 %a, ptr %b) {
940+
; CHECK-LABEL: cmp_srem:
941+
; CHECK: # %bb.0: # %bb
942+
; CHECK-NEXT: movq %rdx, %rcx
943+
; CHECK-NEXT: movl %esi, %eax
944+
; CHECK-NEXT: subl $1, %eax
945+
; CHECK-NEXT: movl (%rdi), %edi
946+
; CHECK-NEXT: cltd
947+
; CHECK-NEXT: {nf} idivl %edi
948+
; CHECK-NEXT: ctestnel {dfv=} %edx, %edx
949+
; CHECK-NEXT: sete %al
950+
; CHECK-NEXT: cmpl $1, %esi
951+
; CHECK-NEXT: ccmpael {dfv=zf} $1, %edi
952+
; CHECK-NEXT: sete %dl
953+
; CHECK-NEXT: orb %al, %dl
954+
; CHECK-NEXT: movb %dl, (%rcx)
955+
; CHECK-NEXT: retq
956+
;
957+
; NDD-LABEL: cmp_srem:
958+
; NDD: # %bb.0: # %bb
959+
; NDD-NEXT: movq %rdx, %rcx
960+
; NDD-NEXT: subl $1, %esi, %eax
961+
; NDD-NEXT: movl (%rdi), %edi
962+
; NDD-NEXT: cltd
963+
; NDD-NEXT: {nf} idivl %edi
964+
; NDD-NEXT: ctestnel {dfv=} %edx, %edx
965+
; NDD-NEXT: sete %al
966+
; NDD-NEXT: cmpl $1, %esi
967+
; NDD-NEXT: ccmpael {dfv=zf} $1, %edi
968+
; NDD-NEXT: sete %dl
969+
; NDD-NEXT: orb %dl, %al
970+
; NDD-NEXT: movb %al, (%rcx)
971+
; NDD-NEXT: retq
972+
bb:
973+
%i = icmp eq i32 %a, 0
974+
%i2 = load i32, ptr %p, align 4
975+
%i3 = icmp eq i32 %i2, 1
976+
%i4 = or i1 %i, %i3
977+
%i5 = add i32 %a, -1
978+
%i6 = srem i32 %i5, %i2
979+
%i7 = icmp eq i32 %i6, 0
980+
%i8 = icmp ne i32 %a, 1
981+
%i9 = and i1 %i8, %i7
982+
%i10 = or i1 %i4, %i9
983+
store i1 %i10, ptr %b, align 1
984+
ret void
985+
}
986+
939987
declare dso_local void @foo(...)

0 commit comments

Comments
 (0)