1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 declare void @use_vec(<2 x i8>)
7 ; ((b | a) & C1) | (b & C2) -> (a & C1) | b iff C1 == ~C2
9 define i32 @or_and_not_constant_commute0(i32 %a, i32 %b) {
10 ; CHECK-LABEL: @or_and_not_constant_commute0(
11 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], 1
12 ; CHECK-NEXT: [[T3:%.*]] = or i32 [[TMP1]], [[B:%.*]]
13 ; CHECK-NEXT: ret i32 [[T3]]
22 define i32 @or_and_not_constant_commute1(i32 %a, i32 %b) {
23 ; CHECK-LABEL: @or_and_not_constant_commute1(
24 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], 1
25 ; CHECK-NEXT: [[T3:%.*]] = or i32 [[TMP1]], [[B:%.*]]
26 ; CHECK-NEXT: ret i32 [[T3]]
35 define i32 @or_and_not_constant_commute2(i32 %a, i32 %b) {
36 ; CHECK-LABEL: @or_and_not_constant_commute2(
37 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], 1
38 ; CHECK-NEXT: [[T3:%.*]] = or i32 [[TMP1]], [[B:%.*]]
39 ; CHECK-NEXT: ret i32 [[T3]]
48 define i32 @or_and_not_constant_commute3(i32 %a, i32 %b) {
49 ; CHECK-LABEL: @or_and_not_constant_commute3(
50 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], 1
51 ; CHECK-NEXT: [[T3:%.*]] = or i32 [[TMP1]], [[B:%.*]]
52 ; CHECK-NEXT: ret i32 [[T3]]
61 define <2 x i7> @or_and_not_constant_commute0_splat(<2 x i7> %a, <2 x i7> %b) {
62 ; CHECK-LABEL: @or_and_not_constant_commute0_splat(
63 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i7> [[A:%.*]], <i7 42, i7 42>
64 ; CHECK-NEXT: [[T3:%.*]] = or <2 x i7> [[TMP1]], [[B:%.*]]
65 ; CHECK-NEXT: ret <2 x i7> [[T3]]
67 %t = or <2 x i7> %b, %a
68 %t1 = and <2 x i7> %t, <i7 42, i7 42>
69 %t2 = and <2 x i7> %b, <i7 -43, i7 -43>
70 %t3 = or <2 x i7> %t1, %t2
74 ; ((x | N) & C1) | (x & C2) --> (x | N) & (C1 | C2)
75 ; iff (C1 & C2) == 0 and (N & ~C1) == 0
77 define i8 @or_and_or_commute0(i8 %x) {
78 ; CHECK-LABEL: @or_and_or_commute0(
79 ; CHECK-NEXT: [[XN:%.*]] = or i8 [[X:%.*]], 16
80 ; CHECK-NEXT: call void @use(i8 [[XN]])
81 ; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], 59
82 ; CHECK-NEXT: call void @use(i8 [[X1]])
83 ; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64
84 ; CHECK-NEXT: call void @use(i8 [[X2]])
85 ; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], 123
86 ; CHECK-NEXT: ret i8 [[R]]
88 %xn = or i8 %x, 16 ; 0001_0000
89 call void @use(i8 %xn)
90 %x1 = and i8 %xn, 59 ; 0011_1011
91 call void @use(i8 %x1)
92 %x2 = and i8 %x, 64 ; 0100_0000
93 call void @use(i8 %x2)
98 define i8 @or_and_or_commute1(i8 %x) {
99 ; CHECK-LABEL: @or_and_or_commute1(
100 ; CHECK-NEXT: [[XN:%.*]] = or i8 [[X:%.*]], 16
101 ; CHECK-NEXT: call void @use(i8 [[XN]])
102 ; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], 59
103 ; CHECK-NEXT: call void @use(i8 [[X1]])
104 ; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64
105 ; CHECK-NEXT: call void @use(i8 [[X2]])
106 ; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], 123
107 ; CHECK-NEXT: ret i8 [[R]]
109 %xn = or i8 %x, 16 ; 0001_0000
110 call void @use(i8 %xn)
111 %x1 = and i8 %xn, 59 ; 0011_1011
112 call void @use(i8 %x1)
113 %x2 = and i8 %x, 64 ; 0100_0000
114 call void @use(i8 %x2)
119 define <2 x i8> @or_and_or_commute1_splat(<2 x i8> %x) {
120 ; CHECK-LABEL: @or_and_or_commute1_splat(
121 ; CHECK-NEXT: [[XN:%.*]] = or <2 x i8> [[X:%.*]], <i8 16, i8 16>
122 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[XN]])
123 ; CHECK-NEXT: [[X1:%.*]] = and <2 x i8> [[XN]], <i8 59, i8 59>
124 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X1]])
125 ; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[X]], <i8 64, i8 64>
126 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]])
127 ; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[XN]], <i8 123, i8 123>
128 ; CHECK-NEXT: ret <2 x i8> [[R]]
130 %xn = or <2 x i8> %x, <i8 16, i8 16>
131 call void @use_vec(<2 x i8> %xn)
132 %x1 = and <2 x i8> %xn, <i8 59, i8 59>
133 call void @use_vec(<2 x i8> %x1)
134 %x2 = and <2 x i8> %x, <i8 64, i8 64>
135 call void @use_vec(<2 x i8> %x2)
136 %r = or <2 x i8> %x2, %x1
140 define i8 @or_and_or_commute2(i8 %x, i8 %y) {
141 ; CHECK-LABEL: @or_and_or_commute2(
142 ; CHECK-NEXT: [[N:%.*]] = lshr i8 [[Y:%.*]], 6
143 ; CHECK-NEXT: [[XN:%.*]] = or i8 [[N]], [[X:%.*]]
144 ; CHECK-NEXT: call void @use(i8 [[XN]])
145 ; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], -69
146 ; CHECK-NEXT: call void @use(i8 [[X1]])
147 ; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64
148 ; CHECK-NEXT: call void @use(i8 [[X2]])
149 ; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], -5
150 ; CHECK-NEXT: ret i8 [[R]]
154 call void @use(i8 %xn)
155 %x1 = and i8 %xn, 187
156 call void @use(i8 %x1)
158 call void @use(i8 %x2)
163 define <2 x i8> @or_and_or_commute2_splat(<2 x i8> %x, <2 x i8> %y) {
164 ; CHECK-LABEL: @or_and_or_commute2_splat(
165 ; CHECK-NEXT: [[N:%.*]] = lshr <2 x i8> [[Y:%.*]], <i8 6, i8 6>
166 ; CHECK-NEXT: [[XN:%.*]] = or <2 x i8> [[N]], [[X:%.*]]
167 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[XN]])
168 ; CHECK-NEXT: [[X1:%.*]] = and <2 x i8> [[XN]], <i8 -69, i8 -69>
169 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X1]])
170 ; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[X]], <i8 64, i8 64>
171 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]])
172 ; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[XN]], <i8 -5, i8 -5>
173 ; CHECK-NEXT: ret <2 x i8> [[R]]
175 %n = lshr <2 x i8> %y, <i8 6, i8 6>
176 %xn = or <2 x i8> %n, %x
177 call void @use_vec(<2 x i8> %xn)
178 %x1 = and <2 x i8> %xn, <i8 187, i8 187>
179 call void @use_vec(<2 x i8> %x1)
180 %x2 = and <2 x i8> %x, <i8 64, i8 64>
181 call void @use_vec(<2 x i8> %x2)
182 %r = or <2 x i8> %x1, %x2
186 define i8 @or_and_or_commute3(i8 %x, i8 %y) {
187 ; CHECK-LABEL: @or_and_or_commute3(
188 ; CHECK-NEXT: [[N:%.*]] = lshr i8 [[Y:%.*]], 6
189 ; CHECK-NEXT: [[XN:%.*]] = or i8 [[N]], [[X:%.*]]
190 ; CHECK-NEXT: call void @use(i8 [[XN]])
191 ; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], -69
192 ; CHECK-NEXT: call void @use(i8 [[X1]])
193 ; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64
194 ; CHECK-NEXT: call void @use(i8 [[X2]])
195 ; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], -5
196 ; CHECK-NEXT: ret i8 [[R]]
200 call void @use(i8 %xn)
201 %x1 = and i8 %xn, 187
202 call void @use(i8 %x1)
204 call void @use(i8 %x2)
209 define i8 @or_and2_or2(i8 %x) {
210 ; CHECK-LABEL: @or_and2_or2(
211 ; CHECK-NEXT: [[O1:%.*]] = or i8 [[X:%.*]], 1
212 ; CHECK-NEXT: call void @use(i8 [[O1]])
213 ; CHECK-NEXT: [[O2:%.*]] = or i8 [[X]], 2
214 ; CHECK-NEXT: call void @use(i8 [[O2]])
215 ; CHECK-NEXT: [[X1:%.*]] = and i8 [[O1]], -71
216 ; CHECK-NEXT: call void @use(i8 [[X1]])
217 ; CHECK-NEXT: [[X2:%.*]] = and i8 [[O2]], 66
218 ; CHECK-NEXT: call void @use(i8 [[X2]])
219 ; CHECK-NEXT: [[BITFIELD:%.*]] = and i8 [[X]], -8
220 ; CHECK-NEXT: [[R:%.*]] = or i8 [[BITFIELD]], 3
221 ; CHECK-NEXT: ret i8 [[R]]
224 call void @use(i8 %o1)
226 call void @use(i8 %o2)
227 %x1 = and i8 %o1, 185
228 call void @use(i8 %x1)
230 call void @use(i8 %x2)
235 define <2 x i8> @or_and2_or2_splat(<2 x i8> %x) {
236 ; CHECK-LABEL: @or_and2_or2_splat(
237 ; CHECK-NEXT: [[O1:%.*]] = or <2 x i8> [[X:%.*]], <i8 1, i8 1>
238 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[O1]])
239 ; CHECK-NEXT: [[O2:%.*]] = or <2 x i8> [[X]], <i8 2, i8 2>
240 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[O2]])
241 ; CHECK-NEXT: [[X1:%.*]] = and <2 x i8> [[O1]], <i8 -71, i8 -71>
242 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X1]])
243 ; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[O2]], <i8 66, i8 66>
244 ; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]])
245 ; CHECK-NEXT: [[BITFIELD:%.*]] = and <2 x i8> [[X]], <i8 -8, i8 -8>
246 ; CHECK-NEXT: [[R:%.*]] = or <2 x i8> [[BITFIELD]], <i8 3, i8 3>
247 ; CHECK-NEXT: ret <2 x i8> [[R]]
249 %o1 = or <2 x i8> %x, <i8 1, i8 1>
250 call void @use_vec(<2 x i8> %o1)
251 %o2 = or <2 x i8> %x, <i8 2, i8 2>
252 call void @use_vec(<2 x i8> %o2)
253 %x1 = and <2 x i8> %o1, <i8 185, i8 185>
254 call void @use_vec(<2 x i8> %x1)
255 %x2 = and <2 x i8> %o2, <i8 66, i8 66>
256 call void @use_vec(<2 x i8> %x2)
257 %r = or <2 x i8> %x1, %x2
262 ; and ({x}or X, Y), C --> {x}or X, (and Y, C)
263 ; ...in the following 5 tests.
265 define i8 @and_or_hoist_mask(i8 %a, i8 %b) {
266 ; CHECK-LABEL: @and_or_hoist_mask(
267 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 [[A:%.*]], 6
268 ; CHECK-NEXT: [[B_MASKED:%.*]] = and i8 [[B:%.*]], 3
269 ; CHECK-NEXT: [[AND:%.*]] = or i8 [[SH]], [[B_MASKED]]
270 ; CHECK-NEXT: ret i8 [[AND]]
278 define <2 x i8> @and_xor_hoist_mask_vec_splat(<2 x i8> %a, <2 x i8> %b) {
279 ; CHECK-LABEL: @and_xor_hoist_mask_vec_splat(
280 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i8> [[A:%.*]], <i8 6, i8 6>
281 ; CHECK-NEXT: [[B_MASKED:%.*]] = and <2 x i8> [[B:%.*]], <i8 3, i8 3>
282 ; CHECK-NEXT: [[AND:%.*]] = xor <2 x i8> [[SH]], [[B_MASKED]]
283 ; CHECK-NEXT: ret <2 x i8> [[AND]]
285 %sh = lshr <2 x i8> %a, <i8 6, i8 6>
286 %xor = xor <2 x i8> %sh, %b
287 %and = and <2 x i8> %xor, <i8 3, i8 3>
291 define i8 @and_xor_hoist_mask_commute(i8 %a, i8 %b) {
292 ; CHECK-LABEL: @and_xor_hoist_mask_commute(
293 ; CHECK-NEXT: [[C:%.*]] = mul i8 [[B:%.*]], 43
294 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 [[A:%.*]], 6
295 ; CHECK-NEXT: [[C_MASKED:%.*]] = and i8 [[C]], 3
296 ; CHECK-NEXT: [[AND:%.*]] = xor i8 [[C_MASKED]], [[SH]]
297 ; CHECK-NEXT: ret i8 [[AND]]
299 %c = mul i8 %b, 43 ; thwart complexity-based ordering
301 %xor = xor i8 %c, %sh
302 %and = and i8 %xor, 3
306 define <2 x i8> @and_or_hoist_mask_commute_vec_splat(<2 x i8> %a, <2 x i8> %b) {
307 ; CHECK-LABEL: @and_or_hoist_mask_commute_vec_splat(
308 ; CHECK-NEXT: [[C:%.*]] = mul <2 x i8> [[B:%.*]], <i8 43, i8 43>
309 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i8> [[A:%.*]], <i8 6, i8 6>
310 ; CHECK-NEXT: [[C_MASKED:%.*]] = and <2 x i8> [[C]], <i8 3, i8 3>
311 ; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[C_MASKED]], [[SH]]
312 ; CHECK-NEXT: ret <2 x i8> [[AND]]
314 %c = mul <2 x i8> %b, <i8 43, i8 43> ; thwart complexity-based ordering
315 %sh = lshr <2 x i8> %a, <i8 6, i8 6>
316 %or = or <2 x i8> %c, %sh
317 %and = and <2 x i8> %or, <i8 3, i8 3>
321 ; Don't transform if the 'or' has multiple uses because that would increase instruction count.
323 define i8 @and_or_do_not_hoist_mask(i8 %a, i8 %b) {
324 ; CHECK-LABEL: @and_or_do_not_hoist_mask(
325 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 [[A:%.*]], 6
326 ; CHECK-NEXT: [[OR:%.*]] = or i8 [[SH]], [[B:%.*]]
327 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[OR]], 3
328 ; CHECK-NEXT: [[EXTRA_USE_OF_OR:%.*]] = mul i8 [[OR]], [[AND]]
329 ; CHECK-NEXT: ret i8 [[EXTRA_USE_OF_OR]]
334 %extra_use_of_or = mul i8 %or, %and
335 ret i8 %extra_use_of_or