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 strchr calls with a string consisting of one or two sequences 5; of all the same characters are folded and those with mixed strings are not. 6 7@s1 = constant [2 x i8] c"\01\00" 8@s11 = constant [3 x i8] c"\01\01\00" 9@s111 = constant [4 x i8] c"\01\01\01\00" 10@s000 = constant [4 x i8] c"\00\00\00\00" 11@s11102 = constant [6 x i8] c"\01\01\01\00\02\00" 12@s21111 = constant [6 x i8] c"\02\01\01\01\01\00" 13 14declare ptr @strchr(ptr, i32) 15 16 17; Fold strchr(S = "\01", C) to C == '\01' ? S : C == '\0' ? S + 1 : null. 18 19define ptr @fold_strchr_s1_C(i32 %C) { 20; CHECK-LABEL: @fold_strchr_s1_C( 21; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 22; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 23; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @s1, i64 1), ptr null 24; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1 25; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], ptr @s1, ptr [[MEMCHR_SEL1]] 26; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]] 27; 28 %ret = call ptr @strchr(ptr @s1, i32 %C) 29 ret ptr %ret 30} 31 32 33; Fold strchr(S = "\01\01", C) to C == '\01' ? S : C == '\0' ? S + 2 : null. 34 35define ptr @fold_strchr_s11_C(i32 %C) { 36; CHECK-LABEL: @fold_strchr_s11_C( 37; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 38; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 39; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @s11, i64 2), ptr null 40; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1 41; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], ptr @s11, ptr [[MEMCHR_SEL1]] 42; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]] 43; 44 %ret = call ptr @strchr(ptr @s11, i32 %C) 45 ret ptr %ret 46} 47 48 49; Fold strchr(S = "\01\01\01", C) to C == '\01' ? S : C == '\0' ? S + 3 : null. 50 51define ptr @fold_strchr_s111_C(i32 %C) { 52; CHECK-LABEL: @fold_strchr_s111_C( 53; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 54; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 55; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @s111, i64 3), ptr null 56; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1 57; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], ptr @s111, ptr [[MEMCHR_SEL1]] 58; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]] 59; 60 %ret = call ptr @strchr(ptr @s111, i32 %C) 61 ret ptr %ret 62} 63 64 65; Fold strchr(S = "\00\00\00", C) to C == '\0' ? S : null. 66 67define ptr @fold_strchr_s000_C(i32 %C) { 68; CHECK-LABEL: @fold_strchr_s000_C( 69; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 70; CHECK-NEXT: [[MEMCHR_CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 0 71; CHECK-NEXT: [[MEMCHR_SEL:%.*]] = select i1 [[MEMCHR_CHAR0CMP]], ptr @s000, ptr null 72; CHECK-NEXT: ret ptr [[MEMCHR_SEL]] 73; 74 %ret = call ptr @strchr(ptr @s000, i32 %C) 75 ret ptr %ret 76} 77 78 79; Do not fold strchr(S = "\02\01\01\01\01", C). It's transformed to 80; memchr(S, C, 6). 81 82define ptr @xform_strchr_s21111_C(i32 %C) { 83; CHECK-LABEL: @xform_strchr_s21111_C( 84; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) @s21111, i32 [[C:%.*]], i64 6) 85; CHECK-NEXT: ret ptr [[MEMCHR]] 86; 87 %ret = call ptr @strchr(ptr @s21111, i32 %C) 88 ret ptr %ret 89} 90 91 92; Fold strchr(S = "\02\01\01\01\01" + 1, C) to 93; C == '\01' ? S + 1 : C == '\0' ? S + 5 : null. 94 95define ptr @fold_strchr_s21111p1_C(i32 %C) { 96; CHECK-LABEL: @fold_strchr_s21111p1_C( 97; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 98; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 99; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @s21111, i64 5), ptr null 100; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1 101; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], ptr getelementptr inbounds nuw (i8, ptr @s21111, i64 1), ptr [[MEMCHR_SEL1]] 102; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]] 103; 104 %ptr = getelementptr inbounds [6 x i8], ptr @s21111, i64 0, i64 1 105 %ret = call ptr @strchr(ptr %ptr, i32 %C) 106 ret ptr %ret 107} 108 109; Fold strchr(S = "\01\01\01\00\02", C) to 110; C == '\01' ? S : C == '\0' ? S + 3 : null. 111 112define ptr @fold_strchr_s11102_C(i32 %C) { 113; CHECK-LABEL: @fold_strchr_s11102_C( 114; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8 115; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 116; CHECK-NEXT: [[MEMCHR_SEL1:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @s11102, i64 3), ptr null 117; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1 118; CHECK-NEXT: [[MEMCHR_SEL2:%.*]] = select i1 [[TMP3]], ptr @s11102, ptr [[MEMCHR_SEL1]] 119; CHECK-NEXT: ret ptr [[MEMCHR_SEL2]] 120; 121 %ret = call ptr @strchr(ptr @s11102, i32 %C) 122 ret ptr %ret 123} 124