1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -passes=instcombine -S | FileCheck %s 3 4; Fold 5; x s/EXACT (1 << y) 6; to 7; x a>>EXACT y 8; iff 1<<y is non-negative 9 10define i8 @t0(i8 %x) { 11; CHECK-LABEL: @t0( 12; CHECK-NEXT: [[DIV:%.*]] = ashr exact i8 [[X:%.*]], 5 13; CHECK-NEXT: ret i8 [[DIV]] 14; 15 %div = sdiv exact i8 %x, 32 16 ret i8 %div 17} 18 19define i8 @n1(i8 %x) { 20; CHECK-LABEL: @n1( 21; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], 32 22; CHECK-NEXT: ret i8 [[DIV]] 23; 24 %div = sdiv i8 %x, 32 ; not exact 25 ret i8 %div 26} 27 28define i8 @n2(i8 %x) { 29; CHECK-LABEL: @n2( 30; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128 31; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i8 32; CHECK-NEXT: ret i8 [[DIV]] 33; 34 %div = sdiv i8 %x, 128 ; negative divisor 35 ret i8 %div 36} 37 38define <2 x i8> @t3_vec_splat(<2 x i8> %x) { 39; CHECK-LABEL: @t3_vec_splat( 40; CHECK-NEXT: [[DIV:%.*]] = ashr exact <2 x i8> [[X:%.*]], splat (i8 5) 41; CHECK-NEXT: ret <2 x i8> [[DIV]] 42; 43 %div = sdiv exact <2 x i8> %x, <i8 32, i8 32> 44 ret <2 x i8> %div 45} 46 47define <2 x i8> @t4_vec(<2 x i8> %x) { 48; CHECK-LABEL: @t4_vec( 49; CHECK-NEXT: [[DIV:%.*]] = ashr exact <2 x i8> [[X:%.*]], <i8 5, i8 4> 50; CHECK-NEXT: ret <2 x i8> [[DIV]] 51; 52 %div = sdiv exact <2 x i8> %x, <i8 32, i8 16> 53 ret <2 x i8> %div 54} 55 56define <2 x i8> @n5_vec_undef(<2 x i8> %x) { 57; CHECK-LABEL: @n5_vec_undef( 58; CHECK-NEXT: ret <2 x i8> poison 59; 60 %div = sdiv exact <2 x i8> %x, <i8 32, i8 undef> 61 ret <2 x i8> %div 62} 63 64define <2 x i8> @n6_vec_negative(<2 x i8> %x) { 65; CHECK-LABEL: @n6_vec_negative( 66; CHECK-NEXT: [[DIV:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 32, i8 -128> 67; CHECK-NEXT: ret <2 x i8> [[DIV]] 68; 69 %div = sdiv exact <2 x i8> %x, <i8 32, i8 128> ; non-non-negative divisor 70 ret <2 x i8> %div 71} 72 73; sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative) 74 75define i8 @shl1_nsw(i8 %x, i8 %y) { 76; CHECK-LABEL: @shl1_nsw( 77; CHECK-NEXT: [[DIV:%.*]] = ashr exact i8 [[X:%.*]], [[Y:%.*]] 78; CHECK-NEXT: ret i8 [[DIV]] 79; 80 %shl = shl nsw i8 1, %y 81 %div = sdiv exact i8 %x, %shl 82 ret i8 %div 83} 84 85; negative test - must have nsw 86 87define i8 @shl1_nuw(i8 %x, i8 %y) { 88; CHECK-LABEL: @shl1_nuw( 89; CHECK-NEXT: [[SHL:%.*]] = shl nuw i8 1, [[Y:%.*]] 90; CHECK-NEXT: [[DIV:%.*]] = sdiv exact i8 [[X:%.*]], [[SHL]] 91; CHECK-NEXT: ret i8 [[DIV]] 92; 93 %shl = shl nuw i8 1, %y 94 %div = sdiv exact i8 %x, %shl 95 ret i8 %div 96} 97 98; negative test - must have exact 99 100define i8 @shl1_nsw_not_exact(i8 %x, i8 %y) { 101; CHECK-LABEL: @shl1_nsw_not_exact( 102; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i8 1, [[Y:%.*]] 103; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], [[SHL]] 104; CHECK-NEXT: ret i8 [[DIV]] 105; 106 %shl = shl nsw i8 1, %y 107 %div = sdiv i8 %x, %shl 108 ret i8 %div 109} 110 111define i8 @prove_exact_with_high_mask(i8 %x, i8 %y) { 112; CHECK-LABEL: @prove_exact_with_high_mask( 113; CHECK-NEXT: [[A:%.*]] = ashr i8 [[X:%.*]], 2 114; CHECK-NEXT: [[D:%.*]] = and i8 [[A]], -2 115; CHECK-NEXT: ret i8 [[D]] 116; 117 %a = and i8 %x, -8 118 %d = sdiv i8 %a, 4 119 ret i8 %d 120} 121 122define i8 @prove_exact_with_high_mask_limit(i8 %x, i8 %y) { 123; CHECK-LABEL: @prove_exact_with_high_mask_limit( 124; CHECK-NEXT: [[A:%.*]] = ashr i8 [[X:%.*]], 3 125; CHECK-NEXT: ret i8 [[A]] 126; 127 %a = and i8 %x, -8 128 %d = sdiv i8 %a, 8 129 ret i8 %d 130} 131 132; negative test - not enough low zeros in dividend 133 134define i8 @not_prove_exact_with_high_mask(i8 %x, i8 %y) { 135; CHECK-LABEL: @not_prove_exact_with_high_mask( 136; CHECK-NEXT: [[A:%.*]] = and i8 [[X:%.*]], -8 137; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[A]], 16 138; CHECK-NEXT: ret i8 [[D]] 139; 140 %a = and i8 %x, -8 141 %d = sdiv i8 %a, 16 142 ret i8 %d 143} 144 145define <2 x i8> @prove_exact_with_high_mask_splat_vec(<2 x i8> %x, <2 x i8> %y) { 146; CHECK-LABEL: @prove_exact_with_high_mask_splat_vec( 147; CHECK-NEXT: [[A:%.*]] = shl <2 x i8> [[X:%.*]], splat (i8 3) 148; CHECK-NEXT: [[D:%.*]] = ashr exact <2 x i8> [[A]], splat (i8 3) 149; CHECK-NEXT: ret <2 x i8> [[D]] 150; 151 %a = shl <2 x i8> %x, <i8 3, i8 3> 152 %d = sdiv <2 x i8> %a, <i8 8, i8 8> 153 ret <2 x i8> %d 154} 155 156; TODO: Needs knownbits to handle arbitrary vector constants. 157 158define <2 x i8> @prove_exact_with_high_mask_vec(<2 x i8> %x, <2 x i8> %y) { 159; CHECK-LABEL: @prove_exact_with_high_mask_vec( 160; CHECK-NEXT: [[A:%.*]] = shl <2 x i8> [[X:%.*]], <i8 3, i8 2> 161; CHECK-NEXT: [[D:%.*]] = sdiv <2 x i8> [[A]], <i8 8, i8 4> 162; CHECK-NEXT: ret <2 x i8> [[D]] 163; 164 %a = shl <2 x i8> %x, <i8 3, i8 2> 165 %d = sdiv <2 x i8> %a, <i8 8, i8 4> 166 ret <2 x i8> %d 167} 168