1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4define i32 @zero_dividend(i32 %A) {
5; CHECK-LABEL: @zero_dividend(
6; CHECK-NEXT:    ret i32 0
7;
8  %B = sdiv i32 0, %A
9  ret i32 %B
10}
11
12define <2 x i32> @zero_dividend_vector(<2 x i32> %A) {
13; CHECK-LABEL: @zero_dividend_vector(
14; CHECK-NEXT:    ret <2 x i32> zeroinitializer
15;
16  %B = udiv <2 x i32> zeroinitializer, %A
17  ret <2 x i32> %B
18}
19
20define <2 x i32> @zero_dividend_vector_undef_elt(<2 x i32> %A) {
21; CHECK-LABEL: @zero_dividend_vector_undef_elt(
22; CHECK-NEXT:    ret <2 x i32> zeroinitializer
23;
24  %B = sdiv <2 x i32> <i32 0, i32 undef>, %A
25  ret <2 x i32> %B
26}
27
28; Division-by-zero is poison. UB in any vector lane means the whole op is poison.
29
30define <2 x i8> @sdiv_zero_elt_vec_constfold(<2 x i8> %x) {
31; CHECK-LABEL: @sdiv_zero_elt_vec_constfold(
32; CHECK-NEXT:    ret <2 x i8> poison
33;
34  %div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
35  ret <2 x i8> %div
36}
37
38define <2 x i8> @udiv_zero_elt_vec_constfold(<2 x i8> %x) {
39; CHECK-LABEL: @udiv_zero_elt_vec_constfold(
40; CHECK-NEXT:    ret <2 x i8> poison
41;
42  %div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
43  ret <2 x i8> %div
44}
45
46define <2 x i8> @sdiv_zero_elt_vec(<2 x i8> %x) {
47; CHECK-LABEL: @sdiv_zero_elt_vec(
48; CHECK-NEXT:    ret <2 x i8> poison
49;
50  %div = sdiv <2 x i8> %x, <i8 -42, i8 0>
51  ret <2 x i8> %div
52}
53
54define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) {
55; CHECK-LABEL: @udiv_zero_elt_vec(
56; CHECK-NEXT:    ret <2 x i8> poison
57;
58  %div = udiv <2 x i8> %x, <i8 0, i8 42>
59  ret <2 x i8> %div
60}
61
62define <2 x i8> @sdiv_undef_elt_vec(<2 x i8> %x) {
63; CHECK-LABEL: @sdiv_undef_elt_vec(
64; CHECK-NEXT:    ret <2 x i8> poison
65;
66  %div = sdiv <2 x i8> %x, <i8 -42, i8 undef>
67  ret <2 x i8> %div
68}
69
70define <2 x i8> @udiv_undef_elt_vec(<2 x i8> %x) {
71; CHECK-LABEL: @udiv_undef_elt_vec(
72; CHECK-NEXT:    ret <2 x i8> poison
73;
74  %div = udiv <2 x i8> %x, <i8 undef, i8 42>
75  ret <2 x i8> %div
76}
77
78; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
79; Thus, we can simplify this: if any element of 'y' is 0, we can do anything.
80; Therefore, assume that all elements of 'y' must be 1.
81
82define <2 x i1> @sdiv_bool_vec(<2 x i1> %x, <2 x i1> %y) {
83; CHECK-LABEL: @sdiv_bool_vec(
84; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
85;
86  %div = sdiv <2 x i1> %x, %y
87  ret <2 x i1> %div
88}
89
90define <2 x i1> @udiv_bool_vec(<2 x i1> %x, <2 x i1> %y) {
91; CHECK-LABEL: @udiv_bool_vec(
92; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
93;
94  %div = udiv <2 x i1> %x, %y
95  ret <2 x i1> %div
96}
97
98define i32 @zext_bool_udiv_divisor(i1 %x, i32 %y) {
99; CHECK-LABEL: @zext_bool_udiv_divisor(
100; CHECK-NEXT:    ret i32 [[Y:%.*]]
101;
102  %ext = zext i1 %x to i32
103  %r = udiv i32 %y, %ext
104  ret i32 %r
105}
106
107define <2 x i32> @zext_bool_sdiv_divisor_vec(<2 x i1> %x, <2 x i32> %y) {
108; CHECK-LABEL: @zext_bool_sdiv_divisor_vec(
109; CHECK-NEXT:    ret <2 x i32> [[Y:%.*]]
110;
111  %ext = zext <2 x i1> %x to <2 x i32>
112  %r = sdiv <2 x i32> %y, %ext
113  ret <2 x i32> %r
114}
115
116define i32 @udiv_dividend_known_smaller_than_constant_divisor(i32 %x) {
117; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor(
118; CHECK-NEXT:    ret i32 0
119;
120  %and = and i32 %x, 250
121  %div = udiv i32 %and, 251
122  ret i32 %div
123}
124
125define i32 @not_udiv_dividend_known_smaller_than_constant_divisor(i32 %x) {
126; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor(
127; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 251
128; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], 251
129; CHECK-NEXT:    ret i32 [[DIV]]
130;
131  %and = and i32 %x, 251
132  %div = udiv i32 %and, 251
133  ret i32 %div
134}
135
136define i32 @udiv_constant_dividend_known_smaller_than_divisor(i32 %x) {
137; CHECK-LABEL: @udiv_constant_dividend_known_smaller_than_divisor(
138; CHECK-NEXT:    ret i32 0
139;
140  %or = or i32 %x, 251
141  %div = udiv i32 250, %or
142  ret i32 %div
143}
144
145define i32 @not_udiv_constant_dividend_known_smaller_than_divisor(i32 %x) {
146; CHECK-LABEL: @not_udiv_constant_dividend_known_smaller_than_divisor(
147; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X:%.*]], 251
148; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 251, [[OR]]
149; CHECK-NEXT:    ret i32 [[DIV]]
150;
151  %or = or i32 %x, 251
152  %div = udiv i32 251, %or
153  ret i32 %div
154}
155
156define i8 @udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
157; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor2(
158; CHECK-NEXT:    [[T0:%.*]] = zext i1 [[B:%.*]] to i8
159; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[T0]], 12
160; CHECK-NEXT:    [[R:%.*]] = udiv i8 [[XOR]], 14
161; CHECK-NEXT:    ret i8 [[R]]
162;
163  %t0 = zext i1 %b to i8
164  %xor = xor i8 %t0, 12
165  %r = udiv i8 %xor, 14
166  ret i8 %r
167}
168
169define i8 @not_udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
170; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor2(
171; CHECK-NEXT:    [[T0:%.*]] = zext i1 [[B:%.*]] to i8
172; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[T0]], 12
173; CHECK-NEXT:    [[R:%.*]] = udiv i8 [[XOR]], 13
174; CHECK-NEXT:    ret i8 [[R]]
175;
176  %t0 = zext i1 %b to i8
177  %xor = xor i8 %t0, 12
178  %r = udiv i8 %xor, 13
179  ret i8 %r
180}
181
182; This would require computing known bits on both x and y. Is it worth doing?
183
184define i32 @udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
185; CHECK-LABEL: @udiv_dividend_known_smaller_than_divisor(
186; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 250
187; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
188; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
189; CHECK-NEXT:    ret i32 [[DIV]]
190;
191  %and = and i32 %x, 250
192  %or = or i32 %y, 251
193  %div = udiv i32 %and, %or
194  ret i32 %div
195}
196
197define i32 @not_udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
198; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_divisor(
199; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 251
200; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
201; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
202; CHECK-NEXT:    ret i32 [[DIV]]
203;
204  %and = and i32 %x, 251
205  %or = or i32 %y, 251
206  %div = udiv i32 %and, %or
207  ret i32 %div
208}
209
210declare i32 @external()
211
212define i32 @div1() {
213; CHECK-LABEL: @div1(
214; CHECK-NEXT:    [[CALL:%.*]] = call i32 @external(), !range [[RNG0:![0-9]+]]
215; CHECK-NEXT:    ret i32 0
216;
217  %call = call i32 @external(), !range !0
218  %urem = udiv i32 %call, 3
219  ret i32 %urem
220}
221
222define i8 @sdiv_minusone_divisor() {
223; CHECK-LABEL: @sdiv_minusone_divisor(
224; CHECK-NEXT:    ret i8 poison
225;
226  %v = sdiv i8 -128, -1
227  ret i8 %v
228}
229
230!0 = !{i32 0, i32 3}
231