1; RUN: opt < %s -indvars -S | FileCheck %s
2;
3; Make sure that indvars isn't inserting canonical IVs.
4; This is kinda hard to do until linear function test replacement is removed.
5
6target 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"
7
8define i32 @sum(i32* %arr, i32 %n) nounwind {
9entry:
10  %precond = icmp slt i32 0, %n
11  br i1 %precond, label %ph, label %return
12
13ph:
14  br label %loop
15
16; CHECK: loop:
17;
18; We should only have 2 IVs.
19; CHECK: phi
20; CHECK: phi
21; CHECK-NOT: phi
22;
23; sext should be eliminated while preserving gep inboundsness.
24; CHECK-NOT: sext
25; CHECK: getelementptr inbounds
26; CHECK: exit:
27loop:
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* %arr, i64 %ofs
32  %val = load 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
37
38exit:
39  %s.lcssa = phi i32 [ %sinc, %loop ]
40  br label %return
41
42return:
43  %s.0.lcssa = phi i32 [ %s.lcssa, %exit ], [ 0, %entry ]
44  ret i32 %s.0.lcssa
45}
46
47define i64 @suml(i32* %arr, i32 %n) nounwind {
48entry:
49  %precond = icmp slt i32 0, %n
50  br i1 %precond, label %ph, label %return
51
52ph:
53  br label %loop
54
55; CHECK: loop:
56;
57; We should only have 2 IVs.
58; CHECK: phi
59; CHECK: phi
60; CHECK-NOT: phi
61;
62; %ofs sext should be eliminated while preserving gep inboundsness.
63; CHECK-NOT: sext
64; CHECK: getelementptr inbounds
65; %vall sext should obviously not be eliminated
66; CHECK: sext
67; CHECK: exit:
68loop:
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* %arr, i64 %ofs
73  %val = load 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
79
80exit:
81  %s.lcssa = phi i64 [ %sinc, %loop ]
82  br label %return
83
84return:
85  %s.0.lcssa = phi i64 [ %s.lcssa, %exit ], [ 0, %entry ]
86  ret i64 %s.0.lcssa
87}
88
89define void @outofbounds(i32* %first, i32* %last, i32 %idx) nounwind {
90  %precond = icmp ne i32* %first, %last
91  br i1 %precond, label %ph, label %return
92
93; CHECK: ph:
94; It's not indvars' job to perform LICM on %ofs
95; CHECK-NOT: sext
96ph:
97  br label %loop
98
99; CHECK: loop:
100;
101; Preserve exactly one pointer type IV.
102; CHECK: phi i32*
103; CHECK-NOT: phi
104;
105; Don't create any extra adds.
106; CHECK-NOT: add
107;
108; Preserve gep inboundsness, and don't factor it.
109; CHECK: getelementptr inbounds i32* %ptriv, i32 1
110; CHECK-NOT: add
111; CHECK: exit:
112loop:
113  %ptriv = phi i32* [ %first, %ph ], [ %ptrpost, %loop ]
114  %ofs = sext i32 %idx to i64
115  %adr = getelementptr inbounds i32* %ptriv, i64 %ofs
116  store i32 3, i32* %adr
117  %ptrpost = getelementptr inbounds i32* %ptriv, i32 1
118  %cond = icmp ne i32* %ptrpost, %last
119  br i1 %cond, label %loop, label %exit
120
121exit:
122  br label %return
123
124return:
125  ret void
126}
127
128%structI = type { i32 }
129
130define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %structI* %base)
131nounwind
132{
133entry:
134  br label %loop
135
136; CHECK: loop:
137;
138; Preserve casts
139; CHECK: phi i32
140; CHECK: bitcast
141; CHECK: getelementptr
142; CHECK: exit:
143loop:
144  %iv = phi i32 [%start, %entry], [%next, %loop]
145  %p = phi %structI* [%base, %entry], [%pinc, %loop]
146  %adr = getelementptr %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* %p, i32 1
151  %next = add i32 %iv, 1
152  %cond = icmp ne i32 %next, %limit
153  br i1 %cond, label %loop, label %exit
154
155exit:
156  ret void
157}
158
159define void @maxvisitor(i32 %limit, i32* %base) nounwind {
160entry:
161 br label %loop
162
163; Test inserting a truncate at a phi use.
164;
165; CHECK: loop:
166; CHECK: phi i64
167; CHECK: trunc
168; CHECK: exit:
169loop:
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* %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
177
178if.then:
179  br label %loop.inc
180
181if.else:
182  br label %loop.inc
183
184loop.inc:
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
189
190exit:
191  ret void
192}
193
194define void @identityphi(i32 %limit) nounwind {
195entry:
196  br label %loop
197
198; Test an edge case of removing an identity phi that directly feeds
199; back to the loop iv.
200;
201; CHECK: loop:
202; CHECK-NOT: phi
203; CHECK: exit:
204loop:
205  %iv = phi i32 [ 0, %entry], [ %iv.next, %control ]
206  br i1 undef, label %if.then, label %control
207
208if.then:
209  br label %control
210
211control:
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
215
216exit:
217  ret void
218}
219
220define i64 @cloneOr(i32 %limit, i64* %base) nounwind {
221entry:
222  ; ensure that the loop can't overflow
223  %halfLim = ashr i32 %limit, 2
224  br label %loop
225
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
230; equivalent.
231;
232; CHECK: loop:
233; CHECK: phi i64
234; CHECK-NOT: sext
235; CHECK: icmp slt i32
236; CHECK: exit:
237; CHECK: add i64
238loop:
239  %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
240  %t1 = sext i32 %iv to i64
241  %adr = getelementptr i64* %base, i64 %t1
242  %val = load i64* %adr
243  %t2 = or i32 %iv, 1
244  %t3 = sext i32 %t2 to i64
245  %iv.next = add i32 %iv, 2
246  %cmp = icmp slt i32 %iv.next, %halfLim
247  br i1 %cmp, label %loop, label %exit
248
249exit:
250  %result = and i64 %val, %t3
251  ret i64 %result
252}
253
254; The i induction variable looks like a wrap-around, but it really is just
255; a simple affine IV.  Make sure that indvars simplifies through.
256define i32 @indirectRecurrence() nounwind {
257entry:
258  br label %loop
259
260; ReplaceLoopExitValue should fold the return value to constant 9.
261; CHECK: loop:
262; CHECK: phi i32
263; CHECK: ret i32 9
264loop:
265  %j.0 = phi i32 [ 1, %entry ], [ %j.next, %cond_true ]
266  %i.0 = phi i32 [ 0, %entry ], [ %j.0, %cond_true ]
267  %tmp = icmp ne i32 %j.0, 10
268  br i1 %tmp, label %cond_true, label %return
269
270cond_true:
271  %j.next = add i32 %j.0, 1
272  br label %loop
273
274return:
275  ret i32 %i.0
276}
277
278; Eliminate the congruent phis j, k, and l.
279; Eliminate the redundant IV increments k.next and l.next.
280; Two phis should remain, one starting at %init, and one at %init1.
281; Two increments should remain, one by %step and one by %step1.
282; CHECK: loop:
283; CHECK: phi i32
284; CHECK: phi i32
285; CHECK-NOT: phi
286; CHECK: add i32
287; CHECK: add i32
288; CHECK: add i32
289; CHECK-NOT: add
290; CHECK: return:
291;
292; Five live-outs should remain.
293; CHECK: lcssa = phi
294; CHECK: lcssa = phi
295; CHECK: lcssa = phi
296; CHECK: lcssa = phi
297; CHECK: lcssa = phi
298; CHECK-NOT: phi
299; CHECK: ret
300define i32 @isomorphic(i32 %init, i32 %step, i32 %lim) nounwind {
301entry:
302  %step1 = add i32 %step, 1
303  %init1 = add i32 %init, %step1
304  %l.0 = sub i32 %init1, %step1
305  br label %loop
306
307loop:
308  %ii = phi i32 [ %init1, %entry ], [ %ii.next, %loop ]
309  %i = phi i32 [ %init, %entry ], [ %ii, %loop ]
310  %j = phi i32 [ %init, %entry ], [ %j.next, %loop ]
311  %k = phi i32 [ %init1, %entry ], [ %k.next, %loop ]
312  %l = phi i32 [ %l.0, %entry ], [ %l.next, %loop ]
313  %ii.next = add i32 %ii, %step1
314  %j.next = add i32 %j, %step1
315  %k.next = add i32 %k, %step1
316  %l.step = add i32 %l, %step
317  %l.next = add i32 %l.step, 1
318  %cmp = icmp ne i32 %ii.next, %lim
319  br i1 %cmp, label %loop, label %return
320
321return:
322  %sum1 = add i32 %i, %j.next
323  %sum2 = add i32 %sum1, %k.next
324  %sum3 = add i32 %sum1, %l.step
325  %sum4 = add i32 %sum1, %l.next
326  ret i32 %sum4
327}
328
329; Test a GEP IV that is derived from another GEP IV by a nop gep that
330; lowers the type without changing the expression.
331%structIF = type { i32, float }
332
333define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
334entry:
335  %first = getelementptr inbounds %structIF* %base, i64 0, i32 0
336  br label %loop
337
338; CHECK: loop:
339; CHECK: phi %structIF*
340; CHECK-NOT: phi
341; CHECK: getelementptr inbounds
342; CHECK-NOT: getelementptr
343; CHECK: exit:
344loop:
345  %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
346  %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
347  store i32 4, i32* %next
348  br i1 undef, label %latch, label %exit
349
350latch:                         ; preds = %for.inc50.i
351  %ptr.inc = getelementptr inbounds %structIF* %ptr.iv, i64 1
352  %next.inc = getelementptr inbounds %structIF* %ptr.inc, i64 0, i32 0
353  br label %loop
354
355exit:
356  ret void
357}
358
359; Test a widened IV that is used by a phi on different paths within the loop.
360;
361; CHECK: for.body:
362; CHECK: phi i64
363; CHECK: trunc i64
364; CHECK: if.then:
365; CHECK: for.inc:
366; CHECK: phi i32
367; CHECK: for.end:
368define void @phiUsesTrunc() nounwind {
369entry:
370  br i1 undef, label %for.body, label %for.end
371
372for.body:
373  %iv = phi i32 [ %inc, %for.inc ], [ 1, %entry ]
374  br i1 undef, label %if.then, label %if.else
375
376if.then:
377  br i1 undef, label %if.then33, label %for.inc
378
379if.then33:
380  br label %for.inc
381
382if.else:
383  br i1 undef, label %if.then97, label %for.inc
384
385if.then97:
386  %idxprom100 = sext i32 %iv to i64
387  br label %for.inc
388
389for.inc:
390  %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
391  %inc = add nsw i32 %iv, 1
392  br i1 undef, label %for.body, label %for.end
393
394for.end:
395  ret void
396}
397