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

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 4 04:46:58 PDT 2024


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/101884

It is unused by subsequent memcpy.

>From 3457d840a6f80856ec5edd7ec10e42ed9e69ba67 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Sun, 4 Aug 2024 14:44:02 +0300
Subject: [PATCH 1/2] Check globals in two tests

---
 llvm/test/Transforms/InstCombine/strncpy-1.ll | 16 +++++++++++++++-
 llvm/test/Transforms/InstCombine/strncpy-3.ll | 12 +++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/strncpy-1.ll b/llvm/test/Transforms/InstCombine/strncpy-1.ll
index c20656f1efe40..4a5e4082fb81d 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 [33 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\00", align 1
+; CHECK: @str.1 = private unnamed_addr constant [33 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\00", align 1
+; CHECK: @str.2 = private unnamed_addr constant [9 x i8] c"hello\00\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..9052d3761e1d1 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 [5 x i8] c"a\00\00\00\00", align 1
+; CHECK: @str.2 = private unnamed_addr constant [129 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\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) }
+;.

>From fdde22189d9732dc0e8635cd12b3eac4f85bb10b Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Sun, 4 Aug 2024 14:44:24 +0300
Subject: [PATCH 2/2] [InstCombine] Don't add extra 0 to string in str[np]cpy
 optimization

It is unused by subsequent memcpy.
---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 3 ++-
 llvm/test/Transforms/InstCombine/stpncpy-1.ll  | 8 ++++----
 llvm/test/Transforms/InstCombine/strncpy-1.ll  | 6 +++---
 llvm/test/Transforms/InstCombine/strncpy-3.ll  | 4 ++--
 4 files changed, 11 insertions(+), 10 deletions(-)

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 4a5e4082fb81d..7028941e79da7 100644
--- a/llvm/test/Transforms/InstCombine/strncpy-1.ll
+++ b/llvm/test/Transforms/InstCombine/strncpy-1.ll
@@ -22,9 +22,9 @@ declare i32 @puts(ptr)
 ; 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 [33 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\00", align 1
-; CHECK: @str.1 = private unnamed_addr constant [33 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\00", align 1
-; CHECK: @str.2 = private unnamed_addr constant [9 x i8] c"hello\00\00\00\00", 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(
diff --git a/llvm/test/Transforms/InstCombine/strncpy-3.ll b/llvm/test/Transforms/InstCombine/strncpy-3.ll
index 9052d3761e1d1..36dbe19684f66 100644
--- a/llvm/test/Transforms/InstCombine/strncpy-3.ll
+++ b/llvm/test/Transforms/InstCombine/strncpy-3.ll
@@ -13,8 +13,8 @@ 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 [5 x i8] c"a\00\00\00\00", align 1
-; CHECK: @str.2 = private unnamed_addr constant [129 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\00", align 1
+; 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(



More information about the llvm-commits mailing list