blob: a1cc85727241fcaa49e701d64f81cf265ffdb30a [file] [log] [blame]
David Majnemer63da0c22017-01-06 22:58:02 +00001; RUN: opt < %s -instsimplify -S | FileCheck %s
2
Sanjay Patel2b1f6f42017-03-09 16:20:52 +00003; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
Sanjay Patel3bbee792017-03-06 18:45:39 +00004
Sanjay Patelbb476162017-03-09 20:31:20 +00005define <2 x i8> @sdiv_zero_elt_vec_constfold(<2 x i8> %x) {
6; CHECK-LABEL: @sdiv_zero_elt_vec_constfold(
Sanjay Patel7e563662017-03-09 20:42:30 +00007; CHECK-NEXT: ret <2 x i8> undef
Sanjay Patelbb476162017-03-09 20:31:20 +00008;
9 %div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
10 ret <2 x i8> %div
11}
12
13define <2 x i8> @udiv_zero_elt_vec_constfold(<2 x i8> %x) {
14; CHECK-LABEL: @udiv_zero_elt_vec_constfold(
Sanjay Patel7e563662017-03-09 20:42:30 +000015; CHECK-NEXT: ret <2 x i8> undef
Sanjay Patelbb476162017-03-09 20:31:20 +000016;
17 %div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
18 ret <2 x i8> %div
19}
20
Sanjay Patel3bbee792017-03-06 18:45:39 +000021define <2 x i8> @sdiv_zero_elt_vec(<2 x i8> %x) {
22; CHECK-LABEL: @sdiv_zero_elt_vec(
Sanjay Patel2b1f6f42017-03-09 16:20:52 +000023; CHECK-NEXT: ret <2 x i8> undef
Sanjay Patel3bbee792017-03-06 18:45:39 +000024;
25 %div = sdiv <2 x i8> %x, <i8 -42, i8 0>
26 ret <2 x i8> %div
27}
28
29define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) {
30; CHECK-LABEL: @udiv_zero_elt_vec(
Sanjay Patel2b1f6f42017-03-09 16:20:52 +000031; CHECK-NEXT: ret <2 x i8> undef
Sanjay Patel3bbee792017-03-06 18:45:39 +000032;
33 %div = udiv <2 x i8> %x, <i8 0, i8 42>
34 ret <2 x i8> %div
35}
36
Sanjay Patel962a8432017-03-09 21:56:03 +000037; Division-by-zero is undef. UB in any vector lane means the whole op is undef.
Sanjay Patel3bbee792017-03-06 18:45:39 +000038; Thus, we can simplify this: if any element of 'y' is 0, we can do anything.
39; Therefore, assume that all elements of 'y' must be 1.
40
41define <2 x i1> @sdiv_bool_vec(<2 x i1> %x, <2 x i1> %y) {
42; CHECK-LABEL: @sdiv_bool_vec(
Sanjay Patel962a8432017-03-09 21:56:03 +000043; CHECK-NEXT: ret <2 x i1> %x
Sanjay Patel3bbee792017-03-06 18:45:39 +000044;
45 %div = sdiv <2 x i1> %x, %y
46 ret <2 x i1> %div
47}
48
49define <2 x i1> @udiv_bool_vec(<2 x i1> %x, <2 x i1> %y) {
50; CHECK-LABEL: @udiv_bool_vec(
Sanjay Patel962a8432017-03-09 21:56:03 +000051; CHECK-NEXT: ret <2 x i1> %x
Sanjay Patel3bbee792017-03-06 18:45:39 +000052;
53 %div = udiv <2 x i1> %x, %y
54 ret <2 x i1> %div
55}
56
Sanjay Patelbb1b1c92017-09-11 20:38:31 +000057define i32 @udiv_dividend_known_smaller_than_constant_divisor(i32 %x) {
58; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +000059; CHECK-NEXT: ret i32 0
60;
61 %and = and i32 %x, 250
62 %div = udiv i32 %and, 251
63 ret i32 %div
64}
65
Sanjay Patelbb1b1c92017-09-11 20:38:31 +000066define i32 @not_udiv_dividend_known_smaller_than_constant_divisor(i32 %x) {
67; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +000068; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 251
69; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[AND]], 251
70; CHECK-NEXT: ret i32 [[DIV]]
71;
72 %and = and i32 %x, 251
73 %div = udiv i32 %and, 251
74 ret i32 %div
75}
76
Sanjay Patelbb1b1c92017-09-11 20:38:31 +000077define i32 @udiv_constant_dividend_known_smaller_than_divisor(i32 %x) {
78; CHECK-LABEL: @udiv_constant_dividend_known_smaller_than_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +000079; CHECK-NEXT: ret i32 0
80;
81 %or = or i32 %x, 251
82 %div = udiv i32 250, %or
83 ret i32 %div
84}
85
Sanjay Patelbb1b1c92017-09-11 20:38:31 +000086define i32 @not_udiv_constant_dividend_known_smaller_than_divisor(i32 %x) {
87; CHECK-LABEL: @not_udiv_constant_dividend_known_smaller_than_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +000088; CHECK-NEXT: [[OR:%.*]] = or i32 %x, 251
89; CHECK-NEXT: [[DIV:%.*]] = udiv i32 251, [[OR]]
90; CHECK-NEXT: ret i32 [[DIV]]
91;
92 %or = or i32 %x, 251
93 %div = udiv i32 251, %or
94 ret i32 %div
95}
96
97; This would require computing known bits on both x and y. Is it worth doing?
98
Sanjay Patelbb1b1c92017-09-11 20:38:31 +000099define i32 @udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
100; CHECK-LABEL: @udiv_dividend_known_smaller_than_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +0000101; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 250
102; CHECK-NEXT: [[OR:%.*]] = or i32 %y, 251
103; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
104; CHECK-NEXT: ret i32 [[DIV]]
105;
106 %and = and i32 %x, 250
107 %or = or i32 %y, 251
108 %div = udiv i32 %and, %or
109 ret i32 %div
110}
111
Sanjay Patelbb1b1c92017-09-11 20:38:31 +0000112define i32 @not_udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) {
113; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_divisor(
Sanjay Patel58761892017-09-10 17:55:08 +0000114; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 251
115; CHECK-NEXT: [[OR:%.*]] = or i32 %y, 251
116; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[AND]], [[OR]]
117; CHECK-NEXT: ret i32 [[DIV]]
118;
119 %and = and i32 %x, 251
120 %or = or i32 %y, 251
121 %div = udiv i32 %and, %or
122 ret i32 %div
123}
124
David Majnemer63da0c22017-01-06 22:58:02 +0000125declare i32 @external()
126
127define i32 @div1() {
128; CHECK-LABEL: @div1(
Sanjay Patelc4942392017-03-06 18:13:01 +0000129; CHECK-NEXT: [[CALL:%.*]] = call i32 @external(), !range !0
David Majnemer63da0c22017-01-06 22:58:02 +0000130; CHECK-NEXT: ret i32 0
131;
132 %call = call i32 @external(), !range !0
133 %urem = udiv i32 %call, 3
134 ret i32 %urem
135}
136
137!0 = !{i32 0, i32 3}