[llvm] 7eec832 - [DSE] Improve handling of `strncpy` in Dead Store Elimination
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 19 09:53:14 PST 2021
Author: Fabian Wolff
Date: 2021-11-19T17:46:29Z
New Revision: 7eec832def5717b1bddb72c3b99c3df4f7a2f6da
URL: https://github.com/llvm/llvm-project/commit/7eec832def5717b1bddb72c3b99c3df4f7a2f6da
DIFF: https://github.com/llvm/llvm-project/commit/7eec832def5717b1bddb72c3b99c3df4f7a2f6da.diff
LOG: [DSE] Improve handling of `strncpy` in Dead Store Elimination
Fixes PR#52062 and one of the remaining cases of PR#47644.
Reviewed By: fhahn
Differential Revision: https://reviews.llvm.org/D114035
Added:
Modified:
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/libcalls.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index f2268fe762450..a8ec8bb979707 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1059,8 +1059,13 @@ struct DSEState {
LibFunc LF;
if (TLI.getLibFunc(*CB, LF) && TLI.has(LF)) {
switch (LF) {
- case LibFunc_strcpy:
case LibFunc_strncpy:
+ if (const auto *Len = dyn_cast<ConstantInt>(CB->getArgOperand(2)))
+ return MemoryLocation(CB->getArgOperand(0),
+ LocationSize::precise(Len->getZExtValue()),
+ CB->getAAMetadata());
+ LLVM_FALLTHROUGH;
+ case LibFunc_strcpy:
case LibFunc_strcat:
case LibFunc_strncat:
return {MemoryLocation::getAfter(CB->getArgOperand(0))};
diff --git a/llvm/test/Transforms/DeadStoreElimination/libcalls.ll b/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
index 0639f95b5eb2a..afe064daf5bd5 100644
--- a/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
@@ -435,8 +435,7 @@ declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind
; strncpy -> memset, full overwrite
define void @dse_strncpy_test1(i8* noalias %out, i8* noalias %in) {
; CHECK-LABEL: @dse_strncpy_test1(
-; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @strncpy(i8* [[OUT:%.*]], i8* [[IN:%.*]], i64 100)
-; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* [[OUT]], i8 42, i64 100, i1 false)
+; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* [[OUT:%.*]], i8 42, i64 100, i1 false)
; CHECK-NEXT: ret void
;
%call = tail call i8* @strncpy(i8* %out, i8* %in, i64 100)
@@ -472,8 +471,7 @@ define void @dse_strncpy_test3(i8* noalias %out1, i8* noalias %out2, i8* noalias
; memset -> strncpy, full overwrite
define void @dse_strncpy_test4(i8* noalias %out, i8* noalias %in) {
; CHECK-LABEL: @dse_strncpy_test4(
-; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* [[OUT:%.*]], i8 42, i64 100, i1 false)
-; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @strncpy(i8* [[OUT]], i8* [[IN:%.*]], i64 100)
+; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @strncpy(i8* [[OUT:%.*]], i8* [[IN:%.*]], i64 100)
; CHECK-NEXT: ret void
;
tail call void @llvm.memset.p0i8.i64(i8* %out, i8 42, i64 100, i1 false)
@@ -484,7 +482,8 @@ define void @dse_strncpy_test4(i8* noalias %out, i8* noalias %in) {
; memset -> strncpy, partial overwrite
define void @dse_strncpy_test5(i8* noalias %out, i8* noalias %in) {
; CHECK-LABEL: @dse_strncpy_test5(
-; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* [[OUT:%.*]], i8 42, i64 100, i1 false)
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, i8* [[OUT:%.*]], i64 99
+; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* align 1 [[TMP1]], i8 42, i64 1, i1 false)
; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @strncpy(i8* [[OUT]], i8* [[IN:%.*]], i64 99)
; CHECK-NEXT: ret void
;
More information about the llvm-commits
mailing list