1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=indvars -S | FileCheck %s
3 ; RUN: opt < %s -S -passes='lcssa,loop-simplify,require<targetir>,require<scalar-evolution>,require<domtree>,loop(indvars)' | FileCheck %s
5 ; Provide legal integer types.
6 target datalayout = "n8:16:32:64"
9 target triple = "x86_64-apple-darwin"
11 declare void @use(i64 %x)
14 ; One trunc for the gep.
15 ; One trunc for the dummy() call.
16 define void @loop_0(ptr %a) {
17 ; CHECK-LABEL: @loop_0(
18 ; CHECK-NEXT: Prologue:
19 ; CHECK-NEXT: br i1 undef, label [[B18_PREHEADER:%.*]], label [[B6:%.*]]
20 ; CHECK: B18.preheader:
21 ; CHECK-NEXT: br label [[B18:%.*]]
23 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[B18_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[B24:%.*]] ]
24 ; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
25 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
26 ; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
27 ; CHECK-NEXT: [[O:%.*]] = getelementptr i32, ptr [[A:%.*]], i32 [[TMP0]]
28 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[O]], align 4
29 ; CHECK-NEXT: [[T:%.*]] = icmp eq i32 [[V]], 0
30 ; CHECK-NEXT: br i1 [[T]], label [[EXIT24:%.*]], label [[B24]]
32 ; CHECK-NEXT: [[T2:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT]], 20
33 ; CHECK-NEXT: br i1 [[T2]], label [[B6_LOOPEXIT:%.*]], label [[B18]]
35 ; CHECK-NEXT: br label [[B6]]
37 ; CHECK-NEXT: ret void
39 ; CHECK-NEXT: [[DOT02_LCSSA_WIDE:%.*]] = phi i64 [ [[INDVARS_IV]], [[B18]] ]
40 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[DOT02_LCSSA_WIDE]] to i32
41 ; CHECK-NEXT: call void @dummy(i32 [[TMP1]])
42 ; CHECK-NEXT: unreachable
45 br i1 undef, label %B18, label %B6
47 B18: ; preds = %B24, %Prologue
48 %.02 = phi i32 [ 0, %Prologue ], [ %tmp33, %B24 ]
49 %tmp23 = zext i32 %.02 to i64
50 call void @use(i64 %tmp23)
51 %tmp33 = add i32 %.02, 1
52 %o = getelementptr i32, ptr %a, i32 %.02
54 %t = icmp eq i32 %v, 0
55 br i1 %t, label %exit24, label %B24
58 %t2 = icmp eq i32 %tmp33, 20
59 br i1 %t2, label %B6, label %B18
61 B6: ; preds = %Prologue
64 exit24: ; preds = %B18
65 call void @dummy(i32 %.02)
69 ; Make sure that dead zext is removed and no widening happens.
70 define void @loop_0_dead(ptr %a) {
71 ; CHECK-LABEL: @loop_0_dead(
72 ; CHECK-NEXT: Prologue:
73 ; CHECK-NEXT: br i1 undef, label [[B18_PREHEADER:%.*]], label [[B6:%.*]]
74 ; CHECK: B18.preheader:
75 ; CHECK-NEXT: br label [[B18:%.*]]
77 ; CHECK-NEXT: [[DOT02:%.*]] = phi i32 [ [[TMP33:%.*]], [[B24:%.*]] ], [ 0, [[B18_PREHEADER]] ]
78 ; CHECK-NEXT: [[TMP33]] = add nuw nsw i32 [[DOT02]], 1
79 ; CHECK-NEXT: [[O:%.*]] = getelementptr i32, ptr [[A:%.*]], i32 [[DOT02]]
80 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[O]], align 4
81 ; CHECK-NEXT: [[T:%.*]] = icmp eq i32 [[V]], 0
82 ; CHECK-NEXT: br i1 [[T]], label [[EXIT24:%.*]], label [[B24]]
84 ; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[TMP33]], 20
85 ; CHECK-NEXT: br i1 [[T2]], label [[B6_LOOPEXIT:%.*]], label [[B18]]
87 ; CHECK-NEXT: br label [[B6]]
89 ; CHECK-NEXT: ret void
91 ; CHECK-NEXT: [[DOT02_LCSSA:%.*]] = phi i32 [ [[DOT02]], [[B18]] ]
92 ; CHECK-NEXT: call void @dummy(i32 [[DOT02_LCSSA]])
93 ; CHECK-NEXT: unreachable
96 br i1 undef, label %B18, label %B6
98 B18: ; preds = %B24, %Prologue
99 %.02 = phi i32 [ 0, %Prologue ], [ %tmp33, %B24 ]
100 %tmp23 = zext i32 %.02 to i64
101 %tmp33 = add i32 %.02, 1
102 %o = getelementptr i32, ptr %a, i32 %.02
103 %v = load i32, ptr %o
104 %t = icmp eq i32 %v, 0
105 br i1 %t, label %exit24, label %B24
108 %t2 = icmp eq i32 %tmp33, 20
109 br i1 %t2, label %B6, label %B18
111 B6: ; preds = %Prologue
114 exit24: ; preds = %B18
115 call void @dummy(i32 %.02)
119 define void @loop_1(i32 %lim) {
120 ; CHECK-LABEL: @loop_1(
122 ; CHECK-NEXT: [[ENTRY_COND:%.*]] = icmp ne i32 [[LIM:%.*]], 0
123 ; CHECK-NEXT: br i1 [[ENTRY_COND]], label [[LOOP_PREHEADER:%.*]], label [[LEAVE:%.*]]
124 ; CHECK: loop.preheader:
125 ; CHECK-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[LIM]], i32 2)
126 ; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[UMAX]] to i64
127 ; CHECK-NEXT: br label [[LOOP:%.*]]
129 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 1, [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
130 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
131 ; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[INDVARS_IV]], -1
132 ; CHECK-NEXT: call void @dummy.i64(i64 [[TMP0]])
133 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
134 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[LEAVE_LOOPEXIT:%.*]]
135 ; CHECK: leave.loopexit:
136 ; CHECK-NEXT: br label [[LEAVE]]
138 ; CHECK-NEXT: ret void
141 %entry.cond = icmp ne i32 %lim, 0
142 br i1 %entry.cond, label %loop, label %leave
146 %iv = phi i32 [ 1, %entry ], [ %iv.inc, %loop ]
147 %iv.inc = add i32 %iv, 1
148 %iv.inc.sub = add i32 %iv, -1
149 %iv.inc.sub.zext = zext i32 %iv.inc.sub to i64
150 call void @dummy.i64(i64 %iv.inc.sub.zext)
151 %be.cond = icmp ult i32 %iv.inc, %lim
152 br i1 %be.cond, label %loop, label %leave
158 declare void @dummy(i32)
159 declare void @dummy.i64(i64)
162 define void @loop_2(i32 %size, i32 %nsteps, i32 %hsize, ptr %lined, i8 %tmp1) {
163 ; CHECK-LABEL: @loop_2(
165 ; CHECK-NEXT: [[CMP215:%.*]] = icmp sgt i32 [[SIZE:%.*]], 1
166 ; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[SIZE]] to i64
167 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[HSIZE:%.*]] to i64
168 ; CHECK-NEXT: [[SMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[NSTEPS:%.*]], i32 1)
169 ; CHECK-NEXT: [[WIDE_TRIP_COUNT14:%.*]] = zext i32 [[SMAX]] to i64
170 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
172 ; CHECK-NEXT: [[INDVARS_IV9:%.*]] = phi i64 [ [[INDVARS_IV_NEXT10:%.*]], [[FOR_INC:%.*]] ], [ 0, [[ENTRY:%.*]] ]
173 ; CHECK-NEXT: [[TMP2:%.*]] = mul nsw i64 [[INDVARS_IV9]], [[TMP0]]
174 ; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[TMP2]], [[TMP1]]
175 ; CHECK-NEXT: br i1 [[CMP215]], label [[FOR_BODY2_PREHEADER:%.*]], label [[FOR_INC]]
176 ; CHECK: for.body2.preheader:
177 ; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[SIZE]] to i64
178 ; CHECK-NEXT: br label [[FOR_BODY2:%.*]]
180 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 1, [[FOR_BODY2_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY2]] ]
181 ; CHECK-NEXT: [[TMP4:%.*]] = add nsw i64 [[TMP3]], [[INDVARS_IV]]
182 ; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[LINED:%.*]], i64 [[TMP4]]
183 ; CHECK-NEXT: store i8 [[TMP1:%.*]], ptr [[ADD_PTR]], align 1
184 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
185 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
186 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY2]], label [[FOR_BODY3_PREHEADER:%.*]]
187 ; CHECK: for.body3.preheader:
188 ; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[TMP3]] to i32
189 ; CHECK-NEXT: [[TMP6:%.*]] = zext i32 [[TMP5]] to i64
190 ; CHECK-NEXT: [[WIDE_TRIP_COUNT7:%.*]] = zext i32 [[SIZE]] to i64
191 ; CHECK-NEXT: br label [[FOR_BODY3:%.*]]
193 ; CHECK-NEXT: [[INDVARS_IV3:%.*]] = phi i64 [ 1, [[FOR_BODY3_PREHEADER]] ], [ [[INDVARS_IV_NEXT4:%.*]], [[FOR_BODY3]] ]
194 ; CHECK-NEXT: [[TMP7:%.*]] = add nuw nsw i64 [[TMP6]], [[INDVARS_IV3]]
195 ; CHECK-NEXT: [[ADD_PTR2:%.*]] = getelementptr inbounds i8, ptr [[LINED]], i64 [[TMP7]]
196 ; CHECK-NEXT: store i8 [[TMP1]], ptr [[ADD_PTR2]], align 1
197 ; CHECK-NEXT: [[INDVARS_IV_NEXT4]] = add nuw nsw i64 [[INDVARS_IV3]], 1
198 ; CHECK-NEXT: [[EXITCOND8:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT4]], [[WIDE_TRIP_COUNT7]]
199 ; CHECK-NEXT: br i1 [[EXITCOND8]], label [[FOR_BODY3]], label [[FOR_INC_LOOPEXIT:%.*]]
200 ; CHECK: for.inc.loopexit:
201 ; CHECK-NEXT: br label [[FOR_INC]]
203 ; CHECK-NEXT: [[INDVARS_IV_NEXT10]] = add nuw nsw i64 [[INDVARS_IV9]], 1
204 ; CHECK-NEXT: [[EXITCOND15:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT10]], [[WIDE_TRIP_COUNT14]]
205 ; CHECK-NEXT: br i1 [[EXITCOND15]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
206 ; CHECK: for.end.loopexit:
207 ; CHECK-NEXT: ret void
210 %cmp215 = icmp sgt i32 %size, 1
214 %j = phi i32 [ 0, %entry ], [ %inc6, %for.inc ]
215 %mul = mul nsw i32 %j, %size
216 %add = add nsw i32 %mul, %hsize
217 br i1 %cmp215, label %for.body2, label %for.inc
219 ; check that the induction variable of the inner loop has been widened after indvars.
221 %k = phi i32 [ %inc, %for.body2 ], [ 1, %for.body ]
222 %add4 = add nsw i32 %add, %k
223 %idx.ext = sext i32 %add4 to i64
224 %add.ptr = getelementptr inbounds i8, ptr %lined, i64 %idx.ext
225 store i8 %tmp1, ptr %add.ptr, align 1
226 %inc = add nsw i32 %k, 1
227 %cmp2 = icmp slt i32 %inc, %size
228 br i1 %cmp2, label %for.body2, label %for.body3
230 ; check that the induction variable of the inner loop has been widened after indvars.
232 %l = phi i32 [ %inc2, %for.body3 ], [ 1, %for.body2 ]
233 %add5 = add nuw i32 %add, %l
234 %idx.ext2 = zext i32 %add5 to i64
235 %add.ptr2 = getelementptr inbounds i8, ptr %lined, i64 %idx.ext2
236 store i8 %tmp1, ptr %add.ptr2, align 1
237 %inc2 = add nsw i32 %l, 1
238 %cmp3 = icmp slt i32 %inc2, %size
239 br i1 %cmp3, label %for.body3, label %for.inc
242 %inc6 = add nsw i32 %j, 1
243 %cmp = icmp slt i32 %inc6, %nsteps
244 br i1 %cmp, label %for.body, label %for.end.loopexit