Skip to content

Commit db97d56

Browse files
authored
[X86][APX] Handle AND_NF instruction for compare peephole (llvm#136233)
1 parent 7f41095 commit db97d56

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,12 +1035,11 @@ inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) {
10351035
return ShAmt < 4 && ShAmt > 0;
10361036
}
10371037

1038-
static bool findRedundantFlagInstr(MachineInstr &CmpInstr,
1039-
MachineInstr &CmpValDefInstr,
1040-
const MachineRegisterInfo *MRI,
1041-
MachineInstr **AndInstr,
1042-
const TargetRegisterInfo *TRI,
1043-
bool &NoSignFlag, bool &ClearsOverflowFlag) {
1038+
static bool
1039+
findRedundantFlagInstr(MachineInstr &CmpInstr, MachineInstr &CmpValDefInstr,
1040+
const MachineRegisterInfo *MRI, MachineInstr **AndInstr,
1041+
const TargetRegisterInfo *TRI, const X86Subtarget &ST,
1042+
bool &NoSignFlag, bool &ClearsOverflowFlag) {
10441043
if (!(CmpValDefInstr.getOpcode() == X86::SUBREG_TO_REG &&
10451044
CmpInstr.getOpcode() == X86::TEST64rr) &&
10461045
!(CmpValDefInstr.getOpcode() == X86::COPY &&
@@ -1103,7 +1102,8 @@ static bool findRedundantFlagInstr(MachineInstr &CmpInstr,
11031102
if (VregDefInstr->getParent() != CmpValDefInstr.getParent())
11041103
return false;
11051104

1106-
if (X86::isAND(VregDefInstr->getOpcode())) {
1105+
if (X86::isAND(VregDefInstr->getOpcode()) &&
1106+
(!ST.hasNF() || VregDefInstr->modifiesRegister(X86::EFLAGS, TRI))) {
11071107
// Get a sequence of instructions like
11081108
// %reg = and* ... // Set EFLAGS
11091109
// ... // EFLAGS not changed
@@ -5433,7 +5433,7 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
54335433
MachineInstr *AndInstr = nullptr;
54345434
if (IsCmpZero &&
54355435
findRedundantFlagInstr(CmpInstr, Inst, MRI, &AndInstr, TRI,
5436-
NoSignFlag, ClearsOverflowFlag)) {
5436+
Subtarget, NoSignFlag, ClearsOverflowFlag)) {
54375437
assert(AndInstr != nullptr && X86::isAND(AndInstr->getOpcode()));
54385438
MI = AndInstr;
54395439
break;

llvm/test/CodeGen/X86/apx/nf-regressions.ll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,36 @@ bb14: ; preds = %bb12
7373
bb16: ; preds = %bb14, %bb11, %bb10, %bb
7474
ret void
7575
}
76+
77+
; We must not try to replace CMP with AND_NF as it sets no flags
78+
define void @cmp_peephole_and_nf(i64 %arg0, ptr %ptr1, ptr %ptr2) {
79+
; CHECK-LABEL: cmp_peephole_and_nf:
80+
; CHECK: # %bb.0: # %entry
81+
; CHECK-NEXT: negq %rdi
82+
; CHECK-NEXT: movl %edi, %eax
83+
; CHECK-NEXT: {nf} andl $1, %eax
84+
; CHECK-NEXT: jb .LBB1_2
85+
; CHECK-NEXT: # %bb.1: # %true
86+
; CHECK-NEXT: testq %rax, %rax
87+
; CHECK-NEXT: sete (%rsi)
88+
; CHECK-NEXT: retq
89+
; CHECK-NEXT: .LBB1_2: # %false
90+
; CHECK-NEXT: movq %rdi, (%rsi)
91+
; CHECK-NEXT: movq %rax, (%rdx)
92+
; CHECK-NEXT: retq
93+
entry:
94+
%sub_flag = sub i64 0, %arg0
95+
%and_nf = and i64 %sub_flag, 1
96+
%elim = icmp eq i64 0, %arg0
97+
br i1 %elim, label %true, label %false
98+
99+
true:
100+
%8 = icmp eq i64 %and_nf, 0
101+
store i1 %8, ptr %ptr1
102+
ret void
103+
104+
false:
105+
store i64 %sub_flag, ptr %ptr1
106+
store i64 %and_nf, ptr %ptr2
107+
ret void
108+
}

0 commit comments

Comments
 (0)