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