1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 @hello = private constant [11 x i8] c"helloworld\00", align 1
5 @NoNulTerminator = private constant [10 x i8] c"helloworld", align 1
6 @StopCharAfterNulTerminator = private constant [12 x i8] c"helloworld\00x", align 1
7 @StringWithEOF = constant [14 x i8] c"helloworld\FFab\00", align 1
9 declare i8* @memccpy(i8*, i8*, i32, i64)
11 define i8* @memccpy_to_memcpy(i8* %dst) {
12 ; CHECK-LABEL: @memccpy_to_memcpy(
13 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i64*
14 ; CHECK-NEXT: store i64 8245940763182785896, i64* [[TMP1]], align 1
15 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 8
16 ; CHECK-NEXT: ret i8* [[TMP2]]
18 %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'
22 define i8* @memccpy_to_memcpy2(i8* %dst) {
23 ; CHECK-LABEL: @memccpy_to_memcpy2(
24 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i64*
25 ; CHECK-NEXT: store i64 8245940763182785896, i64* [[TMP1]], align 1
26 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 8
27 ; CHECK-NEXT: ret i8* [[TMP2]]
29 %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'
33 define void @memccpy_to_memcpy3(i8* %dst) {
34 ; CHECK-LABEL: @memccpy_to_memcpy3(
35 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(5) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 5, i1 false)
36 ; CHECK-NEXT: ret void
38 %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'
42 define void @memccpy_to_memcpy3_tail(i8* %dst) {
43 ; CHECK-LABEL: @memccpy_to_memcpy3_tail(
44 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(5) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 5, i1 false)
45 ; CHECK-NEXT: ret void
47 %call = tail 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'
51 define i8* @memccpy_to_memcpy3_musttail(i8* %dst, i8* %x, i32 %y, i64 %z) {
52 ; CHECK-LABEL: @memccpy_to_memcpy3_musttail(
53 ; CHECK-NEXT: %call = musttail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 111, i64 10)
54 ; CHECK-NEXT: ret i8* %call
56 %call = musttail 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'
61 define void @memccpy_to_memcpy4(i8* %dst) {
62 ; CHECK-LABEL: @memccpy_to_memcpy4(
63 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 11, i1 false)
64 ; CHECK-NEXT: ret void
66 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 0, i64 12)
70 define i8* @memccpy_to_memcpy5(i8* %dst) {
71 ; CHECK-LABEL: @memccpy_to_memcpy5(
72 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(7) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(7) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 7, i1 false)
73 ; CHECK-NEXT: ret i8* null
75 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
79 define i8* @memccpy_to_memcpy5_tail(i8* %dst) {
80 ; CHECK-LABEL: @memccpy_to_memcpy5_tail(
81 ; CHECK-NEXT: tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(7) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(7) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 7, i1 false)
82 ; CHECK-NEXT: ret i8* null
84 %call = tail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
88 define i8* @memccpy_to_memcpy5_musttail(i8* %dst, i8* %x, i32 %y, i64 %z) {
89 ; CHECK-LABEL: @memccpy_to_memcpy5_musttail(
90 ; CHECK-NEXT: %call = musttail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
91 ; CHECK-NEXT: ret i8* %call
93 %call = musttail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
97 define i8* @memccpy_to_memcpy6(i8* %dst) {
98 ; CHECK-LABEL: @memccpy_to_memcpy6(
99 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(6) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(6) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 6, i1 false)
100 ; CHECK-NEXT: ret i8* null
102 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 6);
106 define i8* @memccpy_to_memcpy7(i8* %dst) {
107 ; CHECK-LABEL: @memccpy_to_memcpy7(
108 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(5) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 5, i1 false)
109 ; CHECK-NEXT: ret i8* null
111 %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'
115 define i8* @memccpy_to_memcpy8(i8* %dst) {
116 ; CHECK-LABEL: @memccpy_to_memcpy8(
117 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i64 11, i1 false)
118 ; CHECK-NEXT: ret i8* null
120 %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'
124 define i8* @memccpy_to_memcpy9(i8* %dst, i64 %n) {
125 ; CHECK-LABEL: @memccpy_to_memcpy9(
126 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(12) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @StopCharAfterNulTerminator, i64 0, i64 0), i64 12, i1 false)
127 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 12
128 ; CHECK-NEXT: ret i8* [[TMP1]]
130 %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'
134 define i8* @memccpy_to_memcpy10(i8* %dst, i64 %n) {
135 ; CHECK-LABEL: @memccpy_to_memcpy10(
136 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
137 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
138 ; CHECK-NEXT: ret i8* [[TMP1]]
140 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 255, i64 15)
144 define i8* @memccpy_to_memcpy11(i8* %dst, i64 %n) {
145 ; CHECK-LABEL: @memccpy_to_memcpy11(
146 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
147 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
148 ; CHECK-NEXT: ret i8* [[TMP1]]
150 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 -1, i64 15)
154 define i8* @memccpy_to_memcpy12(i8* %dst, i64 %n) {
155 ; CHECK-LABEL: @memccpy_to_memcpy12(
156 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* noundef nonnull align 1 dereferenceable(11) [[DST:%.*]], i8* noundef nonnull align 1 dereferenceable(11) getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i64 11, i1 false)
157 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
158 ; CHECK-NEXT: ret i8* [[TMP1]]
160 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 1023, i64 15)
164 define i8* @memccpy_to_null(i8* %dst, i8* %src, i32 %c) {
165 ; CHECK-LABEL: @memccpy_to_null(
166 ; CHECK-NEXT: ret i8* null
168 %call = call i8* @memccpy(i8* %dst, i8* %src, i32 %c, i64 0)
172 define void @memccpy_dst_src_same_retval_unused(i8* %dst, i32 %c, i64 %n) {
173 ; CHECK-LABEL: @memccpy_dst_src_same_retval_unused(
174 ; CHECK-NEXT: ret void
176 %call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)
181 define i8* @unknown_src(i8* %dst, i8* %src) {
182 ; CHECK-LABEL: @unknown_src(
183 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[SRC:%.*]], i32 114, i64 12)
184 ; CHECK-NEXT: ret i8* [[CALL]]
186 %call = call i8* @memccpy(i8* %dst, i8* %src, i32 114, i64 12)
190 define i8* @unknown_stop_char(i8* %dst, i32 %c) {
191 ; CHECK-LABEL: @unknown_stop_char(
192 ; 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)
193 ; CHECK-NEXT: ret i8* [[CALL]]
195 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 %c, i64 12)
199 define i8* @unknown_size_n(i8* %dst, i64 %n) {
200 ; CHECK-LABEL: @unknown_size_n(
201 ; 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:%.*]])
202 ; CHECK-NEXT: ret i8* [[CALL]]
204 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 %n)
208 define i8* @no_nul_terminator(i8* %dst, i64 %n) {
209 ; CHECK-LABEL: @no_nul_terminator(
210 ; 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:%.*]])
211 ; CHECK-NEXT: ret i8* [[CALL]]
213 %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'
217 define i8* @possibly_valid_data_after_array(i8* %dst, i64 %n) {
218 ; CHECK-LABEL: @possibly_valid_data_after_array(
219 ; 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:%.*]])
220 ; CHECK-NEXT: ret i8* [[CALL]]
222 %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'
226 define i8* @possibly_valid_data_after_array2(i8* %dst, i64 %n) {
227 ; CHECK-LABEL: @possibly_valid_data_after_array2(
228 ; 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:%.*]])
229 ; CHECK-NEXT: ret i8* [[CALL]]
231 %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'
235 define i8* @possibly_valid_data_after_array3(i8* %dst) {
236 ; CHECK-LABEL: @possibly_valid_data_after_array3(
237 ; 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)
238 ; CHECK-NEXT: ret i8* [[CALL]]
240 %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'
244 define i8* @memccpy_dst_src_same_retval_used(i8* %dst, i32 %c, i64 %n) {
245 ; CHECK-LABEL: @memccpy_dst_src_same_retval_used(
246 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[DST]], i32 [[C:%.*]], i64 [[N:%.*]])
247 ; CHECK-NEXT: ret i8* [[CALL]]
249 %call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)
253 define i8* @memccpy_to_memcpy_musttail(i8* %dst, i8* %x, i32 %y, i64 %z) {
254 ; CHECK-LABEL: @memccpy_to_memcpy_musttail(
255 ; CHECK-NEXT: %call = musttail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 12)
256 ; CHECK-NEXT: ret i8* %call
258 %call = musttail 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'
262 define i8* @memccpy_to_memcpy2_musttail(i8* %dst, i8* %x, i32 %y, i64 %z) {
263 ; CHECK-LABEL: @memccpy_to_memcpy2_musttail(
264 ; CHECK-NEXT: %call = musttail call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 8)
265 ; CHECK-NEXT: ret i8* %call
267 %call = musttail 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'