1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3
4define i32 @poison(i32 %x) {
5; CHECK-LABEL: @poison(
6; CHECK-NEXT:    ret i32 poison
7;
8  %v = xor i32 %x, poison
9  ret i32 %v
10}
11
12define i4 @xor_and_or_not_commute0(i4 %a, i4 %b) {
13; CHECK-LABEL: @xor_and_or_not_commute0(
14; CHECK-NEXT:    [[NOT:%.*]] = xor i4 [[A:%.*]], -1
15; CHECK-NEXT:    ret i4 [[NOT]]
16;
17  %and = and i4 %a, %b
18  %not = xor i4 %a, -1
19  %or = or i4 %not, %b
20  %r = xor i4 %and, %or
21  ret i4 %r
22}
23
24define <2 x i4> @xor_and_or_not_commute1(<2 x i4> %a, <2 x i4> %b) {
25; CHECK-LABEL: @xor_and_or_not_commute1(
26; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i4> [[A:%.*]], <i4 -1, i4 -1>
27; CHECK-NEXT:    ret <2 x i4> [[NOT]]
28;
29  %and = and <2 x i4> %a, %b
30  %not = xor <2 x i4> %a, <i4 -1, i4 -1>
31  %or = or <2 x i4> %not, %b
32  %r = xor <2 x i4> %or, %and
33  ret <2 x i4> %r
34}
35
36define i74 @xor_and_or_not_commute2(i74 %a, i74 %b) {
37; CHECK-LABEL: @xor_and_or_not_commute2(
38; CHECK-NEXT:    [[NOT:%.*]] = xor i74 [[A:%.*]], -1
39; CHECK-NEXT:    ret i74 [[NOT]]
40;
41  %and = and i74 %b, %a
42  %not = xor i74 %a, -1
43  %or = or i74 %not, %b
44  %r = xor i74 %and, %or
45  ret i74 %r
46}
47
48define <2 x i4> @xor_and_or_not_commute3(<2 x i4> %a, <2 x i4> %b) {
49; CHECK-LABEL: @xor_and_or_not_commute3(
50; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i4> [[A:%.*]], <i4 -1, i4 -1>
51; CHECK-NEXT:    ret <2 x i4> [[NOT]]
52;
53  %and = and <2 x i4> %b, %a
54  %not = xor <2 x i4> %a, <i4 -1, i4 -1>
55  %or = or <2 x i4> %not, %b
56  %r = xor <2 x i4> %or, %and
57  ret <2 x i4> %r
58}
59
60define i8 @xor_and_or_not_commute4(i8 %a, i8 %b) {
61; CHECK-LABEL: @xor_and_or_not_commute4(
62; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[A:%.*]], -1
63; CHECK-NEXT:    ret i8 [[NOT]]
64;
65  %and = and i8 %a, %b
66  %not = xor i8 %a, -1
67  %or = or i8 %b, %not
68  %r = xor i8 %and, %or
69  ret i8 %r
70}
71
72define i8 @xor_and_or_not_commute5(i8 %a, i8 %b) {
73; CHECK-LABEL: @xor_and_or_not_commute5(
74; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[A:%.*]], -1
75; CHECK-NEXT:    ret i8 [[NOT]]
76;
77  %and = and i8 %a, %b
78  %not = xor i8 %a, -1
79  %or = or i8 %b, %not
80  %r = xor i8 %or, %and
81  ret i8 %r
82}
83
84define i8 @xor_and_or_not_commute6(i8 %a, i8 %b) {
85; CHECK-LABEL: @xor_and_or_not_commute6(
86; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[A:%.*]], -1
87; CHECK-NEXT:    ret i8 [[NOT]]
88;
89  %and = and i8 %b, %a
90  %not = xor i8 %a, -1
91  %or = or i8 %b, %not
92  %r = xor i8 %and, %or
93  ret i8 %r
94}
95
96define i8 @xor_and_or_not_commute7(i8 %a, i8 %b) {
97; CHECK-LABEL: @xor_and_or_not_commute7(
98; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[A:%.*]], -1
99; CHECK-NEXT:    ret i8 [[NOT]]
100;
101  %and = and i8 %b, %a
102  %not = xor i8 %a, -1
103  %or = or i8 %b, %not
104  %r = xor i8 %or, %and
105  ret i8 %r
106}
107
108; negative test - must match specific values
109
110define i4 @xor_and_or_not_wrong_val1(i4 %a, i4 %b, i4 %c) {
111; CHECK-LABEL: @xor_and_or_not_wrong_val1(
112; CHECK-NEXT:    [[AND:%.*]] = and i4 [[A:%.*]], [[C:%.*]]
113; CHECK-NEXT:    [[NOT:%.*]] = xor i4 [[A]], -1
114; CHECK-NEXT:    [[OR:%.*]] = or i4 [[NOT]], [[B:%.*]]
115; CHECK-NEXT:    [[R:%.*]] = xor i4 [[AND]], [[OR]]
116; CHECK-NEXT:    ret i4 [[R]]
117;
118  %and = and i4 %a, %c
119  %not = xor i4 %a, -1
120  %or = or i4 %not, %b
121  %r = xor i4 %and, %or
122  ret i4 %r
123}
124
125; negative test - must match specific values
126
127define i4 @xor_and_or_not_wrong_val2(i4 %a, i4 %b, i4 %c) {
128; CHECK-LABEL: @xor_and_or_not_wrong_val2(
129; CHECK-NEXT:    [[AND:%.*]] = and i4 [[C:%.*]], [[B:%.*]]
130; CHECK-NEXT:    [[NOT:%.*]] = xor i4 [[A:%.*]], -1
131; CHECK-NEXT:    [[OR:%.*]] = or i4 [[NOT]], [[B]]
132; CHECK-NEXT:    [[R:%.*]] = xor i4 [[AND]], [[OR]]
133; CHECK-NEXT:    ret i4 [[R]]
134;
135  %and = and i4 %c, %b
136  %not = xor i4 %a, -1
137  %or = or i4 %not, %b
138  %r = xor i4 %and, %or
139  ret i4 %r
140}
141
142; negative test - incorrect to propagate undef element
143
144define <2 x i4> @xor_and_or_not_undef_elt(<2 x i4> %a, <2 x i4> %b) {
145; CHECK-LABEL: @xor_and_or_not_undef_elt(
146; CHECK-NEXT:    [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[A:%.*]]
147; CHECK-NEXT:    [[NOT:%.*]] = xor <2 x i4> [[A]], <i4 -1, i4 undef>
148; CHECK-NEXT:    [[OR:%.*]] = or <2 x i4> [[NOT]], [[B]]
149; CHECK-NEXT:    [[R:%.*]] = xor <2 x i4> [[OR]], [[AND]]
150; CHECK-NEXT:    ret <2 x i4> [[R]]
151;
152  %and = and <2 x i4> %b, %a
153  %not = xor <2 x i4> %a, <i4 -1, i4 undef>
154  %or = or <2 x i4> %not, %b
155  %r = xor <2 x i4> %or, %and
156  ret <2 x i4> %r
157}
158
159define i4 @xor_or_and_not_commute0(i4 %a, i4 %b) {
160; CHECK-LABEL: @xor_or_and_not_commute0(
161; CHECK-NEXT:    ret i4 [[A:%.*]]
162;
163  %not = xor i4 %a, -1
164  %and = and i4 %not, %b
165  %or = or i4 %a, %b
166  %r = xor i4 %and, %or
167  ret i4 %r
168}
169
170define <2 x i4> @xor_or_and_not_commute1(<2 x i4> %a, <2 x i4> %b) {
171; CHECK-LABEL: @xor_or_and_not_commute1(
172; CHECK-NEXT:    ret <2 x i4> [[A:%.*]]
173;
174  %not = xor <2 x i4> %a, <i4 -1, i4 -1>
175  %and = and <2 x i4> %not, %b
176  %or = or <2 x i4> %a, %b
177  %r = xor <2 x i4> %or, %and
178  ret <2 x i4> %r
179}
180
181define i74 @xor_or_and_not_commute2(i74 %a, i74 %b) {
182; CHECK-LABEL: @xor_or_and_not_commute2(
183; CHECK-NEXT:    ret i74 [[A:%.*]]
184;
185  %not = xor i74 %a, -1
186  %and = and i74 %b, %not
187  %or = or i74 %a, %b
188  %r = xor i74 %and, %or
189  ret i74 %r
190}
191
192define <2 x i4> @xor_or_and_not_commute3(<2 x i4> %a, <2 x i4> %b) {
193; CHECK-LABEL: @xor_or_and_not_commute3(
194; CHECK-NEXT:    ret <2 x i4> [[A:%.*]]
195;
196  %not = xor <2 x i4> %a, <i4 -1, i4 -1>
197  %and = and <2 x i4> %b, %not
198  %or = or <2 x i4> %a, %b
199  %r = xor <2 x i4> %or, %and
200  ret <2 x i4> %r
201}
202
203define i8 @xor_or_and_not_commute4(i8 %a, i8 %b) {
204; CHECK-LABEL: @xor_or_and_not_commute4(
205; CHECK-NEXT:    ret i8 [[A:%.*]]
206;
207  %not = xor i8 %a, -1
208  %and = and i8 %not, %b
209  %or = or i8 %b, %a
210  %r = xor i8 %and, %or
211  ret i8 %r
212}
213
214define i8 @xor_or_and_not_commute5(i8 %a, i8 %b) {
215; CHECK-LABEL: @xor_or_and_not_commute5(
216; CHECK-NEXT:    ret i8 [[A:%.*]]
217;
218  %not = xor i8 %a, -1
219  %and = and i8 %not, %b
220  %or = or i8 %b, %a
221  %r = xor i8 %or, %and
222  ret i8 %r
223}
224
225define i8 @xor_or_and_not_commute6(i8 %a, i8 %b) {
226; CHECK-LABEL: @xor_or_and_not_commute6(
227; CHECK-NEXT:    ret i8 [[A:%.*]]
228;
229  %not = xor i8 %a, -1
230  %and = and i8 %b, %not
231  %or = or i8 %b, %a
232  %r = xor i8 %and, %or
233  ret i8 %r
234}
235
236define i8 @xor_or_and_not_commute7(i8 %a, i8 %b) {
237; CHECK-LABEL: @xor_or_and_not_commute7(
238; CHECK-NEXT:    ret i8 [[A:%.*]]
239;
240  %not = xor i8 %a, -1
241  %and = and i8 %b, %not
242  %or = or i8 %b, %a
243  %r = xor i8 %or, %and
244  ret i8 %r
245}
246
247; negative test - must match specific values
248
249define i4 @xor_or_and_not_wrong_val1(i4 %a, i4 %b, i4 %c) {
250; CHECK-LABEL: @xor_or_and_not_wrong_val1(
251; CHECK-NEXT:    [[NOT:%.*]] = xor i4 [[A:%.*]], -1
252; CHECK-NEXT:    [[AND:%.*]] = and i4 [[NOT]], [[C:%.*]]
253; CHECK-NEXT:    [[OR:%.*]] = or i4 [[A]], [[B:%.*]]
254; CHECK-NEXT:    [[R:%.*]] = xor i4 [[AND]], [[OR]]
255; CHECK-NEXT:    ret i4 [[R]]
256;
257  %not = xor i4 %a, -1
258  %and = and i4 %not, %c
259  %or = or i4 %a, %b
260  %r = xor i4 %and, %or
261  ret i4 %r
262}
263
264; negative test - must match specific values
265
266define i4 @xor_or_and_not_wrong_val2(i4 %a, i4 %b, i4 %c) {
267; CHECK-LABEL: @xor_or_and_not_wrong_val2(
268; CHECK-NEXT:    [[AND:%.*]] = and i4 [[C:%.*]], [[B:%.*]]
269; CHECK-NEXT:    [[OR:%.*]] = or i4 [[A:%.*]], [[B]]
270; CHECK-NEXT:    [[R:%.*]] = xor i4 [[AND]], [[OR]]
271; CHECK-NEXT:    ret i4 [[R]]
272;
273  %not = xor i4 %a, -1
274  %and = and i4 %c, %b
275  %or = or i4 %a, %b
276  %r = xor i4 %and, %or
277  ret i4 %r
278}
279
280define <2 x i4> @xor_or_and_not_undef_elt(<2 x i4> %a, <2 x i4> %b) {
281; CHECK-LABEL: @xor_or_and_not_undef_elt(
282; CHECK-NEXT:    ret <2 x i4> [[A:%.*]]
283;
284  %not = xor <2 x i4> %a, <i4 -1, i4 undef>
285  %and = and <2 x i4> %b, %not
286  %or = or <2 x i4> %a, %b
287  %r = xor <2 x i4> %or, %and
288  ret <2 x i4> %r
289}
290