1; Test lib call simplification of __strcpy_chk calls with various values
2; for src, dst, and slen.
3;
4; RUN: opt < %s -instcombine -S | FileCheck %s
5
6target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
7
8@a = common global [60 x i8] zeroinitializer, align 1
9@b = common global [60 x i8] zeroinitializer, align 1
10@.str = private constant [12 x i8] c"abcdefghijk\00"
11
12; Check cases where slen >= strlen (src).
13
14define void @test_simplify1() {
15; CHECK-LABEL: @test_simplify1(
16  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
17  %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
18
19; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
20  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 60)
21  ret void
22}
23
24define void @test_simplify2() {
25; CHECK-LABEL: @test_simplify2(
26  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
27  %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
28
29; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
30  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 12)
31  ret void
32}
33
34define void @test_simplify3() {
35; CHECK-LABEL: @test_simplify3(
36  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
37  %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
38
39; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
40  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 -1)
41  ret void
42}
43
44; Check cases where there are no string constants.
45
46define void @test_simplify4() {
47; CHECK-LABEL: @test_simplify4(
48  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
49  %src = getelementptr inbounds [60 x i8]* @b, i32 0, i32 0
50
51; CHECK-NEXT: call i8* @strcpy
52  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 -1)
53  ret void
54}
55
56; Check case where the string length is not constant.
57
58define void @test_simplify5() {
59; CHECK-LABEL: @test_simplify5(
60  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
61  %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
62
63; CHECK: @__memcpy_chk
64  %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
65  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 %len)
66  ret void
67}
68
69; Check case where the source and destination are the same.
70
71define i8* @test_simplify6() {
72; CHECK-LABEL: @test_simplify6(
73  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
74
75; CHECK: getelementptr inbounds ([60 x i8]* @a, i32 0, i32 0)
76  %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
77  %ret = call i8* @__strcpy_chk(i8* %dst, i8* %dst, i32 %len)
78  ret i8* %ret
79}
80
81; Check case where slen < strlen (src).
82
83define void @test_no_simplify1() {
84; CHECK-LABEL: @test_no_simplify1(
85  %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
86  %src = getelementptr inbounds [60 x i8]* @b, i32 0, i32 0
87
88; CHECK-NEXT: call i8* @__strcpy_chk
89  call i8* @__strcpy_chk(i8* %dst, i8* %src, i32 8)
90  ret void
91}
92
93declare i8* @__strcpy_chk(i8*, i8*, i32) nounwind
94declare i32 @llvm.objectsize.i32.p0i8(i8*, i1) nounwind readonly
95