1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2 ; RUN: opt -S -passes=licm < %s | FileCheck %s --check-prefixes=CHECK,SPEC
3 ; RUN: opt -S -passes='licm<no-allowspeculation>' < %s | FileCheck %s --check-prefixes=CHECK,NOSPEC
10 define void @only_one_inbounds(ptr %ptr, i1 %c, i32 %arg) {
11 ; CHECK-LABEL: define void @only_one_inbounds
12 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i32 [[ARG:%.*]]) {
14 ; CHECK-NEXT: [[ARG_EXT:%.*]] = zext i32 [[ARG]] to i64
15 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG_EXT]]
16 ; CHECK-NEXT: br label [[LOOP:%.*]]
18 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @get.i32()
19 ; CHECK-NEXT: [[VAL_EXT:%.*]] = zext i32 [[VAL]] to i64
20 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[INVARIANT_GEP]], i64 [[VAL_EXT]]
21 ; CHECK-NEXT: call void @use(ptr [[GEP]])
22 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
24 ; CHECK-NEXT: ret void
27 %arg.ext = zext i32 %arg to i64
31 %val = call i32 @get.i32()
32 %val.ext = zext i32 %val to i64
33 %ptr2 = getelementptr inbounds i8, ptr %ptr, i64 %val.ext
34 %ptr3 = getelementptr i8, ptr %ptr2, i64 %arg.ext
35 call void @use(ptr %ptr3)
36 br i1 %c, label %loop, label %exit
42 define void @both_inbounds_one_neg(ptr %ptr, i1 %c) {
43 ; CHECK-LABEL: define void @both_inbounds_one_neg
44 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]]) {
46 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr i8, ptr [[PTR]], i64 -1
47 ; CHECK-NEXT: br label [[LOOP:%.*]]
49 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @get.i32()
50 ; CHECK-NEXT: [[VAL_EXT:%.*]] = zext i32 [[VAL]] to i64
51 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[INVARIANT_GEP]], i64 [[VAL_EXT]]
52 ; CHECK-NEXT: call void @use(ptr [[GEP]])
53 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
55 ; CHECK-NEXT: ret void
61 %val = call i32 @get.i32()
62 %val.ext = zext i32 %val to i64
63 %ptr2 = getelementptr inbounds i8, ptr %ptr, i64 %val.ext
64 %ptr3 = getelementptr i8, ptr %ptr2, i64 -1
65 call void @use(ptr %ptr3)
66 br i1 %c, label %loop, label %exit
72 define void @both_inbounds_pos(ptr %ptr, i1 %c) {
73 ; CHECK-LABEL: define void @both_inbounds_pos
74 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]]) {
76 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 1
77 ; CHECK-NEXT: br label [[LOOP:%.*]]
79 ; CHECK-NEXT: [[VAL:%.*]] = call i32 @get.i32()
80 ; CHECK-NEXT: [[VAL_EXT:%.*]] = zext i32 [[VAL]] to i64
81 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[INVARIANT_GEP]], i64 [[VAL_EXT]]
82 ; CHECK-NEXT: call void @use(ptr [[GEP]])
83 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
85 ; CHECK-NEXT: ret void
91 %val = call i32 @get.i32()
92 %val.ext = zext i32 %val to i64
93 %ptr2 = getelementptr inbounds i8, ptr %ptr, i64 %val.ext
94 %ptr3 = getelementptr inbounds i8, ptr %ptr2, i64 1
95 call void @use(ptr %ptr3)
96 br i1 %c, label %loop, label %exit
102 define void @different_elem_types(ptr %ptr, i1 %c, i64 %arg) {
103 ; CHECK-LABEL: define void @different_elem_types
104 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG:%.*]]) {
106 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr i64, ptr [[PTR]], i64 [[ARG]]
107 ; CHECK-NEXT: br label [[LOOP:%.*]]
109 ; CHECK-NEXT: [[VAL:%.*]] = call i64 @get.i64()
110 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[INVARIANT_GEP]], i64 [[VAL]]
111 ; CHECK-NEXT: call void @use(ptr [[GEP]])
112 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
114 ; CHECK-NEXT: ret void
120 %val = call i64 @get.i64()
121 %ptr2 = getelementptr i32, ptr %ptr, i64 %val
122 %ptr3 = getelementptr i64, ptr %ptr2, i64 %arg
123 call void @use(ptr %ptr3)
124 br i1 %c, label %loop, label %exit
130 define void @different_index_types(ptr %ptr, i1 %c, i32 %arg) {
131 ; CHECK-LABEL: define void @different_index_types
132 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i32 [[ARG:%.*]]) {
134 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr i8, ptr [[PTR]], i32 [[ARG]]
135 ; CHECK-NEXT: br label [[LOOP:%.*]]
137 ; CHECK-NEXT: [[VAL:%.*]] = call i64 @get.i64()
138 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[INVARIANT_GEP]], i64 [[VAL]]
139 ; CHECK-NEXT: call void @use(ptr [[GEP]])
140 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
142 ; CHECK-NEXT: ret void
148 %val = call i64 @get.i64()
149 %ptr2 = getelementptr i8, ptr %ptr, i64 %val
150 %ptr3 = getelementptr i8, ptr %ptr2, i32 %arg
151 call void @use(ptr %ptr3)
152 br i1 %c, label %loop, label %exit
158 define void @different_index_count(ptr %ptr, i1 %c, i64 %arg1, i64 %arg2) {
159 ; CHECK-LABEL: define void @different_index_count
160 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
162 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr [0 x i8], ptr [[PTR]], i64 [[ARG1]], i64 [[ARG2]]
163 ; CHECK-NEXT: br label [[LOOP:%.*]]
165 ; CHECK-NEXT: [[VAL:%.*]] = call i64 @get.i64()
166 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[INVARIANT_GEP]], i64 [[VAL]]
167 ; CHECK-NEXT: call void @use(ptr [[GEP]])
168 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
170 ; CHECK-NEXT: ret void
176 %val = call i64 @get.i64()
177 %ptr2 = getelementptr i8, ptr %ptr, i64 %val
178 %ptr3 = getelementptr [0 x i8], ptr %ptr2, i64 %arg1, i64 %arg2
179 call void @use(ptr %ptr3)
180 br i1 %c, label %loop, label %exit
186 define void @src_has_extra_use(ptr %ptr, i1 %c, i64 %arg) {
187 ; CHECK-LABEL: define void @src_has_extra_use
188 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG:%.*]]) {
190 ; CHECK-NEXT: br label [[LOOP:%.*]]
192 ; CHECK-NEXT: [[VAL:%.*]] = call i64 @get.i64()
193 ; CHECK-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[VAL]]
194 ; CHECK-NEXT: call void @use(ptr [[PTR2]])
195 ; CHECK-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG]]
196 ; CHECK-NEXT: call void @use(ptr [[PTR3]])
197 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
199 ; CHECK-NEXT: ret void
205 %val = call i64 @get.i64()
206 %ptr2 = getelementptr i8, ptr %ptr, i64 %val
207 call void @use(ptr %ptr2)
208 %ptr3 = getelementptr i8, ptr %ptr2, i64 %arg
209 call void @use(ptr %ptr3)
210 br i1 %c, label %loop, label %exit
216 define void @src_already_invariant(ptr %ptr, i1 %c, i64 %arg1, i64 %arg2) {
217 ; CHECK-LABEL: define void @src_already_invariant
218 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
220 ; CHECK-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG1]]
221 ; CHECK-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG2]]
222 ; CHECK-NEXT: br label [[LOOP:%.*]]
224 ; CHECK-NEXT: call void @use(ptr [[PTR3]])
225 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
227 ; CHECK-NEXT: ret void
233 %ptr2 = getelementptr i8, ptr %ptr, i64 %arg1
234 %ptr3 = getelementptr i8, ptr %ptr2, i64 %arg2
235 call void @use(ptr %ptr3)
236 br i1 %c, label %loop, label %exit
242 define void @gep_idx_not_invariant(ptr %ptr, i1 %c) {
243 ; CHECK-LABEL: define void @gep_idx_not_invariant
244 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]]) {
246 ; CHECK-NEXT: br label [[LOOP:%.*]]
248 ; CHECK-NEXT: [[VAL1:%.*]] = call i64 @get.i64()
249 ; CHECK-NEXT: [[VAL2:%.*]] = call i64 @get.i64()
250 ; CHECK-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[VAL1]]
251 ; CHECK-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[VAL2]]
252 ; CHECK-NEXT: call void @use(ptr [[PTR3]])
253 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
255 ; CHECK-NEXT: ret void
261 %val1 = call i64 @get.i64()
262 %val2 = call i64 @get.i64()
263 %ptr2 = getelementptr i8, ptr %ptr, i64 %val1
264 %ptr3 = getelementptr i8, ptr %ptr2, i64 %val2
265 call void @use(ptr %ptr3)
266 br i1 %c, label %loop, label %exit
272 define void @src_ptr_not_invariant(i1 %c, i64 %arg) {
273 ; CHECK-LABEL: define void @src_ptr_not_invariant
274 ; CHECK-SAME: (i1 [[C:%.*]], i64 [[ARG:%.*]]) {
276 ; CHECK-NEXT: br label [[LOOP:%.*]]
278 ; CHECK-NEXT: [[VAL:%.*]] = call i64 @get.i64()
279 ; CHECK-NEXT: [[PTR:%.*]] = call ptr @get.ptr()
280 ; CHECK-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG]]
281 ; CHECK-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[VAL]]
282 ; CHECK-NEXT: call void @use(ptr [[PTR3]])
283 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
285 ; CHECK-NEXT: ret void
291 %val = call i64 @get.i64()
292 %ptr = call ptr @get.ptr()
293 %ptr2 = getelementptr i8, ptr %ptr, i64 %arg
294 %ptr3 = getelementptr i8, ptr %ptr2, i64 %val
295 call void @use(ptr %ptr3)
296 br i1 %c, label %loop, label %exit
302 define void @multiple_indices(ptr %ptr, i1 %c, i64 %arg1, i64 %arg2) {
303 ; CHECK-LABEL: define void @multiple_indices
304 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
306 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr [0 x i8], ptr [[PTR]], i64 [[ARG1]], i64 [[ARG2]]
307 ; CHECK-NEXT: br label [[LOOP:%.*]]
309 ; CHECK-NEXT: [[VAL1:%.*]] = call i64 @get.i64()
310 ; CHECK-NEXT: [[VAL2:%.*]] = call i64 @get.i64()
311 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr [0 x i8], ptr [[INVARIANT_GEP]], i64 [[VAL1]], i64 [[VAL2]]
312 ; CHECK-NEXT: call void @use(ptr [[GEP]])
313 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
315 ; CHECK-NEXT: ret void
321 %val1 = call i64 @get.i64()
322 %val2 = call i64 @get.i64()
323 %ptr2 = getelementptr [0 x i8], ptr %ptr, i64 %val1, i64 %val2
324 %ptr3 = getelementptr [0 x i8], ptr %ptr2, i64 %arg1, i64 %arg2
325 call void @use(ptr %ptr3)
326 br i1 %c, label %loop, label %exit
332 define void @multiple_indices_not_invariant(ptr %ptr, i1 %c, i64 %arg1) {
333 ; CHECK-LABEL: define void @multiple_indices_not_invariant
334 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG1:%.*]]) {
336 ; CHECK-NEXT: br label [[LOOP:%.*]]
338 ; CHECK-NEXT: [[VAL1:%.*]] = call i64 @get.i64()
339 ; CHECK-NEXT: [[VAL2:%.*]] = call i64 @get.i64()
340 ; CHECK-NEXT: [[VAL3:%.*]] = call i64 @get.i64()
341 ; CHECK-NEXT: [[PTR2:%.*]] = getelementptr [0 x i8], ptr [[PTR]], i64 [[VAL1]], i64 [[VAL2]]
342 ; CHECK-NEXT: [[PTR3:%.*]] = getelementptr [0 x i8], ptr [[PTR2]], i64 [[ARG1]], i64 [[VAL3]]
343 ; CHECK-NEXT: call void @use(ptr [[PTR3]])
344 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
346 ; CHECK-NEXT: ret void
352 %val1 = call i64 @get.i64()
353 %val2 = call i64 @get.i64()
354 %val3 = call i64 @get.i64()
355 %ptr2 = getelementptr [0 x i8], ptr %ptr, i64 %val1, i64 %val2
356 %ptr3 = getelementptr [0 x i8], ptr %ptr2, i64 %arg1, i64 %val3
357 call void @use(ptr %ptr3)
358 br i1 %c, label %loop, label %exit
364 define void @multiple_indices_very_invariant(ptr %ptr, i1 %c, i64 %arg1, i64 %arg2, i64 %arg3) {
365 ; CHECK-LABEL: define void @multiple_indices_very_invariant
366 ; CHECK-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]], i64 [[ARG3:%.*]]) {
368 ; CHECK-NEXT: [[INVARIANT_GEP:%.*]] = getelementptr [0 x i8], ptr [[PTR]], i64 [[ARG1]], i64 [[ARG2]]
369 ; CHECK-NEXT: br label [[LOOP:%.*]]
371 ; CHECK-NEXT: [[VAL1:%.*]] = call i64 @get.i64()
372 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr [0 x i8], ptr [[INVARIANT_GEP]], i64 [[ARG3]], i64 [[VAL1]]
373 ; CHECK-NEXT: call void @use(ptr [[GEP]])
374 ; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
376 ; CHECK-NEXT: ret void
382 %val1 = call i64 @get.i64()
383 %ptr2 = getelementptr [0 x i8], ptr %ptr, i64 %arg3, i64 %val1
384 %ptr3 = getelementptr [0 x i8], ptr %ptr2, i64 %arg1, i64 %arg2
385 call void @use(ptr %ptr3)
386 br i1 %c, label %loop, label %exit
392 define void @src_already_invariant_speculation(ptr %ptr, i1 %c, i1 %c2, i64 %arg1, i64 %arg2) {
393 ; SPEC-LABEL: define void @src_already_invariant_speculation
394 ; SPEC-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i1 [[C2:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
396 ; SPEC-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG1]]
397 ; SPEC-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG2]]
398 ; SPEC-NEXT: br label [[LOOP:%.*]]
400 ; SPEC-NEXT: br i1 [[C2]], label [[IF:%.*]], label [[LATCH:%.*]]
402 ; SPEC-NEXT: call void @use(ptr [[PTR3]])
403 ; SPEC-NEXT: br label [[LATCH]]
405 ; SPEC-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
407 ; SPEC-NEXT: ret void
409 ; NOSPEC-LABEL: define void @src_already_invariant_speculation
410 ; NOSPEC-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i1 [[C2:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
411 ; NOSPEC-NEXT: entry:
412 ; NOSPEC-NEXT: br label [[LOOP:%.*]]
414 ; NOSPEC-NEXT: br i1 [[C2]], label [[IF:%.*]], label [[LATCH:%.*]]
416 ; NOSPEC-NEXT: [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG1]]
417 ; NOSPEC-NEXT: [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG2]]
418 ; NOSPEC-NEXT: call void @use(ptr [[PTR3]])
419 ; NOSPEC-NEXT: br label [[LATCH]]
421 ; NOSPEC-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
423 ; NOSPEC-NEXT: ret void
429 br i1 %c2, label %if, label %latch
432 %ptr2 = getelementptr i8, ptr %ptr, i64 %arg1
433 %ptr3 = getelementptr i8, ptr %ptr2, i64 %arg2
434 call void @use(ptr %ptr3)
438 br i1 %c, label %loop, label %exit