79 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3;4; Verify that the result of strchr calls used in equality expressions5; 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 115; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i816; 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, %s21 ret i1 %cmp22}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 130; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i831; 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, %s36 ret i1 %cmp37}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 false46;47 %p = call ptr @strchr(ptr %s, i32 0)48 %cmp = icmp eq ptr %p, null49 ret i1 %cmp50}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 true58;59 %p = call ptr @strchr(ptr %s, i32 0)60 %cmp = icmp ne ptr %p, null61 ret i1 %cmp62}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 i872; CHECK-NEXT: [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 4973; CHECK-NEXT: ret i1 [[CHAR0CMP]]74;75 %q = call ptr @strchr(ptr @a5, i32 %c)76 %cmp = icmp eq ptr %q, @a577 ret i1 %cmp78}79