• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; This test makes sure that these instructions are properly eliminated.
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
5
6define i32 @test1(i32 %A) {
7        %B = or i32 %A, 0
8        ret i32 %B
9; CHECK-LABEL: @test1(
10; CHECK: ret i32 %A
11}
12
13define i32 @test2(i32 %A) {
14        %B = or i32 %A, -1
15        ret i32 %B
16; CHECK-LABEL: @test2(
17; CHECK: ret i32 -1
18}
19
20define i8 @test2a(i8 %A) {
21        %B = or i8 %A, -1
22        ret i8 %B
23; CHECK-LABEL: @test2a(
24; CHECK: ret i8 -1
25}
26
27define i1 @test3(i1 %A) {
28        %B = or i1 %A, false
29        ret i1 %B
30; CHECK-LABEL: @test3(
31; CHECK: ret i1 %A
32}
33
34define i1 @test4(i1 %A) {
35        %B = or i1 %A, true
36        ret i1 %B
37; CHECK-LABEL: @test4(
38; CHECK: ret i1 true
39}
40
41define i1 @test5(i1 %A) {
42        %B = or i1 %A, %A
43        ret i1 %B
44; CHECK-LABEL: @test5(
45; CHECK: ret i1 %A
46}
47
48define i32 @test6(i32 %A) {
49        %B = or i32 %A, %A
50        ret i32 %B
51; CHECK-LABEL: @test6(
52; CHECK: ret i32 %A
53}
54
55; A | ~A == -1
56define i32 @test7(i32 %A) {
57        %NotA = xor i32 -1, %A
58        %B = or i32 %A, %NotA
59        ret i32 %B
60; CHECK-LABEL: @test7(
61; CHECK: ret i32 -1
62}
63
64define i8 @test8(i8 %A) {
65        %B = or i8 %A, -2
66        %C = or i8 %B, 1
67        ret i8 %C
68; CHECK-LABEL: @test8(
69; CHECK: ret i8 -1
70}
71
72; Test that (A|c1)|(B|c2) == (A|B)|(c1|c2)
73define i8 @test9(i8 %A, i8 %B) {
74        %C = or i8 %A, 1
75        %D = or i8 %B, -2
76        %E = or i8 %C, %D
77        ret i8 %E
78; CHECK-LABEL: @test9(
79; CHECK: ret i8 -1
80}
81
82define i8 @test10(i8 %A) {
83        %B = or i8 %A, 1
84        %C = and i8 %B, -2
85        ; (X & C1) | C2 --> (X | C2) & (C1|C2)
86        %D = or i8 %C, -2
87        ret i8 %D
88; CHECK-LABEL: @test10(
89; CHECK: ret i8 -2
90}
91
92define i8 @test11(i8 %A) {
93        %B = or i8 %A, -2
94        %C = xor i8 %B, 13
95        ; (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
96        %D = or i8 %C, 1
97        %E = xor i8 %D, 12
98        ret i8 %E
99; CHECK-LABEL: @test11(
100; CHECK: ret i8 -1
101}
102
103define i32 @test12(i32 %A) {
104        ; Should be eliminated
105        %B = or i32 %A, 4
106        %C = and i32 %B, 8
107        ret i32 %C
108; CHECK-LABEL: @test12(
109; CHECK: %C = and i32 %A, 8
110; CHECK: ret i32 %C
111}
112
113define i32 @test13(i32 %A) {
114        %B = or i32 %A, 12
115        ; Always equal to 8
116        %C = and i32 %B, 8
117        ret i32 %C
118; CHECK-LABEL: @test13(
119; CHECK: ret i32 8
120}
121
122define i1 @test14(i32 %A, i32 %B) {
123        %C1 = icmp ult i32 %A, %B
124        %C2 = icmp ugt i32 %A, %B
125        ; (A < B) | (A > B) === A != B
126        %D = or i1 %C1, %C2
127        ret i1 %D
128; CHECK-LABEL: @test14(
129; CHECK: icmp ne i32 %A, %B
130; CHECK: ret i1
131}
132
133define i1 @test15(i32 %A, i32 %B) {
134        %C1 = icmp ult i32 %A, %B
135        %C2 = icmp eq i32 %A, %B
136        ; (A < B) | (A == B) === A <= B
137        %D = or i1 %C1, %C2
138        ret i1 %D
139; CHECK-LABEL: @test15(
140; CHECK:  icmp ule i32 %A, %B
141; CHECK: ret i1
142}
143
144define i32 @test16(i32 %A) {
145        %B = and i32 %A, 1
146        ; -2 = ~1
147        %C = and i32 %A, -2
148        ; %D = and int %B, -1 == %B
149        %D = or i32 %B, %C
150        ret i32 %D
151; CHECK-LABEL: @test16(
152; CHECK: ret i32 %A
153}
154
155define i32 @test17(i32 %A) {
156        %B = and i32 %A, 1
157        %C = and i32 %A, 4
158        ; %D = and int %B, 5
159        %D = or i32 %B, %C
160        ret i32 %D
161; CHECK-LABEL: @test17(
162; CHECK: %D = and i32 %A, 5
163; CHECK: ret i32 %D
164}
165
166define i1 @test18(i32 %A) {
167        %B = icmp sge i32 %A, 100
168        %C = icmp slt i32 %A, 50
169        ;; (A-50) >u 50
170        %D = or i1 %B, %C
171        ret i1 %D
172; CHECK-LABEL: @test18(
173; CHECK: add i32
174; CHECK:  icmp ugt
175; CHECK: ret i1
176}
177
178define i1 @test19(i32 %A) {
179        %B = icmp eq i32 %A, 50
180        %C = icmp eq i32 %A, 51
181        ;; (A&-2) == 50
182        %D = or i1 %B, %C
183        ret i1 %D
184; CHECK-LABEL: @test19(
185; CHECK: and i32
186; CHECK: icmp eq
187; CHECK: ret i1
188}
189
190define i32 @test20(i32 %x) {
191        %y = and i32 %x, 123
192        %z = or i32 %y, %x
193        ret i32 %z
194; CHECK-LABEL: @test20(
195; CHECK: ret i32 %x
196}
197
198define i32 @test21(i32 %tmp.1) {
199        %tmp.1.mask1 = add i32 %tmp.1, 2
200        %tmp.3 = and i32 %tmp.1.mask1, -2
201        %tmp.5 = and i32 %tmp.1, 1
202        ;; add tmp.1, 2
203        %tmp.6 = or i32 %tmp.5, %tmp.3
204        ret i32 %tmp.6
205; CHECK-LABEL: @test21(
206; CHECK:   add i32 %{{[^,]*}}, 2
207; CHECK:   ret i32
208}
209
210define i32 @test22(i32 %B) {
211        %ELIM41 = and i32 %B, 1
212        %ELIM7 = and i32 %B, -2
213        %ELIM5 = or i32 %ELIM41, %ELIM7
214        ret i32 %ELIM5
215; CHECK-LABEL: @test22(
216; CHECK: ret i32 %B
217}
218
219define i16 @test23(i16 %A) {
220        %B = lshr i16 %A, 1
221        ;; fold or into xor
222        %C = or i16 %B, -32768
223        %D = xor i16 %C, 8193
224        ret i16 %D
225; CHECK-LABEL: @test23(
226; CHECK:   %B = lshr i16 %A, 1
227; CHECK:   %D = xor i16 %B, -24575
228; CHECK:   ret i16 %D
229}
230
231; PR1738
232define i1 @test24(double %X, double %Y) {
233        %tmp9 = fcmp uno double %X, 0.000000e+00                ; <i1> [#uses=1]
234        %tmp13 = fcmp uno double %Y, 0.000000e+00               ; <i1> [#uses=1]
235        %bothcond = or i1 %tmp13, %tmp9         ; <i1> [#uses=1]
236        ret i1 %bothcond
237
238; CHECK-LABEL: @test24(
239; CHECK:    = fcmp uno double %Y, %X
240; CHECK:   ret i1
241}
242
243; PR3266 & PR5276
244define i1 @test25(i32 %A, i32 %B) {
245  %C = icmp eq i32 %A, 0
246  %D = icmp eq i32 %B, 57
247  %E = or i1 %C, %D
248  %F = xor i1 %E, -1
249  ret i1 %F
250
251; CHECK-LABEL: @test25(
252; CHECK: icmp ne i32 %A, 0
253; CHECK-NEXT: icmp ne i32 %B, 57
254; CHECK-NEXT:  %F = and i1
255; CHECK-NEXT:  ret i1 %F
256}
257
258; PR5634
259define i1 @test26(i32 %A, i32 %B) {
260        %C1 = icmp eq i32 %A, 0
261        %C2 = icmp eq i32 %B, 0
262        ; (A == 0) & (A == 0)   -->   (A|B) == 0
263        %D = and i1 %C1, %C2
264        ret i1 %D
265; CHECK-LABEL: @test26(
266; CHECK: or i32 %A, %B
267; CHECK: icmp eq i32 {{.*}}, 0
268; CHECK: ret i1
269}
270
271define i1 @test27(i32* %A, i32* %B) {
272  %C1 = ptrtoint i32* %A to i32
273  %C2 = ptrtoint i32* %B to i32
274  %D = or i32 %C1, %C2
275  %E = icmp eq i32 %D, 0
276  ret i1 %E
277; CHECK-LABEL: @test27(
278; CHECK: icmp eq i32* %A, null
279; CHECK: icmp eq i32* %B, null
280; CHECK: and i1
281; CHECK: ret i1
282}
283
284; PR5634
285define i1 @test28(i32 %A, i32 %B) {
286        %C1 = icmp ne i32 %A, 0
287        %C2 = icmp ne i32 %B, 0
288        ; (A != 0) | (A != 0)   -->   (A|B) != 0
289        %D = or i1 %C1, %C2
290        ret i1 %D
291; CHECK-LABEL: @test28(
292; CHECK: or i32 %A, %B
293; CHECK: icmp ne i32 {{.*}}, 0
294; CHECK: ret i1
295}
296
297define i1 @test29(i32* %A, i32* %B) {
298  %C1 = ptrtoint i32* %A to i32
299  %C2 = ptrtoint i32* %B to i32
300  %D = or i32 %C1, %C2
301  %E = icmp ne i32 %D, 0
302  ret i1 %E
303; CHECK-LABEL: @test29(
304; CHECK: icmp ne i32* %A, null
305; CHECK: icmp ne i32* %B, null
306; CHECK: or i1
307; CHECK: ret i1
308}
309
310; PR4216
311define i32 @test30(i32 %A) {
312entry:
313  %B = or i32 %A, 32962
314  %C = and i32 %A, -65536
315  %D = and i32 %B, 40186
316  %E = or i32 %D, %C
317  ret i32 %E
318; CHECK-LABEL: @test30(
319; CHECK: %D = and i32 %A, -58312
320; CHECK: %E = or i32 %D, 32962
321; CHECK: ret i32 %E
322}
323
324; PR4216
325define i64 @test31(i64 %A) nounwind readnone ssp noredzone {
326  %B = or i64 %A, 194
327  %D = and i64 %B, 250
328
329  %C = or i64 %A, 32768
330  %E = and i64 %C, 4294941696
331
332  %F = or i64 %D, %E
333  ret i64 %F
334; CHECK-LABEL: @test31(
335; CHECK-NEXT: %E = and i64 %A, 4294908984
336; CHECK-NEXT: %F = or i64 %E, 32962
337; CHECK-NEXT: ret i64 %F
338}
339
340define <4 x i32> @test32(<4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32> %vecinit6.i191) {
341  %and.i135 = sext <4 x i1> %and.i1352 to <4 x i32> ; <<4 x i32>> [#uses=2]
342  %and.i129 = and <4 x i32> %vecinit6.i176, %and.i135 ; <<4 x i32>> [#uses=1]
343  %neg.i = xor <4 x i32> %and.i135, <i32 -1, i32 -1, i32 -1, i32 -1> ; <<4 x i32>> [#uses=1]
344  %and.i = and <4 x i32> %vecinit6.i191, %neg.i   ; <<4 x i32>> [#uses=1]
345  %or.i = or <4 x i32> %and.i, %and.i129          ; <<4 x i32>> [#uses=1]
346  ret <4 x i32> %or.i
347; codegen is mature enough to handle vector selects.
348; CHECK-LABEL: @test32(
349; CHECK: select <4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32> %vecinit6.i191
350}
351
352define i1 @test33(i1 %X, i1 %Y) {
353  %a = or i1 %X, %Y
354  %b = or i1 %a, %X
355  ret i1 %b
356; CHECK-LABEL: @test33(
357; CHECK-NEXT: or i1 %X, %Y
358; CHECK-NEXT: ret
359}
360
361define i32 @test34(i32 %X, i32 %Y) {
362  %a = or i32 %X, %Y
363  %b = or i32 %Y, %a
364  ret i32 %b
365; CHECK-LABEL: @test34(
366; CHECK-NEXT: or i32 %X, %Y
367; CHECK-NEXT: ret
368}
369
370define i32 @test35(i32 %a, i32 %b) {
371  %1 = or i32 %a, 1135
372  %2 = or i32 %1, %b
373  ret i32 %2
374  ; CHECK-LABEL: @test35(
375  ; CHECK-NEXT: or i32 %a, %b
376  ; CHECK-NEXT: or i32 %1, 1135
377}
378
379define i1 @test36(i32 %x) {
380  %cmp1 = icmp eq i32 %x, 23
381  %cmp2 = icmp eq i32 %x, 24
382  %ret1 = or i1 %cmp1, %cmp2
383  %cmp3 = icmp eq i32 %x, 25
384  %ret2 = or i1 %ret1, %cmp3
385  ret i1 %ret2
386; CHECK-LABEL: @test36(
387; CHECK-NEXT: %x.off = add i32 %x, -23
388; CHECK-NEXT: icmp ult i32 %x.off, 3
389; CHECK-NEXT: ret i1
390}
391
392define i32 @test37(i32* %xp, i32 %y) {
393; CHECK-LABEL: @test37(
394; CHECK: select i1 %tobool, i32 -1, i32 %x
395  %tobool = icmp ne i32 %y, 0
396  %sext = sext i1 %tobool to i32
397  %x = load i32* %xp
398  %or = or i32 %sext, %x
399  ret i32 %or
400}
401
402define i32 @test38(i32* %xp, i32 %y) {
403; CHECK-LABEL: @test38(
404; CHECK: select i1 %tobool, i32 -1, i32 %x
405  %tobool = icmp ne i32 %y, 0
406  %sext = sext i1 %tobool to i32
407  %x = load i32* %xp
408  %or = or i32 %x, %sext
409  ret i32 %or
410}
411