xref: /llvm-project/llvm/test/Transforms/InstCombine/strrchr-3.ll (revision 10f315dc9c96ec2413881ab55a285e35d80def88)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Verify the strrchr("", c) to (unsigned char)c ? "" : 0 transformetion.
3;
4; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5
6@s10 = constant [11 x i8] c"0123456789\00"
7
8declare ptr @strrchr(ptr, i32)
9
10; Fold strrchr(s + 10, c) to (unsigned char)c ? 0 : s + 10.
11
12define ptr @fold_strrchr_sp10_x(i32 %c) {
13; CHECK-LABEL: @fold_strrchr_sp10_x(
14; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
15; CHECK-NEXT:    [[MEMRCHR_CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 0
16; CHECK-NEXT:    [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CHAR0CMP]], ptr getelementptr inbounds nuw (i8, ptr @s10, i64 10), ptr null
17; CHECK-NEXT:    ret ptr [[MEMRCHR_SEL]]
18;
19  %psp10 = getelementptr [11 x i8], ptr @s10, i32 0, i32 10
20  %pc = call ptr @strrchr(ptr %psp10, i32 %c)
21  ret ptr %pc
22}
23
24
25; Transform strrchr(s + 9, c) to [the equivalent of] memrchr(s + 9, c, 2).
26
27define ptr @call_strrchr_sp9_x(i32 %c) {
28; CHECK-LABEL: @call_strrchr_sp9_x(
29; CHECK-NEXT:    [[MEMRCHR:%.*]] = call ptr @memrchr(ptr noundef nonnull dereferenceable(2) getelementptr inbounds nuw (i8, ptr @s10, i64 9), i32 [[C:%.*]], i64 2)
30; CHECK-NEXT:    ret ptr [[MEMRCHR]]
31;
32  %psp9 = getelementptr [11 x i8], ptr @s10, i32 0, i32 9
33  %pc = call ptr @strrchr(ptr %psp9, i32 %c)
34  ret ptr %pc
35}
36
37
38; Do not transform strrchr(s + 2, c) (for short strings this could be
39; folded into a chain of OR expressions ala D128011).
40
41define ptr @call_strrchr_sp2_x(i32 %c) {
42; CHECK-LABEL: @call_strrchr_sp2_x(
43; CHECK-NEXT:    [[MEMRCHR:%.*]] = call ptr @memrchr(ptr noundef nonnull dereferenceable(9) getelementptr inbounds nuw (i8, ptr @s10, i64 2), i32 [[C:%.*]], i64 9)
44; CHECK-NEXT:    ret ptr [[MEMRCHR]]
45;
46  %psp2 = getelementptr [11 x i8], ptr @s10, i32 0, i32 2
47  %pc = call ptr @strrchr(ptr %psp2, i32 %c)
48  ret ptr %pc
49}
50
51
52; Do not transform strrchr(s + 1, c).
53
54define ptr @call_strrchr_sp1_x(i32 %c) {
55; CHECK-LABEL: @call_strrchr_sp1_x(
56; CHECK-NEXT:    [[MEMRCHR:%.*]] = call ptr @memrchr(ptr noundef nonnull dereferenceable(10) getelementptr inbounds nuw (i8, ptr @s10, i64 1), i32 [[C:%.*]], i64 10)
57; CHECK-NEXT:    ret ptr [[MEMRCHR]]
58;
59  %psp1 = getelementptr [11 x i8], ptr @s10, i32 0, i32 1
60  %pc = call ptr @strrchr(ptr %psp1, i32 %c)
61  ret ptr %pc
62}
63