xref: /llvm-project/llvm/test/Transforms/InstCombine/strncpy-1.ll (revision 5c48f6fa543e7d081354d3f6c1c2524eb8075223)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
2; Test that the strncpy library call simplifier works correctly.
3;
4; RUN: opt < %s -passes=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@hello = constant [6 x i8] c"hello\00"
9@null = constant [1 x i8] zeroinitializer
10@null_hello = constant [7 x i8] c"\00hello\00"
11@a = common global [32 x i8] zeroinitializer, align 1
12@b = common global [32 x i8] zeroinitializer, align 1
13
14declare ptr @strncpy(ptr, ptr, i32)
15declare i32 @puts(ptr)
16
17; Check a bunch of strncpy invocations together.
18
19;.
20; CHECK: @hello = constant [6 x i8] c"hello\00"
21; CHECK: @null = constant [1 x i8] zeroinitializer
22; CHECK: @null_hello = constant [7 x i8] c"\00hello\00"
23; CHECK: @a = common global [32 x i8] zeroinitializer, align 1
24; CHECK: @b = common global [32 x i8] zeroinitializer, align 1
25; CHECK: @str = private unnamed_addr constant [32 x i8] c"hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", align 1
26; CHECK: @str.1 = private unnamed_addr constant [32 x i8] c"hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", align 1
27; CHECK: @str.2 = private unnamed_addr constant [8 x i8] c"hello\00\00\00", align 1
28;.
29define i32 @test_simplify1() {
30; CHECK-LABEL: @test_simplify1(
31; CHECK-NEXT:    [[TARGET:%.*]] = alloca [1024 x i8], align 1
32; CHECK-NEXT:    store i8 0, ptr [[TARGET]], align 1
33; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(6) [[TARGET]], ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 6, i1 false)
34; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr noundef nonnull align 1 dereferenceable(42) [[TARGET]], i8 0, i32 42, i1 false)
35; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr noundef nonnull align 1 dereferenceable(42) [[TARGET]], i8 0, i32 42, i1 false)
36; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @puts(ptr noundef nonnull dereferenceable(1) [[TARGET]])
37; CHECK-NEXT:    ret i32 0
38;
39  %target = alloca [1024 x i8]
40  store i8 0, ptr %target
41
42  %rslt1 = call ptr @strncpy(ptr %target, ptr @hello, i32 6)
43
44  %rslt2 = call ptr @strncpy(ptr %rslt1, ptr @null, i32 42)
45
46  %rslt3 = call ptr @strncpy(ptr %rslt2, ptr @null_hello, i32 42)
47
48  call i32 @puts( ptr %rslt3 )
49  ret i32 0
50}
51
52; Check strncpy(x, "", y) -> memset(x, '\0', y, 1).
53
54define void @test_simplify2() {
55; CHECK-LABEL: @test_simplify2(
56; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr noundef nonnull align 1 dereferenceable(32) @a, i8 0, i32 32, i1 false)
57; CHECK-NEXT:    ret void
58;
59
60  call ptr @strncpy(ptr @a, ptr @null, i32 32)
61  ret void
62}
63
64; Check strncpy(x, y, 0) -> x.
65
66define ptr @test_simplify3() {
67; CHECK-LABEL: @test_simplify3(
68; CHECK-NEXT:    ret ptr @a
69;
70
71  %ret = call ptr @strncpy(ptr @a, ptr @hello, i32 0)
72  ret ptr %ret
73}
74
75; Check  strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant].
76
77define void @test_simplify4() {
78; CHECK-LABEL: @test_simplify4(
79; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(6) @a, ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 6, i1 false)
80; CHECK-NEXT:    ret void
81;
82
83  call ptr @strncpy(ptr @a, ptr @hello, i32 6)
84  ret void
85}
86
87define void @test_simplify5(ptr %dst) {
88; CHECK-LABEL: @test_simplify5(
89; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(32) [[DST:%.*]], ptr noundef nonnull align 1 dereferenceable(32) @str, i32 32, i1 false)
90; CHECK-NEXT:    ret void
91;
92  call ptr @strncpy(ptr dereferenceable(8) %dst, ptr @hello, i32 32)
93  ret void
94}
95
96define void @test_simplify6(ptr %dst) {
97; CHECK-LABEL: @test_simplify6(
98; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(80) [[DST:%.*]], ptr noundef nonnull align 1 dereferenceable(32) @str.1, i32 32, i1 false)
99; CHECK-NEXT:    ret void
100;
101  call ptr @strncpy(ptr dereferenceable(80) %dst, ptr @hello, i32 32)
102  ret void
103}
104
105define void @test_simplify7(ptr %dst, i32 %n) {
106; CHECK-LABEL: @test_simplify7(
107; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr align 1 dereferenceable(80) [[DST:%.*]], i8 0, i32 [[N:%.*]], i1 false)
108; CHECK-NEXT:    ret void
109;
110; Unless N is known to be nonzero, strncpy(D, "", N) need not access any
111; bytes in D.
112; TODO: The argument, already annotated dereferenceable, should be
113; annotated noundef and nonnull by the transformation.  See
114; https://reviews.llvm.org/D124633.
115  call ptr @strncpy(ptr dereferenceable(80) %dst, ptr @null, i32 %n)
116  ret void
117}
118
119define ptr @test1(ptr %dst, ptr %src, i32 %n) {
120; CHECK-LABEL: @test1(
121; CHECK-NEXT:    [[RET:%.*]] = call ptr @strncpy(ptr nonnull [[DST:%.*]], ptr nonnull [[SRC:%.*]], i32 [[N:%.*]])
122; CHECK-NEXT:    ret ptr [[RET]]
123;
124; Unless N is known to be nonzero, strncpy(D, S, N) need not access any
125; bytes in either D or S.  Verify that the call isn't annotated with
126; the dereferenceable attribute.
127; TODO: Both arguments should be annotated noundef in addition to nonnull.
128; See https://reviews.llvm.org/D124633.
129  %ret = call ptr @strncpy(ptr nonnull %dst, ptr nonnull %src, i32 %n)
130  ret ptr %ret
131}
132
133define ptr @test2(ptr %dst) {
134; CHECK-LABEL: @test2(
135; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(5) [[DST:%.*]], ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 5, i1 false)
136; CHECK-NEXT:    ret ptr [[DST]]
137;
138  %ret = call ptr @strncpy(ptr nonnull %dst, ptr nonnull @hello, i32 5)
139  ret ptr %ret
140}
141
142define ptr @test3(ptr %dst, i32 %n) {
143; CHECK-LABEL: @test3(
144; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr noalias noundef nonnull align 1 dereferenceable(5) [[DST:%.*]], i8 0, i32 5, i1 false)
145; CHECK-NEXT:    ret ptr [[DST]]
146;
147  %ret = call ptr @strncpy(ptr noalias nonnull %dst, ptr nonnull @null, i32 5);
148  ret ptr %ret
149}
150
151define ptr @test4(ptr %dst, i32 %n) {
152; CHECK-LABEL: @test4(
153; CHECK-NEXT:    call void @llvm.memset.p0.i32(ptr noalias noundef nonnull align 16 dereferenceable(5) [[DST:%.*]], i8 0, i32 5, i1 false)
154; CHECK-NEXT:    ret ptr [[DST]]
155;
156  %ret = call ptr @strncpy(ptr align(16) noalias nonnull %dst, ptr nonnull @null, i32 5);
157  ret ptr %ret
158}
159
160; Check cases that shouldn't be simplified.
161
162define void @test_no_simplify1() {
163; CHECK-LABEL: @test_no_simplify1(
164; CHECK-NEXT:    [[TMP1:%.*]] = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) @a, ptr noundef nonnull dereferenceable(1) @b, i32 32)
165; CHECK-NEXT:    ret void
166;
167
168  call ptr @strncpy(ptr @a, ptr @b, i32 32)
169  ret void
170}
171
172define void @test_no_simplify2() {
173; CHECK-LABEL: @test_no_simplify2(
174; CHECK-NEXT:    store i64 478560413032, ptr @a, align 1
175; CHECK-NEXT:    ret void
176;
177
178  call ptr @strncpy(ptr @a, ptr @hello, i32 8)
179  ret void
180}
181
182define ptr @test_no_simplify3(ptr %dst, ptr %src, i32 %count) {
183; CHECK-LABEL: @test_no_simplify3(
184; CHECK-NEXT:    [[RET:%.*]] = musttail call ptr @strncpy(ptr [[DST:%.*]], ptr [[SRC:%.*]], i32 32)
185; CHECK-NEXT:    ret ptr [[RET]]
186;
187  %ret = musttail call ptr @strncpy(ptr %dst, ptr %src, i32 32)
188  ret ptr %ret
189}
190
191define ptr @test_no_simplify4(ptr %dst, ptr %src, i32 %count) {
192; CHECK-LABEL: @test_no_simplify4(
193; CHECK-NEXT:    [[RET:%.*]] = musttail call ptr @strncpy(ptr [[DST:%.*]], ptr [[SRC:%.*]], i32 6)
194; CHECK-NEXT:    ret ptr [[RET]]
195;
196  %ret = musttail call ptr @strncpy(ptr %dst, ptr %src, i32 6)
197  ret ptr %ret
198}
199
200
201define void @test_no_incompatible_attr() {
202; CHECK-LABEL: @test_no_incompatible_attr(
203; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(6) @a, ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 6, i1 false)
204; CHECK-NEXT:    ret void
205;
206
207  call ptr @strncpy(ptr @a, ptr @hello, i32 6)
208  ret void
209}
210;.
211; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
212; CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: write) }
213;.
214