xref: /llvm-project/llvm/test/Transforms/InstSimplify/and.ll (revision b280ee1dd7e9b36ae7aaa3953556e4b7a7f31a29)
1864dda5fSJuneyoung Lee; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2*b280ee1dSBjorn Pettersson; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3864dda5fSJuneyoung Lee
4864dda5fSJuneyoung Leedefine i32 @poison(i32 %x) {
5864dda5fSJuneyoung Lee; CHECK-LABEL: @poison(
65af8baccSJuneyoung Lee; CHECK-NEXT:    ret i32 poison
7864dda5fSJuneyoung Lee;
8864dda5fSJuneyoung Lee  %v = and i32 %x, poison
9864dda5fSJuneyoung Lee  ret i32 %v
10864dda5fSJuneyoung Lee}
114666324fSSanjay Patel
12e36d351dSSanjay Patel; (X | Y) & (X | ~Y) --> X (commuted 8 ways)
13e36d351dSSanjay Patel
144666324fSSanjay Pateldefine i8 @or_or_not_commute0(i8 %x, i8 %y) {
154666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute0(
16e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
174666324fSSanjay Patel;
184666324fSSanjay Patel  %ynot = xor i8 %y, -1
194666324fSSanjay Patel  %xory = or i8 %x, %y
204666324fSSanjay Patel  %xorynot = or i8 %x, %ynot
214666324fSSanjay Patel  %and = and i8 %xory, %xorynot
224666324fSSanjay Patel  ret i8 %and
234666324fSSanjay Patel}
244666324fSSanjay Patel
254666324fSSanjay Pateldefine <2 x i5> @or_or_not_commute1(<2 x i5> %x, <2 x i5> %y) {
264666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute1(
27e36d351dSSanjay Patel; CHECK-NEXT:    ret <2 x i5> [[X:%.*]]
284666324fSSanjay Patel;
294666324fSSanjay Patel  %ynot = xor <2 x i5> %y, <i5 -1, i5 -1>
304666324fSSanjay Patel  %xory = or <2 x i5> %x, %y
314666324fSSanjay Patel  %xorynot = or <2 x i5> %x, %ynot
324666324fSSanjay Patel  %and = and <2 x i5> %xorynot, %xory
334666324fSSanjay Patel  ret <2 x i5> %and
344666324fSSanjay Patel}
354666324fSSanjay Patel
364666324fSSanjay Pateldefine <2 x i8> @or_or_not_commute2(<2 x i8> %x, <2 x i8> %y) {
374666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute2(
38e36d351dSSanjay Patel; CHECK-NEXT:    ret <2 x i8> [[X:%.*]]
394666324fSSanjay Patel;
404666324fSSanjay Patel  %ynot = xor <2 x i8> %y, <i8 poison, i8 -1>
414666324fSSanjay Patel  %xory = or <2 x i8> %x, %y
424666324fSSanjay Patel  %xorynot = or <2 x i8> %ynot, %x
434666324fSSanjay Patel  %and = and <2 x i8> %xory, %xorynot
444666324fSSanjay Patel  ret <2 x i8> %and
454666324fSSanjay Patel}
464666324fSSanjay Patel
474666324fSSanjay Pateldefine i8 @or_or_not_commute3(i8 %x, i8 %y) {
484666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute3(
49e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
504666324fSSanjay Patel;
514666324fSSanjay Patel  %ynot = xor i8 %y, -1
524666324fSSanjay Patel  %xory = or i8 %x, %y
534666324fSSanjay Patel  %xorynot = or i8 %ynot, %x
544666324fSSanjay Patel  %and = and i8 %xorynot, %xory
554666324fSSanjay Patel  ret i8 %and
564666324fSSanjay Patel}
574666324fSSanjay Pateldefine i8 @or_or_not_commute4(i8 %x, i8 %y) {
584666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute4(
59e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
604666324fSSanjay Patel;
614666324fSSanjay Patel  %ynot = xor i8 %y, -1
624666324fSSanjay Patel  %xory = or i8 %y, %x
634666324fSSanjay Patel  %xorynot = or i8 %x, %ynot
644666324fSSanjay Patel  %and = and i8 %xory, %xorynot
654666324fSSanjay Patel  ret i8 %and
664666324fSSanjay Patel}
674666324fSSanjay Patel
684666324fSSanjay Pateldefine i8 @or_or_not_commute5(i8 %x, i8 %y) {
694666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute5(
70e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
714666324fSSanjay Patel;
724666324fSSanjay Patel  %ynot = xor i8 %y, -1
734666324fSSanjay Patel  %xory = or i8 %y, %x
744666324fSSanjay Patel  %xorynot = or i8 %x, %ynot
754666324fSSanjay Patel  %and = and i8 %xorynot, %xory
764666324fSSanjay Patel  ret i8 %and
774666324fSSanjay Patel}
784666324fSSanjay Patel
794666324fSSanjay Pateldefine i8 @or_or_not_commute6(i8 %x, i8 %y) {
804666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute6(
81e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
824666324fSSanjay Patel;
834666324fSSanjay Patel  %ynot = xor i8 %y, -1
844666324fSSanjay Patel  %xory = or i8 %y, %x
854666324fSSanjay Patel  %xorynot = or i8 %ynot, %x
864666324fSSanjay Patel  %and = and i8 %xory, %xorynot
874666324fSSanjay Patel  ret i8 %and
884666324fSSanjay Patel}
894666324fSSanjay Patel
904666324fSSanjay Pateldefine i8 @or_or_not_commute7(i8 %x, i8 %y) {
914666324fSSanjay Patel; CHECK-LABEL: @or_or_not_commute7(
92e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[X:%.*]]
934666324fSSanjay Patel;
944666324fSSanjay Patel  %ynot = xor i8 %y, -1
954666324fSSanjay Patel  %xory = or i8 %y, %x
964666324fSSanjay Patel  %xorynot = or i8 %ynot, %x
974666324fSSanjay Patel  %and = and i8 %xorynot, %xory
984666324fSSanjay Patel  ret i8 %and
994666324fSSanjay Patel}
100e36d351dSSanjay Patel
101e36d351dSSanjay Patel; negative test - wrong logic op
102e36d351dSSanjay Patel
103e36d351dSSanjay Pateldefine i8 @or_xor_not(i8 %x, i8 %y) {
104e36d351dSSanjay Patel; CHECK-LABEL: @or_xor_not(
105e36d351dSSanjay Patel; CHECK-NEXT:    [[YNOT:%.*]] = xor i8 [[Y:%.*]], -1
106e36d351dSSanjay Patel; CHECK-NEXT:    [[XXORY:%.*]] = xor i8 [[Y]], [[X:%.*]]
107e36d351dSSanjay Patel; CHECK-NEXT:    [[XORYNOT:%.*]] = or i8 [[X]], [[YNOT]]
108e36d351dSSanjay Patel; CHECK-NEXT:    [[AND:%.*]] = and i8 [[XORYNOT]], [[XXORY]]
109e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[AND]]
110e36d351dSSanjay Patel;
111e36d351dSSanjay Patel  %ynot = xor i8 %y, -1
112e36d351dSSanjay Patel  %xxory = xor i8 %y, %x
113e36d351dSSanjay Patel  %xorynot = or i8 %x, %ynot
114e36d351dSSanjay Patel  %and = and i8 %xorynot, %xxory
115e36d351dSSanjay Patel  ret i8 %and
116e36d351dSSanjay Patel}
117e36d351dSSanjay Patel
118e36d351dSSanjay Patel; negative test - must have common operands
119e36d351dSSanjay Patel
120e36d351dSSanjay Pateldefine i8 @or_or_not_no_common_op(i8 %x, i8 %y, i8 %z) {
121e36d351dSSanjay Patel; CHECK-LABEL: @or_or_not_no_common_op(
122e36d351dSSanjay Patel; CHECK-NEXT:    [[XORZ:%.*]] = or i8 [[Z:%.*]], [[X:%.*]]
123e36d351dSSanjay Patel; CHECK-NEXT:    [[YNOT:%.*]] = xor i8 [[Y:%.*]], -1
124e36d351dSSanjay Patel; CHECK-NEXT:    [[XORYNOT:%.*]] = or i8 [[X]], [[YNOT]]
125e36d351dSSanjay Patel; CHECK-NEXT:    [[AND:%.*]] = and i8 [[XORYNOT]], [[XORZ]]
126e36d351dSSanjay Patel; CHECK-NEXT:    ret i8 [[AND]]
127e36d351dSSanjay Patel;
128e36d351dSSanjay Patel  %xorz = or i8 %z, %x
129e36d351dSSanjay Patel  %ynot = xor i8 %y, -1
130e36d351dSSanjay Patel  %xorynot = or i8 %x, %ynot
131e36d351dSSanjay Patel  %and = and i8 %xorynot, %xorz
132e36d351dSSanjay Patel  ret i8 %and
133e36d351dSSanjay Patel}
134b5c49b62SMehrnoosh Heidarpour
1350ff20f2fSMehrnoosh Heidarpour; ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
136b5c49b62SMehrnoosh Heidarpour
137b5c49b62SMehrnoosh Heidarpourdefine i8 @or_xor(i8 %x, i8 %y) {
138b5c49b62SMehrnoosh Heidarpour; CHECK-LABEL: @or_xor(
1390ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i8 0
140b5c49b62SMehrnoosh Heidarpour;
141b5c49b62SMehrnoosh Heidarpour  %or = or i8 %x, %y
142b5c49b62SMehrnoosh Heidarpour  %xor1 = xor i8 %or, %x
143b5c49b62SMehrnoosh Heidarpour  %xor2 = xor i8 %or, %y
144b5c49b62SMehrnoosh Heidarpour  %and = and i8 %xor1, %xor2
145b5c49b62SMehrnoosh Heidarpour  ret i8 %and
146b5c49b62SMehrnoosh Heidarpour}
147b5c49b62SMehrnoosh Heidarpour
1480ff20f2fSMehrnoosh Heidarpour; ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
149b5c49b62SMehrnoosh Heidarpour
150b5c49b62SMehrnoosh Heidarpourdefine i8 @or_xor_commute1(i8 %x, i8 %y) {
151b5c49b62SMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute1(
1520ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i8 0
153b5c49b62SMehrnoosh Heidarpour;
154b5c49b62SMehrnoosh Heidarpour  %or = or i8 %x, %y
155b5c49b62SMehrnoosh Heidarpour  %xor1 = xor i8 %or, %x
156b5c49b62SMehrnoosh Heidarpour  %xor2 = xor i8 %or, %y
157b5c49b62SMehrnoosh Heidarpour  %and = and i8 %xor2, %xor1
158b5c49b62SMehrnoosh Heidarpour  ret i8 %and
159b5c49b62SMehrnoosh Heidarpour}
160b5c49b62SMehrnoosh Heidarpour
1610ff20f2fSMehrnoosh Heidarpour; (X ^ (X | Y) ) & (Y ^ (X | Y)) --> 0
162b5c49b62SMehrnoosh Heidarpour
163b5c49b62SMehrnoosh Heidarpourdefine i71 @or_xor_commute2(i71 %x, i71 %y) {
164b5c49b62SMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute2(
1650ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i71 0
166b5c49b62SMehrnoosh Heidarpour;
167b5c49b62SMehrnoosh Heidarpour  %or = or i71 %x, %y
168b5c49b62SMehrnoosh Heidarpour  %xor1 = xor i71 %x, %or
169b5c49b62SMehrnoosh Heidarpour  %xor2 = xor i71 %y, %or
170b5c49b62SMehrnoosh Heidarpour  %and = and i71 %xor1, %xor2
171b5c49b62SMehrnoosh Heidarpour  ret i71 %and
172b5c49b62SMehrnoosh Heidarpour}
173b5c49b62SMehrnoosh Heidarpour
1740ff20f2fSMehrnoosh Heidarpour; (Y ^ (X | Y) ) & (X ^ (X | Y)) --> 0
175b5c49b62SMehrnoosh Heidarpour
176b5c49b62SMehrnoosh Heidarpourdefine <2 x i64> @or_xor_commute3(<2 x i64> %x, <2 x i64> %y) {
177b5c49b62SMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute3(
1780ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret <2 x i64> zeroinitializer
179b5c49b62SMehrnoosh Heidarpour;
180b5c49b62SMehrnoosh Heidarpour  %or = or <2 x i64> %x, %y
181b5c49b62SMehrnoosh Heidarpour  %xor1 = xor <2 x i64> %y, %or
182b5c49b62SMehrnoosh Heidarpour  %xor2 = xor <2 x i64> %x, %or
183b5c49b62SMehrnoosh Heidarpour  %and = and <2 x i64> %xor1, %xor2
184b5c49b62SMehrnoosh Heidarpour  ret <2 x i64> %and
185b5c49b62SMehrnoosh Heidarpour}
1860ff20f2fSMehrnoosh Heidarpour
1870ff20f2fSMehrnoosh Heidarpour; ((X | Y) ^ X ) & (Y ^ (X | Y)) --> 0
1880ff20f2fSMehrnoosh Heidarpour
1890ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_commute4(i32 %x, i32 %y) {
1900ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute4(
1910ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 0
1920ff20f2fSMehrnoosh Heidarpour;
1930ff20f2fSMehrnoosh Heidarpour  %or = or i32 %x, %y
1940ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %or, %x
1950ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %y, %or
1960ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
1970ff20f2fSMehrnoosh Heidarpour  ret i32 %and
1980ff20f2fSMehrnoosh Heidarpour}
1990ff20f2fSMehrnoosh Heidarpour
2000ff20f2fSMehrnoosh Heidarpour; ((X | Y) ^ Y ) & (X ^ (X | Y)) --> 0
2010ff20f2fSMehrnoosh Heidarpour
2020ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_commute5(i32 %x, i32 %y) {
2030ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute5(
2040ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 0
2050ff20f2fSMehrnoosh Heidarpour;
2060ff20f2fSMehrnoosh Heidarpour  %or = or i32 %x, %y
2070ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %or, %y
2080ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %x, %or
2090ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
2100ff20f2fSMehrnoosh Heidarpour  ret i32 %and
2110ff20f2fSMehrnoosh Heidarpour}
2120ff20f2fSMehrnoosh Heidarpour
2130ff20f2fSMehrnoosh Heidarpour; (X ^ (X | Y) ) & ((X | Y) ^ Y) --> 0
2140ff20f2fSMehrnoosh Heidarpour
2150ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_commute6(i32 %x, i32 %y) {
2160ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute6(
2170ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 0
2180ff20f2fSMehrnoosh Heidarpour;
2190ff20f2fSMehrnoosh Heidarpour  %or = or i32 %x, %y
2200ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %x, %or
2210ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %or, %y
2220ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
2230ff20f2fSMehrnoosh Heidarpour  ret i32 %and
2240ff20f2fSMehrnoosh Heidarpour}
2250ff20f2fSMehrnoosh Heidarpour
2260ff20f2fSMehrnoosh Heidarpour; (Y ^ (X | Y) ) & ((X | Y) ^ X) --> 0
2270ff20f2fSMehrnoosh Heidarpour
2280ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_commute7(i32 %x, i32 %y) {
2290ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_commute7(
2300ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 0
2310ff20f2fSMehrnoosh Heidarpour;
2320ff20f2fSMehrnoosh Heidarpour  %or = or i32 %x, %y
2330ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %y, %or
2340ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %or, %x
2350ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
2360ff20f2fSMehrnoosh Heidarpour  ret i32 %and
2370ff20f2fSMehrnoosh Heidarpour}
2380ff20f2fSMehrnoosh Heidarpour
2390ff20f2fSMehrnoosh Heidarpour; (Y ^ (X | Y) ) & ((X | Y) ^ X) --> 0
2400ff20f2fSMehrnoosh Heidarpour
2410ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_complex_op(i32 %x, i32 %in) {
2420ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_complex_op(
2430ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 0
2440ff20f2fSMehrnoosh Heidarpour;
2450ff20f2fSMehrnoosh Heidarpour  %y = or i32 %in, 1
2460ff20f2fSMehrnoosh Heidarpour  %or = or i32 %x, %y
2470ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %y, %or
2480ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %or, %x
2490ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
2500ff20f2fSMehrnoosh Heidarpour  ret i32 %and
2510ff20f2fSMehrnoosh Heidarpour}
2520ff20f2fSMehrnoosh Heidarpour
2530ff20f2fSMehrnoosh Heidarpourdefine i32 @or_xor_limitation(i32 %x, i32 %y) {
2540ff20f2fSMehrnoosh Heidarpour; CHECK-LABEL: @or_xor_limitation(
2550ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    [[OR1:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
2560ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    [[OR2:%.*]] = or i32 [[X]], [[Y]]
2570ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    [[XOR1:%.*]] = xor i32 [[Y]], [[OR1]]
2580ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    [[XOR2:%.*]] = xor i32 [[OR2]], [[X]]
2590ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    [[AND:%.*]] = and i32 [[XOR1]], [[XOR2]]
2600ff20f2fSMehrnoosh Heidarpour; CHECK-NEXT:    ret i32 [[AND]]
2610ff20f2fSMehrnoosh Heidarpour;
2620ff20f2fSMehrnoosh Heidarpour  %or1 = or i32 %y, %x
2630ff20f2fSMehrnoosh Heidarpour  %or2 = or i32 %x, %y
2640ff20f2fSMehrnoosh Heidarpour  %xor1 = xor i32 %y, %or1
2650ff20f2fSMehrnoosh Heidarpour  %xor2 = xor i32 %or2, %x
2660ff20f2fSMehrnoosh Heidarpour  %and = and i32 %xor1, %xor2
2670ff20f2fSMehrnoosh Heidarpour  ret i32 %and
2680ff20f2fSMehrnoosh Heidarpour}
269