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 memchr calls with constant arrays, or constant characters, 5; or constant bounds are folded (or not) as expected. 6 7declare ptr @memchr(ptr, i32, i64) 8 9@ax = external global [0 x i8] 10@a12345 = constant [5 x i8] c"\01\02\03\04\05" 11@a123f45 = constant [5 x i8] c"\01\02\03\f4\05" 12 13 14; Fold memchr(a12345, '\06', n) to null. 15 16define ptr @fold_memchr_a12345_6_n(i64 %n) { 17; CHECK-LABEL: @fold_memchr_a12345_6_n( 18; CHECK-NEXT: ret ptr null 19; 20 21 %res = call ptr @memchr(ptr @a12345, i32 6, i64 %n) 22 ret ptr %res 23} 24 25 26; Fold memchr(a12345, '\04', 2) to null. 27 28define ptr @fold_memchr_a12345_4_2() { 29; CHECK-LABEL: @fold_memchr_a12345_4_2( 30; CHECK-NEXT: ret ptr null 31; 32 33 %res = call ptr @memchr(ptr @a12345, i32 4, i64 2) 34 ret ptr %res 35} 36 37 38; Fold memchr(a12345, '\04', 3) to null. 39 40define ptr @fold_memchr_a12345_4_3() { 41; CHECK-LABEL: @fold_memchr_a12345_4_3( 42; CHECK-NEXT: ret ptr null 43; 44 45 %res = call ptr @memchr(ptr @a12345, i32 4, i64 3) 46 ret ptr %res 47} 48 49 50; Fold memchr(a12345, '\03', 3) to a12345 + 2. 51 52define ptr @fold_memchr_a12345_3_3() { 53; CHECK-LABEL: @fold_memchr_a12345_3_3( 54; CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @a12345, i64 2) 55; 56 57 %res = call ptr @memchr(ptr @a12345, i32 3, i64 3) 58 ret ptr %res 59} 60 61 62; Fold memchr(a12345, '\03', 9) to a12345 + 2. 63 64define ptr @fold_memchr_a12345_3_9() { 65; CHECK-LABEL: @fold_memchr_a12345_3_9( 66; CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @a12345, i64 2) 67; 68 69 %res = call ptr @memchr(ptr @a12345, i32 3, i64 9) 70 ret ptr %res 71} 72 73 74; Fold memchr(a123f45, 500, 9) to a123f45 + 3 (verify that 500 is 75; truncated to (unsigned char)500 == '\xf4') 76 77define ptr @fold_memchr_a123f45_500_9() { 78; CHECK-LABEL: @fold_memchr_a123f45_500_9( 79; CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @a123f45, i64 3) 80; 81 82 %res = call ptr @memchr(ptr @a123f45, i32 500, i64 9) 83 ret ptr %res 84} 85 86 87; Fold memchr(a12345, '\03', n) to n < 3 ? null : a12345 + 2. 88 89define ptr @fold_a12345_3_n(i64 %n) { 90; CHECK-LABEL: @fold_a12345_3_n( 91; CHECK-NEXT: [[MEMCHR_CMP:%.*]] = icmp ult i64 [[N:%.*]], 3 92; CHECK-NEXT: [[RES:%.*]] = select i1 [[MEMCHR_CMP]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a12345, i64 2) 93; CHECK-NEXT: ret ptr [[RES]] 94; 95 96 %res = call ptr @memchr(ptr @a12345, i32 3, i64 %n) 97 ret ptr %res 98} 99 100 101; Fold memchr(a12345, 259, n) to n < 3 ? null : a12345 + 2 102; to verify the constant 259 is converted to unsigned char (yielding 3). 103 104define ptr @fold_a12345_259_n(i64 %n) { 105; CHECK-LABEL: @fold_a12345_259_n( 106; CHECK-NEXT: [[MEMCHR_CMP:%.*]] = icmp ult i64 [[N:%.*]], 3 107; CHECK-NEXT: [[RES:%.*]] = select i1 [[MEMCHR_CMP]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a12345, i64 2) 108; CHECK-NEXT: ret ptr [[RES]] 109; 110 111 %res = call ptr @memchr(ptr @a12345, i32 259, i64 %n) 112 ret ptr %res 113} 114 115 116; Do no fold memchr(ax, 1, n). 117 118define ptr @call_ax_1_n(i64 %n) { 119; CHECK-LABEL: @call_ax_1_n( 120; CHECK-NEXT: [[RES:%.*]] = call ptr @memchr(ptr nonnull @ax, i32 1, i64 [[N:%.*]]) 121; CHECK-NEXT: ret ptr [[RES]] 122; 123 124 %res = call ptr @memchr(ptr @ax, i32 1, i64 %n) 125 ret ptr %res 126} 127