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_memcpy4(i8* %dst) {
43 ; CHECK-LABEL: @memccpy_to_memcpy4(
44 ; 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)
45 ; CHECK-NEXT: ret void
47 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 0, i64 12)
51 define i8* @memccpy_to_memcpy5(i8* %dst) {
52 ; CHECK-LABEL: @memccpy_to_memcpy5(
53 ; 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)
54 ; CHECK-NEXT: ret i8* null
56 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 7)
60 define i8* @memccpy_to_memcpy6(i8* %dst) {
61 ; CHECK-LABEL: @memccpy_to_memcpy6(
62 ; 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)
63 ; CHECK-NEXT: ret i8* null
65 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 6);
69 define i8* @memccpy_to_memcpy7(i8* %dst) {
70 ; CHECK-LABEL: @memccpy_to_memcpy7(
71 ; 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)
72 ; CHECK-NEXT: ret i8* null
74 %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'
78 define i8* @memccpy_to_memcpy8(i8* %dst) {
79 ; CHECK-LABEL: @memccpy_to_memcpy8(
80 ; 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)
81 ; CHECK-NEXT: ret i8* null
83 %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'
87 define i8* @memccpy_to_memcpy9(i8* %dst, i64 %n) {
88 ; CHECK-LABEL: @memccpy_to_memcpy9(
89 ; 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)
90 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 12
91 ; CHECK-NEXT: ret i8* [[TMP1]]
93 %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'
97 define i8* @memccpy_to_memcpy10(i8* %dst, i64 %n) {
98 ; CHECK-LABEL: @memccpy_to_memcpy10(
99 ; 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)
100 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
101 ; CHECK-NEXT: ret i8* [[TMP1]]
103 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 255, i64 15)
107 define i8* @memccpy_to_memcpy11(i8* %dst, i64 %n) {
108 ; CHECK-LABEL: @memccpy_to_memcpy11(
109 ; 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)
110 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
111 ; CHECK-NEXT: ret i8* [[TMP1]]
113 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 -1, i64 15)
117 define i8* @memccpy_to_memcpy12(i8* %dst, i64 %n) {
118 ; CHECK-LABEL: @memccpy_to_memcpy12(
119 ; 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)
120 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[DST]], i64 11
121 ; CHECK-NEXT: ret i8* [[TMP1]]
123 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @StringWithEOF, i64 0, i64 0), i32 1023, i64 15)
127 define i8* @memccpy_to_null(i8* %dst, i8* %src, i32 %c) {
128 ; CHECK-LABEL: @memccpy_to_null(
129 ; CHECK-NEXT: ret i8* null
131 %call = call i8* @memccpy(i8* %dst, i8* %src, i32 %c, i64 0)
135 define void @memccpy_dst_src_same_retval_unused(i8* %dst, i32 %c, i64 %n) {
136 ; CHECK-LABEL: @memccpy_dst_src_same_retval_unused(
137 ; CHECK-NEXT: ret void
139 %call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)
144 define i8* @unknown_src(i8* %dst, i8* %src) {
145 ; CHECK-LABEL: @unknown_src(
146 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[SRC:%.*]], i32 114, i64 12)
147 ; CHECK-NEXT: ret i8* [[CALL]]
149 %call = call i8* @memccpy(i8* %dst, i8* %src, i32 114, i64 12)
153 define i8* @unknown_stop_char(i8* %dst, i32 %c) {
154 ; CHECK-LABEL: @unknown_stop_char(
155 ; 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)
156 ; CHECK-NEXT: ret i8* [[CALL]]
158 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 %c, i64 12)
162 define i8* @unknown_size_n(i8* %dst, i64 %n) {
163 ; CHECK-LABEL: @unknown_size_n(
164 ; 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:%.*]])
165 ; CHECK-NEXT: ret i8* [[CALL]]
167 %call = call i8* @memccpy(i8* %dst, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @hello, i64 0, i64 0), i32 114, i64 %n)
171 define i8* @no_nul_terminator(i8* %dst, i64 %n) {
172 ; CHECK-LABEL: @no_nul_terminator(
173 ; 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:%.*]])
174 ; CHECK-NEXT: ret i8* [[CALL]]
176 %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'
180 define i8* @possibly_valid_data_after_array(i8* %dst, i64 %n) {
181 ; CHECK-LABEL: @possibly_valid_data_after_array(
182 ; 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:%.*]])
183 ; CHECK-NEXT: ret i8* [[CALL]]
185 %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'
189 define i8* @possibly_valid_data_after_array2(i8* %dst, i64 %n) {
190 ; CHECK-LABEL: @possibly_valid_data_after_array2(
191 ; 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:%.*]])
192 ; CHECK-NEXT: ret i8* [[CALL]]
194 %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'
198 define i8* @possibly_valid_data_after_array3(i8* %dst) {
199 ; CHECK-LABEL: @possibly_valid_data_after_array3(
200 ; 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)
201 ; CHECK-NEXT: ret i8* [[CALL]]
203 %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'
207 define i8* @memccpy_dst_src_same_retval_used(i8* %dst, i32 %c, i64 %n) {
208 ; CHECK-LABEL: @memccpy_dst_src_same_retval_used(
209 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @memccpy(i8* [[DST:%.*]], i8* [[DST]], i32 [[C:%.*]], i64 [[N:%.*]])
210 ; CHECK-NEXT: ret i8* [[CALL]]
212 %call = call i8* @memccpy(i8* %dst, i8* %dst, i32 %c, i64 %n)