1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; Verify that memchr calls with a string consisting of all the same
5 ; characters are folded and those with mixed strings are not.
7 declare i8* @memchr(i8*, i32, i64)
9 @a00000 = constant [5 x i8] zeroinitializer
10 @a11111 = constant [5 x i8] c"\01\01\01\01\01"
11 @a111122 = constant [6 x i8] c"\01\01\01\01\02\02"
12 @a1110111 = constant [7 x i8] c"\01\01\01\00\01\01\01"
15 ; Fold memchr(a00000, C, 5) to *a00000 == C ? a00000 : null.
16 ; TODO: This depends on getConstantStringInfo() being able to handle
17 ; implicitly zeroed out constants.
19 define i8* @fold_memchr_a00000_c_5(i32 %C) {
20 ; CHECK-LABEL: @fold_memchr_a00000_c_5(
21 ; CHECK-NEXT: [[RET:%.*]] = call i8* @memchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @a00000, i64 0, i64 0), i32 [[C:%.*]], i64 5)
22 ; CHECK-NEXT: ret i8* [[RET]]
25 %ptr = getelementptr [5 x i8], [5 x i8]* @a00000, i64 0, i64 0
26 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 5)
31 ; Fold memchr(a11111, C, 5) to *a11111 == C ? a11111 : null.
33 define i8* @fold_memchr_a11111_c_5(i32 %C) {
34 ; CHECK-LABEL: @fold_memchr_a11111_c_5(
35 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
36 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
37 ; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP2]], i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 0), i8* null
38 ; CHECK-NEXT: ret i8* [[MEMCHR_SEL2]]
41 %ptr = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 0
42 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 5)
47 ; Fold memchr(a11111, C, N) to N && *a11111 == C ? a11111 : null,
48 ; on the assumption that N is in bounds.
50 define i8* @fold_memchr_a11111_c_n(i32 %C, i64 %N) {
51 ; CHECK-LABEL: @fold_memchr_a11111_c_n(
52 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
53 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
54 ; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i64 [[N:%.*]], 0
55 ; CHECK-NEXT: [[TMP4:%.*]] = and i1 [[TMP3]], [[TMP2]]
56 ; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP4]], i8* getelementptr inbounds ([5 x i8], [5 x i8]* @a11111, i64 0, i64 0), i8* null
57 ; CHECK-NEXT: ret i8* [[MEMCHR_SEL2]]
60 %ptr = getelementptr [5 x i8], [5 x i8]* @a11111, i64 0, i64 0
61 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 %N)
66 ; Fold memchr(a111122, C, N) to
67 ; N != 0 && C == 1 ? a111122 : N > 4 && C == 2 ? a111122 + 4 : null.
69 define i8* @fold_memchr_a111122_c_n(i32 %C, i64 %N) {
70 ; CHECK-LABEL: @fold_memchr_a111122_c_n(
71 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
72 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 2
73 ; CHECK-NEXT: [[TMP3:%.*]] = icmp ugt i64 [[N:%.*]], 4
74 ; CHECK-NEXT: [[TMP4:%.*]] = and i1 [[TMP2]], [[TMP3]]
75 ; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP4]], i8* getelementptr inbounds ([6 x i8], [6 x i8]* @a111122, i64 0, i64 4), i8* null
76 ; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i8 [[TMP1]], 1
77 ; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i64 [[N]], 0
78 ; CHECK-NEXT: [[TMP7:%.*]] = and i1 [[TMP6]], [[TMP5]]
79 ; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP7]], i8* getelementptr inbounds ([6 x i8], [6 x i8]* @a111122, i64 0, i64 0), i8* [[MEMCHR_SEL1]]
80 ; CHECK-NEXT: ret i8* [[MEMCHR_SEL2]]
83 %ptr = getelementptr [6 x i8], [6 x i8]* @a111122, i64 0, i64 0
84 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 %N)
89 ; Fold memchr(a1110111, C, 3) to a1110111[2] == C ? a1110111 : null.
91 define i8* @fold_memchr_a1110111_c_3(i32 %C) {
92 ; CHECK-LABEL: @fold_memchr_a1110111_c_3(
93 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
94 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
95 ; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP2]], i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i8* null
96 ; CHECK-NEXT: ret i8* [[MEMCHR_SEL2]]
99 %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
100 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 3)
105 ; Don't fold memchr(a1110111, C, 4).
107 define i8* @call_memchr_a1110111_c_4(i32 %C) {
108 ; CHECK-LABEL: @call_memchr_a1110111_c_4(
109 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
110 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0
111 ; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 3), i8* null
112 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1
113 ; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i8* [[MEMCHR_SEL1]]
114 ; CHECK-NEXT: ret i8* [[MEMCHR_SEL2]]
117 %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
118 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 4)
123 ; Don't fold memchr(a1110111, C, 7).
125 define i8* @call_memchr_a1110111_c_7(i32 %C) {
126 ; CHECK-LABEL: @call_memchr_a1110111_c_7(
127 ; CHECK-NEXT: [[RET:%.*]] = call i8* @memchr(i8* noundef nonnull dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[C:%.*]], i64 7)
128 ; CHECK-NEXT: ret i8* [[RET]]
131 %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
132 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 7)
137 ; Don't fold memchr(a1110111, C, N).
139 define i8* @call_memchr_a1110111_c_n(i32 %C, i64 %N) {
140 ; CHECK-LABEL: @call_memchr_a1110111_c_n(
141 ; CHECK-NEXT: [[RET:%.*]] = call i8* @memchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @a1110111, i64 0, i64 0), i32 [[C:%.*]], i64 [[N:%.*]])
142 ; CHECK-NEXT: ret i8* [[RET]]
145 %ptr = getelementptr [7 x i8], [7 x i8]* @a1110111, i64 0, i64 0
146 %ret = call i8* @memchr(i8* %ptr, i32 %C, i64 %N)