aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Analysis/BasicAA/gep-nuw-alias.ll
blob: a5f1c1c747cc3f814ab83d213d405c435c6fd03c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s

; CHECK-LABEL: test_no_lower_bound
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b
define void @test_no_lower_bound(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p, i64 4
  %b = getelementptr nuw i8, ptr %p, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: test_lower_bound_lt_size
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b
define void @test_lower_bound_lt_size(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p
  %add = getelementptr nuw i8, ptr %p, i64 2
  %b = getelementptr nuw i8, ptr %add, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: test_lower_bound_ge_size
;
; CHECK-DAG: NoAlias: i32* %a, i32* %b
define void @test_lower_bound_ge_size(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p
  %add = getelementptr nuw i8, ptr %p, i64 4
  %b = getelementptr nuw i8, ptr %add, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: test_not_all_nuw
;
; If part of the addressing is done with non-nuw GEPs, we can't use properties
; implied by the last GEP with the whole offset. In this case, the calculation
; of %add (%p + 4) could wrap the pointer index type, such that %add +<nuw> %i
; could still alias with %p.
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b
define void @test_not_all_nuw(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p
  %add = getelementptr i8, ptr %p, i64 4
  %b = getelementptr nuw i8, ptr %add, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: test_multi_step_not_all_nuw
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b
define void @test_multi_step_not_all_nuw(ptr %p, i64 %i, i64 %j, i64 %k) {
  %a = getelementptr i8, ptr %p
  %add = getelementptr i8, ptr %p, i64 4
  %step1 = getelementptr i8, ptr %add, i64 %i
  %step2 = getelementptr i8, ptr %step1, i64 %j
  %b = getelementptr nuw i8, ptr %step2, i64 %k

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: test_multi_step_all_nuw
;
; CHECK-DAG: NoAlias: i32* %a, i32* %b
define void @test_multi_step_all_nuw(ptr %p, i64 %i, i64 %j, i64 %k) {
  %a = getelementptr i8, ptr %p
  %add = getelementptr nuw i8, ptr %p, i64 4
  %step1 = getelementptr nuw i8, ptr %add, i64 %i
  %step2 = getelementptr nuw i8, ptr %step1, i64 %j
  %b = getelementptr nuw i8, ptr %step2, i64 %k

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

%struct = type { i64, [2 x i32], i64 }

; CHECK-LABEL: test_struct_no_nuw
;
; The array access may alias with the struct elements before and after, because
; we cannot prove that (%arr + %i) does not alias with the base pointer %p.
;
; CHECK-DAG: MayAlias: i32* %arrayidx, i64* %st
; CHECK-DAG: NoAlias: i64* %after, i64* %st
; CHECK-DAG: MayAlias: i64* %after, i32* %arrayidx

define void @test_struct_no_nuw(ptr %st, i64 %i) {
  %arr = getelementptr i8, ptr %st, i64 8
  %arrayidx = getelementptr [2 x i32], ptr %arr, i64 0, i64 %i
  %after = getelementptr i8, ptr %st, i64 16

  load i64, ptr %st
  load i32, ptr %arrayidx
  load i64, ptr %after

  ret void
}

; CHECK-LABEL: test_struct_nuw
;
; We can prove that the array access does not alias with struct element before,
; because we can prove that (%arr +<nuw> %i) does not wrap the pointer index
; type (add nuw). The array access may still alias with the struct element
; after, as the add nuw property does not preclude this.
;
; CHECK-DAG: NoAlias: i32* %arrayidx, i64* %st
; CHECK-DAG: NoAlias: i64* %after, i64* %st
; CHECK-DAG: MayAlias: i64* %after, i32* %arrayidx

define void @test_struct_nuw(ptr %st, i64 %i) {
  %arr = getelementptr nuw i8, ptr %st, i64 8
  %arrayidx = getelementptr nuw [2 x i32], ptr %arr, i64 0, i64 %i
  %after = getelementptr nuw i8, ptr %st, i64 16

  load i64, ptr %st
  load i32, ptr %arrayidx
  load i64, ptr %after

  ret void
}

; CHECK-LABEL: constant_offset_overflow
;
; If subtraction of constant offsets could overflow in an unsigned sense, we
; cannot prove the lower bound between the GEPs and so they may still alias.
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b

define void @constant_offset_overflow(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p, i64 -8
  %add = getelementptr nuw i8, ptr %p, i64 4
  %b = getelementptr nuw i8, ptr %add, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: equal_var_idx_noalias
;
; If GEPs have equal variable indices, we can prove NoAlias when the Scale of
; the RHS GEP is greater, as in this scenario the constant lower bound holds.
;
; CHECK-DAG: NoAlias: i32* %a, i32* %b

define void @equal_var_idx_noalias(ptr %p, i64 %i) {
  %a = getelementptr i8, ptr %p, i64 %i

  %add = getelementptr nuw i8, ptr %p, i64 4
  %b = getelementptr nuw i16, ptr %add, i64 %i

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: equal_var_idx_alias
;
; If GEPs have equal variable indices, we cannot prove NoAlias when the Scale of
; the RHS GEP is ult Scale of the LHS GEP.
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b

define void @equal_var_idx_alias(ptr %p, i64 %i) {
  %a = getelementptr i32, ptr %p, i64 %i

  %add = getelementptr nuw i8, ptr %p, i64 4
  %b = getelementptr nuw i16, ptr %add, i64 %i

  load i32, ptr %b
  load i32, ptr %a

  ret void
}

; CHECK-LABEL: both_var_idx
;
; If the RHS GEP has unmatched variable indices, we cannot prove a constant
; lower bound between GEPs.
;
; CHECK-DAG: MayAlias: i32* %a, i32* %b

define void @both_var_idx(ptr %p, i64 %i, i64 %j) {
  %a = getelementptr i8, ptr %p, i64 %i

  %add = getelementptr nuw i8, ptr %p, i64 4
  %b = getelementptr nuw i8, ptr %add, i64 %j

  load i32, ptr %a
  load i32, ptr %b

  ret void
}

; CHECK-LABEL: add_no_nuw
; CHECK: MayAlias: i8* %gep, i8* %p
define i8 @add_no_nuw(ptr %p, i64 %n) {
  store i8 3, ptr %p

  %add = add i64 %n, 1
  %gep = getelementptr nuw i8, ptr %p, i64 %add
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: add_nuw
; CHECK: NoAlias: i8* %gep, i8* %p
define i8 @add_nuw(ptr %p, i64 %n) {
  store i8 3, ptr %p

  %add = add nuw i64 %n, 1
  %gep = getelementptr nuw i8, ptr %p, i64 %add
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: add_no_nuw
; CHECK: MayAlias: i8* %gep, i16* %p
define i8 @add_no_nuw_scale(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add i64 %n, 1
  %gep = getelementptr nuw i16, ptr %p, i64 %add
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: add_nuw
; CHECK: NoAlias: i8* %gep, i16* %p
define i8 @add_nuw_scale(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add nuw i64 %n, 1
  %gep = getelementptr nuw i16, ptr %p, i64 %add
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: sub_nuw
; CHECK: MayAlias: i8* %gep, i8* %p
define i8 @sub_nuw(ptr %p, i64 %n) {
  store i8 3, ptr %p

  %add = sub nuw i64 %n, 1
  %gep = getelementptr nuw i8, ptr %p, i64 %add
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: mul_no_nuw
; CHECK: MayAlias: i8* %gep, i16* %p
define i8 @mul_no_nuw(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add nuw i64 %n, 1
  %mul = mul i64 %add, 2
  %gep = getelementptr nuw i8, ptr %p, i64 %mul
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: mul_nuw
; CHECK: NoAlias: i8* %gep, i16* %p
define i8 @mul_nuw(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add nuw i64 %n, 1
  %mul = mul nuw i64 %add, 2
  %gep = getelementptr nuw i8, ptr %p, i64 %mul
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: shl_no_nuw
; CHECK: MayAlias: i8* %gep, i16* %p
define i8 @shl_no_nuw(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add nuw i64 %n, 1
  %shl = shl i64 %add, 1
  %gep = getelementptr nuw i8, ptr %p, i64 %shl
  %val = load i8, ptr %gep
  ret i8 %val
}

; CHECK-LABEL: shl_nuw
; CHECK: NoAlias: i8* %gep, i16* %p
define i8 @shl_nuw(ptr %p, i64 %n) {
  store i16 3, ptr %p

  %add = add nuw i64 %n, 1
  %shl = shl nuw i64 %add, 1
  %gep = getelementptr nuw i8, ptr %p, i64 %shl
  %val = load i8, ptr %gep
  ret i8 %val
}