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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/Transforms/InstCombine/stpncpy-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion llvm/test/Transforms/InstCombine/strncpy-1.ll
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) }
;.
12 changes: 11 additions & 1 deletion llvm/test/Transforms/InstCombine/strncpy-3.ll
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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) }
;.
Loading