• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 undef. UB in any vector lane means the whole op is undef.
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> undef
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> undef
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> undef
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> undef
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> undef
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> undef
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
156; This would require computing known bits on both x and y. Is it worth doing?
157
158define i32 @udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
159; CHECK-LABEL: @udiv_dividend_known_smaller_than_divisor(
160; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 250
161; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
162; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
163; CHECK-NEXT:    ret i32 [[DIV]]
164;
165  %and = and i32 %x, 250
166  %or = or i32 %y, 251
167  %div = udiv i32 %and, %or
168  ret i32 %div
169}
170
171define i32 @not_udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
172; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_divisor(
173; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 251
174; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
175; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
176; CHECK-NEXT:    ret i32 [[DIV]]
177;
178  %and = and i32 %x, 251
179  %or = or i32 %y, 251
180  %div = udiv i32 %and, %or
181  ret i32 %div
182}
183
184declare i32 @external()
185
186define i32 @div1() {
187; CHECK-LABEL: @div1(
188; CHECK-NEXT:    [[CALL:%.*]] = call i32 @external(), !range !0
189; CHECK-NEXT:    ret i32 0
190;
191  %call = call i32 @external(), !range !0
192  %urem = udiv i32 %call, 3
193  ret i32 %urem
194}
195
196!0 = !{i32 0, i32 3}
197