1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Verify that strnlen calls with conditional expressions involving constant 3; string arguments with nonconstant bounds are folded correctly. 4; 5; RUN: opt < %s -passes=instcombine -S | FileCheck %s 6 7declare i64 @strnlen(ptr, i64) 8 9@sx = external global [0 x i8] 10@s3 = constant [4 x i8] c"123\00" 11@s5 = constant [6 x i8] c"12345\00" 12@s5_3 = constant [10 x i8] c"12345\00abc\00" 13 14 15; Fold strnlen (C ? s3 + i : s5, %n) to min(C ? 3 : 5, i) when 16; s3 + i is guaranteed to be within the bounds of s3. 17 18define i64 @fold_strnlen_s3_pi_s5_n(i1 %C, i64 %i, i64 %n) { 19; CHECK-LABEL: @fold_strnlen_s3_pi_s5_n( 20; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds [4 x i8], ptr @s3, i64 0, i64 [[I:%.*]] 21; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @s5 22; CHECK-NEXT: [[LEN:%.*]] = call i64 @strnlen(ptr nonnull [[SEL]], i64 [[N:%.*]]) 23; CHECK-NEXT: ret i64 [[LEN]] 24; 25 26 %ptr = getelementptr inbounds [4 x i8], ptr @s3, i64 0, i64 %i 27 %sel = select i1 %C, ptr %ptr, ptr @s5 28 %len = call i64 @strnlen(ptr %sel, i64 %n) 29 ret i64 %len 30} 31 32 33; Do not fold the same expression as above when s3 + i is not guaranteed 34; to be within the bounds of s3. Also verify that the call is not marked 35; noundef, nonnull, or dereferenceable because a zero bound implies no 36; access. 37 38define i64 @call_strnlen_s3_pi_xbounds_s5_n(i1 %C, i64 %i, i64 %n) { 39; CHECK-LABEL: @call_strnlen_s3_pi_xbounds_s5_n( 40; CHECK-NEXT: [[PTR:%.*]] = getelementptr [4 x i8], ptr @s3, i64 0, i64 [[I:%.*]] 41; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @s5 42; CHECK-NEXT: [[LEN:%.*]] = call i64 @strnlen(ptr [[SEL]], i64 [[N:%.*]]) 43; CHECK-NEXT: ret i64 [[LEN]] 44; 45 46 %ptr = getelementptr [4 x i8], ptr @s3, i64 0, i64 %i 47 %sel = select i1 %C, ptr %ptr, ptr @s5 48 %len = call i64 @strnlen(ptr %sel, i64 %n) 49 ret i64 %len 50} 51 52 53; Do not fold strnlen(C ? s3 + i : sx, n) when sx's length and size 54; are unknown. This also verifies that the folder cleans up the IR after 55; successfully folding the first subexpression IR when folding the second 56; subexpression fails. 57 58define i64 @call_strnlen_s3_pi_sx_n(i1 %C, i64 %i, i64 %n) { 59; CHECK-LABEL: @call_strnlen_s3_pi_sx_n( 60; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds [4 x i8], ptr @s3, i64 0, i64 [[I:%.*]] 61; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @sx 62; CHECK-NEXT: [[LEN:%.*]] = call i64 @strnlen(ptr nonnull [[SEL]], i64 [[N:%.*]]) 63; CHECK-NEXT: ret i64 [[LEN]] 64; 65 66 %ptr = getelementptr inbounds [4 x i8], ptr @s3, i64 0, i64 %i 67 %sel = select i1 %C, ptr %ptr, ptr @sx 68 %len = call i64 @strnlen(ptr %sel, i64 %n) 69 ret i64 %len 70} 71 72 73; Fold strnlen (C ? s3 : s5 + i, n) to min(C ? 3 : 5, i). 74 75define i64 @fold_strnlen_s3_s5_pi_n(i1 %C, i64 %i, i64 %n) { 76; CHECK-LABEL: @fold_strnlen_s3_s5_pi_n( 77; CHECK-NEXT: [[PTR:%.*]] = select i1 [[C:%.*]], ptr @s5, ptr @s3 78; CHECK-NEXT: [[LEN:%.*]] = call i64 @strnlen(ptr nonnull [[PTR]], i64 [[I:%.*]]) 79; CHECK-NEXT: ret i64 [[LEN]] 80; 81 82 %ptr = select i1 %C, ptr @s5, ptr @s3 83 %len = call i64 @strnlen(ptr %ptr, i64 %i) 84 ret i64 %len 85} 86