1 ; RUN: opt < %s -slsr -gvn -S | FileCheck %s
2 ; RUN: opt < %s -passes='slsr,gvn' -S | FileCheck %s
4 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64-p:64:64:64-p1:32:32:32"
16 define void @slsr_gep(i32* %input, i64 %s) {
17 ; CHECK-LABEL: @slsr_gep(
19 %p0 = getelementptr inbounds i32, i32* %input, i64 0
20 call void @foo(i32* %p0)
23 %p1 = getelementptr inbounds i32, i32* %input, i64 %s
24 ; CHECK: %p1 = getelementptr inbounds i32, i32* %input, i64 %s
25 call void @foo(i32* %p1)
28 %s2 = shl nsw i64 %s, 1
29 %p2 = getelementptr inbounds i32, i32* %input, i64 %s2
30 ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %s
31 call void @foo(i32* %p2)
37 ; foo(input[(long)s]);
38 ; foo(input[(long)(s * 2)]);
46 define void @slsr_gep_sext(i32* %input, i32 %s) {
47 ; CHECK-LABEL: @slsr_gep_sext(
49 %p0 = getelementptr inbounds i32, i32* %input, i64 0
50 call void @foo(i32* %p0)
53 %t = sext i32 %s to i64
54 %p1 = getelementptr inbounds i32, i32* %input, i64 %t
55 ; CHECK: %p1 = getelementptr inbounds i32, i32* %input, i64 %t
56 call void @foo(i32* %p1)
59 %s2 = shl nsw i32 %s, 1
60 %t2 = sext i32 %s2 to i64
61 %p2 = getelementptr inbounds i32, i32* %input, i64 %t2
62 ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %t
63 call void @foo(i32* %p2)
70 ; foo(input[s * 2][t]);
71 ; foo(input[s * 3][t]);
79 define void @slsr_gep_2d([10 x [5 x i32]]* %input, i64 %s, i64 %t) {
80 ; CHECK-LABEL: @slsr_gep_2d(
82 %p0 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s, i64 %t
83 call void @foo(i32* %p0)
85 ; v1 = input[s * 2][t];
86 %s2 = shl nsw i64 %s, 1
87 ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 5
88 %p1 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s2, i64 %t
89 ; CHECK: %p1 = getelementptr inbounds i32, i32* %p0, i64 [[BUMP]]
90 call void @foo(i32* %p1)
92 ; v3 = input[s * 3][t];
93 %s3 = mul nsw i64 %s, 3
94 %p2 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s3, i64 %t
95 ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 [[BUMP]]
96 call void @foo(i32* %p2)
101 %struct.S = type <{ i64, i32 }>
103 ; In this case, the bump
104 ; = (char *)&input[s * 2][t].f1 - (char *)&input[s][t].f1
106 ; which may not be divisible by typeof(input[s][t].f1) = 8. Therefore, we
107 ; rewrite the candidates using byte offset instead of index offset as in
109 define void @slsr_gep_uglygep([10 x [5 x %struct.S]]* %input, i64 %s, i64 %t) {
110 ; CHECK-LABEL: @slsr_gep_uglygep(
111 ; v0 = input[s][t].f1;
112 %p0 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s, i64 %t, i32 0
113 call void @bar(i64* %p0)
115 ; v1 = input[s * 2][t].f1;
116 %s2 = shl nsw i64 %s, 1
117 ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 60
118 %p1 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s2, i64 %t, i32 0
119 ; CHECK: getelementptr inbounds i8, i8* %{{[0-9]+}}, i64 [[BUMP]]
120 call void @bar(i64* %p1)
122 ; v2 = input[s * 3][t].f1;
123 %s3 = mul nsw i64 %s, 3
124 %p2 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s3, i64 %t, i32 0
125 ; CHECK: getelementptr inbounds i8, i8* %{{[0-9]+}}, i64 [[BUMP]]
126 call void @bar(i64* %p2)
131 define void @slsr_out_of_bounds_gep(i32* %input, i32 %s) {
132 ; CHECK-LABEL: @slsr_out_of_bounds_gep(
134 %p0 = getelementptr i32, i32* %input, i64 0
135 call void @foo(i32* %p0)
137 ; v1 = input[(long)s];
138 %t = sext i32 %s to i64
139 %p1 = getelementptr i32, i32* %input, i64 %t
140 ; CHECK: %p1 = getelementptr i32, i32* %input, i64 %t
141 call void @foo(i32* %p1)
143 ; v2 = input[(long)(s * 2)];
144 %s2 = shl nsw i32 %s, 1
145 %t2 = sext i32 %s2 to i64
146 %p2 = getelementptr i32, i32* %input, i64 %t2
147 ; CHECK: %p2 = getelementptr i32, i32* %p1, i64 %t
148 call void @foo(i32* %p2)
153 define void @slsr_gep_128bit_index(i32* %input, i128 %s) {
154 ; CHECK-LABEL: @slsr_gep_128bit_index(
156 %p0 = getelementptr inbounds i32, i32* %input, i128 0
157 call void @foo(i32* %p0)
159 ; p1 = &input[s << 125]
160 %s125 = shl nsw i128 %s, 125
161 %p1 = getelementptr inbounds i32, i32* %input, i128 %s125
162 ; CHECK: %p1 = getelementptr inbounds i32, i32* %input, i128 %s125
163 call void @foo(i32* %p1)
165 ; p2 = &input[s << 126]
166 %s126 = shl nsw i128 %s, 126
167 %p2 = getelementptr inbounds i32, i32* %input, i128 %s126
168 ; CHECK: %p2 = getelementptr inbounds i32, i32* %input, i128 %s126
169 call void @foo(i32* %p2)
174 define void @slsr_gep_32bit_pointer(i32 addrspace(1)* %input, i64 %s) {
175 ; CHECK-LABEL: @slsr_gep_32bit_pointer(
177 %p1 = getelementptr inbounds i32, i32 addrspace(1)* %input, i64 %s
178 call void @baz(i32 addrspace(1)* %p1)
181 %s2 = mul nsw i64 %s, 2
182 %p2 = getelementptr inbounds i32, i32 addrspace(1)* %input, i64 %s2
183 ; %s2 is wider than the pointer size of addrspace(1), so do not factor it.
184 ; CHECK: %p2 = getelementptr inbounds i32, i32 addrspace(1)* %input, i64 %s2
185 call void @baz(i32 addrspace(1)* %p2)
190 declare void @foo(i32*)
191 declare void @bar(i64*)
192 declare void @baz(i32 addrspace(1)*)