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
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
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
28 ; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
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> undef
34 %div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
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> undef
42 %div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
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> undef
50 %div = sdiv <2 x i8> %x, <i8 -42, i8 0>
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> undef
58 %div = udiv <2 x i8> %x, <i8 0, i8 42>
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> undef
66 %div = sdiv <2 x i8> %x, <i8 -42, i8 undef>
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> undef
74 %div = udiv <2 x i8> %x, <i8 undef, i8 42>
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
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
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
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
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
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
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
141 %div = udiv i32 250, %or
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]]
152 %div = udiv i32 251, %or
156 ; This would require computing known bits on both x and y. Is it worth doing?
158 define 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]]
165 %and = and i32 %x, 250
167 %div = udiv i32 %and, %or
171 define 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]]
178 %and = and i32 %x, 251
180 %div = udiv i32 %and, %or
184 declare i32 @external()
187 ; CHECK-LABEL: @div1(
188 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @external(), !range !0
189 ; CHECK-NEXT: ret i32 0
191 %call = call i32 @external(), !range !0
192 %urem = udiv i32 %call, 3