11db2551cSBjorn Pettersson; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards -S < %s | FileCheck %s
2cee313d2SEric Christopher; RUN: opt -simple-loop-unswitch -enable-nontrivial-unswitch -simple-loop-unswitch-guards -S < %s | FileCheck %s
31db2551cSBjorn Pettersson; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards  -verify-memoryssa -S < %s | FileCheck %s
4cee313d2SEric Christopher
5cee313d2SEric Christopherdeclare void @llvm.experimental.guard(i1, ...)
6cee313d2SEric Christopher
7cee313d2SEric Christopherdefine void @test_simple_case(i1 %cond, i32 %N) {
8cee313d2SEric Christopher; CHECK-LABEL: @test_simple_case(
9cee313d2SEric Christopher; CHECK-NEXT:  entry:
10cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[COND:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
11cee313d2SEric Christopher; CHECK:       entry.split.us:
12cee313d2SEric Christopher; CHECK-NEXT:    br label [[LOOP_US:%.*]]
13cee313d2SEric Christopher; CHECK:       loop.us:
14cee313d2SEric Christopher; CHECK-NEXT:    [[IV_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
15cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US]]
16cee313d2SEric Christopher; CHECK:       guarded.us:
17cee313d2SEric Christopher; CHECK-NEXT:    [[IV_NEXT_US]] = add i32 [[IV_US]], 1
18cee313d2SEric Christopher; CHECK-NEXT:    [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
19cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[EXIT_SPLIT_US:%.*]]
20cee313d2SEric Christopher; CHECK:       deopt:
21cee313d2SEric Christopher; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
22cee313d2SEric Christopher; CHECK-NEXT:    unreachable
23cee313d2SEric Christopher;
24cee313d2SEric Christopher
25cee313d2SEric Christopherentry:
26cee313d2SEric Christopher  br label %loop
27cee313d2SEric Christopher
28cee313d2SEric Christopherloop:
29cee313d2SEric Christopher  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
30cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
31cee313d2SEric Christopher  %iv.next = add i32 %iv, 1
32cee313d2SEric Christopher  %loop.cond = icmp slt i32 %iv.next, %N
33cee313d2SEric Christopher  br i1 %loop.cond, label %loop, label %exit
34cee313d2SEric Christopher
35cee313d2SEric Christopherexit:
36cee313d2SEric Christopher  ret void
37cee313d2SEric Christopher}
38cee313d2SEric Christopher
39cee313d2SEric Christopherdefine void @test_two_guards(i1 %cond1, i1 %cond2, i32 %N) {
40cee313d2SEric Christopher; CHECK-LABEL: @test_two_guards(
41cee313d2SEric Christopher; CHECK-NEXT:  entry:
42cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[COND1:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
43cee313d2SEric Christopher; CHECK:       entry.split.us:
44cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[ENTRY_SPLIT_US_SPLIT_US:%.*]], label [[ENTRY_SPLIT_US_SPLIT:%.*]]
45cee313d2SEric Christopher; CHECK:       entry.split.us.split.us:
46cee313d2SEric Christopher; CHECK-NEXT:    br label [[LOOP_US_US:%.*]]
47cee313d2SEric Christopher; CHECK:       loop.us.us:
48cee313d2SEric Christopher; CHECK-NEXT:    [[IV_US_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US_SPLIT_US]] ], [ [[IV_NEXT_US_US:%.*]], [[GUARDED_US2:%.*]] ]
49cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US_US:%.*]]
50cee313d2SEric Christopher; CHECK:       guarded.us.us:
51cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US2]]
52cee313d2SEric Christopher; CHECK:       guarded.us2:
53cee313d2SEric Christopher; CHECK-NEXT:    [[IV_NEXT_US_US]] = add i32 [[IV_US_US]], 1
54cee313d2SEric Christopher; CHECK-NEXT:    [[LOOP_COND_US_US:%.*]] = icmp slt i32 [[IV_NEXT_US_US]], [[N:%.*]]
55cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[LOOP_COND_US_US]], label [[LOOP_US_US]], label [[EXIT_SPLIT_US_SPLIT_US:%.*]]
56cee313d2SEric Christopher; CHECK:       deopt1:
57cee313d2SEric Christopher; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
58cee313d2SEric Christopher; CHECK-NEXT:    unreachable
59cee313d2SEric Christopher; CHECK:       deopt:
60cee313d2SEric Christopher; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
61cee313d2SEric Christopher; CHECK-NEXT:    unreachable
62cee313d2SEric Christopher; CHECK:       exit:
63cee313d2SEric Christopher; CHECK-NEXT:    ret void
64cee313d2SEric Christopher;
65cee313d2SEric Christopher
66cee313d2SEric Christopherentry:
67cee313d2SEric Christopher  br label %loop
68cee313d2SEric Christopher
69cee313d2SEric Christopherloop:
70cee313d2SEric Christopher  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
71cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
72cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
73cee313d2SEric Christopher  %iv.next = add i32 %iv, 1
74cee313d2SEric Christopher  %loop.cond = icmp slt i32 %iv.next, %N
75cee313d2SEric Christopher  br i1 %loop.cond, label %loop, label %exit
76cee313d2SEric Christopher
77cee313d2SEric Christopherexit:
78cee313d2SEric Christopher  ret void
79cee313d2SEric Christopher}
80cee313d2SEric Christopher
81cee313d2SEric Christopherdefine void @test_conditional_guards(i1 %cond, i32 %N) {
82cee313d2SEric Christopher; CHECK-LABEL: @test_conditional_guards(
83cee313d2SEric Christopher; CHECK-NEXT:  entry:
84*6a6cc554SFlorian Hahn; CHECK-NEXT:    [[FROZEN:%.+]] = freeze i1 [[COND:%.*]]
85*6a6cc554SFlorian Hahn; CHECK-NEXT:    br i1 [[FROZEN]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
86cee313d2SEric Christopher; CHECK:       entry.split.us:
87cee313d2SEric Christopher; CHECK-NEXT:    br label [[LOOP_US:%.*]]
88cee313d2SEric Christopher; CHECK:       loop.us:
89cee313d2SEric Christopher; CHECK-NEXT:    [[IV_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[BACKEDGE_US:%.*]] ]
90cee313d2SEric Christopher; CHECK-NEXT:    [[CONDITION_US:%.*]] = icmp eq i32 [[IV_US]], 123
91cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[CONDITION_US]], label [[GUARD_US:%.*]], label [[BACKEDGE_US]]
92cee313d2SEric Christopher; CHECK:       guard.us:
93cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US:%.*]]
94cee313d2SEric Christopher; CHECK:       backedge.us:
95cee313d2SEric Christopher; CHECK-NEXT:    [[IV_NEXT_US]] = add i32 [[IV_US]], 1
96cee313d2SEric Christopher; CHECK-NEXT:    [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
97cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[EXIT_SPLIT_US:%.*]]
98cee313d2SEric Christopher; CHECK:       loop:
99cee313d2SEric Christopher; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
100cee313d2SEric Christopher; CHECK-NEXT:    [[CONDITION:%.*]] = icmp eq i32 [[IV]], 123
101cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[CONDITION]], label [[GUARD:%.*]], label [[BACKEDGE]]
102cee313d2SEric Christopher; CHECK:       guard:
103cee313d2SEric Christopher; CHECK-NEXT:    br label [[DEOPT:%.*]]
104cee313d2SEric Christopher; CHECK:       deopt:
105cee313d2SEric Christopher; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
106cee313d2SEric Christopher; CHECK-NEXT:    unreachable
107cee313d2SEric Christopher; CHECK:       backedge:
108cee313d2SEric Christopher; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], 1
109cee313d2SEric Christopher; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[N]]
110cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[LOOP_COND]], label %loop, label [[EXIT_SPLIT:%.*]]
111cee313d2SEric Christopher;
112cee313d2SEric Christopher
113cee313d2SEric Christopherentry:
114cee313d2SEric Christopher  br label %loop
115cee313d2SEric Christopher
116cee313d2SEric Christopherloop:
117cee313d2SEric Christopher  %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
118cee313d2SEric Christopher  %condition = icmp eq i32 %iv, 123
119cee313d2SEric Christopher  br i1 %condition, label %guard, label %backedge
120cee313d2SEric Christopher
121cee313d2SEric Christopherguard:
122cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
123cee313d2SEric Christopher  br label %backedge
124cee313d2SEric Christopher
125cee313d2SEric Christopherbackedge:
126cee313d2SEric Christopher  %iv.next = add i32 %iv, 1
127cee313d2SEric Christopher  %loop.cond = icmp slt i32 %iv.next, %N
128cee313d2SEric Christopher  br i1 %loop.cond, label %loop, label %exit
129cee313d2SEric Christopher
130cee313d2SEric Christopherexit:
131cee313d2SEric Christopher  ret void
132cee313d2SEric Christopher}
133cee313d2SEric Christopher
134cee313d2SEric Christopherdefine void @test_nested_loop(i1 %cond, i32 %N) {
135cee313d2SEric Christopher; CHECK-LABEL: @test_nested_loop(
136cee313d2SEric Christopher; CHECK-NEXT:  entry:
137cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[COND:%.*]], label [[ENTRY_SPLIT:%.*]], label [[OUTER_LOOP_SPLIT:%.*]]
138cee313d2SEric Christopher; CHECK:       entry.split:
139cee313d2SEric Christopher; CHECK-NEXT:    br label [[OUTER_LOOP:%.*]]
140cee313d2SEric Christopher; CHECK:       outer_loop:
141cee313d2SEric Christopher; CHECK-NEXT:    br label [[OUTER_LOOP_SPLIT_US:%.*]]
142cee313d2SEric Christopher; CHECK:       outer_loop.split.us:
143cee313d2SEric Christopher; CHECK-NEXT:    br label [[LOOP_US:%.*]]
144cee313d2SEric Christopher; CHECK:       loop.us:
145cee313d2SEric Christopher; CHECK-NEXT:    [[IV_US:%.*]] = phi i32 [ 0, [[OUTER_LOOP_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
146cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US]]
147cee313d2SEric Christopher; CHECK:       guarded.us:
148cee313d2SEric Christopher; CHECK-NEXT:    [[IV_NEXT_US]] = add i32 [[IV_US]], 1
149cee313d2SEric Christopher; CHECK-NEXT:    [[LOOP_COND_US:%.*]] = icmp slt i32 [[IV_NEXT_US]], [[N:%.*]]
150cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[LOOP_COND_US]], label [[LOOP_US]], label [[OUTER_BACKEDGE_SPLIT_US:%.*]]
151cee313d2SEric Christopher; CHECK:       outer_backedge.split.us:
152cee313d2SEric Christopher; CHECK-NEXT:    br label [[OUTER_BACKEDGE:%.*]]
153cee313d2SEric Christopher; CHECK:       deopt:
154cee313d2SEric Christopher; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
155cee313d2SEric Christopher; CHECK-NEXT:    unreachable
156cee313d2SEric Christopher; CHECK:       outer_backedge:
157cee313d2SEric Christopher; CHECK-NEXT:    br i1 false, label [[OUTER_LOOP]], label [[EXIT:%.*]]
158cee313d2SEric Christopher;
159cee313d2SEric Christopher
160cee313d2SEric Christopherentry:
161cee313d2SEric Christopher  br label %outer_loop
162cee313d2SEric Christopher
163cee313d2SEric Christopherouter_loop:
164cee313d2SEric Christopher  br label %loop
165cee313d2SEric Christopher
166cee313d2SEric Christopherloop:
167cee313d2SEric Christopher  %iv = phi i32 [ 0, %outer_loop ], [ %iv.next, %loop ]
168cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
169cee313d2SEric Christopher  %iv.next = add i32 %iv, 1
170cee313d2SEric Christopher  %loop.cond = icmp slt i32 %iv.next, %N
171cee313d2SEric Christopher  br i1 %loop.cond, label %loop, label %outer_backedge
172cee313d2SEric Christopher
173cee313d2SEric Christopherouter_backedge:
174cee313d2SEric Christopher  br i1 undef, label %outer_loop, label %exit
175cee313d2SEric Christopher
176cee313d2SEric Christopherexit:
177cee313d2SEric Christopher  ret void
178cee313d2SEric Christopher}
179cee313d2SEric Christopher
180cee313d2SEric Christopherdefine void @test_sibling_loops(i1 %cond1, i1 %cond2, i32 %N) {
181cee313d2SEric Christopher; CHECK-LABEL: @test_sibling_loops(
182cee313d2SEric Christopher; CHECK-NEXT:  entry:
183cee313d2SEric Christopher; CHECK-NEXT:    br i1 [[COND1:%.*]], label [[ENTRY_SPLIT_US:%.*]], label [[ENTRY_SPLIT:%.*]]
184cee313d2SEric Christopher; CHECK:         [[IV1_US:%.*]] = phi i32 [ 0, [[ENTRY_SPLIT_US]] ], [ [[IV1_NEXT_US:%.*]], [[GUARDED_US:%.*]] ]
185cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US]]
186cee313d2SEric Christopher; CHECK:         call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
187cee313d2SEric Christopher; CHECK-NEXT:    unreachable
188cee313d2SEric Christopher; CHECK:         [[IV2_US:%.*]] = phi i32 [ 0, [[BETWEEN:%.*]] ], [ [[IV1_NEXT_US2:%.*]], [[GUARDED_US2:%.*]] ]
189cee313d2SEric Christopher; CHECK-NEXT:    br label [[GUARDED_US2]]
190cee313d2SEric Christopher; CHECK:         call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
191cee313d2SEric Christopher; CHECK-NEXT:    unreachable
192cee313d2SEric Christopher;
193cee313d2SEric Christopher
194cee313d2SEric Christopherentry:
195cee313d2SEric Christopher  br label %loop1
196cee313d2SEric Christopher
197cee313d2SEric Christopherloop1:
198cee313d2SEric Christopher  %iv1 = phi i32 [ 0, %entry ], [ %iv1.next, %loop1 ]
199cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond1) [ "deopt"() ]
200cee313d2SEric Christopher  %iv1.next = add i32 %iv1, 1
201cee313d2SEric Christopher  %loop1.cond = icmp slt i32 %iv1.next, %N
202cee313d2SEric Christopher  br i1 %loop1.cond, label %loop1, label %between
203cee313d2SEric Christopher
204cee313d2SEric Christopherbetween:
205cee313d2SEric Christopher  br label %loop2
206cee313d2SEric Christopher
207cee313d2SEric Christopherloop2:
208cee313d2SEric Christopher  %iv2 = phi i32 [ 0, %between ], [ %iv2.next, %loop2 ]
209cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond2) [ "deopt"() ]
210cee313d2SEric Christopher  %iv2.next = add i32 %iv2, 1
211cee313d2SEric Christopher  %loop2.cond = icmp slt i32 %iv2.next, %N
212cee313d2SEric Christopher  br i1 %loop2.cond, label %loop2, label %exit
213cee313d2SEric Christopher
214cee313d2SEric Christopherexit:
215cee313d2SEric Christopher  ret void
216cee313d2SEric Christopher}
217cee313d2SEric Christopher
218cee313d2SEric Christopher; Check that we don't do anything because of cleanuppad.
219cee313d2SEric Christopher; CHECK-LABEL: @test_cleanuppad(
220cee313d2SEric Christopher; CHECK:       call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
221cee313d2SEric Christopher; CHECK-NOT:   call void (i1, ...) @llvm.experimental.guard(
222cee313d2SEric Christopherdefine void @test_cleanuppad(i1 %cond, i32 %N) personality i32 (...)* @__CxxFrameHandler3 {
223cee313d2SEric Christopher
224cee313d2SEric Christopherentry:
225cee313d2SEric Christopher  br label %loop
226cee313d2SEric Christopher
227cee313d2SEric Christopherloop:
228cee313d2SEric Christopher  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
229cee313d2SEric Christopher  call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
230cee313d2SEric Christopher  %iv.next = add i32 %iv, 1
231cee313d2SEric Christopher  invoke void @may_throw(i32 %iv) to label %loop unwind label %exit
232cee313d2SEric Christopher
233cee313d2SEric Christopherexit:
234cee313d2SEric Christopher  %cp = cleanuppad within none []
235cee313d2SEric Christopher  cleanupret from %cp unwind to caller
236cee313d2SEric Christopher
237cee313d2SEric Christopher}
238cee313d2SEric Christopher
239cee313d2SEric Christopherdeclare void @may_throw(i32 %i)
240cee313d2SEric Christopherdeclare i32 @__CxxFrameHandler3(...)
241