1 ; RUN: opt -verify-loop-info -irce -S < %s | FileCheck %s
2 ; RUN: opt -verify-loop-info -passes='require<branch-prob>,loop(irce)' -S < %s | FileCheck %s
4 define void @single_access_no_preloop_no_offset(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
6 %len = load i32, i32* %a_len_ptr, !range !0
7 %first.itr.check = icmp sgt i32 %n, 0
8 br i1 %first.itr.check, label %loop, label %exit
11 %idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds ]
12 %idx.next = add i32 %idx, 1
13 %abc = icmp slt i32 %idx, %len
14 br i1 %abc, label %in.bounds, label %out.of.bounds, !prof !1
17 %addr = getelementptr i32, i32* %arr, i32 %idx
18 store i32 0, i32* %addr
19 %next = icmp slt i32 %idx.next, %n
20 br i1 %next, label %loop, label %exit
29 ; CHECK-LABEL: @single_access_no_preloop_no_offset(
32 ; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
34 ; CHECK: main.exit.selector:
35 ; CHECK-NEXT: %idx.next.lcssa = phi i32 [ %idx.next, %in.bounds ]
36 ; CHECK-NEXT: [[continue:%[^ ]+]] = icmp slt i32 %idx.next.lcssa, %n
37 ; CHECK-NEXT: br i1 [[continue]], label %main.pseudo.exit, label %exit.loopexit
39 ; CHECK: main.pseudo.exit:
40 ; CHECK-NEXT: %idx.copy = phi i32 [ 0, %loop.preheader ], [ %idx.next.lcssa, %main.exit.selector ]
41 ; CHECK-NEXT: %indvar.end = phi i32 [ 0, %loop.preheader ], [ %idx.next.lcssa, %main.exit.selector ]
42 ; CHECK-NEXT: br label %postloop
45 ; CHECK-NEXT: br label %loop.postloop
47 ; CHECK: loop.postloop:
48 ; CHECK-NEXT: %idx.postloop = phi i32 [ %idx.next.postloop, %in.bounds.postloop ], [ %idx.copy, %postloop ]
49 ; CHECK-NEXT: %idx.next.postloop = add i32 %idx.postloop, 1
50 ; CHECK-NEXT: %abc.postloop = icmp slt i32 %idx.postloop, %len
51 ; CHECK-NEXT: br i1 %abc.postloop, label %in.bounds.postloop, label %out.of.bounds
53 ; CHECK: in.bounds.postloop:
54 ; CHECK-NEXT: %addr.postloop = getelementptr i32, i32* %arr, i32 %idx.postloop
55 ; CHECK-NEXT: store i32 0, i32* %addr.postloop
56 ; CHECK-NEXT: %next.postloop = icmp slt i32 %idx.next.postloop, %n
57 ; CHECK-NEXT: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit
60 define void @single_access_no_preloop_with_offset(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
62 %len = load i32, i32* %a_len_ptr, !range !0
63 %first.itr.check = icmp sgt i32 %n, 0
64 br i1 %first.itr.check, label %loop, label %exit
67 %idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds ]
68 %idx.next = add i32 %idx, 1
69 %idx.for.abc = add i32 %idx, 4
70 %abc = icmp slt i32 %idx.for.abc, %len
71 br i1 %abc, label %in.bounds, label %out.of.bounds, !prof !1
74 %addr = getelementptr i32, i32* %arr, i32 %idx.for.abc
75 store i32 0, i32* %addr
76 %next = icmp slt i32 %idx.next, %n
77 br i1 %next, label %loop, label %exit
86 ; CHECK-LABEL: @single_access_no_preloop_with_offset(
88 ; CHECK: loop.preheader:
89 ; CHECK: [[safe_range_end:[^ ]+]] = add nsw i32 %len, -4
90 ; CHECK: [[exit_main_loop_at_hiclamp_cmp:[^ ]+]] = icmp slt i32 %n, [[safe_range_end]]
91 ; CHECK: [[exit_main_loop_at_hiclamp:[^ ]+]] = select i1 [[exit_main_loop_at_hiclamp_cmp]], i32 %n, i32 [[safe_range_end]]
92 ; CHECK: [[exit_main_loop_at_loclamp_cmp:[^ ]+]] = icmp sgt i32 [[exit_main_loop_at_hiclamp]], 0
93 ; CHECK: [[exit_main_loop_at_loclamp:[^ ]+]] = select i1 [[exit_main_loop_at_loclamp_cmp]], i32 [[exit_main_loop_at_hiclamp]], i32 0
94 ; CHECK: [[enter_main_loop:[^ ]+]] = icmp slt i32 0, [[exit_main_loop_at_loclamp]]
95 ; CHECK: br i1 [[enter_main_loop]], label %[[loop_preheader:[^ ,]+]], label %main.pseudo.exit
98 ; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
101 ; CHECK: [[continue_main_loop:[^ ]+]] = icmp slt i32 %idx.next, [[exit_main_loop_at_loclamp]]
102 ; CHECK: br i1 [[continue_main_loop]], label %loop, label %main.exit.selector
104 ; CHECK: main.pseudo.exit:
105 ; CHECK: %idx.copy = phi i32 [ 0, %loop.preheader ], [ %idx.next.lcssa, %main.exit.selector ]
106 ; CHECK: br label %postloop
108 ; CHECK: loop.postloop:
109 ; CHECK: %idx.postloop = phi i32 [ %idx.next.postloop, %in.bounds.postloop ], [ %idx.copy, %postloop ]
111 ; CHECK: in.bounds.postloop:
112 ; CHECK: %next.postloop = icmp slt i32 %idx.next.postloop, %n
113 ; CHECK: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit
115 ; Make sure that we do not do IRCE if we know that the safe iteration range of
116 ; the main loop is empty.
118 define void @single_access_empty_range(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
120 %len = load i32, i32* %a_len_ptr, !range !0
121 %first.itr.check = icmp sgt i32 %n, 0
122 br i1 %first.itr.check, label %loop, label %exit
125 %idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds ]
126 %idx.next = add i32 %idx, 1
127 %abc = icmp slt i32 %idx, 0
128 br i1 %abc, label %in.bounds, label %out.of.bounds, !prof !1
131 %addr = getelementptr i32, i32* %arr, i32 %idx
132 store i32 0, i32* %addr
133 %next = icmp slt i32 %idx.next, %n
134 br i1 %next, label %loop, label %exit
143 ; CHECK-LABEL: @single_access_empty_range(
144 ; CHECK-NOT: br i1 false
146 ; CHECK-NOT: postloop
148 define void @single_access_empty_range_2(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
150 %len = load i32, i32* %a_len_ptr, !range !0
151 %first.itr.check = icmp sgt i32 %n, 0
152 br i1 %first.itr.check, label %loop, label %exit
155 %idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds2 ]
156 %idx.next = add i32 %idx, 1
157 %abc = icmp slt i32 %idx, 60
158 br i1 %abc, label %in.bounds1, label %out.of.bounds, !prof !1
161 %def = icmp slt i32 %idx, 0
162 br i1 %def, label %in.bounds2, label %out.of.bounds, !prof !1
165 %addr = getelementptr i32, i32* %arr, i32 %idx
166 store i32 0, i32* %addr
167 %next = icmp slt i32 %idx.next, %n
168 br i1 %next, label %loop, label %exit
177 ; CHECK-LABEL: @single_access_empty_range_2(
178 ; CHECK-NOT: br i1 false
181 define void @single_access_no_preloop_no_offset_phi_len(i32 *%arr, i32 *%a_len_ptr, i32 *%b_len_ptr, i32 %n, i1 %unknown_cond) {
183 br i1 %unknown_cond, label %if.true, label %if.false
186 %len_a = load i32, i32* %a_len_ptr, !range !0
190 %len_b = load i32, i32* %b_len_ptr, !range !0
194 %len = phi i32 [ %len_a, %if.true ], [ %len_b, %if.false ]
195 %first.itr.check = icmp sgt i32 %n, 0
196 br i1 %first.itr.check, label %loop, label %exit
199 %idx = phi i32 [ 0, %merge ] , [ %idx.next, %in.bounds ]
200 %idx.next = add i32 %idx, 1
201 %abc = icmp slt i32 %idx, %len
202 br i1 %abc, label %in.bounds, label %out.of.bounds, !prof !1
205 %addr = getelementptr i32, i32* %arr, i32 %idx
206 store i32 0, i32* %addr
207 %next = icmp slt i32 %idx.next, %n
208 br i1 %next, label %loop, label %exit
217 ; CHECK-LABEL: @single_access_no_preloop_no_offset_phi_len(
220 ; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
222 ; CHECK: main.exit.selector:
223 ; CHECK-NEXT: %idx.next.lcssa = phi i32 [ %idx.next, %in.bounds ]
224 ; CHECK-NEXT: [[continue:%[^ ]+]] = icmp slt i32 %idx.next.lcssa, %n
225 ; CHECK-NEXT: br i1 [[continue]], label %main.pseudo.exit, label %exit.loopexit
227 ; CHECK: main.pseudo.exit:
228 ; CHECK-NEXT: %idx.copy = phi i32 [ 0, %loop.preheader ], [ %idx.next.lcssa, %main.exit.selector ]
229 ; CHECK-NEXT: %indvar.end = phi i32 [ 0, %loop.preheader ], [ %idx.next.lcssa, %main.exit.selector ]
230 ; CHECK-NEXT: br label %postloop
233 ; CHECK-NEXT: br label %loop.postloop
235 ; CHECK: loop.postloop:
236 ; CHECK-NEXT: %idx.postloop = phi i32 [ %idx.next.postloop, %in.bounds.postloop ], [ %idx.copy, %postloop ]
237 ; CHECK-NEXT: %idx.next.postloop = add i32 %idx.postloop, 1
238 ; CHECK-NEXT: %abc.postloop = icmp slt i32 %idx.postloop, %len
239 ; CHECK-NEXT: br i1 %abc.postloop, label %in.bounds.postloop, label %out.of.bounds
241 ; CHECK: in.bounds.postloop:
242 ; CHECK-NEXT: %addr.postloop = getelementptr i32, i32* %arr, i32 %idx.postloop
243 ; CHECK-NEXT: store i32 0, i32* %addr.postloop
244 ; CHECK-NEXT: %next.postloop = icmp slt i32 %idx.next.postloop, %n
245 ; CHECK-NEXT: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit
247 !0 = !{i32 0, i32 2147483647}
248 !1 = !{!"branch_weights", i32 64, i32 4}