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 i32*
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(%struct.A* sret(%struct.A) %result) {
29 %sret_gep = getelementptr inbounds %struct.A, %struct.A* %result, i64 0, i32 1, i64 2
30 load i8, i8* %sret_gep
32 %sret_gep_outside = getelementptr %struct.A, %struct.A* %result, i64 0, i32 1, i64 7
33 load i8, i8* %sret_gep_outside
38 define void @test(i32 addrspace(1)* dereferenceable(8) %dparam,
39 i8 addrspace(1)* dereferenceable(32) align 1 %dparam.align1,
40 i8 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, i32 addrspace(1)* %dparam
50 ; GLOBAL: %relocate{{.*}}(unaligned)
51 ; POINT-NOT: %relocate{{.*}}(unaligned)
52 %tok = tail call token (i64, i32, i1 ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_i1f(i64 0, i32 0, i1 ()* @return_i1, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %dparam)]
53 %relocate = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %tok, i32 0, i32 0)
54 %load4 = load i32, i32 addrspace(1)* %relocate
57 %dpa = call i32 addrspace(1)* @func1(i32 addrspace(1)* %dparam)
58 %nparam = getelementptr i32, i32 addrspace(1)* %dpa, i32 5
59 %load5 = load i32, i32 addrspace(1)* %nparam
61 ; Load from a non-dereferenceable load
63 %nd_load = load i32*, i32** @globali32ptr
64 %load6 = load i32, i32* %nd_load
66 ; Load from a dereferenceable load
67 ; GLOBAL: %d4_load{{.*}}(unaligned)
68 ; POINT-NOT: %d4_load{{.*}}(unaligned)
69 %d4_load = load i32*, i32** @globali32ptr, !dereferenceable !0
70 %load7 = load i32, i32* %d4_load
72 ; Load from an offset not covered by the dereferenceable portion
74 %d2_load = load i32*, i32** @globali32ptr, !dereferenceable !1
75 %load8 = load i32, i32* %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 i32*, i32** @globali32ptr, !dereferenceable_or_null !0
80 %load9 = load i32, i32* %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 i32*, i32** @globali32ptr, !nonnull !2, !dereferenceable_or_null !0
86 %load10 = load i32, i32* %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, i8 addrspace(1)* %dparam.align1, align 16
94 %load16 = load i8, i8 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, i8 addrspace(1)* %dparam.align1, i32 1
106 %gep.align16.offset1 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 1
107 %gep.align1.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align1, i32 16
108 %gep.align16.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 16
109 %load19 = load i8, i8 addrspace(1)* %gep.align1.offset1, align 16
110 %load20 = load i8, i8 addrspace(1)* %gep.align16.offset1, align 16
111 %load21 = load i8, i8 addrspace(1)* %gep.align1.offset16, align 16
112 %load22 = load i8, i8 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 i32* @foo()
120 %deref_return = call dereferenceable(32) i32* @foo()
121 %deref_and_aligned_return = call dereferenceable(32) align 16 i32* @foo()
122 %load23 = load i32, i32* %no_deref_return
123 %load24 = load i32, i32* %deref_return, align 16
124 %load25 = load i32, i32* %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 i32*, i32** @globali32ptr, !dereferenceable !0
132 %d4_aligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0, !align !{i64 16}
133 %load26 = load i32, i32* %d4_unaligned_load, align 16
134 %load27 = load i32, i32* %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, i1* %alloca.align1, align 16
147 %load18 = load i1, i1* %alloca.align16, align 16
151 ; CHECK-LABEL: 'alloca_basic'
152 ; CHECK: %alloca{{.*}}(aligned)
153 define void @alloca_basic() {
156 %load2 = load i1, i1* %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, i8* %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, i32* %alloca.noalign, align 8
181 ; CHECK-LABEL: 'global'
182 ; CHECK: @globalptr.align1{{.*}}(unaligned)
183 ; CHECK: @globalptr.align16{{.*}}(aligned)
184 ; CHECK: %globalptr{{.*}}(aligned)
185 define void @global() {
186 %load13 = load i8, i8* @globalptr.align1, align 16
187 %load14 = load i8, i8* @globalptr.align16, align 16
189 %globalptr = getelementptr inbounds [6 x i8], [6 x i8]* @globalstr, i32 0, i32 0
190 %load1 = load i8, i8* %globalptr
194 ; It's OK to overrun static array size as long as we stay within underlying
196 ; CHECK-LABEL: 'global_allocationsize'
197 ; CHECK: %within_allocation{{.*}}(aligned)
198 ; CHECK-NOT: %outside_allocation
199 define void @global_allocationsize() {
200 %within_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 0, i64 10
201 %load11 = load i8, i8* %within_allocation
203 %outside_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 1, i64 10
204 %load12 = load i8, i8* %outside_allocation
208 ; Loads from byval arguments
209 ; CHECK-LABEL: 'byval'
210 ; CHECK: %i8_byval{{.*}}(aligned)
211 ; CHECK-NOT: %bad_byval_cast
212 ; CHECK: %byval_gep{{.*}}(aligned)
213 ; CHECK: %good_byval_cast{{.*}}(unaligned)
214 define void @byval(i8* byval(i8) %i8_byval,
215 %struct.A* byval(%struct.A) %A_byval) {
217 load i8, i8* %i8_byval
219 %bad_byval_cast = bitcast i8* %i8_byval to i32*
220 load i32, i32* %bad_byval_cast
222 %byval_gep = getelementptr inbounds %struct.A, %struct.A* %A_byval, i64 0, i32 1, i64 2
223 load i8, i8* %byval_gep
224 %good_byval_cast = bitcast %struct.A* %A_byval to i32*
225 load i32, i32* %good_byval_cast
231 ; GLOBAL: %ptr = inttoptr i32 %val to i32*, !dereferenceable !0
232 ; POINT-NOT: %ptr = inttoptr i32 %val to i32*, !dereferenceable !0
233 define i32 @f_0(i32 %val) {
234 %ptr = inttoptr i32 %val to i32*, !dereferenceable !0
236 %load29 = load i32, i32* %ptr, align 8
241 ; The most basic case showing the difference between legacy global deref
242 ; attribute semantics and the new point-in-time semantics.
243 ; CHECK-LABEL: 'negative'
246 define void @negative(i32* dereferenceable(8) %p) {
248 %v = load i32, i32* %p
252 ; CHECK-LABEL: 'infer_func_attrs1'
254 define void @infer_func_attrs1(i32* dereferenceable(8) %p) nofree nosync {
256 %v = load i32, i32* %p
260 ; CHECK-LABEL: 'infer_func_attrs2'
263 ; FIXME: Can be inferred from attributes
264 define void @infer_func_attrs2(i32* dereferenceable(8) %p) readonly {
266 %v = load i32, i32* %p
270 ; CHECK-LABEL: 'infer_noalias1'
273 ; FIXME: Can be inferred from attributes
274 define void @infer_noalias1(i32* dereferenceable(8) noalias nofree %p) {
276 %v = load i32, i32* %p
280 ; CHECK-LABEL: 'infer_noalias2'
283 ; FIXME: Can be inferred from attributes
284 define void @infer_noalias2(i32* dereferenceable(8) noalias readonly %p) nosync {
286 %v = load i32, i32* %p
291 ; Just check that we don't crash.
292 ; CHECK-LABEL: 'opaque_type_crasher'
293 define void @opaque_type_crasher(%TypeOpaque* dereferenceable(16) %a) {
295 %bc = bitcast %TypeOpaque* %a to i8*
296 %ptr8 = getelementptr inbounds i8, i8* %bc, i32 8
297 %ptr32 = bitcast i8* %ptr8 to i32*
298 br i1 undef, label %if.then, label %if.end
301 %res = load i32, i32* %ptr32, align 4
308 declare token @llvm.experimental.gc.statepoint.p0f_i1f(i64, i32, i1 ()*, i32, i32, ...)
309 declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32)
311 declare i32 addrspace(1)* @func1(i32 addrspace(1)* returned) nounwind argmemonly
313 ; Can free any object accessible in memory
314 declare void @mayfree()