allocsize.ll
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; RUN: opt < %s -instcombine -S | FileCheck %s
;
; Test that instcombine folds allocsize function calls properly.
; Dummy arguments are inserted to verify that allocsize is picking the right
; args, and to prove that arbitrary unfoldable values don't interfere with
; allocsize if they're not used by allocsize.
declare i8* @my_malloc(i8*, i32) allocsize(1)
declare i8* @my_calloc(i8*, i8*, i32, i32) allocsize(2, 3)
; CHECK-LABEL: define void @test_malloc
define void @test_malloc(i8** %p, i64* %r) {
%1 = call i8* @my_malloc(i8* null, i32 100)
store i8* %1, i8** %p, align 8 ; To ensure objectsize isn't killed
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
; CHECK: store i64 100
store i64 %2, i64* %r, align 8
ret void
}
; CHECK-LABEL: define void @test_calloc
define void @test_calloc(i8** %p, i64* %r) {
%1 = call i8* @my_calloc(i8* null, i8* null, i32 100, i32 5)
store i8* %1, i8** %p, align 8 ; To ensure objectsize isn't killed
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
; CHECK: store i64 500
store i64 %2, i64* %r, align 8
ret void
}
; Failure cases with non-constant values...
; CHECK-LABEL: define void @test_malloc_fails
define void @test_malloc_fails(i8** %p, i64* %r, i32 %n) {
%1 = call i8* @my_malloc(i8* null, i32 %n)
store i8* %1, i8** %p, align 8 ; To ensure objectsize isn't killed
; CHECK: @llvm.objectsize.i64.p0i8
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
store i64 %2, i64* %r, align 8
ret void
}
; CHECK-LABEL: define void @test_calloc_fails
define void @test_calloc_fails(i8** %p, i64* %r, i32 %n) {
%1 = call i8* @my_calloc(i8* null, i8* null, i32 %n, i32 5)
store i8* %1, i8** %p, align 8 ; To ensure objectsize isn't killed
; CHECK: @llvm.objectsize.i64.p0i8
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
store i64 %2, i64* %r, align 8
%3 = call i8* @my_calloc(i8* null, i8* null, i32 100, i32 %n)
store i8* %3, i8** %p, align 8 ; To ensure objectsize isn't killed
; CHECK: @llvm.objectsize.i64.p0i8
%4 = call i64 @llvm.objectsize.i64.p0i8(i8* %3, i1 false)
store i64 %4, i64* %r, align 8
ret void
}
declare i8* @my_malloc_outofline(i8*, i32) #0
declare i8* @my_calloc_outofline(i8*, i8*, i32, i32) #1
; Verifying that out of line allocsize is parsed correctly
; CHECK-LABEL: define void @test_outofline
define void @test_outofline(i8** %p, i64* %r) {
%1 = call i8* @my_malloc_outofline(i8* null, i32 100)
store i8* %1, i8** %p, align 8 ; To ensure objectsize isn't killed
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
; CHECK: store i64 100
store i64 %2, i64* %r, align 8
%3 = call i8* @my_calloc_outofline(i8* null, i8* null, i32 100, i32 5)
store i8* %3, i8** %p, align 8 ; To ensure objectsize isn't killed
%4 = call i64 @llvm.objectsize.i64.p0i8(i8* %3, i1 false)
; CHECK: store i64 500
store i64 %4, i64* %r, align 8
ret void
}
declare i8* @my_malloc_i64(i8*, i64) #0
declare i8* @my_tiny_calloc(i8*, i8*, i8, i8) #1
declare i8* @my_varied_calloc(i8*, i8*, i32, i8) #1
; CHECK-LABEL: define void @test_overflow
define void @test_overflow(i8** %p, i32* %r) {
%r64 = bitcast i32* %r to i64*
; (2**31 + 1) * 2 > 2**31. So overflow. Yay.
%big_malloc = call i8* @my_calloc(i8* null, i8* null, i32 2147483649, i32 2)
store i8* %big_malloc, i8** %p, align 8
; CHECK: @llvm.objectsize
%1 = call i32 @llvm.objectsize.i32.p0i8(i8* %big_malloc, i1 false)
store i32 %1, i32* %r, align 4
%big_little_malloc = call i8* @my_tiny_calloc(i8* null, i8* null, i8 127, i8 4)
store i8* %big_little_malloc, i8** %p, align 8
; CHECK: store i32 508
%2 = call i32 @llvm.objectsize.i32.p0i8(i8* %big_little_malloc, i1 false)
store i32 %2, i32* %r, align 4
; malloc(2**33)
%big_malloc_i64 = call i8* @my_malloc_i64(i8* null, i64 8589934592)
store i8* %big_malloc_i64, i8** %p, align 8
; CHECK: @llvm.objectsize
%3 = call i32 @llvm.objectsize.i32.p0i8(i8* %big_malloc_i64, i1 false)
store i32 %3, i32* %r, align 4
%4 = call i64 @llvm.objectsize.i64.p0i8(i8* %big_malloc_i64, i1 false)
; CHECK: store i64 8589934592
store i64 %4, i64* %r64, align 8
; Just intended to ensure that we properly handle args of different types...
%varied_calloc = call i8* @my_varied_calloc(i8* null, i8* null, i32 1000, i8 5)
store i8* %varied_calloc, i8** %p, align 8
; CHECK: store i32 5000
%5 = call i32 @llvm.objectsize.i32.p0i8(i8* %varied_calloc, i1 false)
store i32 %5, i32* %r, align 4
ret void
}
; CHECK-LABEL: define void @test_nobuiltin
; We had a bug where `nobuiltin` would cause `allocsize` to be ignored in
; @llvm.objectsize calculations.
define void @test_nobuiltin(i8** %p, i64* %r) {
%1 = call i8* @my_malloc(i8* null, i32 100) nobuiltin
store i8* %1, i8** %p, align 8
%2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
; CHECK: store i64 100
store i64 %2, i64* %r, align 8
ret void
}
attributes #0 = { allocsize(1) }
attributes #1 = { allocsize(2, 3) }
declare i32 @llvm.objectsize.i32.p0i8(i8*, i1)
declare i64 @llvm.objectsize.i64.p0i8(i8*, i1)