1;
2; Here all the branches we unswitch are exiting from the inner loop.
3; That means we should not be getting exponential behavior on inner-loop
4; unswitch. In fact there should be just a single version of inner-loop,
5; with possibly some outer loop copies.
6;
7; There should be just a single copy of each loop when strictest mutiplier
8; candidates formula (unscaled candidates == 0) is enforced:
9
10; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
11; RUN:     -unswitch-num-initial-unscaled-candidates=0 -unswitch-siblings-toplevel-div=1 \
12; RUN:     -passes='loop(simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | FileCheck %s --check-prefixes=LOOP1
13;
14; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
15; RUN:     -unswitch-num-initial-unscaled-candidates=0 -unswitch-siblings-toplevel-div=16 \
16; RUN:     -passes='loop(simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | FileCheck %s --check-prefixes=LOOP1
17;
18; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
19; RUN:     -unswitch-num-initial-unscaled-candidates=0 -unswitch-siblings-toplevel-div=1 \
20; RUN:     -passes='loop-mssa(simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | FileCheck %s --check-prefixes=LOOP1
21;
22; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
23; RUN:     -unswitch-num-initial-unscaled-candidates=0 -unswitch-siblings-toplevel-div=16 \
24; RUN:     -passes='loop-mssa(simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | FileCheck %s --check-prefixes=LOOP1
25;
26; When we relax the candidates part of a multiplier formula
27; (unscaled candidates == 2) we start getting some unswitches in outer loops,
28; which leads to siblings multiplier kicking in.
29;
30; The tests below also run licm, because it is needed to hoist out
31; loop-invariant freeze instructions, which otherwise may block further
32; unswitching.
33;
34; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
35; RUN:     -unswitch-num-initial-unscaled-candidates=3 -unswitch-siblings-toplevel-div=1 \
36; RUN:     -passes='loop-mssa(licm,simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | \
37; RUN:     sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE3-DIV1
38;
39; NB: sort -b is essential here and below, otherwise blanks might lead to different
40; order depending on locale.
41;
42; RUN: opt < %s -enable-unswitch-cost-multiplier=true \
43; RUN:     -unswitch-num-initial-unscaled-candidates=3 -unswitch-siblings-toplevel-div=2 \
44; RUN:     -passes='loop-mssa(licm,simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | \
45; RUN:     sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-UNSCALE3-DIV2
46;
47; With disabled cost-multiplier we get maximal possible amount of unswitches.
48;
49; RUN: opt < %s -enable-unswitch-cost-multiplier=false \
50; RUN:     -passes='loop-mssa(licm,simple-loop-unswitch<nontrivial>),print<loops>' -disable-output 2>&1 | \
51; RUN:	   sort -b -k 1 | FileCheck %s --check-prefixes=LOOP-MAX
52;
53; Single loop nest, not unswitched
54; LOOP1:     Loop at depth 1 containing:
55; LOOP1-NOT:  Loop at depth 1 containing:
56; LOOP1:     Loop at depth 2 containing:
57; LOOP1-NOT:  Loop at depth 2 containing:
58; LOOP1:     Loop at depth 3 containing:
59; LOOP1-NOT:  Loop at depth 3 containing:
60;
61; Half unswitched loop nests, with unscaled3 and div1 it gets less depth1 loops unswitched
62; since they have more cost.
63; LOOP-UNSCALE3-DIV1-COUNT-4: Loop at depth 1 containing:
64; LOOP-UNSCALE3-DIV1-NOT:      Loop at depth 1 containing:
65; LOOP-UNSCALE3-DIV1-COUNT-1: Loop at depth 2 containing:
66; LOOP-UNSCALE3-DIV1-NOT:      Loop at depth 2 containing:
67; LOOP-UNSCALE3-DIV1-COUNT-1: Loop at depth 3 containing:
68; LOOP-UNSCALE3-DIV1-NOT:      Loop at depth 3 containing:
69;
70; Half unswitched loop nests, with unscaled3 and div2 it gets more depth1 loops unswitched
71; as div2 kicks in.
72; LOOP-UNSCALE3-DIV2-COUNT-6: Loop at depth 1 containing:
73; LOOP-UNSCALE3-DIV2-NOT:      Loop at depth 1 containing:
74; LOOP-UNSCALE3-DIV2-COUNT-1: Loop at depth 2 containing:
75; LOOP-UNSCALE3-DIV2-NOT:      Loop at depth 2 containing:
76; LOOP-UNSCALE3-DIV2-COUNT-1: Loop at depth 3 containing:
77; LOOP-UNSCALE3-DIV2-NOT:      Loop at depth 3 containing:
78;
79; Maximally unswitched (copy of the outer loop per each condition)
80; LOOP-MAX-COUNT-6: Loop at depth 1 containing:
81; LOOP-MAX-NOT:      Loop at depth 1 containing:
82; LOOP-MAX-COUNT-1: Loop at depth 2 containing:
83; LOOP-MAX-NOT:      Loop at depth 2 containing:
84; LOOP-MAX-COUNT-1: Loop at depth 3 containing:
85; LOOP-MAX-NOT:      Loop at depth 3 containing:
86
87declare void @bar()
88
89define void @loop_nested3_conds5(i32* %addr, i1 %c1, i1 %c2, i1 %c3, i1 %c4, i1 %c5) {
90entry:
91  %addr1 = getelementptr i32, i32* %addr, i64 0
92  %addr2 = getelementptr i32, i32* %addr, i64 1
93  %addr3 = getelementptr i32, i32* %addr, i64 2
94  br label %outer
95outer:
96  %iv1 = phi i32 [0, %entry], [%iv1.next, %outer_latch]
97  %iv1.next = add i32 %iv1, 1
98  ;; skip nontrivial unswitch
99  call void @bar()
100  br label %middle
101middle:
102  %iv2 = phi i32 [0, %outer], [%iv2.next, %middle_latch]
103  %iv2.next = add i32 %iv2, 1
104  ;; skip nontrivial unswitch
105  call void @bar()
106  br label %loop
107loop:
108  %iv3 = phi i32 [0, %middle], [%iv3.next, %loop_latch]
109  %iv3.next = add i32 %iv3, 1
110  ;; skip nontrivial unswitch
111  call void @bar()
112  br i1 %c1, label %loop_next1_left, label %outer_latch
113loop_next1_left:
114  br label %loop_next1
115loop_next1_right:
116  br label %loop_next1
117
118loop_next1:
119  br i1 %c2, label %loop_next2_left, label %outer_latch
120loop_next2_left:
121  br label %loop_next2
122loop_next2_right:
123  br label %loop_next2
124
125loop_next2:
126  br i1 %c3, label %loop_next3_left, label %outer_latch
127loop_next3_left:
128  br label %loop_next3
129loop_next3_right:
130  br label %loop_next3
131
132loop_next3:
133  br i1 %c4, label %loop_next4_left, label %outer_latch
134loop_next4_left:
135  br label %loop_next4
136loop_next4_right:
137  br label %loop_next4
138
139loop_next4:
140  br i1 %c5, label %loop_latch_left, label %outer_latch
141loop_latch_left:
142  br label %loop_latch
143loop_latch_right:
144  br label %loop_latch
145
146loop_latch:
147  store volatile i32 0, i32* %addr1
148  %test_loop = icmp slt i32 %iv3, 50
149  br i1 %test_loop, label %loop, label %middle_latch
150middle_latch:
151  store volatile i32 0, i32* %addr2
152  %test_middle = icmp slt i32 %iv2, 50
153  br i1 %test_middle, label %middle, label %outer_latch
154outer_latch:
155  store volatile i32 0, i32* %addr3
156  %test_outer = icmp slt i32 %iv1, 50
157  br i1 %test_outer, label %outer, label %exit
158exit:
159  ret void
160}
161