Skip to content

[InstCombine] Don't add extra 0 to string in str[np]cpy optimization #101884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2024

Conversation

s-barannikov
Copy link
Contributor

It is unused by subsequent memcpy.

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Sergei Barannikov (s-barannikov)

Changes

It is unused by subsequent memcpy.


Full diff: https://github.com/llvm/llvm-project/pull/101884.diff

4 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+2-1)
  • (modified) llvm/test/Transforms/InstCombine/stpncpy-1.ll (+4-4)
  • (modified) llvm/test/Transforms/InstCombine/strncpy-1.ll (+15-1)
  • (modified) llvm/test/Transforms/InstCombine/strncpy-3.ll (+11-1)
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index ded9209cfe249..673cc1a6c08f7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -910,7 +910,8 @@ Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
     // Create a bigger, nul-padded array with the same length, SrcLen,
     // as the original string.
     SrcStr.resize(N, '\0');
-    Src = B.CreateGlobalString(SrcStr, "str");
+    Src = B.CreateGlobalString(SrcStr, "str", /*AddressSpace=*/0,
+                               /*M=*/nullptr, /*AddNull=*/false);
   }
 
   Type *PT = Callee->getFunctionType()->getParamType(0);
diff --git a/llvm/test/Transforms/InstCombine/stpncpy-1.ll b/llvm/test/Transforms/InstCombine/stpncpy-1.ll
index 0a4caa2c05f93..ddfc1c086347f 100644
--- a/llvm/test/Transforms/InstCombine/stpncpy-1.ll
+++ b/llvm/test/Transforms/InstCombine/stpncpy-1.ll
@@ -36,10 +36,10 @@ declare void @sink(ptr, ptr)
 ; ANY: @str.3 = private unnamed_addr constant [4 x i8] c"4\00\00\00", align 1
 ; ANY: @str.4 = private unnamed_addr constant [10 x i8] c"4\00\00\00\00\00\00\00\00\00", align 1
 ; ANY: @str.5 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
-; ANY: @str.6 = private unnamed_addr constant [4 x i8] c"4\00\00\00", align 1
-; ANY: @str.7 = private unnamed_addr constant [10 x i8] c"4\00\00\00\00\00\00\00\00\00", align 1
-; ANY: @str.8 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
-; ANY: @str.9 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
+; ANY: @str.6 = private unnamed_addr constant [3 x i8] c"4\00\00", align 1
+; ANY: @str.7 = private unnamed_addr constant [9 x i8] c"4\00\00\00\00\00\00\00\00", align 1
+; ANY: @str.8 = private unnamed_addr constant [9 x i8] c"1234\00\00\00\00\00", align 1
+; ANY: @str.9 = private unnamed_addr constant [9 x i8] c"1234\00\00\00\00\00", align 1
 ;.
 define void @fold_stpncpy_overlap(ptr %dst, i64 %n) {
 ; ANY-LABEL: @fold_stpncpy_overlap(
diff --git a/llvm/test/Transforms/InstCombine/strncpy-1.ll b/llvm/test/Transforms/InstCombine/strncpy-1.ll
index c20656f1efe40..7028941e79da7 100644
--- a/llvm/test/Transforms/InstCombine/strncpy-1.ll
+++ b/llvm/test/Transforms/InstCombine/strncpy-1.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
 ; Test that the strncpy library call simplifier works correctly.
 ;
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
@@ -16,6 +16,16 @@ declare i32 @puts(ptr)
 
 ; Check a bunch of strncpy invocations together.
 
+;.
+; CHECK: @hello = constant [6 x i8] c"hello\00"
+; CHECK: @null = constant [1 x i8] zeroinitializer
+; CHECK: @null_hello = constant [7 x i8] c"\00hello\00"
+; CHECK: @a = common global [32 x i8] zeroinitializer, align 1
+; CHECK: @b = common global [32 x i8] zeroinitializer, align 1
+; CHECK: @str = private unnamed_addr constant [32 x i8] c"hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", align 1
+; CHECK: @str.1 = private unnamed_addr constant [32 x i8] c"hello\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", align 1
+; CHECK: @str.2 = private unnamed_addr constant [8 x i8] c"hello\00\00\00", align 1
+;.
 define i32 @test_simplify1() {
 ; CHECK-LABEL: @test_simplify1(
 ; CHECK-NEXT:    [[TARGET:%.*]] = alloca [1024 x i8], align 1
@@ -197,3 +207,7 @@ define void @test_no_incompatible_attr() {
   call ptr @strncpy(ptr @a, ptr @hello, i32 6)
   ret void
 }
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
+; CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: write) }
+;.
diff --git a/llvm/test/Transforms/InstCombine/strncpy-3.ll b/llvm/test/Transforms/InstCombine/strncpy-3.ll
index 636b9ac73a671..36dbe19684f66 100644
--- a/llvm/test/Transforms/InstCombine/strncpy-3.ll
+++ b/llvm/test/Transforms/InstCombine/strncpy-3.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
 
@@ -9,6 +9,13 @@
 declare ptr @strncpy(ptr, ptr, i64)
 
 
+;.
+; CHECK: @str = constant [2 x i8] c"a\00"
+; CHECK: @str2 = constant [3 x i8] c"abc"
+; CHECK: @str3 = constant [4 x i8] c"abcd"
+; CHECK: @str.1 = private unnamed_addr constant [4 x i8] c"a\00\00\00", align 1
+; CHECK: @str.2 = private unnamed_addr constant [128 x i8] c"abcd\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", align 1
+;.
 define void @fill_with_zeros(ptr %dst) {
 ; CHECK-LABEL: @fill_with_zeros(
 ; CHECK-NEXT:    store i32 97, ptr [[DST:%.*]], align 1
@@ -53,3 +60,6 @@ define void @no_simplify(ptr %dst) {
   tail call ptr @strncpy(ptr %dst, ptr @str3, i64 129)
   ret void
 }
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
+;.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@s-barannikov s-barannikov merged commit 5c48f6f into llvm:main Aug 4, 2024
9 checks passed
@s-barannikov s-barannikov deleted the libcalls-extra-null branch August 4, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants