Skip to content

Commit 6c4aa1d

Browse files
committed
[InstCombine] Pre-commit tests
1 parent 6b12272 commit 6c4aa1d

File tree

1 file changed

+358
-0
lines changed

1 file changed

+358
-0
lines changed
Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
3+
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
4+
5+
; When C0 is neither 0 nor 1:
6+
; umax(nuw_mul(x, C0), x + 1) is optimized to:
7+
; x == 0 ? 1 : nuw_mul(x, C0)
8+
; When C0 is not 0:
9+
; umax(nuw_shl(x, C0), x + 1) is optimized to:
10+
; x == 0 ? 1 : nuw_shl(x, C0)
11+
12+
; Positive Test Cases for `shl`
13+
14+
define i64 @test_shl_by_2(i64 %x) {
15+
; CHECK-LABEL: define i64 @test_shl_by_2(
16+
; CHECK-SAME: i64 [[X:%.*]]) {
17+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
18+
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw i64 [[X]], 2
19+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
20+
; CHECK-NEXT: ret i64 [[MAX]]
21+
;
22+
%x1 = add i64 %x, 1
23+
%shl = shl nuw i64 %x, 2
24+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
25+
ret i64 %max
26+
}
27+
28+
define i64 @test_shl_by_5(i64 %x) {
29+
; CHECK-LABEL: define i64 @test_shl_by_5(
30+
; CHECK-SAME: i64 [[X:%.*]]) {
31+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
32+
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw i64 [[X]], 5
33+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
34+
; CHECK-NEXT: ret i64 [[MAX]]
35+
;
36+
%x1 = add i64 %x, 1
37+
%shl = shl nuw i64 %x, 5
38+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
39+
ret i64 %max
40+
}
41+
42+
define i64 @test_shl_with_nsw(i64 %x) {
43+
; CHECK-LABEL: define i64 @test_shl_with_nsw(
44+
; CHECK-SAME: i64 [[X:%.*]]) {
45+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
46+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i64 [[X]], 2
47+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[SHL]], i64 [[X1]])
48+
; CHECK-NEXT: ret i64 [[MAX]]
49+
;
50+
%x1 = add i64 %x, 1
51+
%shl = shl nuw nsw i64 %x, 2
52+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
53+
ret i64 %max
54+
}
55+
56+
; Commuted Test Cases for `shl`
57+
58+
define i64 @test_shl_umax_commuted(i64 %x) {
59+
; CHECK-LABEL: define i64 @test_shl_umax_commuted(
60+
; CHECK-SAME: i64 [[X:%.*]]) {
61+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
62+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i64 [[X]], 2
63+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[X1]], i64 [[SHL]])
64+
; CHECK-NEXT: ret i64 [[MAX]]
65+
;
66+
%x1 = add i64 %x, 1
67+
%shl = shl nuw i64 %x, 2
68+
%max = call i64 @llvm.umax.i64(i64 %x1, i64 %shl)
69+
ret i64 %max
70+
}
71+
72+
; Negative Test Cases for `shl`
73+
74+
define i64 @test_shl_by_zero(i64 %x) {
75+
; CHECK-LABEL: define i64 @test_shl_by_zero(
76+
; CHECK-SAME: i64 [[X:%.*]]) {
77+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
78+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[X]], i64 [[X1]])
79+
; CHECK-NEXT: ret i64 [[MAX]]
80+
;
81+
%x1 = add i64 %x, 1
82+
%shl = shl nuw i64 %x, 0
83+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
84+
ret i64 %max
85+
}
86+
87+
define i64 @test_shl_add_by_2(i64 %x) {
88+
; CHECK-LABEL: define i64 @test_shl_add_by_2(
89+
; CHECK-SAME: i64 [[X:%.*]]) {
90+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 2
91+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i64 [[X]], 2
92+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[SHL]], i64 [[X1]])
93+
; CHECK-NEXT: ret i64 [[MAX]]
94+
;
95+
%x1 = add i64 %x, 2
96+
%shl = shl nuw i64 %x, 2
97+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
98+
ret i64 %max
99+
}
100+
101+
define i64 @test_shl_without_nuw(i64 %x) {
102+
; CHECK-LABEL: define i64 @test_shl_without_nuw(
103+
; CHECK-SAME: i64 [[X:%.*]]) {
104+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
105+
; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[X]], 2
106+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[SHL]], i64 [[X1]])
107+
; CHECK-NEXT: ret i64 [[MAX]]
108+
;
109+
%x1 = add i64 %x, 1
110+
%shl = shl i64 %x, 2
111+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
112+
ret i64 %max
113+
}
114+
115+
define i64 @test_shl_umin(i64 %x) {
116+
; CHECK-LABEL: define i64 @test_shl_umin(
117+
; CHECK-SAME: i64 [[X:%.*]]) {
118+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
119+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i64 [[X]], 2
120+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umin.i64(i64 [[SHL]], i64 [[X1]])
121+
; CHECK-NEXT: ret i64 [[MAX]]
122+
;
123+
%x1 = add i64 %x, 1
124+
%shl = shl nuw i64 %x, 2
125+
%max = call i64 @llvm.umin.i64(i64 %shl, i64 %x1)
126+
ret i64 %max
127+
}
128+
129+
; Multi-use Test Cases for `shl`
130+
declare void @use(i64)
131+
132+
define i64 @test_shl_multi_use_add(i64 %x) {
133+
; CHECK-LABEL: define i64 @test_shl_multi_use_add(
134+
; CHECK-SAME: i64 [[X:%.*]]) {
135+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
136+
; CHECK-NEXT: call void @use(i64 [[X1]])
137+
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw i64 [[X]], 3
138+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
139+
; CHECK-NEXT: ret i64 [[MAX]]
140+
;
141+
%x1 = add i64 %x, 1
142+
call void @use(i64 %x1)
143+
%shl = shl nuw i64 %x, 3
144+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
145+
ret i64 %max
146+
}
147+
148+
define i64 @test_shl_multi_use_shl(i64 %x) {
149+
; CHECK-LABEL: define i64 @test_shl_multi_use_shl(
150+
; CHECK-SAME: i64 [[X:%.*]]) {
151+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
152+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i64 [[X]], 2
153+
; CHECK-NEXT: call void @use(i64 [[SHL]])
154+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[SHL]], i64 [[X1]])
155+
; CHECK-NEXT: ret i64 [[MAX]]
156+
;
157+
%x1 = add i64 %x, 1
158+
%shl = shl nuw i64 %x, 2
159+
call void @use(i64 %shl)
160+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
161+
ret i64 %max
162+
}
163+
164+
define i64 @test_shl_multi_use_max(i64 %x) {
165+
; CHECK-LABEL: define i64 @test_shl_multi_use_max(
166+
; CHECK-SAME: i64 [[X:%.*]]) {
167+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
168+
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw i64 [[X]], 3
169+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
170+
; CHECK-NEXT: call void @use(i64 [[MAX]])
171+
; CHECK-NEXT: ret i64 [[MAX]]
172+
;
173+
%x1 = add i64 %x, 1
174+
%shl = shl nuw i64 %x, 3
175+
%max = call i64 @llvm.umax.i64(i64 %shl, i64 %x1)
176+
call void @use(i64 %max)
177+
ret i64 %max
178+
}
179+
180+
; Positive Test Cases for `mul`
181+
182+
define i64 @test_mul_by_3(i64 %x) {
183+
; CHECK-LABEL: define i64 @test_mul_by_3(
184+
; CHECK-SAME: i64 [[X:%.*]]) {
185+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
186+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 3
187+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
188+
; CHECK-NEXT: ret i64 [[MAX]]
189+
;
190+
%x1 = add i64 %x, 1
191+
%mul = mul nuw i64 %x, 3
192+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
193+
ret i64 %max
194+
}
195+
196+
define i64 @test_mul_by_5(i64 %x) {
197+
; CHECK-LABEL: define i64 @test_mul_by_5(
198+
; CHECK-SAME: i64 [[X:%.*]]) {
199+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
200+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 5
201+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
202+
; CHECK-NEXT: ret i64 [[MAX]]
203+
;
204+
%x1 = add i64 %x, 1
205+
%mul = mul nuw i64 %x, 5
206+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
207+
ret i64 %max
208+
}
209+
210+
define i64 @test_mul_with_nsw(i64 %x) {
211+
; CHECK-LABEL: define i64 @test_mul_with_nsw(
212+
; CHECK-SAME: i64 [[X:%.*]]) {
213+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
214+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw nsw i64 [[X]], 3
215+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
216+
; CHECK-NEXT: ret i64 [[MAX]]
217+
;
218+
%x1 = add i64 %x, 1
219+
%mul = mul nuw nsw i64 %x, 3
220+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
221+
ret i64 %max
222+
}
223+
224+
; Commuted Test Cases for `mul`
225+
226+
define i64 @test_mul_max_commuted(i64 %x) {
227+
; CHECK-LABEL: define i64 @test_mul_max_commuted(
228+
; CHECK-SAME: i64 [[X:%.*]]) {
229+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
230+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 3
231+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[X1]], i64 [[MUL]])
232+
; CHECK-NEXT: ret i64 [[MAX]]
233+
;
234+
%x1 = add i64 %x, 1
235+
%mul = mul nuw i64 %x, 3
236+
%max = call i64 @llvm.umax.i64(i64 %x1, i64 %mul)
237+
ret i64 %max
238+
}
239+
240+
; Negative Test Cases for `mul`
241+
242+
define i64 @test_mul_by_zero(i64 %x) {
243+
; CHECK-LABEL: define i64 @test_mul_by_zero(
244+
; CHECK-SAME: i64 [[X:%.*]]) {
245+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
246+
; CHECK-NEXT: ret i64 [[X1]]
247+
;
248+
%x1 = add i64 %x, 1
249+
%mul = mul nuw i64 %x, 0
250+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
251+
ret i64 %max
252+
}
253+
254+
define i64 @test_mul_by_1(i64 %x) {
255+
; CHECK-LABEL: define i64 @test_mul_by_1(
256+
; CHECK-SAME: i64 [[X:%.*]]) {
257+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
258+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[X]], i64 [[X1]])
259+
; CHECK-NEXT: ret i64 [[MAX]]
260+
;
261+
%x1 = add i64 %x, 1
262+
%mul = mul nuw i64 %x, 1
263+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
264+
ret i64 %max
265+
}
266+
267+
define i64 @test_mul_add_by_2(i64 %x) {
268+
; CHECK-LABEL: define i64 @test_mul_add_by_2(
269+
; CHECK-SAME: i64 [[X:%.*]]) {
270+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 2
271+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 3
272+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
273+
; CHECK-NEXT: ret i64 [[MAX]]
274+
;
275+
%x1 = add i64 %x, 2
276+
%mul = mul nuw i64 %x, 3
277+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
278+
ret i64 %max
279+
}
280+
281+
define i64 @test_mul_without_nuw(i64 %x) {
282+
; CHECK-LABEL: define i64 @test_mul_without_nuw(
283+
; CHECK-SAME: i64 [[X:%.*]]) {
284+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
285+
; CHECK-NEXT: [[MUL:%.*]] = mul i64 [[X]], 3
286+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
287+
; CHECK-NEXT: ret i64 [[MAX]]
288+
;
289+
%x1 = add i64 %x, 1
290+
%mul = mul i64 %x, 3
291+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
292+
ret i64 %max
293+
}
294+
295+
define i64 @test_mul_umin(i64 %x) {
296+
; CHECK-LABEL: define i64 @test_mul_umin(
297+
; CHECK-SAME: i64 [[X:%.*]]) {
298+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
299+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 3
300+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umin.i64(i64 [[MUL]], i64 [[X1]])
301+
; CHECK-NEXT: ret i64 [[MAX]]
302+
;
303+
%x1 = add i64 %x, 1
304+
%mul = mul nuw i64 %x, 3
305+
%max = call i64 @llvm.umin.i64(i64 %mul, i64 %x1)
306+
ret i64 %max
307+
}
308+
309+
; Multi-use Test Cases for `mul`
310+
311+
define i64 @test_mul_multi_use_add(i64 %x) {
312+
; CHECK-LABEL: define i64 @test_mul_multi_use_add(
313+
; CHECK-SAME: i64 [[X:%.*]]) {
314+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
315+
; CHECK-NEXT: call void @use(i64 [[X1]])
316+
; CHECK-NEXT: [[TMP2:%.*]] = mul nuw i64 [[X]], 3
317+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
318+
; CHECK-NEXT: ret i64 [[MAX]]
319+
;
320+
%x1 = add i64 %x, 1
321+
call void @use(i64 %x1)
322+
%mul = mul nuw i64 %x, 3
323+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
324+
ret i64 %max
325+
}
326+
327+
define i64 @test_mul_multi_use_mul(i64 %x) {
328+
; CHECK-LABEL: define i64 @test_mul_multi_use_mul(
329+
; CHECK-SAME: i64 [[X:%.*]]) {
330+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
331+
; CHECK-NEXT: [[MUL:%.*]] = mul nuw i64 [[X]], 3
332+
; CHECK-NEXT: call void @use(i64 [[MUL]])
333+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[MUL]], i64 [[X1]])
334+
; CHECK-NEXT: ret i64 [[MAX]]
335+
;
336+
%x1 = add i64 %x, 1
337+
%mul = mul nuw i64 %x, 3
338+
call void @use(i64 %mul)
339+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
340+
ret i64 %max
341+
}
342+
343+
define i64 @test_mul_multi_use_max(i64 %x) {
344+
; CHECK-LABEL: define i64 @test_mul_multi_use_max(
345+
; CHECK-SAME: i64 [[X:%.*]]) {
346+
; CHECK-NEXT: [[X1:%.*]] = add i64 [[X]], 1
347+
; CHECK-NEXT: [[TMP2:%.*]] = mul nuw i64 [[X]], 3
348+
; CHECK-NEXT: [[MAX:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP2]], i64 [[X1]])
349+
; CHECK-NEXT: call void @use(i64 [[MAX]])
350+
; CHECK-NEXT: ret i64 [[MAX]]
351+
;
352+
%x1 = add i64 %x, 1
353+
%mul = mul nuw i64 %x, 3
354+
%max = call i64 @llvm.umax.i64(i64 %mul, i64 %x1)
355+
call void @use(i64 %max)
356+
ret i64 %max
357+
}
358+

0 commit comments

Comments
 (0)