blob: 3091521488978db2ad8983c06070499e149cd667 [file] [log] [blame]
Sanjay Patelac0edcb2018-01-11 22:48:07 +00001; RUN: opt < %s -instsimplify -S | FileCheck %s
2
Sanjay Patel6691e402018-01-12 22:16:26 +00003; In the next 16 tests (4 commutes * 2 (and/or) * 2 optional ptrtoint casts),
4; eliminate the simple (not) null check because that compare is implied by the
5; masked compare of the same operand.
Sanjay Patelac0edcb2018-01-11 22:48:07 +00006; Vary types between scalar and vector and weird for extra coverage.
7
8; or (icmp eq (and X, ?), 0), (icmp eq X, 0) --> icmp eq (and X, ?), 0
9
10define i1 @or_cmps_eq_zero_with_mask_commute1(i64 %x, i64 %y) {
11; CHECK-LABEL: @or_cmps_eq_zero_with_mask_commute1(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000012; CHECK-NEXT: [[SOMEBITS:%.*]] = and i64 %x, %y
13; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq i64 [[SOMEBITS]], 0
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000014; CHECK-NEXT: ret i1 [[SOMEBITS_ARE_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000015;
16 %isnull = icmp eq i64 %x, 0
17 %somebits = and i64 %x, %y
18 %somebits_are_zero = icmp eq i64 %somebits, 0
19 %r = or i1 %somebits_are_zero, %isnull
20 ret i1 %r
21}
22
23; or (icmp eq X, 0), (icmp eq (and X, ?), 0) --> icmp eq (and X, ?), 0
24
25define <2 x i1> @or_cmps_eq_zero_with_mask_commute2(<2 x i64> %x, <2 x i64> %y) {
26; CHECK-LABEL: @or_cmps_eq_zero_with_mask_commute2(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000027; CHECK-NEXT: [[SOMEBITS:%.*]] = and <2 x i64> %x, %y
28; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq <2 x i64> [[SOMEBITS]], zeroinitializer
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000029; CHECK-NEXT: ret <2 x i1> [[SOMEBITS_ARE_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000030;
31 %isnull = icmp eq <2 x i64> %x, zeroinitializer
32 %somebits = and <2 x i64> %x, %y
33 %somebits_are_zero = icmp eq <2 x i64> %somebits, zeroinitializer
34 %r = or <2 x i1> %isnull, %somebits_are_zero
35 ret <2 x i1> %r
36}
37
38; or (icmp eq (and ?, X), 0), (icmp eq X, 0) --> icmp eq (and ?, X), 0
39
40define i1 @or_cmps_eq_zero_with_mask_commute3(i4 %x, i4 %y) {
41; CHECK-LABEL: @or_cmps_eq_zero_with_mask_commute3(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000042; CHECK-NEXT: [[SOMEBITS:%.*]] = and i4 %y, %x
43; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq i4 [[SOMEBITS]], 0
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000044; CHECK-NEXT: ret i1 [[SOMEBITS_ARE_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000045;
46 %isnull = icmp eq i4 %x, 0
47 %somebits = and i4 %y, %x
48 %somebits_are_zero = icmp eq i4 %somebits, 0
49 %r = or i1 %somebits_are_zero, %isnull
50 ret i1 %r
51}
52
53; or (icmp eq X, 0), (icmp eq (and ?, X), 0) --> icmp eq (and ?, X), 0
54
55define <2 x i1> @or_cmps_eq_zero_with_mask_commute4(<2 x i4> %x, <2 x i4> %y) {
56; CHECK-LABEL: @or_cmps_eq_zero_with_mask_commute4(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000057; CHECK-NEXT: [[SOMEBITS:%.*]] = and <2 x i4> %y, %x
58; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq <2 x i4> [[SOMEBITS]], zeroinitializer
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000059; CHECK-NEXT: ret <2 x i1> [[SOMEBITS_ARE_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000060;
61 %isnull = icmp eq <2 x i4> %x, zeroinitializer
62 %somebits = and <2 x i4> %y, %x
63 %somebits_are_zero = icmp eq <2 x i4> %somebits, zeroinitializer
64 %r = or <2 x i1> %isnull, %somebits_are_zero
65 ret <2 x i1> %r
66}
67
68; and (icmp ne (and X, ?), 0), (icmp ne X, 0) --> icmp ne (and X, ?), 0
69
70define <3 x i1> @and_cmps_eq_zero_with_mask_commute1(<3 x i4> %x, <3 x i4> %y) {
71; CHECK-LABEL: @and_cmps_eq_zero_with_mask_commute1(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000072; CHECK-NEXT: [[SOMEBITS:%.*]] = and <3 x i4> %x, %y
73; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne <3 x i4> [[SOMEBITS]], zeroinitializer
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000074; CHECK-NEXT: ret <3 x i1> [[SOMEBITS_ARE_NOT_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000075;
76 %isnotnull = icmp ne <3 x i4> %x, zeroinitializer
77 %somebits = and <3 x i4> %x, %y
78 %somebits_are_not_zero = icmp ne <3 x i4> %somebits, zeroinitializer
79 %r = and <3 x i1> %somebits_are_not_zero, %isnotnull
80 ret <3 x i1> %r
81}
82
83; and (icmp ne X, 0), (icmp ne (and X, ?), 0) --> icmp ne (and X, ?), 0
84
85define i1 @and_cmps_eq_zero_with_mask_commute2(i4 %x, i4 %y) {
86; CHECK-LABEL: @and_cmps_eq_zero_with_mask_commute2(
Sanjay Patelac0edcb2018-01-11 22:48:07 +000087; CHECK-NEXT: [[SOMEBITS:%.*]] = and i4 %x, %y
88; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne i4 [[SOMEBITS]], 0
Sanjay Patel6ef6aa92018-01-11 23:27:37 +000089; CHECK-NEXT: ret i1 [[SOMEBITS_ARE_NOT_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +000090;
91 %isnotnull = icmp ne i4 %x, 0
92 %somebits = and i4 %x, %y
93 %somebits_are_not_zero = icmp ne i4 %somebits, 0
94 %r = and i1 %isnotnull, %somebits_are_not_zero
95 ret i1 %r
96}
97
98; and (icmp ne (and ?, X), 0), (icmp ne X, 0) --> icmp ne (and ?, X), 0
99
100define <3 x i1> @and_cmps_eq_zero_with_mask_commute3(<3 x i64> %x, <3 x i64> %y) {
101; CHECK-LABEL: @and_cmps_eq_zero_with_mask_commute3(
Sanjay Patelac0edcb2018-01-11 22:48:07 +0000102; CHECK-NEXT: [[SOMEBITS:%.*]] = and <3 x i64> %y, %x
103; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne <3 x i64> [[SOMEBITS]], zeroinitializer
Sanjay Patel6ef6aa92018-01-11 23:27:37 +0000104; CHECK-NEXT: ret <3 x i1> [[SOMEBITS_ARE_NOT_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +0000105;
106 %isnotnull = icmp ne <3 x i64> %x, zeroinitializer
107 %somebits = and <3 x i64> %y, %x
108 %somebits_are_not_zero = icmp ne <3 x i64> %somebits, zeroinitializer
109 %r = and <3 x i1> %somebits_are_not_zero, %isnotnull
110 ret <3 x i1> %r
111}
112
113; and (icmp ne X, 0), (icmp ne (and ?, X), 0) --> icmp ne (and ?, X), 0
114
115define i1 @and_cmps_eq_zero_with_mask_commute4(i64 %x, i64 %y) {
116; CHECK-LABEL: @and_cmps_eq_zero_with_mask_commute4(
Sanjay Patelac0edcb2018-01-11 22:48:07 +0000117; CHECK-NEXT: [[SOMEBITS:%.*]] = and i64 %y, %x
118; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne i64 [[SOMEBITS]], 0
Sanjay Patel6ef6aa92018-01-11 23:27:37 +0000119; CHECK-NEXT: ret i1 [[SOMEBITS_ARE_NOT_ZERO]]
Sanjay Patelac0edcb2018-01-11 22:48:07 +0000120;
121 %isnotnull = icmp ne i64 %x, 0
122 %somebits = and i64 %y, %x
123 %somebits_are_not_zero = icmp ne i64 %somebits, 0
124 %r = and i1 %isnotnull, %somebits_are_not_zero
125 ret i1 %r
126}
127
Sanjay Patel6691e402018-01-12 22:16:26 +0000128; or (icmp eq (and (ptrtoint P), ?), 0), (icmp eq P, 0) --> icmp eq (and (ptrtoint P), ?), 0
129
130define i1 @or_cmps_ptr_eq_zero_with_mask_commute1(i64* %p, i64 %y) {
131; CHECK-LABEL: @or_cmps_ptr_eq_zero_with_mask_commute1(
132; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i64* %p, null
133; CHECK-NEXT: [[X:%.*]] = ptrtoint i64* %p to i64
134; CHECK-NEXT: [[SOMEBITS:%.*]] = and i64 [[X]], %y
135; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq i64 [[SOMEBITS]], 0
136; CHECK-NEXT: [[R:%.*]] = or i1 [[SOMEBITS_ARE_ZERO]], [[ISNULL]]
137; CHECK-NEXT: ret i1 [[R]]
138;
139 %isnull = icmp eq i64* %p, null
140 %x = ptrtoint i64* %p to i64
141 %somebits = and i64 %x, %y
142 %somebits_are_zero = icmp eq i64 %somebits, 0
143 %r = or i1 %somebits_are_zero, %isnull
144 ret i1 %r
145}
146
147; or (icmp eq P, 0), (icmp eq (and (ptrtoint P), ?), 0) --> icmp eq (and (ptrtoint P), ?), 0
148
149define <2 x i1> @or_cmps_ptr_eq_zero_with_mask_commute2(<2 x i64*> %p, <2 x i64> %y) {
150; CHECK-LABEL: @or_cmps_ptr_eq_zero_with_mask_commute2(
151; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq <2 x i64*> %p, zeroinitializer
152; CHECK-NEXT: [[X:%.*]] = ptrtoint <2 x i64*> %p to <2 x i64>
153; CHECK-NEXT: [[SOMEBITS:%.*]] = and <2 x i64> [[X]], %y
154; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq <2 x i64> [[SOMEBITS]], zeroinitializer
155; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[ISNULL]], [[SOMEBITS_ARE_ZERO]]
156; CHECK-NEXT: ret <2 x i1> [[R]]
157;
158 %isnull = icmp eq <2 x i64*> %p, zeroinitializer
159 %x = ptrtoint <2 x i64*> %p to <2 x i64>
160 %somebits = and <2 x i64> %x, %y
161 %somebits_are_zero = icmp eq <2 x i64> %somebits, zeroinitializer
162 %r = or <2 x i1> %isnull, %somebits_are_zero
163 ret <2 x i1> %r
164}
165
166; or (icmp eq (and ?, (ptrtoint P)), 0), (icmp eq P, 0) --> icmp eq (and ?, (ptrtoint P)), 0
167
168define i1 @or_cmps_ptr_eq_zero_with_mask_commute3(i4* %p, i4 %y) {
169; CHECK-LABEL: @or_cmps_ptr_eq_zero_with_mask_commute3(
170; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i4* %p, null
171; CHECK-NEXT: [[X:%.*]] = ptrtoint i4* %p to i4
172; CHECK-NEXT: [[SOMEBITS:%.*]] = and i4 %y, [[X]]
173; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq i4 [[SOMEBITS]], 0
174; CHECK-NEXT: [[R:%.*]] = or i1 [[SOMEBITS_ARE_ZERO]], [[ISNULL]]
175; CHECK-NEXT: ret i1 [[R]]
176;
177 %isnull = icmp eq i4* %p, null
178 %x = ptrtoint i4* %p to i4
179 %somebits = and i4 %y, %x
180 %somebits_are_zero = icmp eq i4 %somebits, 0
181 %r = or i1 %somebits_are_zero, %isnull
182 ret i1 %r
183}
184
185; or (icmp eq P, 0), (icmp eq (and ?, (ptrtoint P)), 0) --> icmp eq (and ?, (ptrtoint P)), 0
186
187define <2 x i1> @or_cmps_ptr_eq_zero_with_mask_commute4(<2 x i4*> %p, <2 x i4> %y) {
188; CHECK-LABEL: @or_cmps_ptr_eq_zero_with_mask_commute4(
189; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq <2 x i4*> %p, zeroinitializer
190; CHECK-NEXT: [[X:%.*]] = ptrtoint <2 x i4*> %p to <2 x i4>
191; CHECK-NEXT: [[SOMEBITS:%.*]] = and <2 x i4> %y, [[X]]
192; CHECK-NEXT: [[SOMEBITS_ARE_ZERO:%.*]] = icmp eq <2 x i4> [[SOMEBITS]], zeroinitializer
193; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[ISNULL]], [[SOMEBITS_ARE_ZERO]]
194; CHECK-NEXT: ret <2 x i1> [[R]]
195;
196 %isnull = icmp eq <2 x i4*> %p, zeroinitializer
197 %x = ptrtoint <2 x i4*> %p to <2 x i4>
198 %somebits = and <2 x i4> %y, %x
199 %somebits_are_zero = icmp eq <2 x i4> %somebits, zeroinitializer
200 %r = or <2 x i1> %isnull, %somebits_are_zero
201 ret <2 x i1> %r
202}
203
204; and (icmp ne (and (ptrtoint P), ?), 0), (icmp ne P, 0) --> icmp ne (and (ptrtoint P), ?), 0
205
206define <3 x i1> @and_cmps_ptr_eq_zero_with_mask_commute1(<3 x i4*> %p, <3 x i4> %y) {
207; CHECK-LABEL: @and_cmps_ptr_eq_zero_with_mask_commute1(
208; CHECK-NEXT: [[ISNOTNULL:%.*]] = icmp ne <3 x i4*> %p, zeroinitializer
209; CHECK-NEXT: [[X:%.*]] = ptrtoint <3 x i4*> %p to <3 x i4>
210; CHECK-NEXT: [[SOMEBITS:%.*]] = and <3 x i4> [[X]], %y
211; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne <3 x i4> [[SOMEBITS]], zeroinitializer
212; CHECK-NEXT: [[R:%.*]] = and <3 x i1> [[SOMEBITS_ARE_NOT_ZERO]], [[ISNOTNULL]]
213; CHECK-NEXT: ret <3 x i1> [[R]]
214;
215 %isnotnull = icmp ne <3 x i4*> %p, zeroinitializer
216 %x = ptrtoint <3 x i4*> %p to <3 x i4>
217 %somebits = and <3 x i4> %x, %y
218 %somebits_are_not_zero = icmp ne <3 x i4> %somebits, zeroinitializer
219 %r = and <3 x i1> %somebits_are_not_zero, %isnotnull
220 ret <3 x i1> %r
221}
222
223; and (icmp ne P, 0), (icmp ne (and (ptrtoint P), ?), 0) --> icmp ne (and (ptrtoint P), ?), 0
224
225define i1 @and_cmps_ptr_eq_zero_with_mask_commute2(i4* %p, i4 %y) {
226; CHECK-LABEL: @and_cmps_ptr_eq_zero_with_mask_commute2(
227; CHECK-NEXT: [[ISNOTNULL:%.*]] = icmp ne i4* %p, null
228; CHECK-NEXT: [[X:%.*]] = ptrtoint i4* %p to i4
229; CHECK-NEXT: [[SOMEBITS:%.*]] = and i4 [[X]], %y
230; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne i4 [[SOMEBITS]], 0
231; CHECK-NEXT: [[R:%.*]] = and i1 [[ISNOTNULL]], [[SOMEBITS_ARE_NOT_ZERO]]
232; CHECK-NEXT: ret i1 [[R]]
233;
234 %isnotnull = icmp ne i4* %p, null
235 %x = ptrtoint i4* %p to i4
236 %somebits = and i4 %x, %y
237 %somebits_are_not_zero = icmp ne i4 %somebits, 0
238 %r = and i1 %isnotnull, %somebits_are_not_zero
239 ret i1 %r
240}
241
242; and (icmp ne (and ?, (ptrtoint P)), 0), (icmp ne P, 0) --> icmp ne (and ?, (ptrtoint P)), 0
243
244define <3 x i1> @and_cmps_ptr_eq_zero_with_mask_commute3(<3 x i64*> %p, <3 x i64> %y) {
245; CHECK-LABEL: @and_cmps_ptr_eq_zero_with_mask_commute3(
246; CHECK-NEXT: [[ISNOTNULL:%.*]] = icmp ne <3 x i64*> %p, zeroinitializer
247; CHECK-NEXT: [[X:%.*]] = ptrtoint <3 x i64*> %p to <3 x i64>
248; CHECK-NEXT: [[SOMEBITS:%.*]] = and <3 x i64> %y, [[X]]
249; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne <3 x i64> [[SOMEBITS]], zeroinitializer
250; CHECK-NEXT: [[R:%.*]] = and <3 x i1> [[SOMEBITS_ARE_NOT_ZERO]], [[ISNOTNULL]]
251; CHECK-NEXT: ret <3 x i1> [[R]]
252;
253 %isnotnull = icmp ne <3 x i64*> %p, zeroinitializer
254 %x = ptrtoint <3 x i64*> %p to <3 x i64>
255 %somebits = and <3 x i64> %y, %x
256 %somebits_are_not_zero = icmp ne <3 x i64> %somebits, zeroinitializer
257 %r = and <3 x i1> %somebits_are_not_zero, %isnotnull
258 ret <3 x i1> %r
259}
260
261; and (icmp ne P, 0), (icmp ne (and ?, (ptrtoint P)), 0) --> icmp ne (and ?, (ptrtoint P)), 0
262
263define i1 @and_cmps_ptr_eq_zero_with_mask_commute4(i64* %p, i64 %y) {
264; CHECK-LABEL: @and_cmps_ptr_eq_zero_with_mask_commute4(
265; CHECK-NEXT: [[ISNOTNULL:%.*]] = icmp ne i64* %p, null
266; CHECK-NEXT: [[X:%.*]] = ptrtoint i64* %p to i64
267; CHECK-NEXT: [[SOMEBITS:%.*]] = and i64 %y, [[X]]
268; CHECK-NEXT: [[SOMEBITS_ARE_NOT_ZERO:%.*]] = icmp ne i64 [[SOMEBITS]], 0
269; CHECK-NEXT: [[R:%.*]] = and i1 [[ISNOTNULL]], [[SOMEBITS_ARE_NOT_ZERO]]
270; CHECK-NEXT: ret i1 [[R]]
271;
272 %isnotnull = icmp ne i64* %p, null
273 %x = ptrtoint i64* %p to i64
274 %somebits = and i64 %y, %x
275 %somebits_are_not_zero = icmp ne i64 %somebits, 0
276 %r = and i1 %isnotnull, %somebits_are_not_zero
277 ret i1 %r
278}
279