1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Test that the strncpy library call simplifier works correctly. 3; 4; RUN: opt < %s -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 i8* @strncpy(i8*, i8*, i32) 15declare i32 @puts(i8*) 16 17; Check a bunch of strncpy invocations together. 18 19define 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 29; 30 %target = alloca [1024 x i8] 31 %arg1 = getelementptr [1024 x i8], [1024 x i8]* %target, i32 0, i32 0 32 store i8 0, i8* %arg1 33 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) 36 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) 39 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) 42 43 call i32 @puts( i8* %rslt3 ) 44 ret i32 0 45} 46 47; Check strncpy(x, "", y) -> memset(x, '\0', y, 1). 48 49define 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 53; 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 56 57 call i8* @strncpy(i8* %dst, i8* %src, i32 32) 58 ret void 59} 60 61; Check strncpy(x, y, 0) -> x. 62 63define 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) 66; 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 69 70 %ret = call i8* @strncpy(i8* %dst, i8* %src, i32 0) 71 ret i8* %ret 72} 73 74; Check strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]. 75 76define 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 80; 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 83 84 call i8* @strncpy(i8* %dst, i8* %src, i32 6) 85 ret void 86} 87 88define void @test_simplify5(i8* %dst) { 89; CHECK-LABEL: @test_simplify5( 90; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(32) [[DST:%.*]], i8* nonnull align 1 dereferenceable(32) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i32 0, i32 0), i32 32, i1 false) 91; CHECK-NEXT: ret void 92; 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) 95 ret void 96} 97 98define void @test_simplify6(i8* %dst) { 99; CHECK-LABEL: @test_simplify6( 100; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(80) [[DST:%.*]], i8* nonnull align 1 dereferenceable(32) getelementptr inbounds ([33 x i8], [33 x i8]* @str.1, i32 0, i32 0), i32 32, i1 false) 101; CHECK-NEXT: ret void 102; 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) 105 ret void 106} 107 108define 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 112; 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) 115 ret void 116} 117 118define 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]] 122; 123 %ret = call i8* @strncpy(i8* nonnull %dst, i8* nonnull %src, i32 %n) 124 ret i8* %ret 125} 126 127define 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]] 131; 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) 134 ret i8* %ret 135} 136 137define 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]] 141; 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); 144 ret i8* %ret 145} 146 147; Check cases that shouldn't be simplified. 148 149define 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 153; 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 156 157 call i8* @strncpy(i8* %dst, i8* %src, i32 32) 158 ret void 159} 160 161define void @test_no_simplify2() { 162; CHECK-LABEL: @test_no_simplify2( 163; CHECK-NEXT: store i64 478560413032, i64* bitcast ([32 x i8]* @a to i64*), align 1 164; CHECK-NEXT: ret void 165; 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 168 169 call i8* @strncpy(i8* %dst, i8* %src, i32 8) 170 ret void 171} 172