1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strncpy library call simplifier works correctly.
4 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 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"
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
14 declare i8* @strncpy(i8*, i8*, i32)
15 declare i32 @puts(i8*)
17 ; Check a bunch of strncpy invocations together.
19 define i32 @test_simplify1() {
20 ; CHECK-LABEL: @test_simplify1(
21 ; CHECK-NEXT: [[TARGET:%.*]] = alloca [1024 x i8], align 1
22 ; CHECK-NEXT: [[ARG1:%.*]] = getelementptr inbounds [1024 x i8], [1024 x i8]* [[TARGET]], i32 0, i32 0
23 ; CHECK-NEXT: store i8 0, i8* [[ARG1]], align 1
24 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(6) [[ARG1]], i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 6, i1 false)
25 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(42) [[ARG1]], i8 0, i32 42, i1 false)
26 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(42) [[ARG1]], i8 0, i32 42, i1 false)
27 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @puts(i8* nonnull [[ARG1]])
28 ; CHECK-NEXT: ret i32 0
30 %target = alloca [1024 x i8]
31 %arg1 = getelementptr [1024 x i8], [1024 x i8]* %target, i32 0, i32 0
34 %arg2 = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
35 %rslt1 = call i8* @strncpy(i8* %arg1, i8* %arg2, i32 6)
37 %arg3 = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
38 %rslt2 = call i8* @strncpy(i8* %rslt1, i8* %arg3, i32 42)
40 %arg4 = getelementptr [7 x i8], [7 x i8]* @null_hello, i32 0, i32 0
41 %rslt3 = call i8* @strncpy(i8* %rslt2, i8* %arg4, i32 42)
43 call i32 @puts( i8* %rslt3 )
47 ; Check strncpy(x, "", y) -> memset(x, '\0', y, 1).
49 define void @test_simplify2() {
50 ; CHECK-LABEL: @test_simplify2(
51 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(32) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), i8 0, i32 32, i1 false)
52 ; CHECK-NEXT: ret void
54 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
55 %src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
57 call i8* @strncpy(i8* %dst, i8* %src, i32 32)
61 ; Check strncpy(x, y, 0) -> x.
63 define i8* @test_simplify3() {
64 ; CHECK-LABEL: @test_simplify3(
65 ; CHECK-NEXT: ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0)
67 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
68 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
70 %ret = call i8* @strncpy(i8* %dst, i8* %src, i32 0)
74 ; Check strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant].
76 define void @test_simplify4() {
77 ; CHECK-LABEL: @test_simplify4(
78 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 6, i1 false)
79 ; CHECK-NEXT: ret void
81 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
82 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
84 call i8* @strncpy(i8* %dst, i8* %src, i32 6)
88 define void @test_simplify5(i8* %dst) {
89 ; CHECK-LABEL: @test_simplify5(
90 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncpy(i8* nonnull dereferenceable(8) [[DST:%.*]], i8* nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 32)
91 ; CHECK-NEXT: ret void
93 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
94 call i8* @strncpy(i8* dereferenceable(8) %dst, i8* %src, i32 32)
98 define void @test_simplify6(i8* %dst) {
99 ; CHECK-LABEL: @test_simplify6(
100 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncpy(i8* nonnull dereferenceable(80) [[DST:%.*]], i8* nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 32)
101 ; CHECK-NEXT: ret void
103 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
104 call i8* @strncpy(i8* dereferenceable(80) %dst, i8* %src, i32 32)
108 define void @test_simplify7(i8* %dst, i32 %n) {
109 ; CHECK-LABEL: @test_simplify7(
110 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncpy(i8* nonnull dereferenceable(80) [[DST:%.*]], i8* getelementptr inbounds ([1 x i8], [1 x i8]* @null, i32 0, i32 0), i32 [[N:%.*]])
111 ; CHECK-NEXT: ret void
113 %src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
114 call i8* @strncpy(i8* dereferenceable(80) %dst, i8* %src, i32 %n)
118 define i8* @test1(i8* %dst, i8* %src, i32 %n) {
119 ; CHECK-LABEL: @test1(
120 ; CHECK-NEXT: [[RET:%.*]] = call i8* @strncpy(i8* nonnull [[DST:%.*]], i8* nonnull [[SRC:%.*]], i32 [[N:%.*]])
121 ; CHECK-NEXT: ret i8* [[RET]]
123 %ret = call i8* @strncpy(i8* nonnull %dst, i8* nonnull %src, i32 %n)
127 define i8* @test2(i8* %dst) {
128 ; CHECK-LABEL: @test2(
129 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(5) [[DST:%.*]], i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 5, i1 false)
130 ; CHECK-NEXT: ret i8* [[DST]]
132 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
133 %ret = call i8* @strncpy(i8* nonnull %dst, i8* nonnull %src, i32 5)
137 define i8* @test3(i8* %dst, i32 %n) {
138 ; CHECK-LABEL: @test3(
139 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noalias nonnull align 1 dereferenceable(5) [[DST:%.*]], i8 0, i32 5, i1 false)
140 ; CHECK-NEXT: ret i8* [[DST]]
142 %src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
143 %ret = call i8* @strncpy(i8* noalias nonnull %dst, i8* nonnull %src, i32 5);
147 ; Check cases that shouldn't be simplified.
149 define void @test_no_simplify1() {
150 ; CHECK-LABEL: @test_no_simplify1(
151 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncpy(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @b, i32 0, i32 0), i32 32)
152 ; CHECK-NEXT: ret void
154 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
155 %src = getelementptr [32 x i8], [32 x i8]* @b, i32 0, i32 0
157 call i8* @strncpy(i8* %dst, i8* %src, i32 32)
161 define void @test_no_simplify2() {
162 ; CHECK-LABEL: @test_no_simplify2(
163 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncpy(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0), i8* nonnull dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 8)
164 ; CHECK-NEXT: ret void
166 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
167 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
169 call i8* @strncpy(i8* %dst, i8* %src, i32 8)