Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,7 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
SmallVector<Instruction *, 4> LifetimeMarkers;
SmallSet<Instruction *, 4> NoAliasInstrs;
bool SrcNotDom = false;
SmallSet<Instruction *, 4> OptimizedAllocaInstUsers;

// Recursively track the user and check whether modified alias exist.
auto IsDereferenceableOrNull = [](Value *V, const DataLayout &DL) -> bool {
Expand All @@ -1524,8 +1525,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
};

auto CaptureTrackingWithModRef =
[&](Instruction *AI,
function_ref<bool(Instruction *)> ModRefCallback) -> bool {
[&](Instruction *AI, function_ref<bool(Instruction *)> ModRefCallback,
SmallSet<Instruction *, 4> &AllocaInstUsersWithTBAA) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the argument, the variable is captured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

SmallVector<Instruction *, 8> Worklist;
Worklist.push_back(AI);
unsigned MaxUsesToExplore = getDefaultMaxUsesToExploreForCaptureTracking();
Expand Down Expand Up @@ -1569,6 +1570,9 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
continue;
}
}
if (UI != Store && (UI->hasMetadata(LLVMContext::MD_tbaa) ||
UI->hasMetadata(LLVMContext::MD_tbaa_struct)))
AllocaInstUsersWithTBAA.insert(UI);
if (UI->hasMetadata(LLVMContext::MD_noalias))
NoAliasInstrs.insert(UI);
if (!ModRefCallback(UI))
Expand Down Expand Up @@ -1621,7 +1625,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
return true;
};

if (!CaptureTrackingWithModRef(DestAlloca, DestModRefCallback))
if (!CaptureTrackingWithModRef(DestAlloca, DestModRefCallback,
OptimizedAllocaInstUsers))
return false;
// Bailout if Dest may have any ModRef before Store.
if (!ReachabilityWorklist.empty() &&
Expand All @@ -1647,7 +1652,8 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
return true;
};

if (!CaptureTrackingWithModRef(SrcAlloca, SrcModRefCallback))
if (!CaptureTrackingWithModRef(SrcAlloca, SrcModRefCallback,
OptimizedAllocaInstUsers))
return false;

// We can do the transformation. First, move the SrcAlloca to the start of the
Expand Down Expand Up @@ -1681,6 +1687,12 @@ bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
for (Instruction *I : NoAliasInstrs)
I->setMetadata(LLVMContext::MD_noalias, nullptr);

// Remove !tbaa and !tbaa_struct from the metadata, since they are invalid.
for (Instruction *I : OptimizedAllocaInstUsers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we combine NoAliasInstrs and OptimizedAllocaInstUsers? The purpose is basically the same, I don't think we need separate sets for different metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I combined them.

I->setMetadata(LLVMContext::MD_tbaa, nullptr);
I->setMetadata(LLVMContext::MD_tbaa_struct, nullptr);
}

LLVM_DEBUG(dbgs() << "Stack Move: Performed staack-move optimization\n");
NumStackMove++;
return true;
Expand Down
77 changes: 77 additions & 0 deletions llvm/test/Transforms/MemCpyOpt/memcpy-tbaa.ll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test still doesn't look minimal. Wouldn't it be sufficient to have a store with one tbaa tag, a memcpy and a load with another tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed unnecessary instructions.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=memcpyopt,dse -S -verify-memoryssa | FileCheck %s

define void @test() local_unnamed_addr {
; CHECK-LABEL: define void @test() local_unnamed_addr {
; CHECK-NEXT: [[TEST_ARRAY_B:%.*]] = alloca [31 x float], align 4
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr float, ptr [[TEST_ARRAY_B]], i64 1
; CHECK-NEXT: store float 0x3E6AA51880000000, ptr [[TMP1]], align 4
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr float, ptr [[TEST_ARRAY_B]], i64 1
; CHECK-NEXT: [[TMP3:%.*]] = load float, ptr [[TMP2]], align 4
; CHECK-NEXT: ret void
;
%test_array_a = alloca [31 x float], align 4
%test_array_b = alloca [31 x float], align 4
%1 = getelementptr float, ptr %test_array_b, i64 1
store float 0x3E6AA51880000000, ptr %1, align 4, !tbaa !4
call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(124) %test_array_a, ptr noundef nonnull align 4 dereferenceable(124) %test_array_b, i64 124, i1 false)
%2 = getelementptr float, ptr %test_array_a, i64 1
%3 = load float, ptr %2, align 4, !tbaa !7
ret void
}

%struct.Outer = type { float, double, %struct.Inner }
%struct.Inner = type { i32, float }

; Function Attrs: nounwind uwtable
define dso_local float @f() {
; CHECK-LABEL: define dso_local float @f() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TEST1:%.*]] = alloca [[STRUCT_OUTER:%.*]], align 8
; CHECK-NEXT: [[F:%.*]] = getelementptr inbounds nuw [[STRUCT_OUTER]], ptr [[TEST1]], i32 0, i32 0
; CHECK-NEXT: store float 0.000000e+00, ptr [[F]], align 8
; CHECK-NEXT: [[F1:%.*]] = getelementptr inbounds nuw [[STRUCT_OUTER]], ptr [[TEST1]], i32 0, i32 0
; CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[F1]], align 8
; CHECK-NEXT: [[ADD:%.*]] = fadd float [[TMP0]], 2.000000e+00
; CHECK-NEXT: store float [[ADD]], ptr [[F1]], align 8
; CHECK-NEXT: [[F2:%.*]] = getelementptr inbounds nuw [[STRUCT_OUTER]], ptr [[TEST1]], i32 0, i32 0
; CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[F2]], align 8
; CHECK-NEXT: ret float [[TMP1]]
;
entry:
%test = alloca %struct.Outer, align 8
%test1 = alloca %struct.Outer, align 8
%f = getelementptr inbounds nuw %struct.Outer, ptr %test1, i32 0, i32 0
store float 0.000000e+00, ptr %f, align 8, !tbaa !9
%inner_a = getelementptr inbounds nuw %struct.Outer, ptr %test1, i32 0, i32 2
%i = getelementptr inbounds nuw %struct.Inner, ptr %inner_a, i32 0, i32 0
store i32 0, ptr %i, align 8, !tbaa !17
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %test, ptr align 8 %test1, i64 24, i1 false)
%f1 = getelementptr inbounds nuw %struct.Outer, ptr %test, i32 0, i32 0
%0 = load float, ptr %f1, align 8, !tbaa !9
%add = fadd float %0, 2.000000e+00
store float %add, ptr %f1, align 8, !tbaa !9
%f2 = getelementptr inbounds nuw %struct.Outer, ptr %test, i32 0, i32 0
%1 = load float, ptr %f2, align 8, !tbaa !9
ret float %1
}

!1 = !{!"any data access", !2, i64 0}
!2 = !{!"any access", !3, i64 0}
!3 = !{!"Flang function root test"}
!4 = !{!5, !5, i64 0}
!5 = !{!"allocated data/test_array_a", !6, i64 0}
!6 = !{!"allocated data", !1, i64 0}
!7 = !{!8, !8, i64 0}
!8 = !{!"allocated data/test_array_b", !6, i64 0}
!9 = !{!10, !11, i64 0}
!10 = !{!"Outer", !11, i64 0, !14, i64 8, !15, i64 16}
!11 = !{!"float", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C/C++ TBAA"}
!14 = !{!"double", !12, i64 0}
!15 = !{!"Inner", !16, i64 0, !11, i64 4}
!16 = !{!"int", !12, i64 0}
!17 = !{!10, !16, i64 16}