xref: /llvm-project/llvm/test/Transforms/InstCombine/stpcpy_chk-1.ll (revision 23a239267e8a1d20ed10d3545feaf2a2bb70b085)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test lib call simplification of __stpcpy_chk calls with various values
3; for src, dst, and slen.
4;
5; RUN: opt < %s -passes=instcombine -S | FileCheck %s
6
7target 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"
8
9@a = common global [60 x i8] zeroinitializer, align 1
10@b = common global [60 x i8] zeroinitializer, align 1
11@.str = private constant [12 x i8] c"abcdefghijk\00"
12
13; Check cases where slen >= strlen (src).
14
15define ptr @test_simplify1() {
16; CHECK-LABEL: @test_simplify1(
17; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(12) @a, ptr noundef nonnull align 1 dereferenceable(12) @.str, i32 12, i1 false)
18; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a, i32 11)
19;
20
21  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @.str, i32 60)
22  ret ptr %ret
23}
24
25define ptr @test_simplify2() {
26; CHECK-LABEL: @test_simplify2(
27; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(12) @a, ptr noundef nonnull align 1 dereferenceable(12) @.str, i32 12, i1 false)
28; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a, i32 11)
29;
30
31  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @.str, i32 12)
32  ret ptr %ret
33}
34
35define ptr @test_simplify3() {
36; CHECK-LABEL: @test_simplify3(
37; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(12) @a, ptr noundef nonnull align 1 dereferenceable(12) @.str, i32 12, i1 false)
38; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a, i32 11)
39;
40
41  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @.str, i32 -1)
42  ret ptr %ret
43}
44
45define ptr @test_simplify1_tail() {
46; CHECK-LABEL: @test_simplify1_tail(
47; CHECK-NEXT:    tail call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(12) @a, ptr noundef nonnull align 1 dereferenceable(12) @.str, i32 12, i1 false)
48; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a, i32 11)
49;
50
51  %ret = tail call ptr @__stpcpy_chk(ptr @a, ptr @.str, i32 60)
52  ret ptr %ret
53}
54
55; Check cases where there are no string constants.
56
57define ptr @test_simplify4() {
58; CHECK-LABEL: @test_simplify4(
59; CHECK-NEXT:    [[STPCPY:%.*]] = call ptr @stpcpy(ptr nonnull @a, ptr nonnull @b)
60; CHECK-NEXT:    ret ptr [[STPCPY]]
61;
62
63  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @b, i32 -1)
64  ret ptr %ret
65}
66
67define ptr @test_simplify4_tail() {
68; CHECK-LABEL: @test_simplify4_tail(
69; CHECK-NEXT:    [[STPCPY:%.*]] = tail call ptr @stpcpy(ptr nonnull @a, ptr nonnull @b)
70; CHECK-NEXT:    ret ptr [[STPCPY]]
71;
72
73  %ret = tail call ptr @__stpcpy_chk(ptr @a, ptr @b, i32 -1)
74  ret ptr %ret
75}
76
77; Check case where the string length is not constant.
78
79define ptr @test_simplify5() {
80; CHECK-LABEL: @test_simplify5(
81; CHECK-NEXT:    [[LEN:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
82; CHECK-NEXT:    [[TMP1:%.*]] = call ptr @__memcpy_chk(ptr nonnull @a, ptr nonnull @.str, i32 12, i32 [[LEN]])
83; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @a, i32 11)
84;
85
86  %len = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
87  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @.str, i32 %len)
88  ret ptr %ret
89}
90
91; Check case where the source and destination are the same.
92
93define ptr @test_simplify6() {
94; CHECK-LABEL: @test_simplify6(
95; CHECK-NEXT:    [[STRLEN:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) @a)
96; CHECK-NEXT:    [[RET:%.*]] = getelementptr inbounds i8, ptr @a, i32 [[STRLEN]]
97; CHECK-NEXT:    ret ptr [[RET]]
98;
99
100  %len = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
101  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @a, i32 %len)
102  ret ptr %ret
103}
104
105; Check cases where there are no string constants, and is a tail call.
106
107define ptr @test_simplify7() {
108; CHECK-LABEL: @test_simplify7(
109; CHECK-NEXT:    [[STPCPY:%.*]] = tail call ptr @stpcpy(ptr nonnull @a, ptr nonnull @b)
110; CHECK-NEXT:    ret ptr [[STPCPY]]
111;
112
113  %ret = tail call ptr @__stpcpy_chk(ptr @a, ptr @b, i32 -1)
114  ret ptr %ret
115}
116
117; Check case where slen < strlen (src).
118
119define ptr @test_no_simplify1() {
120; CHECK-LABEL: @test_no_simplify1(
121; CHECK-NEXT:    [[RET:%.*]] = call ptr @__stpcpy_chk(ptr nonnull @a, ptr nonnull @b, i32 8)
122; CHECK-NEXT:    ret ptr [[RET]]
123;
124
125  %ret = call ptr @__stpcpy_chk(ptr @a, ptr @b, i32 8)
126  ret ptr %ret
127}
128
129declare ptr @__stpcpy_chk(ptr, ptr, i32) nounwind
130declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1) nounwind readonly
131