Skip to content

Commit 72679c8

Browse files
[X86][GlobalIsel] Adds support for G_UMIN/G_UMAX/G_SMIN/G_SMAX (#161783)
Original PR broke in rebase #160247. Continuing here This patch adds support for G_[U|S][MIN|MAX] opcodes into X86 Target. This PR addressed review comments 1. About Widening to next power of 2 #160247 (comment) 2. clamping scalar #160247 (comment)
1 parent e83c3c5 commit 72679c8

File tree

5 files changed

+652
-328
lines changed

5 files changed

+652
-328
lines changed

llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
149149
});
150150
}
151151

152+
getActionDefinitionsBuilder({G_UMIN, G_UMAX, G_SMIN, G_SMAX})
153+
.widenScalarToNextPow2(0, /*Min=*/32)
154+
.lower();
155+
152156
// integer addition/subtraction
153157
getActionDefinitionsBuilder({G_ADD, G_SUB})
154158
.legalFor({s8, s16, s32})

llvm/test/CodeGen/X86/isel-smax.ll

Lines changed: 162 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2-
; RUN: llc < %s -mtriple=x86_64-linux-gnu | FileCheck %s --check-prefixes=X64
3-
; RUN: llc < %s -mtriple=x86_64-linux-gnu -fast-isel | FileCheck %s --check-prefixes=FASTISEL-X64
4-
; RUN: llc < %s -mtriple=x86_64-linux-gnu -global-isel -global-isel-abort=2 | FileCheck %s --check-prefixes=X64
5-
; RUN: llc < %s -mtriple=i686-linux-gnu | FileCheck %s --check-prefixes=X86
6-
; RUN: llc < %s -mtriple=i686-linux-gnu -fast-isel | FileCheck %s --check-prefixes=FASTISEL-X86
7-
; RUN: llc < %s -mtriple=i686-linux-gnu -global-isel -global-isel-abort=2 | FileCheck %s --check-prefixes=X86
2+
; RUN: llc < %s -mtriple=x86_64-linux-gnu | FileCheck %s --check-prefixes=X64,DAG-X64
3+
; RUN: llc < %s -mtriple=x86_64-linux-gnu -fast-isel | FileCheck %s --check-prefixes=X64,FASTISEL-X64
4+
; RUN: llc < %s -mtriple=x86_64-linux-gnu -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=GISEL-X64
5+
; RUN: llc < %s -mtriple=i686-linux-gnu | FileCheck %s --check-prefixes=X86,DAG-X86
6+
; RUN: llc < %s -mtriple=i686-linux-gnu -fast-isel | FileCheck %s --check-prefixes=X86,FASTISEL-X86
7+
; RUN: llc < %s -mtriple=i686-linux-gnu -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=GISEL-X86
88

99
define i8 @smax_i8(i8 %a, i8 %b) nounwind readnone {
10-
; X64-LABEL: smax_i8:
11-
; X64: # %bb.0:
12-
; X64-NEXT: movl %esi, %eax
13-
; X64-NEXT: cmpb %al, %dil
14-
; X64-NEXT: cmovgl %edi, %eax
15-
; X64-NEXT: # kill: def $al killed $al killed $eax
16-
; X64-NEXT: retq
10+
; DAG-X64-LABEL: smax_i8:
11+
; DAG-X64: # %bb.0:
12+
; DAG-X64-NEXT: movl %esi, %eax
13+
; DAG-X64-NEXT: cmpb %al, %dil
14+
; DAG-X64-NEXT: cmovgl %edi, %eax
15+
; DAG-X64-NEXT: # kill: def $al killed $al killed $eax
16+
; DAG-X64-NEXT: retq
1717
;
1818
; FASTISEL-X64-LABEL: smax_i8:
1919
; FASTISEL-X64: # %bb.0:
@@ -24,6 +24,17 @@ define i8 @smax_i8(i8 %a, i8 %b) nounwind readnone {
2424
; FASTISEL-X64-NEXT: # kill: def $al killed $al killed $eax
2525
; FASTISEL-X64-NEXT: retq
2626
;
27+
; GISEL-X64-LABEL: smax_i8:
28+
; GISEL-X64: # %bb.0:
29+
; GISEL-X64-NEXT: movl %esi, %eax
30+
; GISEL-X64-NEXT: xorl %ecx, %ecx
31+
; GISEL-X64-NEXT: cmpb %al, %dil
32+
; GISEL-X64-NEXT: setg %cl
33+
; GISEL-X64-NEXT: andl $1, %ecx
34+
; GISEL-X64-NEXT: cmovnew %di, %ax
35+
; GISEL-X64-NEXT: # kill: def $al killed $al killed $eax
36+
; GISEL-X64-NEXT: retq
37+
;
2738
; X86-LABEL: smax_i8:
2839
; X86: # %bb.0:
2940
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
@@ -35,16 +46,20 @@ define i8 @smax_i8(i8 %a, i8 %b) nounwind readnone {
3546
; X86-NEXT: .LBB0_2:
3647
; X86-NEXT: retl
3748
;
38-
; FASTISEL-X86-LABEL: smax_i8:
39-
; FASTISEL-X86: # %bb.0:
40-
; FASTISEL-X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
41-
; FASTISEL-X86-NEXT: movzbl {{[0-9]+}}(%esp), %eax
42-
; FASTISEL-X86-NEXT: cmpb %cl, %al
43-
; FASTISEL-X86-NEXT: jg .LBB0_2
44-
; FASTISEL-X86-NEXT: # %bb.1:
45-
; FASTISEL-X86-NEXT: movl %ecx, %eax
46-
; FASTISEL-X86-NEXT: .LBB0_2:
47-
; FASTISEL-X86-NEXT: retl
49+
; GISEL-X86-LABEL: smax_i8:
50+
; GISEL-X86: # %bb.0:
51+
; GISEL-X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
52+
; GISEL-X86-NEXT: movzbl {{[0-9]+}}(%esp), %eax
53+
; GISEL-X86-NEXT: xorl %edx, %edx
54+
; GISEL-X86-NEXT: cmpb %al, %cl
55+
; GISEL-X86-NEXT: setg %dl
56+
; GISEL-X86-NEXT: andl $1, %edx
57+
; GISEL-X86-NEXT: je .LBB0_2
58+
; GISEL-X86-NEXT: # %bb.1:
59+
; GISEL-X86-NEXT: movl %ecx, %eax
60+
; GISEL-X86-NEXT: .LBB0_2:
61+
; GISEL-X86-NEXT: # kill: def $al killed $al killed $eax
62+
; GISEL-X86-NEXT: retl
4863
%ret = call i8 @llvm.smax.i8(i8 %a, i8 %b)
4964
ret i8 %ret
5065
}
@@ -57,25 +72,28 @@ define i16 @smax_i16(i16 %a, i16 %b) nounwind readnone {
5772
; X64-NEXT: # kill: def $ax killed $ax killed $eax
5873
; X64-NEXT: retq
5974
;
60-
; FASTISEL-X64-LABEL: smax_i16:
61-
; FASTISEL-X64: # %bb.0:
62-
; FASTISEL-X64-NEXT: movl %esi, %eax
63-
; FASTISEL-X64-NEXT: cmpw %ax, %di
64-
; FASTISEL-X64-NEXT: cmovgl %edi, %eax
65-
; FASTISEL-X64-NEXT: # kill: def $ax killed $ax killed $eax
66-
; FASTISEL-X64-NEXT: retq
75+
; GISEL-X64-LABEL: smax_i16:
76+
; GISEL-X64: # %bb.0:
77+
; GISEL-X64-NEXT: movl %edi, %eax
78+
; GISEL-X64-NEXT: xorl %ecx, %ecx
79+
; GISEL-X64-NEXT: cmpw %si, %ax
80+
; GISEL-X64-NEXT: setg %cl
81+
; GISEL-X64-NEXT: andl $1, %ecx
82+
; GISEL-X64-NEXT: cmovew %si, %ax
83+
; GISEL-X64-NEXT: # kill: def $ax killed $ax killed $eax
84+
; GISEL-X64-NEXT: retq
6785
;
68-
; X86-LABEL: smax_i16:
69-
; X86: # %bb.0:
70-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
71-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
72-
; X86-NEXT: cmpw %cx, %ax
73-
; X86-NEXT: jg .LBB1_2
74-
; X86-NEXT: # %bb.1:
75-
; X86-NEXT: movl %ecx, %eax
76-
; X86-NEXT: .LBB1_2:
77-
; X86-NEXT: # kill: def $ax killed $ax killed $eax
78-
; X86-NEXT: retl
86+
; DAG-X86-LABEL: smax_i16:
87+
; DAG-X86: # %bb.0:
88+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
89+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
90+
; DAG-X86-NEXT: cmpw %cx, %ax
91+
; DAG-X86-NEXT: jg .LBB1_2
92+
; DAG-X86-NEXT: # %bb.1:
93+
; DAG-X86-NEXT: movl %ecx, %eax
94+
; DAG-X86-NEXT: .LBB1_2:
95+
; DAG-X86-NEXT: # kill: def $ax killed $ax killed $eax
96+
; DAG-X86-NEXT: retl
7997
;
8098
; FASTISEL-X86-LABEL: smax_i16:
8199
; FASTISEL-X86: # %bb.0:
@@ -88,6 +106,21 @@ define i16 @smax_i16(i16 %a, i16 %b) nounwind readnone {
88106
; FASTISEL-X86-NEXT: .LBB1_2:
89107
; FASTISEL-X86-NEXT: # kill: def $ax killed $ax killed $eax
90108
; FASTISEL-X86-NEXT: retl
109+
;
110+
; GISEL-X86-LABEL: smax_i16:
111+
; GISEL-X86: # %bb.0:
112+
; GISEL-X86-NEXT: movzwl {{[0-9]+}}(%esp), %ecx
113+
; GISEL-X86-NEXT: movzwl {{[0-9]+}}(%esp), %eax
114+
; GISEL-X86-NEXT: xorl %edx, %edx
115+
; GISEL-X86-NEXT: cmpw %ax, %cx
116+
; GISEL-X86-NEXT: setg %dl
117+
; GISEL-X86-NEXT: andl $1, %edx
118+
; GISEL-X86-NEXT: je .LBB1_2
119+
; GISEL-X86-NEXT: # %bb.1:
120+
; GISEL-X86-NEXT: movl %ecx, %eax
121+
; GISEL-X86-NEXT: .LBB1_2:
122+
; GISEL-X86-NEXT: # kill: def $ax killed $ax killed $eax
123+
; GISEL-X86-NEXT: retl
91124
%ret = call i16 @llvm.smax.i16(i16 %a, i16 %b)
92125
ret i16 %ret
93126
}
@@ -99,12 +132,15 @@ define i32 @smax_i32(i32 %a, i32 %b) nounwind readnone {
99132
; X64-NEXT: cmovgl %edi, %eax
100133
; X64-NEXT: retq
101134
;
102-
; FASTISEL-X64-LABEL: smax_i32:
103-
; FASTISEL-X64: # %bb.0:
104-
; FASTISEL-X64-NEXT: movl %esi, %eax
105-
; FASTISEL-X64-NEXT: cmpl %esi, %edi
106-
; FASTISEL-X64-NEXT: cmovgl %edi, %eax
107-
; FASTISEL-X64-NEXT: retq
135+
; GISEL-X64-LABEL: smax_i32:
136+
; GISEL-X64: # %bb.0:
137+
; GISEL-X64-NEXT: movl %edi, %eax
138+
; GISEL-X64-NEXT: xorl %ecx, %ecx
139+
; GISEL-X64-NEXT: cmpl %esi, %edi
140+
; GISEL-X64-NEXT: setg %cl
141+
; GISEL-X64-NEXT: andl $1, %ecx
142+
; GISEL-X64-NEXT: cmovel %esi, %eax
143+
; GISEL-X64-NEXT: retq
108144
;
109145
; X86-LABEL: smax_i32:
110146
; X86: # %bb.0:
@@ -117,16 +153,19 @@ define i32 @smax_i32(i32 %a, i32 %b) nounwind readnone {
117153
; X86-NEXT: .LBB2_2:
118154
; X86-NEXT: retl
119155
;
120-
; FASTISEL-X86-LABEL: smax_i32:
121-
; FASTISEL-X86: # %bb.0:
122-
; FASTISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
123-
; FASTISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
124-
; FASTISEL-X86-NEXT: cmpl %ecx, %eax
125-
; FASTISEL-X86-NEXT: jg .LBB2_2
126-
; FASTISEL-X86-NEXT: # %bb.1:
127-
; FASTISEL-X86-NEXT: movl %ecx, %eax
128-
; FASTISEL-X86-NEXT: .LBB2_2:
129-
; FASTISEL-X86-NEXT: retl
156+
; GISEL-X86-LABEL: smax_i32:
157+
; GISEL-X86: # %bb.0:
158+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
159+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
160+
; GISEL-X86-NEXT: xorl %edx, %edx
161+
; GISEL-X86-NEXT: cmpl %eax, %ecx
162+
; GISEL-X86-NEXT: setg %dl
163+
; GISEL-X86-NEXT: andl $1, %edx
164+
; GISEL-X86-NEXT: je .LBB2_2
165+
; GISEL-X86-NEXT: # %bb.1:
166+
; GISEL-X86-NEXT: movl %ecx, %eax
167+
; GISEL-X86-NEXT: .LBB2_2:
168+
; GISEL-X86-NEXT: retl
130169
%ret = call i32 @llvm.smax.i32(i32 %a, i32 %b)
131170
ret i32 %ret
132171
}
@@ -138,32 +177,35 @@ define i64 @smax_i64(i64 %a, i64 %b) nounwind readnone {
138177
; X64-NEXT: cmovgq %rdi, %rax
139178
; X64-NEXT: retq
140179
;
141-
; FASTISEL-X64-LABEL: smax_i64:
142-
; FASTISEL-X64: # %bb.0:
143-
; FASTISEL-X64-NEXT: movq %rsi, %rax
144-
; FASTISEL-X64-NEXT: cmpq %rsi, %rdi
145-
; FASTISEL-X64-NEXT: cmovgq %rdi, %rax
146-
; FASTISEL-X64-NEXT: retq
180+
; GISEL-X64-LABEL: smax_i64:
181+
; GISEL-X64: # %bb.0:
182+
; GISEL-X64-NEXT: movq %rdi, %rax
183+
; GISEL-X64-NEXT: xorl %ecx, %ecx
184+
; GISEL-X64-NEXT: cmpq %rsi, %rdi
185+
; GISEL-X64-NEXT: setg %cl
186+
; GISEL-X64-NEXT: andl $1, %ecx
187+
; GISEL-X64-NEXT: cmoveq %rsi, %rax
188+
; GISEL-X64-NEXT: retq
147189
;
148-
; X86-LABEL: smax_i64:
149-
; X86: # %bb.0:
150-
; X86-NEXT: pushl %edi
151-
; X86-NEXT: pushl %esi
152-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
153-
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx
154-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
155-
; X86-NEXT: movl {{[0-9]+}}(%esp), %esi
156-
; X86-NEXT: cmpl %eax, %ecx
157-
; X86-NEXT: movl %esi, %edi
158-
; X86-NEXT: sbbl %edx, %edi
159-
; X86-NEXT: jl .LBB3_2
160-
; X86-NEXT: # %bb.1:
161-
; X86-NEXT: movl %ecx, %eax
162-
; X86-NEXT: movl %esi, %edx
163-
; X86-NEXT: .LBB3_2:
164-
; X86-NEXT: popl %esi
165-
; X86-NEXT: popl %edi
166-
; X86-NEXT: retl
190+
; DAG-X86-LABEL: smax_i64:
191+
; DAG-X86: # %bb.0:
192+
; DAG-X86-NEXT: pushl %edi
193+
; DAG-X86-NEXT: pushl %esi
194+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
195+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %edx
196+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
197+
; DAG-X86-NEXT: movl {{[0-9]+}}(%esp), %esi
198+
; DAG-X86-NEXT: cmpl %eax, %ecx
199+
; DAG-X86-NEXT: movl %esi, %edi
200+
; DAG-X86-NEXT: sbbl %edx, %edi
201+
; DAG-X86-NEXT: jl .LBB3_2
202+
; DAG-X86-NEXT: # %bb.1:
203+
; DAG-X86-NEXT: movl %ecx, %eax
204+
; DAG-X86-NEXT: movl %esi, %edx
205+
; DAG-X86-NEXT: .LBB3_2:
206+
; DAG-X86-NEXT: popl %esi
207+
; DAG-X86-NEXT: popl %edi
208+
; DAG-X86-NEXT: retl
167209
;
168210
; FASTISEL-X86-LABEL: smax_i64:
169211
; FASTISEL-X86: # %bb.0:
@@ -184,6 +226,44 @@ define i64 @smax_i64(i64 %a, i64 %b) nounwind readnone {
184226
; FASTISEL-X86-NEXT: popl %esi
185227
; FASTISEL-X86-NEXT: popl %edi
186228
; FASTISEL-X86-NEXT: retl
229+
;
230+
; GISEL-X86-LABEL: smax_i64:
231+
; GISEL-X86: # %bb.0:
232+
; GISEL-X86-NEXT: pushl %ebp
233+
; GISEL-X86-NEXT: pushl %ebx
234+
; GISEL-X86-NEXT: pushl %edi
235+
; GISEL-X86-NEXT: pushl %esi
236+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %esi
237+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %ebp
238+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
239+
; GISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %edx
240+
; GISEL-X86-NEXT: cmpl %eax, %esi
241+
; GISEL-X86-NEXT: seta %bl
242+
; GISEL-X86-NEXT: xorl %ecx, %ecx
243+
; GISEL-X86-NEXT: cmpl %edx, %ebp
244+
; GISEL-X86-NEXT: setg %bh
245+
; GISEL-X86-NEXT: sete %cl
246+
; GISEL-X86-NEXT: testl %ecx, %ecx
247+
; GISEL-X86-NEXT: je .LBB3_2
248+
; GISEL-X86-NEXT: # %bb.1:
249+
; GISEL-X86-NEXT: movb %bl, %bh
250+
; GISEL-X86-NEXT: .LBB3_2:
251+
; GISEL-X86-NEXT: movzbl %bh, %edi
252+
; GISEL-X86-NEXT: andl $1, %edi
253+
; GISEL-X86-NEXT: je .LBB3_4
254+
; GISEL-X86-NEXT: # %bb.3:
255+
; GISEL-X86-NEXT: movl %esi, %eax
256+
; GISEL-X86-NEXT: .LBB3_4:
257+
; GISEL-X86-NEXT: testl %edi, %edi
258+
; GISEL-X86-NEXT: je .LBB3_6
259+
; GISEL-X86-NEXT: # %bb.5:
260+
; GISEL-X86-NEXT: movl %ebp, %edx
261+
; GISEL-X86-NEXT: .LBB3_6:
262+
; GISEL-X86-NEXT: popl %esi
263+
; GISEL-X86-NEXT: popl %edi
264+
; GISEL-X86-NEXT: popl %ebx
265+
; GISEL-X86-NEXT: popl %ebp
266+
; GISEL-X86-NEXT: retl
187267
%ret = call i64 @llvm.smax.i64(i64 %a, i64 %b)
188268
ret i64 %ret
189269
}

0 commit comments

Comments
 (0)