xref: /llvm-project/llvm/test/Transforms/InstCombine/strcpy_chk-1.ll (revision 2caaec65c04ea7d0e9568b7895b7a46d6100cb75)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test lib call simplification of __strcpy_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 @a
19;
20
21  %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 60)
22  ret ptr %ret
23}
24
25define ptr @test_simplify1_tail() {
26; CHECK-LABEL: @test_simplify1_tail(
27; 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)
28; CHECK-NEXT:    ret ptr @a
29;
30
31  %ret = tail call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 60)
32  ret ptr %ret
33}
34
35define ptr @test_simplify2() {
36; CHECK-LABEL: @test_simplify2(
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 @a
39;
40
41  %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 12)
42  ret ptr %ret
43}
44
45define ptr @test_simplify3() {
46; CHECK-LABEL: @test_simplify3(
47; 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)
48; CHECK-NEXT:    ret ptr @a
49;
50
51  %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 -1)
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:    [[STRCPY:%.*]] = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @a, ptr noundef nonnull dereferenceable(1) @b)
60; CHECK-NEXT:    ret ptr @a
61;
62
63  %ret = call ptr @__strcpy_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:    [[STRCPY:%.*]] = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @a, ptr noundef nonnull dereferenceable(1) @b)
70; CHECK-NEXT:    ret ptr @a
71;
72
73  %ret = tail call ptr @__strcpy_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:    [[RET:%.*]] = call ptr @__memcpy_chk(ptr nonnull @a, ptr nonnull @.str, i32 12, i32 [[LEN]])
83; CHECK-NEXT:    ret ptr [[RET]]
84;
85
86  %len = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
87  %ret = call ptr @__strcpy_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:    [[LEN:%.*]] = call i32 @llvm.objectsize.i32.p0(ptr @a, i1 false, i1 false, i1 false)
96; CHECK-NEXT:    [[RET:%.*]] = call ptr @__strcpy_chk(ptr nonnull @a, ptr nonnull @a, i32 [[LEN]])
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 @__strcpy_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:    [[STRCPY:%.*]] = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @a, ptr noundef nonnull dereferenceable(1) @b)
110; CHECK-NEXT:    ret ptr @a
111;
112
113  %ret = tail call ptr @__strcpy_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 @__strcpy_chk(ptr nonnull @a, ptr nonnull @b, i32 8)
122; CHECK-NEXT:    ret ptr [[RET]]
123;
124
125  %ret = call ptr @__strcpy_chk(ptr @a, ptr @b, i32 8)
126  ret ptr %ret
127}
128
129define ptr @test_no_simplify2(ptr %dst, ptr %src, i32 %a) {
130; CHECK-LABEL: @test_no_simplify2(
131; CHECK-NEXT:    [[RET:%.*]] = musttail call ptr @__strcpy_chk(ptr [[DST:%.*]], ptr [[SRC:%.*]], i32 60)
132; CHECK-NEXT:    ret ptr [[RET]]
133;
134  %ret = musttail call ptr @__strcpy_chk(ptr %dst, ptr %src, i32 60)
135  ret ptr %ret
136}
137
138declare ptr @__strcpy_chk(ptr, ptr, i32) nounwind
139declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1) nounwind readonly
140