brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 263f9ee Raw
86 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; Verify that strnlen calls with conditional expressions involving constant3; string arguments with nonconstant bounds are folded correctly.4;5; RUN: opt < %s -passes=instcombine -S | FileCheck %s6 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) when16; 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 i8, ptr @s3, i64 [[I:%.*]]21; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @s522; 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 %i27  %sel = select i1 %C, ptr %ptr, ptr @s528  %len = call i64 @strnlen(ptr %sel, i64 %n)29  ret i64 %len30}31 32 33; Do not fold the same expression as above when s3 + i is not guaranteed34; to be within the bounds of s3.  Also verify that the call is not marked35; noundef, nonnull, or dereferenceable because a zero bound implies no36; 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 i8, ptr @s3, i64 [[I:%.*]]41; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @s542; 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 %i47  %sel = select i1 %C, ptr %ptr, ptr @s548  %len = call i64 @strnlen(ptr %sel, i64 %n)49  ret i64 %len50}51 52 53; Do not fold strnlen(C ? s3 + i : sx, n) when sx's length and size54; are unknown.  This also verifies that the folder cleans up the IR after55; successfully folding the first subexpression IR when folding the second56; 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 i8, ptr @s3, i64 [[I:%.*]]61; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], ptr [[PTR]], ptr @sx62; 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 %i67  %sel = select i1 %C, ptr %ptr, ptr @sx68  %len = call i64 @strnlen(ptr %sel, i64 %n)69  ret i64 %len70}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 @s378; 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 @s383  %len = call i64 @strnlen(ptr %ptr, i64 %i)84  ret i64 %len85}86