1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=loop-idiom < %s -S | FileCheck %s
3 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"
5 ; CHECK: @.memset_pattern = private unnamed_addr constant [4 x i32] [i32 2, i32 2, i32 2, i32 2], align 16
6 ; CHECK: @.memset_pattern.1 = private unnamed_addr constant [4 x i32] [i32 2, i32 2, i32 2, i32 2], align 16
7 ; CHECK: @.memset_pattern.2 = private unnamed_addr constant [4 x i32] [i32 2, i32 2, i32 2, i32 2], align 16
9 target triple = "x86_64-apple-darwin10.0.0"
11 %struct.foo = type { i32, i32 }
12 %struct.foo1 = type { i32, i32, i32 }
14 ;void bar1(foo_t *f, unsigned n) {
15 ; for (unsigned i = 0; i < n; ++i) {
20 define void @bar1(ptr %f, i32 %n) nounwind ssp {
23 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[N:%.*]], 0
24 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
25 ; CHECK: for.body.preheader:
26 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N]] to i64
27 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 3
28 ; CHECK-NEXT: call void @memset_pattern16(ptr [[F:%.*]], ptr @.memset_pattern, i64 [[TMP1]])
29 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
31 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
32 ; CHECK-NEXT: [[A:%.*]] = getelementptr inbounds [[STRUCT_FOO:%.*]], ptr [[F]], i64 [[INDVARS_IV]], i32 0
33 ; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds [[STRUCT_FOO]], ptr [[F]], i64 [[INDVARS_IV]], i32 1
34 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
35 ; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
36 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[LFTR_WIDEIV]], [[N]]
37 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
38 ; CHECK: for.end.loopexit:
39 ; CHECK-NEXT: br label [[FOR_END]]
41 ; CHECK-NEXT: ret void
44 %cmp1 = icmp eq i32 %n, 0
45 br i1 %cmp1, label %for.end, label %for.body.preheader
47 for.body.preheader: ; preds = %entry
50 for.body: ; preds = %for.body.preheader, %for.body
51 %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
52 %a = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 0
53 store i32 2, ptr %a, align 4
54 %b = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 1
55 store i32 2, ptr %b, align 4
56 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
57 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
58 %exitcond = icmp ne i32 %lftr.wideiv, %n
59 br i1 %exitcond, label %for.body, label %for.end.loopexit
61 for.end.loopexit: ; preds = %for.body
64 for.end: ; preds = %for.end.loopexit, %entry
68 ;void bar2(foo_t *f, unsigned n) {
69 ; for (unsigned i = 0; i < n; ++i) {
74 define void @bar2(ptr %f, i32 %n) nounwind ssp {
77 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[N:%.*]], 0
78 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
79 ; CHECK: for.body.preheader:
80 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N]] to i64
81 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 3
82 ; CHECK-NEXT: call void @memset_pattern16(ptr [[F:%.*]], ptr @.memset_pattern.1, i64 [[TMP1]])
83 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
85 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
86 ; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds [[STRUCT_FOO:%.*]], ptr [[F]], i64 [[INDVARS_IV]], i32 1
87 ; CHECK-NEXT: [[A:%.*]] = getelementptr inbounds [[STRUCT_FOO]], ptr [[F]], i64 [[INDVARS_IV]], i32 0
88 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
89 ; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
90 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[LFTR_WIDEIV]], [[N]]
91 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
92 ; CHECK: for.end.loopexit:
93 ; CHECK-NEXT: br label [[FOR_END]]
95 ; CHECK-NEXT: ret void
98 %cmp1 = icmp eq i32 %n, 0
99 br i1 %cmp1, label %for.end, label %for.body.preheader
101 for.body.preheader: ; preds = %entry
104 for.body: ; preds = %for.body.preheader, %for.body
105 %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
106 %b = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 1
107 store i32 2, ptr %b, align 4
108 %a = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 0
109 store i32 2, ptr %a, align 4
110 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
111 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
112 %exitcond = icmp ne i32 %lftr.wideiv, %n
113 br i1 %exitcond, label %for.body, label %for.end.loopexit
115 for.end.loopexit: ; preds = %for.body
118 for.end: ; preds = %for.end.loopexit, %entry
122 ;void bar3(foo_t *f, unsigned n) {
123 ; for (unsigned i = n; i > 0; --i) {
128 define void @bar3(ptr nocapture %f, i32 %n) nounwind ssp {
129 ; CHECK-LABEL: @bar3(
131 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[N:%.*]], 0
132 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
133 ; CHECK: for.body.preheader:
134 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N]] to i64
135 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw nsw i64 [[TMP0]], 3
136 ; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[N]], -1
137 ; CHECK-NEXT: [[TMP3:%.*]] = zext i32 [[TMP2]] to i64
138 ; CHECK-NEXT: [[TMP4:%.*]] = shl nuw nsw i64 [[TMP3]], 3
139 ; CHECK-NEXT: [[TMP5:%.*]] = sub i64 [[TMP1]], [[TMP4]]
140 ; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr i8, ptr [[F:%.*]], i64 [[TMP5]]
141 ; CHECK-NEXT: call void @memset_pattern16(ptr [[UGLYGEP]], ptr @.memset_pattern.2, i64 [[TMP1]])
142 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
144 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
145 ; CHECK-NEXT: [[A:%.*]] = getelementptr inbounds [[STRUCT_FOO:%.*]], ptr [[F]], i64 [[INDVARS_IV]], i32 0
146 ; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds [[STRUCT_FOO]], ptr [[F]], i64 [[INDVARS_IV]], i32 1
147 ; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[INDVARS_IV]] to i32
148 ; CHECK-NEXT: [[DEC:%.*]] = add i32 [[TMP6]], -1
149 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[DEC]], 0
150 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], -1
151 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_END_LOOPEXIT:%.*]], label [[FOR_BODY]]
152 ; CHECK: for.end.loopexit:
153 ; CHECK-NEXT: br label [[FOR_END]]
155 ; CHECK-NEXT: ret void
158 %cmp1 = icmp eq i32 %n, 0
159 br i1 %cmp1, label %for.end, label %for.body.preheader
161 for.body.preheader: ; preds = %entry
162 %0 = zext i32 %n to i64
165 for.body: ; preds = %for.body.preheader, %for.body
166 %indvars.iv = phi i64 [ %0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
167 %a = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 0
168 store i32 2, ptr %a, align 4
169 %b = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 1
170 store i32 2, ptr %b, align 4
171 %1 = trunc i64 %indvars.iv to i32
172 %dec = add i32 %1, -1
173 %cmp = icmp eq i32 %dec, 0
174 %indvars.iv.next = add nsw i64 %indvars.iv, -1
175 br i1 %cmp, label %for.end.loopexit, label %for.body
177 for.end.loopexit: ; preds = %for.body
180 for.end: ; preds = %for.end.loopexit, %entry
184 ;void bar4(foo_t *f, unsigned n) {
185 ; for (unsigned i = 0; i < n; ++i) {
190 define void @bar4(ptr nocapture %f, i32 %n) nounwind ssp {
191 ; CHECK-LABEL: @bar4(
193 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[N:%.*]], 0
194 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
195 ; CHECK: for.body.preheader:
196 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
198 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
199 ; CHECK-NEXT: [[A:%.*]] = getelementptr inbounds [[STRUCT_FOO:%.*]], ptr [[F:%.*]], i64 [[INDVARS_IV]], i32 0
200 ; CHECK-NEXT: store i32 0, ptr [[A]], align 4
201 ; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds [[STRUCT_FOO]], ptr [[F]], i64 [[INDVARS_IV]], i32 1
202 ; CHECK-NEXT: store i32 1, ptr [[B]], align 4
203 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
204 ; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
205 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[LFTR_WIDEIV]], [[N]]
206 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
207 ; CHECK: for.end.loopexit:
208 ; CHECK-NEXT: br label [[FOR_END]]
210 ; CHECK-NEXT: ret void
213 %cmp1 = icmp eq i32 %n, 0
214 br i1 %cmp1, label %for.end, label %for.body.preheader
216 for.body.preheader: ; preds = %entry
219 for.body: ; preds = %for.body.preheader, %for.body
220 %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
221 %a = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 0
222 store i32 0, ptr %a, align 4
223 %b = getelementptr inbounds %struct.foo, ptr %f, i64 %indvars.iv, i32 1
224 store i32 1, ptr %b, align 4
225 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
226 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
227 %exitcond = icmp ne i32 %lftr.wideiv, %n
228 br i1 %exitcond, label %for.body, label %for.end.loopexit
230 for.end.loopexit: ; preds = %for.body
233 for.end: ; preds = %for.end.loopexit, %entry
237 ;void bar5(foo1_t *f, unsigned n) {
238 ; for (unsigned i = 0; i < n; ++i) {
243 define void @bar5(ptr nocapture %f, i32 %n) nounwind ssp {
244 ; CHECK-LABEL: @bar5(
246 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[N:%.*]], 0
247 ; CHECK-NEXT: br i1 [[CMP1]], label [[FOR_END:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
248 ; CHECK: for.body.preheader:
249 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
251 ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
252 ; CHECK-NEXT: [[A:%.*]] = getelementptr inbounds [[STRUCT_FOO1:%.*]], ptr [[F:%.*]], i64 [[INDVARS_IV]], i32 0
253 ; CHECK-NEXT: store i32 1, ptr [[A]], align 4
254 ; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds [[STRUCT_FOO1]], ptr [[F]], i64 [[INDVARS_IV]], i32 1
255 ; CHECK-NEXT: store i32 1, ptr [[B]], align 4
256 ; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
257 ; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
258 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[LFTR_WIDEIV]], [[N]]
259 ; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]]
260 ; CHECK: for.end.loopexit:
261 ; CHECK-NEXT: br label [[FOR_END]]
263 ; CHECK-NEXT: ret void
266 %cmp1 = icmp eq i32 %n, 0
267 br i1 %cmp1, label %for.end, label %for.body.preheader
269 for.body.preheader: ; preds = %entry
272 for.body: ; preds = %for.body.preheader, %for.body
273 %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
274 %a = getelementptr inbounds %struct.foo1, ptr %f, i64 %indvars.iv, i32 0
275 store i32 1, ptr %a, align 4
276 %b = getelementptr inbounds %struct.foo1, ptr %f, i64 %indvars.iv, i32 1
277 store i32 1, ptr %b, align 4
278 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
279 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
280 %exitcond = icmp ne i32 %lftr.wideiv, %n
281 br i1 %exitcond, label %for.body, label %for.end.loopexit
283 for.end.loopexit: ; preds = %for.body
286 for.end: ; preds = %for.end.loopexit, %entry