Skip to content

Commit e5d262c

Browse files
committed
[X86] ICMP EQ/NE MIN_SIGNED_INT - avoid immediate argument by using NEG + SETO/SETNO
Fixes #67709
1 parent 2ca8c85 commit e5d262c

File tree

8 files changed

+45
-43
lines changed

8 files changed

+45
-43
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23816,6 +23816,18 @@ SDValue X86TargetLowering::emitFlagsForSetcc(SDValue Op0, SDValue Op1,
2381623816
}
2381723817
}
2381823818

23819+
// Look for X == INT_MIN or X != INT_MIN. We can use NEG and test for
23820+
// overflow.
23821+
if (isMinSignedConstant(Op1)) {
23822+
EVT VT = Op0.getValueType();
23823+
SDVTList CmpVTs = DAG.getVTList(VT, MVT::i32);
23824+
X86::CondCode CondCode = CC == ISD::SETEQ ? X86::COND_O : X86::COND_NO;
23825+
X86CC = DAG.getTargetConstant(CondCode, dl, MVT::i8);
23826+
SDValue Neg =
23827+
DAG.getNode(X86ISD::SUB, dl, CmpVTs, DAG.getConstant(0, dl, VT), Op0);
23828+
return SDValue(Neg.getNode(), 1);
23829+
}
23830+
2381923831
// Try to use the carry flag from the add in place of an separate CMP for:
2382023832
// (seteq (add X, -1), -1). Similar for setne.
2382123833
if (isAllOnesConstant(Op1) && Op0.getOpcode() == ISD::ADD &&

llvm/test/CodeGen/X86/2008-06-16-SubregsBug.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ define i16 @test(ptr %tmp179) nounwind {
88
; CHECK-NEXT: movzwl (%eax), %eax
99
; CHECK-NEXT: movl %eax, %ecx
1010
; CHECK-NEXT: andl $64512, %ecx ## imm = 0xFC00
11-
; CHECK-NEXT: cmpl $32768, %ecx ## imm = 0x8000
12-
; CHECK-NEXT: jne LBB0_2
11+
; CHECK-NEXT: negw %cx
12+
; CHECK-NEXT: jno LBB0_2
1313
; CHECK-NEXT: ## %bb.1: ## %bb189
1414
; CHECK-NEXT: ## kill: def $ax killed $ax killed $eax
1515
; CHECK-NEXT: retl

llvm/test/CodeGen/X86/abs.ll

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -718,17 +718,18 @@ define i8 @test_minsigned_i8(i8 %a0, i8 %a1) nounwind {
718718
; X64-NEXT: movl %edi, %ecx
719719
; X64-NEXT: xorb %al, %cl
720720
; X64-NEXT: subb %al, %cl
721-
; X64-NEXT: cmpb $-128, %dil
721+
; X64-NEXT: negb %dil
722722
; X64-NEXT: movzbl %cl, %eax
723-
; X64-NEXT: cmovel %esi, %eax
723+
; X64-NEXT: cmovol %esi, %eax
724724
; X64-NEXT: # kill: def $al killed $al killed $eax
725725
; X64-NEXT: retq
726726
;
727727
; X86-LABEL: test_minsigned_i8:
728728
; X86: # %bb.0:
729729
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %eax
730-
; X86-NEXT: cmpb $-128, %al
731-
; X86-NEXT: jne .LBB17_1
730+
; X86-NEXT: movl %eax, %ecx
731+
; X86-NEXT: negb %cl
732+
; X86-NEXT: jno .LBB17_1
732733
; X86-NEXT: # %bb.2:
733734
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
734735
; X86-NEXT: # kill: def $al killed $al killed $eax
@@ -750,12 +751,10 @@ define i8 @test_minsigned_i8(i8 %a0, i8 %a1) nounwind {
750751
define i16 @test_minsigned_i16(i16 %a0, i16 %a1) nounwind {
751752
; X64-LABEL: test_minsigned_i16:
752753
; X64: # %bb.0:
753-
; X64-NEXT: movzwl %di, %ecx
754-
; X64-NEXT: movl %ecx, %eax
754+
; X64-NEXT: movl %edi, %eax
755755
; X64-NEXT: negw %ax
756-
; X64-NEXT: cmovsw %cx, %ax
757-
; X64-NEXT: cmpl $32768, %ecx # imm = 0x8000
758-
; X64-NEXT: cmovel %esi, %eax
756+
; X64-NEXT: cmovsw %di, %ax
757+
; X64-NEXT: cmovol %esi, %eax
759758
; X64-NEXT: # kill: def $ax killed $ax killed $eax
760759
; X64-NEXT: retq
761760
;
@@ -765,11 +764,7 @@ define i16 @test_minsigned_i16(i16 %a0, i16 %a1) nounwind {
765764
; X86-NEXT: movl %ecx, %eax
766765
; X86-NEXT: negw %ax
767766
; X86-NEXT: cmovsw %cx, %ax
768-
; X86-NEXT: cmpl $32768, %ecx # imm = 0x8000
769-
; X86-NEXT: jne .LBB18_2
770-
; X86-NEXT: # %bb.1:
771-
; X86-NEXT: movzwl {{[0-9]+}}(%esp), %eax
772-
; X86-NEXT: .LBB18_2:
767+
; X86-NEXT: cmovow {{[0-9]+}}(%esp), %ax
773768
; X86-NEXT: retl
774769
%lim = icmp eq i16 %a0, -32768
775770
%abs = tail call i16 @llvm.abs.i16(i16 %a0, i1 false)
@@ -783,8 +778,7 @@ define i32 @test_minsigned_i32(i32 %a0, i32 %a1) nounwind {
783778
; X64-NEXT: movl %edi, %eax
784779
; X64-NEXT: negl %eax
785780
; X64-NEXT: cmovsl %edi, %eax
786-
; X64-NEXT: cmpl $-2147483648, %edi # imm = 0x80000000
787-
; X64-NEXT: cmovel %esi, %eax
781+
; X64-NEXT: cmovol %esi, %eax
788782
; X64-NEXT: retq
789783
;
790784
; X86-LABEL: test_minsigned_i32:
@@ -793,11 +787,7 @@ define i32 @test_minsigned_i32(i32 %a0, i32 %a1) nounwind {
793787
; X86-NEXT: movl %ecx, %eax
794788
; X86-NEXT: negl %eax
795789
; X86-NEXT: cmovsl %ecx, %eax
796-
; X86-NEXT: cmpl $-2147483648, %ecx # imm = 0x80000000
797-
; X86-NEXT: jne .LBB19_2
798-
; X86-NEXT: # %bb.1:
799-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
800-
; X86-NEXT: .LBB19_2:
790+
; X86-NEXT: cmovol {{[0-9]+}}(%esp), %eax
801791
; X86-NEXT: retl
802792
%lim = icmp eq i32 %a0, -2147483648
803793
%abs = tail call i32 @llvm.abs.i32(i32 %a0, i1 false)
@@ -811,9 +801,7 @@ define i64 @test_minsigned_i64(i64 %a0, i64 %a1) nounwind {
811801
; X64-NEXT: movq %rdi, %rax
812802
; X64-NEXT: negq %rax
813803
; X64-NEXT: cmovsq %rdi, %rax
814-
; X64-NEXT: movabsq $-9223372036854775808, %rcx # imm = 0x8000000000000000
815-
; X64-NEXT: cmpq %rcx, %rdi
816-
; X64-NEXT: cmoveq %rsi, %rax
804+
; X64-NEXT: cmovoq %rsi, %rax
817805
; X64-NEXT: retq
818806
;
819807
; X86-LABEL: test_minsigned_i64:

llvm/test/CodeGen/X86/combine-sdiv.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ define i32 @combine_sdiv_by_minsigned(i32 %x) {
5858
; CHECK-LABEL: combine_sdiv_by_minsigned:
5959
; CHECK: # %bb.0:
6060
; CHECK-NEXT: xorl %eax, %eax
61-
; CHECK-NEXT: cmpl $-2147483648, %edi # imm = 0x80000000
62-
; CHECK-NEXT: sete %al
61+
; CHECK-NEXT: negl %edi
62+
; CHECK-NEXT: seto %al
6363
; CHECK-NEXT: retq
6464
%1 = sdiv i32 %x, -2147483648
6565
ret i32 %1

llvm/test/CodeGen/X86/is_fpclass.ll

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -937,15 +937,16 @@ entry:
937937
define i1 @is_minus_zero_f(float %x) {
938938
; X86-LABEL: is_minus_zero_f:
939939
; X86: # %bb.0: # %entry
940-
; X86-NEXT: cmpl $-2147483648, {{[0-9]+}}(%esp) # imm = 0x80000000
941-
; X86-NEXT: sete %al
940+
; X86-NEXT: xorl %eax, %eax
941+
; X86-NEXT: cmpl {{[0-9]+}}(%esp), %eax
942+
; X86-NEXT: seto %al
942943
; X86-NEXT: retl
943944
;
944945
; X64-LABEL: is_minus_zero_f:
945946
; X64: # %bb.0: # %entry
946947
; X64-NEXT: movd %xmm0, %eax
947-
; X64-NEXT: cmpl $-2147483648, %eax # imm = 0x80000000
948-
; X64-NEXT: sete %al
948+
; X64-NEXT: negl %eax
949+
; X64-NEXT: seto %al
949950
; X64-NEXT: retq
950951
entry:
951952
%0 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 32) ; 0x20 = "-zero"
@@ -955,15 +956,16 @@ entry:
955956
define i1 @not_is_minus_zero_f(float %x) {
956957
; X86-LABEL: not_is_minus_zero_f:
957958
; X86: # %bb.0: # %entry
958-
; X86-NEXT: cmpl $-2147483648, {{[0-9]+}}(%esp) # imm = 0x80000000
959-
; X86-NEXT: setne %al
959+
; X86-NEXT: xorl %eax, %eax
960+
; X86-NEXT: cmpl {{[0-9]+}}(%esp), %eax
961+
; X86-NEXT: setno %al
960962
; X86-NEXT: retl
961963
;
962964
; X64-LABEL: not_is_minus_zero_f:
963965
; X64: # %bb.0: # %entry
964966
; X64-NEXT: movd %xmm0, %eax
965-
; X64-NEXT: cmpl $-2147483648, %eax # imm = 0x80000000
966-
; X64-NEXT: setne %al
967+
; X64-NEXT: negl %eax
968+
; X64-NEXT: setno %al
967969
; X64-NEXT: retq
968970
entry:
969971
%0 = tail call i1 @llvm.is.fpclass.f32(float %x, i32 991) ; ~0x20 = ~"-zero"

llvm/test/CodeGen/X86/lsr-overflow.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
; The comparison uses the pre-inc value, which could lead LSR to
55
; try to compute -INT64_MIN.
66

7-
; CHECK: movabsq $-9223372036854775808, %rax
8-
; CHECK: cmpq %rax,
9-
; CHECK: sete %al
7+
; CHECK-NOT: movabsq $-9223372036854775808, %rax
8+
; CHECK: negq %r
9+
; CHECK-NEXT: seto %al
1010

1111
declare i64 @bar()
1212

llvm/test/CodeGen/X86/shrink-compare-pgso.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ if.end:
265265
define dso_local void @test_sext_i8_icmp_neg128(i8 %x) nounwind !prof !14 {
266266
; CHECK-LABEL: test_sext_i8_icmp_neg128:
267267
; CHECK: # %bb.0: # %entry
268-
; CHECK-NEXT: cmpb $-128, %dil
269-
; CHECK-NEXT: je bar # TAILCALL
268+
; CHECK-NEXT: negb %dil
269+
; CHECK-NEXT: jo bar # TAILCALL
270270
; CHECK-NEXT: # %bb.1: # %if.end
271271
; CHECK-NEXT: retq
272272
entry:

llvm/test/CodeGen/X86/shrink-compare.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ if.end:
265265
define dso_local void @test_sext_i8_icmp_neg128(i8 %x) nounwind minsize {
266266
; CHECK-LABEL: test_sext_i8_icmp_neg128:
267267
; CHECK: # %bb.0: # %entry
268-
; CHECK-NEXT: cmpb $-128, %dil
269-
; CHECK-NEXT: je bar # TAILCALL
268+
; CHECK-NEXT: negb %dil
269+
; CHECK-NEXT: jo bar # TAILCALL
270270
; CHECK-NEXT: # %bb.1: # %if.end
271271
; CHECK-NEXT: retq
272272
entry:

0 commit comments

Comments
 (0)