[llvm] 5ef2287 - [SLC] Optimize strncpy(a, a, C) to memcpy(a, a000, C)

Dávid Bolvanský via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 09:06:45 PDT 2020


>> We probably want to limit the number of null characters we append here; if the length is long, we could generate an excessively long global.
Any idea about reasonable limit? 32?

>> The strncpy call in fill_with_zeros2 has undefined behavior; is that intentional?
Yes.

pi 14. 8. 2020 o 17:47 Eli Friedman <efriedma at quicinc.com> napísal(a):
>
> We probably want to limit the number of null characters we append here; if the length is long, we could generate an excessively long global.
>
> The strncpy call in fill_with_zeros2 has undefined behavior; is that intentional?
>
> -Eli
>
> -----Original Message-----
> From: llvm-commits <llvm-commits-bounces at lists.llvm.org> On Behalf Of Dávid Bolvanský via llvm-commits
> Sent: Thursday, August 13, 2020 1:23 PM
> To: llvm-commits at lists.llvm.org
> Subject: [EXT] [llvm] 5ef2287 - [SLC] Optimize strncpy(a, a, C) to memcpy(a, a000, C)
>
>
> Author: Dávid Bolvanský
> Date: 2020-08-13T22:22:51+02:00
> New Revision: 5ef2287d36f93bac3b7b8c49c1b371d25d305e85
>
> URL: https://github.com/llvm/llvm-project/commit/5ef2287d36f93bac3b7b8c49c1b371d25d305e85
> DIFF: https://github.com/llvm/llvm-project/commit/5ef2287d36f93bac3b7b8c49c1b371d25d305e85.diff
>
> LOG: [SLC] Optimize strncpy(a, a, C) to memcpy(a, a000, C)
> Solves PR47154
>
> Added:
>     llvm/test/Transforms/InstCombine/strncpy-3.ll
>
> Modified:
>     llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
>
> Removed:
>
>
>
> ################################################################################
> diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> index b03389aec7bf..2f6e60fc09c4 100644
> --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> @@ -608,9 +608,15 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilderBase &B) {
>      return Dst;
>    }
>
> -  // Let strncpy handle the zero padding
> -  if (Len > SrcLen + 1)
> -    return nullptr;
> +  // strncpy(a, "a", 4) - > memcpy(a, "a\0\0\0", 4)
> +  if (Len > SrcLen + 1) {
> +    StringRef Str;
> +    if (!getConstantStringInfo(Src, Str))
> +      return nullptr;
> +    std::string SrcStr = Str.str();
> +    SrcStr.resize(Len, '\0');
> +    Src = B.CreateGlobalString(SrcStr, "str");
> +  }
>
>    Type *PT = Callee->getFunctionType()->getParamType(0);
>    // strncpy(x, s, c) -> memcpy(align 1 x, align 1 s, c) [s and c are constant]
>
> diff  --git a/llvm/test/Transforms/InstCombine/strncpy-3.ll b/llvm/test/Transforms/InstCombine/strncpy-3.ll
> new file mode 100644
> index 000000000000..744f1e4169e0
> --- /dev/null
> +++ b/llvm/test/Transforms/InstCombine/strncpy-3.ll
> @@ -0,0 +1,40 @@
> +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
> +; RUN: opt < %s -instcombine -S | FileCheck %s
> +
> +
> + at str = constant [2 x i8] c"a\00"
> + at str2 = constant [3 x i8] c"abc"
> + at str3 = constant [4 x i8] c"abcd"
> +
> +declare i8* @strncpy(i8*, i8*, i64)
> +
> +
> +define void @fill_with_zeros(i8* %dst) {
> +; CHECK-LABEL: @fill_with_zeros(
> +; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i32*
> +; CHECK-NEXT:    store i32 97, i32* [[TMP1]], align 1
> +; CHECK-NEXT:    ret void
> +;
> +  tail call i8* @strncpy(i8* %dst, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0), i64 4)
> +  ret void
> +}
> +
> +define void @fill_with_zeros2(i8* %dst) {
> +; CHECK-LABEL: @fill_with_zeros2(
> +; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i32*
> +; CHECK-NEXT:    store i32 6513249, i32* [[TMP1]], align 1
> +; CHECK-NEXT:    ret void
> +;
> +  tail call i8* @strncpy(i8* %dst, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str2, i64 0, i64 0), i64 4)
> +  ret void
> +}
> +
> +define void @fill_with_zeros3(i8* %dst) {
> +; CHECK-LABEL: @fill_with_zeros3(
> +; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i8* [[DST:%.*]] to i32*
> +; CHECK-NEXT:    store i32 1684234849, i32* [[TMP1]], align 1
> +; CHECK-NEXT:    ret void
> +;
> +  tail call i8* @strncpy(i8* %dst, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @str3, i64 0, i64 0), i64 4)
> +  ret void
> +}
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list