Skip to content

Commit fd90261

Browse files
committed
[InstCombine] add tests for ashr/lshr of constant with shift amount offset; NFC
Baseline tests for D124369
1 parent 74ddcc2 commit fd90261

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

llvm/test/Transforms/InstCombine/shift-add.ll

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
;
44
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
55

6+
declare void @use(i8)
7+
68
define i32 @shl_C1_add_A_C2_i32(i16 %A) {
79
; CHECK-LABEL: @shl_C1_add_A_C2_i32(
810
; CHECK-NEXT: [[B:%.*]] = zext i16 [[A:%.*]] to i32
@@ -170,6 +172,189 @@ define i32 @shl_add_nsw(i32 %x) {
170172
ret i32 %r
171173
}
172174

175+
; offset precondition check (must be negative constant) for lshr_exact_add_negative_shift_positive
176+
177+
define i32 @lshr_exact_add_positive_shift_positive(i32 %x) {
178+
; CHECK-LABEL: @lshr_exact_add_positive_shift_positive(
179+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], 1
180+
; CHECK-NEXT: [[R:%.*]] = lshr exact i32 2, [[A]]
181+
; CHECK-NEXT: ret i32 [[R]]
182+
;
183+
%a = add i32 %x, 1
184+
%r = lshr exact i32 2, %a
185+
ret i32 %r
186+
}
187+
188+
define i32 @lshr_exact_add_big_negative_offset(i32 %x) {
189+
; CHECK-LABEL: @lshr_exact_add_big_negative_offset(
190+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -33
191+
; CHECK-NEXT: [[R:%.*]] = lshr exact i32 2, [[A]]
192+
; CHECK-NEXT: ret i32 [[R]]
193+
;
194+
%a = add i32 %x, -33
195+
%r = lshr exact i32 2, %a
196+
ret i32 %r
197+
}
198+
199+
; leading zeros for shifted constant precondition check for lshr_exact_add_negative_shift_positive
200+
201+
define i32 @lshr_exact_add_negative_shift_negative(i32 %x) {
202+
; CHECK-LABEL: @lshr_exact_add_negative_shift_negative(
203+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -1
204+
; CHECK-NEXT: [[R:%.*]] = lshr exact i32 -2, [[A]]
205+
; CHECK-NEXT: ret i32 [[R]]
206+
;
207+
%a = add i32 %x, -1
208+
%r = lshr exact i32 -2, %a
209+
ret i32 %r
210+
}
211+
212+
; exact precondition check for lshr_exact_add_negative_shift_positive
213+
214+
define i32 @lshr_add_negative_shift_no_exact(i32 %x) {
215+
; CHECK-LABEL: @lshr_add_negative_shift_no_exact(
216+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -1
217+
; CHECK-NEXT: [[R:%.*]] = lshr i32 2, [[A]]
218+
; CHECK-NEXT: ret i32 [[R]]
219+
;
220+
%a = add i32 %x, -1
221+
%r = lshr i32 2, %a
222+
ret i32 %r
223+
}
224+
225+
define i32 @lshr_exact_add_negative_shift_positive(i32 %x) {
226+
; CHECK-LABEL: @lshr_exact_add_negative_shift_positive(
227+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -1
228+
; CHECK-NEXT: [[R:%.*]] = lshr exact i32 2, [[A]]
229+
; CHECK-NEXT: ret i32 [[R]]
230+
;
231+
%a = add i32 %x, -1
232+
%r = lshr exact i32 2, %a
233+
ret i32 %r
234+
}
235+
236+
define i8 @lshr_exact_add_negative_shift_positive_extra_use(i8 %x) {
237+
; CHECK-LABEL: @lshr_exact_add_negative_shift_positive_extra_use(
238+
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -1
239+
; CHECK-NEXT: call void @use(i8 [[A]])
240+
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 64, [[A]]
241+
; CHECK-NEXT: ret i8 [[R]]
242+
;
243+
%a = add i8 %x, -1
244+
call void @use(i8 %a)
245+
%r = lshr exact i8 64, %a
246+
ret i8 %r
247+
}
248+
249+
define <2 x i9> @lshr_exact_add_negative_shift_positive_vec(<2 x i9> %x) {
250+
; CHECK-LABEL: @lshr_exact_add_negative_shift_positive_vec(
251+
; CHECK-NEXT: [[A:%.*]] = add <2 x i9> [[X:%.*]], <i9 -7, i9 -7>
252+
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i9> <i9 2, i9 2>, [[A]]
253+
; CHECK-NEXT: ret <2 x i9> [[R]]
254+
;
255+
%a = add <2 x i9> %x, <i9 -7, i9 -7>
256+
%r = lshr exact <2 x i9> <i9 2, i9 2>, %a
257+
ret <2 x i9> %r
258+
}
259+
260+
; not enough leading zeros in shift constant
261+
262+
define <2 x i9> @lshr_exact_add_negative_shift_lzcnt(<2 x i9> %x) {
263+
; CHECK-LABEL: @lshr_exact_add_negative_shift_lzcnt(
264+
; CHECK-NEXT: [[A:%.*]] = add <2 x i9> [[X:%.*]], <i9 -7, i9 -7>
265+
; CHECK-NEXT: [[R:%.*]] = lshr exact <2 x i9> <i9 4, i9 4>, [[A]]
266+
; CHECK-NEXT: ret <2 x i9> [[R]]
267+
;
268+
%a = add <2 x i9> %x, <i9 -7, i9 -7>
269+
%r = lshr exact <2 x i9> <i9 4, i9 4>, %a
270+
ret <2 x i9> %r
271+
}
272+
273+
; leading ones precondition check for ashr_exact_add_negative_shift_[positive,negative]
274+
275+
define i8 @ashr_exact_add_negative_shift_no_trailing_zeros(i8 %x) {
276+
; CHECK-LABEL: @ashr_exact_add_negative_shift_no_trailing_zeros(
277+
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -4
278+
; CHECK-NEXT: [[R:%.*]] = ashr exact i8 -112, [[A]]
279+
; CHECK-NEXT: ret i8 [[R]]
280+
;
281+
%a = add i8 %x, -4
282+
%r = ashr exact i8 -112, %a ; 0b1001_0000
283+
ret i8 %r
284+
}
285+
286+
define i32 @ashr_exact_add_big_negative_offset(i32 %x) {
287+
; CHECK-LABEL: @ashr_exact_add_big_negative_offset(
288+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -33
289+
; CHECK-NEXT: [[R:%.*]] = ashr exact i32 -2, [[A]]
290+
; CHECK-NEXT: ret i32 [[R]]
291+
;
292+
%a = add i32 %x, -33
293+
%r = ashr exact i32 -2, %a
294+
ret i32 %r
295+
}
296+
297+
; exact precondition check for ashr_exact_add_negative_shift_[positive,negative]
298+
299+
define i32 @ashr_add_negative_shift_no_exact(i32 %x) {
300+
; CHECK-LABEL: @ashr_add_negative_shift_no_exact(
301+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -1
302+
; CHECK-NEXT: [[R:%.*]] = ashr i32 -2, [[A]]
303+
; CHECK-NEXT: ret i32 [[R]]
304+
;
305+
%a = add i32 %x, -1
306+
%r = ashr i32 -2, %a
307+
ret i32 %r
308+
}
309+
310+
define i32 @ashr_exact_add_negative_shift_negative(i32 %x) {
311+
; CHECK-LABEL: @ashr_exact_add_negative_shift_negative(
312+
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -1
313+
; CHECK-NEXT: [[R:%.*]] = ashr exact i32 -2, [[A]]
314+
; CHECK-NEXT: ret i32 [[R]]
315+
;
316+
%a = add i32 %x, -1
317+
%r = ashr exact i32 -2, %a
318+
ret i32 %r
319+
}
320+
321+
define i8 @ashr_exact_add_negative_shift_negative_extra_use(i8 %x) {
322+
; CHECK-LABEL: @ashr_exact_add_negative_shift_negative_extra_use(
323+
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -2
324+
; CHECK-NEXT: call void @use(i8 [[A]])
325+
; CHECK-NEXT: [[R:%.*]] = ashr exact i8 -32, [[A]]
326+
; CHECK-NEXT: ret i8 [[R]]
327+
;
328+
%a = add i8 %x, -2
329+
call void @use(i8 %a)
330+
%r = ashr exact i8 -32, %a
331+
ret i8 %r
332+
}
333+
334+
define <2 x i7> @ashr_exact_add_negative_shift_negative_vec(<2 x i7> %x) {
335+
; CHECK-LABEL: @ashr_exact_add_negative_shift_negative_vec(
336+
; CHECK-NEXT: [[A:%.*]] = add <2 x i7> [[X:%.*]], <i7 -5, i7 -5>
337+
; CHECK-NEXT: [[R:%.*]] = ashr exact <2 x i7> <i7 -2, i7 -2>, [[A]]
338+
; CHECK-NEXT: ret <2 x i7> [[R]]
339+
;
340+
%a = add <2 x i7> %x, <i7 -5, i7 -5>
341+
%r = ashr exact <2 x i7> <i7 -2, i7 -2>, %a
342+
ret <2 x i7> %r
343+
}
344+
345+
; not enough leading ones in shift constant
346+
347+
define <2 x i7> @ashr_exact_add_negative_leading_ones_vec(<2 x i7> %x) {
348+
; CHECK-LABEL: @ashr_exact_add_negative_leading_ones_vec(
349+
; CHECK-NEXT: [[A:%.*]] = add <2 x i7> [[X:%.*]], <i7 -5, i7 -5>
350+
; CHECK-NEXT: [[R:%.*]] = ashr exact <2 x i7> <i7 -4, i7 -4>, [[A]]
351+
; CHECK-NEXT: ret <2 x i7> [[R]]
352+
;
353+
%a = add <2 x i7> %x, <i7 -5, i7 -5>
354+
%r = ashr exact <2 x i7> <i7 -4, i7 -4>, %a
355+
ret <2 x i7> %r
356+
}
357+
173358
; PR54890
174359

175360
define i32 @shl_nsw_add_negative(i32 %x) {

0 commit comments

Comments
 (0)