1 ; Test that the strlen library call simplifier works correctly.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
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"
7 @hello = constant [6 x i8] c"hello\00"
8 @longer = constant [7 x i8] c"longer\00"
9 @null = constant [1 x i8] zeroinitializer
10 @null_hello = constant [7 x i8] c"\00hello\00"
11 @nullstring = constant i8 0
12 @a = common global [32 x i8] zeroinitializer, align 1
14 declare i32 @strlen(i8*)
16 ; Check strlen(string constant) -> integer constant.
18 define i32 @test_simplify1() {
19 ; CHECK-LABEL: @test_simplify1(
20 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0
21 %hello_l = call i32 @strlen(i8* %hello_p)
23 ; CHECK-NEXT: ret i32 5
26 define i32 @test_simplify2() {
27 ; CHECK-LABEL: @test_simplify2(
28 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
29 %null_l = call i32 @strlen(i8* %null_p)
31 ; CHECK-NEXT: ret i32 0
34 define i32 @test_simplify3() {
35 ; CHECK-LABEL: @test_simplify3(
36 %null_hello_p = getelementptr [7 x i8]* @null_hello, i32 0, i32 0
37 %null_hello_l = call i32 @strlen(i8* %null_hello_p)
39 ; CHECK-NEXT: ret i32 0
42 define i32 @test_simplify4() {
43 ; CHECK-LABEL: @test_simplify4(
44 %len = tail call i32 @strlen(i8* @nullstring) nounwind
46 ; CHECK-NEXT: ret i32 0
49 ; Check strlen(x) == 0 --> *x == 0.
51 define i1 @test_simplify5() {
52 ; CHECK-LABEL: @test_simplify5(
53 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0
54 %hello_l = call i32 @strlen(i8* %hello_p)
55 %eq_hello = icmp eq i32 %hello_l, 0
57 ; CHECK-NEXT: ret i1 false
60 define i1 @test_simplify6() {
61 ; CHECK-LABEL: @test_simplify6(
62 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
63 %null_l = call i32 @strlen(i8* %null_p)
64 %eq_null = icmp eq i32 %null_l, 0
66 ; CHECK-NEXT: ret i1 true
69 ; Check strlen(x) != 0 --> *x != 0.
71 define i1 @test_simplify7() {
72 ; CHECK-LABEL: @test_simplify7(
73 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0
74 %hello_l = call i32 @strlen(i8* %hello_p)
75 %ne_hello = icmp ne i32 %hello_l, 0
77 ; CHECK-NEXT: ret i1 true
80 define i1 @test_simplify8() {
81 ; CHECK-LABEL: @test_simplify8(
82 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
83 %null_l = call i32 @strlen(i8* %null_p)
84 %ne_null = icmp ne i32 %null_l, 0
86 ; CHECK-NEXT: ret i1 false
89 define i32 @test_simplify9(i1 %x) {
90 ; CHECK-LABEL: @test_simplify9
91 %hello = getelementptr [6 x i8]* @hello, i32 0, i32 0
92 %longer = getelementptr [7 x i8]* @longer, i32 0, i32 0
93 %s = select i1 %x, i8* %hello, i8* %longer
94 %l = call i32 @strlen(i8* %s)
95 ; CHECK-NEXT: select i1 %x, i32 5, i32 6
100 ; Check cases that shouldn't be simplified.
102 define i32 @test_no_simplify1() {
103 ; CHECK-LABEL: @test_no_simplify1(
104 %a_p = getelementptr [32 x i8]* @a, i32 0, i32 0
105 %a_l = call i32 @strlen(i8* %a_p)
106 ; CHECK-NEXT: %a_l = call i32 @strlen
108 ; CHECK-NEXT: ret i32 %a_l