1 ; RUN: opt < %s -loop-unswitch -enable-new-pm=0 -verify-loop-info -verify-memoryssa -S < %s 2>&1 | FileCheck %s
3 define i32 @test(i32* %A, i1 %C) {
6 no_exit: ; preds = %no_exit.backedge, %entry
7 %i.0.0 = phi i32 [ 0, %entry ], [ %i.0.0.be, %no_exit.backedge ] ; <i32> [#uses=3]
8 %gep.upgrd.1 = zext i32 %i.0.0 to i64 ; <i64> [#uses=1]
9 %tmp.7 = getelementptr i32, i32* %A, i64 %gep.upgrd.1 ; <i32*> [#uses=4]
10 %tmp.13 = load i32, i32* %tmp.7 ; <i32> [#uses=2]
11 %tmp.14 = add i32 %tmp.13, 1 ; <i32> [#uses=1]
12 store i32 %tmp.14, i32* %tmp.7
13 br i1 %C, label %then, label %endif
14 then: ; preds = %no_exit
15 %tmp.29 = load i32, i32* %tmp.7 ; <i32> [#uses=1]
16 %tmp.30 = add i32 %tmp.29, 2 ; <i32> [#uses=1]
17 store i32 %tmp.30, i32* %tmp.7
18 %inc9 = add i32 %i.0.0, 1 ; <i32> [#uses=2]
19 %tmp.112 = icmp ult i32 %inc9, 100000 ; <i1> [#uses=1]
20 br i1 %tmp.112, label %no_exit.backedge, label %return
21 no_exit.backedge: ; preds = %endif, %then
22 %i.0.0.be = phi i32 [ %inc9, %then ], [ %inc, %endif ] ; <i32> [#uses=1]
24 endif: ; preds = %no_exit
25 %inc = add i32 %i.0.0, 1 ; <i32> [#uses=2]
26 %tmp.1 = icmp ult i32 %inc, 100000 ; <i1> [#uses=1]
27 br i1 %tmp.1, label %no_exit.backedge, label %return
28 return: ; preds = %endif, %then
32 ; This simple test would normally unswitch, but should be inhibited by the presence of
33 ; the noduplicate call.
35 ; CHECK-LABEL: @test2(
36 define i32 @test2(i32* %var) {
38 store i32 2, i32* %mem
39 %c = load i32, i32* %mem
45 %var_val = load i32, i32* %var
47 switch i32 %c, label %default [
53 call void @incf() noreturn nounwind
56 ; CHECK: call void @decf()
57 ; CHECK-NOT: call void @decf()
58 call void @decf() noreturn nounwind noduplicate
67 ; This simple test would normally unswitch, but should be inhibited by the presence of
68 ; the convergent call that is not control-dependent on the unswitch condition.
70 ; CHECK-LABEL: @test3(
71 define i32 @test3(i32* %var) {
73 store i32 2, i32* %mem
74 %c = load i32, i32* %mem
80 %var_val = load i32, i32* %var
82 ; CHECK: call void @conv()
83 ; CHECK-NOT: call void @conv()
84 call void @conv() convergent
86 switch i32 %c, label %default [
92 call void @incf() noreturn nounwind
95 call void @decf() noreturn nounwind
104 ; Make sure we unswitch %a == 0 out of the loop.
106 ; CHECK: define void @and_i2_as_switch_input(i2
108 ; This is an indication that the loop has been unswitched.
109 ; CHECK: icmp eq i2 %a, 0
111 ; There should be no more unswitching after the 1st unswitch.
114 define void @and_i2_as_switch_input(i2 %a) {
119 %i = phi i2 [ 0, %entry ], [ %inc, %for.inc ]
121 %and1 = and i2 %and, %i
122 switch i2 %and1, label %sw.default [
140 %inc = add nsw i2 %i, 1
141 %cmp = icmp slt i2 %inc, 3
142 br i1 %cmp, label %for.body, label %for.end
148 ; Make sure we unswitch %a == !0 out of the loop.
150 ; CHECK: define void @or_i2_as_switch_input(i2
152 ; This is an indication that the loop has been unswitched.
153 ; CHECK: icmp eq i2 %a, -1
155 ; There should be no more unswitching after the 1st unswitch.
158 define void @or_i2_as_switch_input(i2 %a) {
163 %i = phi i2 [ 0, %entry ], [ %inc, %for.inc ]
166 switch i2 %or1, label %sw.default [
184 %inc = add nsw i2 %i, 1
185 %cmp = icmp slt i2 %inc, 3
186 br i1 %cmp, label %for.body, label %for.end
192 ; Make sure we unswitch %a == !0 out of the loop. Even we do not
193 ; have it as a case value. Unswitching it out allows us to simplify
194 ; the or operator chain.
196 ; CHECK: define void @or_i2_as_switch_input_unswitch_default(i2
198 ; This is an indication that the loop has been unswitched.
199 ; CHECK: icmp eq i2 %a, -1
201 ; There should be no more unswitching after the 1st unswitch.
204 define void @or_i2_as_switch_input_unswitch_default(i2 %a) {
209 %i = phi i2 [ 0, %entry ], [ %inc, %for.inc ]
212 switch i2 %or1, label %sw.default [
230 %inc = add nsw i2 %i, 1
231 %cmp = icmp slt i2 %inc, 3
232 br i1 %cmp, label %for.body, label %for.end
238 ; Make sure we don't unswitch, as we can not find an input value %a
239 ; that will effectively unswitch 0 or 3 out of the loop.
241 ; CHECK: define void @and_or_i2_as_switch_input(i2
243 ; This is an indication that the loop has NOT been unswitched.
246 define void @and_or_i2_as_switch_input(i2 %a) {
251 %i = phi i2 [ 0, %entry ], [ %inc, %for.inc ]
254 switch i2 %or, label %sw.default [
272 %inc = add nsw i2 %i, 1
273 %cmp = icmp slt i2 %inc, 3
274 br i1 %cmp, label %for.body, label %for.end
280 ; Make sure we don't unswitch, as we can not find an input value %a
281 ; that will effectively unswitch true/false out of the loop.
283 ; CHECK: define void @and_or_i1_as_branch_input(i1
285 ; This is an indication that the loop has NOT been unswitched.
288 define void @and_or_i1_as_branch_input(i1 %a) {
293 %i = phi i1 [ 0, %entry ], [ %inc, %for.inc ]
296 br i1 %or, label %sw.bb, label %sw.bb1
308 %inc = add nsw i1 %i, 1
309 %cmp = icmp slt i1 %inc, 1
310 br i1 %cmp, label %for.body, label %for.end
316 declare void @incf() noreturn
317 declare void @decf() noreturn
318 declare void @conv() convergent