1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 target datalayout = "n8:16:32"
7 ; (trunc (x << Q) to iDst) << K
8 ; we should rewrite it as
9 ; (trunc (x << (Q+K)) to iDst) iff (Q+K) u< iDst
10 ; This is only valid for shl.
11 ; THIS FOLD DOES *NOT* REQUIRE ANY 'nuw'/`nsw` FLAGS!
15 define i16 @t0(i32 %x, i16 %y) {
17 ; CHECK-NEXT: [[X_TR:%.*]] = trunc i32 [[X:%.*]] to i16
18 ; CHECK-NEXT: [[T5:%.*]] = shl i16 [[X_TR]], 8
19 ; CHECK-NEXT: ret i16 [[T5]]
22 %t1 = zext i16 %t0 to i32
24 %t3 = trunc i32 %t2 to i16
26 %t5 = shl i16 %t3, %t4
30 define <2 x i16> @t1_vec_splat(<2 x i32> %x, <2 x i16> %y) {
31 ; CHECK-LABEL: @t1_vec_splat(
32 ; CHECK-NEXT: [[X_TR:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i16>
33 ; CHECK-NEXT: [[T5:%.*]] = shl <2 x i16> [[X_TR]], <i16 8, i16 8>
34 ; CHECK-NEXT: ret <2 x i16> [[T5]]
36 %t0 = sub <2 x i16> <i16 32, i16 32>, %y
37 %t1 = zext <2 x i16> %t0 to <2 x i32>
38 %t2 = shl <2 x i32> %x, %t1
39 %t3 = trunc <2 x i32> %t2 to <2 x i16>
40 %t4 = add <2 x i16> %y, <i16 -24, i16 -24>
41 %t5 = shl <2 x i16> %t3, %t4
45 define <2 x i16> @t2_vec_nonsplat(<2 x i32> %x, <2 x i16> %y) {
46 ; CHECK-LABEL: @t2_vec_nonsplat(
47 ; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 8, i32 30>
48 ; CHECK-NEXT: [[T5:%.*]] = trunc <2 x i32> [[TMP1]] to <2 x i16>
49 ; CHECK-NEXT: ret <2 x i16> [[T5]]
51 %t0 = sub <2 x i16> <i16 32, i16 30>, %y
52 %t1 = zext <2 x i16> %t0 to <2 x i32>
53 %t2 = shl <2 x i32> %x, %t1
54 %t3 = trunc <2 x i32> %t2 to <2 x i16>
55 %t4 = add <2 x i16> %y, <i16 -24, i16 0>
56 %t5 = shl <2 x i16> %t3, %t4
62 define <3 x i16> @t3_vec_nonsplat_poison0(<3 x i32> %x, <3 x i16> %y) {
63 ; CHECK-LABEL: @t3_vec_nonsplat_poison0(
64 ; CHECK-NEXT: [[X_TR:%.*]] = trunc <3 x i32> [[X:%.*]] to <3 x i16>
65 ; CHECK-NEXT: [[T5:%.*]] = shl <3 x i16> [[X_TR]], <i16 8, i16 poison, i16 8>
66 ; CHECK-NEXT: ret <3 x i16> [[T5]]
68 %t0 = sub <3 x i16> <i16 32, i16 poison, i16 32>, %y
69 %t1 = zext <3 x i16> %t0 to <3 x i32>
70 %t2 = shl <3 x i32> %x, %t1
71 %t3 = trunc <3 x i32> %t2 to <3 x i16>
72 %t4 = add <3 x i16> %y, <i16 -24, i16 -24, i16 -24>
73 %t5 = shl <3 x i16> %t3, %t4
77 define <3 x i16> @t4_vec_nonsplat_poison1(<3 x i32> %x, <3 x i16> %y) {
78 ; CHECK-LABEL: @t4_vec_nonsplat_poison1(
79 ; CHECK-NEXT: [[X_TR:%.*]] = trunc <3 x i32> [[X:%.*]] to <3 x i16>
80 ; CHECK-NEXT: [[T5:%.*]] = shl <3 x i16> [[X_TR]], <i16 8, i16 poison, i16 8>
81 ; CHECK-NEXT: ret <3 x i16> [[T5]]
83 %t0 = sub <3 x i16> <i16 32, i16 32, i16 32>, %y
84 %t1 = zext <3 x i16> %t0 to <3 x i32>
85 %t2 = shl <3 x i32> %x, %t1
86 %t3 = trunc <3 x i32> %t2 to <3 x i16>
87 %t4 = add <3 x i16> %y, <i16 -24, i16 poison, i16 -24>
88 %t5 = shl <3 x i16> %t3, %t4
92 define <3 x i16> @t5_vec_nonsplat_poison1(<3 x i32> %x, <3 x i16> %y) {
93 ; CHECK-LABEL: @t5_vec_nonsplat_poison1(
94 ; CHECK-NEXT: [[X_TR:%.*]] = trunc <3 x i32> [[X:%.*]] to <3 x i16>
95 ; CHECK-NEXT: [[T5:%.*]] = shl <3 x i16> [[X_TR]], <i16 8, i16 poison, i16 8>
96 ; CHECK-NEXT: ret <3 x i16> [[T5]]
98 %t0 = sub <3 x i16> <i16 32, i16 poison, i16 32>, %y
99 %t1 = zext <3 x i16> %t0 to <3 x i32>
100 %t2 = shl <3 x i32> %x, %t1
101 %t3 = trunc <3 x i32> %t2 to <3 x i16>
102 %t4 = add <3 x i16> %y, <i16 -24, i16 poison, i16 -24>
103 %t5 = shl <3 x i16> %t3, %t4
109 declare void @use16(i16)
110 declare void @use32(i32)
112 define i16 @t6_extrause0(i32 %x, i16 %y) {
113 ; CHECK-LABEL: @t6_extrause0(
114 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
115 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
116 ; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X:%.*]], [[T1]]
117 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
118 ; CHECK-NEXT: call void @use16(i16 [[T3]])
119 ; CHECK-NEXT: [[X_TR:%.*]] = trunc i32 [[X]] to i16
120 ; CHECK-NEXT: [[T5:%.*]] = shl i16 [[X_TR]], 8
121 ; CHECK-NEXT: ret i16 [[T5]]
124 %t1 = zext i16 %t0 to i32
125 %t2 = shl i32 %x, %t1
126 %t3 = trunc i32 %t2 to i16
127 %t4 = add i16 %y, -24
128 call void @use16(i16 %t3)
129 %t5 = shl i16 %t3, %t4
133 define i16 @t7_extrause1(i32 %x, i16 %y) {
134 ; CHECK-LABEL: @t7_extrause1(
135 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y:%.*]], -24
136 ; CHECK-NEXT: call void @use16(i16 [[T4]])
137 ; CHECK-NEXT: [[X_TR:%.*]] = trunc i32 [[X:%.*]] to i16
138 ; CHECK-NEXT: [[T5:%.*]] = shl i16 [[X_TR]], 8
139 ; CHECK-NEXT: ret i16 [[T5]]
142 %t1 = zext i16 %t0 to i32
143 %t2 = shl i32 %x, %t1
144 %t3 = trunc i32 %t2 to i16
145 %t4 = add i16 %y, -24
146 call void @use16(i16 %t4)
147 %t5 = shl i16 %t3, %t4
151 define i16 @t8_extrause2(i32 %x, i16 %y) {
152 ; CHECK-LABEL: @t8_extrause2(
153 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
154 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
155 ; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X:%.*]], [[T1]]
156 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
157 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y]], -24
158 ; CHECK-NEXT: call void @use16(i16 [[T3]])
159 ; CHECK-NEXT: call void @use16(i16 [[T4]])
160 ; CHECK-NEXT: [[T5:%.*]] = shl i16 [[T3]], [[T4]]
161 ; CHECK-NEXT: ret i16 [[T5]]
164 %t1 = zext i16 %t0 to i32
165 %t2 = shl i32 %x, %t1
166 %t3 = trunc i32 %t2 to i16
167 %t4 = add i16 %y, -24
168 call void @use16(i16 %t3)
169 call void @use16(i16 %t4)
170 %t5 = shl i16 %t3, %t4
174 ; No 'nuw'/'nsw' flags are to be propagated!
175 ; But we can't test that, such IR does not reach that code.
179 ; Can't fold, total shift would be 32
180 define i16 @n11(i32 %x, i16 %y) {
182 ; CHECK-NEXT: [[T0:%.*]] = sub i16 30, [[Y:%.*]]
183 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
184 ; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X:%.*]], [[T1]]
185 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
186 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y]], -31
187 ; CHECK-NEXT: [[T5:%.*]] = shl i16 [[T3]], [[T4]]
188 ; CHECK-NEXT: ret i16 [[T5]]
191 %t1 = zext i16 %t0 to i32
192 %t2 = shl i32 %x, %t1
193 %t3 = trunc i32 %t2 to i16
194 %t4 = add i16 %y, -31
195 %t5 = shl i16 %t3, %t4
199 ; Bit width mismatch of shift amount
203 define i16 @t01(i32 %x) {
205 ; CHECK-NEXT: [[T0:%.*]] = shl i32 [[X:%.*]], ptrtoint (ptr @Y32 to i32)
206 ; CHECK-NEXT: [[T1:%.*]] = trunc i32 [[T0]] to i16
207 ; CHECK-NEXT: [[T2:%.*]] = shl i16 [[T1]], ptrtoint (ptr @Y16 to i16)
208 ; CHECK-NEXT: ret i16 [[T2]]
210 %t0 = shl i32 %x, ptrtoint (ptr @Y32 to i32)
211 %t1 = trunc i32 %t0 to i16
212 %t2 = shl i16 %t1, ptrtoint (ptr @Y16 to i16)
216 define i16 @shl_tr_shl_constant_shift_amount_uses(i32 %x) {
217 ; CHECK-LABEL: @shl_tr_shl_constant_shift_amount_uses(
218 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 3
219 ; CHECK-NEXT: call void @use32(i32 [[SHL]])
220 ; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[SHL]] to i16
221 ; CHECK-NEXT: call void @use16(i16 [[TR]])
222 ; CHECK-NEXT: [[R:%.*]] = shl i16 [[TR]], 4
223 ; CHECK-NEXT: ret i16 [[R]]
226 call void @use32(i32 %shl)
227 %tr = trunc i32 %shl to i16
228 call void @use16(i16 %tr)
233 ; This would infinite loop because we did not process the
234 ; poison shift before trying to fold shift-of-shift. To reach
235 ; the problematic transform, it needs a datalayout to specify
236 ; that the narrow types are legal, but i64 is not.
238 define i1 @PR51657(i64 %x, i1 %c1) {
239 ; CHECK-LABEL: @PR51657(
241 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[COND_FALSE:%.*]], label [[COND_END:%.*]]
243 ; CHECK-NEXT: br label [[COND_END]]
245 ; CHECK-NEXT: ret i1 true
248 br i1 %c1, label %cond.false, label %cond.end
251 %shl = shl i64 %x, 64
252 %conv26 = trunc i64 %shl to i32
253 %t1 = trunc i64 %shl to i8
257 %cond = phi i32 [ %conv26, %cond.false ], [ 0, %entry ]
258 %t2 = phi i8 [ %t1, %cond.false ], [ 0, %entry ]
259 %conv27 = sext i8 %t2 to i32
260 %eq = icmp eq i32 %cond, %conv27
265 define i16 @extra_use_on_first_shift(i32 %x) {
266 ; CHECK-LABEL: @extra_use_on_first_shift(
267 ; CHECK-NEXT: [[A:%.*]] = ashr i32 [[X:%.*]], 3
268 ; CHECK-NEXT: call void @use32(i32 [[A]])
269 ; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[A]] to i16
270 ; CHECK-NEXT: [[SH:%.*]] = lshr i16 [[TR]], 6
271 ; CHECK-NEXT: ret i16 [[SH]]
274 call void @use32(i32 %a)
275 %tr = trunc i32 %a to i16
276 %sh = lshr i16 %tr, 6