[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / max-of-nots.ll
blobb8643f5002ecbd2e549d37f538d8d703c15a8852
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 define <2 x i32> @umin_of_nots(<2 x i32> %x, <2 x i32> %y) {
5 ; CHECK-LABEL: @umin_of_nots(
6 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i32> [[Y:%.*]], [[X:%.*]]
7 ; CHECK-NEXT:    [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> [[Y]]
8 ; CHECK-NEXT:    [[MIN:%.*]] = xor <2 x i32> [[TMP2]], <i32 -1, i32 -1>
9 ; CHECK-NEXT:    ret <2 x i32> [[MIN]]
11   %notx = xor <2 x i32> %x, <i32 -1, i32 -1>
12   %noty = xor <2 x i32> %y, <i32 -1, i32 -1>
13   %cmp = icmp ult <2 x i32> %notx, %noty
14   %min = select <2 x i1> %cmp, <2 x i32> %notx, <2 x i32> %noty
15   ret <2 x i32> %min
18 define <2 x i32> @smin_of_nots(<2 x i32> %x, <2 x i32> %y) {
19 ; CHECK-LABEL: @smin_of_nots(
20 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp slt <2 x i32> [[Y:%.*]], [[X:%.*]]
21 ; CHECK-NEXT:    [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> [[Y]]
22 ; CHECK-NEXT:    [[MIN:%.*]] = xor <2 x i32> [[TMP2]], <i32 -1, i32 -1>
23 ; CHECK-NEXT:    ret <2 x i32> [[MIN]]
25   %notx = xor <2 x i32> %x, <i32 -1, i32 -1>
26   %noty = xor <2 x i32> %y, <i32 -1, i32 -1>
27   %cmp = icmp sle <2 x i32> %notx, %noty
28   %min = select <2 x i1> %cmp, <2 x i32> %notx, <2 x i32> %noty
29   ret <2 x i32> %min
32 define i32 @compute_min_2(i32 %x, i32 %y) {
33 ; CHECK-LABEL: @compute_min_2(
34 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[Y:%.*]], [[X:%.*]]
35 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[Y]]
36 ; CHECK-NEXT:    ret i32 [[TMP2]]
38   %not_x = sub i32 -1, %x
39   %not_y = sub i32 -1, %y
40   %cmp = icmp sgt i32 %not_x, %not_y
41   %not_min = select i1 %cmp, i32 %not_x, i32 %not_y
42   %min = sub i32 -1, %not_min
43   ret i32 %min
46 declare void @extra_use(i8)
47 define i8 @umin_not_1_extra_use(i8 %x, i8 %y) {
48 ; CHECK-LABEL: @umin_not_1_extra_use(
49 ; CHECK-NEXT:    [[NX:%.*]] = xor i8 [[X:%.*]], -1
50 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[X]], [[Y:%.*]]
51 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i8 [[Y]], i8 [[X]]
52 ; CHECK-NEXT:    [[MINXY:%.*]] = xor i8 [[TMP2]], -1
53 ; CHECK-NEXT:    call void @extra_use(i8 [[NX]])
54 ; CHECK-NEXT:    ret i8 [[MINXY]]
56   %nx = xor i8 %x, -1
57   %ny = xor i8 %y, -1
58   %cmpxy = icmp ult i8 %nx, %ny
59   %minxy = select i1 %cmpxy, i8 %nx, i8 %ny
60   call void @extra_use(i8 %nx)
61   ret i8 %minxy
64 define i8 @umin_not_2_extra_use(i8 %x, i8 %y) {
65 ; CHECK-LABEL: @umin_not_2_extra_use(
66 ; CHECK-NEXT:    [[NX:%.*]] = xor i8 [[X:%.*]], -1
67 ; CHECK-NEXT:    [[NY:%.*]] = xor i8 [[Y:%.*]], -1
68 ; CHECK-NEXT:    [[CMPXY:%.*]] = icmp ult i8 [[NX]], [[NY]]
69 ; CHECK-NEXT:    [[MINXY:%.*]] = select i1 [[CMPXY]], i8 [[NX]], i8 [[NY]]
70 ; CHECK-NEXT:    call void @extra_use(i8 [[NX]])
71 ; CHECK-NEXT:    call void @extra_use(i8 [[NY]])
72 ; CHECK-NEXT:    ret i8 [[MINXY]]
74   %nx = xor i8 %x, -1
75   %ny = xor i8 %y, -1
76   %cmpxy = icmp ult i8 %nx, %ny
77   %minxy = select i1 %cmpxy, i8 %nx, i8 %ny
78   call void @extra_use(i8 %nx)
79   call void @extra_use(i8 %ny)
80   ret i8 %minxy
83 ; PR35834 - https://bugs.llvm.org/show_bug.cgi?id=35834
85 define i8 @umin3_not(i8 %x, i8 %y, i8 %z) {
86 ; CHECK-LABEL: @umin3_not(
87 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[Z:%.*]], [[X:%.*]]
88 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i8 [[X]], i8 [[Z]]
89 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp ugt i8 [[TMP2]], [[Y:%.*]]
90 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[TMP3]], i8 [[TMP2]], i8 [[Y]]
91 ; CHECK-NEXT:    [[R:%.*]] = xor i8 [[R_V]], -1
92 ; CHECK-NEXT:    ret i8 [[R]]
94   %nx = xor i8 %x, -1
95   %ny = xor i8 %y, -1
96   %nz = xor i8 %z, -1
97   %cmpyx = icmp ult i8 %y, %x
98   %cmpxz = icmp ult i8 %nx, %nz
99   %minxz = select i1 %cmpxz, i8 %nx, i8 %nz
100   %cmpyz = icmp ult i8 %ny, %nz
101   %minyz = select i1 %cmpyz, i8 %ny, i8 %nz
102   %r = select i1 %cmpyx, i8 %minxz, i8 %minyz
103   ret i8 %r
106 ; PR35875 - https://bugs.llvm.org/show_bug.cgi?id=35875
108 define i8 @umin3_not_more_uses(i8 %x, i8 %y, i8 %z) {
109 ; CHECK-LABEL: @umin3_not_more_uses(
110 ; CHECK-NEXT:    [[NX:%.*]] = xor i8 [[X:%.*]], -1
111 ; CHECK-NEXT:    [[NY:%.*]] = xor i8 [[Y:%.*]], -1
112 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[X]], [[Z:%.*]]
113 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i8 [[Z]], i8 [[X]]
114 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp ugt i8 [[TMP2]], [[Y]]
115 ; CHECK-NEXT:    [[TMP4:%.*]] = select i1 [[TMP3]], i8 [[TMP2]], i8 [[Y]]
116 ; CHECK-NEXT:    [[R:%.*]] = xor i8 [[TMP4]], -1
117 ; CHECK-NEXT:    call void @extra_use(i8 [[NX]])
118 ; CHECK-NEXT:    call void @extra_use(i8 [[NY]])
119 ; CHECK-NEXT:    ret i8 [[R]]
121   %nx = xor i8 %x, -1
122   %ny = xor i8 %y, -1
123   %nz = xor i8 %z, -1
124   %cmpxz = icmp ult i8 %nx, %nz
125   %minxz = select i1 %cmpxz, i8 %nx, i8 %nz
126   %cmpyz = icmp ult i8 %ny, %nz
127   %minyz = select i1 %cmpyz, i8 %ny, i8 %nz
128   %cmpyx = icmp ult i8 %y, %x
129   %r = select i1 %cmpyx, i8 %minxz, i8 %minyz
130   call void @extra_use(i8 %nx)
131   call void @extra_use(i8 %ny)
132   ret i8 %r
135 declare void @use8(i8)
137 define i8 @umin3_not_all_ops_extra_uses(i8 %x, i8 %y, i8 %z) {
138 ; CHECK-LABEL: @umin3_not_all_ops_extra_uses(
139 ; CHECK-NEXT:    [[XN:%.*]] = xor i8 [[X:%.*]], -1
140 ; CHECK-NEXT:    [[YN:%.*]] = xor i8 [[Y:%.*]], -1
141 ; CHECK-NEXT:    [[ZN:%.*]] = xor i8 [[Z:%.*]], -1
142 ; CHECK-NEXT:    [[CMPXZ:%.*]] = icmp ult i8 [[XN]], [[ZN]]
143 ; CHECK-NEXT:    [[MINXZ:%.*]] = select i1 [[CMPXZ]], i8 [[XN]], i8 [[ZN]]
144 ; CHECK-NEXT:    [[CMPXYZ:%.*]] = icmp ult i8 [[MINXZ]], [[YN]]
145 ; CHECK-NEXT:    [[MINXYZ:%.*]] = select i1 [[CMPXYZ]], i8 [[MINXZ]], i8 [[YN]]
146 ; CHECK-NEXT:    call void @use8(i8 [[XN]])
147 ; CHECK-NEXT:    call void @use8(i8 [[YN]])
148 ; CHECK-NEXT:    call void @use8(i8 [[ZN]])
149 ; CHECK-NEXT:    ret i8 [[MINXYZ]]
151   %xn = xor i8 %x, -1
152   %yn = xor i8 %y, -1
153   %zn = xor i8 %z, -1
154   %cmpxz = icmp ult i8 %xn, %zn
155   %minxz = select i1 %cmpxz, i8 %xn, i8 %zn
156   %cmpxyz = icmp ult i8 %minxz, %yn
157   %minxyz = select i1 %cmpxyz, i8 %minxz, i8 %yn
158   call void @use8(i8 %xn)
159   call void @use8(i8 %yn)
160   call void @use8(i8 %zn)
161   ret i8 %minxyz
164 define i32 @compute_min_3(i32 %x, i32 %y, i32 %z) {
165 ; CHECK-LABEL: @compute_min_3(
166 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[Y:%.*]], [[X:%.*]]
167 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 [[Y]]
168 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], [[Z:%.*]]
169 ; CHECK-NEXT:    [[TMP4:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 [[Z]]
170 ; CHECK-NEXT:    ret i32 [[TMP4]]
172   %not_x = sub i32 -1, %x
173   %not_y = sub i32 -1, %y
174   %not_z = sub i32 -1, %z
175   %cmp_1 = icmp sgt i32 %not_x, %not_y
176   %not_min_1 = select i1 %cmp_1, i32 %not_x, i32 %not_y
177   %cmp_2 = icmp sgt i32 %not_min_1, %not_z
178   %not_min_2 = select i1 %cmp_2, i32 %not_min_1, i32 %not_z
179   %min = sub i32 -1, %not_min_2
180   ret i32 %min
183 ; Don't increase the critical path by moving the 'not' op after the 'select'.
185 define i32 @compute_min_arithmetic(i32 %x, i32 %y) {
186 ; CHECK-LABEL: @compute_min_arithmetic(
187 ; CHECK-NEXT:    [[NOT_VALUE:%.*]] = sub i32 3, [[X:%.*]]
188 ; CHECK-NEXT:    [[NOT_Y:%.*]] = xor i32 [[Y:%.*]], -1
189 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[NOT_VALUE]], [[NOT_Y]]
190 ; CHECK-NEXT:    [[NOT_MIN:%.*]] = select i1 [[CMP]], i32 [[NOT_VALUE]], i32 [[NOT_Y]]
191 ; CHECK-NEXT:    ret i32 [[NOT_MIN]]
193   %not_value = sub i32 3, %x
194   %not_y = sub i32 -1, %y
195   %cmp = icmp sgt i32 %not_value, %not_y
196   %not_min = select i1 %cmp, i32 %not_value, i32 %not_y
197   ret i32 %not_min
200 declare void @fake_use(i32)
202 define i32 @compute_min_pessimization(i32 %x, i32 %y) {
203 ; CHECK-LABEL: @compute_min_pessimization(
204 ; CHECK-NEXT:    [[NOT_VALUE:%.*]] = sub i32 3, [[X:%.*]]
205 ; CHECK-NEXT:    call void @fake_use(i32 [[NOT_VALUE]])
206 ; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X]], -4
207 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], [[Y:%.*]]
208 ; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 [[Y]]
209 ; CHECK-NEXT:    ret i32 [[MIN]]
211   %not_value = sub i32 3, %x
212   call void @fake_use(i32 %not_value)
213   %not_y = sub i32 -1, %y
214   %cmp = icmp sgt i32 %not_value, %not_y
215   %not_min = select i1 %cmp, i32 %not_value, i32 %not_y
216   %min = sub i32 -1, %not_min
217   ret i32 %min
220 define i32 @max_of_nots(i32 %x, i32 %y) {
221 ; CHECK-LABEL: @max_of_nots(
222 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[Y:%.*]], 0
223 ; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[Y]], i32 0
224 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], [[X:%.*]]
225 ; CHECK-NEXT:    [[TMP4:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 [[X]]
226 ; CHECK-NEXT:    [[TMP5:%.*]] = xor i32 [[TMP4]], -1
227 ; CHECK-NEXT:    ret i32 [[TMP5]]
229   %c0 = icmp sgt i32 %y, 0
230   %xor_y = xor i32 %y, -1
231   %s0 = select i1 %c0, i32 %xor_y, i32 -1
232   %xor_x = xor i32 %x, -1
233   %c1 = icmp slt i32 %s0, %xor_x
234   %smax96 = select i1 %c1, i32 %xor_x, i32 %s0
235   ret i32 %smax96
238  ; negative test case (i.e. can not simplify) : ABS(MIN(NOT x,y))
239 define i32 @abs_of_min_of_not(i32 %x, i32 %y) {
240 ; CHECK-LABEL: @abs_of_min_of_not(
241 ; CHECK-NEXT:    [[XORD:%.*]] = xor i32 [[X:%.*]], -1
242 ; CHECK-NEXT:    [[YADD:%.*]] = add i32 [[Y:%.*]], 2
243 ; CHECK-NEXT:    [[COND_I:%.*]] = icmp slt i32 [[YADD]], [[XORD]]
244 ; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[COND_I]], i32 [[YADD]], i32 [[XORD]]
245 ; CHECK-NEXT:    [[CMP2:%.*]] = icmp slt i32 [[MIN]], 0
246 ; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[MIN]]
247 ; CHECK-NEXT:    [[ABS:%.*]] = select i1 [[CMP2]], i32 [[SUB]], i32 [[MIN]]
248 ; CHECK-NEXT:    ret i32 [[ABS]]
251   %xord = xor i32 %x, -1
252   %yadd = add i32 %y, 2
253   %cond.i = icmp sge i32 %yadd, %xord
254   %min = select i1 %cond.i, i32 %xord, i32 %yadd
255   %cmp2 = icmp sgt i32 %min, -1
256   %sub = sub i32 0, %min
257   %abs = select i1 %cmp2, i32 %min, i32 %sub
258   ret i32  %abs
261 define <2 x i32> @max_of_nots_vec(<2 x i32> %x, <2 x i32> %y) {
262 ; CHECK-LABEL: @max_of_nots_vec(
263 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i32> [[Y:%.*]], zeroinitializer
264 ; CHECK-NEXT:    [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[Y]], <2 x i32> zeroinitializer
265 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt <2 x i32> [[TMP2]], [[X:%.*]]
266 ; CHECK-NEXT:    [[TMP4:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP2]], <2 x i32> [[X]]
267 ; CHECK-NEXT:    [[TMP5:%.*]] = xor <2 x i32> [[TMP4]], <i32 -1, i32 -1>
268 ; CHECK-NEXT:    ret <2 x i32> [[TMP5]]
270   %c0 = icmp sgt <2 x i32> %y, zeroinitializer
271   %xor_y = xor <2 x i32> %y, <i32 -1, i32 -1>
272   %s0 = select <2 x i1> %c0, <2 x i32> %xor_y, <2 x i32> <i32 -1, i32 -1>
273   %xor_x = xor <2 x i32> %x, <i32 -1, i32 -1>
274   %c1 = icmp slt <2 x i32> %s0, %xor_x
275   %smax96 = select <2 x i1> %c1, <2 x i32> %xor_x, <2 x i32> %s0
276   ret <2 x i32> %smax96
279 define <2 x i37> @max_of_nots_weird_type_vec(<2 x i37> %x, <2 x i37> %y) {
280 ; CHECK-LABEL: @max_of_nots_weird_type_vec(
281 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i37> [[Y:%.*]], zeroinitializer
282 ; CHECK-NEXT:    [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i37> [[Y]], <2 x i37> zeroinitializer
283 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt <2 x i37> [[TMP2]], [[X:%.*]]
284 ; CHECK-NEXT:    [[TMP4:%.*]] = select <2 x i1> [[TMP3]], <2 x i37> [[TMP2]], <2 x i37> [[X]]
285 ; CHECK-NEXT:    [[TMP5:%.*]] = xor <2 x i37> [[TMP4]], <i37 -1, i37 -1>
286 ; CHECK-NEXT:    ret <2 x i37> [[TMP5]]
288   %c0 = icmp sgt <2 x i37> %y, zeroinitializer
289   %xor_y = xor <2 x i37> %y, <i37 -1, i37 -1>
290   %s0 = select <2 x i1> %c0, <2 x i37> %xor_y, <2 x i37> <i37 -1, i37 -1>
291   %xor_x = xor <2 x i37> %x, <i37 -1, i37 -1>
292   %c1 = icmp slt <2 x i37> %s0, %xor_x
293   %smax96 = select <2 x i1> %c1, <2 x i37> %xor_x, <2 x i37> %s0
294   ret <2 x i37> %smax96
297 ; max(min(%a, -1), -1) == -1
298 define i32 @max_of_min(i32 %a) {
299 ; CHECK-LABEL: @max_of_min(
300 ; CHECK-NEXT:    ret i32 -1
302   %not_a = xor i32 %a, -1
303   %c0 = icmp sgt i32 %a, 0
304   %s0 = select i1 %c0, i32 %not_a, i32 -1
305   %c1 = icmp sgt i32 %s0, -1
306   %s1 = select i1 %c1, i32 %s0, i32 -1
307   ret i32 %s1
310 ; max(min(%a, -1), -1) == -1 (swap predicate and select ops)
311 define i32 @max_of_min_swap(i32 %a) {
312 ; CHECK-LABEL: @max_of_min_swap(
313 ; CHECK-NEXT:    ret i32 -1
315   %not_a = xor i32 %a, -1
316   %c0 = icmp slt i32 %a, 0
317   %s0 = select i1 %c0, i32 -1, i32 %not_a
318   %c1 = icmp sgt i32 %s0, -1
319   %s1 = select i1 %c1, i32 %s0, i32 -1
320   ret i32 %s1
323 ; min(max(%a, -1), -1) == -1
324 define i32 @min_of_max(i32 %a) {
325 ; CHECK-LABEL: @min_of_max(
326 ; CHECK-NEXT:    ret i32 -1
328   %not_a = xor i32 %a, -1
329   %c0 = icmp slt i32 %a, 0
330   %s0 = select i1 %c0, i32 %not_a, i32 -1
331   %c1 = icmp slt i32 %s0, -1
332   %s1 = select i1 %c1, i32 %s0, i32 -1
333   ret i32 %s1
336 ; min(max(%a, -1), -1) == -1 (swap predicate and select ops)
337 define i32 @min_of_max_swap(i32 %a) {
338 ; CHECK-LABEL: @min_of_max_swap(
339 ; CHECK-NEXT:    ret i32 -1
341   %not_a = xor i32 %a, -1
342   %c0 = icmp sgt i32 %a, 0
343   %s0 = select i1 %c0, i32 -1, i32 %not_a
344   %c1 = icmp slt i32 %s0, -1
345   %s1 = select i1 %c1, i32 %s0, i32 -1
346   ret i32 %s1
349 define <2 x i32> @max_of_min_vec(<2 x i32> %a) {
350 ; CHECK-LABEL: @max_of_min_vec(
351 ; CHECK-NEXT:    ret <2 x i32> <i32 -1, i32 -1>
353   %not_a = xor <2 x i32> %a, <i32 -1, i32 -1>
354   %c0 = icmp sgt <2 x i32> %a, zeroinitializer
355   %s0 = select <2 x i1> %c0, <2 x i32> %not_a, <2 x i32> <i32 -1, i32 -1>
356   %c1 = icmp sgt <2 x i32> %s0, <i32 -1, i32 -1>
357   %s1 = select <2 x i1> %c1, <2 x i32> %s0, <2 x i32> <i32 -1, i32 -1>
358   ret <2 x i32> %s1