Home | History | Annotate | Download | only in InstCombine
      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 @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
     13 
     14 declare i32 @strlen(i8*)
     15 
     16 ; Check strlen(string constant) -> integer constant.
     17 
     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)
     22   ret i32 %hello_l
     23 ; CHECK-NEXT: ret i32 5
     24 }
     25 
     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)
     30   ret i32 %null_l
     31 ; CHECK-NEXT: ret i32 0
     32 }
     33 
     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)
     38   ret i32 %null_hello_l
     39 ; CHECK-NEXT: ret i32 0
     40 }
     41 
     42 define i32 @test_simplify4() {
     43 ; CHECK-LABEL: @test_simplify4(
     44   %len = tail call i32 @strlen(i8* @nullstring) nounwind
     45   ret i32 %len
     46 ; CHECK-NEXT: ret i32 0
     47 }
     48 
     49 ; Check strlen(x) == 0 --> *x == 0.
     50 
     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
     56   ret i1 %eq_hello
     57 ; CHECK-NEXT: ret i1 false
     58 }
     59 
     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
     65   ret i1 %eq_null
     66 ; CHECK-NEXT: ret i1 true
     67 }
     68 
     69 ; Check strlen(x) != 0 --> *x != 0.
     70 
     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
     76   ret i1 %ne_hello
     77 ; CHECK-NEXT: ret i1 true
     78 }
     79 
     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
     85   ret i1 %ne_null
     86 ; CHECK-NEXT: ret i1 false
     87 }
     88 
     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
     96   ret i32 %l
     97 ; CHECK-NEXT: ret
     98 }
     99 
    100 ; Check cases that shouldn't be simplified.
    101 
    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
    107   ret i32 %a_l
    108 ; CHECK-NEXT: ret i32 %a_l
    109 }
    110