• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
2
3; Optimize subtracts.
4;
5; RUN: opt < %s -instcombine -S | FileCheck %s
6
7define i32 @test1(i32 %A) {
8	%B = sub i32 %A, %A
9	ret i32 %B
10; CHECK-LABEL: @test1(
11; CHECK: ret i32 0
12}
13
14define i32 @test2(i32 %A) {
15	%B = sub i32 %A, 0
16	ret i32 %B
17; CHECK-LABEL: @test2(
18; CHECK: ret i32 %A
19}
20
21define i32 @test3(i32 %A) {
22	%B = sub i32 0, %A
23	%C = sub i32 0, %B
24	ret i32 %C
25; CHECK-LABEL: @test3(
26; CHECK: ret i32 %A
27}
28
29define i32 @test4(i32 %A, i32 %x) {
30	%B = sub i32 0, %A
31	%C = sub i32 %x, %B
32	ret i32 %C
33; CHECK-LABEL: @test4(
34; CHECK: %C = add i32 %x, %A
35; CHECK: ret i32 %C
36}
37
38define i32 @test5(i32 %A, i32 %B, i32 %C) {
39	%D = sub i32 %B, %C
40	%E = sub i32 %A, %D
41	ret i32 %E
42; CHECK-LABEL: @test5(
43; CHECK: %D1 = sub i32 %C, %B
44; CHECK: %E = add
45; CHECK: ret i32 %E
46}
47
48define i32 @test6(i32 %A, i32 %B) {
49	%C = and i32 %A, %B
50	%D = sub i32 %A, %C
51	ret i32 %D
52; CHECK-LABEL: @test6(
53; CHECK-NEXT: xor i32 %B, -1
54; CHECK-NEXT: %D = and i32
55; CHECK-NEXT: ret i32 %D
56}
57
58define i32 @test7(i32 %A) {
59	%B = sub i32 -1, %A
60	ret i32 %B
61; CHECK-LABEL: @test7(
62; CHECK: %B = xor i32 %A, -1
63; CHECK: ret i32 %B
64}
65
66define i32 @test8(i32 %A) {
67	%B = mul i32 9, %A
68	%C = sub i32 %B, %A
69	ret i32 %C
70; CHECK-LABEL: @test8(
71; CHECK: %C = shl i32 %A, 3
72; CHECK: ret i32 %C
73}
74
75define i32 @test9(i32 %A) {
76	%B = mul i32 3, %A
77	%C = sub i32 %A, %B
78	ret i32 %C
79; CHECK-LABEL: @test9(
80; CHECK: %C = mul i32 %A, -2
81; CHECK: ret i32 %C
82}
83
84define i32 @test10(i32 %A, i32 %B) {
85	%C = sub i32 0, %A
86	%D = sub i32 0, %B
87	%E = mul i32 %C, %D
88	ret i32 %E
89; CHECK-LABEL: @test10(
90; CHECK: %E = mul i32 %A, %B
91; CHECK: ret i32 %E
92}
93
94define i32 @test10a(i32 %A) {
95	%C = sub i32 0, %A
96	%E = mul i32 %C, 7
97	ret i32 %E
98; CHECK-LABEL: @test10a(
99; CHECK: %E = mul i32 %A, -7
100; CHECK: ret i32 %E
101}
102
103define i1 @test11(i8 %A, i8 %B) {
104	%C = sub i8 %A, %B
105	%cD = icmp ne i8 %C, 0
106	ret i1 %cD
107; CHECK-LABEL: @test11(
108; CHECK: %cD = icmp ne i8 %A, %B
109; CHECK: ret i1 %cD
110}
111
112define i32 @test12(i32 %A) {
113	%B = ashr i32 %A, 31
114	%C = sub i32 0, %B
115	ret i32 %C
116; CHECK-LABEL: @test12(
117; CHECK: %C = lshr i32 %A, 31
118; CHECK: ret i32 %C
119}
120
121define i32 @test13(i32 %A) {
122	%B = lshr i32 %A, 31
123	%C = sub i32 0, %B
124	ret i32 %C
125; CHECK-LABEL: @test13(
126; CHECK: %C = ashr i32 %A, 31
127; CHECK: ret i32 %C
128}
129
130define i32 @test14(i32 %A) {
131	%B = lshr i32 %A, 31
132	%C = bitcast i32 %B to i32
133	%D = sub i32 0, %C
134	ret i32 %D
135; CHECK-LABEL: @test14(
136; CHECK: %D = ashr i32 %A, 31
137; CHECK: ret i32 %D
138}
139
140define i32 @test15(i32 %A, i32 %B) {
141	%C = sub i32 0, %A
142	%D = srem i32 %B, %C
143	ret i32 %D
144; CHECK-LABEL: @test15(
145; CHECK:      %[[sub:.*]] = sub i32 0, %A
146; CHECK-NEXT: %[[rem:.*]] = srem i32 %B, %[[sub]]
147; CHECK: ret i32 %[[rem]]
148}
149
150define i32 @test16(i32 %A) {
151	%X = sdiv i32 %A, 1123
152	%Y = sub i32 0, %X
153	ret i32 %Y
154; CHECK-LABEL: @test16(
155; CHECK: %Y = sdiv i32 %A, -1123
156; CHECK: ret i32 %Y
157}
158
159; Can't fold subtract here because negation it might oveflow.
160; PR3142
161define i32 @test17(i32 %A) {
162	%B = sub i32 0, %A
163	%C = sdiv i32 %B, 1234
164	ret i32 %C
165; CHECK-LABEL: @test17(
166; CHECK: %B = sub i32 0, %A
167; CHECK: %C = sdiv i32 %B, 1234
168; CHECK: ret i32 %C
169}
170
171define i64 @test18(i64 %Y) {
172	%tmp.4 = shl i64 %Y, 2
173	%tmp.12 = shl i64 %Y, 2
174	%tmp.8 = sub i64 %tmp.4, %tmp.12
175	ret i64 %tmp.8
176; CHECK-LABEL: @test18(
177; CHECK: ret i64 0
178}
179
180define i32 @test19(i32 %X, i32 %Y) {
181	%Z = sub i32 %X, %Y
182	%Q = add i32 %Z, %Y
183	ret i32 %Q
184; CHECK-LABEL: @test19(
185; CHECK: ret i32 %X
186}
187
188define i1 @test20(i32 %g, i32 %h) {
189	%tmp.2 = sub i32 %g, %h
190	%tmp.4 = icmp ne i32 %tmp.2, %g
191	ret i1 %tmp.4
192; CHECK-LABEL: @test20(
193; CHECK: %tmp.4 = icmp ne i32 %h, 0
194; CHECK: ret i1 %tmp.4
195}
196
197define i1 @test21(i32 %g, i32 %h) {
198	%tmp.2 = sub i32 %g, %h
199	%tmp.4 = icmp ne i32 %tmp.2, %g
200        ret i1 %tmp.4
201; CHECK-LABEL: @test21(
202; CHECK: %tmp.4 = icmp ne i32 %h, 0
203; CHECK: ret i1 %tmp.4
204}
205
206; PR2298
207define zeroext i1 @test22(i32 %a, i32 %b)  nounwind  {
208	%tmp2 = sub i32 0, %a
209	%tmp4 = sub i32 0, %b
210	%tmp5 = icmp eq i32 %tmp2, %tmp4
211	ret i1 %tmp5
212; CHECK-LABEL: @test22(
213; CHECK: %tmp5 = icmp eq i32 %b, %a
214; CHECK: ret i1 %tmp5
215}
216
217; rdar://7362831
218define i32 @test23(i8* %P, i64 %A){
219  %B = getelementptr inbounds i8, i8* %P, i64 %A
220  %C = ptrtoint i8* %B to i64
221  %D = trunc i64 %C to i32
222  %E = ptrtoint i8* %P to i64
223  %F = trunc i64 %E to i32
224  %G = sub i32 %D, %F
225  ret i32 %G
226; CHECK-LABEL: @test23(
227; CHECK-NEXT: = trunc i64 %A to i32
228; CHECK-NEXT: ret i32
229}
230
231define i8 @test23_as1(i8 addrspace(1)* %P, i16 %A) {
232; CHECK: @test23_as1
233; CHECK-NEXT: = trunc i16 %A to i8
234; CHECK-NEXT: ret i8
235  %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
236  %C = ptrtoint i8 addrspace(1)* %B to i16
237  %D = trunc i16 %C to i8
238  %E = ptrtoint i8 addrspace(1)* %P to i16
239  %F = trunc i16 %E to i8
240  %G = sub i8 %D, %F
241  ret i8 %G
242}
243
244define i64 @test24(i8* %P, i64 %A){
245  %B = getelementptr inbounds i8, i8* %P, i64 %A
246  %C = ptrtoint i8* %B to i64
247  %E = ptrtoint i8* %P to i64
248  %G = sub i64 %C, %E
249  ret i64 %G
250; CHECK-LABEL: @test24(
251; CHECK-NEXT: ret i64 %A
252}
253
254define i16 @test24_as1(i8 addrspace(1)* %P, i16 %A) {
255; CHECK: @test24_as1
256; CHECK-NEXT: ret i16 %A
257  %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
258  %C = ptrtoint i8 addrspace(1)* %B to i16
259  %E = ptrtoint i8 addrspace(1)* %P to i16
260  %G = sub i16 %C, %E
261  ret i16 %G
262}
263
264define i64 @test24a(i8* %P, i64 %A){
265  %B = getelementptr inbounds i8, i8* %P, i64 %A
266  %C = ptrtoint i8* %B to i64
267  %E = ptrtoint i8* %P to i64
268  %G = sub i64 %E, %C
269  ret i64 %G
270; CHECK-LABEL: @test24a(
271; CHECK-NEXT: sub i64 0, %A
272; CHECK-NEXT: ret i64
273}
274
275define i16 @test24a_as1(i8 addrspace(1)* %P, i16 %A) {
276; CHECK: @test24a_as1
277; CHECK-NEXT: sub i16 0, %A
278; CHECK-NEXT: ret i16
279  %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
280  %C = ptrtoint i8 addrspace(1)* %B to i16
281  %E = ptrtoint i8 addrspace(1)* %P to i16
282  %G = sub i16 %E, %C
283  ret i16 %G
284}
285
286
287@Arr = external global [42 x i16]
288
289define i64 @test24b(i8* %P, i64 %A){
290  %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A
291  %C = ptrtoint i16* %B to i64
292  %G = sub i64 %C, ptrtoint ([42 x i16]* @Arr to i64)
293  ret i64 %G
294; CHECK-LABEL: @test24b(
295; CHECK-NEXT: shl nuw i64 %A, 1
296; CHECK-NEXT: ret i64
297}
298
299
300define i64 @test25(i8* %P, i64 %A){
301  %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A
302  %C = ptrtoint i16* %B to i64
303  %G = sub i64 %C, ptrtoint (i16* getelementptr ([42 x i16], [42 x i16]* @Arr, i64 1, i64 0) to i64)
304  ret i64 %G
305; CHECK-LABEL: @test25(
306; CHECK-NEXT: shl nuw i64 %A, 1
307; CHECK-NEXT: add i64 {{.*}}, -84
308; CHECK-NEXT: ret i64
309}
310
311@Arr_as1 = external addrspace(1) global [42 x i16]
312
313define i16 @test25_as1(i8 addrspace(1)* %P, i64 %A) {
314; CHECK: @test25_as1
315; CHECK-NEXT: %1 = trunc i64 %A to i16
316; CHECK-NEXT: shl nuw i16 %1, 1
317; CHECK-NEXT: add i16 {{.*}}, -84
318; CHECK-NEXT: ret i16
319  %B = getelementptr inbounds [42 x i16], [42 x i16] addrspace(1)* @Arr_as1, i64 0, i64 %A
320  %C = ptrtoint i16 addrspace(1)* %B to i16
321  %G = sub i16 %C, ptrtoint (i16 addrspace(1)* getelementptr ([42 x i16], [42 x i16] addrspace(1)* @Arr_as1, i64 1, i64 0) to i16)
322  ret i16 %G
323}
324
325define i32 @test26(i32 %x) {
326  %shl = shl i32 3, %x
327  %neg = sub i32 0, %shl
328  ret i32 %neg
329; CHECK-LABEL: @test26(
330; CHECK-NEXT: shl i32 -3
331; CHECK-NEXT: ret i32
332}
333
334define i32 @test27(i32 %x, i32 %y) {
335  %mul = mul i32 %y, -8
336  %sub = sub i32 %x, %mul
337  ret i32 %sub
338; CHECK-LABEL: @test27(
339; CHECK-NEXT: shl i32 %y, 3
340; CHECK-NEXT: add i32
341; CHECK-NEXT: ret i32
342}
343
344define i32 @test28(i32 %x, i32 %y, i32 %z) {
345  %neg = sub i32 0, %z
346  %mul = mul i32 %neg, %y
347  %sub = sub i32 %x, %mul
348  ret i32 %sub
349; CHECK-LABEL: @test28(
350; CHECK-NEXT: mul i32 %z, %y
351; CHECK-NEXT: add i32
352; CHECK-NEXT: ret i32
353}
354
355define i64 @test29(i8* %foo, i64 %i, i64 %j) {
356  %gep1 = getelementptr inbounds i8, i8* %foo, i64 %i
357  %gep2 = getelementptr inbounds i8, i8* %foo, i64 %j
358  %cast1 = ptrtoint i8* %gep1 to i64
359  %cast2 = ptrtoint i8* %gep2 to i64
360  %sub = sub i64 %cast1, %cast2
361  ret i64 %sub
362; CHECK-LABEL: @test29(
363; CHECK-NEXT: sub i64 %i, %j
364; CHECK-NEXT: ret i64
365}
366
367define i64 @test30(i8* %foo, i64 %i, i64 %j) {
368  %bit = bitcast i8* %foo to i32*
369  %gep1 = getelementptr inbounds i32, i32* %bit, i64 %i
370  %gep2 = getelementptr inbounds i8, i8* %foo, i64 %j
371  %cast1 = ptrtoint i32* %gep1 to i64
372  %cast2 = ptrtoint i8* %gep2 to i64
373  %sub = sub i64 %cast1, %cast2
374  ret i64 %sub
375; CHECK-LABEL: @test30(
376; CHECK-NEXT: %gep1.idx = shl nuw i64 %i, 2
377; CHECK-NEXT: sub i64 %gep1.idx, %j
378; CHECK-NEXT: ret i64
379}
380
381define i16 @test30_as1(i8 addrspace(1)* %foo, i16 %i, i16 %j) {
382; CHECK-LABEL: @test30_as1(
383; CHECK-NEXT: %gep1.idx = shl nuw i16 %i, 2
384; CHECK-NEXT: sub i16 %gep1.idx, %j
385; CHECK-NEXT: ret i16
386  %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)*
387  %gep1 = getelementptr inbounds i32, i32 addrspace(1)* %bit, i16 %i
388  %gep2 = getelementptr inbounds i8, i8 addrspace(1)* %foo, i16 %j
389  %cast1 = ptrtoint i32 addrspace(1)* %gep1 to i16
390  %cast2 = ptrtoint i8 addrspace(1)* %gep2 to i16
391  %sub = sub i16 %cast1, %cast2
392  ret i16 %sub
393}
394
395define <2 x i64> @test31(<2 x i64> %A) {
396  %xor = xor <2 x i64> %A, <i64 -1, i64 -1>
397  %sub = sub <2 x i64> <i64 2, i64 3>, %xor
398  ret <2 x i64> %sub
399; CHECK-LABEL: @test31(
400; CHECK-NEXT: %sub = add <2 x i64> %A, <i64 3, i64 4>
401; CHECK-NEXT: ret <2 x i64> %sub
402}
403
404define <2 x i64> @test32(<2 x i64> %A) {
405  %add = add <2 x i64> %A, <i64 -1, i64 -1>
406  %sub = sub <2 x i64> <i64 2, i64 3>, %add
407  ret <2 x i64> %sub
408; CHECK-LABEL: @test32(
409; CHECK-NEXT: %sub = sub <2 x i64> <i64 3, i64 4>
410; CHECK-NEXT: ret <2 x i64> %sub
411}
412
413define <2 x i64> @test33(<2 x i1> %A) {
414  %ext = zext <2 x i1> %A to <2 x i64>
415  %sub = sub <2 x i64> zeroinitializer, %ext
416  ret <2 x i64> %sub
417; CHECK-LABEL: @test33(
418; CHECK-NEXT: %sub = sext <2 x i1> %A to <2 x i64>
419; CHECK-NEXT: ret <2 x i64> %sub
420}
421
422define <2 x i64> @test34(<2 x i1> %A) {
423  %ext = sext <2 x i1> %A to <2 x i64>
424  %sub = sub <2 x i64> zeroinitializer, %ext
425  ret <2 x i64> %sub
426; CHECK-LABEL: @test34(
427; CHECK-NEXT: %sub = zext <2 x i1> %A to <2 x i64>
428; CHECK-NEXT: ret <2 x i64> %sub
429}
430
431define <2 x i64> @test35(<2 x i64> %A) {
432  %mul = mul <2 x i64> %A, <i64 3, i64 4>
433  %sub = sub <2 x i64> %A, %mul
434  ret <2 x i64> %sub
435; CHECK-LABEL: @test35(
436; CHECK-NEXT: %sub = mul <2 x i64> %A, <i64 -2, i64 -3>
437; CHECK-NEXT: ret <2 x i64> %sub
438}
439
440define <2 x i64> @test36(<2 x i64> %A) {
441  %shl = shl <2 x i64> %A, <i64 3, i64 4>
442  %sub = sub <2 x i64> %shl, %A
443  ret <2 x i64> %sub
444; CHECK-LABEL: @test36(
445; CHECK-NEXT: %sub = mul <2 x i64> %A, <i64 7, i64 15>
446; CHECK-NEXT: ret <2 x i64> %sub
447}
448
449define <2 x i32> @test37(<2 x i32> %A) {
450  %div = sdiv <2 x i32> %A, <i32 -2147483648, i32 -2147483648>
451  %sub = sub nsw <2 x i32> zeroinitializer, %div
452  ret <2 x i32> %sub
453; CHECK-LABEL: @test37(
454; CHECK-NEXT: [[ICMP:%.*]] = icmp eq <2 x i32> %A, <i32 -2147483648, i32 -2147483648>
455; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[ICMP]] to <2 x i32>
456; CHECK-NEXT: ret <2 x i32> [[SEXT]]
457}
458
459define i32 @test38(i32 %A) {
460  %div = sdiv i32 %A, -2147483648
461  %sub = sub nsw i32 0, %div
462  ret i32 %sub
463; CHECK-LABEL: @test38(
464; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i32 %A, -2147483648
465; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[ICMP]] to i32
466; CHECK-NEXT: ret i32 [[SEXT]]
467}
468
469define i32 @test39(i32 %A, i32 %x) {
470  %B = sub i32 0, %A
471  %C = sub nsw i32 %x, %B
472  ret i32 %C
473; CHECK-LABEL: @test39(
474; CHECK: %C = add i32 %x, %A
475; CHECK: ret i32 %C
476}
477
478define i16 @test40(i16 %a, i16 %b) {
479  %ashr = ashr i16 %a, 1
480  %ashr1 = ashr i16 %b, 1
481  %sub = sub i16 %ashr, %ashr1
482  ret i16 %sub
483; CHECK-LABEL: @test40(
484; CHECK-NEXT: [[ASHR:%.*]] = ashr i16 %a, 1
485; CHECK-NEXT: [[ASHR1:%.*]] = ashr i16 %b, 1
486; CHECK-NEXT: [[RET:%.*]] = sub nsw i16 [[ASHR]], [[ASHR1]]
487; CHECK: ret i16 [[RET]]
488}
489
490define i32 @test41(i16 %a, i16 %b) {
491  %conv = sext i16 %a to i32
492  %conv1 = sext i16 %b to i32
493  %sub = sub i32 %conv, %conv1
494  ret i32 %sub
495; CHECK-LABEL: @test41(
496; CHECK-NEXT: [[SEXT:%.*]] = sext i16 %a to i32
497; CHECK-NEXT: [[SEXT1:%.*]] = sext i16 %b to i32
498; CHECK-NEXT: [[RET:%.*]] = sub nsw i32 [[SEXT]], [[SEXT1]]
499; CHECK: ret i32 [[RET]]
500}
501
502define i4 @test42(i4 %x, i4 %y) {
503  %a = and i4 %y, 7
504  %b = and i4 %x, 7
505  %c = sub i4 %a, %b
506  ret i4 %c
507; CHECK-LABEL: @test42(
508; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7
509; CHECK-NEXT: [[AND1:%.*]] = and i4 %x, 7
510; CHECK-NEXT: [[RET:%.*]] = sub nsw i4 [[AND]], [[AND1]]
511; CHECK: ret i4 [[RET]]
512}
513
514define i4 @test43(i4 %x, i4 %y) {
515  %a = or i4 %x, -8
516  %b = and i4 %y, 7
517  %c = sub i4 %a, %b
518  ret i4 %c
519; CHECK-LABEL: @test43(
520; CHECK-NEXT: [[OR:%.*]] = or i4 %x, -8
521; CHECK-NEXT: [[AND:%.*]] = and i4 %y, 7
522; CHECK-NEXT: [[RET:%.*]] = sub nuw i4 [[OR]], [[AND]]
523; CHECK: ret i4 [[RET]]
524}
525
526define i32 @test44(i32 %x) {
527  %sub = sub nsw i32 %x, 32768
528  ret i32 %sub
529; CHECK-LABEL: @test44(
530; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 %x, -32768
531; CHECK: ret i32 [[ADD]]
532}
533
534define i32 @test45(i32 %x, i32 %y) {
535  %or = or i32 %x, %y
536  %xor = xor i32 %x, %y
537  %sub = sub i32 %or, %xor
538  ret i32 %sub
539; CHECK-LABEL: @test45(
540; CHECK-NEXT: %sub = and i32 %x, %y
541; CHECK: ret i32 %sub
542}
543
544define i32 @test46(i32 %x, i32 %y) {
545 %or = or i32 %x, %y
546 %sub = sub i32 %or, %x
547 ret i32 %sub
548; CHECK-LABEL: @test46(
549; CHECK-NEXT: %x.not = xor i32 %x, -1
550; CHECK-NEXT: %sub = and i32 %y, %x.not
551; CHECK: ret i32 %sub
552}
553
554define i32 @test47(i1 %A, i32 %B, i32 %C, i32 %D) {
555  %sel0 = select i1 %A, i32 %D, i32 %B
556  %sel1 = select i1 %A, i32 %C, i32 %B
557  %sub = sub i32 %sel0, %sel1
558  ret i32 %sub
559; CHECK-LABEL: @test47(
560; CHECK-NEXT: %[[sub:.*]] = sub i32 %D, %C
561; CHECK-NEXT: %[[sel:.*]] = select i1 %A, i32 %[[sub]], i32 0
562; CHECK-NEXT: ret i32 %[[sel]]
563}
564
565define i32 @test48(i1 %A, i32 %B, i32 %C, i32 %D) {
566  %sel0 = select i1 %A, i32 %B, i32 %D
567  %sel1 = select i1 %A, i32 %B, i32 %C
568  %sub = sub i32 %sel0, %sel1
569  ret i32 %sub
570; CHECK-LABEL: @test48(
571; CHECK-NEXT: %[[sub:.*]] = sub i32 %D, %C
572; CHECK-NEXT: %[[sel:.*]] = select i1 %A, i32 0, i32 %[[sub]]
573; CHECK-NEXT: ret i32 %[[sel]]
574}
575