| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s |
| ; |
| ; Verify that the result of memchr calls used in equality expressions |
| ; with either the first argument or null are optimally folded. |
| |
| declare ptr @memchr(ptr, i32, i64) |
| |
| |
| @a5 = constant [5 x i8] c"12345" |
| |
| ; Fold memchr(a5, c, 5) == a5 to *a5 == c. |
| |
| define i1 @fold_memchr_a_c_5_eq_a(i32 %c) { |
| ; CHECK-LABEL: @fold_memchr_a_c_5_eq_a( |
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 |
| ; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49 |
| ; CHECK-NEXT: ret i1 [[CHAR0CMP]] |
| ; |
| %q = call ptr @memchr(ptr @a5, i32 %c, i64 5) |
| %cmp = icmp eq ptr %q, @a5 |
| ret i1 %cmp |
| } |
| |
| |
| ; Fold memchr(a5, c, n) == a5 to n && *a5 == c. Unlike the case when |
| ; the first argument is an arbitrary, including potentially past-the-end, |
| ; pointer, this is safe because a5 is dereferenceable. |
| |
| define i1 @fold_memchr_a_c_n_eq_a(i32 %c, i64 %n) { |
| ; CHECK-LABEL: @fold_memchr_a_c_n_eq_a( |
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 |
| ; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49 |
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[N:%.*]], 0 |
| ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i1 [[CHAR0CMP]], i1 false |
| ; CHECK-NEXT: ret i1 [[TMP3]] |
| ; |
| %q = call ptr @memchr(ptr @a5, i32 %c, i64 %n) |
| %cmp = icmp eq ptr %q, @a5 |
| ret i1 %cmp |
| } |
| |
| |
| ; Do not fold memchr(a5 + i, c, n). |
| |
| define i1 @call_memchr_api_c_n_eq_a(i64 %i, i32 %c, i64 %n) { |
| ; CHECK-LABEL: @call_memchr_api_c_n_eq_a( |
| ; CHECK-NEXT: [[P:%.*]] = getelementptr [5 x i8], ptr @a5, i64 0, i64 [[I:%.*]] |
| ; CHECK-NEXT: [[Q:%.*]] = call ptr @memchr(ptr [[P]], i32 [[C:%.*]], i64 [[N:%.*]]) |
| ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[Q]], [[P]] |
| ; CHECK-NEXT: ret i1 [[CMP]] |
| ; |
| %p = getelementptr [5 x i8], ptr @a5, i64 0, i64 %i |
| %q = call ptr @memchr(ptr %p, i32 %c, i64 %n) |
| %cmp = icmp eq ptr %q, %p |
| ret i1 %cmp |
| } |
| |
| |
| ; Fold memchr(s, c, 15) == s to *s == c. |
| |
| define i1 @fold_memchr_s_c_15_eq_s(ptr %s, i32 %c) { |
| ; CHECK-LABEL: @fold_memchr_s_c_15_eq_s( |
| ; CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[S:%.*]], align 1 |
| ; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8 |
| ; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], [[TMP2]] |
| ; CHECK-NEXT: ret i1 [[CHAR0CMP]] |
| ; |
| %p = call ptr @memchr(ptr %s, i32 %c, i64 15) |
| %cmp = icmp eq ptr %p, %s |
| ret i1 %cmp |
| } |
| |
| |
| ; Fold memchr(s, c, 17) != s to *s != c. |
| |
| define i1 @fold_memchr_s_c_17_neq_s(ptr %s, i32 %c) { |
| ; CHECK-LABEL: @fold_memchr_s_c_17_neq_s( |
| ; CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[S:%.*]], align 1 |
| ; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8 |
| ; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp ne i8 [[TMP1]], [[TMP2]] |
| ; CHECK-NEXT: ret i1 [[CHAR0CMP]] |
| ; |
| %p = call ptr @memchr(ptr %s, i32 %c, i64 17) |
| %cmp = icmp ne ptr %p, %s |
| ret i1 %cmp |
| } |
| |
| |
| ; Fold memchr(s, c, n) == s to *s == c for nonzero n. |
| |
| define i1 @fold_memchr_s_c_nz_eq_s(ptr %s, i32 %c, i64 %n) { |
| ; CHECK-LABEL: @fold_memchr_s_c_nz_eq_s( |
| ; CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[S:%.*]], align 1 |
| ; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8 |
| ; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], [[TMP2]] |
| ; CHECK-NEXT: ret i1 [[CHAR0CMP]] |
| ; |
| %nz = or i64 %n, 1 |
| %p = call ptr @memchr(ptr %s, i32 %c, i64 %nz) |
| %cmp = icmp eq ptr %p, %s |
| ret i1 %cmp |
| } |
| |
| |
| ; But do not fold memchr(s, c, n) as above if n might be zero. This could |
| ; be optimized to the equivalent of N && *S == C provided a short-circuiting |
| ; AND, otherwise the load could read a byte just past the end of an array. |
| |
| define i1 @call_memchr_s_c_n_eq_s(ptr %s, i32 %c, i64 %n) { |
| ; CHECK-LABEL: @call_memchr_s_c_n_eq_s( |
| ; CHECK-NEXT: [[P:%.*]] = call ptr @memchr(ptr [[S:%.*]], i32 [[C:%.*]], i64 [[N:%.*]]) |
| ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[P]], [[S]] |
| ; CHECK-NEXT: ret i1 [[CMP]] |
| ; |
| %p = call ptr @memchr(ptr %s, i32 %c, i64 %n) |
| %cmp = icmp eq ptr %p, %s |
| ret i1 %cmp |
| } |