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 -passes=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 ptr @strncat(ptr, ptr, i32)
13 define void @test_simplify1() {
14 ; CHECK-LABEL: @test_simplify1(
15 ; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) @a)
16 ; CHECK-NEXT: [[ENDPTR:%.*]] = getelementptr inbounds i8, ptr @a, i32 [[STRLEN]]
17 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(6) [[ENDPTR]], ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 6, i1 false)
18 ; CHECK-NEXT: ret void
21 call ptr @strncat(ptr @a, ptr @hello, i32 13)
25 define void @test_simplify2() {
26 ; CHECK-LABEL: @test_simplify2(
27 ; CHECK-NEXT: ret void
30 call ptr @strncat(ptr @a, ptr @empty, i32 13)
34 define void @test_simplify3() {
35 ; CHECK-LABEL: @test_simplify3(
36 ; CHECK-NEXT: ret void
39 call ptr @strncat(ptr @a, ptr @hello, i32 0)
43 define void @test_nosimplify1() {
44 ; CHECK-LABEL: @test_nosimplify1(
45 ; CHECK-NEXT: [[TMP1:%.*]] = call ptr @strncat(ptr noundef nonnull dereferenceable(1) @a, ptr noundef nonnull dereferenceable(6) @hello, i32 1)
46 ; CHECK-NEXT: ret void
49 call ptr @strncat(ptr @a, ptr @hello, i32 1)
53 ; strncat(nonnull x, nonnull y, n) -> strncat(nonnull x, y, n)
54 define ptr @test1(ptr %str1, ptr %str2, i32 %n) {
55 ; CHECK-LABEL: @test1(
56 ; CHECK-NEXT: [[TEMP1:%.*]] = call ptr @strncat(ptr noundef nonnull dereferenceable(1) [[STR1:%.*]], ptr nonnull [[STR2:%.*]], i32 [[N:%.*]])
57 ; CHECK-NEXT: ret ptr [[TEMP1]]
60 %temp1 = call ptr @strncat(ptr nonnull %str1, ptr nonnull %str2, i32 %n)
64 ; strncat(x, y, 0) -> x
65 define ptr @test2(ptr %str1, ptr %str2, i32 %n) {
66 ; CHECK-LABEL: @test2(
67 ; CHECK-NEXT: ret ptr [[STR1:%.*]]
70 %temp1 = call ptr @strncat(ptr %str1, ptr %str2, i32 0)
74 ; strncat(x, y, 5) -> strncat(nonnull x, nonnull y, 5)
75 define ptr @test3(ptr %str1, ptr %str2, i32 %n) {
76 ; CHECK-LABEL: @test3(
77 ; CHECK-NEXT: [[TEMP1:%.*]] = call ptr @strncat(ptr noundef nonnull dereferenceable(1) [[STR1:%.*]], ptr noundef nonnull dereferenceable(1) [[STR2:%.*]], i32 5)
78 ; CHECK-NEXT: ret ptr [[TEMP1]]
81 %temp1 = call ptr @strncat(ptr %str1, ptr %str2, i32 5)
85 define ptr @test4(ptr %str1, ptr %str2, i32 %n) null_pointer_is_valid {
86 ; CHECK-LABEL: @test4(
87 ; CHECK-NEXT: [[TEMP1:%.*]] = call ptr @strncat(ptr noundef [[STR1:%.*]], ptr [[STR2:%.*]], i32 [[N:%.*]])
88 ; CHECK-NEXT: ret ptr [[TEMP1]]
91 %temp1 = call ptr @strncat(ptr %str1, ptr %str2, i32 %n)
95 define ptr @test5(ptr %str, i32 %n) {
96 ; CHECK-LABEL: @test5(
97 ; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[STR:%.*]])
98 ; CHECK-NEXT: [[ENDPTR:%.*]] = getelementptr inbounds i8, ptr [[STR]], i32 [[STRLEN]]
99 ; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr noundef nonnull align 1 dereferenceable(6) [[ENDPTR]], ptr noundef nonnull align 1 dereferenceable(6) @hello, i32 6, i1 false)
100 ; CHECK-NEXT: ret ptr [[STR]]
102 %temp1 = call ptr @strncat(ptr %str, ptr @hello, i32 10)