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 poison. UB in any vector lane means the whole op is poison. 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> poison 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> poison 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> poison 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> poison 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> poison 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> poison 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 156define 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 159; 160 %t0 = zext i1 %b to i8 161 %xor = xor i8 %t0, 12 162 %r = udiv i8 %xor, 14 163 ret i8 %r 164} 165 166; negative test - dividend can equal 13 167 168define 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]] 174; 175 %t0 = zext i1 %b to i8 176 %xor = xor i8 %t0, 12 177 %r = udiv i8 %xor, 13 178 ret i8 %r 179} 180 181; This would require computing known bits on both x and y. Is it worth doing? 182 183define 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]] 189; 190 %and = and i32 %x, 250 191 %or = or i32 %y, 251 192 %div = udiv i32 %and, %or 193 ret i32 %div 194} 195 196define 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]] 202; 203 %and = and i32 %x, 251 204 %or = or i32 %y, 251 205 %div = udiv i32 %and, %or 206 ret i32 %div 207} 208 209declare i32 @external() 210 211define i32 @div1() { 212; CHECK-LABEL: @div1( 213; CHECK-NEXT: [[CALL:%.*]] = call i32 @external(), !range [[RNG0:![0-9]+]] 214; CHECK-NEXT: ret i32 0 215; 216 %call = call i32 @external(), !range !0 217 %urem = udiv i32 %call, 3 218 ret i32 %urem 219} 220 221define i8 @sdiv_minusone_divisor() { 222; CHECK-LABEL: @sdiv_minusone_divisor( 223; CHECK-NEXT: ret i8 poison 224; 225 %v = sdiv i8 -128, -1 226 ret i8 %v 227} 228 229!0 = !{i32 0, i32 3} 230