xref: /llvm-project/llvm/test/Transforms/InstCombine/strchr-4.ll (revision 4ab40eca080965c65802710e39adbb78c4ce7bde)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3;
4; Verify that the result of strchr calls used in equality expressions
5; with either the first argument or null are optimally folded.
6
7declare ptr @strchr(ptr, i32)
8
9
10; Fold strchr(s, c) == s to *s == c.
11
12define i1 @fold_strchr_s_c_eq_s(ptr %s, i32 %c) {
13; CHECK-LABEL: @fold_strchr_s_c_eq_s(
14; CHECK-NEXT:    [[TMP1:%.*]] = load i8, ptr [[S:%.*]], align 1
15; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8
16; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], [[TMP2]]
17; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
18;
19  %p = call ptr @strchr(ptr %s, i32 %c)
20  %cmp = icmp eq ptr %p, %s
21  ret i1 %cmp
22}
23
24
25; Fold strchr(s, c) != s to *s != c.
26
27define i1 @fold_strchr_s_c_neq_s(ptr %s, i32 %c) {
28; CHECK-LABEL: @fold_strchr_s_c_neq_s(
29; CHECK-NEXT:    [[TMP1:%.*]] = load i8, ptr [[S:%.*]], align 1
30; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8
31; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp ne i8 [[TMP1]], [[TMP2]]
32; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
33;
34  %p = call ptr @strchr(ptr %s, i32 %c)
35  %cmp = icmp ne ptr %p, %s
36  ret i1 %cmp
37}
38
39
40; Fold strchr(s, '\0') == null to false.  (A string must be nul-terminated,
41; otherwise the call would read past the end of the array.)
42
43define i1 @fold_strchr_s_nul_eqz(ptr %s) {
44; CHECK-LABEL: @fold_strchr_s_nul_eqz(
45; CHECK-NEXT:    ret i1 false
46;
47  %p = call ptr @strchr(ptr %s, i32 0)
48  %cmp = icmp eq ptr %p, null
49  ret i1 %cmp
50}
51
52
53; Fold strchr(s, '\0') != null to true.
54
55define i1 @fold_strchr_s_nul_nez(ptr %s) {
56; CHECK-LABEL: @fold_strchr_s_nul_nez(
57; CHECK-NEXT:    ret i1 true
58;
59  %p = call ptr @strchr(ptr %s, i32 0)
60  %cmp = icmp ne ptr %p, null
61  ret i1 %cmp
62}
63
64
65@a5 = constant [5 x i8] c"12345";
66
67; Fold strchr(a5, c) == a5 to *a5 == c.
68
69define i1 @fold_strchr_a_c_eq_a(i32 %c) {
70; CHECK-LABEL: @fold_strchr_a_c_eq_a(
71; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
72; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49
73; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
74;
75  %q = call ptr @strchr(ptr @a5, i32 %c)
76  %cmp = icmp eq ptr %q, @a5
77  ret i1 %cmp
78}
79