1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -S -instsimplify < %s | FileCheck %s 3 4target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 5 6%struct.A = type { [7 x i8] } 7 8define %struct.A* @test1(%struct.A* %b, %struct.A* %e) { 9; CHECK-LABEL: @test1( 10; CHECK-NEXT: ret %struct.A* [[E:%.*]] 11; 12 %e_ptr = ptrtoint %struct.A* %e to i64 13 %b_ptr = ptrtoint %struct.A* %b to i64 14 %sub = sub i64 %e_ptr, %b_ptr 15 %sdiv = sdiv exact i64 %sub, 7 16 %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv 17 ret %struct.A* %gep 18} 19 20define i8* @test2(i8* %b, i8* %e) { 21; CHECK-LABEL: @test2( 22; CHECK-NEXT: ret i8* [[E:%.*]] 23; 24 %e_ptr = ptrtoint i8* %e to i64 25 %b_ptr = ptrtoint i8* %b to i64 26 %sub = sub i64 %e_ptr, %b_ptr 27 %gep = getelementptr inbounds i8, i8* %b, i64 %sub 28 ret i8* %gep 29} 30 31define i64* @test3(i64* %b, i64* %e) { 32; CHECK-LABEL: @test3( 33; CHECK-NEXT: ret i64* [[E:%.*]] 34; 35 %e_ptr = ptrtoint i64* %e to i64 36 %b_ptr = ptrtoint i64* %b to i64 37 %sub = sub i64 %e_ptr, %b_ptr 38 %ashr = ashr exact i64 %sub, 3 39 %gep = getelementptr inbounds i64, i64* %b, i64 %ashr 40 ret i64* %gep 41} 42 43define %struct.A* @test4(%struct.A* %b) { 44; CHECK-LABEL: @test4( 45; CHECK-NEXT: ret %struct.A* null 46; 47 %b_ptr = ptrtoint %struct.A* %b to i64 48 %sub = sub i64 0, %b_ptr 49 %sdiv = sdiv exact i64 %sub, 7 50 %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv 51 ret %struct.A* %gep 52} 53 54define i8* @test5(i8* %b) { 55; CHECK-LABEL: @test5( 56; CHECK-NEXT: ret i8* null 57; 58 %b_ptr = ptrtoint i8* %b to i64 59 %sub = sub i64 0, %b_ptr 60 %gep = getelementptr inbounds i8, i8* %b, i64 %sub 61 ret i8* %gep 62} 63 64define i64* @test6(i64* %b) { 65; CHECK-LABEL: @test6( 66; CHECK-NEXT: ret i64* null 67; 68 %b_ptr = ptrtoint i64* %b to i64 69 %sub = sub i64 0, %b_ptr 70 %ashr = ashr exact i64 %sub, 3 71 %gep = getelementptr inbounds i64, i64* %b, i64 %ashr 72 ret i64* %gep 73} 74 75define i8* @test7(i8* %b, i8** %e) { 76; CHECK-LABEL: @test7( 77; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i8** [[E:%.*]] to i64 78; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64 79; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]] 80; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[B]], i64 [[SUB]] 81; CHECK-NEXT: ret i8* [[GEP]] 82; 83 %e_ptr = ptrtoint i8** %e to i64 84 %b_ptr = ptrtoint i8* %b to i64 85 %sub = sub i64 %e_ptr, %b_ptr 86 %gep = getelementptr inbounds i8, i8* %b, i64 %sub 87 ret i8* %gep 88} 89 90define <8 x i64*> @undef_vec1() { 91; CHECK-LABEL: @undef_vec1( 92; CHECK-NEXT: ret <8 x i64*> undef 93; 94 %el = getelementptr inbounds i64, i64* undef, <8 x i64> undef 95 ret <8 x i64*> %el 96} 97 98define <8 x i64*> @undef_vec2() { 99; CHECK-LABEL: @undef_vec2( 100; CHECK-NEXT: ret <8 x i64*> undef 101; 102 %el = getelementptr i64, <8 x i64*> undef, <8 x i64> undef 103 ret <8 x i64*> %el 104} 105 106; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - begin. 107 108; Constant ptr 109 110define i32* @ptr_idx_scalar() { 111; CHECK-LABEL: @ptr_idx_scalar( 112; CHECK-NEXT: ret i32* inttoptr (i64 4 to i32*) 113; 114 %gep = getelementptr <4 x i32>, <4 x i32>* null, i64 0, i64 1 115 ret i32* %gep 116} 117 118define <2 x i32*> @ptr_idx_vector() { 119; CHECK-LABEL: @ptr_idx_vector( 120; CHECK-NEXT: ret <2 x i32*> getelementptr (i32, i32* null, <2 x i64> <i64 1, i64 1>) 121; 122 %gep = getelementptr i32, i32* null, <2 x i64> <i64 1, i64 1> 123 ret <2 x i32*> %gep 124} 125 126define <4 x i32*> @ptr_idx_mix_scalar_vector(){ 127; CHECK-LABEL: @ptr_idx_mix_scalar_vector( 128; CHECK-NEXT: ret <4 x i32*> getelementptr ([42 x [3 x i32]], [42 x [3 x i32]]* null, <4 x i64> zeroinitializer, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, <4 x i64> zeroinitializer) 129; 130 %gep = getelementptr [42 x [3 x i32]], [42 x [3 x i32]]* null, i64 0, <4 x i64> <i64 0, i64 1, i64 2, i64 3>, i64 0 131 ret <4 x i32*> %gep 132} 133 134; Constant vector 135 136define <4 x i32*> @vector_idx_scalar() { 137; CHECK-LABEL: @vector_idx_scalar( 138; CHECK-NEXT: ret <4 x i32*> getelementptr (i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>) 139; 140 %gep = getelementptr i32, <4 x i32*> zeroinitializer, i64 1 141 ret <4 x i32*> %gep 142} 143 144define <4 x i32*> @vector_idx_vector() { 145; CHECK-LABEL: @vector_idx_vector( 146; CHECK-NEXT: ret <4 x i32*> getelementptr (i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1>) 147; 148 %gep = getelementptr i32, <4 x i32*> zeroinitializer, <4 x i64> <i64 1, i64 1, i64 1, i64 1> 149 ret <4 x i32*> %gep 150} 151 152%struct = type { double, float } 153define <4 x float*> @vector_idx_mix_scalar_vector() { 154; CHECK-LABEL: @vector_idx_mix_scalar_vector( 155; CHECK-NEXT: ret <4 x float*> getelementptr (%struct, <4 x %struct*> zeroinitializer, <4 x i64> zeroinitializer, i32 1) 156; 157 %gep = getelementptr %struct, <4 x %struct*> zeroinitializer, i32 0, <4 x i32> <i32 1, i32 1, i32 1, i32 1> 158 ret <4 x float*> %gep 159} 160 161; Constant scalable 162 163define <vscale x 4 x i32*> @scalable_idx_scalar() { 164; CHECK-LABEL: @scalable_idx_scalar( 165; CHECK-NEXT: ret <vscale x 4 x i32*> getelementptr (i32, <vscale x 4 x i32*> zeroinitializer, <vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> undef, i64 1, i32 0), <vscale x 4 x i64> undef, <vscale x 4 x i32> zeroinitializer)) 166; 167 %gep = getelementptr i32, <vscale x 4 x i32*> zeroinitializer, i64 1 168 ret <vscale x 4 x i32*> %gep 169} 170 171define <vscale x 4 x float*> @scalable_vector_idx_mix_scalar_vector() { 172; CHECK-LABEL: @scalable_vector_idx_mix_scalar_vector( 173; CHECK-NEXT: ret <vscale x 4 x float*> getelementptr (%struct, <vscale x 4 x %struct*> zeroinitializer, <vscale x 4 x i64> zeroinitializer, i32 1) 174; 175 %gep = getelementptr %struct, <vscale x 4 x %struct*> zeroinitializer, i32 0, i32 1 176 ret <vscale x 4 x float*> %gep 177} 178 179define <vscale x 2 x i64*> @ptr_idx_mix_scalar_scalable_vector() { 180; CHECK-LABEL: @ptr_idx_mix_scalar_scalable_vector( 181; CHECK-NEXT: ret <vscale x 2 x i64*> zeroinitializer 182; 183 %v = getelementptr [2 x i64], [2 x i64]* null, i64 0, <vscale x 2 x i64> zeroinitializer 184 ret <vscale x 2 x i64*> %v 185} 186 187; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end. 188 189; TODO: this should return poison 190 191define i8* @poison() { 192; CHECK-LABEL: @poison( 193; CHECK-NEXT: ret i8* undef 194; 195 %v = getelementptr i8, i8* poison, i64 1 196 ret i8* %v 197} 198