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
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
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* %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 ; This test originally checked that the OR instruction was cloned. Now the
227 ; ScalarEvolution is able to understand the loop evolution and that '%iv' at the
228 ; end of the loop is an even value. Thus '%val' is computed at the end of the
229 ; loop and the OR instruction is replaced by an ADD keeping the result
236 ; CHECK: icmp slt i64
240 %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
241 %t1 = sext i32 %iv to i64
242 %adr = getelementptr i64, i64* %base, i64 %t1
243 %val = load i64* %adr
245 %t3 = sext i32 %t2 to i64
246 %iv.next = add i32 %iv, 2
247 %cmp = icmp slt i32 %iv.next, %halfLim
248 br i1 %cmp, label %loop, label %exit
251 %result = and i64 %val, %t3
255 ; The i induction variable looks like a wrap-around, but it really is just
256 ; a simple affine IV. Make sure that indvars simplifies through.
257 define i32 @indirectRecurrence() nounwind {
261 ; ReplaceLoopExitValue should fold the return value to constant 9.
266 %j.0 = phi i32 [ 1, %entry ], [ %j.next, %cond_true ]
267 %i.0 = phi i32 [ 0, %entry ], [ %j.0, %cond_true ]
268 %tmp = icmp ne i32 %j.0, 10
269 br i1 %tmp, label %cond_true, label %return
272 %j.next = add i32 %j.0, 1
279 ; Eliminate the congruent phis j, k, and l.
280 ; Eliminate the redundant IV increments k.next and l.next.
281 ; Two phis should remain, one starting at %init, and one at %init1.
282 ; Two increments should remain, one by %step and one by %step1.
293 ; Five live-outs should remain.
301 define i32 @isomorphic(i32 %init, i32 %step, i32 %lim) nounwind {
303 %step1 = add i32 %step, 1
304 %init1 = add i32 %init, %step1
305 %l.0 = sub i32 %init1, %step1
309 %ii = phi i32 [ %init1, %entry ], [ %ii.next, %loop ]
310 %i = phi i32 [ %init, %entry ], [ %ii, %loop ]
311 %j = phi i32 [ %init, %entry ], [ %j.next, %loop ]
312 %k = phi i32 [ %init1, %entry ], [ %k.next, %loop ]
313 %l = phi i32 [ %l.0, %entry ], [ %l.next, %loop ]
314 %ii.next = add i32 %ii, %step1
315 %j.next = add i32 %j, %step1
316 %k.next = add i32 %k, %step1
317 %l.step = add i32 %l, %step
318 %l.next = add i32 %l.step, 1
319 %cmp = icmp ne i32 %ii.next, %lim
320 br i1 %cmp, label %loop, label %return
323 %sum1 = add i32 %i, %j.next
324 %sum2 = add i32 %sum1, %k.next
325 %sum3 = add i32 %sum1, %l.step
326 %sum4 = add i32 %sum1, %l.next
330 ; Test a GEP IV that is derived from another GEP IV by a nop gep that
331 ; lowers the type without changing the expression.
332 %structIF = type { i32, float }
334 define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
336 %first = getelementptr inbounds %structIF, %structIF* %base, i64 0, i32 0
340 ; CHECK: phi %structIF*
342 ; CHECK: getelementptr inbounds
343 ; CHECK-NOT: getelementptr
346 %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
347 %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
348 store i32 4, i32* %next
349 br i1 undef, label %latch, label %exit
351 latch: ; preds = %for.inc50.i
352 %ptr.inc = getelementptr inbounds %structIF, %structIF* %ptr.iv, i64 1
353 %next.inc = getelementptr inbounds %structIF, %structIF* %ptr.inc, i64 0, i32 0
360 ; Test a widened IV that is used by a phi on different paths within the loop.
369 define void @phiUsesTrunc() nounwind {
371 br i1 undef, label %for.body, label %for.end
374 %iv = phi i32 [ %inc, %for.inc ], [ 1, %entry ]
375 br i1 undef, label %if.then, label %if.else
378 br i1 undef, label %if.then33, label %for.inc
384 br i1 undef, label %if.then97, label %for.inc
387 %idxprom100 = sext i32 %iv to i64
391 %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
392 %inc = add nsw i32 %iv, 1
393 br i1 undef, label %for.body, label %for.end