1 ; RUN: opt -passes=print-memderefs -S < %s -disable-output -use-dereferenceable-at-point-semantics=0 2>&1 | FileCheck %s --check-prefixes=CHECK,GLOBAL
2 ; RUN: opt -passes=print-memderefs -S < %s -disable-output -use-dereferenceable-at-point-semantics=1 2>&1 | FileCheck %s --check-prefixes=CHECK,POINT
5 ; Uses the print-deref (+ analyze to print) pass to run
6 ; isDereferenceablePointer() on many load instruction operands
8 target datalayout = "e-i32:32:64"
10 %TypeOpaque = type opaque
12 declare zeroext i1 @return_i1()
15 @globalstr = global [6 x i8] c"hello\00"
16 @globali32ptr = external global ptr
18 %struct.A = type { [8 x i8], [5 x i8] }
19 @globalstruct = external global %struct.A
21 @globalptr.align1 = external global i8, align 1
22 @globalptr.align16 = external global i8, align 16
24 ; Loads from sret arguments
25 ; CHECK-LABEL: 'test_sret'
26 ; CHECK: %sret_gep{{.*}}(aligned)
27 ; CHECK-NOT: %sret_gep_outside
28 define void @test_sret(ptr sret(%struct.A) %result) {
29 %sret_gep = getelementptr inbounds %struct.A, ptr %result, i64 0, i32 1, i64 2
30 load i8, ptr %sret_gep
32 %sret_gep_outside = getelementptr %struct.A, ptr %result, i64 0, i32 1, i64 7
33 load i8, ptr %sret_gep_outside
38 define void @test(ptr addrspace(1) dereferenceable(8) %dparam,
39 ptr addrspace(1) dereferenceable(32) align 1 %dparam.align1,
40 ptr addrspace(1) dereferenceable(32) align 16 %dparam.align16)
41 gc "statepoint-example" {
42 ; CHECK: The following are dereferenceable:
46 ; GLOBAL: %dparam{{.*}}(unaligned)
47 ; POINT-NOT: %dparam{{.*}}(unaligned)
48 %load3 = load i32, ptr addrspace(1) %dparam
50 ; GLOBAL: %relocate{{.*}}(unaligned)
51 ; POINT-NOT: %relocate{{.*}}(unaligned)
52 %tok = tail call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 0, i32 0, ptr elementtype(i1 ()) @return_i1, i32 0, i32 0, i32 0, i32 0) ["gc-live" (ptr addrspace(1) %dparam)]
53 %relocate = call ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %tok, i32 0, i32 0)
54 %load4 = load i32, ptr addrspace(1) %relocate
57 %dpa = call ptr addrspace(1) @func1(ptr addrspace(1) %dparam)
58 %nparam = getelementptr i32, ptr addrspace(1) %dpa, i32 5
59 %load5 = load i32, ptr addrspace(1) %nparam
61 ; Load from a non-dereferenceable load
63 %nd_load = load ptr, ptr @globali32ptr
64 %load6 = load i32, ptr %nd_load
66 ; Load from a dereferenceable load
67 ; GLOBAL: %d4_load{{.*}}(unaligned)
68 ; POINT-NOT: %d4_load{{.*}}(unaligned)
69 %d4_load = load ptr, ptr @globali32ptr, !dereferenceable !0
70 %load7 = load i32, ptr %d4_load
72 ; Load from an offset not covered by the dereferenceable portion
74 %d2_load = load ptr, ptr @globali32ptr, !dereferenceable !1
75 %load8 = load i32, ptr %d2_load
77 ; Load from a potentially null pointer with dereferenceable_or_null
78 ; CHECK-NOT: %d_or_null_load
79 %d_or_null_load = load ptr, ptr @globali32ptr, !dereferenceable_or_null !0
80 %load9 = load i32, ptr %d_or_null_load
82 ; Load from a non-null pointer with dereferenceable_or_null
83 ; GLOBAL: %d_or_null_non_null_load{{.*}}(unaligned)
84 ; POINT-NOT: %d_or_null_non_null_load{{.*}}(unaligned)
85 %d_or_null_non_null_load = load ptr, ptr @globali32ptr, !nonnull !2, !dereferenceable_or_null !0
86 %load10 = load i32, ptr %d_or_null_non_null_load
88 ; Loads from aligned arguments
89 ; GLOBAL: %dparam.align1{{.*}}(unaligned)
90 ; POINT-NOT: %dparam.align1{{.*}}(unaligned)
91 ; POINT-NOT: %dparam.align16{{.*}}(aligned)
92 ; GLOBAL: %dparam.align16{{.*}}(aligned)
93 %load15 = load i8, ptr addrspace(1) %dparam.align1, align 16
94 %load16 = load i8, ptr addrspace(1) %dparam.align16, align 16
97 ; GLOBAL: %gep.align1.offset1{{.*}}(unaligned)
98 ; GLOBAL: %gep.align16.offset1{{.*}}(unaligned)
99 ; GLOBAL: %gep.align1.offset16{{.*}}(unaligned)
100 ; GLOBAL: %gep.align16.offset16{{.*}}(aligned)
101 ; POINT-NOT: %gep.align1.offset1{{.*}}(unaligned)
102 ; POINT-NOT: %gep.align16.offset1{{.*}}(unaligned)
103 ; POINT-NOT: %gep.align1.offset16{{.*}}(unaligned)
104 ; POINT-NOT: %gep.align16.offset16{{.*}}(aligned)
105 %gep.align1.offset1 = getelementptr inbounds i8, ptr addrspace(1) %dparam.align1, i32 1
106 %gep.align16.offset1 = getelementptr inbounds i8, ptr addrspace(1) %dparam.align16, i32 1
107 %gep.align1.offset16 = getelementptr inbounds i8, ptr addrspace(1) %dparam.align1, i32 16
108 %gep.align16.offset16 = getelementptr inbounds i8, ptr addrspace(1) %dparam.align16, i32 16
109 %load19 = load i8, ptr addrspace(1) %gep.align1.offset1, align 16
110 %load20 = load i8, ptr addrspace(1) %gep.align16.offset1, align 16
111 %load21 = load i8, ptr addrspace(1) %gep.align1.offset16, align 16
112 %load22 = load i8, ptr addrspace(1) %gep.align16.offset16, align 16
114 ; CHECK-NOT: %no_deref_return
115 ; GLOBAL: %deref_return{{.*}}(unaligned)
116 ; GLOBAL: %deref_and_aligned_return{{.*}}(aligned)
117 ; POINT-NOT: %deref_return{{.*}}(unaligned)
118 ; POINT-NOT: %deref_and_aligned_return{{.*}}(aligned)
119 %no_deref_return = call ptr @foo()
120 %deref_return = call dereferenceable(32) ptr @foo()
121 %deref_and_aligned_return = call dereferenceable(32) align 16 ptr @foo()
122 %load23 = load i32, ptr %no_deref_return
123 %load24 = load i32, ptr %deref_return, align 16
124 %load25 = load i32, ptr %deref_and_aligned_return, align 16
126 ; Load from a dereferenceable and aligned load
127 ; GLOBAL: %d4_unaligned_load{{.*}}(unaligned)
128 ; GLOBAL: %d4_aligned_load{{.*}}(aligned)
129 ; POINT-NOT: %d4_unaligned_load{{.*}}(unaligned)
130 ; POINT-NOT: %d4_aligned_load{{.*}}(aligned)
131 %d4_unaligned_load = load ptr, ptr @globali32ptr, !dereferenceable !0
132 %d4_aligned_load = load ptr, ptr @globali32ptr, !dereferenceable !0, !align !{i64 16}
133 %load26 = load i32, ptr %d4_unaligned_load, align 16
134 %load27 = load i32, ptr %d4_aligned_load, align 16
138 ; Loads from aligned allocas
139 ; CHECK-LABEL: 'alloca_aligned'
140 ; CHECK: %alloca.align1{{.*}}(unaligned)
141 ; CHECK: %alloca.align16{{.*}}(aligned)
142 define void @alloca_aligned() {
143 %alloca.align1 = alloca i1, align 1
144 %alloca.align16 = alloca i1, align 16
146 %load17 = load i1, ptr %alloca.align1, align 16
147 %load18 = load i1, ptr %alloca.align16, align 16
151 ; CHECK-LABEL: 'alloca_basic'
152 ; CHECK: %alloca{{.*}}(aligned)
153 define void @alloca_basic() {
156 %load2 = load i1, ptr %alloca
160 ; Load from empty array alloca
161 ; CHECK-LABEL: 'alloca_empty'
162 ; CHECK-NOT: %empty_alloca
163 define void @alloca_empty() {
164 %empty_alloca = alloca i8, i64 0
166 %empty_load = load i8, ptr %empty_alloca
170 ; Alloca with no explicit alignment is aligned to preferred alignment of
171 ; the type (specified by datalayout string).
172 ; CHECK-LABEL: 'alloca_perfalign'
173 ; CHECK: %alloca.noalign{{.*}}(aligned)
174 define void @alloca_perfalign() {
175 %alloca.noalign = alloca i32
177 %load28 = load i32, ptr %alloca.noalign, align 8
181 ; CHECK-LABEL: 'global'
182 ; CHECK: @globalptr.align1{{.*}}(unaligned)
183 ; CHECK: @globalptr.align16{{.*}}(aligned)
184 ; CHECK: @globalstr{{.*}}(aligned)
185 define void @global() {
186 %load13 = load i8, ptr @globalptr.align1, align 16
187 %load14 = load i8, ptr @globalptr.align16, align 16
189 %load1 = load i8, ptr @globalstr
193 ; It's OK to overrun static array size as long as we stay within underlying
195 ; CHECK-LABEL: 'global_allocationsize'
196 ; CHECK: %within_allocation{{.*}}(aligned)
197 ; CHECK-NOT: %outside_allocation
198 define void @global_allocationsize() {
199 %within_allocation = getelementptr inbounds %struct.A, ptr @globalstruct, i64 0, i32 0, i64 10
200 %load11 = load i8, ptr %within_allocation
202 %outside_allocation = getelementptr inbounds %struct.A, ptr @globalstruct, i64 0, i32 1, i64 10
203 %load12 = load i8, ptr %outside_allocation
207 ; Loads from byval arguments
208 ; CHECK-LABEL: 'byval'
209 ; CHECK: %i8_byval{{.*}}(aligned)
210 ; CHECK-NOT: %bad_byval_cast
211 ; CHECK: %byval_gep{{.*}}(aligned)
212 ; CHECK: %A_byval{{.*}}(unaligned)
213 define void @byval(ptr byval(i8) %i8_byval,
214 ptr byval(%struct.A) %A_byval) {
216 load i8, ptr %i8_byval
218 load i32, ptr %i8_byval
220 %byval_gep = getelementptr inbounds %struct.A, ptr %A_byval, i64 0, i32 1, i64 2
221 load i8, ptr %byval_gep
222 load i32, ptr %A_byval
228 ; GLOBAL: %ptr = inttoptr i32 %val to ptr, !dereferenceable !0
229 ; POINT-NOT: %ptr = inttoptr i32 %val to ptr, !dereferenceable !0
230 define i32 @f_0(i32 %val) {
231 %ptr = inttoptr i32 %val to ptr, !dereferenceable !0
233 %load29 = load i32, ptr %ptr, align 8
238 ; The most basic case showing the difference between legacy global deref
239 ; attribute semantics and the new point-in-time semantics.
240 ; CHECK-LABEL: 'negative'
243 define void @negative(ptr dereferenceable(8) %p) {
245 %v = load i32, ptr %p
249 ; CHECK-LABEL: 'infer_func_attrs1'
251 define void @infer_func_attrs1(ptr dereferenceable(8) %p) nofree nosync {
253 %v = load i32, ptr %p
257 ; CHECK-LABEL: 'infer_func_attrs2'
260 ; FIXME: Can be inferred from attributes
261 define void @infer_func_attrs2(ptr dereferenceable(8) %p) readonly {
263 %v = load i32, ptr %p
267 ; CHECK-LABEL: 'infer_noalias1'
270 ; FIXME: Can be inferred from attributes
271 define void @infer_noalias1(ptr dereferenceable(8) noalias nofree %p) {
273 %v = load i32, ptr %p
277 ; CHECK-LABEL: 'infer_noalias2'
280 ; FIXME: Can be inferred from attributes
281 define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) nosync {
283 %v = load i32, ptr %p
288 ; Just check that we don't crash.
289 ; CHECK-LABEL: 'opaque_type_crasher'
290 define void @opaque_type_crasher(ptr dereferenceable(16) %a) {
292 %ptr8 = getelementptr inbounds i8, ptr %a, i32 8
293 br i1 undef, label %if.then, label %if.end
296 %res = load i32, ptr %ptr8, align 4
303 declare token @llvm.experimental.gc.statepoint.p0(i64, i32, ptr, i32, i32, ...)
304 declare ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token, i32, i32)
306 declare ptr addrspace(1) @func1(ptr addrspace(1) returned) nounwind argmemonly
308 ; Can free any object accessible in memory
309 declare void @mayfree()