xref: /llvm-project/llvm/test/Transforms/InstCombine/not.ll (revision 38fffa630ee80163dc65e759392ad29798905679)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4declare void @use1(i1)
5declare void @use8(i8)
6declare void @f1()
7declare void @f2()
8
9define i32 @test1(i32 %A) {
10; CHECK-LABEL: @test1(
11; CHECK-NEXT:    ret i32 [[A:%.*]]
12;
13  %B = xor i32 %A, -1
14  %C = xor i32 %B, -1
15  ret i32 %C
16}
17
18define i1 @invert_icmp(i32 %A, i32 %B) {
19; CHECK-LABEL: @invert_icmp(
20; CHECK-NEXT:    [[CMP_NOT:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
21; CHECK-NEXT:    ret i1 [[CMP_NOT]]
22;
23  %cmp = icmp sle i32 %A, %B
24  %not = xor i1 %cmp, true
25  ret i1 %not
26}
27
28; PR1570
29
30define i1 @invert_fcmp(float %X, float %Y) {
31; CHECK-LABEL: @invert_fcmp(
32; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[X:%.*]], [[Y:%.*]]
33; CHECK-NEXT:    ret i1 [[CMP]]
34;
35  %cmp = fcmp olt float %X, %Y
36  %not = xor i1 %cmp, true
37  ret i1 %not
38}
39
40; PR2298
41
42define i1 @not_not_cmp(i32 %a, i32 %b) {
43; CHECK-LABEL: @not_not_cmp(
44; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
45; CHECK-NEXT:    ret i1 [[CMP]]
46;
47  %nota = xor i32 %a, -1
48  %notb = xor i32 %b, -1
49  %cmp = icmp slt i32 %nota, %notb
50  ret i1 %cmp
51}
52
53define <2 x i1> @not_not_cmp_vector(<2 x i32> %a, <2 x i32> %b) {
54; CHECK-LABEL: @not_not_cmp_vector(
55; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <2 x i32> [[A:%.*]], [[B:%.*]]
56; CHECK-NEXT:    ret <2 x i1> [[CMP]]
57;
58  %nota = xor <2 x i32> %a, <i32 -1, i32 -1>
59  %notb = xor <2 x i32> %b, <i32 -1, i32 -1>
60  %cmp = icmp ugt <2 x i32> %nota, %notb
61  ret <2 x i1> %cmp
62}
63
64define i1 @not_cmp_constant(i32 %a) {
65; CHECK-LABEL: @not_cmp_constant(
66; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[A:%.*]], -43
67; CHECK-NEXT:    ret i1 [[CMP]]
68;
69  %nota = xor i32 %a, -1
70  %cmp = icmp ugt i32 %nota, 42
71  ret i1 %cmp
72}
73
74define <2 x i1> @not_cmp_constant_vector(<2 x i32> %a) {
75; CHECK-LABEL: @not_cmp_constant_vector(
76; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt <2 x i32> [[A:%.*]], splat (i32 -43)
77; CHECK-NEXT:    ret <2 x i1> [[CMP]]
78;
79  %nota = xor <2 x i32> %a, <i32 -1, i32 -1>
80  %cmp = icmp slt <2 x i32> %nota, <i32 42, i32 42>
81  ret <2 x i1> %cmp
82}
83
84define <2 x i1> @test7(<2 x i32> %A, <2 x i32> %B) {
85; CHECK-LABEL: @test7(
86; CHECK-NEXT:    [[COND_NOT:%.*]] = icmp sgt <2 x i32> [[A:%.*]], [[B:%.*]]
87; CHECK-NEXT:    ret <2 x i1> [[COND_NOT]]
88;
89  %cond = icmp sle <2 x i32> %A, %B
90  %Ret = xor <2 x i1> %cond, <i1 true, i1 true>
91  ret <2 x i1> %Ret
92}
93
94define i32 @not_ashr_not(i32 %A, i32 %B) {
95; CHECK-LABEL: @not_ashr_not(
96; CHECK-NEXT:    [[NOT1_NOT:%.*]] = ashr i32 [[A:%.*]], [[B:%.*]]
97; CHECK-NEXT:    ret i32 [[NOT1_NOT]]
98;
99  %not1 = xor i32 %A, -1
100  %ashr = ashr i32 %not1, %B
101  %not2 = xor i32 %ashr, -1
102  ret i32 %not2
103}
104
105define i8 @not_ashr_const(i8 %x) {
106; CHECK-LABEL: @not_ashr_const(
107; CHECK-NEXT:    [[NOT:%.*]] = lshr i8 41, [[X:%.*]]
108; CHECK-NEXT:    ret i8 [[NOT]]
109;
110  %shr = ashr i8 -42, %x
111  %not = xor i8 %shr, -1
112  ret i8 %not
113}
114
115define <2 x i8> @not_ashr_const_splat(<2 x i8> %x) {
116; CHECK-LABEL: @not_ashr_const_splat(
117; CHECK-NEXT:    [[NOT:%.*]] = lshr <2 x i8> splat (i8 41), [[X:%.*]]
118; CHECK-NEXT:    ret <2 x i8> [[NOT]]
119;
120  %shr = ashr <2 x i8> <i8 -42, i8 -42>, %x
121  %not = xor <2 x i8> %shr, <i8 -1, i8 -1>
122  ret <2 x i8> %not
123}
124
125; We can't get rid of the 'not' on a logical shift of a negative constant.
126
127define i8 @not_lshr_const_negative(i8 %x) {
128; CHECK-LABEL: @not_lshr_const_negative(
129; CHECK-NEXT:    [[SHR:%.*]] = lshr i8 -42, [[X:%.*]]
130; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SHR]], -1
131; CHECK-NEXT:    ret i8 [[NOT]]
132;
133  %shr = lshr i8 -42, %x
134  %not = xor i8 %shr, -1
135  ret i8 %not
136}
137
138define i8 @not_lshr_const(i8 %x) {
139; CHECK-LABEL: @not_lshr_const(
140; CHECK-NEXT:    [[NOT:%.*]] = ashr i8 -43, [[X:%.*]]
141; CHECK-NEXT:    ret i8 [[NOT]]
142;
143  %shr = lshr i8 42, %x
144  %not = xor i8 %shr, -1
145  ret i8 %not
146}
147
148define <2 x i8> @not_lshr_const_splat(<2 x i8> %x) {
149; CHECK-LABEL: @not_lshr_const_splat(
150; CHECK-NEXT:    [[NOT:%.*]] = ashr <2 x i8> splat (i8 -43), [[X:%.*]]
151; CHECK-NEXT:    ret <2 x i8> [[NOT]]
152;
153  %shr = lshr <2 x i8> <i8 42, i8 42>, %x
154  %not = xor <2 x i8> %shr, <i8 -1, i8 -1>
155  ret <2 x i8> %not
156}
157
158define i32 @not_sub(i32 %y) {
159; CHECK-LABEL: @not_sub(
160; CHECK-NEXT:    [[R:%.*]] = add i32 [[Y:%.*]], -124
161; CHECK-NEXT:    ret i32 [[R]]
162;
163  %s = sub i32 123, %y
164  %r = xor i32 %s, -1
165  ret i32 %r
166}
167
168define i32 @not_sub_extra_use(i32 %y, ptr %p) {
169; CHECK-LABEL: @not_sub_extra_use(
170; CHECK-NEXT:    [[S:%.*]] = sub i32 123, [[Y:%.*]]
171; CHECK-NEXT:    store i32 [[S]], ptr [[P:%.*]], align 4
172; CHECK-NEXT:    [[R:%.*]] = add i32 [[Y]], -124
173; CHECK-NEXT:    ret i32 [[R]]
174;
175  %s = sub i32 123, %y
176  store i32 %s, ptr %p
177  %r = xor i32 %s, -1
178  ret i32 %r
179}
180
181define <2 x i32> @not_sub_splat(<2 x i32> %y) {
182; CHECK-LABEL: @not_sub_splat(
183; CHECK-NEXT:    [[R:%.*]] = add <2 x i32> [[Y:%.*]], splat (i32 -124)
184; CHECK-NEXT:    ret <2 x i32> [[R]]
185;
186  %s = sub <2 x i32> <i32 123, i32 123>, %y
187  %r = xor <2 x i32> %s, <i32 -1, i32 -1>
188  ret <2 x i32> %r
189}
190
191define <2 x i32> @not_sub_extra_use_splat(<2 x i32> %y, ptr %p) {
192; CHECK-LABEL: @not_sub_extra_use_splat(
193; CHECK-NEXT:    [[S:%.*]] = sub <2 x i32> splat (i32 123), [[Y:%.*]]
194; CHECK-NEXT:    store <2 x i32> [[S]], ptr [[P:%.*]], align 8
195; CHECK-NEXT:    [[R:%.*]] = add <2 x i32> [[Y]], splat (i32 -124)
196; CHECK-NEXT:    ret <2 x i32> [[R]]
197;
198  %s = sub <2 x i32> <i32 123, i32 123>, %y
199  store <2 x i32> %s, ptr %p
200  %r = xor <2 x i32> %s, <i32 -1, i32 -1>
201  ret <2 x i32> %r
202}
203
204define <2 x i32> @not_sub_vec(<2 x i32> %y) {
205; CHECK-LABEL: @not_sub_vec(
206; CHECK-NEXT:    [[R:%.*]] = add <2 x i32> [[Y:%.*]], <i32 -43, i32 -124>
207; CHECK-NEXT:    ret <2 x i32> [[R]]
208;
209  %s = sub <2 x i32> <i32 42, i32 123>, %y
210  %r = xor <2 x i32> %s, <i32 -1, i32 -1>
211  ret <2 x i32> %r
212}
213
214define <2 x i32> @not_sub_extra_use_vec(<2 x i32> %y, ptr %p) {
215; CHECK-LABEL: @not_sub_extra_use_vec(
216; CHECK-NEXT:    [[S:%.*]] = sub <2 x i32> <i32 123, i32 42>, [[Y:%.*]]
217; CHECK-NEXT:    store <2 x i32> [[S]], ptr [[P:%.*]], align 8
218; CHECK-NEXT:    [[R:%.*]] = add <2 x i32> [[Y]], <i32 -124, i32 -43>
219; CHECK-NEXT:    ret <2 x i32> [[R]]
220;
221  %s = sub <2 x i32> <i32 123, i32 42>, %y
222  store <2 x i32> %s, ptr %p
223  %r = xor <2 x i32> %s, <i32 -1, i32 -1>
224  ret <2 x i32> %r
225}
226
227; ~(X + C) --> -X - C - 1 --> -(C + 1) - X
228
229define i32 @not_add(i32 %x) {
230; CHECK-LABEL: @not_add(
231; CHECK-NEXT:    [[R:%.*]] = sub i32 -124, [[X:%.*]]
232; CHECK-NEXT:    ret i32 [[R]]
233;
234  %a = add i32 %x, 123
235  %r = xor i32 %a, -1
236  ret i32 %r
237}
238
239define <2 x i32> @not_add_splat(<2 x i32> %x) {
240; CHECK-LABEL: @not_add_splat(
241; CHECK-NEXT:    [[R:%.*]] = sub <2 x i32> splat (i32 -124), [[X:%.*]]
242; CHECK-NEXT:    ret <2 x i32> [[R]]
243;
244  %a = add <2 x i32> %x, <i32 123, i32 123>
245  %r = xor <2 x i32> %a, <i32 -1, i32 -1>
246  ret <2 x i32> %r
247}
248
249define <2 x i32> @not_add_vec(<2 x i32> %x) {
250; CHECK-LABEL: @not_add_vec(
251; CHECK-NEXT:    [[R:%.*]] = sub <2 x i32> <i32 -43, i32 -124>, [[X:%.*]]
252; CHECK-NEXT:    ret <2 x i32> [[R]]
253;
254  %a = add <2 x i32> %x, <i32 42, i32 123>
255  %r = xor <2 x i32> %a, <i32 -1, i32 -1>
256  ret <2 x i32> %r
257}
258
259define i1 @not_select_cmp_cmp(i32 %x, i32 %y, float %z, float %w, i1 %cond) {
260; CHECK-LABEL: @not_select_cmp_cmp(
261; CHECK-NEXT:    [[CMPT:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
262; CHECK-NEXT:    [[CMPF:%.*]] = fcmp ole float [[Z:%.*]], [[W:%.*]]
263; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]]
264; CHECK-NEXT:    ret i1 [[SEL]]
265;
266  %cmpt = icmp sle i32 %x, %y
267  %cmpf = fcmp ugt float %z, %w
268  %sel = select i1 %cond, i1 %cmpt, i1 %cmpf
269  %not = xor i1 %sel, true
270  ret i1 %not
271}
272
273; TODO: Missed canonicalization - hoist 'not'?
274
275define i1 @not_select_cmp_cmp_extra_use1(i32 %x, i32 %y, float %z, float %w, i1 %cond) {
276; CHECK-LABEL: @not_select_cmp_cmp_extra_use1(
277; CHECK-NEXT:    [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
278; CHECK-NEXT:    call void @use1(i1 [[CMPT]])
279; CHECK-NEXT:    [[CMPF:%.*]] = fcmp ugt float [[Z:%.*]], [[W:%.*]]
280; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]]
281; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
282; CHECK-NEXT:    ret i1 [[NOT]]
283;
284  %cmpt = icmp sle i32 %x, %y
285  call void @use1(i1 %cmpt)
286  %cmpf = fcmp ugt float %z, %w
287  %sel = select i1 %cond, i1 %cmpt, i1 %cmpf
288  %not = xor i1 %sel, true
289  ret i1 %not
290}
291
292; TODO: Missed canonicalization - hoist 'not'?
293
294define i1 @not_select_cmp_cmp_extra_use2(i32 %x, i32 %y, float %z, float %w, i1 %cond) {
295; CHECK-LABEL: @not_select_cmp_cmp_extra_use2(
296; CHECK-NEXT:    [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
297; CHECK-NEXT:    [[CMPF:%.*]] = fcmp ugt float [[Z:%.*]], [[W:%.*]]
298; CHECK-NEXT:    call void @use1(i1 [[CMPF]])
299; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]]
300; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
301; CHECK-NEXT:    ret i1 [[NOT]]
302;
303  %cmpt = icmp sle i32 %x, %y
304  %cmpf = fcmp ugt float %z, %w
305  call void @use1(i1 %cmpf)
306  %sel = select i1 %cond, i1 %cmpt, i1 %cmpf
307  %not = xor i1 %sel, true
308  ret i1 %not
309}
310
311; Negative test - extra uses would require more instructions.
312
313define i1 @not_select_cmp_cmp_extra_use3(i32 %x, i32 %y, float %z, float %w, i1 %cond) {
314; CHECK-LABEL: @not_select_cmp_cmp_extra_use3(
315; CHECK-NEXT:    [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
316; CHECK-NEXT:    call void @use1(i1 [[CMPT]])
317; CHECK-NEXT:    [[CMPF:%.*]] = fcmp ugt float [[Z:%.*]], [[W:%.*]]
318; CHECK-NEXT:    call void @use1(i1 [[CMPF]])
319; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]]
320; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
321; CHECK-NEXT:    ret i1 [[NOT]]
322;
323  %cmpt = icmp sle i32 %x, %y
324  call void @use1(i1 %cmpt)
325  %cmpf = fcmp ugt float %z, %w
326  call void @use1(i1 %cmpf)
327  %sel = select i1 %cond, i1 %cmpt, i1 %cmpf
328  %not = xor i1 %sel, true
329  ret i1 %not
330}
331
332; Negative test - extra uses would require more instructions.
333
334define i1 @not_select_cmp_cmp_extra_use4(i32 %x, i32 %y, float %z, float %w, i1 %cond) {
335; CHECK-LABEL: @not_select_cmp_cmp_extra_use4(
336; CHECK-NEXT:    [[CMPT:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
337; CHECK-NEXT:    [[CMPF:%.*]] = fcmp ugt float [[Z:%.*]], [[W:%.*]]
338; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[CMPF]]
339; CHECK-NEXT:    call void @use1(i1 [[SEL]])
340; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
341; CHECK-NEXT:    ret i1 [[NOT]]
342;
343  %cmpt = icmp sle i32 %x, %y
344  %cmpf = fcmp ugt float %z, %w
345  %sel = select i1 %cond, i1 %cmpt, i1 %cmpf
346  call void @use1(i1 %sel)
347  %not = xor i1 %sel, true
348  ret i1 %not
349}
350
351; TODO: Missed canonicalization - hoist 'not'?
352
353define i1 @not_select_cmpt(double %x, double %y, i1 %z, i1 %cond) {
354; CHECK-LABEL: @not_select_cmpt(
355; CHECK-NEXT:    [[CMPT:%.*]] = fcmp oeq double [[X:%.*]], [[Y:%.*]]
356; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[Z:%.*]]
357; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
358; CHECK-NEXT:    ret i1 [[NOT]]
359;
360  %cmpt = fcmp oeq double %x, %y
361  %sel = select i1 %cond, i1 %cmpt, i1 %z
362  %not = xor i1 %sel, true
363  ret i1 %not
364}
365
366; TODO: Missed canonicalization - hoist 'not'?
367
368define i1 @not_select_cmpf(i1 %x, i32 %z, i32 %w, i1 %cond) {
369; CHECK-LABEL: @not_select_cmpf(
370; CHECK-NEXT:    [[CMPF:%.*]] = icmp ugt i32 [[Z:%.*]], [[W:%.*]]
371; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[X:%.*]], i1 [[CMPF]]
372; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
373; CHECK-NEXT:    ret i1 [[NOT]]
374;
375  %cmpf = icmp ugt i32 %z, %w
376  %sel = select i1 %cond, i1 %x, i1 %cmpf
377  %not = xor i1 %sel, true
378  ret i1 %not
379}
380
381define i1 @not_select_cmpt_extra_use(double %x, double %y, i1 %z, i1 %cond) {
382; CHECK-LABEL: @not_select_cmpt_extra_use(
383; CHECK-NEXT:    [[CMPT:%.*]] = fcmp oeq double [[X:%.*]], [[Y:%.*]]
384; CHECK-NEXT:    call void @use1(i1 [[CMPT]])
385; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[CMPT]], i1 [[Z:%.*]]
386; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
387; CHECK-NEXT:    ret i1 [[NOT]]
388;
389  %cmpt = fcmp oeq double %x, %y
390  call void @use1(i1 %cmpt)
391  %sel = select i1 %cond, i1 %cmpt, i1 %z
392  %not = xor i1 %sel, true
393  ret i1 %not
394}
395
396define i1 @not_select_cmpf_extra_use(i1 %x, i32 %z, i32 %w, i1 %cond) {
397; CHECK-LABEL: @not_select_cmpf_extra_use(
398; CHECK-NEXT:    [[CMPF:%.*]] = icmp ugt i32 [[Z:%.*]], [[W:%.*]]
399; CHECK-NEXT:    call void @use1(i1 [[CMPF]])
400; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i1 [[X:%.*]], i1 [[CMPF]]
401; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[SEL]], true
402; CHECK-NEXT:    ret i1 [[NOT]]
403;
404  %cmpf = icmp ugt i32 %z, %w
405  call void @use1(i1 %cmpf)
406  %sel = select i1 %cond, i1 %x, i1 %cmpf
407  %not = xor i1 %sel, true
408  ret i1 %not
409}
410
411define i8 @not_or_neg(i8 %x, i8 %y)  {
412; CHECK-LABEL: @not_or_neg(
413; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
414; CHECK-NEXT:    [[TMP2:%.*]] = xor i8 [[X:%.*]], -1
415; CHECK-NEXT:    [[NOT:%.*]] = and i8 [[TMP1]], [[TMP2]]
416; CHECK-NEXT:    ret i8 [[NOT]]
417;
418  %s = sub i8 0, %y
419  %o = or i8 %s, %x
420  %not = xor i8 %o, -1
421  ret i8 %not
422}
423
424define <3 x i5> @not_or_neg_commute_vec(<3 x i5> %x, <3 x i5> %p)  {
425; CHECK-LABEL: @not_or_neg_commute_vec(
426; CHECK-NEXT:    [[Y:%.*]] = mul <3 x i5> [[P:%.*]], <i5 1, i5 2, i5 3>
427; CHECK-NEXT:    [[TMP1:%.*]] = add <3 x i5> [[X:%.*]], splat (i5 -1)
428; CHECK-NEXT:    [[TMP2:%.*]] = xor <3 x i5> [[Y]], splat (i5 -1)
429; CHECK-NEXT:    [[NOT:%.*]] = and <3 x i5> [[TMP1]], [[TMP2]]
430; CHECK-NEXT:    ret <3 x i5> [[NOT]]
431;
432  %y = mul <3 x i5> %p, <i5 1, i5 2, i5 3> ; thwart complexity-based-canonicalization
433  %s = sub <3 x i5> <i5 0, i5 0, i5 poison>, %x
434  %o = or <3 x i5> %y, %s
435  %not = xor <3 x i5> %o, <i5 -1, i5 poison, i5 -1>
436  ret <3 x i5> %not
437}
438
439; negative test
440
441define i8 @not_or_neg_use1(i8 %x, i8 %y)  {
442; CHECK-LABEL: @not_or_neg_use1(
443; CHECK-NEXT:    [[S:%.*]] = sub i8 0, [[Y:%.*]]
444; CHECK-NEXT:    call void @use8(i8 [[S]])
445; CHECK-NEXT:    [[O:%.*]] = or i8 [[X:%.*]], [[S]]
446; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[O]], -1
447; CHECK-NEXT:    ret i8 [[NOT]]
448;
449  %s = sub i8 0, %y
450  call void @use8(i8 %s)
451  %o = or i8 %s, %x
452  %not = xor i8 %o, -1
453  ret i8 %not
454}
455
456; negative test
457
458define i8 @not_or_neg_use2(i8 %x, i8 %y)  {
459; CHECK-LABEL: @not_or_neg_use2(
460; CHECK-NEXT:    [[S:%.*]] = sub i8 0, [[Y:%.*]]
461; CHECK-NEXT:    [[O:%.*]] = or i8 [[X:%.*]], [[S]]
462; CHECK-NEXT:    call void @use8(i8 [[O]])
463; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[O]], -1
464; CHECK-NEXT:    ret i8 [[NOT]]
465;
466  %s = sub i8 0, %y
467  %o = or i8 %s, %x
468  call void @use8(i8 %o)
469  %not = xor i8 %o, -1
470  ret i8 %not
471}
472
473define i1 @not_select_bool(i1 %x, i1 %y, i1 %z) {
474; CHECK-LABEL: @not_select_bool(
475; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[X:%.*]], i1 [[Y:%.*]], i1 [[Z:%.*]]
476; CHECK-NEXT:    [[R:%.*]] = xor i1 [[SEL]], true
477; CHECK-NEXT:    ret i1 [[R]]
478;
479  %sel = select i1 %x, i1 %y, i1 %z
480  %r = xor i1 %sel, true
481  ret i1 %r
482}
483
484define i1 @not_select_bool_const1(i1 %x, i1 %y) {
485; CHECK-LABEL: @not_select_bool_const1(
486; CHECK-NEXT:    [[Y_NOT:%.*]] = xor i1 [[Y:%.*]], true
487; CHECK-NEXT:    [[R:%.*]] = select i1 [[X:%.*]], i1 [[Y_NOT]], i1 false
488; CHECK-NEXT:    ret i1 [[R]]
489;
490  %sel = select i1 %x, i1 %y, i1 true
491  %r = xor i1 %sel, true
492  ret i1 %r
493}
494
495define i1 @not_select_bool_const2(i1 %x, i1 %y) {
496; CHECK-LABEL: @not_select_bool_const2(
497; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[X:%.*]], i1 [[Y:%.*]], i1 false
498; CHECK-NEXT:    [[R:%.*]] = xor i1 [[SEL]], true
499; CHECK-NEXT:    ret i1 [[R]]
500;
501  %sel = select i1 %x, i1 %y, i1 false
502  %r = xor i1 %sel, true
503  ret i1 %r
504}
505
506define i1 @not_select_bool_const3(i1 %x, i1 %y) {
507; CHECK-LABEL: @not_select_bool_const3(
508; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]]
509; CHECK-NEXT:    [[R:%.*]] = xor i1 [[SEL]], true
510; CHECK-NEXT:    ret i1 [[R]]
511;
512  %sel = select i1 %x, i1 true, i1 %y
513  %r = xor i1 %sel, true
514  ret i1 %r
515}
516
517define i1 @not_select_bool_const4(i1 %x, i1 %y) {
518; CHECK-LABEL: @not_select_bool_const4(
519; CHECK-NEXT:    [[Y_NOT:%.*]] = xor i1 [[Y:%.*]], true
520; CHECK-NEXT:    [[R:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y_NOT]]
521; CHECK-NEXT:    ret i1 [[R]]
522;
523  %sel = select i1 %x, i1 false, i1 %y
524  %r = xor i1 %sel, true
525  ret i1 %r
526}
527
528define <2 x i1> @not_logicalAnd_not_op0(<2 x i1> %x, <2 x i1> %y) {
529; CHECK-LABEL: @not_logicalAnd_not_op0(
530; CHECK-NEXT:    [[Y_NOT:%.*]] = xor <2 x i1> [[Y:%.*]], splat (i1 true)
531; CHECK-NEXT:    [[NOTAND:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> splat (i1 true), <2 x i1> [[Y_NOT]]
532; CHECK-NEXT:    ret <2 x i1> [[NOTAND]]
533;
534  %notx = xor <2 x i1> %x, <i1 true, i1 true>
535  %and = select <2 x i1> %notx, <2 x i1> %y, <2 x i1> zeroinitializer
536  %notand = xor <2 x i1> %and, <i1 true, i1 true>
537  ret <2 x i1> %notand
538}
539
540define i1 @not_logicalAnd_not_op1(i1 %x, i1 %y) {
541; CHECK-LABEL: @not_logicalAnd_not_op1(
542; CHECK-NEXT:    [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
543; CHECK-NEXT:    [[NOTAND:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]]
544; CHECK-NEXT:    ret i1 [[NOTAND]]
545;
546  %noty = xor i1 %y, true
547  %and = select i1 %x, i1 %noty, i1 false
548  %notand = xor i1 %and, true
549  ret i1 %notand
550}
551
552define i1 @not_logicalAnd_not_op0_use1(i1 %x, i1 %y) {
553; CHECK-LABEL: @not_logicalAnd_not_op0_use1(
554; CHECK-NEXT:    [[NOTX:%.*]] = xor i1 [[X:%.*]], true
555; CHECK-NEXT:    call void @use1(i1 [[NOTX]])
556; CHECK-NEXT:    [[Y_NOT:%.*]] = xor i1 [[Y:%.*]], true
557; CHECK-NEXT:    [[NOTAND:%.*]] = select i1 [[X]], i1 true, i1 [[Y_NOT]]
558; CHECK-NEXT:    ret i1 [[NOTAND]]
559;
560  %notx = xor i1 %x, true
561  call void @use1(i1 %notx)
562  %and = select i1 %notx, i1 %y, i1 false
563  %notand = xor i1 %and, true
564  ret i1 %notand
565}
566
567; negative test
568
569define i1 @not_logicalAnd_not_op0_use2(i1 %x, i1 %y) {
570; CHECK-LABEL: @not_logicalAnd_not_op0_use2(
571; CHECK-NEXT:    [[NOTX:%.*]] = xor i1 [[X:%.*]], true
572; CHECK-NEXT:    [[AND:%.*]] = select i1 [[NOTX]], i1 [[Y:%.*]], i1 false
573; CHECK-NEXT:    call void @use1(i1 [[AND]])
574; CHECK-NEXT:    [[NOTAND:%.*]] = xor i1 [[AND]], true
575; CHECK-NEXT:    ret i1 [[NOTAND]]
576;
577  %notx = xor i1 %x, true
578  %and = select i1 %notx, i1 %y, i1 false
579  call void @use1(i1 %and)
580  %notand = xor i1 %and, true
581  ret i1 %notand
582}
583
584define <2 x i1> @not_logicalOr_not_op0(<2 x i1> %x, <2 x i1> %y) {
585; CHECK-LABEL: @not_logicalOr_not_op0(
586; CHECK-NEXT:    [[Y_NOT:%.*]] = xor <2 x i1> [[Y:%.*]], splat (i1 true)
587; CHECK-NEXT:    [[NOTOR:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> [[Y_NOT]], <2 x i1> zeroinitializer
588; CHECK-NEXT:    ret <2 x i1> [[NOTOR]]
589;
590  %notx = xor <2 x i1> %x, <i1 true, i1 true>
591  %or = select <2 x i1> %notx, <2 x i1> <i1 true, i1 true>, <2 x i1> %y
592  %notor = xor <2 x i1> %or, <i1 true, i1 true>
593  ret <2 x i1> %notor
594}
595
596define i1 @not_logicalOr_not_op1(i1 %x, i1 %y) {
597; CHECK-LABEL: @not_logicalOr_not_op1(
598; CHECK-NEXT:    [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
599; CHECK-NEXT:    [[NOTOR:%.*]] = select i1 [[NOT_X]], i1 [[Y:%.*]], i1 false
600; CHECK-NEXT:    ret i1 [[NOTOR]]
601;
602  %noty = xor i1 %y, true
603  %or = select i1 %x, i1 true, i1 %noty
604  %notor = xor i1 %or, true
605  ret i1 %notor
606}
607
608define i1 @not_logicalOr_not_op0_use1(i1 %x, i1 %y) {
609; CHECK-LABEL: @not_logicalOr_not_op0_use1(
610; CHECK-NEXT:    [[NOTX:%.*]] = xor i1 [[X:%.*]], true
611; CHECK-NEXT:    call void @use1(i1 [[NOTX]])
612; CHECK-NEXT:    [[Y_NOT:%.*]] = xor i1 [[Y:%.*]], true
613; CHECK-NEXT:    [[NOTOR:%.*]] = select i1 [[X]], i1 [[Y_NOT]], i1 false
614; CHECK-NEXT:    ret i1 [[NOTOR]]
615;
616  %notx = xor i1 %x, true
617  call void @use1(i1 %notx)
618  %or = select i1 %notx, i1 true, i1 %y
619  %notor = xor i1 %or, true
620  ret i1 %notor
621}
622
623; negative test
624
625define i1 @not_logicalOr_not_op0_use2(i1 %x, i1 %y) {
626; CHECK-LABEL: @not_logicalOr_not_op0_use2(
627; CHECK-NEXT:    [[NOTX:%.*]] = xor i1 [[X:%.*]], true
628; CHECK-NEXT:    [[OR:%.*]] = select i1 [[NOTX]], i1 true, i1 [[Y:%.*]]
629; CHECK-NEXT:    call void @use1(i1 [[OR]])
630; CHECK-NEXT:    [[NOTOR:%.*]] = xor i1 [[OR]], true
631; CHECK-NEXT:    ret i1 [[NOTOR]]
632;
633  %notx = xor i1 %x, true
634  %or = select i1 %notx, i1 true, i1 %y
635  call void @use1(i1 %or)
636  %notor = xor i1 %or, true
637  ret i1 %notor
638}
639
640; canonicalize 'not' ahead of casts of a bool value
641
642define <2 x i64> @bitcast_to_wide_elts_sext_bool(<4 x i1> %b) {
643; CHECK-LABEL: @bitcast_to_wide_elts_sext_bool(
644; CHECK-NEXT:    [[TMP1:%.*]] = xor <4 x i1> [[B:%.*]], splat (i1 true)
645; CHECK-NEXT:    [[TMP2:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32>
646; CHECK-NEXT:    [[NOT:%.*]] = bitcast <4 x i32> [[TMP2]] to <2 x i64>
647; CHECK-NEXT:    ret <2 x i64> [[NOT]]
648;
649  %sext = sext <4 x i1> %b to <4 x i32>
650  %bc = bitcast <4 x i32> %sext to <2 x i64>
651  %not = xor <2 x i64> %bc, <i64 -1, i64 -1>
652  ret <2 x i64> %not
653}
654
655define <8 x i16> @bitcast_to_narrow_elts_sext_bool(<4 x i1> %b) {
656; CHECK-LABEL: @bitcast_to_narrow_elts_sext_bool(
657; CHECK-NEXT:    [[TMP1:%.*]] = xor <4 x i1> [[B:%.*]], splat (i1 true)
658; CHECK-NEXT:    [[TMP2:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32>
659; CHECK-NEXT:    [[NOT:%.*]] = bitcast <4 x i32> [[TMP2]] to <8 x i16>
660; CHECK-NEXT:    ret <8 x i16> [[NOT]]
661;
662  %sext = sext <4 x i1> %b to <4 x i32>
663  %bc = bitcast <4 x i32> %sext to <8 x i16>
664  %not = xor <8 x i16> %bc, <i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1>
665  ret <8 x i16> %not
666}
667
668define <2 x i16> @bitcast_to_vec_sext_bool(i1 %b) {
669; CHECK-LABEL: @bitcast_to_vec_sext_bool(
670; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[B:%.*]], true
671; CHECK-NEXT:    [[TMP2:%.*]] = sext i1 [[TMP1]] to i32
672; CHECK-NEXT:    [[NOT:%.*]] = bitcast i32 [[TMP2]] to <2 x i16>
673; CHECK-NEXT:    ret <2 x i16> [[NOT]]
674;
675  %sext = sext i1 %b to i32
676  %bc = bitcast i32 %sext to <2 x i16>
677  %not = xor <2 x i16> %bc, <i16 -1, i16 -1>
678  ret <2 x i16> %not
679}
680
681define i128 @bitcast_to_scalar_sext_bool(<4 x i1> %b) {
682; CHECK-LABEL: @bitcast_to_scalar_sext_bool(
683; CHECK-NEXT:    [[TMP1:%.*]] = xor <4 x i1> [[B:%.*]], splat (i1 true)
684; CHECK-NEXT:    [[TMP2:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32>
685; CHECK-NEXT:    [[NOT:%.*]] = bitcast <4 x i32> [[TMP2]] to i128
686; CHECK-NEXT:    ret i128 [[NOT]]
687;
688  %sext = sext <4 x i1> %b to <4 x i32>
689  %bc = bitcast <4 x i32> %sext to i128
690  %not = xor i128 %bc, -1
691  ret i128 %not
692}
693
694; negative test
695
696define <2 x i4> @bitcast_to_vec_sext_bool_use1(i1 %b) {
697; CHECK-LABEL: @bitcast_to_vec_sext_bool_use1(
698; CHECK-NEXT:    [[SEXT:%.*]] = sext i1 [[B:%.*]] to i8
699; CHECK-NEXT:    call void @use8(i8 [[SEXT]])
700; CHECK-NEXT:    [[BC:%.*]] = bitcast i8 [[SEXT]] to <2 x i4>
701; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i4> [[BC]], splat (i4 -1)
702; CHECK-NEXT:    ret <2 x i4> [[NOT]]
703;
704  %sext = sext i1 %b to i8
705  call void @use8(i8 %sext)
706  %bc = bitcast i8 %sext to <2 x i4>
707  %not = xor <2 x i4> %bc, <i4 -1, i4 -1>
708  ret <2 x i4> %not
709}
710
711; negative test
712
713define i8 @bitcast_to_scalar_sext_bool_use2(<4 x i1> %b) {
714; CHECK-LABEL: @bitcast_to_scalar_sext_bool_use2(
715; CHECK-NEXT:    [[SEXT:%.*]] = sext <4 x i1> [[B:%.*]] to <4 x i2>
716; CHECK-NEXT:    [[BC:%.*]] = bitcast <4 x i2> [[SEXT]] to i8
717; CHECK-NEXT:    call void @use8(i8 [[BC]])
718; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[BC]], -1
719; CHECK-NEXT:    ret i8 [[NOT]]
720;
721  %sext = sext <4 x i1> %b to <4 x i2>
722  %bc = bitcast <4 x i2> %sext to i8
723  call void @use8(i8 %bc)
724  %not = xor i8 %bc, -1
725  ret i8 %not
726}
727
728; PR74302
729define i1 @invert_both_cmp_operands_add(i32 %a, i32 %b) {
730; CHECK-LABEL: @invert_both_cmp_operands_add(
731; CHECK-NEXT:  entry:
732; CHECK-NEXT:    [[TMP0:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
733; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[TMP0]], -1
734; CHECK-NEXT:    ret i1 [[CMP]]
735;
736entry:
737  %not.a = xor i32 %a, -1
738  %add = add i32 %b, %not.a
739  %cmp = icmp sgt i32 %add, 0
740  ret i1 %cmp
741}
742
743define i1 @invert_both_cmp_operands_sub(i32 %a, i32 %b) {
744; CHECK-LABEL: @invert_both_cmp_operands_sub(
745; CHECK-NEXT:  entry:
746; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[A:%.*]], [[B:%.*]]
747; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[TMP0]], -43
748; CHECK-NEXT:    ret i1 [[CMP]]
749;
750entry:
751  %not.a = xor i32 %a, -1
752  %add = sub i32 %not.a, %b
753  %cmp = icmp ult i32 %add, 42
754  ret i1 %cmp
755}
756
757define i1 @invert_both_cmp_operands_complex(i1 %x, i32 %a, i32 %b, i32 %c) {
758; CHECK-LABEL: @invert_both_cmp_operands_complex(
759; CHECK-NEXT:  entry:
760; CHECK-NEXT:    [[TMP0:%.*]] = sub i32 [[A:%.*]], [[C:%.*]]
761; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[X:%.*]], i32 [[TMP0]], i32 [[B:%.*]]
762; CHECK-NEXT:    [[CMP:%.*]] = icmp sge i32 [[TMP1]], [[C]]
763; CHECK-NEXT:    ret i1 [[CMP]]
764;
765entry:
766  %not.a = xor i32 %a, -1
767  %not.b = xor i32 %b, -1
768  %not.c = xor i32 %c, -1
769  %add = add i32 %c, %not.a
770  %select = select i1 %x, i32 %add, i32 %not.b
771  %cmp = icmp sle i32 %select, %not.c
772  ret i1 %cmp
773}
774
775define i32 @test_sext(i32 %a, i32 %b){
776; CHECK-LABEL: @test_sext(
777; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
778; CHECK-NEXT:    [[TMP2:%.*]] = sext i1 [[TMP1]] to i32
779; CHECK-NEXT:    [[NOT:%.*]] = sub i32 [[TMP2]], [[B:%.*]]
780; CHECK-NEXT:    ret i32 [[NOT]]
781;
782  %cmp = icmp eq i32 %a, 0
783  %sext = sext i1 %cmp to i32
784  %add = add i32 %b, %sext
785  %not = xor i32 %add, -1
786  ret i32 %not
787}
788
789define <2 x i32> @test_sext_vec(<2 x i32> %a, <2 x i32> %b){
790; CHECK-LABEL: @test_sext_vec(
791; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i32> [[A:%.*]], zeroinitializer
792; CHECK-NEXT:    [[TMP2:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i32>
793; CHECK-NEXT:    [[NOT:%.*]] = sub <2 x i32> [[TMP2]], [[B:%.*]]
794; CHECK-NEXT:    ret <2 x i32> [[NOT]]
795;
796  %cmp = icmp eq <2 x i32> %a, zeroinitializer
797  %sext = sext <2 x i1> %cmp to <2 x i32>
798  %add = add <2 x i32> %b, %sext
799  %not = xor <2 x i32> %add, <i32 -1, i32 -1>
800  ret <2 x i32> %not
801}
802
803define i64 @test_zext_nneg(i32 %c1, i64 %c2, i64 %c3){
804; CHECK-LABEL: @test_zext_nneg(
805; CHECK-NEXT:    [[DOTNEG:%.*]] = add i64 [[C2:%.*]], -4
806; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[C1:%.*]] to i64
807; CHECK-NEXT:    [[TMP2:%.*]] = sub i64 [[TMP1]], [[C3:%.*]]
808; CHECK-NEXT:    [[SUB:%.*]] = add i64 [[DOTNEG]], [[TMP2]]
809; CHECK-NEXT:    ret i64 [[SUB]]
810;
811  %not = xor i32 %c1, -1
812  %conv = zext nneg i32 %not to i64
813  %add1 = add i64 %c2, -5
814  %add2 = add i64 %conv, %c3
815  %sub = sub i64 %add1, %add2
816  ret i64 %sub
817}
818
819define i8 @test_trunc(i8 %a){
820; CHECK-LABEL: @test_trunc(
821; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i8 [[A:%.*]], 0
822; CHECK-NEXT:    [[NOT:%.*]] = sext i1 [[TMP1]] to i8
823; CHECK-NEXT:    ret i8 [[NOT]]
824;
825  %zext = zext i8 %a to i32
826  %sub = add nsw i32 %zext, -1
827  %shr = ashr i32 %sub, 31
828  %conv = trunc i32 %shr to i8
829  %not = xor i8 %conv, -1
830  ret i8 %not
831}
832
833define <2 x i8> @test_trunc_vec(<2 x i8> %a){
834; CHECK-LABEL: @test_trunc_vec(
835; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i8> [[A:%.*]], zeroinitializer
836; CHECK-NEXT:    [[NOT:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
837; CHECK-NEXT:    ret <2 x i8> [[NOT]]
838;
839  %zext = zext <2 x i8> %a to <2 x i32>
840  %sub = add nsw <2 x i32> %zext, <i32 -1, i32 -1>
841  %shr = ashr <2 x i32> %sub, <i32 31, i32 31>
842  %conv = trunc <2 x i32> %shr to <2 x i8>
843  %not = xor <2 x i8> %conv, <i8 -1, i8 -1>
844  ret <2 x i8> %not
845}
846
847; Negative tests
848
849define i32 @test_zext(i32 %a, i32 %b){
850; CHECK-LABEL: @test_zext(
851; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
852; CHECK-NEXT:    [[SEXT:%.*]] = zext i1 [[CMP]] to i32
853; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[B:%.*]], [[SEXT]]
854; CHECK-NEXT:    [[NOT:%.*]] = xor i32 [[ADD]], -1
855; CHECK-NEXT:    ret i32 [[NOT]]
856;
857  %cmp = icmp eq i32 %a, 0
858  %sext = zext i1 %cmp to i32
859  %add = add i32 %b, %sext
860  %not = xor i32 %add, -1
861  ret i32 %not
862}
863
864define void @test_invert_demorgan_or(i32 %a, i32 %b, i1 %cond) {
865; CHECK-LABEL: @test_invert_demorgan_or(
866; CHECK-NEXT:  entry:
867; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
868; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i32 [[B:%.*]], 0
869; CHECK-NEXT:    [[OR_NOT1:%.*]] = and i1 [[CMP1]], [[CMP2]]
870; CHECK-NEXT:    [[MERGE_NOT:%.*]] = and i1 [[OR_NOT1]], [[COND:%.*]]
871; CHECK-NEXT:    br i1 [[MERGE_NOT]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
872; CHECK:       if.then:
873; CHECK-NEXT:    call void @f1()
874; CHECK-NEXT:    unreachable
875; CHECK:       if.else:
876; CHECK-NEXT:    call void @f2()
877; CHECK-NEXT:    unreachable
878;
879entry:
880  %cmp1 = icmp eq i32 %a, 0
881  %cmp2 = icmp ne i32 %b, 0
882  %or = or i1 %cmp1, %cmp2
883  %not = xor i1 %cond, true
884  %merge = or i1 %not, %or
885  br i1 %merge, label %if.then, label %if.else
886if.then:
887  call void @f1()
888  unreachable
889if.else:
890  call void @f2()
891  unreachable
892}
893
894define i1 @test_invert_demorgan_or2(i64 %a, i64 %b, i64 %c) {
895; CHECK-LABEL: @test_invert_demorgan_or2(
896; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult i64 [[A:%.*]], 24
897; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult i64 [[B:%.*]], 60
898; CHECK-NEXT:    [[OR1_NOT1:%.*]] = and i1 [[CMP1]], [[CMP2]]
899; CHECK-NEXT:    [[CMP3:%.*]] = icmp ult i64 [[C:%.*]], 60
900; CHECK-NEXT:    [[OR2_NOT:%.*]] = and i1 [[OR1_NOT1]], [[CMP3]]
901; CHECK-NEXT:    ret i1 [[OR2_NOT]]
902;
903  %cmp1 = icmp ugt i64 %a, 23
904  %cmp2 = icmp ugt i64 %b, 59
905  %or1 = or i1 %cmp1, %cmp2
906  %cmp3 = icmp ugt i64 %c, 59
907  %or2 = or i1 %or1, %cmp3
908  %not = xor i1 %or2, true
909  ret i1 %not
910}
911
912define i1 @test_invert_demorgan_or3(i32 %a, i32 %b) {
913; CHECK-LABEL: @test_invert_demorgan_or3(
914; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 178206
915; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[B:%.*]], -196608
916; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult i32 [[TMP1]], -1506
917; CHECK-NEXT:    [[TMP2:%.*]] = add i32 [[B]], -917760
918; CHECK-NEXT:    [[CMP3:%.*]] = icmp ult i32 [[TMP2]], -716213
919; CHECK-NEXT:    [[TMP3:%.*]] = add i32 [[B]], -1114112
920; CHECK-NEXT:    [[CMP4:%.*]] = icmp ult i32 [[TMP3]], -196112
921; CHECK-NEXT:    [[OR1_NOT2:%.*]] = and i1 [[CMP1]], [[CMP2]]
922; CHECK-NEXT:    [[OR2_NOT1:%.*]] = and i1 [[OR1_NOT2]], [[CMP3]]
923; CHECK-NEXT:    [[OR3_NOT:%.*]] = and i1 [[OR2_NOT1]], [[CMP4]]
924; CHECK-NEXT:    ret i1 [[OR3_NOT]]
925;
926  %cmp1 = icmp eq i32 %a, 178206
927  %v1 = add i32 %b, -195102
928  %cmp2 = icmp ult i32 %v1, 1506
929  %v2 = add i32 %b, -201547
930  %cmp3 = icmp ult i32 %v2, 716213
931  %v3 = add i32 %b, -918000
932  %cmp4 = icmp ult i32 %v3, 196112
933  %or1 = or i1 %cmp1, %cmp2
934  %or2 = or i1 %or1, %cmp3
935  %or3 = or i1 %or2, %cmp4
936  %not = xor i1 %or3, true
937  ret i1 %not
938}
939
940define i1 @test_invert_demorgan_logical_or(i64 %x, i64 %y) {
941; CHECK-LABEL: @test_invert_demorgan_logical_or(
942; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i64 [[X:%.*]], 27
943; CHECK-NEXT:    [[CMP2:%.*]] = icmp ne i64 [[Y:%.*]], 0
944; CHECK-NEXT:    [[SEL_NOT1:%.*]] = select i1 [[CMP1]], i1 [[CMP2]], i1 false
945; CHECK-NEXT:    [[CMP3:%.*]] = icmp ne i64 [[X]], 0
946; CHECK-NEXT:    [[OR_NOT:%.*]] = and i1 [[CMP3]], [[SEL_NOT1]]
947; CHECK-NEXT:    ret i1 [[OR_NOT]]
948;
949  %cmp1 = icmp eq i64 %x, 27
950  %cmp2 = icmp eq i64 %y, 0
951  %sel = select i1 %cmp1, i1 true, i1 %cmp2
952  %cmp3 = icmp eq i64 %x, 0
953  %or = or i1 %cmp3, %sel
954  %not = xor i1 %or, true
955  ret i1 %not
956}
957
958define i1 @test_invert_demorgan_and(i32 %a, i32 %b, i1 %cond) {
959; CHECK-LABEL: @test_invert_demorgan_and(
960; CHECK-NEXT:  entry:
961; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
962; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq i32 [[B:%.*]], 0
963; CHECK-NEXT:    [[AND_NOT1:%.*]] = or i1 [[CMP1]], [[CMP2]]
964; CHECK-NEXT:    [[MERGE_NOT:%.*]] = or i1 [[AND_NOT1]], [[COND:%.*]]
965; CHECK-NEXT:    br i1 [[MERGE_NOT]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
966; CHECK:       if.then:
967; CHECK-NEXT:    call void @f1()
968; CHECK-NEXT:    unreachable
969; CHECK:       if.else:
970; CHECK-NEXT:    call void @f2()
971; CHECK-NEXT:    unreachable
972;
973entry:
974  %cmp1 = icmp eq i32 %a, 0
975  %cmp2 = icmp ne i32 %b, 0
976  %and = and i1 %cmp1, %cmp2
977  %not = xor i1 %cond, true
978  %merge = and i1 %not, %and
979  br i1 %merge, label %if.then, label %if.else
980if.then:
981  call void @f1()
982  unreachable
983if.else:
984  call void @f2()
985  unreachable
986}
987
988define i64 @test_invert_demorgan_and2(i64 %x) {
989; CHECK-LABEL: @test_invert_demorgan_and2(
990; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[X:%.*]]
991; CHECK-NEXT:    [[SUB:%.*]] = or i64 [[TMP1]], -9223372036854775808
992; CHECK-NEXT:    ret i64 [[SUB]]
993;
994  %add = add i64 %x, 9223372036854775807
995  %and = and i64 %add, 9223372036854775807
996  %sub = xor i64 %and, -1
997  ret i64 %sub
998}
999
1000define i1 @test_invert_demorgan_and3(i32 %a, i32 %b) {
1001; CHECK-LABEL: @test_invert_demorgan_and3(
1002; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
1003; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 4095
1004; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP2]], 4095
1005; CHECK-NEXT:    ret i1 [[CMP]]
1006;
1007  %not = xor i32 %a, -1
1008  %add = add i32 %b, %not
1009  %and = and i32 %add, 4095
1010  %cmp = icmp eq i32 %and, 0
1011  ret i1 %cmp
1012}
1013
1014define i1 @test_invert_demorgan_logical_and(i64 %x, i64 %y) {
1015; CHECK-LABEL: @test_invert_demorgan_logical_and(
1016; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i64 [[X:%.*]], 27
1017; CHECK-NEXT:    [[CMP2:%.*]] = icmp ne i64 [[Y:%.*]], 0
1018; CHECK-NEXT:    [[SEL_NOT1:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
1019; CHECK-NEXT:    [[CMP3:%.*]] = icmp ne i64 [[X]], 0
1020; CHECK-NEXT:    [[OR_NOT:%.*]] = and i1 [[CMP3]], [[SEL_NOT1]]
1021; CHECK-NEXT:    ret i1 [[OR_NOT]]
1022;
1023  %cmp1 = icmp eq i64 %x, 27
1024  %cmp2 = icmp eq i64 %y, 0
1025  %sel = select i1 %cmp1, i1 %cmp2, i1 false
1026  %cmp3 = icmp eq i64 %x, 0
1027  %or = or i1 %cmp3, %sel
1028  %not = xor i1 %or, true
1029  ret i1 %not
1030}
1031
1032define i1 @test_invert_demorgan_and_multiuse(i32 %a, i32 %b, i1 %cond) {
1033; CHECK-LABEL: @test_invert_demorgan_and_multiuse(
1034; CHECK-NEXT:  entry:
1035; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i32 [[A:%.*]], 0
1036; CHECK-NEXT:    call void @use1(i1 [[CMP1]])
1037; CHECK-NEXT:    [[CMP2:%.*]] = icmp ne i32 [[B:%.*]], 0
1038; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[COND:%.*]], true
1039; CHECK-NEXT:    [[TMP0:%.*]] = and i1 [[CMP2]], [[NOT]]
1040; CHECK-NEXT:    [[MERGE:%.*]] = and i1 [[TMP0]], [[CMP1]]
1041; CHECK-NEXT:    br i1 [[MERGE]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1042; CHECK:       if.then:
1043; CHECK-NEXT:    call void @f1()
1044; CHECK-NEXT:    unreachable
1045; CHECK:       if.else:
1046; CHECK-NEXT:    call void @f2()
1047; CHECK-NEXT:    unreachable
1048;
1049entry:
1050  %cmp1 = icmp eq i32 %a, 0
1051  call void @use1(i1 %cmp1)
1052  %cmp2 = icmp ne i32 %b, 0
1053  %and = and i1 %cmp1, %cmp2
1054  %not = xor i1 %cond, true
1055  %merge = and i1 %not, %and
1056  br i1 %merge, label %if.then, label %if.else
1057if.then:
1058  call void @f1()
1059  unreachable
1060if.else:
1061  call void @f2()
1062  unreachable
1063}
1064