1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strncat libcall 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 @empty = constant [1 x i8] c"\00"
10 @a = common global [32 x i8] zeroinitializer, align 1
11 declare i8* @strncat(i8*, i8*, i32)
13 define void @test_simplify1() {
14 ; CHECK-LABEL: @test_simplify1(
15 ; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @a, i32 0, i32 0))
16 ; CHECK-NEXT: [[ENDPTR:%.*]] = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 [[STRLEN]]
17 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(6) [[ENDPTR]], i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 6, i1 false)
18 ; CHECK-NEXT: ret void
21 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
22 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
23 call i8* @strncat(i8* %dst, i8* %src, i32 13)
27 define void @test_simplify2() {
28 ; CHECK-LABEL: @test_simplify2(
29 ; CHECK-NEXT: ret void
32 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
33 %src = getelementptr [1 x i8], [1 x i8]* @empty, i32 0, i32 0
34 call i8* @strncat(i8* %dst, i8* %src, i32 13)
38 define void @test_simplify3() {
39 ; CHECK-LABEL: @test_simplify3(
40 ; CHECK-NEXT: ret void
43 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
44 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
45 call i8* @strncat(i8* %dst, i8* %src, i32 0)
49 define void @test_nosimplify1() {
50 ; CHECK-LABEL: @test_nosimplify1(
51 ; CHECK-NEXT: [[TMP1:%.*]] = call i8* @strncat(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 1)
52 ; CHECK-NEXT: ret void
55 %dst = getelementptr [32 x i8], [32 x i8]* @a, i32 0, i32 0
56 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
57 call i8* @strncat(i8* %dst, i8* %src, i32 1)
61 ; strncat(nonnull x, nonnull y, n) -> strncat(nonnull x, y, n)
62 define i8* @test1(i8* %str1, i8* %str2, i32 %n) {
63 ; CHECK-LABEL: @test1(
64 ; CHECK-NEXT: [[TEMP1:%.*]] = call i8* @strncat(i8* nonnull [[STR1:%.*]], i8* nonnull [[STR2:%.*]], i32 [[N:%.*]])
65 ; CHECK-NEXT: ret i8* [[TEMP1]]
68 %temp1 = call i8* @strncat(i8* nonnull %str1, i8* nonnull %str2, i32 %n)
72 ; strncat(x, y, 0) -> x
73 define i8* @test2(i8* %str1, i8* %str2, i32 %n) {
74 ; CHECK-LABEL: @test2(
75 ; CHECK-NEXT: ret i8* [[STR1:%.*]]
78 %temp1 = call i8* @strncat(i8* %str1, i8* %str2, i32 0)
82 ; strncat(x, y, 5) -> strncat(nonnull x, nonnull y, 5)
83 define i8* @test3(i8* %str1, i8* %str2, i32 %n) {
84 ; CHECK-LABEL: @test3(
85 ; CHECK-NEXT: [[TEMP1:%.*]] = call i8* @strncat(i8* nonnull dereferenceable(1) [[STR1:%.*]], i8* nonnull dereferenceable(1) [[STR2:%.*]], i32 5)
86 ; CHECK-NEXT: ret i8* [[TEMP1]]
89 %temp1 = call i8* @strncat(i8* %str1, i8* %str2, i32 5)
93 define i8* @test4(i8* %str1, i8* %str2, i32 %n) "null-pointer-is-valid"="true" {
94 ; CHECK-LABEL: @test4(
95 ; CHECK-NEXT: [[TEMP1:%.*]] = call i8* @strncat(i8* [[STR1:%.*]], i8* [[STR2:%.*]], i32 [[N:%.*]])
96 ; CHECK-NEXT: ret i8* [[TEMP1]]
99 %temp1 = call i8* @strncat(i8* %str1, i8* %str2, i32 %n)
103 define i8* @test5(i8* %str, i32 %n) {
104 ; CHECK-LABEL: @test5(
105 ; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
106 ; CHECK-NEXT: [[ENDPTR:%.*]] = getelementptr i8, i8* [[STR]], i32 [[STRLEN]]
107 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(6) [[ENDPTR]], i8* nonnull align 1 dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @hello, i32 0, i32 0), i32 6, i1 false)
108 ; CHECK-NEXT: ret i8* [[STR]]
110 %src = getelementptr [6 x i8], [6 x i8]* @hello, i32 0, i32 0
111 %temp1 = call i8* @strncat(i8* %str, i8* %src, i32 10)