309 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; Verify that calls to memchr with arrays of elements larger than char3; are folded correctly.4; RUN: opt < %s -passes=instcombine -S -data-layout="E" | FileCheck %s --check-prefixes=CHECK,BE-CHECK5; RUN: opt < %s -passes=instcombine -S -data-layout="e" | FileCheck %s --check-prefixes=CHECK,LE-CHECK6;7; Exercise folding of memchr calls with addition expressions involving8; pointers into constant arrays of types larger than char and fractional9; offsets.10 11declare ptr @memchr(ptr, i32, i64)12 13%struct.A = type { [2 x i16], [2 x i16] }14 15; Hex byte representation: 00 00 01 01 02 02 03 0316@a = constant [1 x %struct.A] [%struct.A { [2 x i16] [i16 0, i16 257], [2 x i16] [i16 514, i16 771] }]17 18 19define void @fold_memchr_A_pIb_cst_cst(ptr %pchr) {20; CHECK-LABEL: @fold_memchr_A_pIb_cst_cst(21; CHECK-NEXT: store ptr @a, ptr [[PCHR:%.*]], align 822; CHECK-NEXT: [[PST_0_1_1:%.*]] = getelementptr i8, ptr [[PCHR]], i64 823; CHECK-NEXT: store ptr null, ptr [[PST_0_1_1]], align 824; CHECK-NEXT: [[PST_0_4_4:%.*]] = getelementptr i8, ptr [[PCHR]], i64 1625; CHECK-NEXT: store ptr null, ptr [[PST_0_4_4]], align 826; CHECK-NEXT: [[PST_1_0_1:%.*]] = getelementptr i8, ptr [[PCHR]], i64 2427; CHECK-NEXT: store ptr getelementptr inbounds nuw (i8, ptr @a, i64 1), ptr [[PST_1_0_1]], align 828; CHECK-NEXT: [[PST_1_0_3:%.*]] = getelementptr i8, ptr [[PCHR]], i64 3229; CHECK-NEXT: store ptr getelementptr inbounds nuw (i8, ptr @a, i64 1), ptr [[PST_1_0_3]], align 830; CHECK-NEXT: [[PST_1_1_1:%.*]] = getelementptr i8, ptr [[PCHR]], i64 4031; CHECK-NEXT: store ptr null, ptr [[PST_1_1_1]], align 832; CHECK-NEXT: [[PST_1_1_2:%.*]] = getelementptr i8, ptr [[PCHR]], i64 4833; CHECK-NEXT: store ptr getelementptr inbounds nuw (i8, ptr @a, i64 2), ptr [[PST_1_1_2]], align 834; CHECK-NEXT: [[PST_1_3_3:%.*]] = getelementptr i8, ptr [[PCHR]], i64 5635; CHECK-NEXT: store ptr null, ptr [[PST_1_3_3]], align 836; CHECK-NEXT: [[PST_1_3_4:%.*]] = getelementptr i8, ptr [[PCHR]], i64 6437; CHECK-NEXT: store ptr null, ptr [[PST_1_3_4]], align 838; CHECK-NEXT: [[PST_1_3_6:%.*]] = getelementptr i8, ptr [[PCHR]], i64 8039; CHECK-NEXT: store ptr getelementptr inbounds nuw (i8, ptr @a, i64 6), ptr [[PST_1_3_6]], align 840; CHECK-NEXT: ret void41;42 43 44 ; Fold memchr((char*)a + 0, '\0', 1) to a.45 %chr_0_0_1 = call ptr @memchr(ptr @a, i32 0, i64 1)46 store ptr %chr_0_0_1, ptr %pchr47 48 ; Fold memchr((char*)a + 0, '\01', 1) to null.49 %pst_0_1_1 = getelementptr ptr, ptr %pchr, i32 150 %chr_0_1_1 = call ptr @memchr(ptr @a, i32 1, i64 1)51 store ptr %chr_0_1_1, ptr %pst_0_1_152 53 ; Fold memchr((char*)a + 0, '\04', 4) to null.54 %pst_0_4_4 = getelementptr ptr, ptr %pchr, i32 255 %chr_0_4_4 = call ptr @memchr(ptr @a, i32 4, i64 4)56 store ptr %chr_0_4_4, ptr %pst_0_4_457 58 59 %pi8ap1 = getelementptr i8, ptr @a, i32 160 61 ; Fold memchr((char*)a + 1, '\0', 1) to (char*)a + 1.62 %pst_1_0_1 = getelementptr ptr, ptr %pchr, i32 363 %chr_1_0_1 = call ptr @memchr(ptr %pi8ap1, i32 0, i64 1)64 store ptr %chr_1_0_1, ptr %pst_1_0_165 66 ; Fold memchr((char*)a + 1, '\0', 3) to (char*)a + 1.67 %pst_1_0_3 = getelementptr ptr, ptr %pchr, i32 468 %chr_1_0_3 = call ptr @memchr(ptr %pi8ap1, i32 0, i64 3)69 store ptr %chr_1_0_3, ptr %pst_1_0_370 71 ; Fold memchr((char*)a + 1, '\01', 1) to null.72 %pst_1_1_1 = getelementptr ptr, ptr %pchr, i32 573 %chr_1_1_1 = call ptr @memchr(ptr %pi8ap1, i32 1, i64 1)74 store ptr %chr_1_1_1, ptr %pst_1_1_175 76 ; Fold memchr((char*)a + 1, '\01', 2) to (char*)a + 2.77 %pst_1_1_2 = getelementptr ptr, ptr %pchr, i32 678 %chr_1_1_2 = call ptr @memchr(ptr %pi8ap1, i32 1, i64 2)79 store ptr %chr_1_1_2, ptr %pst_1_1_280 81 ; Fold memchr((char*)a + 1, '\03', 3) to null.82 %pst_1_3_3 = getelementptr ptr, ptr %pchr, i32 783 %chr_1_3_3 = call ptr @memchr(ptr %pi8ap1, i32 3, i64 3)84 store ptr %chr_1_3_3, ptr %pst_1_3_385 86 ; Fold memchr((char*)a + 1, '\03', 4) to null.87 %pst_1_3_4 = getelementptr ptr, ptr %pchr, i32 888 %chr_1_3_4 = call ptr @memchr(ptr %pi8ap1, i32 3, i64 4)89 store ptr %chr_1_3_4, ptr %pst_1_3_490 91 ; Fold memchr((char*)a + 1, '\03', 5) to null.92 %pst_1_3_5 = getelementptr ptr, ptr %pchr, i32 993 %chr_1_3_5 = call ptr @memchr(ptr %pi8ap1, i32 3, i64 5)94 store ptr %chr_1_3_4, ptr %pst_1_3_495 96 ; Fold memchr((char*)a + 1, '\03', 6) to (char*)a + 5.97 %pst_1_3_6 = getelementptr ptr, ptr %pchr, i32 1098 %chr_1_3_6 = call ptr @memchr(ptr %pi8ap1, i32 3, i64 6)99 store ptr %chr_1_3_6, ptr %pst_1_3_6100 101 102 ret void103}104 105 106define void @fold_memchr_A_pIb_cst_N(i64 %N, ptr %pchr) {107; CHECK-LABEL: @fold_memchr_A_pIb_cst_N(108; CHECK-NEXT: [[MEMCHR_CMP:%.*]] = icmp eq i64 [[N:%.*]], 0109; CHECK-NEXT: [[CHR_0_0_N:%.*]] = select i1 [[MEMCHR_CMP]], ptr null, ptr @a110; CHECK-NEXT: store ptr [[CHR_0_0_N]], ptr [[PCHR:%.*]], align 8111; CHECK-NEXT: [[PST_0_1_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 8112; CHECK-NEXT: [[MEMCHR_CMP1:%.*]] = icmp ult i64 [[N]], 3113; CHECK-NEXT: [[CHR_0_1_N:%.*]] = select i1 [[MEMCHR_CMP1]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 2)114; CHECK-NEXT: store ptr [[CHR_0_1_N]], ptr [[PST_0_1_N]], align 8115; CHECK-NEXT: [[PST_0_4_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 16116; CHECK-NEXT: store ptr null, ptr [[PST_0_4_N]], align 8117; CHECK-NEXT: [[PST_1_0_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 24118; CHECK-NEXT: [[MEMCHR_CMP2:%.*]] = icmp eq i64 [[N]], 0119; CHECK-NEXT: [[CHR_1_0_N:%.*]] = select i1 [[MEMCHR_CMP2]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 1)120; CHECK-NEXT: store ptr [[CHR_1_0_N]], ptr [[PST_1_0_N]], align 8121; CHECK-NEXT: [[PST_1_1_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 32122; CHECK-NEXT: [[MEMCHR_CMP3:%.*]] = icmp ult i64 [[N]], 2123; CHECK-NEXT: [[CHR_1_1_N:%.*]] = select i1 [[MEMCHR_CMP3]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 2)124; CHECK-NEXT: store ptr [[CHR_1_1_N]], ptr [[PST_1_1_N]], align 8125; CHECK-NEXT: [[PST_1_2_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 40126; CHECK-NEXT: [[MEMCHR_CMP4:%.*]] = icmp ult i64 [[N]], 4127; CHECK-NEXT: [[CHR_1_2_N:%.*]] = select i1 [[MEMCHR_CMP4]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 4)128; CHECK-NEXT: store ptr [[CHR_1_2_N]], ptr [[PST_1_2_N]], align 8129; CHECK-NEXT: [[PST_1_3_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 48130; CHECK-NEXT: [[MEMCHR_CMP5:%.*]] = icmp ult i64 [[N]], 6131; CHECK-NEXT: [[CHR_1_3_N:%.*]] = select i1 [[MEMCHR_CMP5]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 6)132; CHECK-NEXT: store ptr [[CHR_1_3_N]], ptr [[PST_1_3_N]], align 8133; CHECK-NEXT: [[PST_1_4_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 56134; CHECK-NEXT: store ptr null, ptr [[PST_1_4_N]], align 8135; CHECK-NEXT: [[PST_2_0_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 64136; CHECK-NEXT: store ptr null, ptr [[PST_2_0_N]], align 8137; CHECK-NEXT: [[PST_2_1_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 72138; CHECK-NEXT: [[MEMCHR_CMP6:%.*]] = icmp eq i64 [[N]], 0139; CHECK-NEXT: [[CHR_2_1_N:%.*]] = select i1 [[MEMCHR_CMP6]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 2)140; CHECK-NEXT: store ptr [[CHR_2_1_N]], ptr [[PST_2_1_N]], align 8141; CHECK-NEXT: [[PST_2_2_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 80142; CHECK-NEXT: [[MEMCHR_CMP7:%.*]] = icmp ult i64 [[N]], 3143; CHECK-NEXT: [[CHR_2_2_N:%.*]] = select i1 [[MEMCHR_CMP7]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 4)144; CHECK-NEXT: store ptr [[CHR_2_2_N]], ptr [[PST_2_2_N]], align 8145; CHECK-NEXT: [[PST_2_3_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 88146; CHECK-NEXT: [[MEMCHR_CMP8:%.*]] = icmp ult i64 [[N]], 5147; CHECK-NEXT: [[CHR_2_3_N:%.*]] = select i1 [[MEMCHR_CMP8]], ptr null, ptr getelementptr inbounds nuw (i8, ptr @a, i64 6)148; CHECK-NEXT: store ptr [[CHR_2_3_N]], ptr [[PST_2_3_N]], align 8149; CHECK-NEXT: [[PST_2_4_N:%.*]] = getelementptr i8, ptr [[PCHR]], i64 96150; CHECK-NEXT: store ptr null, ptr [[PST_2_4_N]], align 8151; CHECK-NEXT: ret void152;153 154 155 ; Fold memchr((char*)a + 0, '\0', N) to N ? a : null.156 %chr_0_0_n = call ptr @memchr(ptr @a, i32 0, i64 %N)157 store ptr %chr_0_0_n, ptr %pchr158 159 ; Fold memchr((char*)a + 0, '\01', N) to N < 2 ? null : a.160 %pst_0_1_n = getelementptr ptr, ptr %pchr, i32 1161 %chr_0_1_n = call ptr @memchr(ptr @a, i32 1, i64 %N)162 store ptr %chr_0_1_n, ptr %pst_0_1_n163 164 ; Fold memchr((char*)a + 0, '\04', N) to null.165 %pst_0_4_n = getelementptr ptr, ptr %pchr, i32 2166 %chr_0_4_n = call ptr @memchr(ptr @a, i32 4, i64 %N)167 store ptr %chr_0_4_n, ptr %pst_0_4_n168 169 170 %pi8ap1 = getelementptr i8, ptr @a, i32 1171 172 ; Fold memchr((char*)a + 1, '\0', N) to null.173 %pst_1_0_n = getelementptr ptr, ptr %pchr, i32 3174 %chr_1_0_n = call ptr @memchr(ptr %pi8ap1, i32 0, i64 %N)175 store ptr %chr_1_0_n, ptr %pst_1_0_n176 177 ; Fold memchr((char*)a + 1, '\01', N) N ? (char*)a + 1 : null.178 %pst_1_1_n = getelementptr ptr, ptr %pchr, i32 4179 %chr_1_1_n = call ptr @memchr(ptr %pi8ap1, i32 1, i64 %N)180 store ptr %chr_1_1_n, ptr %pst_1_1_n181 182 ; Fold memchr((char*)a + 1, '\02', N) to N < 2 ? null : (char*)a + 4.183 %pst_1_2_n = getelementptr ptr, ptr %pchr, i32 5184 %chr_1_2_n = call ptr @memchr(ptr %pi8ap1, i32 2, i64 %N)185 store ptr %chr_1_2_n, ptr %pst_1_2_n186 187 ; Fold memchr((char*)a + 1, '\03', N) to N < 6 ? null : (char*)a + 6.188 %pst_1_3_n = getelementptr ptr, ptr %pchr, i32 6189 %chr_1_3_n = call ptr @memchr(ptr %pi8ap1, i32 3, i64 %N)190 store ptr %chr_1_3_n, ptr %pst_1_3_n191 192 ; Fold memchr((char*)a + 1, '\04', N) to null.193 %pst_1_4_n = getelementptr ptr, ptr %pchr, i32 7194 %chr_1_4_n = call ptr @memchr(ptr %pi8ap1, i32 4, i64 %N)195 store ptr %chr_1_4_n, ptr %pst_1_4_n196 197 198 %pi8ap2 = getelementptr i8, ptr @a, i32 2199 200 ; Fold memchr((char*)a + 2, '\0', N) to null.201 %pst_2_0_n = getelementptr ptr, ptr %pchr, i32 8202 %chr_2_0_n = call ptr @memchr(ptr %pi8ap2, i32 0, i64 %N)203 store ptr %chr_2_0_n, ptr %pst_2_0_n204 205 ; Fold memchr((char*)a + 2, '\01', N) N ? (char*)a + 2 : null.206 %pst_2_1_n = getelementptr ptr, ptr %pchr, i32 9207 %chr_2_1_n = call ptr @memchr(ptr %pi8ap2, i32 1, i64 %N)208 store ptr %chr_2_1_n, ptr %pst_2_1_n209 210 ; Fold memchr((char*)a + 2, '\02', N) to N < 3 ? null : (char*)a + 2.211 %pst_2_2_n = getelementptr ptr, ptr %pchr, i32 10212 %chr_2_2_n = call ptr @memchr(ptr %pi8ap2, i32 2, i64 %N)213 store ptr %chr_2_2_n, ptr %pst_2_2_n214 215 ; Fold memchr((char*)a + 2, '\03', N) to N < 5 ? null : (char*)a + 4.216 %pst_2_3_n = getelementptr ptr, ptr %pchr, i32 11217 %chr_2_3_n = call ptr @memchr(ptr %pi8ap2, i32 3, i64 %N)218 store ptr %chr_2_3_n, ptr %pst_2_3_n219 220 ; Fold memchr((char*)a + 2, '\04', N) to null.221 %pst_2_4_n = getelementptr ptr, ptr %pchr, i32 12222 %chr_2_4_n = call ptr @memchr(ptr %pi8ap2, i32 4, i64 %N)223 store ptr %chr_2_4_n, ptr %pst_2_4_n224 225 ret void226}227 228 229; Verify that calls with out of bounds offsets are not folded.230 231define void @call_memchr_A_pIb_xs_cst(ptr %pchr) {232; CHECK-LABEL: @call_memchr_A_pIb_xs_cst(233; CHECK-NEXT: [[CHR_1_0_0_2:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds nuw (i8, ptr @a, i64 8), i32 0, i64 2)234; CHECK-NEXT: store ptr [[CHR_1_0_0_2]], ptr [[PCHR:%.*]], align 8235; CHECK-NEXT: [[PST_1_0_1_2:%.*]] = getelementptr i8, ptr [[PCHR]], i64 8236; CHECK-NEXT: [[CHR_1_0_1_2:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds nuw (i8, ptr @a, i64 8), i32 0, i64 2)237; CHECK-NEXT: store ptr [[CHR_1_0_1_2]], ptr [[PST_1_0_1_2]], align 8238; CHECK-NEXT: [[PST_0_0_8_2:%.*]] = getelementptr i8, ptr [[PCHR]], i64 16239; CHECK-NEXT: [[CHR_0_0_8_2:%.*]] = call ptr @memchr(ptr noundef nonnull dereferenceable(1) getelementptr inbounds nuw (i8, ptr @a, i64 8), i32 0, i64 2)240; CHECK-NEXT: store ptr [[CHR_0_0_8_2]], ptr [[PST_0_0_8_2]], align 8241; CHECK-NEXT: ret void242;243; Verify that the call isn't folded when the first GEP index is excessive.244 %pa1 = getelementptr [1 x %struct.A], ptr @a, i64 1, i64 0245 246 247 ; Don't fold memchr((char*)&a[1] + 0, '\0', 2).248 %chr_1_0_0_2 = call ptr @memchr(ptr %pa1, i32 0, i64 2)249 store ptr %chr_1_0_0_2, ptr %pchr250 251 %pi8a1p1 = getelementptr i8, ptr %pa1, i32 1252 253 ; Likewise, don't fold memchr((char*)&a[1] + 1, '\0', 2).254 %pst_1_0_1_2 = getelementptr ptr, ptr %pchr, i32 1255 %chr_1_0_1_2 = call ptr @memchr(ptr %pa1, i32 0, i64 2)256 store ptr %chr_1_0_1_2, ptr %pst_1_0_1_2257 258; Verify that the call isn't folded when the first GEP index is in bounds259; but the byte offset is excessive.260 261 %pi8a0p8 = getelementptr i8, ptr @a, i32 8262 263 ; Don't fold memchr((char*)&a[0] + 8, '\0', 2).264 %pst_0_0_8_2 = getelementptr ptr, ptr %pchr, i32 2265 %chr_0_0_8_2 = call ptr @memchr(ptr %pi8a0p8, i32 0, i64 2)266 store ptr %chr_0_0_8_2, ptr %pst_0_0_8_2267 268 ret void269}270 271 272@ai64 = constant [2 x i64] [i64 0, i64 -1]273 274; Verify that a memchr call with an argument consisting of three GEPs275; is folded.276 277define ptr @fold_memchr_gep_gep_gep() {278; CHECK-LABEL: @fold_memchr_gep_gep_gep(279; CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @ai64, i64 14)280;281 282 %p8_1 = getelementptr [2 x i64], ptr @ai64, i64 0, i64 1283 %p4_1 = getelementptr i32, ptr %p8_1, i64 1284 285 %p2_1 = getelementptr i16, ptr %p4_1, i64 1286 287 %pc = call ptr @memchr(ptr %p2_1, i32 -1, i64 2)288 ret ptr %pc289}290 291 292%union.U = type { [2 x i32] }293 294@u = constant %union.U { [2 x i32] [i32 286331153, i32 35791394] }295 296; Verify memchr folding of a union member.297 298define ptr @fold_memchr_union_member() {299; BE-CHECK-LABEL: @fold_memchr_union_member(300; BE-CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @u, i64 5)301;302; LE-CHECK-LABEL: @fold_memchr_union_member(303; LE-CHECK-NEXT: ret ptr getelementptr inbounds nuw (i8, ptr @u, i64 4)304;305 %pi8u_p1 = getelementptr i8, ptr @u, i64 1306 %pc = call ptr @memchr(ptr %pi8u_p1, i32 34, i64 8)307 ret ptr %pc308}309