1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S < %s -passes='loop(loop-rotate)' -loop-rotate-multi=true | FileCheck %s
4 ; Test loop rotation with multiple exits, some of them - deoptimizing.
5 ; We should end up with a latch which exit is non-deoptimizing, so we should rotate
8 declare i32 @llvm.experimental.deoptimize.i32(...)
10 define i32 @test_cond_with_one_deopt_exit(ptr nonnull %a, i64 %x) {
11 ; Rotation done twice.
12 ; Latch should be at the 2nd condition (for.cond2), exiting to %return.
14 ; CHECK-LABEL: @test_cond_with_one_deopt_exit(
16 ; CHECK-NEXT: [[VAL_A_IDX3:%.*]] = load i32, ptr %a, align 4
17 ; CHECK-NEXT: [[ZERO_CHECK4:%.*]] = icmp eq i32 [[VAL_A_IDX3]], 0
18 ; CHECK-NEXT: br i1 [[ZERO_CHECK4]], label %deopt.exit, label %for.cond2.lr.ph
19 ; CHECK: for.cond2.lr.ph:
20 ; CHECK-NEXT: [[FOR_CHECK8:%.*]] = icmp ult i64 0, %x
21 ; CHECK-NEXT: br i1 [[FOR_CHECK8]], label %for.body.lr.ph, label %return
22 ; CHECK: for.body.lr.ph:
23 ; CHECK-NEXT: br label %for.body
25 ; CHECK: [[FOR_CHECK:%.*]] = icmp ult i64 {{%.*}}, %x
26 ; CHECK-NEXT: br i1 [[FOR_CHECK]], label %for.body, label %for.cond2.return_crit_edge
28 ; CHECK: br label %for.tail
30 ; CHECK: [[VAL_A_IDX:%.*]] = load i32, ptr
31 ; CHECK-NEXT: [[ZERO_CHECK:%.*]] = icmp eq i32 [[VAL_A_IDX]], 0
32 ; CHECK-NEXT: br i1 [[ZERO_CHECK]], label %for.cond1.deopt.exit_crit_edge, label %for.cond2
33 ; CHECK: for.cond2.return_crit_edge:
34 ; CHECK-NEXT: {{%.*}} = phi i32
35 ; CHECK-NEXT: br label %return
37 ; CHECK-NEXT: [[SUM_LCSSA2:%.*]] = phi i32
38 ; CHECK-NEXT: ret i32 [[SUM_LCSSA2]]
39 ; CHECK: for.cond1.deopt.exit_crit_edge:
40 ; CHECK-NEXT: {{%.*}} = phi i32
41 ; CHECK-NEXT: br label %deopt.exit
43 ; CHECK: [[DEOPT_VAL:%.*]] = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 {{%.*}}) ]
44 ; CHECK-NEXT: ret i32 [[DEOPT_VAL]]
50 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
51 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
52 %a.idx = getelementptr inbounds i32, ptr %a, i64 %idx
53 %val.a.idx = load i32, ptr %a.idx, align 4
54 %zero.check = icmp eq i32 %val.a.idx, 0
55 br i1 %zero.check, label %deopt.exit, label %for.cond2
58 %for.check = icmp ult i64 %idx, %x
59 br i1 %for.check, label %for.body, label %return
65 %sum.next = add i32 %sum, %val.a.idx
66 %idx.next = add nuw nsw i64 %idx, 1
73 %deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 %val.a.idx) ]
77 define i32 @test_cond_with_two_deopt_exits(ptr nonnull %a, i64 %x) {
78 ; Rotation done three times.
79 ; Latch should be at the 3rd condition (for.cond3), exiting to %return.
81 ; CHECK-LABEL: @test_cond_with_two_deopt_exits(
83 ; CHECK-NEXT: [[A_IDX_DEREF4:%.*]] = load ptr, ptr %a
84 ; CHECK-NEXT: [[NULL_CHECK5:%.*]] = icmp eq ptr [[A_IDX_DEREF4]], null
85 ; CHECK-NEXT: br i1 [[NULL_CHECK5]], label %deopt.exit1, label %for.cond2.lr.ph
86 ; CHECK: for.cond2.lr.ph:
87 ; CHECK-NEXT: [[VAL_A_IDX9:%.*]] = load i32, ptr [[A_IDX_DEREF4]], align 4
88 ; CHECK-NEXT: [[ZERO_CHECK10:%.*]] = icmp eq i32 [[VAL_A_IDX9]], 0
89 ; CHECK-NEXT: br i1 [[ZERO_CHECK10]], label %deopt.exit2, label %for.cond3.lr.ph
90 ; CHECK: for.cond3.lr.ph:
91 ; CHECK-NEXT: [[FOR_CHECK14:%.*]] = icmp ult i64 0, %x
92 ; CHECK-NEXT: br i1 [[FOR_CHECK14]], label %for.body.lr.ph, label %return
93 ; CHECK: for.body.lr.ph:
94 ; CHECK-NEXT: br label %for.body
96 ; CHECK: [[VAL_A_IDX:%.*]] = load i32, ptr
97 ; CHECK-NEXT: [[ZERO_CHECK:%.*]] = icmp eq i32 [[VAL_A_IDX]], 0
98 ; CHECK-NEXT: br i1 [[ZERO_CHECK]], label %for.cond2.deopt.exit2_crit_edge, label %for.cond3
100 ; CHECK: [[FOR_CHECK:%.*]] = icmp ult i64 {{%.*}}, %x
101 ; CHECK-NEXT: br i1 [[FOR_CHECK]], label %for.body, label %for.cond3.return_crit_edge
103 ; CHECK: br label %for.tail
105 ; CHECK: [[IDX_NEXT:%.*]] = add nuw nsw i64 {{%.*}}, 1
106 ; CHECK: [[NULL_CHECK:%.*]] = icmp eq ptr {{%.*}}, null
107 ; CHECK-NEXT: br i1 [[NULL_CHECK]], label %for.cond1.deopt.exit1_crit_edge, label %for.cond2
108 ; CHECK: for.cond3.return_crit_edge:
109 ; CHECK-NEXT: [[SPLIT18:%.*]] = phi i32
110 ; CHECK-NEXT: br label %return
112 ; CHECK-NEXT: [[SUM_LCSSA2:%.*]] = phi i32
113 ; CHECK-NEXT: ret i32 [[SUM_LCSSA2]]
114 ; CHECK: for.cond1.deopt.exit1_crit_edge:
115 ; CHECK-NEXT: br label %deopt.exit1
116 ; CHECK: deopt.exit1:
117 ; CHECK-NEXT: [[DEOPT_VAL1:%.*]] = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 0) ]
118 ; CHECK-NEXT: ret i32 [[DEOPT_VAL1]]
119 ; CHECK: for.cond2.deopt.exit2_crit_edge:
120 ; CHECK-NEXT: [[SPLIT:%.*]] = phi i32
121 ; CHECK-NEXT: br label %deopt.exit2
122 ; CHECK: deopt.exit2:
123 ; CHECK-NEXT: [[VAL_A_IDX_LCSSA:%.*]] = phi i32
124 ; CHECK-NEXT: [[DEOPT_VAL2:%.*]] = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 [[VAL_A_IDX_LCSSA]]) ]
125 ; CHECK-NEXT: ret i32 [[DEOPT_VAL2]]
131 %idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
132 %sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
133 %a.idx = getelementptr inbounds ptr, ptr %a, i64 %idx
134 %a.idx.deref = load ptr, ptr %a.idx
135 %null.check = icmp eq ptr %a.idx.deref, null
136 br i1 %null.check, label %deopt.exit1, label %for.cond2
139 %val.a.idx = load i32, ptr %a.idx.deref, align 4
140 %zero.check = icmp eq i32 %val.a.idx, 0
141 br i1 %zero.check, label %deopt.exit2, label %for.cond3
144 %for.check = icmp ult i64 %idx, %x
145 br i1 %for.check, label %for.body, label %return
151 %sum.next = add i32 %sum, %val.a.idx
152 %idx.next = add nuw nsw i64 %idx, 1
159 %deopt.val1 = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 0) ]
162 %deopt.val2 = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 %val.a.idx) ]