]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/ValueTracking/memory-dereferenceable.ll
Vendor import of llvm trunk r321414:
[FreeBSD/FreeBSD.git] / test / Analysis / ValueTracking / memory-dereferenceable.ll
1 ; RUN: opt -print-memderefs -analyze -S <%s | FileCheck %s
2
3 ; Uses the print-deref (+ analyze to print) pass to run
4 ; isDereferenceablePointer() on many load instruction operands
5
6 target datalayout = "e-i32:32:64"
7
8 %TypeOpaque = type opaque
9
10 declare zeroext i1 @return_i1()
11
12 declare i32* @foo()
13 @globalstr = global [6 x i8] c"hello\00"
14 @globali32ptr = external global i32*
15
16 %struct.A = type { [8 x i8], [5 x i8] }
17 @globalstruct = external global %struct.A
18
19 @globalptr.align1 = external global i8, align 1
20 @globalptr.align16 = external global i8, align 16
21
22 ; CHECK-LABEL: 'test'
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,
27                   i8* byval %i8_byval,
28                   %struct.A* byval %A_byval)
29     gc "statepoint-example" {
30 ; CHECK: The following are dereferenceable:
31 entry:
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
35
36 ; CHECK: %alloca{{.*}}(aligned)
37     %alloca = alloca i1
38     %load2 = load i1, i1* %alloca
39
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
44
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
49
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
53
54 ; CHECK: %dparam{{.*}}(aligned)
55     %load3 = load i32, i32 addrspace(1)* %dparam
56
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
61
62 ; CHECK-NOT: %nparam
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
66
67     ; Load from a non-dereferenceable load
68 ; CHECK-NOT: %nd_load
69     %nd_load = load i32*, i32** @globali32ptr
70     %load6 = load i32, i32* %nd_load
71
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
76
77     ; Load from an offset not covered by the dereferenceable portion
78 ; CHECK-NOT: %d2_load
79     %d2_load = load i32*, i32** @globali32ptr, !dereferenceable !1
80     %load8 = load i32, i32* %d2_load
81
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
86
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
91
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
96
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
101
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
107
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
113
114     ; Loads from byval arguments
115 ; CHECK: %i8_byval{{.*}}(aligned)
116     %i8_byval_load = load i8, i8* %i8_byval
117
118 ; CHECK-NOT: %byval_cast
119     %byval_cast = bitcast i8* %i8_byval to i32*
120     %bad_byval_load = load i32, i32* %byval_cast
121
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
125
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
133
134     ; Loads from GEPs
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
147
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
157
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
165
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
171
172     ret void
173 }
174
175 ; Just check that we don't crash.
176 ; CHECK-LABEL: 'opaque_type_crasher'
177 define void @opaque_type_crasher(%TypeOpaque* dereferenceable(16) %a) {
178 entry:
179   %bc = bitcast %TypeOpaque* %a to i8*
180   %ptr8 = getelementptr inbounds i8, i8* %bc, i32 8
181   %ptr32 = bitcast i8* %ptr8 to i32*
182   br i1 undef, label %if.then, label %if.end
183
184 if.then:
185   %res = load i32, i32* %ptr32, align 4
186   br label %if.end
187
188 if.end:
189   ret void
190 }
191
192 declare token @llvm.experimental.gc.statepoint.p0f_i1f(i64, i32, i1 ()*, i32, i32, ...)
193 declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32)
194
195 declare i32 addrspace(1)* @func1(i32 addrspace(1)* returned) nounwind argmemonly
196
197 !0 = !{i64 4}
198 !1 = !{i64 2}
199 !2 = !{}