1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; Verify that the result of strchr calls used in equality expressions
5 ; with either the first argument or null are optimally folded.
7 declare ptr @strchr(ptr, i32)
10 ; Fold strchr(s, c) == s to *s == c.
12 define 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]]
19 %p = call ptr @strchr(ptr %s, i32 %c)
20 %cmp = icmp eq ptr %p, %s
25 ; Fold strchr(s, c) != s to *s != c.
27 define 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]]
34 %p = call ptr @strchr(ptr %s, i32 %c)
35 %cmp = icmp ne ptr %p, %s
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.)
43 define i1 @fold_strchr_s_nul_eqz(ptr %s) {
44 ; CHECK-LABEL: @fold_strchr_s_nul_eqz(
45 ; CHECK-NEXT: ret i1 false
47 %p = call ptr @strchr(ptr %s, i32 0)
48 %cmp = icmp eq ptr %p, null
53 ; Fold strchr(s, '\0') != null to true.
55 define i1 @fold_strchr_s_nul_nez(ptr %s) {
56 ; CHECK-LABEL: @fold_strchr_s_nul_nez(
57 ; CHECK-NEXT: ret i1 true
59 %p = call ptr @strchr(ptr %s, i32 0)
60 %cmp = icmp ne ptr %p, null
65 @a5 = constant [5 x i8] c"12345";
67 ; Fold strchr(a5, c) == a5 to *a5 == c.
69 define 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]]
75 %q = call ptr @strchr(ptr @a5, i32 %c)
76 %cmp = icmp eq ptr %q, @a5