CannotBeOrderedLessThanZero: add some missing cases
[oota-llvm.git] / test / Transforms / IndVarSimplify / widen-loop-comp.ll
1 ; RUN: opt < %s -indvars -S | FileCheck %s
2 target triple = "aarch64--linux-gnu"
3
4 ; Provide legal integer types.
5 target datalayout = "n8:16:32:64"
6
7
8 ; Check the loop exit i32 compare instruction and operand are widened to i64
9 ; instead of truncating IV before its use in the i32 compare instruction.
10
11 @idx = common global i32 0, align 4
12 @e = common global i32 0, align 4
13 @ptr = common global i32* null, align 8
14
15 ; CHECK-LABEL: @test1
16 ; CHECK: for.body.lr.ph:
17 ; CHECK: sext i32
18 ; CHECK: for.cond:
19 ; CHECK: icmp slt i64
20 ; CHECK: for.body:
21 ; CHECK: phi i64
22
23 define i32 @test1() {
24 entry:
25   store i32 -1, i32* @idx, align 4
26   %0 = load i32, i32* @e, align 4
27   %cmp4 = icmp slt i32 %0, 0
28   br i1 %cmp4, label %for.end.loopexit, label %for.body.lr.ph
29
30 for.body.lr.ph:
31   %1 = load i32*, i32** @ptr, align 8
32   %2 = load i32, i32* @e, align 4
33   br label %for.body
34
35 for.cond:
36   %inc = add nsw i32 %i.05, 1
37   %cmp = icmp slt i32 %i.05, %2
38   br i1 %cmp, label %for.body, label %for.cond.for.end.loopexit_crit_edge
39
40 for.body:
41   %i.05 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.cond ]
42   %idxprom = sext i32 %i.05 to i64
43   %arrayidx = getelementptr inbounds i32, i32* %1, i64 %idxprom
44   %3 = load i32, i32* %arrayidx, align 4
45   %tobool = icmp eq i32 %3, 0
46   br i1 %tobool, label %if.then, label %for.cond
47
48 if.then:
49   %i.05.lcssa = phi i32 [ %i.05, %for.body ]
50   store i32 %i.05.lcssa, i32* @idx, align 4
51   br label %for.end
52
53 for.cond.for.end.loopexit_crit_edge:
54   br label %for.end.loopexit
55
56 for.end.loopexit:
57   br label %for.end
58
59 for.end:
60   %4 = load i32, i32* @idx, align 4
61   ret i32 %4
62 }
63
64 ; CHECK-LABEL: @test2
65 ; CHECK: for.body4.us
66 ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
67 ; CHECK: %cmp2.us = icmp slt i64
68 ; CHECK-NOT: %2 = trunc i64 %indvars.iv.next to i32
69 ; CHECK-NOT: %cmp2.us = icmp slt i32
70
71 define void @test2([8 x i8]* %a, i8* %b, i8 %limit) {
72 entry:
73   %conv = zext i8 %limit to i32
74   br i1 undef, label %for.cond1.preheader, label %for.cond1.preheader.us
75
76 for.cond1.preheader.us:
77   %storemerge5.us = phi i32 [ 0, %entry ], [ %inc14.us, %for.inc13.us ]
78   br i1 true, label %for.body4.lr.ph.us, label %for.inc13.us
79
80 for.inc13.us:
81   %inc14.us = add nsw i32 %storemerge5.us, 1
82   %cmp.us = icmp slt i32 %inc14.us, 4
83   br i1 %cmp.us, label %for.cond1.preheader.us, label %for.end
84
85 for.body4.us:
86   %storemerge14.us = phi i32 [ 0, %for.body4.lr.ph.us ], [ %inc.us, %for.body4.us ]
87   %idxprom.us = sext i32 %storemerge14.us to i64
88   %arrayidx6.us = getelementptr inbounds [8 x i8], [8 x i8]* %a, i64 %idxprom5.us, i64 %idxprom.us
89   %0 = load i8, i8* %arrayidx6.us, align 1
90   %idxprom7.us = zext i8 %0 to i64
91   %arrayidx8.us = getelementptr inbounds i8, i8* %b, i64 %idxprom7.us
92   %1 = load i8, i8* %arrayidx8.us, align 1
93   store i8 %1, i8* %arrayidx6.us, align 1
94   %inc.us = add nsw i32 %storemerge14.us, 1
95   %cmp2.us = icmp slt i32 %inc.us, %conv
96   br i1 %cmp2.us, label %for.body4.us, label %for.inc13.us
97
98 for.body4.lr.ph.us:
99   %idxprom5.us = sext i32 %storemerge5.us to i64
100   br label %for.body4.us
101
102 for.cond1.preheader:
103   %storemerge5 = phi i32 [ 0, %entry ], [ %inc14, %for.inc13 ]
104   br i1 false, label %for.inc13, label %for.inc13
105
106 for.inc13:
107   %inc14 = add nsw i32 %storemerge5, 1
108   %cmp = icmp slt i32 %inc14, 4
109   br i1 %cmp, label %for.cond1.preheader, label %for.end
110
111 for.end:
112   ret void
113 }
114
115 ; CHECK-LABEL: @test3
116 ; CHECK: sext i32 %b
117 ; CHECK: for.cond:
118 ; CHECK: phi i64
119 ; CHECK: icmp slt i64
120
121 define i32 @test3(i32* %a, i32 %b) {
122 entry:
123   br label %for.cond
124
125 for.cond:
126   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
127   %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
128   %cmp = icmp slt i32 %i.0, %b
129   br i1 %cmp, label %for.body, label %for.end
130
131 for.body:
132   %idxprom = sext i32 %i.0 to i64
133   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
134   %0 = load i32, i32* %arrayidx, align 4
135   %add = add nsw i32 %sum.0, %0
136   %inc = add nsw i32 %i.0, 1
137   br label %for.cond
138
139 for.end:
140   ret i32 %sum.0
141 }
142
143 declare i32 @fn1(i8 signext)
144
145 ; PR21030
146 ; CHECK-LABEL: @test4
147 ; CHECK: for.body:
148 ; CHECK: phi i32
149 ; CHECK: icmp sgt i8
150
151 define i32 @test4(i32 %a) {
152 entry:
153   br label %for.body
154
155 for.body:
156   %c.07 = phi i8 [ -3, %entry ], [ %dec, %for.body ]
157   %conv6 = zext i8 %c.07 to i32
158   %or = or i32 %a, %conv6
159   %conv3 = trunc i32 %or to i8
160   %call = call i32 @fn1(i8 signext %conv3)
161   %dec = add i8 %c.07, -1
162   %cmp = icmp sgt i8 %dec, -14
163   br i1 %cmp, label %for.body, label %for.end
164
165 for.end:
166   ret i32 0
167 }
168
169 ; CHECK-LABEL: @test5
170 ; CHECK: zext i32 %b
171 ; CHECK: for.cond:
172 ; CHECK: phi i64
173 ; CHECK: icmp ule i64
174
175 define i32 @test5(i32* %a, i32 %b) {
176 entry:
177   br label %for.cond
178
179 for.cond:
180   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
181   %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
182   %cmp = icmp ule i32 %i.0, %b
183   br i1 %cmp, label %for.body, label %for.end
184
185 for.body:
186   %idxprom = zext i32 %i.0 to i64
187   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
188   %0 = load i32, i32* %arrayidx, align 4
189   %add = add nsw i32 %sum.0, %0
190   %inc = add nsw i32 %i.0, 1
191   br label %for.cond
192
193 for.end:
194   ret i32 %sum.0
195 }
196
197 define i32 @test6(i32* %a, i32 %b) {
198 ; CHECK-LABEL: @test6(
199 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
200 ; CHECK: for.cond:
201 ; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]]
202
203 entry:
204   br label %for.cond
205
206 for.cond:
207   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
208   %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
209   %cmp = icmp sle i32 %i.0, %b
210   br i1 %cmp, label %for.body, label %for.end
211
212 for.body:
213   %idxprom = zext i32 %i.0 to i64
214   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
215   %0 = load i32, i32* %arrayidx, align 4
216   %add = add nsw i32 %sum.0, %0
217   %inc = add nsw i32 %i.0, 1
218   br label %for.cond
219
220 for.end:
221   ret i32 %sum.0
222 }
223
224 define i32 @test7(i32* %a, i32 %b) {
225 ; CHECK-LABEL: @test7(
226 ; CHECK: [[B_ZEXT:%[a-z0-9]+]] = zext i32 %b to i64
227 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
228 ; CHECK: for.cond:
229 ; CHECK: icmp ule i64 %indvars.iv, [[B_ZEXT]]
230 ; CHECK: for.body:
231 ; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]]
232
233 entry:
234   br label %for.cond
235
236 for.cond:
237   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
238   %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
239   %cmp = icmp ule i32 %i.0, %b
240   br i1 %cmp, label %for.body, label %for.end
241
242 for.body:
243   %idxprom = sext i32 %i.0 to i64
244   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
245   %0 = load i32, i32* %arrayidx, align 4
246   %add = add nsw i32 %sum.0, %0
247   %inc = add nsw i32 %i.0, 1
248   %cmp2 = icmp sle i32 %i.0, %b
249   br i1 %cmp2, label %for.cond, label %for.end
250
251 for.end:
252   ret i32 %sum.0
253 }
254
255 define i32 @test8(i32* %a, i32 %b, i32 %init) {
256 ; CHECK-LABEL: @test8(
257 ; CHECK: [[INIT_SEXT:%[a-z0-9]+]] = sext i32 %init to i64
258 ; CHECK: [[B_ZEXT:%[a-z0-9]+]] = zext i32 %b to i64
259 ; CHECK: for.cond:
260 ;     Note: %indvars.iv is the sign extension of %i.0
261 ; CHECK: %indvars.iv = phi i64 [ [[INIT_SEXT]], %for.cond.preheader ], [ %indvars.iv.next, %for.body ]
262 ; CHECK: icmp ule i64 %indvars.iv, [[B_ZEXT]]
263
264 entry:
265   %e = icmp sgt i32 %init, 0
266   br i1 %e, label %for.cond, label %leave
267
268 for.cond:
269   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
270   %i.0 = phi i32 [ %init, %entry ], [ %inc, %for.body ]
271   %cmp = icmp ule i32 %i.0, %b
272   br i1 %cmp, label %for.body, label %for.end
273
274 for.body:
275   %idxprom = sext i32 %i.0 to i64
276   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
277   %0 = load i32, i32* %arrayidx, align 4
278   %add = add nsw i32 %sum.0, %0
279   %inc = add nsw i32 %i.0, 1
280   %cmp2 = icmp slt i32 0, %inc
281   br i1 %cmp2, label %for.cond, label %for.end
282
283 for.end:
284   ret i32 %sum.0
285
286 leave:
287   ret i32 0
288 }
289
290 define i32 @test9(i32* %a, i32 %b, i32 %init) {
291 ; CHECK-LABEL: @test9(
292 ; CHECK: [[INIT_ZEXT:%[a-z0-9]+]] = zext i32 %init to i64
293 ; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64
294 ; CHECK: for.cond:
295 ;     Note: %indvars.iv is the zero extension of %i.0
296 ; CHECK: %indvars.iv = phi i64 [ [[INIT_ZEXT]], %for.cond.preheader ], [ %indvars.iv.next, %for.body ]
297 ; CHECK: icmp slt i64 %indvars.iv, [[B_SEXT]]
298
299 entry:
300   %e = icmp sgt i32 %init, 0
301   br i1 %e, label %for.cond, label %leave
302
303 for.cond:
304   %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]
305   %i.0 = phi i32 [ %init, %entry ], [ %inc, %for.body ]
306   %cmp = icmp slt i32 %i.0, %b
307   br i1 %cmp, label %for.body, label %for.end
308
309 for.body:
310   %idxprom = zext i32 %i.0 to i64
311   %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
312   %0 = load i32, i32* %arrayidx, align 4
313   %add = add nsw i32 %sum.0, %0
314   %inc = add nsw i32 %i.0, 1
315   %cmp2 = icmp slt i32 0, %inc
316   br i1 %cmp2, label %for.cond, label %for.end
317
318 for.end:
319   ret i32 %sum.0
320
321 leave:
322   ret i32 0
323 }
324
325 declare void @consume.i64(i64)
326 declare void @consume.i1(i1)
327
328 define i32 @test10(i32 %v) {
329 ; CHECK-LABEL: @test10(
330  entry:
331 ; CHECK-NOT: zext
332   br label %loop
333
334  loop:
335 ; CHECK: loop:
336 ; CHECK: %indvars.iv = phi i64 [ %indvars.iv.next, %loop ], [ 0, %entry ]
337 ; CHECK: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
338 ; CHECK: [[MUL:%[a-z0-9]+]] = mul nsw i64 %indvars.iv, -1
339 ; CHECK: [[MUL_TRUNC:%[a-z0-9]+]] = trunc i64 [[MUL]] to i32
340 ; CHECK: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[MUL_TRUNC]], %v
341 ; CHECK: call void @consume.i1(i1 [[CMP]])
342
343   %i = phi i32 [ 0, %entry ], [ %i.inc, %loop ]
344   %i.inc = add i32 %i, 1
345   %iv = mul i32 %i, -1
346   %cmp = icmp eq i32 %iv, %v
347   call void @consume.i1(i1 %cmp)
348   %be.cond = icmp slt i32 %i.inc, 11
349   %ext = sext i32 %iv to i64
350   call void @consume.i64(i64 %ext)
351   br i1 %be.cond, label %loop, label %leave
352
353  leave:
354   ret i32 22
355 }