1; This test makes sure that these instructions are properly eliminated.
2;
3; RUN: opt < %s -instcombine -S | \
4; RUN:    FileCheck %s
5; END.
6@G1 = global i32 0		; <i32*> [#uses=1]
7@G2 = global i32 0		; <i32*> [#uses=1]
8
9define i1 @test0(i1 %A) {
10; CHECK-LABEL: @test0(
11; CHECK-NEXT: ret i1 %A
12	%B = xor i1 %A, false		; <i1> [#uses=1]
13	ret i1 %B
14}
15
16define i32 @test1(i32 %A) {
17; CHECK-LABEL: @test1(
18; CHECK-NEXT: ret i32 %A
19	%B = xor i32 %A, 0		; <i32> [#uses=1]
20	ret i32 %B
21}
22
23define i1 @test2(i1 %A) {
24; CHECK-LABEL: @test2(
25; CHECK-NEXT: ret i1 false
26	%B = xor i1 %A, %A		; <i1> [#uses=1]
27	ret i1 %B
28}
29
30define i32 @test3(i32 %A) {
31; CHECK-LABEL: @test3(
32; CHECK-NEXT: ret i32 0
33	%B = xor i32 %A, %A		; <i32> [#uses=1]
34	ret i32 %B
35}
36
37define i32 @test4(i32 %A) {
38; CHECK-LABEL: @test4(
39; CHECK-NEXT: ret i32 -1
40	%NotA = xor i32 -1, %A		; <i32> [#uses=1]
41	%B = xor i32 %A, %NotA		; <i32> [#uses=1]
42	ret i32 %B
43}
44
45define i32 @test5(i32 %A) {
46; CHECK-LABEL: @test5(
47; CHECK-NEXT: %1 = and i32 %A, -124
48; CHECK-NEXT: ret i32 %1
49	%t1 = or i32 %A, 123		; <i32> [#uses=1]
50	%r = xor i32 %t1, 123		; <i32> [#uses=1]
51	ret i32 %r
52}
53
54define i8 @test6(i8 %A) {
55; CHECK-LABEL: @test6(
56; CHECK-NEXT: ret i8 %A
57	%B = xor i8 %A, 17		; <i8> [#uses=1]
58	%C = xor i8 %B, 17		; <i8> [#uses=1]
59	ret i8 %C
60}
61
62define i32 @test7(i32 %A, i32 %B) {
63; CHECK-LABEL: @test7(
64; CHECK-NEXT: %A1 = and i32 %A, 7
65; CHECK-NEXT: %B1 = and i32 %B, 128
66; CHECK-NEXT: %C11 = or i32 %A1, %B1
67; CHECK-NEXT: ret i32 %C11
68	%A1 = and i32 %A, 7		; <i32> [#uses=1]
69	%B1 = and i32 %B, 128		; <i32> [#uses=1]
70	%C1 = xor i32 %A1, %B1		; <i32> [#uses=1]
71	ret i32 %C1
72}
73
74define i8 @test8(i1 %c) {
75; CHECK-LABEL: @test8(
76; CHECK: br i1 %c, label %False, label %True
77	%d = xor i1 %c, true		; <i1> [#uses=1]
78	br i1 %d, label %True, label %False
79
80True:		; preds = %0
81	ret i8 1
82
83False:		; preds = %0
84	ret i8 3
85}
86
87define i1 @test9(i8 %A) {
88; CHECK-LABEL: @test9(
89; CHECK-NEXT: %C = icmp eq i8 %A, 89
90; CHECK-NEXT: ret i1 %C
91	%B = xor i8 %A, 123		; <i8> [#uses=1]
92	%C = icmp eq i8 %B, 34		; <i1> [#uses=1]
93	ret i1 %C
94}
95
96define i8 @test10(i8 %A) {
97; CHECK-LABEL: @test10(
98; CHECK-NEXT: %B = and i8 %A, 3
99; CHECK-NEXT: %C1 = or i8 %B, 4
100; CHECK-NEXT: ret i8 %C1
101	%B = and i8 %A, 3		; <i8> [#uses=1]
102	%C = xor i8 %B, 4		; <i8> [#uses=1]
103	ret i8 %C
104}
105
106define i8 @test11(i8 %A) {
107; CHECK-LABEL: @test11(
108; CHECK-NEXT: %B = and i8 %A, -13
109; CHECK-NEXT: %1 = or i8 %B, 8
110; CHECK-NEXT: ret i8 %1
111	%B = or i8 %A, 12		; <i8> [#uses=1]
112	%C = xor i8 %B, 4		; <i8> [#uses=1]
113	ret i8 %C
114}
115
116define i1 @test12(i8 %A) {
117; CHECK-LABEL: @test12(
118; CHECK-NEXT: %c = icmp ne i8 %A, 4
119; CHECK-NEXT: ret i1 %c
120	%B = xor i8 %A, 4		; <i8> [#uses=1]
121	%c = icmp ne i8 %B, 0		; <i1> [#uses=1]
122	ret i1 %c
123}
124
125define i1 @test13(i8 %A, i8 %B) {
126; CHECK-LABEL: @test13(
127; CHECK-NEXT: %1 = icmp ne i8 %A, %B
128; CHECK-NEXT: ret i1 %1
129	%C = icmp ult i8 %A, %B		; <i1> [#uses=1]
130	%D = icmp ugt i8 %A, %B		; <i1> [#uses=1]
131	%E = xor i1 %C, %D		; <i1> [#uses=1]
132	ret i1 %E
133}
134
135define i1 @test14(i8 %A, i8 %B) {
136; CHECK-LABEL: @test14(
137; CHECK-NEXT: ret i1 true
138	%C = icmp eq i8 %A, %B		; <i1> [#uses=1]
139	%D = icmp ne i8 %B, %A		; <i1> [#uses=1]
140	%E = xor i1 %C, %D		; <i1> [#uses=1]
141	ret i1 %E
142}
143
144define i32 @test15(i32 %A) {
145; CHECK-LABEL: @test15(
146; CHECK-NEXT: %C = sub i32 0, %A
147; CHECK-NEXT: ret i32 %C
148	%B = add i32 %A, -1		; <i32> [#uses=1]
149	%C = xor i32 %B, -1		; <i32> [#uses=1]
150	ret i32 %C
151}
152
153define i32 @test16(i32 %A) {
154; CHECK-LABEL: @test16(
155; CHECK-NEXT: %C = sub i32 -124, %A
156; CHECK-NEXT: ret i32 %C
157	%B = add i32 %A, 123		; <i32> [#uses=1]
158	%C = xor i32 %B, -1		; <i32> [#uses=1]
159	ret i32 %C
160}
161
162define i32 @test17(i32 %A) {
163; CHECK-LABEL: @test17(
164; CHECK-NEXT: %C = add i32 %A, -124
165; CHECK-NEXT: ret i32 %C
166	%B = sub i32 123, %A		; <i32> [#uses=1]
167	%C = xor i32 %B, -1		; <i32> [#uses=1]
168	ret i32 %C
169}
170
171define i32 @test18(i32 %A) {
172; CHECK-LABEL: @test18(
173; CHECK-NEXT: %C = add i32 %A, 124
174; CHECK-NEXT: ret i32 %C
175	%B = xor i32 %A, -1		; <i32> [#uses=1]
176	%C = sub i32 123, %B		; <i32> [#uses=1]
177	ret i32 %C
178}
179
180define i32 @test19(i32 %A, i32 %B) {
181; CHECK-LABEL: @test19(
182; CHECK-NEXT: ret i32 %B
183	%C = xor i32 %A, %B		; <i32> [#uses=1]
184	%D = xor i32 %C, %A		; <i32> [#uses=1]
185	ret i32 %D
186}
187
188define void @test20(i32 %A, i32 %B) {
189; CHECK-LABEL: @test20(
190; CHECK-NEXT: store i32 %B, i32* @G1
191; CHECK-NEXT: store i32 %A, i32* @G2
192; CHECK-NEXT: ret void
193	%tmp.2 = xor i32 %B, %A		; <i32> [#uses=2]
194	%tmp.5 = xor i32 %tmp.2, %B		; <i32> [#uses=2]
195	%tmp.8 = xor i32 %tmp.5, %tmp.2		; <i32> [#uses=1]
196	store i32 %tmp.8, i32* @G1
197	store i32 %tmp.5, i32* @G2
198	ret void
199}
200
201define i32 @test21(i1 %C, i32 %A, i32 %B) {
202; CHECK-LABEL: @test21(
203; CHECK-NEXT: %D = select i1 %C, i32 %B, i32 %A
204; CHECK-NEXT: ret i32 %D
205	%C2 = xor i1 %C, true		; <i1> [#uses=1]
206	%D = select i1 %C2, i32 %A, i32 %B		; <i32> [#uses=1]
207	ret i32 %D
208}
209
210define i32 @test22(i1 %X) {
211; CHECK-LABEL: @test22(
212; CHECK-NEXT: %1 = zext i1 %X to i32
213; CHECK-NEXT: ret i32 %1
214	%Y = xor i1 %X, true		; <i1> [#uses=1]
215	%Z = zext i1 %Y to i32		; <i32> [#uses=1]
216	%Q = xor i32 %Z, 1		; <i32> [#uses=1]
217	ret i32 %Q
218}
219
220define i1 @test23(i32 %a, i32 %b) {
221; CHECK-LABEL: @test23(
222; CHECK-NEXT: %tmp.4 = icmp eq i32 %b, 0
223; CHECK-NEXT: ret i1 %tmp.4
224	%tmp.2 = xor i32 %b, %a		; <i32> [#uses=1]
225	%tmp.4 = icmp eq i32 %tmp.2, %a		; <i1> [#uses=1]
226	ret i1 %tmp.4
227}
228
229define i1 @test24(i32 %c, i32 %d) {
230; CHECK-LABEL: @test24(
231; CHECK-NEXT: %tmp.4 = icmp ne i32 %d, 0
232; CHECK-NEXT: ret i1 %tmp.4
233	%tmp.2 = xor i32 %d, %c		; <i32> [#uses=1]
234	%tmp.4 = icmp ne i32 %tmp.2, %c		; <i1> [#uses=1]
235	ret i1 %tmp.4
236}
237
238define i32 @test25(i32 %g, i32 %h) {
239; CHECK-LABEL: @test25(
240; CHECK-NEXT: %tmp4 = and i32 %h, %g
241; CHECK-NEXT: ret i32 %tmp4
242	%h2 = xor i32 %h, -1		; <i32> [#uses=1]
243	%tmp2 = and i32 %h2, %g		; <i32> [#uses=1]
244	%tmp4 = xor i32 %tmp2, %g		; <i32> [#uses=1]
245	ret i32 %tmp4
246}
247
248define i32 @test26(i32 %a, i32 %b) {
249; CHECK-LABEL: @test26(
250; CHECK-NEXT: %tmp4 = and i32 %a, %b
251; CHECK-NEXT: ret i32 %tmp4
252	%b2 = xor i32 %b, -1		; <i32> [#uses=1]
253	%tmp2 = xor i32 %a, %b2		; <i32> [#uses=1]
254	%tmp4 = and i32 %tmp2, %a		; <i32> [#uses=1]
255	ret i32 %tmp4
256}
257
258define i32 @test27(i32 %b, i32 %c, i32 %d) {
259; CHECK-LABEL: @test27(
260; CHECK-NEXT: %tmp = icmp eq i32 %b, %c
261; CHECK-NEXT: %tmp6 = zext i1 %tmp to i32
262; CHECK-NEXT: ret i32 %tmp6
263	%tmp2 = xor i32 %d, %b		; <i32> [#uses=1]
264	%tmp5 = xor i32 %d, %c		; <i32> [#uses=1]
265	%tmp = icmp eq i32 %tmp2, %tmp5		; <i1> [#uses=1]
266	%tmp6 = zext i1 %tmp to i32		; <i32> [#uses=1]
267	ret i32 %tmp6
268}
269
270define i32 @test28(i32 %indvar) {
271; CHECK-LABEL: @test28(
272; CHECK-NEXT: %tmp214 = add i32 %indvar, 1
273; CHECK-NEXT: ret i32 %tmp214
274	%tmp7 = add i32 %indvar, -2147483647		; <i32> [#uses=1]
275	%tmp214 = xor i32 %tmp7, -2147483648		; <i32> [#uses=1]
276	ret i32 %tmp214
277}
278