-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It is unused by subsequent memcpy.
@llvm/pr-subscribers-llvm-transforms Author: Sergei Barannikov (s-barannikov) ChangesIt is unused by subsequent memcpy. Full diff: https://github.com/llvm/llvm-project/pull/101884.diff 4 Files Affected:
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) }
+;.
|
nikic
approved these changes
Aug 4, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is unused by subsequent memcpy.