[PATCH] D86004: [SLC] Transform strncpy(dst, "text", C) to memcpy(dst, "text\0\0\0", C) for C <= 128 only

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 16:53:51 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGf62de7c9c711: [SLC] Transform strncpy(dst, "text", C) to memcpy(dst, "text\0\0\0", C) for C… (authored by xbolva00).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D86004/new/

https://reviews.llvm.org/D86004

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/strncpy-3.ll


Index: llvm/test/Transforms/InstCombine/strncpy-3.ll
===================================================================
--- llvm/test/Transforms/InstCombine/strncpy-3.ll
+++ llvm/test/Transforms/InstCombine/strncpy-3.ll
@@ -38,3 +38,21 @@
   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
+}
Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -610,12 +610,16 @@
 
   // 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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86004.285792.patch
Type: text/x-patch
Size: 2137 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200814/493a6287/attachment.bin>


More information about the llvm-commits mailing list