xref: /llvm-project/llvm/test/Transforms/InstCombine/strpbrk-1.ll (revision 10f315dc9c96ec2413881ab55a285e35d80def88)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Test that the strpbrk library call simplifier works correctly.
3;
4; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5
6target 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
8@hello = constant [12 x i8] c"hello world\00"
9@w = constant [2 x i8] c"w\00"
10@null = constant [1 x i8] zeroinitializer
11
12declare ptr @strpbrk(ptr, ptr)
13
14; Check strpbrk(s, "") -> NULL.
15
16define ptr @test_simplify1(ptr %str) {
17; CHECK-LABEL: @test_simplify1(
18; CHECK-NEXT:    ret ptr null
19;
20
21  %ret = call ptr @strpbrk(ptr %str, ptr @null)
22  ret ptr %ret
23}
24
25; Check strpbrk("", s) -> NULL.
26
27define ptr @test_simplify2(ptr %pat) {
28; CHECK-LABEL: @test_simplify2(
29; CHECK-NEXT:    ret ptr null
30;
31
32  %ret = call ptr @strpbrk(ptr @null, ptr %pat)
33  ret ptr %ret
34}
35
36; Check strpbrk(s1, s2), where s1 and s2 are constants.
37
38define ptr @test_simplify3() {
39; CHECK-LABEL: @test_simplify3(
40; CHECK-NEXT:    ret ptr getelementptr inbounds nuw (i8, ptr @hello, i32 6)
41;
42
43  %ret = call ptr @strpbrk(ptr @hello, ptr @w)
44  ret ptr %ret
45}
46
47; Check strpbrk(s, "a") -> strchr(s, 'a').
48
49define ptr @test_simplify4(ptr %str) {
50; CHECK-LABEL: @test_simplify4(
51; CHECK-NEXT:    [[STRCHR:%.*]] = call ptr @strchr(ptr noundef nonnull dereferenceable(1) [[STR:%.*]], i32 119)
52; CHECK-NEXT:    ret ptr [[STRCHR]]
53;
54
55  %ret = call ptr @strpbrk(ptr %str, ptr @w)
56  ret ptr %ret
57}
58
59; Check cases that shouldn't be simplified.
60
61define ptr @test_no_simplify1(ptr %str, ptr %pat) {
62; CHECK-LABEL: @test_no_simplify1(
63; CHECK-NEXT:    [[RET:%.*]] = call ptr @strpbrk(ptr [[STR:%.*]], ptr [[PAT:%.*]])
64; CHECK-NEXT:    ret ptr [[RET]]
65;
66
67  %ret = call ptr @strpbrk(ptr %str, ptr %pat)
68  ret ptr %ret
69}
70