[llvm] 5c48f6f - [InstCombine] Don't add extra 0 to string in str[np]cpy optimization (#101884)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 4 06:12:16 PDT 2024
Author: Sergei Barannikov
Date: 2024-08-04T16:12:14+03:00
New Revision: 5c48f6fa543e7d081354d3f6c1c2524eb8075223
URL: https://github.com/llvm/llvm-project/commit/5c48f6fa543e7d081354d3f6c1c2524eb8075223
DIFF: https://github.com/llvm/llvm-project/commit/5c48f6fa543e7d081354d3f6c1c2524eb8075223.diff
LOG: [InstCombine] Don't add extra 0 to string in str[np]cpy optimization (#101884)
It is unused by subsequent memcpy.
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/stpncpy-1.ll
llvm/test/Transforms/InstCombine/strncpy-1.ll
llvm/test/Transforms/InstCombine/strncpy-3.ll
Removed:
################################################################################
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) }
+;.
More information about the llvm-commits
mailing list