1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -enable-load-pre -enable-pre -passes=lcssa,gvn -S < %s | FileCheck %s
4 declare void @side_effect() nofree
5 declare i1 @side_effect_cond() nofree
6 declare void @may_free_memory()
8 declare i32 @personality_function()
10 ; We can PRE the load from gc-managed memory away from the hot path.
11 define i32 @test_load_on_cold_path_gc(ptr addrspace(1) %p) gc "statepoint-example" personality ptr @"personality_function" {
12 ; CHECK-LABEL: @test_load_on_cold_path_gc(
14 ; CHECK-NEXT: [[X_PRE1:%.*]] = load i32, ptr addrspace(1) [[P:%.*]], align 4
15 ; CHECK-NEXT: br label [[LOOP:%.*]]
17 ; CHECK-NEXT: [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
18 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
19 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
20 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
22 ; CHECK-NEXT: br label [[BACKEDGE]]
24 ; CHECK-NEXT: call void @may_free_memory()
25 ; CHECK-NEXT: [[X_PRE:%.*]] = load i32, ptr addrspace(1) [[P]], align 4
26 ; CHECK-NEXT: br label [[BACKEDGE]]
28 ; CHECK-NEXT: [[X2]] = phi i32 [ [[X_PRE]], [[COLD_PATH]] ], [ [[X]], [[HOT_PATH]] ]
29 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
30 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
31 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
33 ; CHECK-NEXT: ret i32 [[X]]
39 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
40 %x = load i32, ptr addrspace(1) %p
41 %cond = icmp ne i32 %x, 0
42 br i1 %cond, label %hot_path, label %cold_path
48 call void @may_free_memory()
52 %iv.next = add i32 %iv, %x
53 %loop.cond = icmp ult i32 %iv.next, 1000
54 br i1 %loop.cond, label %loop, label %exit
60 ; Do not PRE here because a loop-variant pointer.
61 define i32 @test_load_on_cold_path_gc_variant_neg(ptr addrspace(1) %pp) gc "statepoint-example" personality ptr @"personality_function" {
62 ; CHECK-LABEL: @test_load_on_cold_path_gc_variant_neg(
64 ; CHECK-NEXT: br label [[LOOP:%.*]]
66 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
67 ; CHECK-NEXT: [[P:%.*]] = load volatile ptr addrspace(1), ptr addrspace(1) [[PP:%.*]], align 8
68 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr addrspace(1) [[P]], align 4
69 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
70 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
72 ; CHECK-NEXT: br label [[BACKEDGE]]
74 ; CHECK-NEXT: call void @may_free_memory()
75 ; CHECK-NEXT: br label [[BACKEDGE]]
77 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
78 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
79 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
81 ; CHECK-NEXT: ret i32 [[X]]
87 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
88 %p = load volatile ptr addrspace(1), ptr addrspace(1) %pp
89 %x = load i32, ptr addrspace(1) %p
90 %cond = icmp ne i32 %x, 0
91 br i1 %cond, label %hot_path, label %cold_path
97 call void @may_free_memory()
101 %iv.next = add i32 %iv, %x
102 %loop.cond = icmp ult i32 %iv.next, 1000
103 br i1 %loop.cond, label %loop, label %exit
110 ; TODO: We can PRE the load away from the hot path.
111 define i32 @test_load_on_cold_path(ptr %p) {
112 ; CHECK-LABEL: @test_load_on_cold_path(
114 ; CHECK-NEXT: br label [[LOOP:%.*]]
116 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
117 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
118 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
119 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
121 ; CHECK-NEXT: br label [[BACKEDGE]]
123 ; CHECK-NEXT: call void @side_effect() #[[ATTR0:[0-9]+]]
124 ; CHECK-NEXT: br label [[BACKEDGE]]
126 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
127 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
128 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
130 ; CHECK-NEXT: ret i32 [[X]]
136 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
137 %x = load i32, ptr %p
138 %cond = icmp ne i32 %x, 0
139 br i1 %cond, label %hot_path, label %cold_path
145 call void @side_effect() nofree
149 %iv.next = add i32 %iv, %x
150 %loop.cond = icmp ult i32 %iv.next, 1000
151 br i1 %loop.cond, label %loop, label %exit
157 ; We should NOT PRE here because the function on the cold path may possibly
158 ; free the memory under the pointer.
159 define i32 @test_load_on_cold_path_may_free_memory_neg(ptr %p) {
160 ; CHECK-LABEL: @test_load_on_cold_path_may_free_memory_neg(
162 ; CHECK-NEXT: br label [[LOOP:%.*]]
164 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
165 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
166 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
167 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
169 ; CHECK-NEXT: br label [[BACKEDGE]]
171 ; CHECK-NEXT: call void @may_free_memory()
172 ; CHECK-NEXT: br label [[BACKEDGE]]
174 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
175 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
176 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
178 ; CHECK-NEXT: ret i32 [[X]]
184 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
185 %x = load i32, ptr %p
186 %cond = icmp ne i32 %x, 0
187 br i1 %cond, label %hot_path, label %cold_path
193 call void @may_free_memory()
197 %iv.next = add i32 %iv, %x
198 %loop.cond = icmp ult i32 %iv.next, 1000
199 br i1 %loop.cond, label %loop, label %exit
205 declare void @may_modify_or_free_pointer(ptr %p)
207 ; TODO: Despite the fact that the function may free memory in general, it
208 ; cannot free memory allocated by alloca.
209 define i32 @test_load_on_cold_path_may_free_memory_alloca() {
210 ; CHECK-LABEL: @test_load_on_cold_path_may_free_memory_alloca(
212 ; CHECK-NEXT: [[P:%.*]] = alloca i32, align 4
213 ; CHECK-NEXT: call void @may_modify_or_free_pointer(ptr [[P]])
214 ; CHECK-NEXT: br label [[LOOP:%.*]]
216 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
217 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P]], align 4
218 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
219 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
221 ; CHECK-NEXT: br label [[BACKEDGE]]
223 ; CHECK-NEXT: call void @may_modify_or_free_pointer(ptr [[P]])
224 ; CHECK-NEXT: br label [[BACKEDGE]]
226 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
227 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
228 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
230 ; CHECK-NEXT: ret i32 [[X]]
234 call void @may_modify_or_free_pointer(ptr %p)
238 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
239 %x = load i32, ptr %p
240 %cond = icmp ne i32 %x, 0
241 br i1 %cond, label %hot_path, label %cold_path
247 call void @may_modify_or_free_pointer(ptr %p)
251 %iv.next = add i32 %iv, %x
252 %loop.cond = icmp ult i32 %iv.next, 1000
253 br i1 %loop.cond, label %loop, label %exit
260 ; PRE here is meaningless, so we should not do it.
261 define i32 @test_load_on_both_paths(ptr %p) {
262 ; CHECK-LABEL: @test_load_on_both_paths(
264 ; CHECK-NEXT: br label [[LOOP:%.*]]
266 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
267 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
268 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
269 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
271 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
272 ; CHECK-NEXT: br label [[BACKEDGE]]
274 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
275 ; CHECK-NEXT: br label [[BACKEDGE]]
277 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
278 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
279 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
281 ; CHECK-NEXT: ret i32 [[X]]
287 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
288 %x = load i32, ptr %p
289 %cond = icmp ne i32 %x, 0
290 br i1 %cond, label %hot_path, label %cold_path
293 call void @side_effect() nofree
297 call void @side_effect() nofree
301 %iv.next = add i32 %iv, %x
302 %loop.cond = icmp ult i32 %iv.next, 1000
303 br i1 %loop.cond, label %loop, label %exit
310 ; We could PRE here, but it doesn't seem very profitable.
311 define i32 @test_load_on_backedge(ptr %p) {
312 ; CHECK-LABEL: @test_load_on_backedge(
314 ; CHECK-NEXT: br label [[LOOP:%.*]]
316 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
317 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
318 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
319 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
321 ; CHECK-NEXT: br label [[BACKEDGE]]
323 ; CHECK-NEXT: br label [[BACKEDGE]]
325 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
326 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
327 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
328 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
330 ; CHECK-NEXT: ret i32 [[X]]
336 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
337 %x = load i32, ptr %p
338 %cond = icmp ne i32 %x, 0
339 br i1 %cond, label %hot_path, label %cold_path
348 %iv.next = add i32 %iv, %x
349 call void @side_effect() nofree
350 %loop.cond = icmp ult i32 %iv.next, 1000
351 br i1 %loop.cond, label %loop, label %exit
357 ; TODO: We can PRE via splitting of the critical edge in the cold path.
358 define i32 @test_load_on_exiting_cold_path_01(ptr %p) {
359 ; CHECK-LABEL: @test_load_on_exiting_cold_path_01(
361 ; CHECK-NEXT: br label [[LOOP:%.*]]
363 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
364 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
365 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
366 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
368 ; CHECK-NEXT: br label [[BACKEDGE]]
370 ; CHECK-NEXT: [[SIDE_COND:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
371 ; CHECK-NEXT: br i1 [[SIDE_COND]], label [[BACKEDGE]], label [[COLD_EXIT:%.*]]
373 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
374 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
375 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
377 ; CHECK-NEXT: ret i32 [[X]]
379 ; CHECK-NEXT: ret i32 -1
385 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
386 %x = load i32, ptr %p
387 %cond = icmp ne i32 %x, 0
388 br i1 %cond, label %hot_path, label %cold_path
394 %side_cond = call i1 @side_effect_cond() nofree
395 br i1 %side_cond, label %backedge, label %cold_exit
398 %iv.next = add i32 %iv, %x
399 %loop.cond = icmp ult i32 %iv.next, 1000
400 br i1 %loop.cond, label %loop, label %exit
409 ; TODO: We can PRE via splitting of the critical edge in the cold path.
410 define i32 @test_load_on_exiting_cold_path_02(ptr %p) gc "statepoint-example" personality ptr @personality_function {
411 ; CHECK-LABEL: @test_load_on_exiting_cold_path_02(
413 ; CHECK-NEXT: br label [[LOOP:%.*]]
415 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
416 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
417 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
418 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
420 ; CHECK-NEXT: br label [[BACKEDGE]]
422 ; CHECK-NEXT: invoke void @side_effect()
423 ; CHECK-NEXT: to label [[BACKEDGE]] unwind label [[COLD_EXIT:%.*]]
425 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
426 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
427 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
429 ; CHECK-NEXT: ret i32 [[X]]
431 ; CHECK-NEXT: [[LANDING_PAD:%.*]] = landingpad token
432 ; CHECK-NEXT: cleanup
433 ; CHECK-NEXT: ret i32 -1
439 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
440 %x = load i32, ptr %p
441 %cond = icmp ne i32 %x, 0
442 br i1 %cond, label %hot_path, label %cold_path
448 invoke void @side_effect() to label %backedge unwind label %cold_exit
451 %iv.next = add i32 %iv, %x
452 %loop.cond = icmp ult i32 %iv.next, 1000
453 br i1 %loop.cond, label %loop, label %exit
459 %landing_pad = landingpad token
464 ; Make sure we do not insert load into both cold path & backedge.
465 define i32 @test_load_on_cold_path_and_backedge(ptr %p) {
466 ; CHECK-LABEL: @test_load_on_cold_path_and_backedge(
468 ; CHECK-NEXT: br label [[LOOP:%.*]]
470 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
471 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
472 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
473 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
475 ; CHECK-NEXT: br label [[BACKEDGE]]
477 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
478 ; CHECK-NEXT: br label [[BACKEDGE]]
480 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
481 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
482 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
483 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
485 ; CHECK-NEXT: ret i32 [[X]]
491 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
492 %x = load i32, ptr %p
493 %cond = icmp ne i32 %x, 0
494 br i1 %cond, label %hot_path, label %cold_path
500 call void @side_effect() nofree
504 %iv.next = add i32 %iv, %x
505 %loop.cond = icmp ult i32 %iv.next, 1000
506 call void @side_effect() nofree
507 br i1 %loop.cond, label %loop, label %exit
513 ; TODO: We can PRE the load away from the hot path. Make sure we only insert 1 load.
514 define i32 @test_load_multi_block_cold_path(ptr %p) {
515 ; CHECK-LABEL: @test_load_multi_block_cold_path(
517 ; CHECK-NEXT: br label [[LOOP:%.*]]
519 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
520 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
521 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
522 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH_1:%.*]]
524 ; CHECK-NEXT: br label [[BACKEDGE]]
525 ; CHECK: cold_path.1:
526 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
527 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
528 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
529 ; CHECK-NEXT: br label [[BACKEDGE]]
531 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
532 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
533 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
534 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
536 ; CHECK-NEXT: ret i32 [[X]]
542 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
543 %x = load i32, ptr %p
544 %cond = icmp ne i32 %x, 0
545 br i1 %cond, label %hot_path, label %cold_path.1
551 call void @side_effect() nofree
552 br label %cold_path.2
555 call void @side_effect() nofree
556 br label %cold_path.3
559 call void @side_effect() nofree
563 %iv.next = add i32 %iv, %x
564 %loop.cond = icmp ult i32 %iv.next, 1000
565 call void @side_effect() nofree
566 br i1 %loop.cond, label %loop, label %exit
572 ; TODO: We can PRE via splitting of the critical edge in the cold path. Make sure we only insert 1 load.
573 define i32 @test_load_on_multi_exiting_cold_path(ptr %p) {
574 ; CHECK-LABEL: @test_load_on_multi_exiting_cold_path(
576 ; CHECK-NEXT: br label [[LOOP:%.*]]
578 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
579 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
580 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
581 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH_1:%.*]]
583 ; CHECK-NEXT: br label [[BACKEDGE]]
584 ; CHECK: cold_path.1:
585 ; CHECK-NEXT: [[SIDE_COND_1:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
586 ; CHECK-NEXT: br i1 [[SIDE_COND_1]], label [[COLD_PATH_2:%.*]], label [[COLD_EXIT:%.*]]
587 ; CHECK: cold_path.2:
588 ; CHECK-NEXT: [[SIDE_COND_2:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
589 ; CHECK-NEXT: br i1 [[SIDE_COND_2]], label [[COLD_PATH_3:%.*]], label [[COLD_EXIT]]
590 ; CHECK: cold_path.3:
591 ; CHECK-NEXT: [[SIDE_COND_3:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
592 ; CHECK-NEXT: br i1 [[SIDE_COND_3]], label [[BACKEDGE]], label [[COLD_EXIT]]
594 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
595 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
596 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
598 ; CHECK-NEXT: ret i32 [[X]]
600 ; CHECK-NEXT: ret i32 -1
606 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
607 %x = load i32, ptr %p
608 %cond = icmp ne i32 %x, 0
609 br i1 %cond, label %hot_path, label %cold_path.1
615 %side_cond.1 = call i1 @side_effect_cond() nofree
616 br i1 %side_cond.1, label %cold_path.2, label %cold_exit
619 %side_cond.2 = call i1 @side_effect_cond() nofree
620 br i1 %side_cond.2, label %cold_path.3, label %cold_exit
623 %side_cond.3 = call i1 @side_effect_cond() nofree
624 br i1 %side_cond.3, label %backedge, label %cold_exit
627 %iv.next = add i32 %iv, %x
628 %loop.cond = icmp ult i32 %iv.next, 1000
629 br i1 %loop.cond, label %loop, label %exit
638 ; TODO: PRE via splittinga backedge in the cold loop. Make sure we don't insert a load into an inner loop.
639 define i32 @test_inner_loop(ptr %p) {
640 ; CHECK-LABEL: @test_inner_loop(
642 ; CHECK-NEXT: br label [[LOOP:%.*]]
644 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
645 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
646 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
647 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
649 ; CHECK-NEXT: br label [[BACKEDGE]]
651 ; CHECK-NEXT: br label [[INNER_LOOP:%.*]]
653 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
654 ; CHECK-NEXT: br i1 undef, label [[INNER_LOOP]], label [[BACKEDGE]]
656 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
657 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
658 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
660 ; CHECK-NEXT: ret i32 [[X]]
666 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
667 %x = load i32, ptr %p
668 %cond = icmp ne i32 %x, 0
669 br i1 %cond, label %hot_path, label %cold_path
678 call void @side_effect() nofree
679 br i1 undef, label %inner_loop, label %backedge
682 %iv.next = add i32 %iv, %x
683 %loop.cond = icmp ult i32 %iv.next, 1000
684 br i1 %loop.cond, label %loop, label %exit
690 ; TODO: We can PRE here, but profitablility depends on frequency of cold blocks. Conservatively, we should not do it unless there is a reason.
691 define i32 @test_multiple_cold_paths(ptr %p) {
692 ; CHECK-LABEL: @test_multiple_cold_paths(
694 ; CHECK-NEXT: br label [[LOOP:%.*]]
696 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
697 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
698 ; CHECK-NEXT: [[COND_1:%.*]] = icmp ne i32 [[X]], 0
699 ; CHECK-NEXT: br i1 [[COND_1]], label [[HOT_PATH_1:%.*]], label [[COLD_PATH_1:%.*]]
701 ; CHECK-NEXT: br label [[DOM_1:%.*]]
702 ; CHECK: cold_path.1:
703 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
704 ; CHECK-NEXT: br label [[DOM_1]]
706 ; CHECK-NEXT: [[COND_2:%.*]] = icmp ne i32 [[X]], 1
707 ; CHECK-NEXT: br i1 [[COND_2]], label [[HOT_PATH_2:%.*]], label [[COLD_PATH_2:%.*]]
709 ; CHECK-NEXT: br label [[DOM_2:%.*]]
710 ; CHECK: cold_path.2:
711 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
712 ; CHECK-NEXT: br label [[DOM_2]]
714 ; CHECK-NEXT: [[COND_3:%.*]] = icmp ne i32 [[X]], 2
715 ; CHECK-NEXT: br i1 [[COND_3]], label [[HOT_PATH_3:%.*]], label [[COLD_PATH_3:%.*]]
717 ; CHECK-NEXT: br label [[BACKEDGE]]
718 ; CHECK: cold_path.3:
719 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
720 ; CHECK-NEXT: br label [[BACKEDGE]]
722 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
723 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
724 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
726 ; CHECK-NEXT: ret i32 [[X]]
732 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
733 %x = load i32, ptr %p
734 %cond.1 = icmp ne i32 %x, 0
735 br i1 %cond.1, label %hot_path.1, label %cold_path.1
741 call void @side_effect() nofree
745 %cond.2 = icmp ne i32 %x, 1
746 br i1 %cond.2, label %hot_path.2, label %cold_path.2
752 call void @side_effect() nofree
756 %cond.3 = icmp ne i32 %x, 2
757 br i1 %cond.3, label %hot_path.3, label %cold_path.3
763 call void @side_effect() nofree
767 %iv.next = add i32 %iv, %x
768 %loop.cond = icmp ult i32 %iv.next, 1000
769 br i1 %loop.cond, label %loop, label %exit
775 ; TODO: We can PRE via split of critical edge.
776 define i32 @test_side_exit_after_merge(ptr %p) {
777 ; CHECK-LABEL: @test_side_exit_after_merge(
779 ; CHECK-NEXT: br label [[LOOP:%.*]]
781 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
782 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
783 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[X]], 0
784 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
786 ; CHECK-NEXT: br label [[BACKEDGE]]
788 ; CHECK-NEXT: [[COND_1:%.*]] = icmp ne i32 [[IV]], 1
789 ; CHECK-NEXT: br i1 [[COND_1]], label [[DO_CALL:%.*]], label [[SIDE_EXITING:%.*]]
791 ; CHECK-NEXT: [[SIDE_COND:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
792 ; CHECK-NEXT: br label [[SIDE_EXITING]]
793 ; CHECK: side_exiting:
794 ; CHECK-NEXT: [[SIDE_COND_PHI:%.*]] = phi i1 [ [[SIDE_COND]], [[DO_CALL]] ], [ true, [[COLD_PATH]] ]
795 ; CHECK-NEXT: br i1 [[SIDE_COND_PHI]], label [[BACKEDGE]], label [[COLD_EXIT:%.*]]
797 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
798 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
799 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
801 ; CHECK-NEXT: ret i32 [[X]]
803 ; CHECK-NEXT: ret i32 -1
809 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
810 %x = load i32, ptr %p
811 %cond = icmp ne i32 %x, 0
812 br i1 %cond, label %hot_path, label %cold_path
818 %cond.1 = icmp ne i32 %iv, 1
819 br i1 %cond.1, label %do_call, label %side_exiting
822 %side_cond = call i1 @side_effect_cond() nofree
823 br label %side_exiting
826 %side_cond_phi = phi i1 [%side_cond, %do_call], [true, %cold_path]
827 br i1 %side_cond_phi, label %backedge, label %cold_exit
830 %iv.next = add i32 %iv, %x
831 %loop.cond = icmp ult i32 %iv.next, 1000
832 br i1 %loop.cond, label %loop, label %exit
841 declare void @llvm.experimental.guard(i1, ...)
843 define i32 @test_guard_1(ptr %p, i32 %g) {
844 ; CHECK-LABEL: @test_guard_1(
846 ; CHECK-NEXT: br label [[LOOP:%.*]]
848 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
849 ; CHECK-NEXT: [[GUARD_COND:%.*]] = icmp ne i32 [[IV]], [[G:%.*]]
850 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[GUARD_COND]]) [ "deopt"() ]
851 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
852 ; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[X]], 100
853 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
855 ; CHECK-NEXT: br label [[BACKEDGE]]
857 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
858 ; CHECK-NEXT: br label [[BACKEDGE]]
860 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
861 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
862 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
864 ; CHECK-NEXT: ret i32 [[X]]
870 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
871 %guard_cond = icmp ne i32 %iv, %g
872 call void(i1, ...) @llvm.experimental.guard(i1 %guard_cond) [ "deopt"() ]
873 %x = load i32, ptr %p
874 %cond = icmp ult i32 %x, 100
875 br i1 %cond, label %hot_path, label %cold_path
881 call void @side_effect() nofree
885 %iv.next = add i32 %iv, %x
886 %loop.cond = icmp ult i32 %iv.next, 1000
887 br i1 %loop.cond, label %loop, label %exit
893 define i32 @test_guard_2(ptr %p, i32 %g) {
894 ; CHECK-LABEL: @test_guard_2(
896 ; CHECK-NEXT: br label [[LOOP:%.*]]
898 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
899 ; CHECK-NEXT: [[GUARD_COND:%.*]] = icmp ne i32 [[IV]], [[G:%.*]]
900 ; CHECK-NEXT: [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
901 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[GUARD_COND]]) [ "deopt"() ]
902 ; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[X]], 100
903 ; CHECK-NEXT: br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
905 ; CHECK-NEXT: br label [[BACKEDGE]]
907 ; CHECK-NEXT: call void @side_effect() #[[ATTR0]]
908 ; CHECK-NEXT: br label [[BACKEDGE]]
910 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], [[X]]
911 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
912 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
914 ; CHECK-NEXT: ret i32 [[X]]
920 %iv = phi i32 [ 0, %entry], [%iv.next, %backedge]
921 %guard_cond = icmp ne i32 %iv, %g
922 %x = load i32, ptr %p
923 call void(i1, ...) @llvm.experimental.guard(i1 %guard_cond) [ "deopt"() ]
924 %cond = icmp ult i32 %x, 100
925 br i1 %cond, label %hot_path, label %cold_path
931 call void @side_effect() nofree
935 %iv.next = add i32 %iv, %x
936 %loop.cond = icmp ult i32 %iv.next, 1000
937 br i1 %loop.cond, label %loop, label %exit