• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -indvars -S -indvars-predicate-loops=0  | FileCheck %s
3;
4; Make sure that indvars isn't inserting canonical IVs.
5; This is kinda hard to do until linear function test replacement is removed.
6
7target 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
9; We should only have 2 IVs.
10; sext should be eliminated while preserving gep inboundsness.
11define i32 @sum(i32* %arr, i32 %n) nounwind {
12; CHECK-LABEL: @sum(
13; CHECK-NEXT:  entry:
14; CHECK-NEXT:    [[PRECOND:%.*]] = icmp slt i32 0, [[N:%.*]]
15; CHECK-NEXT:    br i1 [[PRECOND]], label [[PH:%.*]], label [[RETURN:%.*]]
16; CHECK:       ph:
17; CHECK-NEXT:    [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[N]] to i64
18; CHECK-NEXT:    br label [[LOOP:%.*]]
19; CHECK:       loop:
20; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[PH]] ]
21; CHECK-NEXT:    [[S_01:%.*]] = phi i32 [ 0, [[PH]] ], [ [[SINC:%.*]], [[LOOP]] ]
22; CHECK-NEXT:    [[ADR:%.*]] = getelementptr inbounds i32, i32* [[ARR:%.*]], i64 [[INDVARS_IV]]
23; CHECK-NEXT:    [[VAL:%.*]] = load i32, i32* [[ADR]], align 4
24; CHECK-NEXT:    [[SINC]] = add nsw i32 [[S_01]], [[VAL]]
25; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
26; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
27; CHECK-NEXT:    br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT:%.*]]
28; CHECK:       exit:
29; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi i32 [ [[SINC]], [[LOOP]] ]
30; CHECK-NEXT:    br label [[RETURN]]
31; CHECK:       return:
32; CHECK-NEXT:    [[S_0_LCSSA:%.*]] = phi i32 [ [[S_LCSSA]], [[EXIT]] ], [ 0, [[ENTRY:%.*]] ]
33; CHECK-NEXT:    ret i32 [[S_0_LCSSA]]
34;
35entry:
36  %precond = icmp slt i32 0, %n
37  br i1 %precond, label %ph, label %return
38
39ph:
40  br label %loop
41
42loop:
43  %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
44  %s.01 = phi i32 [ 0, %ph ], [ %sinc, %loop ]
45  %ofs = sext i32 %i.02 to i64
46  %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
47  %val = load i32, i32* %adr
48  %sinc = add nsw i32 %s.01, %val
49  %iinc = add nsw i32 %i.02, 1
50  %cond = icmp slt i32 %iinc, %n
51  br i1 %cond, label %loop, label %exit
52
53exit:
54  %s.lcssa = phi i32 [ %sinc, %loop ]
55  br label %return
56
57return:
58  %s.0.lcssa = phi i32 [ %s.lcssa, %exit ], [ 0, %entry ]
59  ret i32 %s.0.lcssa
60}
61
62; We should only have 2 IVs.
63; %ofs sext should be eliminated while preserving gep inboundsness.
64; %vall sext should obviously not be eliminated
65define i64 @suml(i32* %arr, i32 %n) nounwind {
66; CHECK-LABEL: @suml(
67; CHECK-NEXT:  entry:
68; CHECK-NEXT:    [[PRECOND:%.*]] = icmp slt i32 0, [[N:%.*]]
69; CHECK-NEXT:    br i1 [[PRECOND]], label [[PH:%.*]], label [[RETURN:%.*]]
70; CHECK:       ph:
71; CHECK-NEXT:    [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[N]] to i64
72; CHECK-NEXT:    br label [[LOOP:%.*]]
73; CHECK:       loop:
74; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[PH]] ]
75; CHECK-NEXT:    [[S_01:%.*]] = phi i64 [ 0, [[PH]] ], [ [[SINC:%.*]], [[LOOP]] ]
76; CHECK-NEXT:    [[ADR:%.*]] = getelementptr inbounds i32, i32* [[ARR:%.*]], i64 [[INDVARS_IV]]
77; CHECK-NEXT:    [[VAL:%.*]] = load i32, i32* [[ADR]], align 4
78; CHECK-NEXT:    [[VALL:%.*]] = sext i32 [[VAL]] to i64
79; CHECK-NEXT:    [[SINC]] = add nsw i64 [[S_01]], [[VALL]]
80; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
81; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
82; CHECK-NEXT:    br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT:%.*]]
83; CHECK:       exit:
84; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi i64 [ [[SINC]], [[LOOP]] ]
85; CHECK-NEXT:    br label [[RETURN]]
86; CHECK:       return:
87; CHECK-NEXT:    [[S_0_LCSSA:%.*]] = phi i64 [ [[S_LCSSA]], [[EXIT]] ], [ 0, [[ENTRY:%.*]] ]
88; CHECK-NEXT:    ret i64 [[S_0_LCSSA]]
89;
90entry:
91  %precond = icmp slt i32 0, %n
92  br i1 %precond, label %ph, label %return
93
94ph:
95  br label %loop
96
97loop:
98  %i.02 = phi i32 [ 0, %ph ], [ %iinc, %loop ]
99  %s.01 = phi i64 [ 0, %ph ], [ %sinc, %loop ]
100  %ofs = sext i32 %i.02 to i64
101  %adr = getelementptr inbounds i32, i32* %arr, i64 %ofs
102  %val = load i32, i32* %adr
103  %vall = sext i32 %val to i64
104  %sinc = add nsw i64 %s.01, %vall
105  %iinc = add nsw i32 %i.02, 1
106  %cond = icmp slt i32 %iinc, %n
107  br i1 %cond, label %loop, label %exit
108
109exit:
110  %s.lcssa = phi i64 [ %sinc, %loop ]
111  br label %return
112
113return:
114  %s.0.lcssa = phi i64 [ %s.lcssa, %exit ], [ 0, %entry ]
115  ret i64 %s.0.lcssa
116}
117
118; It's not indvars' job to perform LICM on %ofs
119; Preserve exactly one pointer type IV.
120; Don't create any extra adds.
121; Preserve gep inboundsness, and don't factor it.
122define void @outofbounds(i32* %first, i32* %last, i32 %idx) nounwind {
123; CHECK-LABEL: @outofbounds(
124; CHECK-NEXT:    [[PRECOND:%.*]] = icmp ne i32* [[FIRST:%.*]], [[LAST:%.*]]
125; CHECK-NEXT:    br i1 [[PRECOND]], label [[PH:%.*]], label [[RETURN:%.*]]
126; CHECK:       ph:
127; CHECK-NEXT:    br label [[LOOP:%.*]]
128; CHECK:       loop:
129; CHECK-NEXT:    [[PTRIV:%.*]] = phi i32* [ [[FIRST]], [[PH]] ], [ [[PTRPOST:%.*]], [[LOOP]] ]
130; CHECK-NEXT:    [[OFS:%.*]] = sext i32 [[IDX:%.*]] to i64
131; CHECK-NEXT:    [[ADR:%.*]] = getelementptr inbounds i32, i32* [[PTRIV]], i64 [[OFS]]
132; CHECK-NEXT:    store i32 3, i32* [[ADR]], align 4
133; CHECK-NEXT:    [[PTRPOST]] = getelementptr inbounds i32, i32* [[PTRIV]], i32 1
134; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32* [[PTRPOST]], [[LAST]]
135; CHECK-NEXT:    br i1 [[COND]], label [[LOOP]], label [[EXIT:%.*]]
136; CHECK:       exit:
137; CHECK-NEXT:    br label [[RETURN]]
138; CHECK:       return:
139; CHECK-NEXT:    ret void
140;
141  %precond = icmp ne i32* %first, %last
142  br i1 %precond, label %ph, label %return
143
144ph:
145  br label %loop
146
147loop:
148  %ptriv = phi i32* [ %first, %ph ], [ %ptrpost, %loop ]
149  %ofs = sext i32 %idx to i64
150  %adr = getelementptr inbounds i32, i32* %ptriv, i64 %ofs
151  store i32 3, i32* %adr
152  %ptrpost = getelementptr inbounds i32, i32* %ptriv, i32 1
153  %cond = icmp ne i32* %ptrpost, %last
154  br i1 %cond, label %loop, label %exit
155
156exit:
157  br label %return
158
159return:
160  ret void
161}
162
163%structI = type { i32 }
164
165; Preserve casts
166define void @bitcastiv(i32 %start, i32 %limit, i32 %step, %structI* %base)
167; CHECK-LABEL: @bitcastiv(
168; CHECK-NEXT:  entry:
169; CHECK-NEXT:    br label [[LOOP:%.*]]
170; CHECK:       loop:
171; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[NEXT:%.*]], [[LOOP]] ]
172; CHECK-NEXT:    [[P:%.*]] = phi %structI* [ [[BASE:%.*]], [[ENTRY]] ], [ [[PINC:%.*]], [[LOOP]] ]
173; CHECK-NEXT:    [[ADR:%.*]] = getelementptr [[STRUCTI:%.*]], %structI* [[P]], i32 0, i32 0
174; CHECK-NEXT:    store i32 3, i32* [[ADR]], align 4
175; CHECK-NEXT:    [[PP:%.*]] = bitcast %structI* [[P]] to i32*
176; CHECK-NEXT:    store i32 4, i32* [[PP]], align 4
177; CHECK-NEXT:    [[PINC]] = getelementptr [[STRUCTI]], %structI* [[P]], i32 1
178; CHECK-NEXT:    [[NEXT]] = add i32 [[IV]], 1
179; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[NEXT]], [[LIMIT:%.*]]
180; CHECK-NEXT:    br i1 [[COND]], label [[LOOP]], label [[EXIT:%.*]]
181; CHECK:       exit:
182; CHECK-NEXT:    ret void
183;
184nounwind
185{
186entry:
187  br label %loop
188
189loop:
190  %iv = phi i32 [%start, %entry], [%next, %loop]
191  %p = phi %structI* [%base, %entry], [%pinc, %loop]
192  %adr = getelementptr %structI, %structI* %p, i32 0, i32 0
193  store i32 3, i32* %adr
194  %pp = bitcast %structI* %p to i32*
195  store i32 4, i32* %pp
196  %pinc = getelementptr %structI, %structI* %p, i32 1
197  %next = add i32 %iv, 1
198  %cond = icmp ne i32 %next, %limit
199  br i1 %cond, label %loop, label %exit
200
201exit:
202  ret void
203}
204
205; Test inserting a truncate at a phi use.
206define void @maxvisitor(i32 %limit, i32* %base) nounwind {
207; CHECK-LABEL: @maxvisitor(
208; CHECK-NEXT:  entry:
209; CHECK-NEXT:    [[TMP0:%.*]] = icmp sgt i32 [[LIMIT:%.*]], 1
210; CHECK-NEXT:    [[SMAX:%.*]] = select i1 [[TMP0]], i32 [[LIMIT]], i32 1
211; CHECK-NEXT:    [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[SMAX]] to i64
212; CHECK-NEXT:    br label [[LOOP:%.*]]
213; CHECK:       loop:
214; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP_INC:%.*]] ], [ 0, [[ENTRY:%.*]] ]
215; CHECK-NEXT:    [[MAX:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[MAX_NEXT:%.*]], [[LOOP_INC]] ]
216; CHECK-NEXT:    [[ADR:%.*]] = getelementptr inbounds i32, i32* [[BASE:%.*]], i64 [[INDVARS_IV]]
217; CHECK-NEXT:    [[VAL:%.*]] = load i32, i32* [[ADR]], align 4
218; CHECK-NEXT:    [[CMP19:%.*]] = icmp sgt i32 [[VAL]], [[MAX]]
219; CHECK-NEXT:    br i1 [[CMP19]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
220; CHECK:       if.then:
221; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[INDVARS_IV]] to i32
222; CHECK-NEXT:    br label [[LOOP_INC]]
223; CHECK:       if.else:
224; CHECK-NEXT:    br label [[LOOP_INC]]
225; CHECK:       loop.inc:
226; CHECK-NEXT:    [[MAX_NEXT]] = phi i32 [ [[TMP1]], [[IF_THEN]] ], [ [[MAX]], [[IF_ELSE]] ]
227; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
228; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
229; CHECK-NEXT:    br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT:%.*]]
230; CHECK:       exit:
231; CHECK-NEXT:    ret void
232;
233entry:
234  br label %loop
235
236loop:
237  %idx = phi i32 [ 0, %entry ], [ %idx.next, %loop.inc ]
238  %max = phi i32 [ 0, %entry ], [ %max.next, %loop.inc ]
239  %idxprom = sext i32 %idx to i64
240  %adr = getelementptr inbounds i32, i32* %base, i64 %idxprom
241  %val = load i32, i32* %adr
242  %cmp19 = icmp sgt i32 %val, %max
243  br i1 %cmp19, label %if.then, label %if.else
244
245if.then:
246  br label %loop.inc
247
248if.else:
249  br label %loop.inc
250
251loop.inc:
252  %max.next = phi i32 [ %idx, %if.then ], [ %max, %if.else ]
253  %idx.next = add nsw i32 %idx, 1
254  %cmp = icmp slt i32 %idx.next, %limit
255  br i1 %cmp, label %loop, label %exit
256
257exit:
258  ret void
259}
260
261; Test an edge case of removing an identity phi that directly feeds
262; back to the loop iv.
263define void @identityphi(i32 %limit) nounwind {
264; CHECK-LABEL: @identityphi(
265; CHECK-NEXT:  entry:
266; CHECK-NEXT:    br label [[LOOP:%.*]]
267; CHECK:       loop:
268; CHECK-NEXT:    br i1 undef, label [[IF_THEN:%.*]], label [[CONTROL:%.*]]
269; CHECK:       if.then:
270; CHECK-NEXT:    br label [[CONTROL]]
271; CHECK:       control:
272; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 0, [[LIMIT:%.*]]
273; CHECK-NEXT:    br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
274; CHECK:       exit:
275; CHECK-NEXT:    ret void
276;
277entry:
278  br label %loop
279
280loop:
281  %iv = phi i32 [ 0, %entry], [ %iv.next, %control ]
282  br i1 undef, label %if.then, label %control
283
284if.then:
285  br label %control
286
287control:
288  %iv.next = phi i32 [ %iv, %loop ], [ undef, %if.then ]
289  %cmp = icmp slt i32 %iv.next, %limit
290  br i1 %cmp, label %loop, label %exit
291
292exit:
293  ret void
294}
295
296; Test cloning an or, which is not an OverflowBinaryOperator.
297define i64 @cloneOr(i32 %limit, i64* %base) nounwind {
298; CHECK-LABEL: @cloneOr(
299; CHECK-NEXT:  entry:
300; CHECK-NEXT:    [[HALFLIM:%.*]] = ashr i32 [[LIMIT:%.*]], 2
301; CHECK-NEXT:    [[TMP0:%.*]] = sext i32 [[HALFLIM]] to i64
302; CHECK-NEXT:    br label [[LOOP:%.*]]
303; CHECK:       loop:
304; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
305; CHECK-NEXT:    [[ADR:%.*]] = getelementptr i64, i64* [[BASE:%.*]], i64 [[INDVARS_IV]]
306; CHECK-NEXT:    [[VAL:%.*]] = load i64, i64* [[ADR]], align 8
307; CHECK-NEXT:    [[TMP1:%.*]] = or i64 [[INDVARS_IV]], 1
308; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 2
309; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i64 [[INDVARS_IV_NEXT]], [[TMP0]]
310; CHECK-NEXT:    br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
311; CHECK:       exit:
312; CHECK-NEXT:    [[VAL_LCSSA:%.*]] = phi i64 [ [[VAL]], [[LOOP]] ]
313; CHECK-NEXT:    [[T3_LCSSA:%.*]] = phi i64 [ [[TMP1]], [[LOOP]] ]
314; CHECK-NEXT:    [[RESULT:%.*]] = and i64 [[VAL_LCSSA]], [[T3_LCSSA]]
315; CHECK-NEXT:    ret i64 [[RESULT]]
316;
317entry:
318  ; ensure that the loop can't overflow
319  %halfLim = ashr i32 %limit, 2
320  br label %loop
321
322loop:
323  %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
324  %t1 = sext i32 %iv to i64
325  %adr = getelementptr i64, i64* %base, i64 %t1
326  %val = load i64, i64* %adr
327  %t2 = or i32 %iv, 1
328  %t3 = sext i32 %t2 to i64
329  %iv.next = add i32 %iv, 2
330  %cmp = icmp slt i32 %iv.next, %halfLim
331  br i1 %cmp, label %loop, label %exit
332
333exit:
334  %result = and i64 %val, %t3
335  ret i64 %result
336}
337
338; The i induction variable looks like a wrap-around, but it really is just
339; a simple affine IV.  Make sure that indvars simplifies through.
340; ReplaceLoopExitValue should fold the return value to constant 9.
341define i32 @indirectRecurrence() nounwind {
342; CHECK-LABEL: @indirectRecurrence(
343; CHECK-NEXT:  entry:
344; CHECK-NEXT:    br label [[LOOP:%.*]]
345; CHECK:       loop:
346; CHECK-NEXT:    [[J_0:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[J_NEXT:%.*]], [[COND_TRUE:%.*]] ]
347; CHECK-NEXT:    [[TMP:%.*]] = icmp ne i32 [[J_0]], 10
348; CHECK-NEXT:    br i1 [[TMP]], label [[COND_TRUE]], label [[RETURN:%.*]]
349; CHECK:       cond_true:
350; CHECK-NEXT:    [[J_NEXT]] = add nuw nsw i32 [[J_0]], 1
351; CHECK-NEXT:    br label [[LOOP]]
352; CHECK:       return:
353; CHECK-NEXT:    ret i32 9
354;
355entry:
356  br label %loop
357
358loop:
359  %j.0 = phi i32 [ 1, %entry ], [ %j.next, %cond_true ]
360  %i.0 = phi i32 [ 0, %entry ], [ %j.0, %cond_true ]
361  %tmp = icmp ne i32 %j.0, 10
362  br i1 %tmp, label %cond_true, label %return
363
364cond_true:
365  %j.next = add i32 %j.0, 1
366  br label %loop
367
368return:
369  ret i32 %i.0
370}
371
372; Eliminate the congruent phis j, k, and l.
373; Eliminate the redundant IV increments k.next and l.next.
374; Two phis should remain, one starting at %init, and one at %init1.
375; Two increments should remain, one by %step and one by %step1.
376; Five live-outs should remain.
377define i32 @isomorphic(i32 %init, i32 %step, i32 %lim) nounwind {
378; CHECK-LABEL: @isomorphic(
379; CHECK-NEXT:  entry:
380; CHECK-NEXT:    [[STEP1:%.*]] = add i32 [[STEP:%.*]], 1
381; CHECK-NEXT:    [[INIT1:%.*]] = add i32 [[INIT:%.*]], [[STEP1]]
382; CHECK-NEXT:    br label [[LOOP:%.*]]
383; CHECK:       loop:
384; CHECK-NEXT:    [[II:%.*]] = phi i32 [ [[INIT1]], [[ENTRY:%.*]] ], [ [[II_NEXT:%.*]], [[LOOP]] ]
385; CHECK-NEXT:    [[J:%.*]] = phi i32 [ [[INIT]], [[ENTRY]] ], [ [[J_NEXT:%.*]], [[LOOP]] ]
386; CHECK-NEXT:    [[II_NEXT]] = add i32 [[II]], [[STEP1]]
387; CHECK-NEXT:    [[J_NEXT]] = add i32 [[J]], [[STEP1]]
388; CHECK-NEXT:    [[L_STEP:%.*]] = add i32 [[J]], [[STEP]]
389; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[II_NEXT]], [[LIM:%.*]]
390; CHECK-NEXT:    br i1 [[CMP]], label [[LOOP]], label [[RETURN:%.*]]
391; CHECK:       return:
392; CHECK-NEXT:    [[I_LCSSA:%.*]] = phi i32 [ [[J]], [[LOOP]] ]
393; CHECK-NEXT:    [[J_NEXT_LCSSA:%.*]] = phi i32 [ [[J_NEXT]], [[LOOP]] ]
394; CHECK-NEXT:    [[K_NEXT_LCSSA:%.*]] = phi i32 [ [[II_NEXT]], [[LOOP]] ]
395; CHECK-NEXT:    [[L_STEP_LCSSA:%.*]] = phi i32 [ [[L_STEP]], [[LOOP]] ]
396; CHECK-NEXT:    [[L_NEXT_LCSSA:%.*]] = phi i32 [ [[J_NEXT]], [[LOOP]] ]
397; CHECK-NEXT:    [[SUM1:%.*]] = add i32 [[I_LCSSA]], [[J_NEXT_LCSSA]]
398; CHECK-NEXT:    [[SUM2:%.*]] = add i32 [[SUM1]], [[K_NEXT_LCSSA]]
399; CHECK-NEXT:    [[SUM3:%.*]] = add i32 [[SUM1]], [[L_STEP_LCSSA]]
400; CHECK-NEXT:    [[SUM4:%.*]] = add i32 [[SUM1]], [[L_NEXT_LCSSA]]
401; CHECK-NEXT:    ret i32 [[SUM4]]
402;
403entry:
404  %step1 = add i32 %step, 1
405  %init1 = add i32 %init, %step1
406  %l.0 = sub i32 %init1, %step1
407  br label %loop
408
409loop:
410  %ii = phi i32 [ %init1, %entry ], [ %ii.next, %loop ]
411  %i = phi i32 [ %init, %entry ], [ %ii, %loop ]
412  %j = phi i32 [ %init, %entry ], [ %j.next, %loop ]
413  %k = phi i32 [ %init1, %entry ], [ %k.next, %loop ]
414  %l = phi i32 [ %l.0, %entry ], [ %l.next, %loop ]
415  %ii.next = add i32 %ii, %step1
416  %j.next = add i32 %j, %step1
417  %k.next = add i32 %k, %step1
418  %l.step = add i32 %l, %step
419  %l.next = add i32 %l.step, 1
420  %cmp = icmp ne i32 %ii.next, %lim
421  br i1 %cmp, label %loop, label %return
422
423return:
424  %sum1 = add i32 %i, %j.next
425  %sum2 = add i32 %sum1, %k.next
426  %sum3 = add i32 %sum1, %l.step
427  %sum4 = add i32 %sum1, %l.next
428  ret i32 %sum4
429}
430
431; Test a GEP IV that is derived from another GEP IV by a nop gep that
432; lowers the type without changing the expression.
433%structIF = type { i32, float }
434
435define void @congruentgepiv(%structIF* %base) nounwind uwtable ssp {
436; CHECK-LABEL: @congruentgepiv(
437; CHECK-NEXT:  entry:
438; CHECK-NEXT:    br label [[LOOP:%.*]]
439; CHECK:       loop:
440; CHECK-NEXT:    [[PTR_IV:%.*]] = phi %structIF* [ [[PTR_INC:%.*]], [[LATCH:%.*]] ], [ [[BASE:%.*]], [[ENTRY:%.*]] ]
441; CHECK-NEXT:    [[INDVARS1:%.*]] = bitcast %structIF* [[PTR_IV]] to i32*
442; CHECK-NEXT:    store i32 4, i32* [[INDVARS1]], align 4
443; CHECK-NEXT:    br i1 false, label [[LATCH]], label [[EXIT:%.*]]
444; CHECK:       latch:
445; CHECK-NEXT:    [[PTR_INC]] = getelementptr inbounds [[STRUCTIF:%.*]], %structIF* [[PTR_IV]], i64 1
446; CHECK-NEXT:    br label [[LOOP]]
447; CHECK:       exit:
448; CHECK-NEXT:    ret void
449;
450entry:
451  %first = getelementptr inbounds %structIF, %structIF* %base, i64 0, i32 0
452  br label %loop
453
454loop:
455  %ptr.iv = phi %structIF* [ %ptr.inc, %latch ], [ %base, %entry ]
456  %next = phi i32* [ %next.inc, %latch ], [ %first, %entry ]
457  store i32 4, i32* %next
458  br i1 undef, label %latch, label %exit
459
460latch:                         ; preds = %for.inc50.i
461  %ptr.inc = getelementptr inbounds %structIF, %structIF* %ptr.iv, i64 1
462  %next.inc = getelementptr inbounds %structIF, %structIF* %ptr.inc, i64 0, i32 0
463  br label %loop
464
465exit:
466  ret void
467}
468
469declare void @use32(i32 %x)
470declare void @use64(i64 %x)
471
472; Test a widened IV that is used by a phi on different paths within the loop.
473define void @phiUsesTrunc() nounwind {
474; CHECK-LABEL: @phiUsesTrunc(
475; CHECK-NEXT:  entry:
476; CHECK-NEXT:    br i1 undef, label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_END:%.*]]
477; CHECK:       for.body.preheader:
478; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
479; CHECK:       for.body:
480; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ 1, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_INC:%.*]] ]
481; CHECK-NEXT:    [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
482; CHECK-NEXT:    br i1 undef, label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
483; CHECK:       if.then:
484; CHECK-NEXT:    br i1 undef, label [[IF_THEN33:%.*]], label [[FOR_INC]]
485; CHECK:       if.then33:
486; CHECK-NEXT:    br label [[FOR_INC]]
487; CHECK:       if.else:
488; CHECK-NEXT:    br i1 undef, label [[IF_THEN97:%.*]], label [[FOR_INC]]
489; CHECK:       if.then97:
490; CHECK-NEXT:    call void @use64(i64 [[INDVARS_IV]])
491; CHECK-NEXT:    br label [[FOR_INC]]
492; CHECK:       for.inc:
493; CHECK-NEXT:    [[KMIN_1:%.*]] = phi i32 [ [[TMP0]], [[IF_THEN33]] ], [ 0, [[IF_THEN]] ], [ [[TMP0]], [[IF_THEN97]] ], [ 0, [[IF_ELSE]] ]
494; CHECK-NEXT:    call void @use32(i32 [[KMIN_1]])
495; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
496; CHECK-NEXT:    br i1 false, label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
497; CHECK:       for.end.loopexit:
498; CHECK-NEXT:    br label [[FOR_END]]
499; CHECK:       for.end:
500; CHECK-NEXT:    ret void
501;
502entry:
503  br i1 undef, label %for.body, label %for.end
504
505for.body:
506  %iv = phi i32 [ %inc, %for.inc ], [ 1, %entry ]
507  br i1 undef, label %if.then, label %if.else
508
509if.then:
510  br i1 undef, label %if.then33, label %for.inc
511
512if.then33:
513  br label %for.inc
514
515if.else:
516  br i1 undef, label %if.then97, label %for.inc
517
518if.then97:
519  %idxprom100 = sext i32 %iv to i64
520  call void @use64(i64 %idxprom100)
521  br label %for.inc
522
523for.inc:
524  %kmin.1 = phi i32 [ %iv, %if.then33 ], [ 0, %if.then ], [ %iv, %if.then97 ], [ 0, %if.else ]
525  call void @use32(i32 %kmin.1)
526  %inc = add nsw i32 %iv, 1
527  br i1 undef, label %for.body, label %for.end
528
529for.end:
530  ret void
531}
532