1 ; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards -S < %s | FileCheck %s
2 ; RUN: opt -passes='simple-loop-unswitch<nontrivial>' -simple-loop-unswitch-guards -S < %s | FileCheck %s
3 ; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards -verify-memoryssa -verify-loop-info -S < %s | FileCheck %s
5 declare void @llvm.experimental.guard(i1, ...)
7 define void @test_simple_case(i1 %cond, i32 %N) {
8 ; CHECK-LABEL: @test_simple_case(
10 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
11 ; CHECK: entry.split.us:
12 ; CHECK-NEXT: br label [[LOOP_US:%.*]]
14 ; CHECK-NEXT: [[IV_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
15 ; CHECK-NEXT: br label [[GUARDED_US]]
17 ; CHECK-NEXT: [[IV_NEXT_US]] = add i32 [[IV_US]], 1
18 ; CHECK-NEXT: [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
19 ; CHECK-NEXT: br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[EXIT_SPLIT_US:%.*]]
21 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
22 ; CHECK-NEXT: unreachable
29 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
30 call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
31 %iv.next = add i32 %iv, 1
32 %loop.cond = icmp slt i32 %iv.next, %N
33 br i1 %loop.cond, label %loop, label %exit
39 define void @test_two_guards(i1 %cond1, i1 %cond2, i32 %N) {
40 ; CHECK-LABEL: @test_two_guards(
42 ; CHECK-NEXT: br i1 [[COND1:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
43 ; CHECK: entry.split.us:
44 ; CHECK-NEXT: br i1 [[COND2:%.*]], label [[ENTRY_SPLIT_US_SPLIT_US:%.*]], label [[ENTRY_SPLIT_US_SPLIT:%.*]]
45 ; CHECK: entry.split.us.split.us:
46 ; CHECK-NEXT: br label [[LOOP_US_US:%.*]]
48 ; CHECK-NEXT: [[IV_US_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US_SPLIT_US]] ], [ [[IV_NEXT_US_US:%.*]], [[GUARDED_US2:%.*]] ]
49 ; CHECK-NEXT: br label [[GUARDED_US_US:%.*]]
50 ; CHECK: guarded.us.us:
51 ; CHECK-NEXT: br label [[GUARDED_US2]]
53 ; CHECK-NEXT: [[IV_NEXT_US_US]] = add i32 [[IV_US_US]], 1
54 ; CHECK-NEXT: [[LOOP_COND_US_US:%.*]] = icmp slt i32 [[IV_NEXT_US_US]], [[N:%.*]]
55 ; CHECK-NEXT: br i1 [[LOOP_COND_US_US]], label [[LOOP_US_US]], label [[EXIT_SPLIT_US_SPLIT_US:%.*]]
57 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
58 ; CHECK-NEXT: unreachable
60 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
61 ; CHECK-NEXT: unreachable
63 ; CHECK-NEXT: ret void
70 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
71 call void (i1, ...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
72 call void (i1, ...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
73 %iv.next = add i32 %iv, 1
74 %loop.cond = icmp slt i32 %iv.next, %N
75 br i1 %loop.cond, label %loop, label %exit
81 define void @test_conditional_guards(i1 %cond, i32 %N) {
82 ; CHECK-LABEL: @test_conditional_guards(
84 ; CHECK-NEXT: [[FROZEN:%.+]] = freeze i1 [[COND:%.*]]
85 ; CHECK-NEXT: br i1 [[FROZEN]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
86 ; CHECK: entry.split.us:
87 ; CHECK-NEXT: br label [[LOOP_US:%.*]]
89 ; CHECK-NEXT: [[IV_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[BACKEDGE_US:%.*]] ]
90 ; CHECK-NEXT: [[CONDITION_US:%.*]] = icmp eq i32 [[IV_US]], 123
91 ; CHECK-NEXT: br i1 [[CONDITION_US]], label [[GUARD_US:%.*]], label [[BACKEDGE_US]]
93 ; CHECK-NEXT: br label [[GUARDED_US:%.*]]
95 ; CHECK-NEXT: [[IV_NEXT_US]] = add i32 [[IV_US]], 1
96 ; CHECK-NEXT: [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
97 ; CHECK-NEXT: br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[EXIT_SPLIT_US:%.*]]
99 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
100 ; CHECK-NEXT: [[CONDITION:%.*]] = icmp eq i32 [[IV]], 123
101 ; CHECK-NEXT: br i1 [[CONDITION]], label [[GUARD:%.*]], label [[BACKEDGE]]
103 ; CHECK-NEXT: br label [[DEOPT:%.*]]
105 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
106 ; CHECK-NEXT: unreachable
108 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
109 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[N]]
110 ; CHECK-NEXT: br i1 [[LOOP_COND]], label %loop, label [[EXIT_SPLIT:%.*]]
117 %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
118 %condition = icmp eq i32 %iv, 123
119 br i1 %condition, label %guard, label %backedge
122 call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
126 %iv.next = add i32 %iv, 1
127 %loop.cond = icmp slt i32 %iv.next, %N
128 br i1 %loop.cond, label %loop, label %exit
134 define void @test_nested_loop(i1 %cond, i32 %N) {
135 ; CHECK-LABEL: @test_nested_loop(
137 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[ENTRY_SPLIT:%.*]], label [[OUTER_LOOP_SPLIT:%.*]]
138 ; CHECK: entry.split:
139 ; CHECK-NEXT: br label [[OUTER_LOOP:%.*]]
141 ; CHECK-NEXT: br label [[OUTER_LOOP_SPLIT_US:%.*]]
142 ; CHECK: outer_loop.split.us:
143 ; CHECK-NEXT: br label [[LOOP_US:%.*]]
145 ; CHECK-NEXT: [[IV_US:%.*]] = phi i32 [ 0, [[OUTER_LOOP_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
146 ; CHECK-NEXT: br label [[GUARDED_US]]
148 ; CHECK-NEXT: [[IV_NEXT_US]] = add i32 [[IV_US]], 1
149 ; CHECK-NEXT: [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
150 ; CHECK-NEXT: br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[OUTER_BACKEDGE_SPLIT_US:%.*]]
151 ; CHECK: outer_backedge.split.us:
152 ; CHECK-NEXT: br label [[OUTER_BACKEDGE:%.*]]
154 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
155 ; CHECK-NEXT: unreachable
156 ; CHECK: outer_backedge:
157 ; CHECK-NEXT: br i1 false, label [[OUTER_LOOP]], label [[EXIT:%.*]]
167 %iv = phi i32 [ 0, %outer_loop ], [ %iv.next, %loop ]
168 call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
169 %iv.next = add i32 %iv, 1
170 %loop.cond = icmp slt i32 %iv.next, %N
171 br i1 %loop.cond, label %loop, label %outer_backedge
174 br i1 undef, label %outer_loop, label %exit
180 define void @test_sibling_loops(i1 %cond1, i1 %cond2, i32 %N) {
181 ; CHECK-LABEL: @test_sibling_loops(
183 ; CHECK-NEXT: br i1 [[COND1:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
184 ; CHECK: [[IV1_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV1_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
185 ; CHECK-NEXT: br label [[GUARDED_US]]
186 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
187 ; CHECK-NEXT: unreachable
188 ; CHECK: [[IV2_US:%.*]] = phi i32 [ 0, [[BETWEEN:%.*]] ], [ [[IV1_NEXT_US2:%.*]], [[GUARDED_US2:%.*]] ]
189 ; CHECK-NEXT: br label [[GUARDED_US2]]
190 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
191 ; CHECK-NEXT: unreachable
198 %iv1 = phi i32 [ 0, %entry ], [ %iv1.next, %loop1 ]
199 call void (i1, ...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
200 %iv1.next = add i32 %iv1, 1
201 %loop1.cond = icmp slt i32 %iv1.next, %N
202 br i1 %loop1.cond, label %loop1, label %between
208 %iv2 = phi i32 [ 0, %between ], [ %iv2.next, %loop2 ]
209 call void (i1, ...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
210 %iv2.next = add i32 %iv2, 1
211 %loop2.cond = icmp slt i32 %iv2.next, %N
212 br i1 %loop2.cond, label %loop2, label %exit
218 ; Check that we don't do anything because of cleanuppad.
219 ; CHECK-LABEL: @test_cleanuppad(
220 ; CHECK: call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
221 ; CHECK-NOT: call void (i1, ...) @llvm.experimental.guard(
222 define void @test_cleanuppad(i1 %cond, i32 %N) personality ptr @__CxxFrameHandler3 {
228 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
229 call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
230 %iv.next = add i32 %iv, 1
231 invoke void @may_throw(i32 %iv) to label %loop unwind label %exit
234 %cp = cleanuppad within none []
235 cleanupret from %cp unwind to caller
239 declare void @may_throw(i32 %i)
240 declare i32 @__CxxFrameHandler3(...)