memccpy.ll
10.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
@hello = private constant [11 x i8] c"helloworld\00", align 1
@NoNulTerminator = private constant [10 x i8] c"helloworld", align 1
@StopCharAfterNulTerminator = private constant [12 x i8] c"helloworld\00x", align 1
@StringWithEOF = constant [14 x i8] c"helloworld\FFab\00", align 1
declare i8* @memccpy(i8*, i8*, i32, i64)
define i8* @memccpy_to_memcpy(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy(
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i64*
; CHECK-NEXT: store i64 8245940763182785896, i64* [[TMP1]], align 1
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 8
; CHECK-NEXT: ret i8* [[TMP2]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 12) ; 114 is 'r'
ret i8* %call
}
define i8* @memccpy_to_memcpy2(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy2(
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i64*
; CHECK-NEXT: store i64 8245940763182785896, i64* [[TMP1]], align 1
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 8
; CHECK-NEXT: ret i8* [[TMP2]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 8); ; 114 is 'r'
ret i8* %call
}
define void @memccpy_to_memcpy3(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy3(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* nonnull align 1 dereferenceable(5) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 5, i1 false)
; CHECK-NEXT: ret void
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 111, i64 10) ; 111 is 'o'
ret void
}
define void @memccpy_to_memcpy4(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy4(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* nonnull align 1 dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 11, i1 false)
; CHECK-NEXT: ret void
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 0, i64 12)
ret void
}
define i8* @memccpy_to_memcpy5(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy5(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(7) [[DST:%.*]], i8* nonnull align 1 dereferenceable(7) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 7, i1 false)
; CHECK-NEXT: ret i8* null
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
ret i8* %call
}
define i8* @memccpy_to_memcpy6(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy6(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(6) [[DST:%.*]], i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 6, i1 false)
; CHECK-NEXT: ret i8* null
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 6);
ret i8* %call
}
define i8* @memccpy_to_memcpy7(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy7(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* nonnull align 1 dereferenceable(5) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 5, i1 false)
; CHECK-NEXT: ret i8* null
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 5) ; 115 is 's'
ret i8* %call
}
define i8* @memccpy_to_memcpy8(i8* %dst) {
; CHECK-LABEL: @memccpy_to_memcpy8(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* nonnull align 1 dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 11, i1 false)
; CHECK-NEXT: ret i8* null
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 11) ; 115 is 's'
ret i8* %call
}
define i8* @memccpy_to_memcpy9(i8* %dst, i64 %n) {
; CHECK-LABEL: @memccpy_to_memcpy9(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(12) [[DST:%.*]], i8* nonnull align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @StopCharAfterNulTerminator, i64 0, i64 0), i64 12, i1 false)
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 12
; CHECK-NEXT: ret i8* [[TMP1]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @StopCharAfterNulTerminator, i64 0, i64 0), i32 120, i64 15) ; 120 is 'x'
ret i8* %call
}
define i8* @memccpy_to_memcpy10(i8* %dst, i64 %n) {
; CHECK-LABEL: @memccpy_to_memcpy10(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
; CHECK-NEXT: ret i8* [[TMP1]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 255, i64 15)
ret i8* %call
}
define i8* @memccpy_to_memcpy11(i8* %dst, i64 %n) {
; CHECK-LABEL: @memccpy_to_memcpy11(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
; CHECK-NEXT: ret i8* [[TMP1]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 -1, i64 15)
ret i8* %call
}
define i8* @memccpy_to_memcpy12(i8* %dst, i64 %n) {
; CHECK-LABEL: @memccpy_to_memcpy12(
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
; CHECK-NEXT: ret i8* [[TMP1]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 1023, i64 15)
ret i8* %call
}
define i8* @memccpy_to_null(i8* %dst, i8* %src, i32 %c) {
; CHECK-LABEL: @memccpy_to_null(
; CHECK-NEXT: ret i8* null
;
%call = call i8* @memccpy(i8* %dst, i8* %src, i32 %c, i64 0)
ret i8* %call
}
define void @memccpy_dst_src_same_retval_unused(i8* %dst, i32 %c, i64 %n) {
; CHECK-LABEL: @memccpy_dst_src_same_retval_unused(
; CHECK-NEXT: ret void
;
%call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)
ret void
}
; Negative tests
define i8* @unknown_src(i8* %dst, i8* %src) {
; CHECK-LABEL: @unknown_src(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[SRC:%.*]], i32 114, i64 12)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* %src, i32 114, i64 12)
ret i8* %call
}
define i8* @unknown_stop_char(i8* %dst, i32 %c) {
; CHECK-LABEL: @unknown_stop_char(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 [[C:%.*]], i64 12)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 %c, i64 12)
ret i8* %call
}
define i8* @unknown_size_n(i8* %dst, i64 %n) {
; CHECK-LABEL: @unknown_size_n(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 [[N:%.*]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 %n)
ret i8* %call
}
define i8* @no_nul_terminator(i8* %dst, i64 %n) {
; CHECK-LABEL: @no_nul_terminator(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([12 x i8], [12 x i8]* @StopCharAfterNulTerminator, i64 0, i64 0), i32 120, i64 [[N:%.*]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @StopCharAfterNulTerminator, i64 0, i64 0), i32 120, i64 %n) ; 120 is 'x'
ret i8* %call
}
define i8* @possibly_valid_data_after_array(i8* %dst, i64 %n) {
; CHECK-LABEL: @possibly_valid_data_after_array(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([10 x i8], [10 x i8]* @NoNulTerminator, i64 0, i64 0), i32 115, i64 [[N:%.*]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @NoNulTerminator, i64 0, i64 0), i32 115, i64 %n) ; 115 is 's'
ret i8* %call
}
define i8* @possibly_valid_data_after_array2(i8* %dst, i64 %n) {
; CHECK-LABEL: @possibly_valid_data_after_array2(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 [[N:%.*]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 %n) ; 115 is 's'
ret i8* %call
}
define i8* @possibly_valid_data_after_array3(i8* %dst) {
; CHECK-LABEL: @possibly_valid_data_after_array3(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 12)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 115, i64 12) ; 115 is 's'
ret i8* %call
}
define i8* @memccpy_dst_src_same_retval_used(i8* %dst, i32 %c, i64 %n) {
; CHECK-LABEL: @memccpy_dst_src_same_retval_used(
; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[DST]], i32 [[C:%.*]], i64 [[N:%.*]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)
ret i8* %call
}