Skip to content

Commit 507e0c3

Browse files
authored
[X86][APX] Try to replace non-NF with NF instructions when optimizeCompareInstr (#130488)
https://godbolt.org/z/rWYdqnjjx
1 parent 5d92171 commit 507e0c3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,10 +5352,12 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
53525352
MachineInstr *MI = nullptr;
53535353
MachineInstr *Sub = nullptr;
53545354
MachineInstr *Movr0Inst = nullptr;
5355+
SmallVector<std::pair<MachineInstr *, unsigned>, 4> InstsToUpdate;
53555356
bool NoSignFlag = false;
53565357
bool ClearsOverflowFlag = false;
53575358
bool ShouldUpdateCC = false;
53585359
bool IsSwapped = false;
5360+
bool HasNF = Subtarget.hasNF();
53595361
unsigned OpNo = 0;
53605362
X86::CondCode NewCC = X86::COND_INVALID;
53615363
int64_t ImmDelta = 0;
@@ -5441,6 +5443,16 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
54415443
continue;
54425444
}
54435445

5446+
// Try to replace non-NF with NF instructions.
5447+
if (HasNF && Inst.registerDefIsDead(X86::EFLAGS, TRI)) {
5448+
unsigned NewOp = X86::getNFVariant(Inst.getOpcode());
5449+
if (!NewOp)
5450+
return false;
5451+
5452+
InstsToUpdate.push_back(std::make_pair(&Inst, NewOp));
5453+
continue;
5454+
}
5455+
54445456
// Cannot do anything for any other EFLAG changes.
54455457
return false;
54465458
}
@@ -5637,6 +5649,12 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
56375649
return false;
56385650
}
56395651

5652+
// Replace non-NF with NF instructions.
5653+
for (auto &Inst : InstsToUpdate) {
5654+
Inst.first->setDesc(get(Inst.second));
5655+
Inst.first->removeOperand(Inst.first->getNumOperands() - 1);
5656+
}
5657+
56405658
// Make sure Sub instruction defines EFLAGS and mark the def live.
56415659
MachineOperand *FlagDef =
56425660
Sub->findRegisterDefOperand(X86::EFLAGS, /*TRI=*/nullptr);

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2-
; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+avx512f -verify-machineinstrs | FileCheck %s
2+
; RUN: llc < %s -mtriple=x86_64 -mattr=+cf,+nf,+avx512f -verify-machineinstrs | FileCheck %s
33

44
define void @basic(i32 %a, ptr %b, ptr %p, ptr %q) {
55
; CHECK-LABEL: basic:
@@ -125,7 +125,7 @@ entry:
125125
ret void
126126
}
127127

128-
define void @single_cmp(i32 %a, i32 %b, ptr %c, ptr %d) #2 {
128+
define void @single_cmp(i32 %a, i32 %b, ptr %c, ptr %d) {
129129
; CHECK-LABEL: single_cmp:
130130
; CHECK: # %bb.0: # %entry
131131
; CHECK-NEXT: cmpl %esi, %edi
@@ -139,3 +139,22 @@ entry:
139139
tail call void @llvm.masked.store.v1i16.p0(<1 x i16> %2, ptr %d, i32 2, <1 x i1> %1)
140140
ret void
141141
}
142+
143+
define void @load_add_store(i32 %a, i32 %b, ptr %p) {
144+
; CHECK-LABEL: load_add_store:
145+
; CHECK: # %bb.0: # %entry
146+
; CHECK-NEXT: cmpl %esi, %edi
147+
; CHECK-NEXT: cfcmovnew (%rdx), %ax
148+
; CHECK-NEXT: {nf} incl %eax
149+
; CHECK-NEXT: cfcmovnew %ax, (%rdx)
150+
; CHECK-NEXT: retq
151+
entry:
152+
%0 = icmp ne i32 %a, %b
153+
%1 = insertelement <1 x i1> poison, i1 %0, i64 0
154+
%2 = tail call <1 x i16> @llvm.masked.load.v1i16.p0(ptr %p, i32 2, <1 x i1> %1, <1 x i16> poison)
155+
%3 = extractelement <1 x i16> %2, i64 0
156+
%4 = add i16 %3, 1
157+
%5 = insertelement <1 x i16> poison, i16 %4, i64 0
158+
tail call void @llvm.masked.store.v1i16.p0(<1 x i16> %5, ptr %p, i32 2, <1 x i1> %1)
159+
ret void
160+
}

0 commit comments

Comments
 (0)