1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3
4define i1 @logical_and_of_or_commute0(i1 %x, i1 %y) {
5; CHECK-LABEL: @logical_and_of_or_commute0(
6; CHECK-NEXT:    ret i1 [[X:%.*]]
7;
8  %ynot = xor i1 %y, -1
9  %xory = select i1 %x, i1 true, i1 %y
10  %xorynot = select i1 %x, i1 true, i1 %ynot
11  %and = select i1 %xory, i1 %xorynot, i1 false
12  ret i1 %and
13}
14
15define <2 x i1> @logical_and_of_or_commute1(<2 x i1> %x, <2 x i1> %y) {
16; CHECK-LABEL: @logical_and_of_or_commute1(
17; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
18;
19  %ynot = xor <2 x i1> %y, <i1 -1, i1 poison>
20  %xory = select <2 x i1> %y, <2 x i1> <i1 true, i1 true>, <2 x i1> %x
21  %xorynot = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %ynot
22  %and = select <2 x i1> %xory, <2 x i1> %xorynot, <2 x i1> zeroinitializer
23  ret <2 x i1> %and
24}
25
26define i1 @logical_and_of_or_commute2(i1 %x, i1 %y) {
27; CHECK-LABEL: @logical_and_of_or_commute2(
28; CHECK-NEXT:    ret i1 [[X:%.*]]
29;
30  %ynot = xor i1 %y, -1
31  %xory = select i1 %x, i1 true, i1 %y
32  %xorynot = select i1 %ynot, i1 true, i1 %x
33  %and = select i1 %xory, i1 %xorynot, i1 false
34  ret i1 %and
35}
36
37define i1 @logical_and_of_or_commute3(i1 %x, i1 %y) {
38; CHECK-LABEL: @logical_and_of_or_commute3(
39; CHECK-NEXT:    ret i1 [[X:%.*]]
40;
41  %ynot = xor i1 %y, -1
42  %xory = select i1 %y, i1 true, i1 %x
43  %xorynot = select i1 %ynot, i1 true, i1 %x
44  %and = select i1 %xory, i1 %xorynot, i1 false
45  ret i1 %and
46}
47
48define i1 @logical_and_of_or_commute4(i1 %x, i1 %y) {
49; CHECK-LABEL: @logical_and_of_or_commute4(
50; CHECK-NEXT:    ret i1 [[X:%.*]]
51;
52  %ynot = xor i1 %y, -1
53  %xory = select i1 %x, i1 true, i1 %y
54  %xorynot = select i1 %x, i1 true, i1 %ynot
55  %and = select i1 %xorynot, i1 %xory, i1 false
56  ret i1 %and
57}
58
59define i1 @logical_and_of_or_commute5(i1 %x, i1 %y) {
60; CHECK-LABEL: @logical_and_of_or_commute5(
61; CHECK-NEXT:    ret i1 [[X:%.*]]
62;
63  %ynot = xor i1 %y, -1
64  %xory = select i1 %y, i1 true, i1 %x
65  %xorynot = select i1 %x, i1 true, i1 %ynot
66  %and = select i1 %xorynot, i1 %xory, i1 false
67  ret i1 %and
68}
69
70define i1 @logical_and_of_or_commute6(i1 %x, i1 %y) {
71; CHECK-LABEL: @logical_and_of_or_commute6(
72; CHECK-NEXT:    ret i1 [[X:%.*]]
73;
74  %ynot = xor i1 %y, -1
75  %xory = select i1 %x, i1 true, i1 %y
76  %xorynot = select i1 %ynot, i1 true, i1 %x
77  %and = select i1 %xorynot, i1 %xory, i1 false
78  ret i1 %and
79}
80
81define i1 @logical_and_of_or_commute7(i1 %x, i1 %y) {
82; CHECK-LABEL: @logical_and_of_or_commute7(
83; CHECK-NEXT:    ret i1 [[X:%.*]]
84;
85  %ynot = xor i1 %y, -1
86  %xory = select i1 %y, i1 true, i1 %x
87  %xorynot = select i1 %ynot, i1 true, i1 %x
88  %and = select i1 %xorynot, i1 %xory, i1 false
89  ret i1 %and
90}
91
92; negative test - wrong logic op
93
94define i1 @logical_and_of_or_and(i1 %x, i1 %y) {
95; CHECK-LABEL: @logical_and_of_or_and(
96; CHECK-NEXT:    [[XANDY:%.*]] = select i1 [[Y:%.*]], i1 [[X:%.*]], i1 false
97; CHECK-NEXT:    [[YNOT:%.*]] = xor i1 [[Y]], true
98; CHECK-NEXT:    [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
99; CHECK-NEXT:    [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XANDY]], i1 false
100; CHECK-NEXT:    ret i1 [[AND]]
101;
102  %xandy = select i1 %y, i1 %x, i1 false
103  %ynot = xor i1 %y, -1
104  %xorynot = select i1 %ynot, i1 true, i1 %x
105  %and = select i1 %xorynot, i1 %xandy, i1 false
106  ret i1 %and
107}
108
109; negative test - must have common operands
110
111define i1 @logical_and_of_or_no_common_op(i1 %x, i1 %y, i1 %z) {
112; CHECK-LABEL: @logical_and_of_or_no_common_op(
113; CHECK-NEXT:    [[XORZ:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Z:%.*]]
114; CHECK-NEXT:    [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
115; CHECK-NEXT:    [[XORYNOT:%.*]] = select i1 [[X]], i1 true, i1 [[YNOT]]
116; CHECK-NEXT:    [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORZ]], i1 false
117; CHECK-NEXT:    ret i1 [[AND]]
118;
119  %xorz = select i1 %x, i1 true, i1 %z
120  %ynot = xor i1 %y, -1
121  %xorynot = select i1 %x, i1 true, i1 %ynot
122  %and = select i1 %xorynot, i1 %xorz, i1 false
123  ret i1 %and
124}
125