1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Verify the strrchr("", c) to (unsigned char)c ? "" : 0 transformetion.
4 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
6 @s10 = constant [11 x i8] c"0123456789\00"
8 declare ptr @strrchr(ptr, i32)
10 ; Fold strrchr(s + 10, c) to (unsigned char)c ? 0 : s + 10.
12 define 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 (i8, ptr @s10, i64 10), ptr null
17 ; CHECK-NEXT: ret ptr [[MEMRCHR_SEL]]
19 %psp10 = getelementptr [11 x i8], ptr @s10, i32 0, i32 10
20 %pc = call ptr @strrchr(ptr %psp10, i32 %c)
25 ; Transform strrchr(s + 9, c) to [the equivalent of] memrchr(s + 9, c, 2).
27 define 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 (i8, ptr @s10, i64 9), i32 [[C:%.*]], i64 2)
30 ; CHECK-NEXT: ret ptr [[MEMRCHR]]
32 %psp9 = getelementptr [11 x i8], ptr @s10, i32 0, i32 9
33 %pc = call ptr @strrchr(ptr %psp9, i32 %c)
38 ; Do not transform strrchr(s + 2, c) (for short strings this could be
39 ; folded into a chain of OR expressions ala D128011).
41 define 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 (i8, ptr @s10, i64 2), i32 [[C:%.*]], i64 9)
44 ; CHECK-NEXT: ret ptr [[MEMRCHR]]
46 %psp2 = getelementptr [11 x i8], ptr @s10, i32 0, i32 2
47 %pc = call ptr @strrchr(ptr %psp2, i32 %c)
52 ; Do not transform strrchr(s + 1, c).
54 define 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 (i8, ptr @s10, i64 1), i32 [[C:%.*]], i64 10)
57 ; CHECK-NEXT: ret ptr [[MEMRCHR]]
59 %psp1 = getelementptr [11 x i8], ptr @s10, i32 0, i32 1
60 %pc = call ptr @strrchr(ptr %psp1, i32 %c)