1 ; RUN: opt -basic-aa -loop-distribute -enable-loop-distribute -verify-loop-info -verify-dom-info -S \
2 ; RUN: < %s | FileCheck %s
4 ; RUN: opt -basic-aa -loop-distribute -enable-loop-distribute -loop-vectorize -force-vector-width=4 \
5 ; RUN: -verify-loop-info -verify-dom-info -S < %s | \
6 ; RUN: FileCheck --check-prefix=VECTORIZE %s
8 ; RUN: opt -basic-aa -loop-distribute -enable-loop-distribute -verify-loop-info -verify-dom-info \
9 ; RUN: -loop-accesses -analyze < %s -enable-new-pm=0 | FileCheck %s --check-prefix=ANALYSIS
11 ; TODO: the following changes the order loop-access printing prints loops, remove legacy RUN and change after NPM switch
12 ; TODO: opt -aa-pipeline=basic-aa -passes='loop-distribute,print-access-info' -enable-loop-distribute \
13 ; TODO: -verify-loop-info -verify-dom-info -disable-output < %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
15 ; The memcheck version of basic.ll. We should distribute and vectorize the
16 ; second part of this loop with 5 memchecks (A+1 x {C, D, E} + C x {A, B})
18 ; for (i = 0; i < n; i++) {
19 ; A[i + 1] = A[i] * B[i];
20 ; -------------------------------
24 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
25 target triple = "x86_64-apple-macosx10.10.0"
27 @B = common global i32* null, align 8
28 @A = common global i32* null, align 8
29 @C = common global i32* null, align 8
30 @D = common global i32* null, align 8
31 @E = common global i32* null, align 8
36 %a = load i32*, i32** @A, align 8
37 %b = load i32*, i32** @B, align 8
38 %c = load i32*, i32** @C, align 8
39 %d = load i32*, i32** @D, align 8
40 %e = load i32*, i32** @E, align 8
43 ; We have two compares for each array overlap check.
44 ; Since the checks to A and A + 4 get merged, this will give us a
45 ; total of 8 compares.
47 ; CHECK: for.body.lver.check:
61 ; CHECK: br i1 %memcheck.conflict, label %for.body.ph.lver.orig, label %for.body.ph.ldist1
63 ; The non-distributed loop that the memchecks fall back on.
65 ; CHECK: for.body.ph.lver.orig:
66 ; CHECK: br label %for.body.lver.orig
67 ; CHECK: for.body.lver.orig:
68 ; CHECK: br i1 %exitcond.lver.orig, label %for.end.loopexit, label %for.body.lver.orig
70 ; Verify the two distributed loops.
72 ; CHECK: for.body.ph.ldist1:
73 ; CHECK: br label %for.body.ldist1
74 ; CHECK: for.body.ldist1:
75 ; CHECK: %mulA.ldist1 = mul i32 %loadB.ldist1, %loadA.ldist1
76 ; CHECK: br i1 %exitcond.ldist1, label %for.body.ph, label %for.body.ldist1
79 ; CHECK: br label %for.body
81 ; CHECK: %mulC = mul i32 %loadD, %loadE
85 ; VECTORIZE: mul <4 x i32>
87 for.body: ; preds = %for.body, %entry
88 %ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
90 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
91 %loadA = load i32, i32* %arrayidxA, align 4
93 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
94 %loadB = load i32, i32* %arrayidxB, align 4
96 %mulA = mul i32 %loadB, %loadA
98 %add = add nuw nsw i64 %ind, 1
99 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
100 store i32 %mulA, i32* %arrayidxA_plus_4, align 4
102 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
103 %loadD = load i32, i32* %arrayidxD, align 4
105 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
106 %loadE = load i32, i32* %arrayidxE, align 4
108 %mulC = mul i32 %loadD, %loadE
110 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
111 store i32 %mulC, i32* %arrayidxC, align 4
113 %exitcond = icmp eq i64 %add, 20
114 br i1 %exitcond, label %for.end, label %for.body
116 for.end: ; preds = %for.body
120 ; Make sure there's no "Multiple reports generated" assert with a
121 ; volatile load, and no distribution
123 ; TODO: Distribution of volatile may be possible under some
124 ; circumstance, but the current implementation does not touch them.
126 ; CHECK-LABEL: @f_volatile_load(
127 ; CHECK: br label %for.body{{$}}
131 ; CHECK: {{^}}for.body:
134 ; CHECK: load volatile i32
136 ; CHECK: br i1 %exitcond, label %for.end, label %for.body{{$}}
140 ; VECTORIZE-NOT: load <4 x i32>
141 ; VECTORIZE-NOT: mul <4 x i32>
142 define void @f_volatile_load() {
144 %a = load i32*, i32** @A, align 8
145 %b = load i32*, i32** @B, align 8
146 %c = load i32*, i32** @C, align 8
147 %d = load i32*, i32** @D, align 8
148 %e = load i32*, i32** @E, align 8
152 %ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
154 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
155 %loadA = load i32, i32* %arrayidxA, align 4
157 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
158 %loadB = load i32, i32* %arrayidxB, align 4
160 %mulA = mul i32 %loadB, %loadA
162 %add = add nuw nsw i64 %ind, 1
163 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
164 store i32 %mulA, i32* %arrayidxA_plus_4, align 4
166 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
167 %loadD = load volatile i32, i32* %arrayidxD, align 4
169 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
170 %loadE = load i32, i32* %arrayidxE, align 4
172 %mulC = mul i32 %loadD, %loadE
174 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
175 store i32 %mulC, i32* %arrayidxC, align 4
177 %exitcond = icmp eq i64 %add, 20
178 br i1 %exitcond, label %for.end, label %for.body
184 declare i32 @llvm.convergent(i32) #0
186 ; This is the same as f, and would require the same bounds
187 ; check. However, it is not OK to introduce new control dependencies
188 ; on the convergent call.
190 ; CHECK-LABEL: @f_with_convergent(
191 ; CHECK: call i32 @llvm.convergent
192 ; CHECK-NOT: call i32 @llvm.convergent
194 ; ANALYSIS: for.body:
195 ; ANALYSIS: Report: cannot add control dependency to convergent operation
196 define void @f_with_convergent() #1 {
198 %a = load i32*, i32** @A, align 8
199 %b = load i32*, i32** @B, align 8
200 %c = load i32*, i32** @C, align 8
201 %d = load i32*, i32** @D, align 8
202 %e = load i32*, i32** @E, align 8
205 for.body: ; preds = %for.body, %entry
206 %ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
208 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
209 %loadA = load i32, i32* %arrayidxA, align 4
211 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
212 %loadB = load i32, i32* %arrayidxB, align 4
214 %mulA = mul i32 %loadB, %loadA
216 %add = add nuw nsw i64 %ind, 1
217 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
218 store i32 %mulA, i32* %arrayidxA_plus_4, align 4
220 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
221 %loadD = load i32, i32* %arrayidxD, align 4
223 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
224 %loadE = load i32, i32* %arrayidxE, align 4
226 %convergentD = call i32 @llvm.convergent(i32 %loadD)
227 %mulC = mul i32 %convergentD, %loadE
229 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
230 store i32 %mulC, i32* %arrayidxC, align 4
232 %exitcond = icmp eq i64 %add, 20
233 br i1 %exitcond, label %for.end, label %for.body
235 for.end: ; preds = %for.body
239 ; Make sure an explicit request for distribution is ignored if it
240 ; requires possibly illegal checks.
242 ; CHECK-LABEL: @f_with_convergent_forced_distribute(
243 ; CHECK: call i32 @llvm.convergent
244 ; CHECK-NOT: call i32 @llvm.convergent
245 define void @f_with_convergent_forced_distribute() #1 {
247 %a = load i32*, i32** @A, align 8
248 %b = load i32*, i32** @B, align 8
249 %c = load i32*, i32** @C, align 8
250 %d = load i32*, i32** @D, align 8
251 %e = load i32*, i32** @E, align 8
254 for.body: ; preds = %for.body, %entry
255 %ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
257 %arrayidxA = getelementptr inbounds i32, i32* %a, i64 %ind
258 %loadA = load i32, i32* %arrayidxA, align 4
260 %arrayidxB = getelementptr inbounds i32, i32* %b, i64 %ind
261 %loadB = load i32, i32* %arrayidxB, align 4
263 %mulA = mul i32 %loadB, %loadA
265 %add = add nuw nsw i64 %ind, 1
266 %arrayidxA_plus_4 = getelementptr inbounds i32, i32* %a, i64 %add
267 store i32 %mulA, i32* %arrayidxA_plus_4, align 4
269 %arrayidxD = getelementptr inbounds i32, i32* %d, i64 %ind
270 %loadD = load i32, i32* %arrayidxD, align 4
272 %arrayidxE = getelementptr inbounds i32, i32* %e, i64 %ind
273 %loadE = load i32, i32* %arrayidxE, align 4
275 %convergentD = call i32 @llvm.convergent(i32 %loadD)
276 %mulC = mul i32 %convergentD, %loadE
278 %arrayidxC = getelementptr inbounds i32, i32* %c, i64 %ind
279 store i32 %mulC, i32* %arrayidxC, align 4
281 %exitcond = icmp eq i64 %add, 20
282 br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !0
284 for.end: ; preds = %for.body
288 attributes #0 = { nounwind readnone convergent }
289 attributes #1 = { nounwind convergent }
291 !0 = distinct !{!0, !1}
292 !1 = !{!"llvm.loop.distribute.enable", i1 true}