<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Shows you how often I use the c level routines these days...</p>
<p>Philip<br>
</p>
<div class="moz-cite-prefix">On 8/15/20 3:01 AM, Dávid Bolvanský
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:A8479052-F9E5-46A9-BF3F-35DFFB89ABB4@gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
Right, strncpy does zeroing, so we need to preserve it.<br>
<div dir="ltr"><br>
<blockquote type="cite">Dňa 15. 8. 2020 o 5:19 užívateľ Craig
Topper <a class="moz-txt-link-rfc2396E" href="mailto:craig.topper@gmail.com"><craig.topper@gmail.com></a> napísal:<br>
<br>
</blockquote>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">Doesn't strncpy promise that any bytes past the
end of the source string are zeroed in the destination?
<div><br clear="all">
<div>
<div dir="ltr" class="gmail_signature"
data-smartmail="gmail_signature">~Craig</div>
</div>
<br>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Aug 14, 2020 at
7:55 PM Philip Reames via llvm-commits <<a
href="mailto:llvm-commits@lists.llvm.org"
moz-do-not-send="true">llvm-commits@lists.llvm.org</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">Looking at your
example, I'm confused by the need to pad. In the <br>
examples, both the source string length and C are
constant. If we know <br>
the source doesn't contain an embedded zero, why not just
do a memcpy <br>
for min(Src.length, C)?<br>
<br>
Philip<br>
<br>
On 8/14/20 4:53 PM, Dávid Bolvanský via llvm-commits
wrote:<br>
> Author: Dávid Bolvanský<br>
> Date: 2020-08-15T01:53:32+02:00<br>
> New Revision:
f62de7c9c71134af060a3a1686e30e69d439e785<br>
><br>
> URL: <a
href="https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785"
rel="noreferrer" target="_blank" moz-do-not-send="true">https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785</a><br>
> DIFF: <a
href="https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785.diff"
rel="noreferrer" target="_blank" moz-do-not-send="true">https://github.com/llvm/llvm-project/commit/f62de7c9c71134af060a3a1686e30e69d439e785.diff</a><br>
><br>
> LOG: [SLC] Transform strncpy(dst, "text", C) to
memcpy(dst, "text\0\0\0", C) for C <= 128 only<br>
><br>
> Transformation creates big strings for big C values,
so bail out for C > 128.<br>
><br>
> Reviewed By: efriedma<br>
><br>
> Differential Revision: <a
href="https://reviews.llvm.org/D86004" rel="noreferrer"
target="_blank" moz-do-not-send="true">https://reviews.llvm.org/D86004</a><br>
><br>
> Added:<br>
> <br>
><br>
> Modified:<br>
> llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp<br>
> llvm/test/Transforms/InstCombine/strncpy-3.ll<br>
><br>
> Removed:<br>
> <br>
><br>
><br>
>
################################################################################<br>
> diff --git
a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp<br>
> index 1a7654983e9f..db7078b9283f 100644<br>
> --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp<br>
> +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp<br>
> @@ -610,12 +610,16 @@ Value
*LibCallSimplifier::optimizeStrNCpy(CallInst *CI,
IRBuilderBase &B) {<br>
> <br>
> // strncpy(a, "a", 4) - > memcpy(a, "a\0\0\0",
4)<br>
> if (Len > SrcLen + 1) {<br>
> - StringRef Str;<br>
> - if (!getConstantStringInfo(Src, Str))<br>
> + if (Len <= 128) {<br>
> + StringRef Str;<br>
> + if (!getConstantStringInfo(Src, Str))<br>
> + return nullptr;<br>
> + std::string SrcStr = Str.str();<br>
> + SrcStr.resize(Len, '\0');<br>
> + Src = B.CreateGlobalString(SrcStr, "str");<br>
> + } else {<br>
> return nullptr;<br>
> - std::string SrcStr = Str.str();<br>
> - SrcStr.resize(Len, '\0');<br>
> - Src = B.CreateGlobalString(SrcStr, "str");<br>
> + }<br>
> }<br>
> <br>
> Type *PT =
Callee->getFunctionType()->getParamType(0);<br>
><br>
> diff --git
a/llvm/test/Transforms/InstCombine/strncpy-3.ll
b/llvm/test/Transforms/InstCombine/strncpy-3.ll<br>
> index 744f1e4169e0..28d27a4f3396 100644<br>
> --- a/llvm/test/Transforms/InstCombine/strncpy-3.ll<br>
> +++ b/llvm/test/Transforms/InstCombine/strncpy-3.ll<br>
> @@ -38,3 +38,21 @@ define void @fill_with_zeros3(i8*
%dst) {<br>
> tail call i8* @strncpy(i8* %dst, i8*
getelementptr inbounds ([4 x i8], [4 x i8]* @str3, i64 0,
i64 0), i64 4)<br>
> ret void<br>
> }<br>
> +<br>
> +define void @fill_with_zeros4(i8* %dst) {<br>
> +; CHECK-LABEL: @fill_with_zeros4(<br>
> +; 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)<br>
> +; CHECK-NEXT: ret void<br>
> +;<br>
> + tail call i8* @strncpy(i8* %dst, i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @str3, i64 0, i64 0), i64
128)<br>
> + ret void<br>
> +}<br>
> +<br>
> +define void @no_simplify(i8* %dst) {<br>
> +; CHECK-LABEL: @no_simplify(<br>
> +; 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)<br>
> +; CHECK-NEXT: ret void<br>
> +;<br>
> + tail call i8* @strncpy(i8* %dst, i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @str3, i64 0, i64 0), i64
129)<br>
> + ret void<br>
> +}<br>
><br>
><br>
> <br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org"
target="_blank" moz-do-not-send="true">llvm-commits@lists.llvm.org</a><br>
> <a
href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits"
rel="noreferrer" target="_blank" moz-do-not-send="true">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org"
target="_blank" moz-do-not-send="true">llvm-commits@lists.llvm.org</a><br>
<a
href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits"
rel="noreferrer" target="_blank" moz-do-not-send="true">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote>
</div>
</div>
</blockquote>
</blockquote>
</body>
</html>