1 ; RUN: opt -print-memderefs -analyze -S <%s | FileCheck %s
3 ; Uses the print-deref (+ analyze to print) pass to run
4 ; isDereferenceablePointer() on many load instruction operands
6 target datalayout = "e-i32:32:64"
8 %TypeOpaque = type opaque
10 declare zeroext i1 @return_i1()
13 @globalstr = global [6 x i8] c"hello\00"
14 @globali32ptr = external global i32*
16 %struct.A = type { [8 x i8], [5 x i8] }
17 @globalstruct = external global %struct.A
19 @globalptr.align1 = external global i8, align 1
20 @globalptr.align16 = external global i8, align 16
23 define void @test(%struct.A* sret %result,
24 i32 addrspace(1)* dereferenceable(8) %dparam,
25 i8 addrspace(1)* dereferenceable(32) align 1 %dparam.align1,
26 i8 addrspace(1)* dereferenceable(32) align 16 %dparam.align16,
28 %struct.A* byval %A_byval)
29 gc "statepoint-example" {
30 ; CHECK: The following are dereferenceable:
32 ; CHECK: %globalptr{{.*}}(aligned)
33 %globalptr = getelementptr inbounds [6 x i8], [6 x i8]* @globalstr, i32 0, i32 0
34 %load1 = load i8, i8* %globalptr
36 ; CHECK: %alloca{{.*}}(aligned)
38 %load2 = load i1, i1* %alloca
40 ; Load from empty array alloca
41 ; CHECK-NOT: %empty_alloca
42 %empty_alloca = alloca i8, i64 0
43 %empty_load = load i8, i8* %empty_alloca
45 ; Loads from sret arguments
46 ; CHECK: %sret_gep{{.*}}(aligned)
47 %sret_gep = getelementptr inbounds %struct.A, %struct.A* %result, i64 0, i32 1, i64 2
48 load i8, i8* %sret_gep
50 ; CHECK-NOT: %sret_gep_outside
51 %sret_gep_outside = getelementptr %struct.A, %struct.A* %result, i64 0, i32 1, i64 7
52 load i8, i8* %sret_gep_outside
54 ; CHECK: %dparam{{.*}}(aligned)
55 %load3 = load i32, i32 addrspace(1)* %dparam
57 ; CHECK: %relocate{{.*}}(aligned)
58 %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, i32 addrspace(1)* %dparam)
59 %relocate = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %tok, i32 7, i32 7)
60 %load4 = load i32, i32 addrspace(1)* %relocate
63 %dpa = call i32 addrspace(1)* @func1(i32 addrspace(1)* %dparam)
64 %nparam = getelementptr i32, i32 addrspace(1)* %dpa, i32 5
65 %load5 = load i32, i32 addrspace(1)* %nparam
67 ; Load from a non-dereferenceable load
69 %nd_load = load i32*, i32** @globali32ptr
70 %load6 = load i32, i32* %nd_load
72 ; Load from a dereferenceable load
73 ; CHECK: %d4_load{{.*}}(aligned)
74 %d4_load = load i32*, i32** @globali32ptr, !dereferenceable !0
75 %load7 = load i32, i32* %d4_load
77 ; Load from an offset not covered by the dereferenceable portion
79 %d2_load = load i32*, i32** @globali32ptr, !dereferenceable !1
80 %load8 = load i32, i32* %d2_load
82 ; Load from a potentially null pointer with dereferenceable_or_null
83 ; CHECK-NOT: %d_or_null_load
84 %d_or_null_load = load i32*, i32** @globali32ptr, !dereferenceable_or_null !0
85 %load9 = load i32, i32* %d_or_null_load
87 ; Load from a non-null pointer with dereferenceable_or_null
88 ; CHECK: %d_or_null_non_null_load{{.*}}(aligned)
89 %d_or_null_non_null_load = load i32*, i32** @globali32ptr, !nonnull !2, !dereferenceable_or_null !0
90 %load10 = load i32, i32* %d_or_null_non_null_load
92 ; It's OK to overrun static array size as long as we stay within underlying object size
93 ; CHECK: %within_allocation{{.*}}(aligned)
94 %within_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 0, i64 10
95 %load11 = load i8, i8* %within_allocation
97 ; GEP is outside the underlying object size
98 ; CHECK-NOT: %outside_allocation
99 %outside_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 1, i64 10
100 %load12 = load i8, i8* %outside_allocation
102 ; Loads from aligned globals
103 ; CHECK: @globalptr.align1{{.*}}(unaligned)
104 ; CHECK: @globalptr.align16{{.*}}(aligned)
105 %load13 = load i8, i8* @globalptr.align1, align 16
106 %load14 = load i8, i8* @globalptr.align16, align 16
108 ; Loads from aligned arguments
109 ; CHECK: %dparam.align1{{.*}}(unaligned)
110 ; CHECK: %dparam.align16{{.*}}(aligned)
111 %load15 = load i8, i8 addrspace(1)* %dparam.align1, align 16
112 %load16 = load i8, i8 addrspace(1)* %dparam.align16, align 16
114 ; Loads from byval arguments
115 ; CHECK: %i8_byval{{.*}}(aligned)
116 %i8_byval_load = load i8, i8* %i8_byval
118 ; CHECK-NOT: %byval_cast
119 %byval_cast = bitcast i8* %i8_byval to i32*
120 %bad_byval_load = load i32, i32* %byval_cast
122 ; CHECK: %byval_gep{{.*}}(aligned)
123 %byval_gep = getelementptr inbounds %struct.A, %struct.A* %A_byval, i64 0, i32 1, i64 2
124 load i8, i8* %byval_gep
126 ; Loads from aligned allocas
127 ; CHECK: %alloca.align1{{.*}}(unaligned)
128 ; CHECK: %alloca.align16{{.*}}(aligned)
129 %alloca.align1 = alloca i1, align 1
130 %alloca.align16 = alloca i1, align 16
131 %load17 = load i1, i1* %alloca.align1, align 16
132 %load18 = load i1, i1* %alloca.align16, align 16
135 ; CHECK: %gep.align1.offset1{{.*}}(unaligned)
136 ; CHECK: %gep.align16.offset1{{.*}}(unaligned)
137 ; CHECK: %gep.align1.offset16{{.*}}(unaligned)
138 ; CHECK: %gep.align16.offset16{{.*}}(aligned)
139 %gep.align1.offset1 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align1, i32 1
140 %gep.align16.offset1 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 1
141 %gep.align1.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align1, i32 16
142 %gep.align16.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 16
143 %load19 = load i8, i8 addrspace(1)* %gep.align1.offset1, align 16
144 %load20 = load i8, i8 addrspace(1)* %gep.align16.offset1, align 16
145 %load21 = load i8, i8 addrspace(1)* %gep.align1.offset16, align 16
146 %load22 = load i8, i8 addrspace(1)* %gep.align16.offset16, align 16
148 ; CHECK-NOT: %no_deref_return
149 ; CHECK: %deref_return{{.*}}(unaligned)
150 ; CHECK: %deref_and_aligned_return{{.*}}(aligned)
151 %no_deref_return = call i32* @foo()
152 %deref_return = call dereferenceable(32) i32* @foo()
153 %deref_and_aligned_return = call dereferenceable(32) align 16 i32* @foo()
154 %load23 = load i32, i32* %no_deref_return
155 %load24 = load i32, i32* %deref_return, align 16
156 %load25 = load i32, i32* %deref_and_aligned_return, align 16
158 ; Load from a dereferenceable and aligned load
159 ; CHECK: %d4_unaligned_load{{.*}}(unaligned)
160 ; CHECK: %d4_aligned_load{{.*}}(aligned)
161 %d4_unaligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0
162 %d4_aligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0, !align !{i64 16}
163 %load26 = load i32, i32* %d4_unaligned_load, align 16
164 %load27 = load i32, i32* %d4_aligned_load, align 16
166 ; Alloca with no explicit alignment is aligned to preferred alignment of
167 ; the type (specified by datalayout string).
168 ; CHECK: %alloca.noalign{{.*}}(aligned)
169 %alloca.noalign = alloca i32
170 %load28 = load i32, i32* %alloca.noalign, align 8
175 ; CHECK: The following are dereferenceable:
176 ; CHECK: %ptr = inttoptr i32 %val to i32*, !dereferenceable !0
177 define i32 @f_0(i32 %val) {
178 %ptr = inttoptr i32 %val to i32*, !dereferenceable !0
179 %load29 = load i32, i32* %ptr, align 8
183 ; Just check that we don't crash.
184 ; CHECK-LABEL: 'opaque_type_crasher'
185 define void @opaque_type_crasher(%TypeOpaque* dereferenceable(16) %a) {
187 %bc = bitcast %TypeOpaque* %a to i8*
188 %ptr8 = getelementptr inbounds i8, i8* %bc, i32 8
189 %ptr32 = bitcast i8* %ptr8 to i32*
190 br i1 undef, label %if.then, label %if.end
193 %res = load i32, i32* %ptr32, align 4
200 declare token @llvm.experimental.gc.statepoint.p0f_i1f(i64, i32, i1 ()*, i32, i32, ...)
201 declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32)
203 declare i32 addrspace(1)* @func1(i32 addrspace(1)* returned) nounwind argmemonly