blob: 3526aab68a34f5a606767d36cae32ffdba0690a5 [file] [log] [blame]
Sanjay Patelbcc5a742018-11-13 16:47:16 +00001; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4declare i32 @llvm.fshl.i32(i32, i32, i32)
5declare i33 @llvm.fshr.i33(i33, i33, i33)
6declare <2 x i32> @llvm.fshr.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
7declare <2 x i31> @llvm.fshl.v2i31(<2 x i31>, <2 x i31>, <2 x i31>)
8
9; If the shift mask doesn't include any demanded bits, the funnel shift can be eliminated.
10
11define i32 @fshl_mask_simplify1(i32 %x, i32 %y, i32 %sh) {
12; CHECK-LABEL: @fshl_mask_simplify1(
Sanjay Patela1395642018-11-13 23:27:23 +000013; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patelbcc5a742018-11-13 16:47:16 +000014;
15 %maskedsh = and i32 %sh, 32
16 %r = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %maskedsh)
17 ret i32 %r
18}
19
20define <2 x i32> @fshr_mask_simplify2(<2 x i32> %x, <2 x i32> %y, <2 x i32> %sh) {
21; CHECK-LABEL: @fshr_mask_simplify2(
Sanjay Patela1395642018-11-13 23:27:23 +000022; CHECK-NEXT: ret <2 x i32> [[Y:%.*]]
Sanjay Patelbcc5a742018-11-13 16:47:16 +000023;
24 %maskedsh = and <2 x i32> %sh, <i32 64, i32 64>
25 %r = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i32> %maskedsh)
26 ret <2 x i32> %r
27}
28
29; Negative test.
30
31define i32 @fshl_mask_simplify3(i32 %x, i32 %y, i32 %sh) {
32; CHECK-LABEL: @fshl_mask_simplify3(
33; CHECK-NEXT: [[MASKEDSH:%.*]] = and i32 [[SH:%.*]], 16
34; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[MASKEDSH]])
35; CHECK-NEXT: ret i32 [[R]]
36;
37 %maskedsh = and i32 %sh, 16
38 %r = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %maskedsh)
39 ret i32 %r
40}
41
Sanjay Patela1395642018-11-13 23:27:23 +000042; Check again with weird bitwidths - the analysis is invalid with non-power-of-2.
Sanjay Patelbcc5a742018-11-13 16:47:16 +000043
44define i33 @fshr_mask_simplify1(i33 %x, i33 %y, i33 %sh) {
45; CHECK-LABEL: @fshr_mask_simplify1(
46; CHECK-NEXT: [[MASKEDSH:%.*]] = and i33 [[SH:%.*]], 64
47; CHECK-NEXT: [[R:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 [[Y:%.*]], i33 [[MASKEDSH]])
48; CHECK-NEXT: ret i33 [[R]]
49;
50 %maskedsh = and i33 %sh, 64
51 %r = call i33 @llvm.fshr.i33(i33 %x, i33 %y, i33 %maskedsh)
52 ret i33 %r
53}
54
Sanjay Patela1395642018-11-13 23:27:23 +000055; Check again with weird bitwidths - the analysis is invalid with non-power-of-2.
Sanjay Patelbcc5a742018-11-13 16:47:16 +000056
57define <2 x i31> @fshl_mask_simplify2(<2 x i31> %x, <2 x i31> %y, <2 x i31> %sh) {
58; CHECK-LABEL: @fshl_mask_simplify2(
59; CHECK-NEXT: [[MASKEDSH:%.*]] = and <2 x i31> [[SH:%.*]], <i31 32, i31 32>
60; CHECK-NEXT: [[R:%.*]] = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> [[X:%.*]], <2 x i31> [[Y:%.*]], <2 x i31> [[MASKEDSH]])
61; CHECK-NEXT: ret <2 x i31> [[R]]
62;
63 %maskedsh = and <2 x i31> %sh, <i31 32, i31 32>
64 %r = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> %x, <2 x i31> %y, <2 x i31> %maskedsh)
65 ret <2 x i31> %r
66}
67
Sanjay Patela1395642018-11-13 23:27:23 +000068; Check again with weird bitwidths - the analysis is invalid with non-power-of-2.
Sanjay Patelbcc5a742018-11-13 16:47:16 +000069
70define i33 @fshr_mask_simplify3(i33 %x, i33 %y, i33 %sh) {
71; CHECK-LABEL: @fshr_mask_simplify3(
72; CHECK-NEXT: [[MASKEDSH:%.*]] = and i33 [[SH:%.*]], 32
73; CHECK-NEXT: [[R:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 [[Y:%.*]], i33 [[MASKEDSH]])
74; CHECK-NEXT: ret i33 [[R]]
75;
76 %maskedsh = and i33 %sh, 32
77 %r = call i33 @llvm.fshr.i33(i33 %x, i33 %y, i33 %maskedsh)
78 ret i33 %r
79}
80
81; This mask op is unnecessary.
82
83define i32 @fshl_mask_not_required(i32 %x, i32 %y, i32 %sh) {
84; CHECK-LABEL: @fshl_mask_not_required(
Sanjay Patela1395642018-11-13 23:27:23 +000085; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[SH:%.*]])
Sanjay Patelbcc5a742018-11-13 16:47:16 +000086; CHECK-NEXT: ret i32 [[R]]
87;
88 %maskedsh = and i32 %sh, 31
89 %r = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %maskedsh)
90 ret i32 %r
91}
92
93; This mask op can be reduced.
94
95define i32 @fshl_mask_reduce_constant(i32 %x, i32 %y, i32 %sh) {
96; CHECK-LABEL: @fshl_mask_reduce_constant(
Sanjay Patela1395642018-11-13 23:27:23 +000097; CHECK-NEXT: [[MASKEDSH:%.*]] = and i32 [[SH:%.*]], 1
Sanjay Patelbcc5a742018-11-13 16:47:16 +000098; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[MASKEDSH]])
99; CHECK-NEXT: ret i32 [[R]]
100;
101 %maskedsh = and i32 %sh, 33
102 %r = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %maskedsh)
103 ret i32 %r
104}
105
106; But this mask op is required.
107
108define i32 @fshl_mask_negative(i32 %x, i32 %y, i32 %sh) {
109; CHECK-LABEL: @fshl_mask_negative(
110; CHECK-NEXT: [[MASKEDSH:%.*]] = and i32 [[SH:%.*]], 15
111; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[MASKEDSH]])
112; CHECK-NEXT: ret i32 [[R]]
113;
114 %maskedsh = and i32 %sh, 15
115 %r = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %maskedsh)
116 ret i32 %r
117}
118
119; The transform is not limited to mask ops.
120
121define <2 x i32> @fshr_set_but_not_demanded_vec(<2 x i32> %x, <2 x i32> %y, <2 x i32> %sh) {
122; CHECK-LABEL: @fshr_set_but_not_demanded_vec(
Sanjay Patela1395642018-11-13 23:27:23 +0000123; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]], <2 x i32> [[SH:%.*]])
Sanjay Patelbcc5a742018-11-13 16:47:16 +0000124; CHECK-NEXT: ret <2 x i32> [[R]]
125;
126 %bogusbits = or <2 x i32> %sh, <i32 32, i32 32>
127 %r = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i32> %bogusbits)
128 ret <2 x i32> %r
129}
130
Sanjay Patela1395642018-11-13 23:27:23 +0000131; Check again with weird bitwidths - the analysis is invalid with non-power-of-2.
132
Sanjay Patelbcc5a742018-11-13 16:47:16 +0000133define <2 x i31> @fshl_set_but_not_demanded_vec(<2 x i31> %x, <2 x i31> %y, <2 x i31> %sh) {
134; CHECK-LABEL: @fshl_set_but_not_demanded_vec(
135; CHECK-NEXT: [[BOGUSBITS:%.*]] = or <2 x i31> [[SH:%.*]], <i31 32, i31 32>
136; CHECK-NEXT: [[R:%.*]] = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> [[X:%.*]], <2 x i31> [[Y:%.*]], <2 x i31> [[BOGUSBITS]])
137; CHECK-NEXT: ret <2 x i31> [[R]]
138;
139 %bogusbits = or <2 x i31> %sh, <i31 32, i31 32>
140 %r = call <2 x i31> @llvm.fshl.v2i31(<2 x i31> %x, <2 x i31> %y, <2 x i31> %bogusbits)
141 ret <2 x i31> %r
142}
143
Sanjay Patel96152dc2018-11-20 17:51:49 +0000144; Simplify one undef operand and constant shift amount.
145
146define i32 @fshl_op0_undef(i32 %x) {
147; CHECK-LABEL: @fshl_op0_undef(
148; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 undef, i32 [[X:%.*]], i32 7)
149; CHECK-NEXT: ret i32 [[R]]
150;
151 %r = call i32 @llvm.fshl.i32(i32 undef, i32 %x, i32 7)
152 ret i32 %r
153}
154
155define i33 @fshr_op0_undef(i33 %x) {
156; CHECK-LABEL: @fshr_op0_undef(
157; CHECK-NEXT: [[R:%.*]] = call i33 @llvm.fshr.i33(i33 undef, i33 [[X:%.*]], i33 7)
158; CHECK-NEXT: ret i33 [[R]]
159;
160 %r = call i33 @llvm.fshr.i33(i33 undef, i33 %x, i33 7)
161 ret i33 %r
162}
163
164define i32 @fshl_op1_undef(i32 %x) {
165; CHECK-LABEL: @fshl_op1_undef(
166; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 undef, i32 7)
167; CHECK-NEXT: ret i32 [[R]]
168;
169 %r = call i32 @llvm.fshl.i32(i32 %x, i32 undef, i32 7)
170 ret i32 %r
171}
172
173define i33 @fshr_op1_undef(i33 %x) {
174; CHECK-LABEL: @fshr_op1_undef(
175; CHECK-NEXT: [[R:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 undef, i33 7)
176; CHECK-NEXT: ret i33 [[R]]
177;
178 %r = call i33 @llvm.fshr.i33(i33 %x, i33 undef, i33 7)
179 ret i33 %r
180}
181
182; Only demand bits from one of the operands.
183
184define i32 @fshl_only_op0_demanded(i32 %x, i32 %y) {
185; CHECK-LABEL: @fshl_only_op0_demanded(
186; CHECK-NEXT: [[Z:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 7)
187; CHECK-NEXT: [[R:%.*]] = and i32 [[Z]], 128
188; CHECK-NEXT: ret i32 [[R]]
189;
190 %z = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 7)
191 %r = and i32 %z, 128
192 ret i32 %r
193}
194
195define i32 @fshl_only_op1_demanded(i32 %x, i32 %y) {
196; CHECK-LABEL: @fshl_only_op1_demanded(
197; CHECK-NEXT: [[Z:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 7)
198; CHECK-NEXT: [[R:%.*]] = and i32 [[Z]], 63
199; CHECK-NEXT: ret i32 [[R]]
200;
201 %z = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 7)
202 %r = and i32 %z, 63
203 ret i32 %r
204}
205
206define i33 @fshr_only_op0_demanded(i33 %x, i33 %y) {
207; CHECK-LABEL: @fshr_only_op0_demanded(
208; CHECK-NEXT: [[Z:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 [[Y:%.*]], i33 7)
209; CHECK-NEXT: [[R:%.*]] = and i33 [[Z]], 12392
210; CHECK-NEXT: ret i33 [[R]]
211;
212 %z = call i33 @llvm.fshr.i33(i33 %x, i33 %y, i33 7)
213 %r = and i33 %z, 12392
214 ret i33 %r
215}
216
217define i33 @fshr_only_op1_demanded(i33 %x, i33 %y) {
218; CHECK-LABEL: @fshr_only_op1_demanded(
219; CHECK-NEXT: [[Z:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 [[Y:%.*]], i33 7)
220; CHECK-NEXT: [[R:%.*]] = lshr i33 [[Z]], 30
221; CHECK-NEXT: ret i33 [[R]]
222;
223 %z = call i33 @llvm.fshr.i33(i33 %x, i33 %y, i33 7)
224 %r = lshr i33 %z, 30
225 ret i33 %r
226}
227
228; Demand bits from both operands -- cannot simplify.
229
230define i32 @fshl_both_ops_demanded(i32 %x, i32 %y) {
231; CHECK-LABEL: @fshl_both_ops_demanded(
232; CHECK-NEXT: [[Z:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 7)
233; CHECK-NEXT: [[R:%.*]] = and i32 [[Z]], 192
234; CHECK-NEXT: ret i32 [[R]]
235;
236 %z = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 7)
237 %r = and i32 %z, 192
238 ret i32 %r
239}
240
241define i33 @fshr_both_ops_demanded(i33 %x, i33 %y) {
242; CHECK-LABEL: @fshr_both_ops_demanded(
243; CHECK-NEXT: [[Z:%.*]] = call i33 @llvm.fshr.i33(i33 [[X:%.*]], i33 [[Y:%.*]], i33 26)
244; CHECK-NEXT: [[R:%.*]] = and i33 [[Z]], 192
245; CHECK-NEXT: ret i33 [[R]]
246;
247 %z = call i33 @llvm.fshr.i33(i33 %x, i33 %y, i33 26)
248 %r = and i33 %z, 192
249 ret i33 %r
250}
251
252; Both operands are demanded, but there are known bits.
253
254define i32 @fshl_known_bits(i32 %x, i32 %y) {
255; CHECK-LABEL: @fshl_known_bits(
256; CHECK-NEXT: [[X2:%.*]] = or i32 [[X:%.*]], 1
257; CHECK-NEXT: [[Y2:%.*]] = lshr i32 [[Y:%.*]], 1
258; CHECK-NEXT: [[Z:%.*]] = call i32 @llvm.fshl.i32(i32 [[X2]], i32 [[Y2]], i32 7)
259; CHECK-NEXT: [[R:%.*]] = and i32 [[Z]], 192
260; CHECK-NEXT: ret i32 [[R]]
261;
262 %x2 = or i32 %x, 1 ; lo bit set
263 %y2 = lshr i32 %y, 1 ; hi bit clear
264 %z = call i32 @llvm.fshl.i32(i32 %x2, i32 %y2, i32 7)
265 %r = and i32 %z, 192
266 ret i32 %r
267}
268
269define i33 @fshr_known_bits(i33 %x, i33 %y) {
270; CHECK-LABEL: @fshr_known_bits(
271; CHECK-NEXT: [[X2:%.*]] = or i33 [[X:%.*]], 1
272; CHECK-NEXT: [[Y2:%.*]] = lshr i33 [[Y:%.*]], 1
273; CHECK-NEXT: [[Z:%.*]] = call i33 @llvm.fshr.i33(i33 [[X2]], i33 [[Y2]], i33 26)
274; CHECK-NEXT: [[R:%.*]] = and i33 [[Z]], 192
275; CHECK-NEXT: ret i33 [[R]]
276;
277 %x2 = or i33 %x, 1 ; lo bit set
278 %y2 = lshr i33 %y, 1 ; hi bit set
279 %z = call i33 @llvm.fshr.i33(i33 %x2, i33 %y2, i33 26)
280 %r = and i33 %z, 192
281 ret i33 %r
282}
283
284; This case fails to simplify due to multiple uses.
285
286define i33 @fshr_multi_use(i33 %a) {
287; CHECK-LABEL: @fshr_multi_use(
288; CHECK-NEXT: [[B:%.*]] = tail call i33 @llvm.fshr.i33(i33 [[A:%.*]], i33 [[A]], i33 1)
289; CHECK-NEXT: [[C:%.*]] = lshr i33 [[B]], 23
290; CHECK-NEXT: [[D:%.*]] = xor i33 [[C]], [[B]]
291; CHECK-NEXT: [[E:%.*]] = and i33 [[D]], 31
292; CHECK-NEXT: ret i33 [[E]]
293;
294 %b = tail call i33 @llvm.fshr.i33(i33 %a, i33 %a, i33 1)
295 %c = lshr i33 %b, 23
296 %d = xor i33 %c, %b
297 %e = and i33 %d, 31
298 ret i33 %e
299}
300
301; This demonstrates the same simplification working if the fshr intrinsic
302; is expanded into shifts and or.
303
304define i33 @expanded_fshr_multi_use(i33 %a) {
305; CHECK-LABEL: @expanded_fshr_multi_use(
306; CHECK-NEXT: [[TMP:%.*]] = lshr i33 [[A:%.*]], 1
307; CHECK-NEXT: [[C:%.*]] = lshr i33 [[A]], 24
308; CHECK-NEXT: [[D:%.*]] = xor i33 [[C]], [[TMP]]
309; CHECK-NEXT: [[E:%.*]] = and i33 [[D]], 31
310; CHECK-NEXT: ret i33 [[E]]
311;
312 %tmp = lshr i33 %a, 1
313 %tmp2 = shl i33 %a, 32
314 %b = or i33 %tmp, %tmp2
315 %c = lshr i33 %b, 23
316 %d = xor i33 %c, %b
317 %e = and i33 %d, 31
318 ret i33 %e
319}
320