Skip to content

Commit 5c48f6f

Browse files
authored
[InstCombine] Don't add extra 0 to string in str[np]cpy optimization (#101884)
It is unused by subsequent memcpy.
1 parent 0dba538 commit 5c48f6f

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
910910
// Create a bigger, nul-padded array with the same length, SrcLen,
911911
// as the original string.
912912
SrcStr.resize(N, '\0');
913-
Src = B.CreateGlobalString(SrcStr, "str");
913+
Src = B.CreateGlobalString(SrcStr, "str", /*AddressSpace=*/0,
914+
/*M=*/nullptr, /*AddNull=*/false);
914915
}
915916

916917
Type *PT = Callee->getFunctionType()->getParamType(0);

llvm/test/Transforms/InstCombine/stpncpy-1.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ declare void @sink(ptr, ptr)
3636
; ANY: @str.3 = private unnamed_addr constant [4 x i8] c"4\00\00\00", align 1
3737
; ANY: @str.4 = private unnamed_addr constant [10 x i8] c"4\00\00\00\00\00\00\00\00\00", align 1
3838
; ANY: @str.5 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
39-
; ANY: @str.6 = private unnamed_addr constant [4 x i8] c"4\00\00\00", align 1
40-
; ANY: @str.7 = private unnamed_addr constant [10 x i8] c"4\00\00\00\00\00\00\00\00\00", align 1
41-
; ANY: @str.8 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
42-
; ANY: @str.9 = private unnamed_addr constant [10 x i8] c"1234\00\00\00\00\00\00", align 1
39+
; ANY: @str.6 = private unnamed_addr constant [3 x i8] c"4\00\00", align 1
40+
; ANY: @str.7 = private unnamed_addr constant [9 x i8] c"4\00\00\00\00\00\00\00\00", align 1
41+
; ANY: @str.8 = private unnamed_addr constant [9 x i8] c"1234\00\00\00\00\00", align 1
42+
; ANY: @str.9 = private unnamed_addr constant [9 x i8] c"1234\00\00\00\00\00", align 1
4343
;.
4444
define void @fold_stpncpy_overlap(ptr %dst, i64 %n) {
4545
; ANY-LABEL: @fold_stpncpy_overlap(

llvm/test/Transforms/InstCombine/strncpy-1.ll

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; Test that the strncpy library call simplifier works correctly.
33
;
44
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
@@ -16,6 +16,16 @@ declare i32 @puts(ptr)
1616

1717
; Check a bunch of strncpy invocations together.
1818

19+
;.
20+
; CHECK: @hello = constant [6 x i8] c"hello\00"
21+
; CHECK: @null = constant [1 x i8] zeroinitializer
22+
; CHECK: @null_hello = constant [7 x i8] c"\00hello\00"
23+
; CHECK: @a = common global [32 x i8] zeroinitializer, align 1
24+
; CHECK: @b = common global [32 x i8] zeroinitializer, align 1
25+
; 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
26+
; 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
27+
; CHECK: @str.2 = private unnamed_addr constant [8 x i8] c"hello\00\00\00", align 1
28+
;.
1929
define i32 @test_simplify1() {
2030
; CHECK-LABEL: @test_simplify1(
2131
; CHECK-NEXT: [[TARGET:%.*]] = alloca [1024 x i8], align 1
@@ -197,3 +207,7 @@ define void @test_no_incompatible_attr() {
197207
call ptr @strncpy(ptr @a, ptr @hello, i32 6)
198208
ret void
199209
}
210+
;.
211+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
212+
; CHECK: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: write) }
213+
;.

llvm/test/Transforms/InstCombine/strncpy-3.ll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33

44

@@ -9,6 +9,13 @@
99
declare ptr @strncpy(ptr, ptr, i64)
1010

1111

12+
;.
13+
; CHECK: @str = constant [2 x i8] c"a\00"
14+
; CHECK: @str2 = constant [3 x i8] c"abc"
15+
; CHECK: @str3 = constant [4 x i8] c"abcd"
16+
; CHECK: @str.1 = private unnamed_addr constant [4 x i8] c"a\00\00\00", align 1
17+
; 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
18+
;.
1219
define void @fill_with_zeros(ptr %dst) {
1320
; CHECK-LABEL: @fill_with_zeros(
1421
; CHECK-NEXT: store i32 97, ptr [[DST:%.*]], align 1
@@ -53,3 +60,6 @@ define void @no_simplify(ptr %dst) {
5360
tail call ptr @strncpy(ptr %dst, ptr @str3, i64 129)
5461
ret void
5562
}
63+
;.
64+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
65+
;.

0 commit comments

Comments
 (0)