1 ; Test that the strlen library call simplifier works correctly. 2 ; 3 ; RUN: opt < %s -instcombine -S | FileCheck %s 4 5 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" 6 7 @hello = constant [6 x i8] c"hello\00" 8 @null = constant [1 x i8] zeroinitializer 9 @null_hello = constant [7 x i8] c"\00hello\00" 10 @nullstring = constant i8 0 11 @a = common global [32 x i8] zeroinitializer, align 1 12 13 declare i32 @strlen(i8*) 14 15 ; Check strlen(string constant) -> integer constant. 16 17 define i32 @test_simplify1() { 18 ; CHECK: @test_simplify1 19 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0 20 %hello_l = call i32 @strlen(i8* %hello_p) 21 ret i32 %hello_l 22 ; CHECK-NEXT: ret i32 5 23 } 24 25 define i32 @test_simplify2() { 26 ; CHECK: @test_simplify2 27 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0 28 %null_l = call i32 @strlen(i8* %null_p) 29 ret i32 %null_l 30 ; CHECK-NEXT: ret i32 0 31 } 32 33 define i32 @test_simplify3() { 34 ; CHECK: @test_simplify3 35 %null_hello_p = getelementptr [7 x i8]* @null_hello, i32 0, i32 0 36 %null_hello_l = call i32 @strlen(i8* %null_hello_p) 37 ret i32 %null_hello_l 38 ; CHECK-NEXT: ret i32 0 39 } 40 41 define i32 @test_simplify4() { 42 ; CHECK: @test_simplify4 43 %len = tail call i32 @strlen(i8* @nullstring) nounwind 44 ret i32 %len 45 ; CHECK-NEXT: ret i32 0 46 } 47 48 ; Check strlen(x) == 0 --> *x == 0. 49 50 define i1 @test_simplify5() { 51 ; CHECK: @test_simplify5 52 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0 53 %hello_l = call i32 @strlen(i8* %hello_p) 54 %eq_hello = icmp eq i32 %hello_l, 0 55 ret i1 %eq_hello 56 ; CHECK-NEXT: ret i1 false 57 } 58 59 define i1 @test_simplify6() { 60 ; CHECK: @test_simplify6 61 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0 62 %null_l = call i32 @strlen(i8* %null_p) 63 %eq_null = icmp eq i32 %null_l, 0 64 ret i1 %eq_null 65 ; CHECK-NEXT: ret i1 true 66 } 67 68 ; Check strlen(x) != 0 --> *x != 0. 69 70 define i1 @test_simplify7() { 71 ; CHECK: @test_simplify7 72 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0 73 %hello_l = call i32 @strlen(i8* %hello_p) 74 %ne_hello = icmp ne i32 %hello_l, 0 75 ret i1 %ne_hello 76 ; CHECK-NEXT: ret i1 true 77 } 78 79 define i1 @test_simplify8() { 80 ; CHECK: @test_simplify8 81 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0 82 %null_l = call i32 @strlen(i8* %null_p) 83 %ne_null = icmp ne i32 %null_l, 0 84 ret i1 %ne_null 85 ; CHECK-NEXT: ret i1 false 86 } 87 88 ; Check cases that shouldn't be simplified. 89 90 define i32 @test_no_simplify1() { 91 ; CHECK: @test_no_simplify1 92 %a_p = getelementptr [32 x i8]* @a, i32 0, i32 0 93 %a_l = call i32 @strlen(i8* %a_p) 94 ; CHECK-NEXT: %a_l = call i32 @strlen 95 ret i32 %a_l 96 ; CHECK-NEXT: ret i32 %a_l 97 } 98