[llvm] f62de7c - [SLC] Transform strncpy(dst, "text", C) to memcpy(dst, "text\0\0\0", C) for C <= 128 only

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 20:19:32 PDT 2020


Doesn't strncpy promise that any bytes past the end of the source string
are zeroed in the destination?

~Craig


On Fri, Aug 14, 2020 at 7:55 PM Philip Reames via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Looking at your example, I'm confused by the need to pad.  In the
> examples, both the source string length and C are constant.  If we know
> the source doesn't contain an embedded zero, why not just do a memcpy
> for min(Src.length, C)?
>
> Philip
>
> On 8/14/20 4:53 PM, Dávid Bolvanský via llvm-commits wrote:
> > Author: Dávid Bolvanský
> > Date: 2020-08-15T01:53:32+02:00
> > New Revision: f62de7c9c71134af060a3a1686e30e69d439e785
> >
> > URL:
> https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785
> > DIFF:
> https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785.diff
> >
> > LOG: [SLC] Transform strncpy(dst, "text", C) to memcpy(dst,
> "text\0\0\0", C) for C <= 128 only
> >
> > Transformation creates big strings for big C values, so bail out for C >
> 128.
> >
> > Reviewed By: efriedma
> >
> > Differential Revision: https://reviews.llvm.org/D86004
> >
> > Added:
> >
> >
> > Modified:
> >      llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> >      llvm/test/Transforms/InstCombine/strncpy-3.ll
> >
> > Removed:
> >
> >
> >
> >
> ################################################################################
> > diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> > index 1a7654983e9f..db7078b9283f 100644
> > --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> > +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
> > @@ -610,12 +610,16 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst
> *CI, IRBuilderBase &B) {
> >
> >     // strncpy(a, "a", 4) - > memcpy(a, "a\0\0\0", 4)
> >     if (Len > SrcLen + 1) {
> > -    StringRef Str;
> > -    if (!getConstantStringInfo(Src, Str))
> > +    if (Len <= 128) {
> > +      StringRef Str;
> > +      if (!getConstantStringInfo(Src, Str))
> > +        return nullptr;
> > +      std::string SrcStr = Str.str();
> > +      SrcStr.resize(Len, '\0');
> > +      Src = B.CreateGlobalString(SrcStr, "str");
> > +    } else {
> >         return nullptr;
> > -    std::string SrcStr = Str.str();
> > -    SrcStr.resize(Len, '\0');
> > -    Src = B.CreateGlobalString(SrcStr, "str");
> > +    }
> >     }
> >
> >     Type *PT = Callee->getFunctionType()->getParamType(0);
> >
> > diff  --git a/llvm/test/Transforms/InstCombine/strncpy-3.ll
> b/llvm/test/Transforms/InstCombine/strncpy-3.ll
> > index 744f1e4169e0..28d27a4f3396 100644
> > --- a/llvm/test/Transforms/InstCombine/strncpy-3.ll
> > +++ b/llvm/test/Transforms/InstCombine/strncpy-3.ll
> > @@ -38,3 +38,21 @@ define void @fill_with_zeros3(i8* %dst) {
> >     tail call i8* @strncpy(i8* %dst, i8* getelementptr inbounds ([4 x
> i8], [4 x i8]* @str3, i64 0, i64 0), i64 4)
> >     ret void
> >   }
> > +
> > +define void @fill_with_zeros4(i8* %dst) {
> > +; CHECK-LABEL: @fill_with_zeros4(
> > +; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align
> 1 dereferenceable(128) [[DST:%.*]], i8* nonnull align 1
> dereferenceable(128) getelementptr inbounds ([129 x i8], [129 x i8]*
> @str.2, i64 0, i64 0), i64 128, i1 false)
> > +; 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 128)
> > +  ret void
> > +}
> > +
> > +define void @no_simplify(i8* %dst) {
> > +; CHECK-LABEL: @no_simplify(
> > +; CHECK-NEXT:    [[TMP1:%.*]] = tail call i8* @strncpy(i8* nonnull
> dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(5)
> getelementptr inbounds ([4 x i8], [4 x i8]* @str3, i64 0, i64 0), i64 129)
> > +; 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 129)
> > +  ret void
> > +}
> >
> >
> >
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at lists.llvm.org
> > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200814/6b5d67d8/attachment.html>


More information about the llvm-commits mailing list