1; This test makes sure that these instructions are properly eliminated.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5; PR1253
6define i1 @test0(i32 %A) {
7; CHECK-LABEL: @test0(
8; CHECK: %C = icmp slt i32 %A, 0
9	%B = xor i32 %A, -2147483648
10	%C = icmp sgt i32 %B, -1
11	ret i1 %C
12}
13
14define i1 @test1(i32 %A) {
15; CHECK-LABEL: @test1(
16; CHECK: %C = icmp slt i32 %A, 0
17	%B = xor i32 %A, 12345
18	%C = icmp slt i32 %B, 0
19	ret i1 %C
20}
21
22; PR1014
23define i32 @test2(i32 %tmp1) {
24; CHECK-LABEL:      @test2(
25; CHECK-NEXT:   and i32 %tmp1, 32
26; CHECK-NEXT:   or i32 %ovm, 8
27; CHECK-NEXT:   ret i32
28        %ovm = and i32 %tmp1, 32
29        %ov3 = add i32 %ovm, 145
30        %ov110 = xor i32 %ov3, 153
31        ret i32 %ov110
32}
33
34define i32 @test3(i32 %tmp1) {
35; CHECK-LABEL:      @test3(
36; CHECK-NEXT:   and i32 %tmp1, 32
37; CHECK-NEXT:   or i32 %ovm, 8
38; CHECK-NEXT:   ret i32
39  %ovm = or i32 %tmp1, 145
40  %ov31 = and i32 %ovm, 177
41  %ov110 = xor i32 %ov31, 153
42  ret i32 %ov110
43}
44
45define i32 @test4(i32 %A, i32 %B) {
46	%1 = xor i32 %A, -1
47	%2 = ashr i32 %1, %B
48	%3 = xor i32 %2, -1
49	ret i32 %3
50; CHECK-LABEL: @test4(
51; CHECK: %1 = ashr i32 %A, %B
52; CHECK: ret i32 %1
53}
54
55; defect-2 in rdar://12329730
56; (X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
57;   where the "X" has more than one use
58define i32 @test5(i32 %val1) {
59test5:
60  %xor = xor i32 %val1, 1234
61  %shr = lshr i32 %xor, 8
62  %xor1 = xor i32 %shr, 1
63  %add = add i32 %xor1, %xor
64  ret i32 %add
65; CHECK-LABEL: @test5(
66; CHECK: lshr i32 %val1, 8
67; CHECK: ret
68}
69
70; defect-1 in rdar://12329730
71; Simplify (X^Y) -> X or Y in the user's context if we know that
72; only bits from X or Y are demanded.
73; e.g. the "x ^ 1234" can be optimized into x in the context of "t >> 16".
74;  Put in other word, t >> 16 -> x >> 16.
75; unsigned foo(unsigned x) { unsigned t = x ^ 1234; ;  return (t >> 16) + t;}
76define i32 @test6(i32 %x) {
77  %xor = xor i32 %x, 1234
78  %shr = lshr i32 %xor, 16
79  %add = add i32 %shr, %xor
80  ret i32 %add
81; CHECK-LABEL: @test6(
82; CHECK: lshr i32 %x, 16
83; CHECK: ret
84}
85
86
87; (A | B) ^ (~A) -> (A | ~B)
88define i32 @test7(i32 %a, i32 %b) {
89 %or = or i32 %a, %b
90 %neg = xor i32 %a, -1
91 %xor = xor i32 %or, %neg
92 ret i32 %xor
93; CHECK-LABEL: @test7(
94; CHECK-NEXT: %[[b_not:.*]] = xor i32 %b, -1
95; CHECK-NEXT: %[[or:.*]] = or i32 %a, %[[b_not]]
96; CHECK-NEXT: ret i32 %[[or]]
97}
98
99; (~A) ^ (A | B) -> (A | ~B)
100define i32 @test8(i32 %a, i32 %b) {
101 %neg = xor i32 %a, -1
102 %or = or i32 %a, %b
103 %xor = xor i32 %neg, %or
104 ret i32 %xor
105; CHECK-LABEL: @test8(
106; CHECK-NEXT: %[[b_not:.*]] = xor i32 %b, -1
107; CHECK-NEXT: %[[or:.*]] = or i32 %a, %[[b_not]]
108; CHECK-NEXT: ret i32 %[[or]]
109}
110
111; (A & B) ^ (A ^ B) -> (A | B)
112define i32 @test9(i32 %b, i32 %c) {
113 %and = and i32 %b, %c
114 %xor = xor i32 %b, %c
115 %xor2 = xor i32 %and, %xor
116 ret i32 %xor2
117; CHECK-LABEL: @test9(
118; CHECK-NEXT: %xor2 = or i32 %b, %c
119}
120
121; (A ^ B) ^ (A & B) -> (A | B)
122define i32 @test10(i32 %b, i32 %c) {
123 %xor = xor i32 %b, %c
124 %and = and i32 %b, %c
125 %xor2 = xor i32 %xor, %and
126 ret i32 %xor2
127; CHECK-LABEL: @test10(
128; CHECK-NEXT: %xor2 = or i32 %b, %c
129}
130
131define i32 @test11(i32 %A, i32 %B) {
132  %xor1 = xor i32 %B, %A
133  %not = xor i32 %A, -1
134  %xor2 = xor i32 %not, %B
135  %and = and i32 %xor1, %xor2
136  ret i32 %and
137; CHECK-LABEL: @test11(
138; CHECK-NEXT: ret i32 0
139}
140
141define i32 @test12(i32 %a, i32 %b) {
142 %negb = xor i32 %b, -1
143 %and = and i32 %a, %negb
144 %nega = xor i32 %a, -1
145 %xor = xor i32 %and, %nega
146 ret i32 %xor
147; CHECK-LABEL: @test12(
148; CHECK-NEXT: %1 = and i32 %a, %b
149; CHECK-NEXT: %xor = xor i32 %1, -1
150}
151
152define i32 @test13(i32 %a, i32 %b) {
153 %nega = xor i32 %a, -1
154 %negb = xor i32 %b, -1
155 %and = and i32 %a, %negb
156 %xor = xor i32 %nega, %and
157 ret i32 %xor
158; CHECK-LABEL: @test13(
159; CHECK-NEXT: %1 = and i32 %a, %b
160; CHECK-NEXT: %xor = xor i32 %1, -1
161}
162
163; (A ^ C) ^ (A | B) -> ((~A) & B) ^ C
164define i32 @test14(i32 %a, i32 %b, i32 %c) {
165 %neg = xor i32 %a, %c
166 %or = or i32 %a, %b
167 %xor = xor i32 %neg, %or
168 ret i32 %xor
169; CHECK-LABEL: @test14(
170; CHECK-NEXT: %[[not:.*]] = xor i32 %a, -1
171; CHECK-NEXT: %[[and:.*]] = and i32 %[[not]], %b
172; CHECK-NEXT: %[[xor:.*]] = xor i32 %[[and]], %c
173; CHECK-NEXT: ret i32 %[[xor]]
174}
175