Skip to content

Commit 34f83e8

Browse files
committed
[InstSimplify] Precommit extra tests for PR71528
1 parent 29f03bf commit 34f83e8

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

llvm/test/Transforms/InstSimplify/po2-shift-add-and-to-zero.ll

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2-
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
2+
; RUN: opt -passes=instsimplify -S < %s | FileCheck %s
33

44
;; The and X, (add Y, -1) pattern is from an earlier instcombine pass which
55
;; converted
@@ -29,25 +29,65 @@
2929
;; calls were the same Value*, but since there's two of them it doesn't
3030
;; work and we convert the urem to add/and. CSE then gets rid of the extra
3131
;; vscale, leaving us with a new pattern to match. This only works because
32-
;; vscale is known to be a nonzero power of 2 (assuming there's a defined
33-
;; range for it).
32+
;; vscale is known to be a power of 2 (assuming there's a defined range for it).
3433

3534
define i64 @f1() #0 {
3635
; CHECK-LABEL: define i64 @f1
3736
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
3837
; CHECK-NEXT: entry:
3938
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
40-
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 4
41-
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw nsw i64 [[TMP0]], 3
42-
; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[TMP2]], -1
39+
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[TMP0]], 4
40+
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP0]], 3
41+
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP2]], -1
4342
; CHECK-NEXT: [[REM:%.*]] = and i64 [[TMP1]], [[TMP3]]
4443
; CHECK-NEXT: ret i64 [[REM]]
4544
;
4645
entry:
4746
%0 = call i64 @llvm.vscale.i64()
48-
%1 = shl nuw nsw i64 %0, 4
49-
%2 = shl nuw nsw i64 %0, 3
50-
%3 = add nsw i64 %2, -1
47+
%1 = shl i64 %0, 4
48+
%2 = shl i64 %0, 3
49+
%3 = add i64 %2, -1
50+
%rem = and i64 %1, %3
51+
ret i64 %rem
52+
}
53+
54+
;; Make sure it works if the value could also be zero.
55+
define i64 @test_pow2_or_zero(i64 %arg) {
56+
; CHECK-LABEL: define i64 @test_pow2_or_zero
57+
; CHECK-SAME: (i64 [[ARG:%.*]]) {
58+
; CHECK-NEXT: [[NEG:%.*]] = sub i64 0, [[ARG]]
59+
; CHECK-NEXT: [[X:%.*]] = and i64 [[NEG]], [[ARG]]
60+
; CHECK-NEXT: [[SHL1:%.*]] = shl i64 [[X]], 4
61+
; CHECK-NEXT: [[SHL2:%.*]] = shl i64 [[X]], 3
62+
; CHECK-NEXT: [[MASK:%.*]] = add i64 [[SHL2]], -1
63+
; CHECK-NEXT: [[REM:%.*]] = and i64 [[SHL1]], [[MASK]]
64+
; CHECK-NEXT: ret i64 [[REM]]
65+
;
66+
%neg = sub i64 0, %arg
67+
%x = and i64 %neg, %arg
68+
%shl1 = shl i64 %x, 4
69+
%shl2 = shl i64 %x, 3
70+
%mask = add i64 %shl2, -1
71+
%rem = and i64 %shl1, %mask
72+
ret i64 %rem
73+
}
74+
75+
;; Make sure it doesn't work if the value isn't known to be a power of 2.
76+
define i64 @no_pow2() {
77+
; CHECK-LABEL: define i64 @no_pow2() {
78+
; CHECK-NEXT: entry:
79+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
80+
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[TMP0]], 4
81+
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP0]], 3
82+
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP2]], -1
83+
; CHECK-NEXT: [[REM:%.*]] = and i64 [[TMP1]], [[TMP3]]
84+
; CHECK-NEXT: ret i64 [[REM]]
85+
;
86+
entry:
87+
%0 = call i64 @llvm.vscale.i64()
88+
%1 = shl i64 %0, 4
89+
%2 = shl i64 %0, 3
90+
%3 = add i64 %2, -1
5191
%rem = and i64 %1, %3
5292
ret i64 %rem
5393
}

0 commit comments

Comments
 (0)