simplify-libcalls: fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
[oota-llvm.git] / test / Transforms / SimplifyLibCalls / StrStr.ll
1 ; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
2 ; PR5783
3
4 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-n8:16:32"
5 target triple = "i386-apple-darwin9.0"
6
7 @.str = private constant [1 x i8] zeroinitializer ; <[1 x i8]*> [#uses=1]
8 @.str1 = private constant [2 x i8] c"a\00"        ; <[2 x i8]*> [#uses=1]
9 @.str2 = private constant [6 x i8] c"abcde\00"    ; <[6 x i8]*> [#uses=1]
10 @.str3 = private constant [4 x i8] c"bcd\00"      ; <[4 x i8]*> [#uses=1]
11
12 define i8* @test1(i8* %P) nounwind readonly {
13 entry:
14   %call = tail call i8* @strstr(i8* %P, i8* getelementptr inbounds ([1 x i8]* @.str, i32 0, i32 0)) nounwind ; <i8*> [#uses=1]
15   ret i8* %call
16 ; strstr(P, "") -> P
17 ; CHECK: @test1
18 ; CHECK: ret i8* %P
19 }
20
21 declare i8* @strstr(i8*, i8* nocapture) nounwind readonly
22
23 define i8* @test2(i8* %P) nounwind readonly {
24 entry:
25   %call = tail call i8* @strstr(i8* %P, i8* getelementptr inbounds ([2 x i8]* @.str1, i32 0, i32 0)) nounwind ; <i8*> [#uses=1]
26   ret i8* %call
27 ; strstr(P, "a") -> strchr(P, 'a')
28 ; CHECK: @test2
29 ; CHECK: @strchr(i8* %P, i32 97)
30 }
31
32 define i8* @test3(i8* nocapture %P) nounwind readonly {
33 entry:
34   %call = tail call i8* @strstr(i8* getelementptr inbounds ([6 x i8]* @.str2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8]* @.str3, i32 0, i32 0)) nounwind ; <i8*> [#uses=1]
35   ret i8* %call
36 ; strstr("abcde", "bcd") -> "abcde"+1
37 ; CHECK: @test3
38 ; CHECK: getelementptr inbounds ([6 x i8]* @.str2, i32 0, i64 1)
39 }
40
41 define i8* @test4(i8* %P) nounwind readonly {
42 entry:
43   %call = tail call i8* @strstr(i8* %P, i8* %P) nounwind ; <i8*> [#uses=1]
44   ret i8* %call
45 ; strstr(P, P) -> P
46 ; CHECK: @test4
47 ; CHECK: ret i8* %P
48 }
49
50 define i1 @test5(i8* %P, i8* %Q) nounwind readonly {
51 entry:
52   %call = tail call i8* @strstr(i8* %P, i8* %Q) nounwind ; <i8*> [#uses=1]
53   %cmp = icmp eq i8* %call, %P
54   ret i1 %cmp
55 ; CHECK: @test5
56 ; CHECK: [[LEN:%[a-z]+]] = call {{i[0-9]+}} @strlen(i8* %Q)
57 ; CHECK: [[NCMP:%[a-z]+]] = call {{i[0-9]+}} @strncmp(i8* %P, i8* %Q, {{i[0-9]+}} [[LEN]])
58 ; CHECK: icmp eq {{i[0-9]+}} [[NCMP]], 0
59 ; CHECK: ret i1
60 }