Skip to content

[X86][APX] Remove the EFLAGS def operand rather than the last one #131430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2025

Conversation

phoebewang
Copy link
Contributor

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

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
@llvmbot
Copy link
Member

llvmbot commented Mar 15, 2025

@llvm/pr-subscribers-backend-x86

Author: Phoebe Wang (phoebewang)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/131430.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86InstrInfo.cpp (+2-1)
  • (modified) llvm/test/CodeGen/X86/apx/ctest.ll (+50-2)
diff --git a/llvm/lib/Target/X86/X86InstrInfo.cpp b/llvm/lib/Target/X86/X86InstrInfo.cpp
index b13412df4b8b0..0c1329c55fcd6 100644
--- a/llvm/lib/Target/X86/X86InstrInfo.cpp
+++ b/llvm/lib/Target/X86/X86InstrInfo.cpp
@@ -5691,7 +5691,8 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
   // Replace non-NF with NF instructions.
   for (auto &Inst : InstsToUpdate) {
     Inst.first->setDesc(get(Inst.second));
-    Inst.first->removeOperand(Inst.first->getNumOperands() - 1);
+    Inst.first->removeOperand(
+        Inst.first->findRegisterDefOperandIdx(X86::EFLAGS, /*TRI=*/nullptr));
   }
 
   // Make sure Sub instruction defines EFLAGS and mark the def live.
diff --git a/llvm/test/CodeGen/X86/apx/ctest.ll b/llvm/test/CodeGen/X86/apx/ctest.ll
index 4f79b0a999632..5f3ec8a19d0a4 100644
--- a/llvm/test/CodeGen/X86/apx/ctest.ll
+++ b/llvm/test/CodeGen/X86/apx/ctest.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp -verify-machineinstrs | FileCheck %s
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+ndd -verify-machineinstrs | FileCheck %s --check-prefix=NDD
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+nf -verify-machineinstrs | FileCheck %s
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+ccmp,+ndd,+nf -verify-machineinstrs | FileCheck %s --check-prefix=NDD
 
 define void @ctest8rr_zf(i8 noundef %a, i8 noundef %b) {
 ; CHECK-LABEL: ctest8rr_zf:
@@ -936,4 +936,52 @@ if.end:                                           ; preds = %entry, %if.then
   ret void
 }
 
+define void @cmp_srem(ptr %p, i32 %a, ptr %b) {
+; CHECK-LABEL: cmp_srem:
+; CHECK:       # %bb.0: # %bb
+; CHECK-NEXT:    movq %rdx, %rcx
+; CHECK-NEXT:    movl %esi, %eax
+; CHECK-NEXT:    subl $1, %eax
+; CHECK-NEXT:    movl (%rdi), %edi
+; CHECK-NEXT:    cltd
+; CHECK-NEXT:    {nf} idivl %edi
+; CHECK-NEXT:    ctestnel {dfv=} %edx, %edx
+; CHECK-NEXT:    sete %al
+; CHECK-NEXT:    cmpl $1, %esi
+; CHECK-NEXT:    ccmpael {dfv=zf} $1, %edi
+; CHECK-NEXT:    sete %dl
+; CHECK-NEXT:    orb %al, %dl
+; CHECK-NEXT:    movb %dl, (%rcx)
+; CHECK-NEXT:    retq
+;
+; NDD-LABEL: cmp_srem:
+; NDD:       # %bb.0: # %bb
+; NDD-NEXT:    movq %rdx, %rcx
+; NDD-NEXT:    subl $1, %esi, %eax
+; NDD-NEXT:    movl (%rdi), %edi
+; NDD-NEXT:    cltd
+; NDD-NEXT:    {nf} idivl %edi
+; NDD-NEXT:    ctestnel {dfv=} %edx, %edx
+; NDD-NEXT:    sete %al
+; NDD-NEXT:    cmpl $1, %esi
+; NDD-NEXT:    ccmpael {dfv=zf} $1, %edi
+; NDD-NEXT:    sete %dl
+; NDD-NEXT:    orb %dl, %al
+; NDD-NEXT:    movb %al, (%rcx)
+; NDD-NEXT:    retq
+bb:
+  %i = icmp eq i32 %a, 0
+  %i2 = load i32, ptr %p, align 4
+  %i3 = icmp eq i32 %i2, 1
+  %i4 = or i1 %i, %i3
+  %i5 = add i32 %a, -1
+  %i6 = srem i32 %i5, %i2
+  %i7 = icmp eq i32 %i6, 0
+  %i8 = icmp ne i32 %a, 1
+  %i9 = and i1 %i8, %i7
+  %i10 = or i1 %i4, %i9
+  store i1 %i10, ptr %b, align 1
+  ret void
+}
+
 declare dso_local void @foo(...)

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@phoebewang phoebewang merged commit 2549517 into llvm:main Mar 15, 2025
13 checks passed
@phoebewang phoebewang deleted the APX branch March 15, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants