David Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 1 | ; RUN: opt < %s -instsimplify -S | FileCheck %s |
| 2 | |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 3 | ; Division-by-zero is undef. UB in any vector lane means the whole op is undef. |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 4 | |
Sanjay Patel | bb47616 | 2017-03-09 20:31:20 +0000 | [diff] [blame] | 5 | define <2 x i8> @sdiv_zero_elt_vec_constfold(<2 x i8> %x) { |
| 6 | ; CHECK-LABEL: @sdiv_zero_elt_vec_constfold( |
Sanjay Patel | 7e56366 | 2017-03-09 20:42:30 +0000 | [diff] [blame] | 7 | ; CHECK-NEXT: ret <2 x i8> undef |
Sanjay Patel | bb47616 | 2017-03-09 20:31:20 +0000 | [diff] [blame] | 8 | ; |
| 9 | %div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42> |
| 10 | ret <2 x i8> %div |
| 11 | } |
| 12 | |
| 13 | define <2 x i8> @udiv_zero_elt_vec_constfold(<2 x i8> %x) { |
| 14 | ; CHECK-LABEL: @udiv_zero_elt_vec_constfold( |
Sanjay Patel | 7e56366 | 2017-03-09 20:42:30 +0000 | [diff] [blame] | 15 | ; CHECK-NEXT: ret <2 x i8> undef |
Sanjay Patel | bb47616 | 2017-03-09 20:31:20 +0000 | [diff] [blame] | 16 | ; |
| 17 | %div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0> |
| 18 | ret <2 x i8> %div |
| 19 | } |
| 20 | |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 21 | define <2 x i8> @sdiv_zero_elt_vec(<2 x i8> %x) { |
| 22 | ; CHECK-LABEL: @sdiv_zero_elt_vec( |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 23 | ; CHECK-NEXT: ret <2 x i8> undef |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 24 | ; |
| 25 | %div = sdiv <2 x i8> %x, <i8 -42, i8 0> |
| 26 | ret <2 x i8> %div |
| 27 | } |
| 28 | |
| 29 | define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) { |
| 30 | ; CHECK-LABEL: @udiv_zero_elt_vec( |
Sanjay Patel | 2b1f6f4 | 2017-03-09 16:20:52 +0000 | [diff] [blame] | 31 | ; CHECK-NEXT: ret <2 x i8> undef |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 32 | ; |
| 33 | %div = udiv <2 x i8> %x, <i8 0, i8 42> |
| 34 | ret <2 x i8> %div |
| 35 | } |
| 36 | |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 37 | ; Division-by-zero is undef. UB in any vector lane means the whole op is undef. |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 38 | ; 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 | |
| 41 | define <2 x i1> @sdiv_bool_vec(<2 x i1> %x, <2 x i1> %y) { |
| 42 | ; CHECK-LABEL: @sdiv_bool_vec( |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 43 | ; CHECK-NEXT: ret <2 x i1> %x |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 44 | ; |
| 45 | %div = sdiv <2 x i1> %x, %y |
| 46 | ret <2 x i1> %div |
| 47 | } |
| 48 | |
| 49 | define <2 x i1> @udiv_bool_vec(<2 x i1> %x, <2 x i1> %y) { |
| 50 | ; CHECK-LABEL: @udiv_bool_vec( |
Sanjay Patel | 962a843 | 2017-03-09 21:56:03 +0000 | [diff] [blame] | 51 | ; CHECK-NEXT: ret <2 x i1> %x |
Sanjay Patel | 3bbee79 | 2017-03-06 18:45:39 +0000 | [diff] [blame] | 52 | ; |
| 53 | %div = udiv <2 x i1> %x, %y |
| 54 | ret <2 x i1> %div |
| 55 | } |
| 56 | |
Sanjay Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 57 | define i32 @udiv_dividend_known_smaller_than_constant_divisor(i32 %x) { |
| 58 | ; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 59 | ; 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 Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 66 | define i32 @not_udiv_dividend_known_smaller_than_constant_divisor(i32 %x) { |
| 67 | ; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 68 | ; 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 Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 77 | define i32 @udiv_constant_dividend_known_smaller_than_divisor(i32 %x) { |
| 78 | ; CHECK-LABEL: @udiv_constant_dividend_known_smaller_than_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 79 | ; 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 Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 86 | define i32 @not_udiv_constant_dividend_known_smaller_than_divisor(i32 %x) { |
| 87 | ; CHECK-LABEL: @not_udiv_constant_dividend_known_smaller_than_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 88 | ; 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 Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 99 | define i32 @udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) { |
| 100 | ; CHECK-LABEL: @udiv_dividend_known_smaller_than_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 101 | ; 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 Patel | bb1b1c9 | 2017-09-11 20:38:31 +0000 | [diff] [blame] | 112 | define i32 @not_udiv_dividend_known_smaller_than_divisor(i32 %x, i32 %y) { |
| 113 | ; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_divisor( |
Sanjay Patel | 5876189 | 2017-09-10 17:55:08 +0000 | [diff] [blame] | 114 | ; 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 Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 125 | declare i32 @external() |
| 126 | |
| 127 | define i32 @div1() { |
| 128 | ; CHECK-LABEL: @div1( |
Sanjay Patel | c494239 | 2017-03-06 18:13:01 +0000 | [diff] [blame] | 129 | ; CHECK-NEXT: [[CALL:%.*]] = call i32 @external(), !range !0 |
David Majnemer | 63da0c2 | 2017-01-06 22:58:02 +0000 | [diff] [blame] | 130 | ; 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} |