1 ; RUN: opt < %s -indvars -S | FileCheck %s
3 ; Make sure that indvars isn't inserting canonical IVs.
4 ; This is kinda hard to do until linear function test replacement is removed.
6 target datalayout = "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"
8 define i32 @sum(i32* %arr, i32 %n) nounwind {
10 %precond = icmp slt i32 0, %n
11 br i1 %precond, label %ph, label %return
18 ; We should only have 2 IVs.
23 ; sext should be eliminated while preserving gep inboundsness.
25 ; CHECK: getelementptr inbounds
28 %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
29 %s.01 = phi i32 [ 0, %ph ], [ %sinc, %loop ]
30 %ofs = sext i32 %i.02 to i64
31 %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
32 %val = load i32, i32* %adr
33 %sinc = add nsw i32 %s.01, %val
34 %iinc = add nsw i32 %i.02, 1
35 %cond = icmp slt i32 %iinc, %n
36 br i1 %cond, label %loop, label %exit
39 %s.lcssa = phi i32 [ %sinc, %loop ]
43 %s.0.lcssa = phi i32 [ %s.lcssa, %exit ], [ 0, %entry ]
47 define i64 @suml(i32* %arr, i32 %n) nounwind {
49 %precond = icmp slt i32 0, %n
50 br i1 %precond, label %ph, label %return
57 ; We should only have 2 IVs.
62 ; %ofs sext should be eliminated while preserving gep inboundsness.
64 ; CHECK: getelementptr inbounds
65 ; %vall sext should obviously not be eliminated
69 %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
70 %s.01 = phi i64 [ 0, %ph ], [ %sinc, %loop ]
71 %ofs = sext i32 %i.02 to i64
72 %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
73 %val = load i32, i32* %adr
74 %vall = sext i32 %val to i64
75 %sinc = add nsw i64 %s.01, %vall
76 %iinc = add nsw i32 %i.02, 1
77 %cond = icmp slt i32 %iinc, %n
78 br i1 %cond, label %loop, label %exit
81 %s.lcssa = phi i64 [ %sinc, %loop ]
85 %s.0.lcssa = phi i64 [ %s.lcssa, %exit ], [ 0, %entry ]
89 define void @outofbounds(i32* %first, i32* %last, i32 %idx) nounwind {
90 %precond = icmp ne i32* %first, %last
91 br i1 %precond, label %ph, label %return
94 ; It's not indvars' job to perform LICM on %ofs
101 ; Preserve exactly one pointer type IV.
105 ; Don't create any extra adds.
108 ; Preserve gep inboundsness, and don't factor it.
109 ; CHECK: getelementptr inbounds i32, i32* %ptriv, i32 1
113 %ptriv = phi i32* [ %first, %ph ], [ %ptrpost, %loop ]
114 %ofs = sext i32 %idx to i64
115 %adr = getelementptr inbounds i32, i32* %ptriv, i64 %ofs
116 store i32 3, i32* %adr
117 %ptrpost = getelementptr inbounds i32, i32* %ptriv, i32 1
118 %cond = icmp ne i32* %ptrpost, %last
119 br i1 %cond, label %loop, label %exit
128 %structI = type { i32 }
130 define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %structI* %base)
141 ; CHECK: getelementptr
144 %iv = phi i32 [%start, %entry], [%next, %loop]
145 %p = phi %structI* [%base, %entry], [%pinc, %loop]
146 %adr = getelementptr %structI, %structI* %p, i32 0, i32 0
147 store i32 3, i32* %adr
148 %pp = bitcast %structI* %p to i32*
149 store i32 4, i32* %pp
150 %pinc = getelementptr %structI, %structI* %p, i32 1
151 %next = add i32 %iv, 1
152 %cond = icmp ne i32 %next, %limit
153 br i1 %cond, label %loop, label %exit
159 define void @maxvisitor(i32 %limit, i32* %base) nounwind {
163 ; Test inserting a truncate at a phi use.
170 %idx = phi i32 [ 0, %entry ], [ %idx.next, %loop.inc ]
171 %max = phi i32 [ 0, %entry ], [ %max.next, %loop.inc ]
172 %idxprom = sext i32 %idx to i64
173 %adr = getelementptr inbounds i32, i32* %base, i64 %idxprom
174 %val = load i32, i32* %adr
175 %cmp19 = icmp sgt i32 %val, %max
176 br i1 %cmp19, label %if.then, label %if.else
185 %max.next = phi i32 [ %idx, %if.then ], [ %max, %if.else ]
186 %idx.next = add nsw i32 %idx, 1
187 %cmp = icmp slt i32 %idx.next, %limit
188 br i1 %cmp, label %loop, label %exit
194 define void @identityphi(i32 %limit) nounwind {
198 ; Test an edge case of removing an identity phi that directly feeds
199 ; back to the loop iv.
205 %iv = phi i32 [ 0, %entry], [ %iv.next, %control ]
206 br i1 undef, label %if.then, label %control
212 %iv.next = phi i32 [ %iv, %loop ], [ undef, %if.then ]
213 %cmp = icmp slt i32 %iv.next, %limit
214 br i1 %cmp, label %loop, label %exit
220 define i64 @cloneOr(i32 %limit, i64* %base) nounwind {
222 ; ensure that the loop can't overflow
223 %halfLim = ashr i32 %limit, 2
226 ; Test cloning an or, which is not an OverflowBinaryOperator.
235 %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
236 %t1 = sext i32 %iv to i64
237 %adr = getelementptr i64, i64* %base, i64 %t1
238 %val = load i64, i64* %adr
240 %t3 = sext i32 %t2 to i64
241 %iv.next = add i32 %iv, 2
242 %cmp = icmp slt i32 %iv.next, %halfLim
243 br i1 %cmp, label %loop, label %exit
246 %result = and i64 %val, %t3
250 ; The i induction variable looks like a wrap-around, but it really is just
251 ; a simple affine IV. Make sure that indvars simplifies through.
252 define i32 @indirectRecurrence() nounwind {
256 ; ReplaceLoopExitValue should fold the return value to constant 9.
261 %j.0 = phi i32 [ 1, %entry ], [ %j.next, %cond_true ]
262 %i.0 = phi i32 [ 0, %entry ], [ %j.0, %cond_true ]
263 %tmp = icmp ne i32 %j.0, 10
264 br i1 %tmp, label %cond_true, label %return
267 %j.next = add i32 %j.0, 1
274 ; Eliminate the congruent phis j, k, and l.
275 ; Eliminate the redundant IV increments k.next and l.next.
276 ; Two phis should remain, one starting at %init, and one at %init1.
277 ; Two increments should remain, one by %step and one by %step1.
288 ; Five live-outs should remain.
296 define i32 @isomorphic(i32 %init, i32 %step, i32 %lim) nounwind {
298 %step1 = add i32 %step, 1
299 %init1 = add i32 %init, %step1
300 %l.0 = sub i32 %init1, %step1
304 %ii = phi i32 [ %init1, %entry ], [ %ii.next, %loop ]
305 %i = phi i32 [ %init, %entry ], [ %ii, %loop ]
306 %j = phi i32 [ %init, %entry ], [ %j.next, %loop ]
307 %k = phi i32 [ %init1, %entry ], [ %k.next, %loop ]
308 %l = phi i32 [ %l.0, %entry ], [ %l.next, %loop ]
309 %ii.next = add i32 %ii, %step1
310 %j.next = add i32 %j, %step1
311 %k.next = add i32 %k, %step1
312 %l.step = add i32 %l, %step
313 %l.next = add i32 %l.step, 1
314 %cmp = icmp ne i32 %ii.next, %lim
315 br i1 %cmp, label %loop, label %return
318 %sum1 = add i32 %i, %j.next
319 %sum2 = add i32 %sum1, %k.next
320 %sum3 = add i32 %sum1, %l.step
321 %sum4 = add i32 %sum1, %l.next
325 ; Test a GEP IV that is derived from another GEP IV by a nop gep that
326 ; lowers the type without changing the expression.
327 %structIF = type { i32, float }
329 define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
331 %first = getelementptr inbounds %structIF, %structIF* %base, i64 0, i32 0
335 ; CHECK: phi %structIF*
337 ; CHECK: getelementptr inbounds
338 ; CHECK-NOT: getelementptr
341 %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
342 %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
343 store i32 4, i32* %next
344 br i1 undef, label %latch, label %exit
346 latch: ; preds = %for.inc50.i
347 %ptr.inc = getelementptr inbounds %structIF, %structIF* %ptr.iv, i64 1
348 %next.inc = getelementptr inbounds %structIF, %structIF* %ptr.inc, i64 0, i32 0
355 declare void @use32(i32 %x)
356 declare void @use64(i64 %x)
358 ; Test a widened IV that is used by a phi on different paths within the loop.
367 define void @phiUsesTrunc() nounwind {
369 br i1 undef, label %for.body, label %for.end
372 %iv = phi i32 [ %inc, %for.inc ], [ 1, %entry ]
373 br i1 undef, label %if.then, label %if.else
376 br i1 undef, label %if.then33, label %for.inc
382 br i1 undef, label %if.then97, label %for.inc
385 %idxprom100 = sext i32 %iv to i64
386 call void @use64(i64 %idxprom100)
390 %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
391 call void @use32(i32 %kmin.1)
392 %inc = add nsw i32 %iv, 1
393 br i1 undef, label %for.body, label %for.end