1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=indvars < %s | FileCheck %s
4 ; Check that we replace signed comparisons between non-negative values with
5 ; unsigned comparisons if we can.
7 target datalayout = "n8:16:32:64"
9 define i32 @test_01(i32 %a, i32 %b, ptr %p) {
10 ; CHECK-LABEL: @test_01(
12 ; CHECK-NEXT: br label [[LOOP_ENTRY:%.*]]
14 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP_BE:%.*]] ]
15 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[IV]], 100
16 ; CHECK-NEXT: br i1 [[CMP1]], label [[B1:%.*]], label [[B2:%.*]]
18 ; CHECK-NEXT: store i32 [[IV]], ptr [[P:%.*]], align 4
19 ; CHECK-NEXT: br label [[MERGE:%.*]]
21 ; CHECK-NEXT: store i32 [[A:%.*]], ptr [[P]], align 4
22 ; CHECK-NEXT: br label [[MERGE]]
24 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[IV]], 100
25 ; CHECK-NEXT: br i1 [[CMP2]], label [[B3:%.*]], label [[B4:%.*]]
27 ; CHECK-NEXT: store i32 [[IV]], ptr [[P]], align 4
28 ; CHECK-NEXT: br label [[LOOP_BE]]
30 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[P]], align 4
31 ; CHECK-NEXT: br label [[LOOP_BE]]
33 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
34 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_NEXT]], 1000
35 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP_ENTRY]], label [[EXIT:%.*]]
37 ; CHECK-NEXT: ret i32 999
44 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.be ]
45 %cmp1 = icmp slt i32 %iv, 100
46 br i1 %cmp1, label %b1, label %b2
57 %cmp2 = icmp ult i32 %iv, 100
58 br i1 %cmp2, label %b3, label %b4
69 %iv.next = add i32 %iv, 1
70 %cmp3 = icmp slt i32 %iv.next, 1000
71 br i1 %cmp3, label %loop.entry, label %exit
77 define i32 @test_02(i32 %a, i32 %b, ptr %p) {
78 ; CHECK-LABEL: @test_02(
80 ; CHECK-NEXT: br label [[LOOP_ENTRY:%.*]]
82 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP_BE:%.*]] ]
83 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i32 100, [[IV]]
84 ; CHECK-NEXT: br i1 [[CMP1]], label [[B1:%.*]], label [[B2:%.*]]
86 ; CHECK-NEXT: store i32 [[IV]], ptr [[P:%.*]], align 4
87 ; CHECK-NEXT: br label [[MERGE:%.*]]
89 ; CHECK-NEXT: store i32 [[A:%.*]], ptr [[P]], align 4
90 ; CHECK-NEXT: br label [[MERGE]]
92 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 100, [[IV]]
93 ; CHECK-NEXT: br i1 [[CMP2]], label [[B3:%.*]], label [[B4:%.*]]
95 ; CHECK-NEXT: store i32 [[IV]], ptr [[P]], align 4
96 ; CHECK-NEXT: br label [[LOOP_BE]]
98 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[P]], align 4
99 ; CHECK-NEXT: br label [[LOOP_BE]]
101 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
102 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV_NEXT]], 1000
103 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP_ENTRY]], label [[EXIT:%.*]]
105 ; CHECK-NEXT: ret i32 999
112 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.be ]
113 %cmp1 = icmp sgt i32 100, %iv
114 br i1 %cmp1, label %b1, label %b2
117 store i32 %iv, ptr %p
125 %cmp2 = icmp ugt i32 100, %iv
126 br i1 %cmp2, label %b3, label %b4
129 store i32 %iv, ptr %p
137 %iv.next = add i32 %iv, 1
138 %cmp3 = icmp sgt i32 1000, %iv.next
139 br i1 %cmp3, label %loop.entry, label %exit
145 define i32 @test_03(ptr %p, ptr %capacity_p, ptr %num_elements_p) {
146 ; CHECK-LABEL: @test_03(
148 ; CHECK-NEXT: [[CAPACITY:%.*]] = load i32, ptr [[CAPACITY_P:%.*]], align 4, !range [[RNG0:![0-9]+]]
149 ; CHECK-NEXT: [[NUM_ELEMENTS:%.*]] = load i32, ptr [[NUM_ELEMENTS_P:%.*]], align 4, !range [[RNG0]]
150 ; CHECK-NEXT: br label [[LOOP:%.*]]
152 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
153 ; CHECK-NEXT: [[BYTES_TO_WRITE:%.*]] = sub nuw nsw i32 [[CAPACITY]], [[IV]]
154 ; CHECK-NEXT: [[CAPACITY_CHECK:%.*]] = icmp slt i32 [[BYTES_TO_WRITE]], 4
155 ; CHECK-NEXT: br i1 [[CAPACITY_CHECK]], label [[OUT_OF_BOUNDS:%.*]], label [[BACKEDGE]]
157 ; CHECK-NEXT: [[EL_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
158 ; CHECK-NEXT: store i32 1, ptr [[EL_PTR]], align 4
159 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 4
160 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[NUM_ELEMENTS]]
161 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
163 ; CHECK-NEXT: [[IV_NEXT_LCSSA:%.*]] = phi i32 [ [[IV_NEXT]], [[BACKEDGE]] ]
164 ; CHECK-NEXT: ret i32 [[IV_NEXT_LCSSA]]
165 ; CHECK: out_of_bounds:
166 ; CHECK-NEXT: ret i32 -1
169 %capacity = load i32, ptr %capacity_p, !range !0
170 %num_elements = load i32, ptr %num_elements_p, !range !0
174 %iv = phi i32 [0, %entry], [%iv.next, %backedge]
175 %bytes_to_write = sub i32 %capacity, %iv
176 %capacity_check = icmp slt i32 %bytes_to_write, 4
177 br i1 %capacity_check, label %out_of_bounds, label %backedge
180 %el.ptr = getelementptr i32, ptr %p, i32 %iv
181 store i32 1, ptr %el.ptr
182 %iv.next = add nuw nsw i32 %iv, 4
183 %loop_cond = icmp slt i32 %iv.next, %num_elements
184 br i1 %loop_cond, label %loop, label %exit
193 define i32 @test_04(ptr %p, ptr %capacity_p, ptr %num_elements_p) {
194 ; CHECK-LABEL: @test_04(
196 ; CHECK-NEXT: [[CAPACITY:%.*]] = load i32, ptr [[CAPACITY_P:%.*]], align 4, !range [[RNG0]]
197 ; CHECK-NEXT: [[NUM_ELEMENTS:%.*]] = load i32, ptr [[NUM_ELEMENTS_P:%.*]], align 4, !range [[RNG0]]
198 ; CHECK-NEXT: br label [[LOOP:%.*]]
200 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
201 ; CHECK-NEXT: [[BYTES_TO_WRITE:%.*]] = sub nuw nsw i32 [[CAPACITY]], [[IV]]
202 ; CHECK-NEXT: [[CAPACITY_CHECK:%.*]] = icmp sle i32 [[BYTES_TO_WRITE]], 3
203 ; CHECK-NEXT: br i1 [[CAPACITY_CHECK]], label [[OUT_OF_BOUNDS:%.*]], label [[BACKEDGE]]
205 ; CHECK-NEXT: [[EL_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
206 ; CHECK-NEXT: store i32 1, ptr [[EL_PTR]], align 4
207 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 4
208 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[NUM_ELEMENTS]]
209 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
211 ; CHECK-NEXT: [[IV_NEXT_LCSSA:%.*]] = phi i32 [ [[IV_NEXT]], [[BACKEDGE]] ]
212 ; CHECK-NEXT: ret i32 [[IV_NEXT_LCSSA]]
213 ; CHECK: out_of_bounds:
214 ; CHECK-NEXT: ret i32 -1
217 %capacity = load i32, ptr %capacity_p, !range !0
218 %num_elements = load i32, ptr %num_elements_p, !range !0
222 %iv = phi i32 [0, %entry], [%iv.next, %backedge]
223 %bytes_to_write = sub i32 %capacity, %iv
224 %capacity_check = icmp sle i32 %bytes_to_write, 3
225 br i1 %capacity_check, label %out_of_bounds, label %backedge
228 %el.ptr = getelementptr i32, ptr %p, i32 %iv
229 store i32 1, ptr %el.ptr
230 %iv.next = add nuw nsw i32 %iv, 4
231 %loop_cond = icmp slt i32 %iv.next, %num_elements
232 br i1 %loop_cond, label %loop, label %exit
241 define i32 @test_05(ptr %p, ptr %capacity_p, ptr %num_elements_p) {
242 ; CHECK-LABEL: @test_05(
244 ; CHECK-NEXT: [[CAPACITY:%.*]] = load i32, ptr [[CAPACITY_P:%.*]], align 4, !range [[RNG0]]
245 ; CHECK-NEXT: [[NUM_ELEMENTS:%.*]] = load i32, ptr [[NUM_ELEMENTS_P:%.*]], align 4, !range [[RNG0]]
246 ; CHECK-NEXT: br label [[LOOP:%.*]]
248 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
249 ; CHECK-NEXT: [[BYTES_TO_WRITE:%.*]] = sub nuw nsw i32 [[CAPACITY]], [[IV]]
250 ; CHECK-NEXT: [[CAPACITY_CHECK:%.*]] = icmp ult i32 [[BYTES_TO_WRITE]], 4
251 ; CHECK-NEXT: br i1 [[CAPACITY_CHECK]], label [[OUT_OF_BOUNDS:%.*]], label [[BACKEDGE]]
253 ; CHECK-NEXT: [[EL_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
254 ; CHECK-NEXT: store i32 1, ptr [[EL_PTR]], align 4
255 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 4
256 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[NUM_ELEMENTS]]
257 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
259 ; CHECK-NEXT: [[IV_NEXT_LCSSA:%.*]] = phi i32 [ [[IV_NEXT]], [[BACKEDGE]] ]
260 ; CHECK-NEXT: ret i32 [[IV_NEXT_LCSSA]]
261 ; CHECK: out_of_bounds:
262 ; CHECK-NEXT: ret i32 -1
265 %capacity = load i32, ptr %capacity_p, !range !0
266 %num_elements = load i32, ptr %num_elements_p, !range !0
270 %iv = phi i32 [0, %entry], [%iv.next, %backedge]
271 %bytes_to_write = sub i32 %capacity, %iv
272 %capacity_check = icmp ult i32 %bytes_to_write, 4
273 br i1 %capacity_check, label %out_of_bounds, label %backedge
276 %el.ptr = getelementptr i32, ptr %p, i32 %iv
277 store i32 1, ptr %el.ptr
278 %iv.next = add nuw nsw i32 %iv, 4
279 %loop_cond = icmp slt i32 %iv.next, %num_elements
280 br i1 %loop_cond, label %loop, label %exit
289 define i32 @test_06(ptr %p, ptr %capacity_p, ptr %num_elements_p) {
290 ; CHECK-LABEL: @test_06(
292 ; CHECK-NEXT: [[CAPACITY:%.*]] = load i32, ptr [[CAPACITY_P:%.*]], align 4, !range [[RNG0]]
293 ; CHECK-NEXT: [[NUM_ELEMENTS:%.*]] = load i32, ptr [[NUM_ELEMENTS_P:%.*]], align 4, !range [[RNG0]]
294 ; CHECK-NEXT: br label [[LOOP:%.*]]
296 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
297 ; CHECK-NEXT: [[BYTES_TO_WRITE:%.*]] = sub nuw nsw i32 [[CAPACITY]], [[IV]]
298 ; CHECK-NEXT: [[CAPACITY_CHECK:%.*]] = icmp ule i32 [[BYTES_TO_WRITE]], 3
299 ; CHECK-NEXT: br i1 [[CAPACITY_CHECK]], label [[OUT_OF_BOUNDS:%.*]], label [[BACKEDGE]]
301 ; CHECK-NEXT: [[EL_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
302 ; CHECK-NEXT: store i32 1, ptr [[EL_PTR]], align 4
303 ; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 4
304 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[IV_NEXT]], [[NUM_ELEMENTS]]
305 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
307 ; CHECK-NEXT: [[IV_NEXT_LCSSA:%.*]] = phi i32 [ [[IV_NEXT]], [[BACKEDGE]] ]
308 ; CHECK-NEXT: ret i32 [[IV_NEXT_LCSSA]]
309 ; CHECK: out_of_bounds:
310 ; CHECK-NEXT: ret i32 -1
313 %capacity = load i32, ptr %capacity_p, !range !0
314 %num_elements = load i32, ptr %num_elements_p, !range !0
318 %iv = phi i32 [0, %entry], [%iv.next, %backedge]
319 %bytes_to_write = sub i32 %capacity, %iv
320 %capacity_check = icmp ule i32 %bytes_to_write, 3
321 br i1 %capacity_check, label %out_of_bounds, label %backedge
324 %el.ptr = getelementptr i32, ptr %p, i32 %iv
325 store i32 1, ptr %el.ptr
326 %iv.next = add nuw nsw i32 %iv, 4
327 %loop_cond = icmp slt i32 %iv.next, %num_elements
328 br i1 %loop_cond, label %loop, label %exit
337 !0 = !{i32 1, i32 2147483648}