1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=lower-constant-intrinsics -S < %s | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
5 target triple = "x86_64-apple-darwin10.0.0"
7 declare i64 @llvm.objectsize.i64(ptr, i1, i1, i1) nounwind readonly
8 declare i64 @llvm.objectsize.i64.p1(ptr addrspace(1), i1, i1, i1) nounwind readonly
9 declare void @llvm.trap() nounwind
11 ; objectsize should fold to a constant, which causes the branch to fold to an
13 define i32 @test1(ptr %ptr) nounwind ssp noredzone align 2 {
14 ; CHECK-LABEL: @test1(
16 ; CHECK-NEXT: br label [[T:%.*]]
18 ; CHECK-NEXT: ret i32 4
21 %0 = tail call i64 @llvm.objectsize.i64(ptr %ptr, i1 false, i1 false, i1 false)
22 %1 = icmp ugt i64 %0, 3
23 br i1 %1, label %T, label %trap
26 trap: ; preds = %0, %entry
27 tail call void @llvm.trap() noreturn nounwind
34 define i64 @test_objectsize_null_flag(ptr %ptr) {
35 ; CHECK-LABEL: @test_objectsize_null_flag(
37 ; CHECK-NEXT: ret i64 -1
40 %0 = tail call i64 @llvm.objectsize.i64(ptr null, i1 false, i1 true, i1 false)
44 define i64 @test_objectsize_null_flag_min(ptr %ptr) {
45 ; CHECK-LABEL: @test_objectsize_null_flag_min(
47 ; CHECK-NEXT: ret i64 0
50 %0 = tail call i64 @llvm.objectsize.i64(ptr null, i1 true, i1 true, i1 false)
54 ; Test foldable null pointers because we evaluate them with non-exact modes in
56 define i64 @test_objectsize_null_flag_noas0() {
57 ; CHECK-LABEL: @test_objectsize_null_flag_noas0(
59 ; CHECK-NEXT: ret i64 -1
62 %0 = tail call i64 @llvm.objectsize.i64.p1(ptr addrspace(1) null, i1 false,
67 define i64 @test_objectsize_null_flag_min_noas0() {
68 ; CHECK-LABEL: @test_objectsize_null_flag_min_noas0(
70 ; CHECK-NEXT: ret i64 0
73 %0 = tail call i64 @llvm.objectsize.i64.p1(ptr addrspace(1) null, i1 true,
78 define i64 @test_objectsize_null_known_flag_noas0() {
79 ; CHECK-LABEL: @test_objectsize_null_known_flag_noas0(
81 ; CHECK-NEXT: ret i64 -1
84 %0 = tail call i64 @llvm.objectsize.i64.p1(ptr addrspace(1) null, i1 false,
89 define i64 @test_objectsize_null_known_flag_min_noas0() {
90 ; CHECK-LABEL: @test_objectsize_null_known_flag_min_noas0(
92 ; CHECK-NEXT: ret i64 0
95 %0 = tail call i64 @llvm.objectsize.i64.p1(ptr addrspace(1) null, i1 true,
100 define i64 @test_objectsize_byval_arg(ptr byval([42 x i8]) %ptr) {
101 ; CHECK-LABEL: @test_objectsize_byval_arg(
102 ; CHECK-NEXT: ret i64 42
104 %size = tail call i64 @llvm.objectsize.i64(ptr %ptr, i1 true, i1 false, i1 false)
108 define i64 @test_objectsize_byref_arg(ptr byref([42 x i8]) %ptr) {
109 ; CHECK-LABEL: @test_objectsize_byref_arg(
110 ; CHECK-NEXT: ret i64 42
112 %size = tail call i64 @llvm.objectsize.i64(ptr %ptr, i1 true, i1 false, i1 false)
116 ; https://llvm.org/PR50023
117 ; The alloca operand type may not match pointer type size.
119 define i64 @vla_pointer_size_mismatch(i42 %x) {
120 ; CHECK-LABEL: @vla_pointer_size_mismatch(
121 ; CHECK-NEXT: [[TMP1:%.*]] = zext i42 [[X:%.*]] to i64
122 ; CHECK-NEXT: [[TMP2:%.*]] = mul i64 1, [[TMP1]]
123 ; CHECK-NEXT: [[A:%.*]] = alloca i8, i42 [[X]], align 1
124 ; CHECK-NEXT: [[G1:%.*]] = getelementptr i8, ptr [[A]], i8 17
125 ; CHECK-NEXT: [[TMP3:%.*]] = sub i64 [[TMP2]], 17
126 ; CHECK-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP2]], 17
127 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i64 0, i64 [[TMP3]]
128 ; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i64 [[TMP5]], -1
129 ; CHECK-NEXT: call void @llvm.assume(i1 [[TMP6]])
130 ; CHECK-NEXT: ret i64 [[TMP5]]
132 %A = alloca i8, i42 %x, align 1
133 %G1 = getelementptr i8, ptr %A, i8 17
134 %objsize = call i64 @llvm.objectsize.i64(ptr %G1, i1 false, i1 true, i1 true)
138 declare ptr @malloc(i64) allocsize(0)
140 define i64 @test_objectsize_malloc() {
141 ; CHECK-LABEL: @test_objectsize_malloc(
142 ; CHECK-NEXT: [[PTR:%.*]] = call ptr @malloc(i64 16)
143 ; CHECK-NEXT: ret i64 16
145 %ptr = call ptr @malloc(i64 16)
146 %objsize = call i64 @llvm.objectsize.i64(ptr %ptr, i1 false, i1 true, i1 true)
150 @gv_weak = weak global i64 zeroinitializer, align 16
152 define i32 @promote_with_objectsize_min_false() {
153 ; CHECK-LABEL: @promote_with_objectsize_min_false(
154 ; CHECK-NEXT: ret i32 -1
156 %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 false, i1 false, i1 false)
160 define i32 @promote_with_objectsize_min_true() {
161 ; CHECK-LABEL: @promote_with_objectsize_min_true(
162 ; CHECK-NEXT: ret i32 8
164 %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_weak, i1 true, i1 false, i1 false)
168 @gv_extern = extern_weak global i64, align 16
170 define i32 @promote_with_objectsize_nullunknown_false() {
171 ; CHECK-LABEL: @promote_with_objectsize_nullunknown_false(
172 ; CHECK-NEXT: ret i32 0
174 %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 false, i1 false)
178 define i32 @promote_with_objectsize_nullunknown_true() {
179 ; CHECK-LABEL: @promote_with_objectsize_nullunknown_true(
180 ; CHECK-NEXT: ret i32 0
182 %size = call i32 @llvm.objectsize.i32.p0(ptr @gv_extern, i1 true, i1 true, i1 false)
186 define i64 @out_of_bound_gep() {
187 ; CHECK-LABEL: @out_of_bound_gep(
188 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i32 4, align 1
189 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i8 8
190 ; CHECK-NEXT: ret i64 0
192 %obj = alloca i8, i32 4
193 %slide = getelementptr i8, ptr %obj, i8 8
194 %objsize = call i64 @llvm.objectsize.i64(ptr %slide, i1 false, i1 false, i1 false)
198 define i64 @wrapping_gep(i1 %c) {
199 ; CHECK-LABEL: @wrapping_gep(
200 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i64 4, align 1
201 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i64 -9223372036854775807
202 ; CHECK-NEXT: [[SLIDE_BIS:%.*]] = getelementptr i8, ptr [[SLIDE]], i64 -9223372036854775808
203 ; CHECK-NEXT: ret i64 3
205 %obj = alloca i8, i64 4
206 %slide = getelementptr i8, ptr %obj, i64 9223372036854775809
207 %slide.bis = getelementptr i8, ptr %slide, i64 9223372036854775808
208 %objsize = call i64 @llvm.objectsize.i64(ptr %slide.bis, i1 false, i1 false, i1 false)
212 define i64 @wrapping_gep_neg(i1 %c) {
213 ; CHECK-LABEL: @wrapping_gep_neg(
214 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i64 4, align 1
215 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i64 9223372036854775807
216 ; CHECK-NEXT: [[SLIDE_BIS:%.*]] = getelementptr i8, ptr [[SLIDE]], i64 9223372036854775807
217 ; CHECK-NEXT: ret i64 0
219 %obj = alloca i8, i64 4
220 %slide = getelementptr i8, ptr %obj, i64 9223372036854775807
221 %slide.bis = getelementptr i8, ptr %slide, i64 9223372036854775807
222 %objsize = call i64 @llvm.objectsize.i64(ptr %slide.bis, i1 false, i1 false, i1 false)
226 define i64 @wrapping_gep_large_alloc(i1 %c) {
227 ; CHECK-LABEL: @wrapping_gep_large_alloc(
228 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i64 9223372036854775807, align 1
229 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i64 9223372036854775807
230 ; CHECK-NEXT: [[SLIDE_BIS:%.*]] = getelementptr i8, ptr [[SLIDE]], i64 3
231 ; CHECK-NEXT: [[SLIDE_TER:%.*]] = getelementptr i8, ptr [[SLIDE_BIS]], i64 -4
232 ; CHECK-NEXT: ret i64 1
234 %obj = alloca i8, i64 9223372036854775807
235 %slide = getelementptr i8, ptr %obj, i64 9223372036854775807
236 %slide.bis = getelementptr i8, ptr %slide, i64 3
237 %slide.ter = getelementptr i8, ptr %slide.bis, i64 -4
238 %objsize = call i64 @llvm.objectsize.i64(ptr %slide.ter, i1 false, i1 false, i1 false)
242 ; We don't analyze allocations larger than platform's ptrdiff_t
243 define i64 @large_alloca() {
244 ; CHECK-LABEL: @large_alloca(
245 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i64 -9223372036854775808, align 1
246 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i64 9223372036854775807
247 ; CHECK-NEXT: ret i64 -1
249 %obj = alloca i8, i64 9223372036854775808
250 %slide = getelementptr i8, ptr %obj, i64 9223372036854775807
251 %objsize = call i64 @llvm.objectsize.i64(ptr %slide, i1 false, i1 false, i1 false)
255 ; We don't analyze allocations larger than platform's ptrdiff_t
256 define i64 @large_malloc() {
257 ; CHECK-LABEL: @large_malloc(
258 ; CHECK-NEXT: [[OBJ:%.*]] = call ptr @malloc(i64 -9223372036854775808)
259 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i64 9223372036854775807
260 ; CHECK-NEXT: ret i64 -1
262 %obj = call ptr @malloc(i64 9223372036854775808)
263 %slide = getelementptr i8, ptr %obj, i64 9223372036854775807
264 %objsize = call i64 @llvm.objectsize.i64(ptr %slide, i1 false, i1 false, i1 false)
268 define i64 @out_of_bound_negative_gep(i1 %c) {
269 ; CHECK-LABEL: @out_of_bound_negative_gep(
270 ; CHECK-NEXT: [[OBJ:%.*]] = alloca i8, i32 4, align 1
271 ; CHECK-NEXT: [[SLIDE:%.*]] = getelementptr i8, ptr [[OBJ]], i8 -8
272 ; CHECK-NEXT: ret i64 0
274 %obj = alloca i8, i32 4
275 %slide = getelementptr i8, ptr %obj, i8 -8
276 %objsize = call i64 @llvm.objectsize.i64(ptr %slide, i1 false, i1 false, i1 false)
280 declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1)