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 @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
13 declare i32 @strlen(i8*)
15 ; Check strlen(string constant) -> integer constant.
17 define i32 @test_simplify1() {
18 ; CHECK-LABEL: @test_simplify1(
19 %hello_p = getelementptr [6 x i8]* @hello, i32 0, i32 0
20 %hello_l = call i32 @strlen(i8* %hello_p)
22 ; CHECK-NEXT: ret i32 5
25 define i32 @test_simplify2() {
26 ; CHECK-LABEL: @test_simplify2(
27 %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
28 %null_l = call i32 @strlen(i8* %null_p)
30 ; CHECK-NEXT: ret i32 0
33 define i32 @test_simplify3() {
34 ; CHECK-LABEL: @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)
38 ; CHECK-NEXT: ret i32 0
41 define i32 @test_simplify4() {
42 ; CHECK-LABEL: @test_simplify4(
43 %len = tail call i32 @strlen(i8* @nullstring) nounwind
45 ; CHECK-NEXT: ret i32 0
48 ; Check strlen(x) == 0 --> *x == 0.
50 define i1 @test_simplify5() {
51 ; CHECK-LABEL: @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
56 ; CHECK-NEXT: ret i1 false
59 define i1 @test_simplify6() {
60 ; CHECK-LABEL: @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
65 ; CHECK-NEXT: ret i1 true
68 ; Check strlen(x) != 0 --> *x != 0.
70 define i1 @test_simplify7() {
71 ; CHECK-LABEL: @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
76 ; CHECK-NEXT: ret i1 true
79 define i1 @test_simplify8() {
80 ; CHECK-LABEL: @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
85 ; CHECK-NEXT: ret i1 false
88 ; Check cases that shouldn't be simplified.
90 define i32 @test_no_simplify1() {
91 ; CHECK-LABEL: @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
96 ; CHECK-NEXT: ret i32 %a_l