Skip to content

Commit

Permalink
[DSE] Improve handling of strncpy in Dead Store Elimination
Browse files Browse the repository at this point in the history
Fixes PR#52062 and one of the remaining cases of PR#47644.

Reviewed By: fhahn

Differential Revision: https://reviews.llvm.org/D114035
  • Loading branch information
FabianWolff authored and fhahn committed Nov 19, 2021
1 parent d5de568 commit 7eec832
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Expand Up @@ -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))};
Expand Down
9 changes: 4 additions & 5 deletions llvm/test/Transforms/DeadStoreElimination/libcalls.ll
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
;
Expand Down

0 comments on commit 7eec832

Please sign in to comment.