1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=indvars -S "-data-layout=e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" | FileCheck -check-prefix=PTR64 %s
3 ; RUN: opt < %s -passes=indvars -S "-data-layout=e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32" | FileCheck -check-prefix=PTR32 %s
5 ; PR11279: Assertion !IVLimit->getType()->isPointerTy()
7 ; Test LinearFunctionTestReplace of a pointer-type loop counter. Note
8 ; that BECount may or may not be a pointer type. A pointer type
9 ; BECount doesn't really make sense, but that's what falls out of
10 ; SCEV. Since it's an ptr, it has unit stride so we never adjust the
11 ; SCEV expression in a way that would convert it to an integer type.
13 define i8 @testnullptrptr(ptr %buf, ptr %end) nounwind {
14 ; PTR64-LABEL: @testnullptrptr(
15 ; PTR64-NEXT: [[END1:%.*]] = ptrtoint ptr [[END:%.*]] to i64
16 ; PTR64-NEXT: br label [[LOOPGUARD:%.*]]
18 ; PTR64-NEXT: [[GUARD:%.*]] = icmp ult ptr null, [[END]]
19 ; PTR64-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
21 ; PTR64-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr null, i64 [[END1]]
22 ; PTR64-NEXT: br label [[LOOP:%.*]]
24 ; PTR64-NEXT: [[P_01_US_US:%.*]] = phi ptr [ null, [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
25 ; PTR64-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
26 ; PTR64-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
27 ; PTR64-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
28 ; PTR64-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
29 ; PTR64: exit.loopexit:
30 ; PTR64-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
31 ; PTR64-NEXT: br label [[EXIT]]
33 ; PTR64-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
34 ; PTR64-NEXT: ret i8 [[RET]]
36 ; PTR32-LABEL: @testnullptrptr(
37 ; PTR32-NEXT: [[END1:%.*]] = ptrtoint ptr [[END:%.*]] to i32
38 ; PTR32-NEXT: br label [[LOOPGUARD:%.*]]
40 ; PTR32-NEXT: [[GUARD:%.*]] = icmp ult ptr null, [[END]]
41 ; PTR32-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
43 ; PTR32-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr null, i32 [[END1]]
44 ; PTR32-NEXT: br label [[LOOP:%.*]]
46 ; PTR32-NEXT: [[P_01_US_US:%.*]] = phi ptr [ null, [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
47 ; PTR32-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
48 ; PTR32-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
49 ; PTR32-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
50 ; PTR32-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
51 ; PTR32: exit.loopexit:
52 ; PTR32-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
53 ; PTR32-NEXT: br label [[EXIT]]
55 ; PTR32-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
56 ; PTR32-NEXT: ret i8 [[RET]]
61 %guard = icmp ult ptr null, %end
62 br i1 %guard, label %preheader, label %exit
68 %p.01.us.us = phi ptr [ null, %preheader ], [ %gep, %loop ]
69 %s = phi i8 [0, %preheader], [%snext, %loop]
70 %gep = getelementptr inbounds i8, ptr %p.01.us.us, i64 1
71 %snext = load i8, ptr %gep
72 %cmp = icmp ult ptr %gep, %end
73 br i1 %cmp, label %loop, label %exit
76 %ret = phi i8 [0, %loopguard], [%snext, %loop]
80 define i8 @testptrptr(ptr %buf, ptr %end) nounwind {
81 ; PTR64-LABEL: @testptrptr(
82 ; PTR64-NEXT: [[BUF2:%.*]] = ptrtoint ptr [[BUF:%.*]] to i64
83 ; PTR64-NEXT: [[END1:%.*]] = ptrtoint ptr [[END:%.*]] to i64
84 ; PTR64-NEXT: br label [[LOOPGUARD:%.*]]
86 ; PTR64-NEXT: [[GUARD:%.*]] = icmp ult ptr [[BUF]], [[END]]
87 ; PTR64-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
89 ; PTR64-NEXT: [[TMP1:%.*]] = sub i64 [[END1]], [[BUF2]]
90 ; PTR64-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[BUF]], i64 [[TMP1]]
91 ; PTR64-NEXT: br label [[LOOP:%.*]]
93 ; PTR64-NEXT: [[P_01_US_US:%.*]] = phi ptr [ [[BUF]], [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
94 ; PTR64-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
95 ; PTR64-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
96 ; PTR64-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
97 ; PTR64-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
98 ; PTR64: exit.loopexit:
99 ; PTR64-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
100 ; PTR64-NEXT: br label [[EXIT]]
102 ; PTR64-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
103 ; PTR64-NEXT: ret i8 [[RET]]
105 ; PTR32-LABEL: @testptrptr(
106 ; PTR32-NEXT: [[BUF2:%.*]] = ptrtoint ptr [[BUF:%.*]] to i32
107 ; PTR32-NEXT: [[END1:%.*]] = ptrtoint ptr [[END:%.*]] to i32
108 ; PTR32-NEXT: br label [[LOOPGUARD:%.*]]
110 ; PTR32-NEXT: [[GUARD:%.*]] = icmp ult ptr [[BUF]], [[END]]
111 ; PTR32-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
113 ; PTR32-NEXT: [[TMP1:%.*]] = sub i32 [[END1]], [[BUF2]]
114 ; PTR32-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[BUF]], i32 [[TMP1]]
115 ; PTR32-NEXT: br label [[LOOP:%.*]]
117 ; PTR32-NEXT: [[P_01_US_US:%.*]] = phi ptr [ [[BUF]], [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
118 ; PTR32-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
119 ; PTR32-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
120 ; PTR32-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
121 ; PTR32-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
122 ; PTR32: exit.loopexit:
123 ; PTR32-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
124 ; PTR32-NEXT: br label [[EXIT]]
126 ; PTR32-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
127 ; PTR32-NEXT: ret i8 [[RET]]
132 %guard = icmp ult ptr %buf, %end
133 br i1 %guard, label %preheader, label %exit
139 %p.01.us.us = phi ptr [ %buf, %preheader ], [ %gep, %loop ]
140 %s = phi i8 [0, %preheader], [%snext, %loop]
141 %gep = getelementptr inbounds i8, ptr %p.01.us.us, i64 1
142 %snext = load i8, ptr %gep
143 %cmp = icmp ult ptr %gep, %end
144 br i1 %cmp, label %loop, label %exit
147 %ret = phi i8 [0, %loopguard], [%snext, %loop]
151 define i8 @testnullptrint(ptr %buf, ptr %end) nounwind {
152 ; PTR64-LABEL: @testnullptrint(
153 ; PTR64-NEXT: br label [[LOOPGUARD:%.*]]
155 ; PTR64-NEXT: [[BI:%.*]] = ptrtoint ptr [[BUF:%.*]] to i32
156 ; PTR64-NEXT: [[EI:%.*]] = ptrtoint ptr [[END:%.*]] to i32
157 ; PTR64-NEXT: [[CNT:%.*]] = sub i32 [[EI]], [[BI]]
158 ; PTR64-NEXT: [[GUARD:%.*]] = icmp ult i32 0, [[CNT]]
159 ; PTR64-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
161 ; PTR64-NEXT: [[TMP1:%.*]] = add i32 [[EI]], -1
162 ; PTR64-NEXT: [[TMP2:%.*]] = sub i32 [[TMP1]], [[BI]]
163 ; PTR64-NEXT: [[TMP3:%.*]] = zext i32 [[TMP2]] to i64
164 ; PTR64-NEXT: [[TMP4:%.*]] = add nuw nsw i64 [[TMP3]], 1
165 ; PTR64-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr null, i64 [[TMP4]]
166 ; PTR64-NEXT: br label [[LOOP:%.*]]
168 ; PTR64-NEXT: [[P_01_US_US:%.*]] = phi ptr [ null, [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
169 ; PTR64-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
170 ; PTR64-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
171 ; PTR64-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
172 ; PTR64-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
173 ; PTR64: exit.loopexit:
174 ; PTR64-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
175 ; PTR64-NEXT: br label [[EXIT]]
177 ; PTR64-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
178 ; PTR64-NEXT: ret i8 [[RET]]
180 ; PTR32-LABEL: @testnullptrint(
181 ; PTR32-NEXT: br label [[LOOPGUARD:%.*]]
183 ; PTR32-NEXT: [[BI:%.*]] = ptrtoint ptr [[BUF:%.*]] to i32
184 ; PTR32-NEXT: [[EI:%.*]] = ptrtoint ptr [[END:%.*]] to i32
185 ; PTR32-NEXT: [[CNT:%.*]] = sub i32 [[EI]], [[BI]]
186 ; PTR32-NEXT: [[GUARD:%.*]] = icmp ult i32 0, [[CNT]]
187 ; PTR32-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
189 ; PTR32-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr null, i32 [[CNT]]
190 ; PTR32-NEXT: br label [[LOOP:%.*]]
192 ; PTR32-NEXT: [[P_01_US_US:%.*]] = phi ptr [ null, [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
193 ; PTR32-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
194 ; PTR32-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
195 ; PTR32-NEXT: [[EXITCOND:%.*]] = icmp ne ptr [[GEP]], [[SCEVGEP]]
196 ; PTR32-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
197 ; PTR32: exit.loopexit:
198 ; PTR32-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
199 ; PTR32-NEXT: br label [[EXIT]]
201 ; PTR32-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
202 ; PTR32-NEXT: ret i8 [[RET]]
207 %bi = ptrtoint ptr %buf to i32
208 %ei = ptrtoint ptr %end to i32
209 %cnt = sub i32 %ei, %bi
210 %guard = icmp ult i32 0, %cnt
211 br i1 %guard, label %preheader, label %exit
217 %p.01.us.us = phi ptr [ null, %preheader ], [ %gep, %loop ]
218 %iv = phi i32 [ 0, %preheader ], [ %ivnext, %loop ]
219 %s = phi i8 [0, %preheader], [%snext, %loop]
220 %gep = getelementptr inbounds i8, ptr %p.01.us.us, i64 1
221 %snext = load i8, ptr %gep
222 %ivnext = add i32 %iv, 1
223 %cmp = icmp ult i32 %ivnext, %cnt
224 br i1 %cmp, label %loop, label %exit
227 %ret = phi i8 [0, %loopguard], [%snext, %loop]
231 define i8 @testptrint(ptr %buf, ptr %end) nounwind {
232 ; PTR64-LABEL: @testptrint(
233 ; PTR64-NEXT: br label [[LOOPGUARD:%.*]]
235 ; PTR64-NEXT: [[BI:%.*]] = ptrtoint ptr [[BUF:%.*]] to i32
236 ; PTR64-NEXT: [[EI:%.*]] = ptrtoint ptr [[END:%.*]] to i32
237 ; PTR64-NEXT: [[CNT:%.*]] = sub i32 [[EI]], [[BI]]
238 ; PTR64-NEXT: [[GUARD:%.*]] = icmp ult i32 [[BI]], [[CNT]]
239 ; PTR64-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
241 ; PTR64-NEXT: br label [[LOOP:%.*]]
243 ; PTR64-NEXT: [[P_01_US_US:%.*]] = phi ptr [ [[BUF]], [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
244 ; PTR64-NEXT: [[IV:%.*]] = phi i32 [ [[BI]], [[PREHEADER]] ], [ [[IVNEXT:%.*]], [[LOOP]] ]
245 ; PTR64-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
246 ; PTR64-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
247 ; PTR64-NEXT: [[IVNEXT]] = add nuw i32 [[IV]], 1
248 ; PTR64-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IVNEXT]], [[CNT]]
249 ; PTR64-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
250 ; PTR64: exit.loopexit:
251 ; PTR64-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
252 ; PTR64-NEXT: br label [[EXIT]]
254 ; PTR64-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
255 ; PTR64-NEXT: ret i8 [[RET]]
257 ; PTR32-LABEL: @testptrint(
258 ; PTR32-NEXT: br label [[LOOPGUARD:%.*]]
260 ; PTR32-NEXT: [[BI:%.*]] = ptrtoint ptr [[BUF:%.*]] to i32
261 ; PTR32-NEXT: [[EI:%.*]] = ptrtoint ptr [[END:%.*]] to i32
262 ; PTR32-NEXT: [[CNT:%.*]] = sub i32 [[EI]], [[BI]]
263 ; PTR32-NEXT: [[GUARD:%.*]] = icmp ult i32 [[BI]], [[CNT]]
264 ; PTR32-NEXT: br i1 [[GUARD]], label [[PREHEADER:%.*]], label [[EXIT:%.*]]
266 ; PTR32-NEXT: br label [[LOOP:%.*]]
268 ; PTR32-NEXT: [[P_01_US_US:%.*]] = phi ptr [ [[BUF]], [[PREHEADER]] ], [ [[GEP:%.*]], [[LOOP]] ]
269 ; PTR32-NEXT: [[IV:%.*]] = phi i32 [ [[BI]], [[PREHEADER]] ], [ [[IVNEXT:%.*]], [[LOOP]] ]
270 ; PTR32-NEXT: [[GEP]] = getelementptr inbounds i8, ptr [[P_01_US_US]], i64 1
271 ; PTR32-NEXT: [[SNEXT:%.*]] = load i8, ptr [[GEP]], align 1
272 ; PTR32-NEXT: [[IVNEXT]] = add nuw i32 [[IV]], 1
273 ; PTR32-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IVNEXT]], [[CNT]]
274 ; PTR32-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT_LOOPEXIT:%.*]]
275 ; PTR32: exit.loopexit:
276 ; PTR32-NEXT: [[SNEXT_LCSSA:%.*]] = phi i8 [ [[SNEXT]], [[LOOP]] ]
277 ; PTR32-NEXT: br label [[EXIT]]
279 ; PTR32-NEXT: [[RET:%.*]] = phi i8 [ 0, [[LOOPGUARD]] ], [ [[SNEXT_LCSSA]], [[EXIT_LOOPEXIT]] ]
280 ; PTR32-NEXT: ret i8 [[RET]]
285 %bi = ptrtoint ptr %buf to i32
286 %ei = ptrtoint ptr %end to i32
287 %cnt = sub i32 %ei, %bi
288 %guard = icmp ult i32 %bi, %cnt
289 br i1 %guard, label %preheader, label %exit
295 %p.01.us.us = phi ptr [ %buf, %preheader ], [ %gep, %loop ]
296 %iv = phi i32 [ %bi, %preheader ], [ %ivnext, %loop ]
297 %s = phi i8 [0, %preheader], [%snext, %loop]
298 %gep = getelementptr inbounds i8, ptr %p.01.us.us, i64 1
299 %snext = load i8, ptr %gep
300 %ivnext = add i32 %iv, 1
301 %cmp = icmp ult i32 %ivnext, %cnt
302 br i1 %cmp, label %loop, label %exit
305 %ret = phi i8 [0, %loopguard], [%snext, %loop]
309 ; IV and BECount have two different pointer types here.
310 define void @testnullptr(ptr %base) nounwind {
311 ; PTR64-LABEL: @testnullptr(
313 ; PTR64-NEXT: [[ADD_PTR1603:%.*]] = getelementptr [512 x i8], ptr [[BASE:%.*]], i64 0, i64 512
314 ; PTR64-NEXT: br label [[PREHEADER:%.*]]
316 ; PTR64-NEXT: [[CMP1604192:%.*]] = icmp ult ptr undef, [[ADD_PTR1603]]
317 ; PTR64-NEXT: br i1 [[CMP1604192]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_END1609:%.*]]
318 ; PTR64: for.body.preheader:
319 ; PTR64-NEXT: br label [[FOR_BODY:%.*]]
321 ; PTR64-NEXT: br i1 false, label [[FOR_BODY]], label [[FOR_END1609_LOOPEXIT:%.*]]
322 ; PTR64: for.end1609.loopexit:
323 ; PTR64-NEXT: br label [[FOR_END1609]]
324 ; PTR64: for.end1609:
325 ; PTR64-NEXT: unreachable
327 ; PTR32-LABEL: @testnullptr(
329 ; PTR32-NEXT: [[ADD_PTR1603:%.*]] = getelementptr [512 x i8], ptr [[BASE:%.*]], i64 0, i64 512
330 ; PTR32-NEXT: br label [[PREHEADER:%.*]]
332 ; PTR32-NEXT: [[CMP1604192:%.*]] = icmp ult ptr undef, [[ADD_PTR1603]]
333 ; PTR32-NEXT: br i1 [[CMP1604192]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_END1609:%.*]]
334 ; PTR32: for.body.preheader:
335 ; PTR32-NEXT: br label [[FOR_BODY:%.*]]
337 ; PTR32-NEXT: br i1 false, label [[FOR_BODY]], label [[FOR_END1609_LOOPEXIT:%.*]]
338 ; PTR32: for.end1609.loopexit:
339 ; PTR32-NEXT: br label [[FOR_END1609]]
340 ; PTR32: for.end1609:
341 ; PTR32-NEXT: unreachable
344 %add.ptr1603 = getelementptr [512 x i8], ptr %base, i64 0, i64 512
348 %cmp1604192 = icmp ult ptr undef, %add.ptr1603
349 br i1 %cmp1604192, label %for.body, label %for.end1609
352 %r.17193 = phi ptr [ %incdec.ptr1608, %for.body ], [ null, %preheader ]
353 %incdec.ptr1608 = getelementptr i8, ptr %r.17193, i64 1
354 %cmp1604 = icmp ult ptr %incdec.ptr1608, %add.ptr1603
355 br i1 %cmp1604, label %for.body, label %for.end1609