1 ; RUN: opt -basic-aa -loop-accesses -analyze -enable-new-pm=0 < %s | FileCheck %s -check-prefix=LAA
2 ; RUN: opt -passes='require<aa>,require<scalar-evolution>,require<aa>,loop(print-access-info)' -aa-pipeline='basic-aa' -disable-output < %s 2>&1 | FileCheck %s --check-prefix=LAA
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
8 ; for (int i = 0; i < n; i++) {
9 ; A[2 * index] = A[2 * index] + B[i];
13 ; SCEV is unable to prove that A[2 * i] does not overflow.
15 ; Analyzing the IR does not help us because the GEPs are not
16 ; affine AddRecExprs. However, we can turn them into AddRecExprs
17 ; using SCEV Predicates.
19 ; Once we have an affine expression we need to add an additional NUSW
20 ; to check that the pointers don't wrap since the GEPs are not
24 ; LAA: Memory dependences are safe{{$}}
25 ; LAA: SCEV assumptions:
26 ; LAA-NEXT: {0,+,2}<%for.body> Added Flags: <nusw>
27 ; LAA-NEXT: {%a,+,4}<%for.body> Added Flags: <nusw>
29 ; The expression for %mul_ext as analyzed by SCEV is
30 ; (zext i32 {0,+,2}<%for.body> to i64)
31 ; We have added the nusw flag to turn this expression into the SCEV expression:
32 ; i64 {0,+,2}<%for.body>
34 ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext:
35 ; LAA-NEXT: ((2 * (zext i32 {0,+,2}<%for.body> to i64))<nuw><nsw> + %a)
36 ; LAA-NEXT: --> {%a,+,4}<%for.body>
39 define void @f1(i16* noalias %a,
40 i16* noalias %b, i64 %N) {
44 for.body: ; preds = %for.body, %entry
45 %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
46 %ind1 = phi i32 [ 0, %entry ], [ %inc1, %for.body ]
48 %mul = mul i32 %ind1, 2
49 %mul_ext = zext i32 %mul to i64
51 %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
52 %loadA = load i16, i16* %arrayidxA, align 2
54 %arrayidxB = getelementptr i16, i16* %b, i64 %ind
55 %loadB = load i16, i16* %arrayidxB, align 2
57 %add = mul i16 %loadA, %loadB
59 store i16 %add, i16* %arrayidxA, align 2
61 %inc = add nuw nsw i64 %ind, 1
62 %inc1 = add i32 %ind1, 1
64 %exitcond = icmp eq i64 %inc, %N
65 br i1 %exitcond, label %for.end, label %for.body
67 for.end: ; preds = %for.body
73 ; for (int i = 0; i < n; i++) {
74 ; A[2 * index] = A[2 * index] + B[i];
78 ; the SCEV expression for 2 * index is not an AddRecExpr
79 ; (and implictly not affine). However, we are able to make assumptions
80 ; that will turn the expression into an affine one and continue the
83 ; Once we have an affine expression we need to add an additional NUSW
84 ; to check that the pointers don't wrap since the GEPs are not
87 ; This loop has a negative stride for A, and the nusw flag is required in
88 ; order to properly extend the increment from i32 -4 to i64 -4.
91 ; LAA: Memory dependences are safe{{$}}
92 ; LAA: SCEV assumptions:
93 ; LAA-NEXT: {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> Added Flags: <nusw>
94 ; LAA-NEXT: {((4 * (zext i31 (trunc i64 %N to i31) to i64))<nuw><nsw> + %a),+,-4}<%for.body> Added Flags: <nusw>
96 ; The expression for %mul_ext as analyzed by SCEV is
97 ; (zext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)
98 ; We have added the nusw flag to turn this expression into the following SCEV:
99 ; i64 {zext i32 (2 * (trunc i64 %N to i32)) to i64,+,-2}<%for.body>
101 ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext:
102 ; LAA-NEXT: ((2 * (zext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64))<nuw><nsw> + %a)
103 ; LAA-NEXT: --> {((4 * (zext i31 (trunc i64 %N to i31) to i64))<nuw><nsw> + %a),+,-4}<%for.body>
105 define void @f2(i16* noalias %a,
106 i16* noalias %b, i64 %N) {
108 %TruncN = trunc i64 %N to i32
111 for.body: ; preds = %for.body, %entry
112 %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
113 %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
115 %mul = mul i32 %ind1, 2
116 %mul_ext = zext i32 %mul to i64
118 %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
119 %loadA = load i16, i16* %arrayidxA, align 2
121 %arrayidxB = getelementptr i16, i16* %b, i64 %ind
122 %loadB = load i16, i16* %arrayidxB, align 2
124 %add = mul i16 %loadA, %loadB
126 store i16 %add, i16* %arrayidxA, align 2
128 %inc = add nuw nsw i64 %ind, 1
129 %dec = sub i32 %ind1, 1
131 %exitcond = icmp eq i64 %inc, %N
132 br i1 %exitcond, label %for.end, label %for.body
134 for.end: ; preds = %for.body
138 ; We replicate the tests above, but this time sign extend 2 * index instead
139 ; of zero extending it.
142 ; LAA: Memory dependences are safe{{$}}
143 ; LAA: SCEV assumptions:
144 ; LAA-NEXT: {0,+,2}<%for.body> Added Flags: <nssw>
145 ; LAA-NEXT: {%a,+,4}<%for.body> Added Flags: <nusw>
147 ; The expression for %mul_ext as analyzed by SCEV is
148 ; i64 (sext i32 {0,+,2}<%for.body> to i64)
149 ; We have added the nssw flag to turn this expression into the following SCEV:
150 ; i64 {0,+,2}<%for.body>
152 ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext:
153 ; LAA-NEXT: ((2 * (sext i32 {0,+,2}<%for.body> to i64))<nsw> + %a)
154 ; LAA-NEXT: --> {%a,+,4}<%for.body>
156 define void @f3(i16* noalias %a,
157 i16* noalias %b, i64 %N) {
161 for.body: ; preds = %for.body, %entry
162 %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
163 %ind1 = phi i32 [ 0, %entry ], [ %inc1, %for.body ]
165 %mul = mul i32 %ind1, 2
166 %mul_ext = sext i32 %mul to i64
168 %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
169 %loadA = load i16, i16* %arrayidxA, align 2
171 %arrayidxB = getelementptr i16, i16* %b, i64 %ind
172 %loadB = load i16, i16* %arrayidxB, align 2
174 %add = mul i16 %loadA, %loadB
176 store i16 %add, i16* %arrayidxA, align 2
178 %inc = add nuw nsw i64 %ind, 1
179 %inc1 = add i32 %ind1, 1
181 %exitcond = icmp eq i64 %inc, %N
182 br i1 %exitcond, label %for.end, label %for.body
184 for.end: ; preds = %for.body
189 ; LAA: Memory dependences are safe{{$}}
190 ; LAA: SCEV assumptions:
191 ; LAA-NEXT: {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> Added Flags: <nssw>
192 ; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64))<nsw> + %a),+,-4}<%for.body> Added Flags: <nusw>
194 ; The expression for %mul_ext as analyzed by SCEV is
195 ; i64 (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)
196 ; We have added the nssw flag to turn this expression into the following SCEV:
197 ; i64 {sext i32 (2 * (trunc i64 %N to i32)) to i64,+,-2}<%for.body>
199 ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext:
200 ; LAA-NEXT: ((2 * (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64))<nsw> + %a)
201 ; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64))<nsw> + %a),+,-4}<%for.body>
203 define void @f4(i16* noalias %a,
204 i16* noalias %b, i64 %N) {
206 %TruncN = trunc i64 %N to i32
209 for.body: ; preds = %for.body, %entry
210 %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
211 %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
213 %mul = mul i32 %ind1, 2
214 %mul_ext = sext i32 %mul to i64
216 %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext
217 %loadA = load i16, i16* %arrayidxA, align 2
219 %arrayidxB = getelementptr i16, i16* %b, i64 %ind
220 %loadB = load i16, i16* %arrayidxB, align 2
222 %add = mul i16 %loadA, %loadB
224 store i16 %add, i16* %arrayidxA, align 2
226 %inc = add nuw nsw i64 %ind, 1
227 %dec = sub i32 %ind1, 1
229 %exitcond = icmp eq i64 %inc, %N
230 br i1 %exitcond, label %for.end, label %for.body
232 for.end: ; preds = %for.body
236 ; The following function is similar to the one above, but has the GEP
237 ; to pointer %A inbounds. The index %mul doesn't have the nsw flag.
238 ; This means that the SCEV expression for %mul can wrap and we need
239 ; a SCEV predicate to continue analysis.
241 ; We can still analyze this by adding the required no wrap SCEV predicates.
244 ; LAA: Memory dependences are safe{{$}}
245 ; LAA: SCEV assumptions:
246 ; LAA-NEXT: {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> Added Flags: <nssw>
247 ; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64))<nsw> + %a),+,-4}<%for.body> Added Flags: <nusw>
249 ; LAA: [PSE] %arrayidxA = getelementptr inbounds i16, i16* %a, i32 %mul:
250 ; LAA-NEXT: ((2 * (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64))<nsw> + %a)
251 ; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64))<nsw> + %a),+,-4}<%for.body>
253 define void @f5(i16* noalias %a,
254 i16* noalias %b, i64 %N) {
256 %TruncN = trunc i64 %N to i32
259 for.body: ; preds = %for.body, %entry
260 %ind = phi i64 [ 0, %entry ], [ %inc, %for.body ]
261 %ind1 = phi i32 [ %TruncN, %entry ], [ %dec, %for.body ]
263 %mul = mul i32 %ind1, 2
265 %arrayidxA = getelementptr inbounds i16, i16* %a, i32 %mul
266 %loadA = load i16, i16* %arrayidxA, align 2
268 %arrayidxB = getelementptr inbounds i16, i16* %b, i64 %ind
269 %loadB = load i16, i16* %arrayidxB, align 2
271 %add = mul i16 %loadA, %loadB
273 store i16 %add, i16* %arrayidxA, align 2
275 %inc = add nuw nsw i64 %ind, 1
276 %dec = sub i32 %ind1, 1
278 %exitcond = icmp eq i64 %inc, %N
279 br i1 %exitcond, label %for.end, label %for.body
281 for.end: ; preds = %for.body