1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instsimplify -S | FileCheck %s
3target datalayout = "p:32:32-p1:64:64"
4
5define i1 @ptrtoint() {
6; CHECK-LABEL: @ptrtoint(
7; CHECK-NEXT:    ret i1 false
8;
9  %a = alloca i8
10  %tmp = ptrtoint i8* %a to i32
11  %r = icmp eq i32 %tmp, 0
12  ret i1 %r
13}
14
15define i1 @bitcast() {
16; CHECK-LABEL: @bitcast(
17; CHECK-NEXT:    ret i1 false
18;
19  %a = alloca i32
20  %b = alloca i64
21  %x = bitcast i32* %a to i8*
22  %y = bitcast i64* %b to i8*
23  %cmp = icmp eq i8* %x, %y
24  ret i1 %cmp
25}
26
27define i1 @gep() {
28; CHECK-LABEL: @gep(
29; CHECK-NEXT:    ret i1 false
30;
31  %a = alloca [3 x i8], align 8
32  %x = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
33  %cmp = icmp eq i8* %x, null
34  ret i1 %cmp
35}
36
37define i1 @gep2() {
38; CHECK-LABEL: @gep2(
39; CHECK-NEXT:    ret i1 true
40;
41  %a = alloca [3 x i8], align 8
42  %x = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
43  %y = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
44  %cmp = icmp eq i8* %x, %y
45  ret i1 %cmp
46}
47
48; PR11238
49%gept = type { i32, i32 }
50@gepy = global %gept zeroinitializer, align 8
51@gepz = extern_weak global %gept
52
53define i1 @gep3() {
54; CHECK-LABEL: @gep3(
55; CHECK-NEXT:    ret i1 false
56;
57  %x = alloca %gept, align 8
58  %a = getelementptr %gept, %gept* %x, i64 0, i32 0
59  %b = getelementptr %gept, %gept* %x, i64 0, i32 1
60  %equal = icmp eq i32* %a, %b
61  ret i1 %equal
62}
63
64define i1 @gep4() {
65; CHECK-LABEL: @gep4(
66; CHECK-NEXT:    ret i1 false
67;
68  %x = alloca %gept, align 8
69  %a = getelementptr %gept, %gept* @gepy, i64 0, i32 0
70  %b = getelementptr %gept, %gept* @gepy, i64 0, i32 1
71  %equal = icmp eq i32* %a, %b
72  ret i1 %equal
73}
74
75@a = common global [1 x i32] zeroinitializer, align 4
76
77define i1 @PR31262() {
78; CHECK-LABEL: @PR31262(
79; CHECK-NEXT:    ret i1 icmp uge (i32* getelementptr ([1 x i32], [1 x i32]* @a, i32 0, i32 undef), i32* getelementptr inbounds ([1 x i32], [1 x i32]* @a, i32 0, i32 0))
80;
81  %idx = getelementptr inbounds [1 x i32], [1 x i32]* @a, i64 0, i64 undef
82  %cmp = icmp uge i32* %idx, getelementptr inbounds ([1 x i32], [1 x i32]* @a, i32 0, i32 0)
83  ret i1 %cmp
84}
85
86define i1 @gep5() {
87; CHECK-LABEL: @gep5(
88; CHECK-NEXT:    ret i1 false
89;
90  %x = alloca %gept, align 8
91  %a = getelementptr inbounds %gept, %gept* %x, i64 0, i32 1
92  %b = getelementptr %gept, %gept* @gepy, i64 0, i32 0
93  %equal = icmp eq i32* %a, %b
94  ret i1 %equal
95}
96
97define i1 @gep6(%gept* %x) {
98; Same as @gep3 but potentially null.
99; CHECK-LABEL: @gep6(
100; CHECK-NEXT:    ret i1 false
101;
102  %a = getelementptr %gept, %gept* %x, i64 0, i32 0
103  %b = getelementptr %gept, %gept* %x, i64 0, i32 1
104  %equal = icmp eq i32* %a, %b
105  ret i1 %equal
106}
107
108define i1 @gep7(%gept* %x) {
109; CHECK-LABEL: @gep7(
110; CHECK-NEXT:    [[A:%.*]] = getelementptr [[GEPT:%.*]], %gept* [[X:%.*]], i64 0, i32 0
111; CHECK-NEXT:    [[EQUAL:%.*]] = icmp eq i32* [[A]], getelementptr (%gept, %gept* @gepz, i32 0, i32 0)
112; CHECK-NEXT:    ret i1 [[EQUAL]]
113;
114  %a = getelementptr %gept, %gept* %x, i64 0, i32 0
115  %b = getelementptr %gept, %gept* @gepz, i64 0, i32 0
116  %equal = icmp eq i32* %a, %b
117  ret i1 %equal
118}
119
120define i1 @gep8(%gept* %x) {
121; CHECK-LABEL: @gep8(
122; CHECK-NEXT:    [[A:%.*]] = getelementptr [[GEPT:%.*]], %gept* [[X:%.*]], i32 1
123; CHECK-NEXT:    [[B:%.*]] = getelementptr [[GEPT]], %gept* [[X]], i32 -1
124; CHECK-NEXT:    [[EQUAL:%.*]] = icmp ugt %gept* [[A]], [[B]]
125; CHECK-NEXT:    ret i1 [[EQUAL]]
126;
127  %a = getelementptr %gept, %gept* %x, i32 1
128  %b = getelementptr %gept, %gept* %x, i32 -1
129  %equal = icmp ugt %gept* %a, %b
130  ret i1 %equal
131}
132
133define i1 @gep9(i8* %ptr) {
134; CHECK-LABEL: @gep9(
135; CHECK-NEXT:  entry:
136; CHECK-NEXT:    ret i1 true
137;
138entry:
139  %first1 = getelementptr inbounds i8, i8* %ptr, i32 0
140  %first2 = getelementptr inbounds i8, i8* %first1, i32 1
141  %first3 = getelementptr inbounds i8, i8* %first2, i32 2
142  %first4 = getelementptr inbounds i8, i8* %first3, i32 4
143  %last1 = getelementptr inbounds i8, i8* %first2, i32 48
144  %last2 = getelementptr inbounds i8, i8* %last1, i32 8
145  %last3 = getelementptr inbounds i8, i8* %last2, i32 -4
146  %last4 = getelementptr inbounds i8, i8* %last3, i32 -4
147  %first.int = ptrtoint i8* %first4 to i32
148  %last.int = ptrtoint i8* %last4 to i32
149  %cmp = icmp ne i32 %last.int, %first.int
150  ret i1 %cmp
151}
152
153define i1 @gep10(i8* %ptr) {
154; CHECK-LABEL: @gep10(
155; CHECK-NEXT:  entry:
156; CHECK-NEXT:    ret i1 true
157;
158entry:
159  %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
160  %first2 = getelementptr inbounds i8, i8* %first1, i32 44
161  %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
162  %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
163  %first.int = ptrtoint i8* %first2 to i32
164  %last.int = ptrtoint i8* %last2 to i32
165  %cmp = icmp eq i32 %last.int, %first.int
166  ret i1 %cmp
167}
168
169define i1 @gep11(i8* %ptr) {
170; CHECK-LABEL: @gep11(
171; CHECK-NEXT:  entry:
172; CHECK-NEXT:    ret i1 true
173;
174entry:
175  %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
176  %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
177  %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
178  %cmp = icmp ult i8* %first1, %last2
179  ret i1 %cmp
180}
181
182define i1 @gep12(i8* %ptr) {
183; CHECK-LABEL: @gep12(
184; CHECK-NEXT:  entry:
185; CHECK-NEXT:    [[FIRST1:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i32 -2
186; CHECK-NEXT:    [[LAST1:%.*]] = getelementptr inbounds i8, i8* [[PTR]], i32 48
187; CHECK-NEXT:    [[LAST2:%.*]] = getelementptr inbounds i8, i8* [[LAST1]], i32 -6
188; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8* [[FIRST1]], [[LAST2]]
189; CHECK-NEXT:    ret i1 [[CMP]]
190;
191entry:
192  %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
193  %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
194  %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
195  %cmp = icmp slt i8* %first1, %last2
196  ret i1 %cmp
197}
198
199define i1 @gep13(i8* %ptr) {
200; CHECK-LABEL: @gep13(
201; CHECK-NEXT:    ret i1 false
202;
203; We can prove this GEP is non-null because it is inbounds.
204  %x = getelementptr inbounds i8, i8* %ptr, i32 1
205  %cmp = icmp eq i8* %x, null
206  ret i1 %cmp
207}
208
209define i1 @gep13_no_null_opt(i8* %ptr) #0 {
210; We can't prove this GEP is non-null.
211; CHECK-LABEL: @gep13_no_null_opt(
212; CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i32 1
213; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[X]], null
214; CHECK-NEXT:    ret i1 [[CMP]]
215;
216  %x = getelementptr inbounds i8, i8* %ptr, i32 1
217  %cmp = icmp eq i8* %x, null
218  ret i1 %cmp
219}
220
221define i1 @gep14({ {}, i8 }* %ptr) {
222; CHECK-LABEL: @gep14(
223; CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds { {}, i8 }, { {}, i8 }* [[PTR:%.*]], i32 0, i32 1
224; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[X]], null
225; CHECK-NEXT:    ret i1 [[CMP]]
226;
227; We can't simplify this because the offset of one in the GEP actually doesn't
228; move the pointer.
229  %x = getelementptr inbounds { {}, i8 }, { {}, i8 }* %ptr, i32 0, i32 1
230  %cmp = icmp eq i8* %x, null
231  ret i1 %cmp
232}
233
234define i1 @gep15({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) {
235; CHECK-LABEL: @gep15(
236; CHECK-NEXT:    ret i1 false
237;
238; We can prove this GEP is non-null even though there is a user value, as we
239; would necessarily violate inbounds on one side or the other.
240  %x = getelementptr inbounds { {}, [4 x {i8, i8}]}, { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
241  %cmp = icmp eq i8* %x, null
242  ret i1 %cmp
243}
244
245define i1 @gep15_no_null_opt({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) #0 {
246; We can't prove this GEP is non-null.
247; CHECK-LABEL: @gep15_no_null_opt(
248; CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds { {}, [4 x { i8, i8 }] }, { {}, [4 x { i8, i8 }] }* [[PTR:%.*]], i32 0, i32 1, i32 [[Y:%.*]], i32 1
249; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[X]], null
250; CHECK-NEXT:    ret i1 [[CMP]]
251;
252  %x = getelementptr inbounds { {}, [4 x {i8, i8}]}, { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
253  %cmp = icmp eq i8* %x, null
254  ret i1 %cmp
255}
256
257define i1 @gep16(i8* %ptr, i32 %a) {
258; CHECK-LABEL: @gep16(
259; CHECK-NEXT:    ret i1 false
260;
261; We can prove this GEP is non-null because it is inbounds and because we know
262; %b is non-zero even though we don't know its value.
263  %b = or i32 %a, 1
264  %x = getelementptr inbounds i8, i8* %ptr, i32 %b
265  %cmp = icmp eq i8* %x, null
266  ret i1 %cmp
267}
268
269define i1 @gep16_no_null_opt(i8* %ptr, i32 %a) #0 {
270; We can't prove this GEP is non-null.
271; CHECK-LABEL: @gep16_no_null_opt(
272; CHECK-NEXT:    [[B:%.*]] = or i32 [[A:%.*]], 1
273; CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i32 [[B]]
274; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[X]], null
275; CHECK-NEXT:    ret i1 [[CMP]]
276;
277  %b = or i32 %a, 1
278  %x = getelementptr inbounds i8, i8* %ptr, i32 %b
279  %cmp = icmp eq i8* %x, null
280  ret i1 %cmp
281}
282
283define i1 @gep17() {
284; CHECK-LABEL: @gep17(
285; CHECK-NEXT:    ret i1 true
286;
287  %alloca = alloca i32, align 4
288  %bc = bitcast i32* %alloca to [4 x i8]*
289  %gep1 = getelementptr inbounds i32, i32* %alloca, i32 1
290  %pti1 = ptrtoint i32* %gep1 to i32
291  %gep2 = getelementptr inbounds [4 x i8], [4 x i8]* %bc, i32 0, i32 1
292  %pti2 = ptrtoint i8* %gep2 to i32
293  %cmp = icmp ugt i32 %pti1, %pti2
294  ret i1 %cmp
295}
296
297define i1 @gep_same_base_constant_indices(i8* %a) {
298; CHECK-LABEL: @gep_same_base_constant_indices(
299; CHECK-NEXT:    ret i1 true
300;
301  %arrayidx1 = getelementptr inbounds i8, i8* %a, i64 1
302  %arrayidx2 = getelementptr inbounds i8, i8* %a, i64 10
303  %cmp = icmp slt i8* %arrayidx1, %arrayidx2
304  ret i1 %cmp
305}
306
307define i1 @zext(i32 %x) {
308; CHECK-LABEL: @zext(
309; CHECK-NEXT:    ret i1 true
310;
311  %e1 = zext i32 %x to i64
312  %e2 = zext i32 %x to i64
313  %r = icmp eq i64 %e1, %e2
314  ret i1 %r
315}
316
317define i1 @zext2(i1 %x) {
318; CHECK-LABEL: @zext2(
319; CHECK-NEXT:    ret i1 [[X:%.*]]
320;
321  %e = zext i1 %x to i32
322  %c = icmp ne i32 %e, 0
323  ret i1 %c
324}
325
326define i1 @zext3() {
327; CHECK-LABEL: @zext3(
328; CHECK-NEXT:    ret i1 true
329;
330  %e = zext i1 1 to i32
331  %c = icmp ne i32 %e, 0
332  ret i1 %c
333}
334
335define i1 @sext(i32 %x) {
336; CHECK-LABEL: @sext(
337; CHECK-NEXT:    ret i1 true
338;
339  %e1 = sext i32 %x to i64
340  %e2 = sext i32 %x to i64
341  %r = icmp eq i64 %e1, %e2
342  ret i1 %r
343}
344
345define i1 @sext2(i1 %x) {
346; CHECK-LABEL: @sext2(
347; CHECK-NEXT:    ret i1 [[X:%.*]]
348;
349  %e = sext i1 %x to i32
350  %c = icmp ne i32 %e, 0
351  ret i1 %c
352}
353
354define i1 @sext3() {
355; CHECK-LABEL: @sext3(
356; CHECK-NEXT:    ret i1 true
357;
358  %e = sext i1 1 to i32
359  %c = icmp ne i32 %e, 0
360  ret i1 %c
361}
362
363define i1 @add(i32 %x, i32 %y) {
364; CHECK-LABEL: @add(
365; CHECK-NEXT:    ret i1 false
366;
367  %l = lshr i32 %x, 1
368  %q = lshr i32 %y, 1
369  %r = or i32 %q, 1
370  %s = add i32 %l, %r
371  %c = icmp eq i32 %s, 0
372  ret i1 %c
373}
374
375define i1 @addv(<2 x i32> %x, <2 x i32> %y) {
376; CHECK-LABEL: @addv(
377; CHECK-NEXT:    ret i1 false
378;
379  %l = lshr <2 x i32> %x, <i32 1, i32 0>
380  %q = lshr <2 x i32> %y, <i32 1, i32 0>
381  %r = or <2 x i32> %q, <i32 1, i32 0>
382  %s = add <2 x i32> %l, %r
383  %e = extractelement <2 x i32> %s, i32 0
384  %c = icmp eq i32 %e, 0
385  ret i1 %c
386}
387
388define i1 @add2(i8 %x, i8 %y) {
389; CHECK-LABEL: @add2(
390; CHECK-NEXT:    ret i1 false
391;
392  %l = or i8 %x, 128
393  %r = or i8 %y, 129
394  %s = add i8 %l, %r
395  %c = icmp eq i8 %s, 0
396  ret i1 %c
397}
398
399define i1 @add2v(<2 x i8> %x, <2 x i8> %y) {
400; CHECK-LABEL: @add2v(
401; CHECK-NEXT:    ret i1 false
402;
403  %l = or <2 x i8> %x, <i8 0, i8 128>
404  %r = or <2 x i8> %y, <i8 0, i8 129>
405  %s = add <2 x i8> %l, %r
406  %e = extractelement <2 x i8> %s, i32 1
407  %c = icmp eq i8 %e, 0
408  ret i1 %c
409}
410
411define i1 @add3(i8 %x, i8 %y) {
412; CHECK-LABEL: @add3(
413; CHECK-NEXT:    [[L:%.*]] = zext i8 [[X:%.*]] to i32
414; CHECK-NEXT:    [[R:%.*]] = zext i8 [[Y:%.*]] to i32
415; CHECK-NEXT:    [[S:%.*]] = add i32 [[L]], [[R]]
416; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[S]], 0
417; CHECK-NEXT:    ret i1 [[C]]
418;
419  %l = zext i8 %x to i32
420  %r = zext i8 %y to i32
421  %s = add i32 %l, %r
422  %c = icmp eq i32 %s, 0
423  ret i1 %c
424}
425
426define i1 @add4(i32 %x, i32 %y) {
427; CHECK-LABEL: @add4(
428; CHECK-NEXT:    ret i1 true
429;
430  %z = add nsw i32 %y, 1
431  %s1 = add nsw i32 %x, %y
432  %s2 = add nsw i32 %x, %z
433  %c = icmp slt i32 %s1, %s2
434  ret i1 %c
435}
436
437define i1 @add5(i32 %x, i32 %y) {
438; CHECK-LABEL: @add5(
439; CHECK-NEXT:    ret i1 true
440;
441  %z = add nuw i32 %y, 1
442  %s1 = add nuw i32 %x, %z
443  %s2 = add nuw i32 %x, %y
444  %c = icmp ugt i32 %s1, %s2
445  ret i1 %c
446}
447
448define i1 @add6(i64 %A, i64 %B) {
449; CHECK-LABEL: @add6(
450; CHECK-NEXT:    ret i1 true
451;
452  %s1 = add i64 %A, %B
453  %s2 = add i64 %B, %A
454  %cmp = icmp eq i64 %s1, %s2
455  ret i1 %cmp
456}
457
458define i1 @addpowtwo(i32 %x, i32 %y) {
459; CHECK-LABEL: @addpowtwo(
460; CHECK-NEXT:    ret i1 false
461;
462  %l = lshr i32 %x, 1
463  %r = shl i32 1, %y
464  %s = add i32 %l, %r
465  %c = icmp eq i32 %s, 0
466  ret i1 %c
467}
468
469define i1 @addpowtwov(<2 x i32> %x, <2 x i32> %y) {
470; CHECK-LABEL: @addpowtwov(
471; CHECK-NEXT:    [[L:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 1, i32 0>
472; CHECK-NEXT:    [[R:%.*]] = shl <2 x i32> <i32 1, i32 0>, [[Y:%.*]]
473; CHECK-NEXT:    [[S:%.*]] = add <2 x i32> [[L]], [[R]]
474; CHECK-NEXT:    [[E:%.*]] = extractelement <2 x i32> [[S]], i32 0
475; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[E]], 0
476; CHECK-NEXT:    ret i1 [[C]]
477;
478  %l = lshr <2 x i32> %x, <i32 1, i32 0>
479  %r = shl <2 x i32> <i32 1, i32 0>, %y
480  %s = add <2 x i32> %l, %r
481  %e = extractelement <2 x i32> %s, i32 0
482  %c = icmp eq i32 %e, 0
483  ret i1 %c
484}
485
486define i1 @or(i32 %x) {
487; CHECK-LABEL: @or(
488; CHECK-NEXT:    ret i1 false
489;
490  %o = or i32 %x, 1
491  %c = icmp eq i32 %o, 0
492  ret i1 %c
493}
494
495; Do not simplify if we cannot guarantee that the ConstantExpr is a non-zero
496; constant.
497@GV = common global i32* null
498define i1 @or_constexp(i32 %x) {
499; CHECK-LABEL: @or_constexp(
500; CHECK-NEXT:  entry:
501; CHECK-NEXT:    [[O:%.*]] = or i32 [[X:%.*]], and (i32 ptrtoint (i32** @GV to i32), i32 32)
502; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[O]], 0
503; CHECK-NEXT:    ret i1 [[C]]
504;
505entry:
506  %0 = and i32 ptrtoint (i32** @GV to i32), 32
507  %o = or i32 %x, %0
508  %c = icmp eq i32 %o, 0
509  ret i1 %c
510}
511
512define i1 @shl1(i32 %x) {
513; CHECK-LABEL: @shl1(
514; CHECK-NEXT:    ret i1 false
515;
516  %s = shl i32 1, %x
517  %c = icmp eq i32 %s, 0
518  ret i1 %c
519}
520
521define i1 @lshr1(i32 %x) {
522; CHECK-LABEL: @lshr1(
523; CHECK-NEXT:    ret i1 false
524;
525  %s = lshr i32 -1, %x
526  %c = icmp eq i32 %s, 0
527  ret i1 %c
528}
529
530define i1 @lshr3(i32 %x) {
531; CHECK-LABEL: @lshr3(
532; CHECK-NEXT:    ret i1 true
533;
534  %s = lshr i32 %x, %x
535  %c = icmp eq i32 %s, 0
536  ret i1 %c
537}
538
539define i1 @lshr4(i32 %X, i32 %Y) {
540; CHECK-LABEL: @lshr4(
541; CHECK-NEXT:    ret i1 true
542;
543  %A = lshr i32 %X, %Y
544  %C = icmp ule i32 %A, %X
545  ret i1 %C
546}
547
548define i1 @lshr5(i32 %X, i32 %Y) {
549; CHECK-LABEL: @lshr5(
550; CHECK-NEXT:    ret i1 false
551;
552  %A = lshr i32 %X, %Y
553  %C = icmp ugt i32 %A, %X
554  ret i1 %C
555}
556
557define i1 @lshr6(i32 %X, i32 %Y) {
558; CHECK-LABEL: @lshr6(
559; CHECK-NEXT:    ret i1 false
560;
561  %A = lshr i32 %X, %Y
562  %C = icmp ult i32 %X, %A
563  ret i1 %C
564}
565
566define i1 @lshr7(i32 %X, i32 %Y) {
567; CHECK-LABEL: @lshr7(
568; CHECK-NEXT:    ret i1 true
569;
570  %A = lshr i32 %X, %Y
571  %C = icmp uge i32 %X, %A
572  ret i1 %C
573}
574
575define i1 @ashr1(i32 %x) {
576; CHECK-LABEL: @ashr1(
577; CHECK-NEXT:    ret i1 false
578;
579  %s = ashr i32 -1, %x
580  %c = icmp eq i32 %s, 0
581  ret i1 %c
582}
583
584define i1 @ashr3(i32 %x) {
585; CHECK-LABEL: @ashr3(
586; CHECK-NEXT:    ret i1 true
587;
588  %s = ashr i32 %x, %x
589  %c = icmp eq i32 %s, 0
590  ret i1 %c
591}
592
593define i1 @select1(i1 %cond) {
594; CHECK-LABEL: @select1(
595; CHECK-NEXT:    ret i1 [[COND:%.*]]
596;
597  %s = select i1 %cond, i32 1, i32 0
598  %c = icmp eq i32 %s, 1
599  ret i1 %c
600}
601
602define i1 @select2(i1 %cond) {
603; CHECK-LABEL: @select2(
604; CHECK-NEXT:    ret i1 [[COND:%.*]]
605;
606  %x = zext i1 %cond to i32
607  %s = select i1 %cond, i32 %x, i32 0
608  %c = icmp ne i32 %s, 0
609  ret i1 %c
610}
611
612define i1 @select3(i1 %cond) {
613; CHECK-LABEL: @select3(
614; CHECK-NEXT:    ret i1 [[COND:%.*]]
615;
616  %x = zext i1 %cond to i32
617  %s = select i1 %cond, i32 1, i32 %x
618  %c = icmp ne i32 %s, 0
619  ret i1 %c
620}
621
622define i1 @select4(i1 %cond) {
623; CHECK-LABEL: @select4(
624; CHECK-NEXT:    ret i1 [[COND:%.*]]
625;
626  %invert = xor i1 %cond, 1
627  %s = select i1 %invert, i32 0, i32 1
628  %c = icmp ne i32 %s, 0
629  ret i1 %c
630}
631
632define i1 @select5(i32 %x) {
633; CHECK-LABEL: @select5(
634; CHECK-NEXT:    ret i1 false
635;
636  %c = icmp eq i32 %x, 0
637  %s = select i1 %c, i32 1, i32 %x
638  %c2 = icmp eq i32 %s, 0
639  ret i1 %c2
640}
641
642define i1 @select6(i32 %x) {
643; CHECK-LABEL: @select6(
644; CHECK-NEXT:    [[C:%.*]] = icmp sgt i32 [[X:%.*]], 0
645; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 [[X]], i32 4
646; CHECK-NEXT:    [[C2:%.*]] = icmp eq i32 [[S]], 0
647; CHECK-NEXT:    ret i1 [[C2]]
648;
649  %c = icmp sgt i32 %x, 0
650  %s = select i1 %c, i32 %x, i32 4
651  %c2 = icmp eq i32 %s, 0
652  ret i1 %c2
653}
654
655define i1 @urem1(i32 %X, i32 %Y) {
656; CHECK-LABEL: @urem1(
657; CHECK-NEXT:    ret i1 true
658;
659  %A = urem i32 %X, %Y
660  %B = icmp ult i32 %A, %Y
661  ret i1 %B
662}
663
664define i1 @urem2(i32 %X, i32 %Y) {
665; CHECK-LABEL: @urem2(
666; CHECK-NEXT:    ret i1 false
667;
668  %A = urem i32 %X, %Y
669  %B = icmp eq i32 %A, %Y
670  ret i1 %B
671}
672
673define i1 @urem4(i32 %X) {
674; CHECK-LABEL: @urem4(
675; CHECK-NEXT:    [[A:%.*]] = urem i32 [[X:%.*]], 15
676; CHECK-NEXT:    [[B:%.*]] = icmp ult i32 [[A]], 10
677; CHECK-NEXT:    ret i1 [[B]]
678;
679  %A = urem i32 %X, 15
680  %B = icmp ult i32 %A, 10
681  ret i1 %B
682}
683
684define i1 @urem5(i16 %X, i32 %Y) {
685; CHECK-LABEL: @urem5(
686; CHECK-NEXT:    [[A:%.*]] = zext i16 [[X:%.*]] to i32
687; CHECK-NEXT:    [[B:%.*]] = urem i32 [[A]], [[Y:%.*]]
688; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[B]], [[Y]]
689; CHECK-NEXT:    ret i1 [[C]]
690;
691  %A = zext i16 %X to i32
692  %B = urem i32 %A, %Y
693  %C = icmp slt i32 %B, %Y
694  ret i1 %C
695}
696
697define i1 @urem6(i32 %X, i32 %Y) {
698; CHECK-LABEL: @urem6(
699; CHECK-NEXT:    ret i1 true
700;
701  %A = urem i32 %X, %Y
702  %B = icmp ugt i32 %Y, %A
703  ret i1 %B
704}
705
706define i1 @urem7(i32 %X) {
707; CHECK-LABEL: @urem7(
708; CHECK-NEXT:    [[A:%.*]] = urem i32 1, [[X:%.*]]
709; CHECK-NEXT:    [[B:%.*]] = icmp sgt i32 [[A]], [[X]]
710; CHECK-NEXT:    ret i1 [[B]]
711;
712  %A = urem i32 1, %X
713  %B = icmp sgt i32 %A, %X
714  ret i1 %B
715}
716
717define i1 @urem8(i8 %X, i8 %Y) {
718; CHECK-LABEL: @urem8(
719; CHECK-NEXT:    ret i1 true
720;
721  %A = urem i8 %X, %Y
722  %B = icmp ule i8 %A, %X
723  ret i1 %B
724}
725
726define i1 @urem9(i8 %X, i8 %Y) {
727; CHECK-LABEL: @urem9(
728; CHECK-NEXT:    ret i1 false
729;
730  %A = urem i8 %X, %Y
731  %B = icmp ugt i8 %A, %X
732  ret i1 %B
733}
734
735define i1 @urem10(i8 %X, i8 %Y) {
736; CHECK-LABEL: @urem10(
737; CHECK-NEXT:    ret i1 true
738;
739  %A = urem i8 %X, %Y
740  %B = icmp uge i8 %X, %A
741  ret i1 %B
742}
743
744define i1 @urem11(i8 %X, i8 %Y) {
745; CHECK-LABEL: @urem11(
746; CHECK-NEXT:    ret i1 false
747;
748  %A = urem i8 %X, %Y
749  %B = icmp ult i8 %X, %A
750  ret i1 %B
751}
752
753; PR9343 #15
754define i1 @srem2(i16 %X, i32 %Y) {
755; CHECK-LABEL: @srem2(
756; CHECK-NEXT:    ret i1 false
757;
758  %A = zext i16 %X to i32
759  %B = add nsw i32 %A, 1
760  %C = srem i32 %B, %Y
761  %D = icmp slt i32 %C, 0
762  ret i1 %D
763}
764
765define i1 @srem2v(<2 x i16> %X, <2 x i32> %Y) {
766; CHECK-LABEL: @srem2v(
767; CHECK-NEXT:    ret i1 false
768;
769  %A = zext <2 x i16> %X to <2 x i32>
770  %B = add nsw <2 x i32> %A, <i32 1, i32 0>
771  %C = srem <2 x i32> %B, %Y
772  %D = extractelement <2 x i32> %C, i32 0
773  %E = icmp slt i32 %D, 0
774  ret i1 %E
775}
776
777define i1 @srem3(i16 %X, i32 %Y) {
778; CHECK-LABEL: @srem3(
779; CHECK-NEXT:    ret i1 false
780;
781  %A = zext i16 %X to i32
782  %B = or i32 2147483648, %A
783  %C = sub nsw i32 1, %B
784  %D = srem i32 %C, %Y
785  %E = icmp slt i32 %D, 0
786  ret i1 %E
787}
788
789define i1 @srem3v(<2 x i16> %X, <2 x i32> %Y) {
790; CHECK-LABEL: @srem3v(
791; CHECK-NEXT:    ret i1 false
792;
793  %A = zext <2 x i16> %X to <2 x i32>
794  %B = or <2 x i32> <i32 1, i32 2147483648>, %A
795  %C = sub nsw <2 x i32> <i32 0, i32 1>, %B
796  %D = srem <2 x i32> %C, %Y
797  %E = extractelement <2 x i32> %C, i32 1
798  %F = icmp slt i32 %E, 0
799  ret i1 %F
800}
801
802define i1 @udiv2(i32 %Z) {
803; CHECK-LABEL: @udiv2(
804; CHECK-NEXT:    ret i1 true
805;
806  %A = udiv exact i32 10, %Z
807  %B = udiv exact i32 20, %Z
808  %C = icmp ult i32 %A, %B
809  ret i1 %C
810}
811
812; Exact sdiv and equality preds can simplify.
813
814define i1 @sdiv_exact_equality(i32 %Z) {
815; CHECK-LABEL: @sdiv_exact_equality(
816; CHECK-NEXT:    ret i1 false
817;
818  %A = sdiv exact i32 10, %Z
819  %B = sdiv exact i32 20, %Z
820  %C = icmp eq i32 %A, %B
821  ret i1 %C
822}
823
824; But not other preds: PR32949 - https://bugs.llvm.org/show_bug.cgi?id=32949
825
826define i1 @sdiv_exact_not_equality(i32 %Z) {
827; CHECK-LABEL: @sdiv_exact_not_equality(
828; CHECK-NEXT:    [[A:%.*]] = sdiv exact i32 10, [[Z:%.*]]
829; CHECK-NEXT:    [[B:%.*]] = sdiv exact i32 20, [[Z]]
830; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[A]], [[B]]
831; CHECK-NEXT:    ret i1 [[C]]
832;
833  %A = sdiv exact i32 10, %Z
834  %B = sdiv exact i32 20, %Z
835  %C = icmp ult i32 %A, %B
836  ret i1 %C
837}
838
839define i1 @udiv3(i32 %X, i32 %Y) {
840; CHECK-LABEL: @udiv3(
841; CHECK-NEXT:    ret i1 false
842;
843  %A = udiv i32 %X, %Y
844  %C = icmp ugt i32 %A, %X
845  ret i1 %C
846}
847
848define i1 @udiv4(i32 %X, i32 %Y) {
849; CHECK-LABEL: @udiv4(
850; CHECK-NEXT:    ret i1 true
851;
852  %A = udiv i32 %X, %Y
853  %C = icmp ule i32 %A, %X
854  ret i1 %C
855}
856
857; PR11340
858define i1 @udiv6(i32 %X) nounwind {
859; CHECK-LABEL: @udiv6(
860; CHECK-NEXT:    [[A:%.*]] = udiv i32 1, [[X:%.*]]
861; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[A]], 0
862; CHECK-NEXT:    ret i1 [[C]]
863;
864  %A = udiv i32 1, %X
865  %C = icmp eq i32 %A, 0
866  ret i1 %C
867}
868
869define i1 @udiv7(i32 %X, i32 %Y) {
870; CHECK-LABEL: @udiv7(
871; CHECK-NEXT:    ret i1 false
872;
873  %A = udiv i32 %X, %Y
874  %C = icmp ult i32 %X, %A
875  ret i1 %C
876}
877
878define i1 @udiv8(i32 %X, i32 %Y) {
879; CHECK-LABEL: @udiv8(
880; CHECK-NEXT:    ret i1 true
881;
882  %A = udiv i32 %X, %Y
883  %C = icmp uge i32 %X, %A
884  ret i1 %C
885}
886
887; Square of a non-zero number is non-zero if there is no overflow.
888define i1 @mul1(i32 %X) {
889; CHECK-LABEL: @mul1(
890; CHECK-NEXT:    ret i1 false
891;
892  %Y = or i32 %X, 1
893  %M = mul nuw i32 %Y, %Y
894  %C = icmp eq i32 %M, 0
895  ret i1 %C
896}
897
898define i1 @mul1v(<2 x i32> %X) {
899; CHECK-LABEL: @mul1v(
900; CHECK-NEXT:    ret i1 false
901;
902  %Y = or <2 x i32> %X, <i32 1, i32 0>
903  %M = mul nuw <2 x i32> %Y, %Y
904  %E = extractelement <2 x i32> %M, i32 0
905  %C = icmp eq i32 %E, 0
906  ret i1 %C
907}
908
909; Square of a non-zero number is positive if there is no signed overflow.
910define i1 @mul2(i32 %X) {
911; CHECK-LABEL: @mul2(
912; CHECK-NEXT:    ret i1 true
913;
914  %Y = or i32 %X, 1
915  %M = mul nsw i32 %Y, %Y
916  %C = icmp sgt i32 %M, 0
917  ret i1 %C
918}
919
920define i1 @mul2v(<2 x i32> %X) {
921; CHECK-LABEL: @mul2v(
922; CHECK-NEXT:    ret i1 true
923;
924  %Y = or <2 x i32> %X, <i32 0, i32 1>
925  %M = mul nsw <2 x i32> %Y, %Y
926  %E = extractelement <2 x i32> %M, i32 1
927  %C = icmp sgt i32 %E, 0
928  ret i1 %C
929}
930
931; Product of non-negative numbers is non-negative if there is no signed overflow.
932define i1 @mul3(i32 %X, i32 %Y) {
933; CHECK-LABEL: @mul3(
934; CHECK-NEXT:    ret i1 true
935;
936  %XX = mul nsw i32 %X, %X
937  %YY = mul nsw i32 %Y, %Y
938  %M = mul nsw i32 %XX, %YY
939  %C = icmp sge i32 %M, 0
940  ret i1 %C
941}
942
943define <2 x i1> @mul3v(<2 x i32> %X, <2 x i32> %Y) {
944; CHECK-LABEL: @mul3v(
945; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
946;
947  %XX = mul nsw <2 x i32> %X, %X
948  %YY = mul nsw <2 x i32> %Y, %Y
949  %M = mul nsw <2 x i32> %XX, %YY
950  %C = icmp sge <2 x i32> %M, zeroinitializer
951  ret <2 x i1> %C
952}
953
954define <2 x i1> @vectorselect1(<2 x i1> %cond) {
955; CHECK-LABEL: @vectorselect1(
956; CHECK-NEXT:    ret <2 x i1> [[COND:%.*]]
957;
958  %invert = xor <2 x i1> %cond, <i1 1, i1 1>
959  %s = select <2 x i1> %invert, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 1, i32 1>
960  %c = icmp ne <2 x i32> %s, <i32 0, i32 0>
961  ret <2 x i1> %c
962}
963
964; PR11948
965define <2 x i1> @vectorselectcrash(i32 %arg1) {
966; CHECK-LABEL: @vectorselectcrash(
967; CHECK-NEXT:    [[TOBOOL40:%.*]] = icmp ne i32 [[ARG1:%.*]], 0
968; CHECK-NEXT:    [[COND43:%.*]] = select i1 [[TOBOOL40]], <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
969; CHECK-NEXT:    [[CMP45:%.*]] = icmp ugt <2 x i16> [[COND43]], <i16 73, i16 21>
970; CHECK-NEXT:    ret <2 x i1> [[CMP45]]
971;
972  %tobool40 = icmp ne i32 %arg1, 0
973  %cond43 = select i1 %tobool40, <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
974  %cmp45 = icmp ugt <2 x i16> %cond43, <i16 73, i16 21>
975  ret <2 x i1> %cmp45
976}
977
978; PR12013
979define i1 @alloca_compare(i64 %idx) {
980; CHECK-LABEL: @alloca_compare(
981; CHECK-NEXT:    ret i1 false
982;
983  %sv = alloca { i32, i32, [124 x i32] }
984  %1 = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
985  %2 = icmp eq i32* %1, null
986  ret i1 %2
987}
988
989define i1 @alloca_compare_no_null_opt(i64 %idx) #0 {
990; CHECK-LABEL: @alloca_compare_no_null_opt(
991; CHECK-NEXT:    [[SV:%.*]] = alloca { i32, i32, [124 x i32] }, align 8
992; CHECK-NEXT:    [[CMP:%.*]] = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* [[SV]], i32 0, i32 2, i64 [[IDX:%.*]]
993; CHECK-NEXT:    [[X:%.*]] = icmp eq i32* [[CMP]], null
994; CHECK-NEXT:    ret i1 [[X]]
995;
996  %sv = alloca { i32, i32, [124 x i32] }
997  %cmp = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
998  %X = icmp eq i32* %cmp, null
999  ret i1 %X
1000}
1001; PR12075
1002define i1 @infinite_gep() {
1003; CHECK-LABEL: @infinite_gep(
1004; CHECK-NEXT:    ret i1 true
1005; CHECK:       unreachableblock:
1006; CHECK-NEXT:    [[X:%.*]] = getelementptr i32, i32* [[X]], i32 1
1007; CHECK-NEXT:    [[Y:%.*]] = icmp eq i32* [[X]], null
1008; CHECK-NEXT:    ret i1 [[Y]]
1009;
1010  ret i1 1
1011
1012unreachableblock:
1013  %X = getelementptr i32, i32 *%X, i32 1
1014  %Y = icmp eq i32* %X, null
1015  ret i1 %Y
1016}
1017
1018; It's not valid to fold a comparison of an argument with an alloca, even though
1019; that's tempting. An argument can't *alias* an alloca, however the aliasing rule
1020; relies on restrictions against guessing an object's address and dereferencing.
1021; There are no restrictions against guessing an object's address and comparing.
1022
1023define i1 @alloca_argument_compare(i64* %arg) {
1024; CHECK-LABEL: @alloca_argument_compare(
1025; CHECK-NEXT:    [[ALLOC:%.*]] = alloca i64, align 8
1026; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64* [[ARG:%.*]], [[ALLOC]]
1027; CHECK-NEXT:    ret i1 [[CMP]]
1028;
1029  %alloc = alloca i64
1030  %cmp = icmp eq i64* %arg, %alloc
1031  ret i1 %cmp
1032}
1033
1034; As above, but with the operands reversed.
1035
1036define i1 @alloca_argument_compare_swapped(i64* %arg) {
1037; CHECK-LABEL: @alloca_argument_compare_swapped(
1038; CHECK-NEXT:    [[ALLOC:%.*]] = alloca i64, align 8
1039; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64* [[ALLOC]], [[ARG:%.*]]
1040; CHECK-NEXT:    ret i1 [[CMP]]
1041;
1042  %alloc = alloca i64
1043  %cmp = icmp eq i64* %alloc, %arg
1044  ret i1 %cmp
1045}
1046
1047; Don't assume that a noalias argument isn't equal to a global variable's
1048; address. This is an example where AliasAnalysis' NoAlias concept is
1049; different from actual pointer inequality.
1050
1051@y = external global i32
1052define zeroext i1 @external_compare(i32* noalias %x) {
1053; CHECK-LABEL: @external_compare(
1054; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32* [[X:%.*]], @y
1055; CHECK-NEXT:    ret i1 [[CMP]]
1056;
1057  %cmp = icmp eq i32* %x, @y
1058  ret i1 %cmp
1059}
1060
1061define i1 @alloca_gep(i64 %a, i64 %b) {
1062; CHECK-LABEL: @alloca_gep(
1063; CHECK-NEXT:    ret i1 false
1064;
1065; We can prove this GEP is non-null because it is inbounds and the pointer
1066; is non-null.
1067  %strs = alloca [1000 x [1001 x i8]], align 16
1068  %x = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
1069  %cmp = icmp eq i8* %x, null
1070  ret i1 %cmp
1071}
1072
1073define i1 @alloca_gep_no_null_opt(i64 %a, i64 %b) #0 {
1074; CHECK-LABEL: @alloca_gep_no_null_opt(
1075; CHECK-NEXT:    [[STRS:%.*]] = alloca [1000 x [1001 x i8]], align 16
1076; CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* [[STRS]], i64 0, i64 [[A:%.*]], i64 [[B:%.*]]
1077; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[X]], null
1078; CHECK-NEXT:    ret i1 [[CMP]]
1079;
1080; We can't prove this GEP is non-null.
1081  %strs = alloca [1000 x [1001 x i8]], align 16
1082  %x = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
1083  %cmp = icmp eq i8* %x, null
1084  ret i1 %cmp
1085}
1086
1087define i1 @non_inbounds_gep_compare(i64* %a) {
1088; CHECK-LABEL: @non_inbounds_gep_compare(
1089; CHECK-NEXT:    ret i1 true
1090;
1091; Equality compares with non-inbounds GEPs can be folded.
1092  %x = getelementptr i64, i64* %a, i64 42
1093  %y = getelementptr inbounds i64, i64* %x, i64 -42
1094  %z = getelementptr i64, i64* %a, i64 -42
1095  %w = getelementptr inbounds i64, i64* %z, i64 42
1096  %cmp = icmp eq i64* %y, %w
1097  ret i1 %cmp
1098}
1099
1100define i1 @non_inbounds_gep_compare2(i64* %a) {
1101; CHECK-LABEL: @non_inbounds_gep_compare2(
1102; CHECK-NEXT:    ret i1 true
1103;
1104; Equality compares with non-inbounds GEPs can be folded.
1105  %x = getelementptr i64, i64* %a, i64 4294967297
1106  %y = getelementptr i64, i64* %a, i64 1
1107  %cmp = icmp eq i64* %y, %y
1108  ret i1 %cmp
1109}
1110
1111define i1 @compare_always_true_slt(i16 %a) {
1112; CHECK-LABEL: @compare_always_true_slt(
1113; CHECK-NEXT:    ret i1 true
1114;
1115  %t1 = zext i16 %a to i32
1116  %t2 = sub i32 0, %t1
1117  %t3 = icmp slt i32 %t2, 1
1118  ret i1 %t3
1119}
1120
1121define <2 x i1> @compare_always_true_slt_splat(<2 x i16> %a) {
1122; CHECK-LABEL: @compare_always_true_slt_splat(
1123; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1124;
1125  %t1 = zext <2 x i16> %a to <2 x i32>
1126  %t2 = sub <2 x i32> zeroinitializer, %t1
1127  %t3 = icmp slt <2 x i32> %t2, <i32 1, i32 1>
1128  ret <2 x i1> %t3
1129}
1130
1131define i1 @compare_always_true_sle(i16 %a) {
1132; CHECK-LABEL: @compare_always_true_sle(
1133; CHECK-NEXT:    ret i1 true
1134;
1135  %t1 = zext i16 %a to i32
1136  %t2 = sub i32 0, %t1
1137  %t3 = icmp sle i32 %t2, 0
1138  ret i1 %t3
1139}
1140
1141define <2 x i1> @compare_always_true_sle_splat(<2 x i16> %a) {
1142; CHECK-LABEL: @compare_always_true_sle_splat(
1143; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1144;
1145  %t1 = zext <2 x i16> %a to <2 x i32>
1146  %t2 = sub <2 x i32> zeroinitializer, %t1
1147  %t3 = icmp sle <2 x i32> %t2, zeroinitializer
1148  ret <2 x i1> %t3
1149}
1150
1151define i1 @compare_always_false_sgt(i16 %a) {
1152; CHECK-LABEL: @compare_always_false_sgt(
1153; CHECK-NEXT:    ret i1 false
1154;
1155  %t1 = zext i16 %a to i32
1156  %t2 = sub i32 0, %t1
1157  %t3 = icmp sgt i32 %t2, 0
1158  ret i1 %t3
1159}
1160
1161define <2 x i1> @compare_always_false_sgt_splat(<2 x i16> %a) {
1162; CHECK-LABEL: @compare_always_false_sgt_splat(
1163; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1164;
1165  %t1 = zext <2 x i16> %a to <2 x i32>
1166  %t2 = sub <2 x i32> zeroinitializer, %t1
1167  %t3 = icmp sgt <2 x i32> %t2, zeroinitializer
1168  ret <2 x i1> %t3
1169}
1170
1171define i1 @compare_always_false_sge(i16 %a) {
1172; CHECK-LABEL: @compare_always_false_sge(
1173; CHECK-NEXT:    ret i1 false
1174;
1175  %t1 = zext i16 %a to i32
1176  %t2 = sub i32 0, %t1
1177  %t3 = icmp sge i32 %t2, 1
1178  ret i1 %t3
1179}
1180
1181define <2 x i1> @compare_always_false_sge_splat(<2 x i16> %a) {
1182; CHECK-LABEL: @compare_always_false_sge_splat(
1183; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1184;
1185  %t1 = zext <2 x i16> %a to <2 x i32>
1186  %t2 = sub <2 x i32> zeroinitializer, %t1
1187  %t3 = icmp sge <2 x i32> %t2, <i32 1, i32 1>
1188  ret <2 x i1> %t3
1189}
1190
1191define i1 @compare_always_false_eq(i16 %a) {
1192; CHECK-LABEL: @compare_always_false_eq(
1193; CHECK-NEXT:    ret i1 false
1194;
1195  %t1 = zext i16 %a to i32
1196  %t2 = sub i32 0, %t1
1197  %t3 = icmp eq i32 %t2, 1
1198  ret i1 %t3
1199}
1200
1201define <2 x i1> @compare_always_false_eq_splat(<2 x i16> %a) {
1202; CHECK-LABEL: @compare_always_false_eq_splat(
1203; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1204;
1205  %t1 = zext <2 x i16> %a to <2 x i32>
1206  %t2 = sub <2 x i32> zeroinitializer, %t1
1207  %t3 = icmp eq <2 x i32> %t2, <i32 1, i32 1>
1208  ret <2 x i1> %t3
1209}
1210
1211define i1 @compare_always_true_ne(i16 %a) {
1212; CHECK-LABEL: @compare_always_true_ne(
1213; CHECK-NEXT:    ret i1 true
1214;
1215  %t1 = zext i16 %a to i32
1216  %t2 = sub i32 0, %t1
1217  %t3 = icmp ne i32 %t2, 1
1218  ret i1 %t3
1219}
1220
1221define <2 x i1> @compare_always_true_ne_splat(<2 x i16> %a) {
1222; CHECK-LABEL: @compare_always_true_ne_splat(
1223; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1224;
1225  %t1 = zext <2 x i16> %a to <2 x i32>
1226  %t2 = sub <2 x i32> zeroinitializer, %t1
1227  %t3 = icmp ne <2 x i32> %t2, <i32 1, i32 1>
1228  ret <2 x i1> %t3
1229}
1230
1231define i1 @lshr_ugt_false(i32 %a) {
1232; CHECK-LABEL: @lshr_ugt_false(
1233; CHECK-NEXT:    ret i1 false
1234;
1235  %shr = lshr i32 1, %a
1236  %cmp = icmp ugt i32 %shr, 1
1237  ret i1 %cmp
1238}
1239
1240define i1 @nonnull_arg(i32* nonnull %i) {
1241; CHECK-LABEL: @nonnull_arg(
1242; CHECK-NEXT:    ret i1 false
1243;
1244  %cmp = icmp eq i32* %i, null
1245  ret i1 %cmp
1246}
1247
1248define i1 @nonnull_arg_no_null_opt(i32* nonnull %i) #0 {
1249; CHECK-LABEL: @nonnull_arg_no_null_opt(
1250; CHECK-NEXT:    ret i1 false
1251;
1252  %cmp = icmp eq i32* %i, null
1253  ret i1 %cmp
1254}
1255
1256define i1 @nonnull_deref_arg(i32* dereferenceable(4) %i) {
1257; CHECK-LABEL: @nonnull_deref_arg(
1258; CHECK-NEXT:    ret i1 false
1259;
1260  %cmp = icmp eq i32* %i, null
1261  ret i1 %cmp
1262}
1263
1264define i1 @nonnull_deref_arg_no_null_opt(i32* dereferenceable(4) %i) #0 {
1265; CHECK-LABEL: @nonnull_deref_arg_no_null_opt(
1266; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32* [[I:%.*]], null
1267; CHECK-NEXT:    ret i1 [[CMP]]
1268;
1269  %cmp = icmp eq i32* %i, null
1270  ret i1 %cmp
1271}
1272define i1 @nonnull_deref_as_arg(i32 addrspace(1)* dereferenceable(4) %i) {
1273; CHECK-LABEL: @nonnull_deref_as_arg(
1274; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 addrspace(1)* [[I:%.*]], null
1275; CHECK-NEXT:    ret i1 [[CMP]]
1276;
1277  %cmp = icmp eq i32 addrspace(1)* %i, null
1278  ret i1 %cmp
1279}
1280
1281declare nonnull i32* @returns_nonnull_helper()
1282define i1 @returns_nonnull() {
1283; CHECK-LABEL: @returns_nonnull(
1284; CHECK-NEXT:    [[CALL:%.*]] = call nonnull i32* @returns_nonnull_helper()
1285; CHECK-NEXT:    ret i1 false
1286;
1287  %call = call nonnull i32* @returns_nonnull_helper()
1288  %cmp = icmp eq i32* %call, null
1289  ret i1 %cmp
1290}
1291
1292declare dereferenceable(4) i32* @returns_nonnull_deref_helper()
1293define i1 @returns_nonnull_deref() {
1294; CHECK-LABEL: @returns_nonnull_deref(
1295; CHECK-NEXT:    [[CALL:%.*]] = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
1296; CHECK-NEXT:    ret i1 false
1297;
1298  %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
1299  %cmp = icmp eq i32* %call, null
1300  ret i1 %cmp
1301}
1302
1303define i1 @returns_nonnull_deref_no_null_opt () #0 {
1304; CHECK-LABEL: @returns_nonnull_deref_no_null_opt(
1305; CHECK-NEXT:    [[CALL:%.*]] = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
1306; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32* [[CALL]], null
1307; CHECK-NEXT:    ret i1 [[CMP]]
1308;
1309  %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
1310  %cmp = icmp eq i32* %call, null
1311  ret i1 %cmp
1312}
1313
1314declare dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
1315define i1 @returns_nonnull_as_deref() {
1316; CHECK-LABEL: @returns_nonnull_as_deref(
1317; CHECK-NEXT:    [[CALL:%.*]] = call dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
1318; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 addrspace(1)* [[CALL]], null
1319; CHECK-NEXT:    ret i1 [[CMP]]
1320;
1321  %call = call dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
1322  %cmp = icmp eq i32 addrspace(1)* %call, null
1323  ret i1 %cmp
1324}
1325
1326define i1 @nonnull_load(i32** %addr) {
1327; CHECK-LABEL: @nonnull_load(
1328; CHECK-NEXT:    ret i1 false
1329;
1330  %ptr = load i32*, i32** %addr, !nonnull !{}
1331  %cmp = icmp eq i32* %ptr, null
1332  ret i1 %cmp
1333}
1334
1335define i1 @nonnull_load_as_outer(i32* addrspace(1)* %addr) {
1336; CHECK-LABEL: @nonnull_load_as_outer(
1337; CHECK-NEXT:    ret i1 false
1338;
1339  %ptr = load i32*, i32* addrspace(1)* %addr, !nonnull !{}
1340  %cmp = icmp eq i32* %ptr, null
1341  ret i1 %cmp
1342}
1343define i1 @nonnull_load_as_inner(i32 addrspace(1)** %addr) {
1344; CHECK-LABEL: @nonnull_load_as_inner(
1345; CHECK-NEXT:    ret i1 false
1346;
1347  %ptr = load i32 addrspace(1)*, i32 addrspace(1)** %addr, !nonnull !{}
1348  %cmp = icmp eq i32 addrspace(1)* %ptr, null
1349  ret i1 %cmp
1350}
1351
1352; If a bit is known to be zero for A and known to be one for B,
1353; then A and B cannot be equal.
1354define i1 @icmp_eq_const(i32 %a) {
1355; CHECK-LABEL: @icmp_eq_const(
1356; CHECK-NEXT:    ret i1 false
1357;
1358  %b = mul nsw i32 %a, -2
1359  %c = icmp eq i32 %b, 1
1360  ret i1 %c
1361}
1362
1363define <2 x i1> @icmp_eq_const_vec(<2 x i32> %a) {
1364; CHECK-LABEL: @icmp_eq_const_vec(
1365; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1366;
1367  %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
1368  %c = icmp eq <2 x i32> %b, <i32 1, i32 1>
1369  ret <2 x i1> %c
1370}
1371
1372define i1 @icmp_ne_const(i32 %a) {
1373; CHECK-LABEL: @icmp_ne_const(
1374; CHECK-NEXT:    ret i1 true
1375;
1376  %b = mul nsw i32 %a, -2
1377  %c = icmp ne i32 %b, 1
1378  ret i1 %c
1379}
1380
1381define <2 x i1> @icmp_ne_const_vec(<2 x i32> %a) {
1382; CHECK-LABEL: @icmp_ne_const_vec(
1383; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1384;
1385  %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
1386  %c = icmp ne <2 x i32> %b, <i32 1, i32 1>
1387  ret <2 x i1> %c
1388}
1389
1390define i1 @icmp_sdiv_int_min(i32 %a) {
1391; CHECK-LABEL: @icmp_sdiv_int_min(
1392; CHECK-NEXT:    [[DIV:%.*]] = sdiv i32 -2147483648, [[A:%.*]]
1393; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[DIV]], -1073741824
1394; CHECK-NEXT:    ret i1 [[CMP]]
1395;
1396  %div = sdiv i32 -2147483648, %a
1397  %cmp = icmp ne i32 %div, -1073741824
1398  ret i1 %cmp
1399
1400}
1401
1402define i1 @icmp_sdiv_pr20288(i64 %a) {
1403; CHECK-LABEL: @icmp_sdiv_pr20288(
1404; CHECK-NEXT:    [[DIV:%.*]] = sdiv i64 [[A:%.*]], -8589934592
1405; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824
1406; CHECK-NEXT:    ret i1 [[CMP]]
1407;
1408  %div = sdiv i64 %a, -8589934592
1409  %cmp = icmp ne i64 %div, 1073741824
1410  ret i1 %cmp
1411
1412}
1413
1414define i1 @icmp_sdiv_neg1(i64 %a) {
1415; CHECK-LABEL: @icmp_sdiv_neg1(
1416; CHECK-NEXT:    [[DIV:%.*]] = sdiv i64 [[A:%.*]], -1
1417; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824
1418; CHECK-NEXT:    ret i1 [[CMP]]
1419;
1420  %div = sdiv i64 %a, -1
1421  %cmp = icmp ne i64 %div, 1073741824
1422  ret i1 %cmp
1423
1424}
1425
1426define i1 @icmp_known_bits(i4 %x, i4 %y) {
1427; CHECK-LABEL: @icmp_known_bits(
1428; CHECK-NEXT:    ret i1 false
1429;
1430  %and1 = and i4 %y, -7
1431  %and2 = and i4 %x, -7
1432  %or1 = or i4 %and1, 2
1433  %or2 = or i4 %and2, 2
1434  %add = add i4 %or1, %or2
1435  %cmp = icmp eq i4 %add, 0
1436  ret i1 %cmp
1437}
1438
1439define i1 @icmp_known_bits_vec(<2 x i4> %x, <2 x i4> %y) {
1440; CHECK-LABEL: @icmp_known_bits_vec(
1441; CHECK-NEXT:    ret i1 false
1442;
1443  %and1 = and <2 x i4> %y, <i4 -7, i4 -1>
1444  %and2 = and <2 x i4> %x, <i4 -7, i4 -1>
1445  %or1 = or <2 x i4> %and1, <i4 2, i4 2>
1446  %or2 = or <2 x i4> %and2, <i4 2, i4 2>
1447  %add = add <2 x i4> %or1, %or2
1448  %ext = extractelement <2 x i4> %add,i32 0
1449  %cmp = icmp eq i4 %ext, 0
1450  ret i1 %cmp
1451}
1452
1453define i1 @icmp_shl_nuw_1(i64 %a) {
1454; CHECK-LABEL: @icmp_shl_nuw_1(
1455; CHECK-NEXT:    ret i1 true
1456;
1457  %shl = shl nuw i64 1, %a
1458  %cmp = icmp ne i64 %shl, 0
1459  ret i1 %cmp
1460}
1461
1462define i1 @icmp_shl_1_V_ugt_2147483648(i32 %V) {
1463; CHECK-LABEL: @icmp_shl_1_V_ugt_2147483648(
1464; CHECK-NEXT:    ret i1 false
1465;
1466  %shl = shl i32 1, %V
1467  %cmp = icmp ugt i32 %shl, 2147483648
1468  ret i1 %cmp
1469}
1470
1471define <2 x i1> @icmp_shl_1_ugt_signmask(<2 x i8> %V) {
1472; CHECK-LABEL: @icmp_shl_1_ugt_signmask(
1473; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1474;
1475  %shl = shl <2 x i8> <i8 1, i8 1>, %V
1476  %cmp = icmp ugt <2 x i8> %shl, <i8 128, i8 128>
1477  ret <2 x i1> %cmp
1478}
1479
1480define <2 x i1> @icmp_shl_1_ugt_signmask_undef(<2 x i8> %V) {
1481; CHECK-LABEL: @icmp_shl_1_ugt_signmask_undef(
1482; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1483;
1484  %shl = shl <2 x i8> <i8 1, i8 1>, %V
1485  %cmp = icmp ugt <2 x i8> %shl, <i8 128, i8 undef>
1486  ret <2 x i1> %cmp
1487}
1488
1489define <2 x i1> @icmp_shl_1_ugt_signmask_undef2(<2 x i8> %V) {
1490; CHECK-LABEL: @icmp_shl_1_ugt_signmask_undef2(
1491; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1492;
1493  %shl = shl <2 x i8> <i8 1, i8 undef>, %V
1494  %cmp = icmp ugt <2 x i8> %shl, <i8 undef, i8 128>
1495  ret <2 x i1> %cmp
1496}
1497
1498define i1 @icmp_shl_1_V_ule_2147483648(i32 %V) {
1499; CHECK-LABEL: @icmp_shl_1_V_ule_2147483648(
1500; CHECK-NEXT:    ret i1 true
1501;
1502  %shl = shl i32 1, %V
1503  %cmp = icmp ule i32 %shl, 2147483648
1504  ret i1 %cmp
1505}
1506
1507define <2 x i1> @icmp_shl_1_ule_signmask(<2 x i8> %V) {
1508; CHECK-LABEL: @icmp_shl_1_ule_signmask(
1509; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1510;
1511  %shl = shl <2 x i8> <i8 1, i8 1>, %V
1512  %cmp = icmp ule <2 x i8> %shl, <i8 128, i8 128>
1513  ret <2 x i1> %cmp
1514}
1515
1516define <2 x i1> @icmp_shl_1_ule_signmask_undef(<2 x i8> %V) {
1517; CHECK-LABEL: @icmp_shl_1_ule_signmask_undef(
1518; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1519;
1520  %shl = shl <2 x i8> <i8 1, i8 1>, %V
1521  %cmp = icmp ule <2 x i8> %shl, <i8 128, i8 undef>
1522  ret <2 x i1> %cmp
1523}
1524
1525define <2 x i1> @icmp_shl_1_ule_signmask_undef2(<2 x i8> %V) {
1526; CHECK-LABEL: @icmp_shl_1_ule_signmask_undef2(
1527; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1528;
1529  %shl = shl <2 x i8> <i8 1, i8 undef>, %V
1530  %cmp = icmp ule <2 x i8> %shl, <i8 undef, i8 128>
1531  ret <2 x i1> %cmp
1532}
1533
1534define i1 @shl_1_cmp_eq_nonpow2(i32 %x) {
1535; CHECK-LABEL: @shl_1_cmp_eq_nonpow2(
1536; CHECK-NEXT:    ret i1 false
1537;
1538  %s = shl i32 1, %x
1539  %c = icmp eq i32 %s, 31
1540  ret i1 %c
1541}
1542
1543define <2 x i1> @shl_1_cmp_eq_nonpow2_splat(<2 x i32> %x) {
1544; CHECK-LABEL: @shl_1_cmp_eq_nonpow2_splat(
1545; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1546;
1547  %s = shl <2 x i32> <i32 1, i32 1>, %x
1548  %c = icmp eq <2 x i32> %s, <i32 31, i32 31>
1549  ret <2 x i1> %c
1550}
1551
1552define <2 x i1> @shl_1_cmp_eq_nonpow2_splat_undef(<2 x i32> %x) {
1553; CHECK-LABEL: @shl_1_cmp_eq_nonpow2_splat_undef(
1554; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1555;
1556  %s = shl <2 x i32> <i32 1, i32 1>, %x
1557  %c = icmp eq <2 x i32> %s, <i32 31, i32 undef>
1558  ret <2 x i1> %c
1559}
1560
1561define i1 @shl_1_cmp_ne_nonpow2(i32 %x) {
1562; CHECK-LABEL: @shl_1_cmp_ne_nonpow2(
1563; CHECK-NEXT:    ret i1 true
1564;
1565  %s = shl i32 1, %x
1566  %c = icmp ne i32 %s, 42
1567  ret i1 %c
1568}
1569
1570define <2 x i1> @shl_1_cmp_ne_nonpow2_splat(<2 x i32> %x) {
1571; CHECK-LABEL: @shl_1_cmp_ne_nonpow2_splat(
1572; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1573;
1574  %s = shl <2 x i32> <i32 1, i32 1>, %x
1575  %c = icmp ne <2 x i32> %s, <i32 42, i32 42>
1576  ret <2 x i1> %c
1577}
1578
1579define <2 x i1> @shl_1_cmp_ne_nonpow2_splat_undef(<2 x i32> %x) {
1580; CHECK-LABEL: @shl_1_cmp_ne_nonpow2_splat_undef(
1581; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1582;
1583  %s = shl <2 x i32> <i32 undef, i32 1>, %x
1584  %c = icmp ne <2 x i32> %s, <i32 42, i32 undef>
1585  ret <2 x i1> %c
1586}
1587
1588define i1 @shl_pow2_cmp_eq_nonpow2(i32 %x) {
1589; CHECK-LABEL: @shl_pow2_cmp_eq_nonpow2(
1590; CHECK-NEXT:    ret i1 false
1591;
1592  %s = shl i32 4, %x
1593  %c = icmp eq i32 %s, 31
1594  ret i1 %c
1595}
1596
1597define <2 x i1> @shl_pow21_cmp_ne_nonpow2_splat_undef(<2 x i32> %x) {
1598; CHECK-LABEL: @shl_pow21_cmp_ne_nonpow2_splat_undef(
1599; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1600;
1601  %s = shl <2 x i32> <i32 undef, i32 4>, %x
1602  %c = icmp ne <2 x i32> %s, <i32 31, i32 undef>
1603  ret <2 x i1> %c
1604}
1605
1606; Negative test - overflowing shift could be zero.
1607
1608define i1 @shl_pow2_cmp_ne_zero(i32 %x) {
1609; CHECK-LABEL: @shl_pow2_cmp_ne_zero(
1610; CHECK-NEXT:    [[S:%.*]] = shl i32 16, [[X:%.*]]
1611; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[S]], 0
1612; CHECK-NEXT:    ret i1 [[C]]
1613;
1614  %s = shl i32 16, %x
1615  %c = icmp ne i32 %s, 0
1616  ret i1 %c
1617}
1618
1619; Negative test - overflowing shift could be zero.
1620
1621define <2 x i1> @shl_pow2_cmp_ne_zero_splat(<2 x i32> %x) {
1622; CHECK-LABEL: @shl_pow2_cmp_ne_zero_splat(
1623; CHECK-NEXT:    [[S:%.*]] = shl <2 x i32> <i32 16, i32 16>, [[X:%.*]]
1624; CHECK-NEXT:    [[C:%.*]] = icmp ne <2 x i32> [[S]], zeroinitializer
1625; CHECK-NEXT:    ret <2 x i1> [[C]]
1626;
1627  %s = shl <2 x i32> <i32 16, i32 16>, %x
1628  %c = icmp ne <2 x i32> %s, zeroinitializer
1629  ret <2 x i1> %c
1630}
1631
1632define i1 @shl_pow2_cmp_eq_zero_nuw(i32 %x) {
1633; CHECK-LABEL: @shl_pow2_cmp_eq_zero_nuw(
1634; CHECK-NEXT:    ret i1 false
1635;
1636  %s = shl nuw i32 16, %x
1637  %c = icmp eq i32 %s, 0
1638  ret i1 %c
1639}
1640
1641define <2 x i1> @shl_pow2_cmp_ne_zero_nuw_splat_undef(<2 x i32> %x) {
1642; CHECK-LABEL: @shl_pow2_cmp_ne_zero_nuw_splat_undef(
1643; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
1644;
1645  %s = shl nuw <2 x i32> <i32 16, i32 undef>, %x
1646  %c = icmp ne <2 x i32> %s, <i32 undef, i32 0>
1647  ret <2 x i1> %c
1648}
1649
1650define i1 @shl_pow2_cmp_ne_zero_nsw(i32 %x) {
1651; CHECK-LABEL: @shl_pow2_cmp_ne_zero_nsw(
1652; CHECK-NEXT:    ret i1 true
1653;
1654  %s = shl nsw i32 16, %x
1655  %c = icmp ne i32 %s, 0
1656  ret i1 %c
1657}
1658
1659define <2 x i1> @shl_pow2_cmp_eq_zero_nsw_splat_undef(<2 x i32> %x) {
1660; CHECK-LABEL: @shl_pow2_cmp_eq_zero_nsw_splat_undef(
1661; CHECK-NEXT:    ret <2 x i1> zeroinitializer
1662;
1663  %s = shl nsw <2 x i32> <i32 undef, i32 16>, %x
1664  %c = icmp eq <2 x i32> %s, <i32 0, i32 undef>
1665  ret <2 x i1> %c
1666}
1667
1668define i1 @tautological1(i32 %A, i32 %B) {
1669; CHECK-LABEL: @tautological1(
1670; CHECK-NEXT:    ret i1 false
1671;
1672  %C = and i32 %A, %B
1673  %D = icmp ugt i32 %C, %A
1674  ret i1 %D
1675}
1676
1677define i1 @tautological2(i32 %A, i32 %B) {
1678; CHECK-LABEL: @tautological2(
1679; CHECK-NEXT:    ret i1 true
1680;
1681  %C = and i32 %A, %B
1682  %D = icmp ule i32 %C, %A
1683  ret i1 %D
1684}
1685
1686define i1 @tautological3(i32 %A, i32 %B) {
1687; CHECK-LABEL: @tautological3(
1688; CHECK-NEXT:    ret i1 true
1689;
1690  %C = or i32 %A, %B
1691  %D = icmp ule i32 %A, %C
1692  ret i1 %D
1693}
1694
1695define i1 @tautological4(i32 %A, i32 %B) {
1696; CHECK-LABEL: @tautological4(
1697; CHECK-NEXT:    ret i1 false
1698;
1699  %C = or i32 %A, %B
1700  %D = icmp ugt i32 %A, %C
1701  ret i1 %D
1702}
1703
1704define i1 @tautological5(i32 %A, i32 %B) {
1705; CHECK-LABEL: @tautological5(
1706; CHECK-NEXT:    ret i1 false
1707;
1708  %C = or i32 %A, %B
1709  %D = icmp ult i32 %C, %A
1710  ret i1 %D
1711}
1712
1713define i1 @tautological6(i32 %A, i32 %B) {
1714; CHECK-LABEL: @tautological6(
1715; CHECK-NEXT:    ret i1 true
1716;
1717  %C = or i32 %A, %B
1718  %D = icmp uge i32 %C, %A
1719  ret i1 %D
1720}
1721
1722define i1 @tautological7(i32 %A, i32 %B) {
1723; CHECK-LABEL: @tautological7(
1724; CHECK-NEXT:    ret i1 true
1725;
1726  %C = and i32 %A, %B
1727  %D = icmp uge i32 %A, %C
1728  ret i1 %D
1729}
1730
1731define i1 @tautological8(i32 %A, i32 %B) {
1732; CHECK-LABEL: @tautological8(
1733; CHECK-NEXT:    ret i1 false
1734;
1735  %C = and i32 %A, %B
1736  %D = icmp ult i32 %A, %C
1737  ret i1 %D
1738}
1739
1740declare void @helper_i1(i1)
1741; Series of tests for icmp s[lt|ge] (or A, B), A and icmp s[gt|le] A, (or A, B)
1742define void @icmp_slt_sge_or(i32 %Ax, i32 %Bx) {
1743; 'p' for positive, 'n' for negative, 'x' for potentially either.
1744; %D is 'icmp slt (or A, B), A'
1745; %E is 'icmp sge (or A, B), A' making it the not of %D
1746; %F is 'icmp sgt A, (or A, B)' making it the same as %D
1747; %G is 'icmp sle A, (or A, B)' making it the not of %D
1748; CHECK-LABEL: @icmp_slt_sge_or(
1749; CHECK-NEXT:    [[APOS:%.*]] = and i32 [[AX:%.*]], 2147483647
1750; CHECK-NEXT:    [[BNEG:%.*]] = or i32 [[BX:%.*]], -2147483648
1751; CHECK-NEXT:    [[CPX:%.*]] = or i32 [[APOS]], [[BX]]
1752; CHECK-NEXT:    [[DPX:%.*]] = icmp slt i32 [[CPX]], [[APOS]]
1753; CHECK-NEXT:    [[EPX:%.*]] = icmp sge i32 [[CPX]], [[APOS]]
1754; CHECK-NEXT:    [[FPX:%.*]] = icmp sgt i32 [[APOS]], [[CPX]]
1755; CHECK-NEXT:    [[GPX:%.*]] = icmp sle i32 [[APOS]], [[CPX]]
1756; CHECK-NEXT:    [[CXX:%.*]] = or i32 [[AX]], [[BX]]
1757; CHECK-NEXT:    [[DXX:%.*]] = icmp slt i32 [[CXX]], [[AX]]
1758; CHECK-NEXT:    [[EXX:%.*]] = icmp sge i32 [[CXX]], [[AX]]
1759; CHECK-NEXT:    [[FXX:%.*]] = icmp sgt i32 [[AX]], [[CXX]]
1760; CHECK-NEXT:    [[GXX:%.*]] = icmp sle i32 [[AX]], [[CXX]]
1761; CHECK-NEXT:    [[CXN:%.*]] = or i32 [[AX]], [[BNEG]]
1762; CHECK-NEXT:    [[DXN:%.*]] = icmp slt i32 [[CXN]], [[AX]]
1763; CHECK-NEXT:    [[EXN:%.*]] = icmp sge i32 [[CXN]], [[AX]]
1764; CHECK-NEXT:    [[FXN:%.*]] = icmp sgt i32 [[AX]], [[CXN]]
1765; CHECK-NEXT:    [[GXN:%.*]] = icmp sle i32 [[AX]], [[CXN]]
1766; CHECK-NEXT:    call void @helper_i1(i1 false)
1767; CHECK-NEXT:    call void @helper_i1(i1 true)
1768; CHECK-NEXT:    call void @helper_i1(i1 false)
1769; CHECK-NEXT:    call void @helper_i1(i1 true)
1770; CHECK-NEXT:    call void @helper_i1(i1 [[DPX]])
1771; CHECK-NEXT:    call void @helper_i1(i1 [[EPX]])
1772; CHECK-NEXT:    call void @helper_i1(i1 [[FPX]])
1773; CHECK-NEXT:    call void @helper_i1(i1 [[GPX]])
1774; CHECK-NEXT:    call void @helper_i1(i1 true)
1775; CHECK-NEXT:    call void @helper_i1(i1 false)
1776; CHECK-NEXT:    call void @helper_i1(i1 true)
1777; CHECK-NEXT:    call void @helper_i1(i1 false)
1778; CHECK-NEXT:    call void @helper_i1(i1 false)
1779; CHECK-NEXT:    call void @helper_i1(i1 true)
1780; CHECK-NEXT:    call void @helper_i1(i1 false)
1781; CHECK-NEXT:    call void @helper_i1(i1 true)
1782; CHECK-NEXT:    call void @helper_i1(i1 [[DXX]])
1783; CHECK-NEXT:    call void @helper_i1(i1 [[EXX]])
1784; CHECK-NEXT:    call void @helper_i1(i1 [[FXX]])
1785; CHECK-NEXT:    call void @helper_i1(i1 [[GXX]])
1786; CHECK-NEXT:    call void @helper_i1(i1 [[DXN]])
1787; CHECK-NEXT:    call void @helper_i1(i1 [[EXN]])
1788; CHECK-NEXT:    call void @helper_i1(i1 [[FXN]])
1789; CHECK-NEXT:    call void @helper_i1(i1 [[GXN]])
1790; CHECK-NEXT:    call void @helper_i1(i1 false)
1791; CHECK-NEXT:    call void @helper_i1(i1 true)
1792; CHECK-NEXT:    call void @helper_i1(i1 false)
1793; CHECK-NEXT:    call void @helper_i1(i1 true)
1794; CHECK-NEXT:    call void @helper_i1(i1 false)
1795; CHECK-NEXT:    call void @helper_i1(i1 true)
1796; CHECK-NEXT:    call void @helper_i1(i1 false)
1797; CHECK-NEXT:    call void @helper_i1(i1 true)
1798; CHECK-NEXT:    call void @helper_i1(i1 false)
1799; CHECK-NEXT:    call void @helper_i1(i1 true)
1800; CHECK-NEXT:    call void @helper_i1(i1 false)
1801; CHECK-NEXT:    call void @helper_i1(i1 true)
1802; CHECK-NEXT:    ret void
1803;
1804  %Aneg = or i32 %Ax, 2147483648
1805  %Apos = and i32 %Ax, 2147483647
1806  %Bneg = or i32 %Bx, 2147483648
1807  %Bpos = and i32 %Bx, 2147483647
1808
1809  %Cpp = or i32 %Apos, %Bpos
1810  %Dpp = icmp slt i32 %Cpp, %Apos
1811  %Epp = icmp sge i32 %Cpp, %Apos
1812  %Fpp = icmp sgt i32 %Apos, %Cpp
1813  %Gpp = icmp sle i32 %Apos, %Cpp
1814  %Cpx = or i32 %Apos, %Bx
1815  %Dpx = icmp slt i32 %Cpx, %Apos
1816  %Epx = icmp sge i32 %Cpx, %Apos
1817  %Fpx = icmp sgt i32 %Apos, %Cpx
1818  %Gpx = icmp sle i32 %Apos, %Cpx
1819  %Cpn = or i32 %Apos, %Bneg
1820  %Dpn = icmp slt i32 %Cpn, %Apos
1821  %Epn = icmp sge i32 %Cpn, %Apos
1822  %Fpn = icmp sgt i32 %Apos, %Cpn
1823  %Gpn = icmp sle i32 %Apos, %Cpn
1824
1825  %Cxp = or i32 %Ax, %Bpos
1826  %Dxp = icmp slt i32 %Cxp, %Ax
1827  %Exp = icmp sge i32 %Cxp, %Ax
1828  %Fxp = icmp sgt i32 %Ax, %Cxp
1829  %Gxp = icmp sle i32 %Ax, %Cxp
1830  %Cxx = or i32 %Ax, %Bx
1831  %Dxx = icmp slt i32 %Cxx, %Ax
1832  %Exx = icmp sge i32 %Cxx, %Ax
1833  %Fxx = icmp sgt i32 %Ax, %Cxx
1834  %Gxx = icmp sle i32 %Ax, %Cxx
1835  %Cxn = or i32 %Ax, %Bneg
1836  %Dxn = icmp slt i32 %Cxn, %Ax
1837  %Exn = icmp sge i32 %Cxn, %Ax
1838  %Fxn = icmp sgt i32 %Ax, %Cxn
1839  %Gxn = icmp sle i32 %Ax, %Cxn
1840
1841  %Cnp = or i32 %Aneg, %Bpos
1842  %Dnp = icmp slt i32 %Cnp, %Aneg
1843  %Enp = icmp sge i32 %Cnp, %Aneg
1844  %Fnp = icmp sgt i32 %Aneg, %Cnp
1845  %Gnp = icmp sle i32 %Aneg, %Cnp
1846  %Cnx = or i32 %Aneg, %Bx
1847  %Dnx = icmp slt i32 %Cnx, %Aneg
1848  %Enx = icmp sge i32 %Cnx, %Aneg
1849  %Fnx = icmp sgt i32 %Aneg, %Cnx
1850  %Gnx = icmp sle i32 %Aneg, %Cnx
1851  %Cnn = or i32 %Aneg, %Bneg
1852  %Dnn = icmp slt i32 %Cnn, %Aneg
1853  %Enn = icmp sge i32 %Cnn, %Aneg
1854  %Fnn = icmp sgt i32 %Aneg, %Cnn
1855  %Gnn = icmp sle i32 %Aneg, %Cnn
1856
1857  call void @helper_i1(i1 %Dpp)
1858  call void @helper_i1(i1 %Epp)
1859  call void @helper_i1(i1 %Fpp)
1860  call void @helper_i1(i1 %Gpp)
1861  call void @helper_i1(i1 %Dpx)
1862  call void @helper_i1(i1 %Epx)
1863  call void @helper_i1(i1 %Fpx)
1864  call void @helper_i1(i1 %Gpx)
1865  call void @helper_i1(i1 %Dpn)
1866  call void @helper_i1(i1 %Epn)
1867  call void @helper_i1(i1 %Fpn)
1868  call void @helper_i1(i1 %Gpn)
1869  call void @helper_i1(i1 %Dxp)
1870  call void @helper_i1(i1 %Exp)
1871  call void @helper_i1(i1 %Fxp)
1872  call void @helper_i1(i1 %Gxp)
1873  call void @helper_i1(i1 %Dxx)
1874  call void @helper_i1(i1 %Exx)
1875  call void @helper_i1(i1 %Fxx)
1876  call void @helper_i1(i1 %Gxx)
1877  call void @helper_i1(i1 %Dxn)
1878  call void @helper_i1(i1 %Exn)
1879  call void @helper_i1(i1 %Fxn)
1880  call void @helper_i1(i1 %Gxn)
1881  call void @helper_i1(i1 %Dnp)
1882  call void @helper_i1(i1 %Enp)
1883  call void @helper_i1(i1 %Fnp)
1884  call void @helper_i1(i1 %Gnp)
1885  call void @helper_i1(i1 %Dnx)
1886  call void @helper_i1(i1 %Enx)
1887  call void @helper_i1(i1 %Fnx)
1888  call void @helper_i1(i1 %Gnx)
1889  call void @helper_i1(i1 %Dnn)
1890  call void @helper_i1(i1 %Enn)
1891  call void @helper_i1(i1 %Fnn)
1892  call void @helper_i1(i1 %Gnn)
1893  ret void
1894}
1895
1896define i1 @constant_fold_inttoptr_null() {
1897; CHECK-LABEL: @constant_fold_inttoptr_null(
1898; CHECK-NEXT:    ret i1 false
1899;
1900  %x = icmp eq i32* inttoptr (i64 32 to i32*), null
1901  ret i1 %x
1902}
1903
1904define i1 @constant_fold_null_inttoptr() {
1905; CHECK-LABEL: @constant_fold_null_inttoptr(
1906; CHECK-NEXT:    ret i1 false
1907;
1908  %x = icmp eq i32* null, inttoptr (i64 32 to i32*)
1909  ret i1 %x
1910}
1911
1912define i1 @cmp_through_addrspacecast(i32 addrspace(1)* %p1) {
1913; CHECK-LABEL: @cmp_through_addrspacecast(
1914; CHECK-NEXT:    ret i1 true
1915;
1916  %p0 = addrspacecast i32 addrspace(1)* %p1 to i32*
1917  %p0.1 = getelementptr inbounds i32, i32* %p0, i64 1
1918  %cmp = icmp ne i32* %p0, %p0.1
1919  ret i1 %cmp
1920}
1921
1922; Test simplifications for: icmp (X+Y), (X+Z) -> icmp Y,Z
1923; Test the overflow check when the RHS has NSW set and constant Z is greater
1924; than Y, then we know X+Y also can't overflow.
1925
1926define i1 @icmp_nsw_1(i32 %V) {
1927; CHECK-LABEL: @icmp_nsw_1(
1928; CHECK-NEXT:    ret i1 true
1929;
1930  %add5 = add i32 %V, 5
1931  %add6 = add nsw i32 %V, 6
1932  %s1 = sext i32 %add5 to i64
1933  %s2 = sext i32 %add6 to i64
1934  %cmp = icmp slt i64 %s1, %s2
1935  ret i1 %cmp
1936}
1937
1938define i1 @icmp_nsw_2(i32 %V) {
1939; CHECK-LABEL: @icmp_nsw_2(
1940; CHECK-NEXT:    ret i1 true
1941;
1942  %add5 = add i32 %V, 5
1943  %add6 = add nsw i32 %V, 6
1944  %cmp = icmp slt i32 %add5, %add6
1945  ret i1 %cmp
1946}
1947
1948define i1 @icmp_nsw_commute(i32 %V) {
1949; CHECK-LABEL: @icmp_nsw_commute(
1950; CHECK-NEXT:    ret i1 true
1951;
1952  %add5 = add i32 5, %V
1953  %add6 = add nsw i32 %V, 6
1954  %cmp = icmp slt i32 %add5, %add6
1955  ret i1 %cmp
1956}
1957
1958define i1 @icmp_nsw_commute2(i32 %V) {
1959; CHECK-LABEL: @icmp_nsw_commute2(
1960; CHECK-NEXT:    ret i1 true
1961;
1962  %add5 = add i32 %V, 5
1963  %add6 = add nsw i32 6, %V
1964  %cmp = icmp slt i32 %add5, %add6
1965  ret i1 %cmp
1966}
1967
1968define i1 @icmp_nsw_commute3(i32 %V) {
1969; CHECK-LABEL: @icmp_nsw_commute3(
1970; CHECK-NEXT:    ret i1 true
1971;
1972  %add5 = add i32 5, %V
1973  %add6 = add nsw i32 6, %V
1974  %cmp = icmp slt i32 %add5, %add6
1975  ret i1 %cmp
1976}
1977
1978define i1 @icmp_nsw_22(i32 %V) {
1979; CHECK-LABEL: @icmp_nsw_22(
1980; CHECK-NEXT:    ret i1 true
1981;
1982  %add5 = add nsw i32 %V, 5
1983  %add6 = add nsw i32 %V, 6
1984  %cmp = icmp slt i32 %add5, %add6
1985  ret i1 %cmp
1986}
1987
1988define i1 @icmp_nsw_23(i32 %V) {
1989; CHECK-LABEL: @icmp_nsw_23(
1990; CHECK-NEXT:    [[ADD5:%.*]] = add nsw i32 [[V:%.*]], 5
1991; CHECK-NEXT:    [[ADD6:%.*]] = add i32 [[V]], 6
1992; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD6]]
1993; CHECK-NEXT:    ret i1 [[CMP]]
1994;
1995  %add5 = add nsw i32 %V, 5
1996  %add6 = add i32 %V, 6
1997  %cmp = icmp slt i32 %add5, %add6
1998  ret i1 %cmp
1999}
2000
2001define i1 @icmp_nsw_false(i32 %V) {
2002; CHECK-LABEL: @icmp_nsw_false(
2003; CHECK-NEXT:    ret i1 false
2004;
2005  %add5 = add nsw i32 %V, 6
2006  %add6 = add i32 %V, 5
2007  %cmp = icmp slt i32 %add5, %add6
2008  ret i1 %cmp
2009}
2010
2011define i1 @icmp_nsw_false_2(i32 %V) {
2012; CHECK-LABEL: @icmp_nsw_false_2(
2013; CHECK-NEXT:    ret i1 false
2014;
2015  %add5 = add nsw i32 %V, 6
2016  %add6 = add nsw i32 %V, 5
2017  %cmp = icmp slt i32 %add5, %add6
2018  ret i1 %cmp
2019}
2020
2021define i1 @icmp_nsw_false_3(i32 %V) {
2022; CHECK-LABEL: @icmp_nsw_false_3(
2023; CHECK-NEXT:    [[ADD5:%.*]] = add nsw i32 [[V:%.*]], 5
2024; CHECK-NEXT:    [[ADD6:%.*]] = add i32 [[V]], 5
2025; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD6]]
2026; CHECK-NEXT:    ret i1 [[CMP]]
2027;
2028  %add5 = add nsw i32 %V, 5
2029  %add6 = add i32 %V, 5
2030  %cmp = icmp slt i32 %add5, %add6
2031  ret i1 %cmp
2032}
2033
2034define i1 @icmp_nsw_false_4(i32 %V) {
2035; CHECK-LABEL: @icmp_nsw_false_4(
2036; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 6
2037; CHECK-NEXT:    [[ADD6:%.*]] = add nsw i32 [[V]], 5
2038; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD6]]
2039; CHECK-NEXT:    ret i1 [[CMP]]
2040;
2041  %add5 = add i32 %V, 6
2042  %add6 = add nsw i32 %V, 5
2043  %cmp = icmp slt i32 %add5, %add6
2044  ret i1 %cmp
2045}
2046
2047define i1 @icmp_nsw_false_5(i8 %V) {
2048; CHECK-LABEL: @icmp_nsw_false_5(
2049; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[V:%.*]], 121
2050; CHECK-NEXT:    [[ADDNSW:%.*]] = add nsw i8 [[V]], -104
2051; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[ADD]], [[ADDNSW]]
2052; CHECK-NEXT:    ret i1 [[CMP]]
2053;
2054  %add = add i8 %V, 121
2055  %addnsw = add nsw i8 %V, -104
2056  %cmp = icmp slt i8 %add, %addnsw
2057  ret i1 %cmp
2058}
2059
2060define i1 @icmp_nsw_i8(i8 %V) {
2061; CHECK-LABEL: @icmp_nsw_i8(
2062; CHECK-NEXT:    ret i1 true
2063;
2064  %add5 = add i8 %V, 5
2065  %add6 = add nsw i8 %V, 6
2066  %cmp = icmp slt i8 %add5, %add6
2067  ret i1 %cmp
2068}
2069
2070define i1 @icmp_nsw_i16(i16 %V) {
2071; CHECK-LABEL: @icmp_nsw_i16(
2072; CHECK-NEXT:    ret i1 true
2073;
2074  %add5 = add i16 %V, 0
2075  %add6 = add nsw i16 %V, 1
2076  %cmp = icmp slt i16 %add5, %add6
2077  ret i1 %cmp
2078}
2079
2080define i1 @icmp_nsw_i64(i64 %V) {
2081; CHECK-LABEL: @icmp_nsw_i64(
2082; CHECK-NEXT:    ret i1 true
2083;
2084  %add5 = add i64 %V, 5
2085  %add6 = add nsw i64 %V, 6
2086  %cmp = icmp slt i64 %add5, %add6
2087  ret i1 %cmp
2088}
2089
2090define <4 x i1> @icmp_nsw_vec(<4 x i32> %V) {
2091; CHECK-LABEL: @icmp_nsw_vec(
2092; CHECK-NEXT:    ret <4 x i1> <i1 true, i1 true, i1 true, i1 true>
2093;
2094  %add5 = add <4 x i32> %V, <i32 5, i32 5, i32 5, i32 5>
2095  %add6 = add nsw <4 x i32> %V, <i32 6, i32 6, i32 6, i32 6>
2096  %cmp = icmp slt <4 x i32> %add5, %add6
2097  ret <4 x i1> %cmp
2098}
2099
2100define i1 @icmp_nsw_3(i32 %V) {
2101; CHECK-LABEL: @icmp_nsw_3(
2102; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 5
2103; CHECK-NEXT:    [[ADD5_2:%.*]] = add nsw i32 [[V]], 5
2104; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD5_2]]
2105; CHECK-NEXT:    ret i1 [[CMP]]
2106;
2107  %add5 = add i32 %V, 5
2108  %add5_2 = add nsw i32 %V, 5
2109  %cmp = icmp slt i32 %add5, %add5_2
2110  ret i1 %cmp
2111}
2112
2113define i1 @icmp_nsw_4(i32 %V) {
2114; CHECK-LABEL: @icmp_nsw_4(
2115; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 5
2116; CHECK-NEXT:    [[ADD4:%.*]] = add nsw i32 [[V]], 4
2117; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD4]]
2118; CHECK-NEXT:    ret i1 [[CMP]]
2119;
2120  %add5 = add i32 %V, 5
2121  %add4 = add nsw i32 %V, 4
2122  %cmp = icmp slt i32 %add5, %add4
2123  ret i1 %cmp
2124}
2125
2126define i1 @icmp_nsw_5(i32 %V) {
2127; CHECK-LABEL: @icmp_nsw_5(
2128; CHECK-NEXT:    [[ADD5:%.*]] = add nsw i32 [[V:%.*]], 5
2129; CHECK-NEXT:    [[ADD6:%.*]] = add i32 [[V]], 6
2130; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD6]]
2131; CHECK-NEXT:    ret i1 [[CMP]]
2132;
2133  %add5 = add nsw i32 %V, 5
2134  %add6 = add i32 %V, 6
2135  %cmp = icmp slt i32 %add5, %add6
2136  ret i1 %cmp
2137}
2138
2139define i1 @icmp_nsw_7(i32 %V, i32 %arg) {
2140; CHECK-LABEL: @icmp_nsw_7(
2141; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 5
2142; CHECK-NEXT:    [[ADDARG:%.*]] = add nsw i32 [[V]], [[ARG:%.*]]
2143; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADDARG]]
2144; CHECK-NEXT:    ret i1 [[CMP]]
2145;
2146  %add5 = add i32 %V, 5
2147  %addarg = add nsw i32 %V, %arg
2148  %cmp = icmp slt i32 %add5, %addarg
2149  ret i1 %cmp
2150}
2151
2152define i1 @icmp_nsw_8(i32 %V, i32 %arg) {
2153; CHECK-LABEL: @icmp_nsw_8(
2154; CHECK-NEXT:    [[ADDARG:%.*]] = add i32 [[V:%.*]], [[ARG:%.*]]
2155; CHECK-NEXT:    [[ADD6:%.*]] = add nsw i32 [[V]], 5
2156; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADDARG]], [[ADD6]]
2157; CHECK-NEXT:    ret i1 [[CMP]]
2158;
2159  %addarg = add i32 %V, %arg
2160  %add6 = add nsw i32 %V, 5
2161  %cmp = icmp slt i32 %addarg, %add6
2162  ret i1 %cmp
2163}
2164
2165define i1 @icmp_nsw_9(i32 %V1, i32 %V2) {
2166; CHECK-LABEL: @icmp_nsw_9(
2167; CHECK-NEXT:    [[ADD_V1:%.*]] = add i32 [[V1:%.*]], 5
2168; CHECK-NEXT:    [[ADD_V2:%.*]] = add nsw i32 [[V2:%.*]], 6
2169; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD_V1]], [[ADD_V2]]
2170; CHECK-NEXT:    ret i1 [[CMP]]
2171;
2172  %add_V1 = add i32 %V1, 5
2173  %add_V2 = add nsw i32 %V2, 6
2174  %cmp = icmp slt i32 %add_V1, %add_V2
2175  ret i1 %cmp
2176}
2177
2178define i1 @icmp_nsw_10(i32 %V) {
2179; CHECK-LABEL: @icmp_nsw_10(
2180; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 5
2181; CHECK-NEXT:    [[ADD6:%.*]] = add nsw i32 [[V]], 6
2182; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[ADD6]], [[ADD5]]
2183; CHECK-NEXT:    ret i1 [[CMP]]
2184;
2185  %add5 = add i32 %V, 5
2186  %add6 = add nsw i32 %V, 6
2187  %cmp = icmp sgt i32 %add6, %add5
2188  ret i1 %cmp
2189}
2190
2191define i1 @icmp_nsw_11(i32 %V) {
2192; CHECK-LABEL: @icmp_nsw_11(
2193; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], -125
2194; CHECK-NEXT:    [[ADD6:%.*]] = add nsw i32 [[V]], -99
2195; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[ADD6]]
2196; CHECK-NEXT:    ret i1 [[CMP]]
2197;
2198  %add5 = add i32 %V, -125
2199  %add6 = add nsw i32 %V, -99
2200  %cmp = icmp slt i32 %add5, %add6
2201  ret i1 %cmp
2202}
2203
2204define i1 @icmp_nsw_nonpos(i32 %V) {
2205; CHECK-LABEL: @icmp_nsw_nonpos(
2206; CHECK-NEXT:    ret i1 false
2207;
2208  %add5 = add i32 %V, 0
2209  %add6 = add nsw i32 %V, -1
2210  %cmp = icmp slt i32 %add5, %add6
2211  ret i1 %cmp
2212}
2213
2214define i1 @icmp_nsw_nonpos2(i32 %V) {
2215; CHECK-LABEL: @icmp_nsw_nonpos2(
2216; CHECK-NEXT:    [[ADD5:%.*]] = add i32 [[V:%.*]], 1
2217; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[ADD5]], [[V]]
2218; CHECK-NEXT:    ret i1 [[CMP]]
2219;
2220  %add5 = add i32 %V, 1
2221  %add6 = add nsw i32 %V, 0
2222  %cmp = icmp slt i32 %add5, %add6
2223  ret i1 %cmp
2224}
2225
2226declare i11 @llvm.ctpop.i11(i11)
2227declare i73 @llvm.ctpop.i73(i73)
2228declare <2 x i13> @llvm.ctpop.v2i13(<2 x i13>)
2229
2230define i1 @ctpop_sgt_bitwidth(i11 %x) {
2231; CHECK-LABEL: @ctpop_sgt_bitwidth(
2232; CHECK-NEXT:    ret i1 false
2233;
2234  %pop = call i11 @llvm.ctpop.i11(i11 %x)
2235  %cmp = icmp sgt i11 %pop, 11
2236  ret i1 %cmp
2237}
2238
2239define i1 @ctpop_sle_minus1(i11 %x) {
2240; CHECK-LABEL: @ctpop_sle_minus1(
2241; CHECK-NEXT:    ret i1 false
2242;
2243  %pop = call i11 @llvm.ctpop.i11(i11 %x)
2244  %cmp = icmp sle i11 %pop, -1
2245  ret i1 %cmp
2246}
2247
2248define i1 @ctpop_ugt_bitwidth(i73 %x) {
2249; CHECK-LABEL: @ctpop_ugt_bitwidth(
2250; CHECK-NEXT:    ret i1 false
2251;
2252  %pop = call i73 @llvm.ctpop.i73(i73 %x)
2253  %cmp = icmp ugt i73 %pop, 73
2254  ret i1 %cmp
2255}
2256
2257; Negative test - does not simplify, but instcombine could reduce this.
2258
2259define i1 @ctpop_ugt_bitwidth_minus1(i73 %x) {
2260; CHECK-LABEL: @ctpop_ugt_bitwidth_minus1(
2261; CHECK-NEXT:    [[POP:%.*]] = call i73 @llvm.ctpop.i73(i73 [[X:%.*]])
2262; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i73 [[POP]], 72
2263; CHECK-NEXT:    ret i1 [[CMP]]
2264;
2265  %pop = call i73 @llvm.ctpop.i73(i73 %x)
2266  %cmp = icmp ugt i73 %pop, 72
2267  ret i1 %cmp
2268}
2269
2270define <2 x i1> @ctpop_sgt_bitwidth_splat(<2 x i13> %x) {
2271; CHECK-LABEL: @ctpop_sgt_bitwidth_splat(
2272; CHECK-NEXT:    ret <2 x i1> zeroinitializer
2273;
2274  %pop = call <2 x i13> @llvm.ctpop.v2i13(<2 x i13> %x)
2275  %cmp = icmp sgt <2 x i13> %pop, <i13 13, i13 13>
2276  ret <2 x i1> %cmp
2277}
2278
2279define i1 @ctpop_ult_plus1_bitwidth(i11 %x) {
2280; CHECK-LABEL: @ctpop_ult_plus1_bitwidth(
2281; CHECK-NEXT:    ret i1 true
2282;
2283  %pop = call i11 @llvm.ctpop.i11(i11 %x)
2284  %cmp = icmp ult i11 %pop, 12
2285  ret i1 %cmp
2286}
2287
2288define i1 @ctpop_ne_big_bitwidth(i73 %x) {
2289; CHECK-LABEL: @ctpop_ne_big_bitwidth(
2290; CHECK-NEXT:    ret i1 true
2291;
2292  %pop = call i73 @llvm.ctpop.i73(i73 %x)
2293  %cmp = icmp ne i73 %pop, 75
2294  ret i1 %cmp
2295}
2296
2297define <2 x i1> @ctpop_slt_bitwidth_plus1_splat(<2 x i13> %x) {
2298; CHECK-LABEL: @ctpop_slt_bitwidth_plus1_splat(
2299; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
2300;
2301  %pop = call <2 x i13> @llvm.ctpop.v2i13(<2 x i13> %x)
2302  %cmp = icmp slt <2 x i13> %pop, <i13 14, i13 14>
2303  ret <2 x i1> %cmp
2304}
2305
2306; Negative test - does not simplify, but instcombine could reduce this.
2307
2308define <2 x i1> @ctpop_slt_bitwidth_splat(<2 x i13> %x) {
2309; CHECK-LABEL: @ctpop_slt_bitwidth_splat(
2310; CHECK-NEXT:    [[POP:%.*]] = call <2 x i13> @llvm.ctpop.v2i13(<2 x i13> [[X:%.*]])
2311; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i13> [[POP]], <i13 13, i13 13>
2312; CHECK-NEXT:    ret <2 x i1> [[CMP]]
2313;
2314  %pop = call <2 x i13> @llvm.ctpop.v2i13(<2 x i13> %x)
2315  %cmp = icmp slt <2 x i13> %pop, <i13 13, i13 13>
2316  ret <2 x i1> %cmp
2317}
2318
2319declare i11 @llvm.ctlz.i11(i11)
2320declare i73 @llvm.ctlz.i73(i73)
2321declare <2 x i13> @llvm.ctlz.v2i13(<2 x i13>)
2322
2323define i1 @ctlz_sgt_bitwidth(i11 %x) {
2324; CHECK-LABEL: @ctlz_sgt_bitwidth(
2325; CHECK-NEXT:    ret i1 false
2326;
2327  %pop = call i11 @llvm.ctlz.i11(i11 %x)
2328  %cmp = icmp sgt i11 %pop, 11
2329  ret i1 %cmp
2330}
2331
2332define i1 @ctlz_sle_minus1(i11 %x) {
2333; CHECK-LABEL: @ctlz_sle_minus1(
2334; CHECK-NEXT:    ret i1 false
2335;
2336  %pop = call i11 @llvm.ctlz.i11(i11 %x)
2337  %cmp = icmp sle i11 %pop, -1
2338  ret i1 %cmp
2339}
2340
2341define i1 @ctlz_ugt_bitwidth(i73 %x) {
2342; CHECK-LABEL: @ctlz_ugt_bitwidth(
2343; CHECK-NEXT:    ret i1 false
2344;
2345  %pop = call i73 @llvm.ctlz.i73(i73 %x)
2346  %cmp = icmp ugt i73 %pop, 73
2347  ret i1 %cmp
2348}
2349
2350; Negative test - does not simplify, but instcombine could reduce this.
2351
2352define i1 @ctlz_ugt_bitwidth_minus1(i73 %x) {
2353; CHECK-LABEL: @ctlz_ugt_bitwidth_minus1(
2354; CHECK-NEXT:    [[POP:%.*]] = call i73 @llvm.ctlz.i73(i73 [[X:%.*]], i1 false)
2355; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i73 [[POP]], 72
2356; CHECK-NEXT:    ret i1 [[CMP]]
2357;
2358  %pop = call i73 @llvm.ctlz.i73(i73 %x)
2359  %cmp = icmp ugt i73 %pop, 72
2360  ret i1 %cmp
2361}
2362
2363define <2 x i1> @ctlz_sgt_bitwidth_splat(<2 x i13> %x) {
2364; CHECK-LABEL: @ctlz_sgt_bitwidth_splat(
2365; CHECK-NEXT:    ret <2 x i1> zeroinitializer
2366;
2367  %pop = call <2 x i13> @llvm.ctlz.v2i13(<2 x i13> %x)
2368  %cmp = icmp sgt <2 x i13> %pop, <i13 13, i13 13>
2369  ret <2 x i1> %cmp
2370}
2371
2372define i1 @ctlz_ult_plus1_bitwidth(i11 %x) {
2373; CHECK-LABEL: @ctlz_ult_plus1_bitwidth(
2374; CHECK-NEXT:    ret i1 true
2375;
2376  %pop = call i11 @llvm.ctlz.i11(i11 %x)
2377  %cmp = icmp ult i11 %pop, 12
2378  ret i1 %cmp
2379}
2380
2381define i1 @ctlz_ne_big_bitwidth(i73 %x) {
2382; CHECK-LABEL: @ctlz_ne_big_bitwidth(
2383; CHECK-NEXT:    ret i1 true
2384;
2385  %pop = call i73 @llvm.ctlz.i73(i73 %x)
2386  %cmp = icmp ne i73 %pop, 75
2387  ret i1 %cmp
2388}
2389
2390define <2 x i1> @ctlz_slt_bitwidth_plus1_splat(<2 x i13> %x) {
2391; CHECK-LABEL: @ctlz_slt_bitwidth_plus1_splat(
2392; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
2393;
2394  %pop = call <2 x i13> @llvm.ctlz.v2i13(<2 x i13> %x)
2395  %cmp = icmp slt <2 x i13> %pop, <i13 14, i13 14>
2396  ret <2 x i1> %cmp
2397}
2398
2399; Negative test - does not simplify, but instcombine could reduce this.
2400
2401define <2 x i1> @ctlz_slt_bitwidth_splat(<2 x i13> %x) {
2402; CHECK-LABEL: @ctlz_slt_bitwidth_splat(
2403; CHECK-NEXT:    [[POP:%.*]] = call <2 x i13> @llvm.ctlz.v2i13(<2 x i13> [[X:%.*]], i1 false)
2404; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i13> [[POP]], <i13 13, i13 13>
2405; CHECK-NEXT:    ret <2 x i1> [[CMP]]
2406;
2407  %pop = call <2 x i13> @llvm.ctlz.v2i13(<2 x i13> %x)
2408  %cmp = icmp slt <2 x i13> %pop, <i13 13, i13 13>
2409  ret <2 x i1> %cmp
2410}
2411
2412declare i11 @llvm.cttz.i11(i11)
2413declare i73 @llvm.cttz.i73(i73)
2414declare <2 x i13> @llvm.cttz.v2i13(<2 x i13>)
2415
2416define i1 @cttz_sgt_bitwidth(i11 %x) {
2417; CHECK-LABEL: @cttz_sgt_bitwidth(
2418; CHECK-NEXT:    ret i1 false
2419;
2420  %pop = call i11 @llvm.cttz.i11(i11 %x)
2421  %cmp = icmp sgt i11 %pop, 11
2422  ret i1 %cmp
2423}
2424
2425define i1 @cttz_sle_minus1(i11 %x) {
2426; CHECK-LABEL: @cttz_sle_minus1(
2427; CHECK-NEXT:    ret i1 false
2428;
2429  %pop = call i11 @llvm.cttz.i11(i11 %x)
2430  %cmp = icmp sle i11 %pop, -1
2431  ret i1 %cmp
2432}
2433
2434define i1 @cttz_ugt_bitwidth(i73 %x) {
2435; CHECK-LABEL: @cttz_ugt_bitwidth(
2436; CHECK-NEXT:    ret i1 false
2437;
2438  %pop = call i73 @llvm.cttz.i73(i73 %x)
2439  %cmp = icmp ugt i73 %pop, 73
2440  ret i1 %cmp
2441}
2442
2443; Negative test - does not simplify, but instcombine could reduce this.
2444
2445define i1 @cttz_ugt_bitwidth_minus1(i73 %x) {
2446; CHECK-LABEL: @cttz_ugt_bitwidth_minus1(
2447; CHECK-NEXT:    [[POP:%.*]] = call i73 @llvm.cttz.i73(i73 [[X:%.*]], i1 false)
2448; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i73 [[POP]], 72
2449; CHECK-NEXT:    ret i1 [[CMP]]
2450;
2451  %pop = call i73 @llvm.cttz.i73(i73 %x)
2452  %cmp = icmp ugt i73 %pop, 72
2453  ret i1 %cmp
2454}
2455
2456define <2 x i1> @cttz_sgt_bitwidth_splat(<2 x i13> %x) {
2457; CHECK-LABEL: @cttz_sgt_bitwidth_splat(
2458; CHECK-NEXT:    ret <2 x i1> zeroinitializer
2459;
2460  %pop = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> %x)
2461  %cmp = icmp sgt <2 x i13> %pop, <i13 13, i13 13>
2462  ret <2 x i1> %cmp
2463}
2464
2465define i1 @cttz_ult_plus1_bitwidth(i11 %x) {
2466; CHECK-LABEL: @cttz_ult_plus1_bitwidth(
2467; CHECK-NEXT:    ret i1 true
2468;
2469  %pop = call i11 @llvm.cttz.i11(i11 %x)
2470  %cmp = icmp ult i11 %pop, 12
2471  ret i1 %cmp
2472}
2473
2474define i1 @cttz_ne_big_bitwidth(i73 %x) {
2475; CHECK-LABEL: @cttz_ne_big_bitwidth(
2476; CHECK-NEXT:    ret i1 true
2477;
2478  %pop = call i73 @llvm.cttz.i73(i73 %x)
2479  %cmp = icmp ne i73 %pop, 75
2480  ret i1 %cmp
2481}
2482
2483define <2 x i1> @cttz_slt_bitwidth_plus1_splat(<2 x i13> %x) {
2484; CHECK-LABEL: @cttz_slt_bitwidth_plus1_splat(
2485; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
2486;
2487  %pop = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> %x)
2488  %cmp = icmp slt <2 x i13> %pop, <i13 14, i13 14>
2489  ret <2 x i1> %cmp
2490}
2491
2492; Negative test - does not simplify, but instcombine could reduce this.
2493
2494define <2 x i1> @cttz_slt_bitwidth_splat(<2 x i13> %x) {
2495; CHECK-LABEL: @cttz_slt_bitwidth_splat(
2496; CHECK-NEXT:    [[POP:%.*]] = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> [[X:%.*]], i1 false)
2497; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i13> [[POP]], <i13 13, i13 13>
2498; CHECK-NEXT:    ret <2 x i1> [[CMP]]
2499;
2500  %pop = call <2 x i13> @llvm.cttz.v2i13(<2 x i13> %x)
2501  %cmp = icmp slt <2 x i13> %pop, <i13 13, i13 13>
2502  ret <2 x i1> %cmp
2503}
2504
2505attributes #0 = { null_pointer_is_valid }
2506