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.
5 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
7 target 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"
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"
13 ; Check cases where slen >= strlen (src).
15 define 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
21 %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 60)
25 define 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
31 %ret = tail call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 60)
35 define 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
41 %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 12)
45 define 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
51 %ret = call ptr @__strcpy_chk(ptr @a, ptr @.str, i32 -1)
55 ; Check cases where there are no string constants.
57 define 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
63 %ret = call ptr @__strcpy_chk(ptr @a, ptr @b, i32 -1)
67 define 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
73 %ret = tail call ptr @__strcpy_chk(ptr @a, ptr @b, i32 -1)
77 ; Check case where the string length is not constant.
79 define 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]]
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)
91 ; Check case where the source and destination are the same.
93 define 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]]
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)
105 ; Check cases where there are no string constants, and is a tail call.
107 define 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
113 %ret = tail call ptr @__strcpy_chk(ptr @a, ptr @b, i32 -1)
117 ; Check case where slen < strlen (src).
119 define 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]]
125 %ret = call ptr @__strcpy_chk(ptr @a, ptr @b, i32 8)
129 define 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]]
134 %ret = musttail call ptr @__strcpy_chk(ptr %dst, ptr %src, i32 60)
138 declare ptr @__strcpy_chk(ptr, ptr, i32) nounwind
139 declare i32 @llvm.objectsize.i32.p0(ptr, i1, i1, i1) nounwind readonly