[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / test / Transforms / InstSimplify / div.ll
blob7eed067b284dcb7a9a2762a9310402733c9b7ce6
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instsimplify -S | FileCheck %s
4 define i32 @zero_dividend(i32 %A) {
5 ; CHECK-LABEL: @zero_dividend(
6 ; CHECK-NEXT:    ret i32 0
8   %B = sdiv i32 0, %A
9   ret i32 %B
12 define <2 x i32> @zero_dividend_vector(<2 x i32> %A) {
13 ; CHECK-LABEL: @zero_dividend_vector(
14 ; CHECK-NEXT:    ret <2 x i32> zeroinitializer
16   %B = udiv <2 x i32> zeroinitializer, %A
17   ret <2 x i32> %B
20 define <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
24   %B = sdiv <2 x i32> <i32 0, i32 undef>, %A
25   ret <2 x i32> %B
28 ; Division-by-zero is poison. UB in any vector lane means the whole op is poison.
30 define <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
34   %div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
35   ret <2 x i8> %div
38 define <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
42   %div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
43   ret <2 x i8> %div
46 define <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
50   %div = sdiv <2 x i8> %x, <i8 -42, i8 0>
51   ret <2 x i8> %div
54 define <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
58   %div = udiv <2 x i8> %x, <i8 0, i8 42>
59   ret <2 x i8> %div
62 define <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
66   %div = sdiv <2 x i8> %x, <i8 -42, i8 undef>
67   ret <2 x i8> %div
70 define <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
74   %div = udiv <2 x i8> %x, <i8 undef, i8 42>
75   ret <2 x i8> %div
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.
82 define <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:%.*]]
86   %div = sdiv <2 x i1> %x, %y
87   ret <2 x i1> %div
90 define <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:%.*]]
94   %div = udiv <2 x i1> %x, %y
95   ret <2 x i1> %div
98 define i32 @zext_bool_udiv_divisor(i1 %x, i32 %y) {
99 ; CHECK-LABEL: @zext_bool_udiv_divisor(
100 ; CHECK-NEXT:    ret i32 [[Y:%.*]]
102   %ext = zext i1 %x to i32
103   %r = udiv i32 %y, %ext
104   ret i32 %r
107 define <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:%.*]]
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
116 define 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
120   %and = and i32 %x, 250
121   %div = udiv i32 %and, 251
122   ret i32 %div
125 define 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]]
131   %and = and i32 %x, 251
132   %div = udiv i32 %and, 251
133   ret i32 %div
136 define 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
140   %or = or i32 %x, 251
141   %div = udiv i32 250, %or
142   ret i32 %div
145 define 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]]
151   %or = or i32 %x, 251
152   %div = udiv i32 251, %or
153   ret i32 %div
156 define i8 @udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
157 ; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor2(
158 ; CHECK-NEXT:    ret i8 0
160   %t0 = zext i1 %b to i8
161   %xor = xor i8 %t0, 12
162   %r = udiv i8 %xor, 14
163   ret i8 %r
166 ; negative test - dividend can equal 13
168 define i8 @not_udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
169 ; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor2(
170 ; CHECK-NEXT:    [[T0:%.*]] = zext i1 [[B:%.*]] to i8
171 ; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[T0]], 12
172 ; CHECK-NEXT:    [[R:%.*]] = udiv i8 [[XOR]], 13
173 ; CHECK-NEXT:    ret i8 [[R]]
175   %t0 = zext i1 %b to i8
176   %xor = xor i8 %t0, 12
177   %r = udiv i8 %xor, 13
178   ret i8 %r
181 ; This would require computing known bits on both x and y. Is it worth doing?
183 define i32 @udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
184 ; CHECK-LABEL: @udiv_dividend_known_smaller_than_divisor(
185 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 250
186 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
187 ; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
188 ; CHECK-NEXT:    ret i32 [[DIV]]
190   %and = and i32 %x, 250
191   %or = or i32 %y, 251
192   %div = udiv i32 %and, %or
193   ret i32 %div
196 define i32 @not_udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
197 ; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_divisor(
198 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X:%.*]], 251
199 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], 251
200 ; CHECK-NEXT:    [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
201 ; CHECK-NEXT:    ret i32 [[DIV]]
203   %and = and i32 %x, 251
204   %or = or i32 %y, 251
205   %div = udiv i32 %and, %or
206   ret i32 %div
209 declare i32 @external()
211 define i32 @div1() {
212 ; CHECK-LABEL: @div1(
213 ; CHECK-NEXT:    [[CALL:%.*]] = call i32 @external(), !range [[RNG0:![0-9]+]]
214 ; CHECK-NEXT:    ret i32 0
216   %call = call i32 @external(), !range !0
217   %urem = udiv i32 %call, 3
218   ret i32 %urem
221 define i8 @sdiv_minusone_divisor() {
222 ; CHECK-LABEL: @sdiv_minusone_divisor(
223 ; CHECK-NEXT:    ret i8 poison
225   %v = sdiv i8 -128, -1
226   ret i8 %v
229 !0 = !{i32 0, i32 3}