blob: 8a02529c61ca7be6f2bdaafd76c4720e9129ffa5 [file] [log] [blame]
Meador Ingecdb2ca52012-10-31 00:20:51 +00001; Test lib call simplification of __stpcpy_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() {
Stephen Linc1c7a132013-07-14 01:42:54 +000015; CHECK-LABEL: @test_simplify1(
Meador Ingecdb2ca52012-10-31 00:20:51 +000016 %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
17 %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
18
Meador Inge9a6a1902012-10-31 00:20:56 +000019; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
Meador Ingecdb2ca52012-10-31 00:20:51 +000020 call i8* @__stpcpy_chk(i8* %dst, i8* %src, i32 60)
21 ret void
22}
23
24define void @test_simplify2() {
Stephen Linc1c7a132013-07-14 01:42:54 +000025; CHECK-LABEL: @test_simplify2(
Meador Ingecdb2ca52012-10-31 00:20:51 +000026 %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
27 %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
28
Meador Inge9a6a1902012-10-31 00:20:56 +000029; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
Meador Ingecdb2ca52012-10-31 00:20:51 +000030 call i8* @__stpcpy_chk(i8* %dst, i8* %src, i32 12)
31 ret void
32}
33
34define void @test_simplify3() {
Stephen Linc1c7a132013-07-14 01:42:54 +000035; CHECK-LABEL: @test_simplify3(
Meador Ingecdb2ca52012-10-31 00:20:51 +000036 %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
37 %src = getelementptr inbounds [12 x i8]* @.str, i32 0, i32 0
38
Meador Inge9a6a1902012-10-31 00:20:56 +000039; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32
Meador Ingecdb2ca52012-10-31 00:20:51 +000040 call i8* @__stpcpy_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() {
Stephen Linc1c7a132013-07-14 01:42:54 +000047; CHECK-LABEL: @test_simplify4(
Meador Ingecdb2ca52012-10-31 00:20:51 +000048 %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* @stpcpy
52 call i8* @__stpcpy_chk(i8* %dst, i8* %src, i32 -1)
53 ret void
54}
55
56; Check case where the string length is not constant.
57
58define i8* @test_simplify5() {
Stephen Linc1c7a132013-07-14 01:42:54 +000059; CHECK-LABEL: @test_simplify5(
Meador Ingecdb2ca52012-10-31 00:20:51 +000060 %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
Matt Arsenaultfbcbce42013-10-07 18:06:48 +000064 %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
Meador Ingecdb2ca52012-10-31 00:20:51 +000065 %ret = call i8* @__stpcpy_chk(i8* %dst, i8* %src, i32 %len)
66; CHECK: ret i8* getelementptr inbounds ([60 x i8]* @a, i32 0, i32 11)
67 ret i8* %ret
68}
69
70; Check case where the source and destination are the same.
71
72define i8* @test_simplify6() {
Stephen Linc1c7a132013-07-14 01:42:54 +000073; CHECK-LABEL: @test_simplify6(
Meador Ingecdb2ca52012-10-31 00:20:51 +000074 %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
75
76; CHECK: [[LEN:%[a-z]+]] = call i32 @strlen
77; CHECK-NEXT: getelementptr inbounds [60 x i8]* @a, i32 0, i32 [[LEN]]
Matt Arsenaultfbcbce42013-10-07 18:06:48 +000078 %len = call i32 @llvm.objectsize.i32.p0i8(i8* %dst, i1 false)
Meador Ingecdb2ca52012-10-31 00:20:51 +000079 %ret = call i8* @__stpcpy_chk(i8* %dst, i8* %dst, i32 %len)
80 ret i8* %ret
81}
82
83; Check case where slen < strlen (src).
84
85define void @test_no_simplify1() {
Stephen Linc1c7a132013-07-14 01:42:54 +000086; CHECK-LABEL: @test_no_simplify1(
Meador Ingecdb2ca52012-10-31 00:20:51 +000087 %dst = getelementptr inbounds [60 x i8]* @a, i32 0, i32 0
88 %src = getelementptr inbounds [60 x i8]* @b, i32 0, i32 0
89
90; CHECK-NEXT: call i8* @__stpcpy_chk
91 call i8* @__stpcpy_chk(i8* %dst, i8* %src, i32 8)
92 ret void
93}
94
95declare i8* @__stpcpy_chk(i8*, i8*, i32) nounwind
Matt Arsenaultfbcbce42013-10-07 18:06:48 +000096declare i32 @llvm.objectsize.i32.p0i8(i8*, i1) nounwind readonly