1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the strrchr 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 [14 x i8] c"hello world\5Cn\00"
8 @null = constant [1 x i8] zeroinitializer
9 @chp = global i8* zeroinitializer
11 declare i8* @strrchr(i8*, i32)
13 define void @test_simplify1() {
14 ; CHECK-LABEL: @test_simplify1(
15 ; CHECK-NEXT: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 6), i8** @chp, align 4
16 ; CHECK-NEXT: ret void
19 %str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
20 %dst = call i8* @strrchr(i8* %str, i32 119)
21 store i8* %dst, i8** @chp
25 define void @test_simplify2() {
26 ; CHECK-LABEL: @test_simplify2(
27 ; CHECK-NEXT: store i8* null, i8** @chp, align 4
28 ; CHECK-NEXT: ret void
31 %str = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
32 %dst = call i8* @strrchr(i8* %str, i32 119)
33 store i8* %dst, i8** @chp
37 define void @test_simplify3() {
38 ; CHECK-LABEL: @test_simplify3(
39 ; CHECK-NEXT: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13), i8** @chp, align 4
40 ; CHECK-NEXT: ret void
43 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
44 %dst = call i8* @strrchr(i8* %src, i32 0)
45 store i8* %dst, i8** @chp
49 define void @test_simplify4() {
50 ; CHECK-LABEL: @test_simplify4(
51 ; CHECK-NEXT: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 13), i8** @chp, align 4
52 ; CHECK-NEXT: ret void
55 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
56 %dst = call i8* @strrchr(i8* %src, i32 65280)
57 store i8* %dst, i8** @chp
61 define void @test_nosimplify1(i32 %chr) {
62 ; CHECK-LABEL: @test_nosimplify1(
63 ; CHECK-NEXT: [[DST:%.*]] = call i8* @strrchr(i8* nonnull dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @hello, i32 0, i32 0), i32 [[CHR:%.*]])
64 ; CHECK-NEXT: store i8* [[DST]], i8** @chp, align 4
65 ; CHECK-NEXT: ret void
68 %src = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
69 %dst = call i8* @strrchr(i8* %src, i32 %chr)
70 store i8* %dst, i8** @chp
74 define i8* @test1(i8* %str, i32 %c) {
75 ; CHECK-LABEL: @test1(
76 ; CHECK-NEXT: [[RET:%.*]] = call i8* @strrchr(i8* nonnull dereferenceable(1) [[STR:%.*]], i32 [[C:%.*]])
77 ; CHECK-NEXT: ret i8* [[RET]]
80 %ret = call i8* @strrchr(i8* %str, i32 %c)
84 define i8* @test2(i8* %str, i32 %c) "null-pointer-is-valid"="true" {
85 ; CHECK-LABEL: @test2(
86 ; CHECK-NEXT: [[RET:%.*]] = call i8* @strrchr(i8* [[STR:%.*]], i32 [[C:%.*]])
87 ; CHECK-NEXT: ret i8* [[RET]]
90 %ret = call i8* @strrchr(i8* %str, i32 %c)