1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -instcombine -S | FileCheck %s 3 4target datalayout = "e-m:m-p:40:64:64:32-i32:32-i16:16-i8:8-n32" 5 6%struct.B = type { double } 7%struct.A = type { %struct.B, i32, i32 } 8%struct.C = type { [7 x i8] } 9 10 11@Global = constant [10 x i8] c"helloworld" 12 13 14; Test that two array indexing geps fold 15define i32* @test1(i32* %I) { 16; CHECK-LABEL: @test1( 17; CHECK-NEXT: [[B:%.*]] = getelementptr i32, i32* [[I:%.*]], i32 21 18; CHECK-NEXT: ret i32* [[B]] 19; 20 %A = getelementptr i32, i32* %I, i8 17 21 %B = getelementptr i32, i32* %A, i16 4 22 ret i32* %B 23} 24 25; Test that two getelementptr insts fold 26define i32* @test2({ i32 }* %I) { 27; CHECK-LABEL: @test2( 28; CHECK-NEXT: [[B:%.*]] = getelementptr { i32 }, { i32 }* [[I:%.*]], i32 1, i32 0 29; CHECK-NEXT: ret i32* [[B]] 30; 31 %A = getelementptr { i32 }, { i32 }* %I, i32 1 32 %B = getelementptr { i32 }, { i32 }* %A, i32 0, i32 0 33 ret i32* %B 34} 35 36define void @test3(i8 %B) { 37; This should be turned into a constexpr instead of being an instruction 38; CHECK-LABEL: @test3( 39; CHECK-NEXT: store i8 [[B:%.*]], i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i32 0, i32 4), align 1 40; CHECK-NEXT: ret void 41; 42 %A = getelementptr [10 x i8], [10 x i8]* @Global, i32 0, i32 4 43 store i8 %B, i8* %A 44 ret void 45} 46 47%as1_ptr_struct = type { i32 addrspace(1)* } 48%as2_ptr_struct = type { i32 addrspace(2)* } 49 50@global_as2 = addrspace(2) global i32 zeroinitializer 51@global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 } 52 53; This should be turned into a constexpr instead of being an instruction 54define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) { 55; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs( 56; CHECK-NEXT: store i32 addrspace(2)* [[B:%.*]], i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct, [[AS2_PTR_STRUCT:%.*]] addrspace(1)* @global_as1_as2_ptr, i32 0, i32 0), align 8 57; CHECK-NEXT: ret void 58; 59 %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i32 0, i32 0 60 store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A 61 ret void 62} 63 64@arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer 65 66define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) { 67; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array( 68; CHECK-NEXT: store i8 addrspace(2)* [[B:%.*]], i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i32 0, i32 2), align 16 69; CHECK-NEXT: ret void 70; 71 72 %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2 73 store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A 74 ret void 75} 76 77define i32* @test4(i32* %I, i32 %C, i32 %D) { 78; CHECK-LABEL: @test4( 79; CHECK-NEXT: [[A:%.*]] = getelementptr i32, i32* [[I:%.*]], i32 [[C:%.*]] 80; CHECK-NEXT: [[B:%.*]] = getelementptr i32, i32* [[A]], i32 [[D:%.*]] 81; CHECK-NEXT: ret i32* [[B]] 82; 83 %A = getelementptr i32, i32* %I, i32 %C 84 %B = getelementptr i32, i32* %A, i32 %D 85 ret i32* %B 86} 87 88 89define i1 @test5({ i32, i32 }* %x, { i32, i32 }* %y) { 90; CHECK-LABEL: @test5( 91; CHECK-NEXT: [[TMP_4:%.*]] = icmp eq { i32, i32 }* [[X:%.*]], [[Y:%.*]] 92; CHECK-NEXT: ret i1 [[TMP_4]] 93; 94 %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1 95 %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1 96 ;; seteq x, y 97 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3 98 ret i1 %tmp.4 99} 100 101%S = type { i32, [ 100 x i32] } 102 103define <2 x i1> @test6(<2 x i32> %X, <2 x %S*> %P) nounwind { 104; CHECK-LABEL: @test6( 105; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 1073741823, i32 1073741823> 106; CHECK-NEXT: ret <2 x i1> [[C]] 107; 108 %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i32> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i32> %X 109 %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 0, i32 0> 110 %C = icmp eq <2 x i32*> %A, %B 111 ret <2 x i1> %C 112} 113 114@G = external global [3 x i8] 115define i8* @test7(i16 %Idx) { 116; CHECK-LABEL: @test7( 117; CHECK-NEXT: [[ZE_IDX:%.*]] = zext i16 [[IDX:%.*]] to i32 118; CHECK-NEXT: [[TMP:%.*]] = getelementptr [3 x i8], [3 x i8]* @G, i32 0, i32 [[ZE_IDX]] 119; CHECK-NEXT: ret i8* [[TMP]] 120; 121 %ZE_Idx = zext i16 %Idx to i32 122 %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i32 %ZE_Idx 123 ret i8* %tmp 124} 125 126 127; Test folding of constantexpr geps into normal geps. 128@Array = external global [40 x i32] 129define i32 *@test8(i32 %X) { 130; CHECK-LABEL: @test8( 131; CHECK-NEXT: [[A:%.*]] = getelementptr [40 x i32], [40 x i32]* @Array, i32 0, i32 [[X:%.*]] 132; CHECK-NEXT: ret i32* [[A]] 133; 134 %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i32 0, i32 0), i32 %X 135 ret i32* %A 136} 137 138define i32 *@test9(i32 *%base, i8 %ind) { 139; CHECK-LABEL: @test9( 140; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[IND:%.*]] to i32 141; CHECK-NEXT: [[RES:%.*]] = getelementptr i32, i32* [[BASE:%.*]], i32 [[TMP1]] 142; CHECK-NEXT: ret i32* [[RES]] 143; 144 %res = getelementptr i32, i32 *%base, i8 %ind 145 ret i32* %res 146} 147 148define i32 @test10() { 149; CHECK-LABEL: @test10( 150; CHECK-NEXT: ret i32 8 151; 152 %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1 153 %B = ptrtoint double* %A to i32 154 ret i32 %B 155} 156