From e6de9f21b162c57dd09cb4de3147b7ab09ef8681 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 9 Feb 2024 13:29:19 +0000 Subject: [PATCH 1/2] [TBAA] Extract logic to use TBAA tag for field of !tbaa.struct (NFC). --- llvm/include/llvm/IR/Metadata.h | 5 +++++ llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp | 14 ++++++++++++++ .../Transforms/InstCombine/InstCombineCalls.cpp | 15 +-------------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h index db1f44fea3b45..6f23ac44dee96 100644 --- a/llvm/include/llvm/IR/Metadata.h +++ b/llvm/include/llvm/IR/Metadata.h @@ -844,6 +844,11 @@ struct AAMDNodes { /// together. Different from `merge`, where different locations should /// overlap each other, `concat` puts non-overlapping locations together. AAMDNodes concat(const AAMDNodes &Other) const; + + /// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode. + /// If his AAMDNode has !tbaa.struct and \p AccessSize matches the size of the + /// field at offset 0, get the TBAA tag describing the accessed field. + AAMDNodes adjustForAccess(unsigned AccessSize); }; // Specialize DenseMapInfo for AAMDNodes. diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp index e4dc1a867f6f0..edc08cde686f1 100644 --- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp +++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp @@ -817,3 +817,17 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) { ConstantAsMetadata::get(ConstantInt::get(PreviousSize->getType(), Len)); return MDNode::get(MD->getContext(), NextNodes); } + +AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) { + AAMDNodes New = *this; + MDNode *M = New.TBAAStruct; + New.TBAAStruct = nullptr; + if (M && M->getNumOperands() == 3 && M->getOperand(0) && + mdconst::hasa(M->getOperand(0)) && + mdconst::extract(M->getOperand(0))->isZero() && + M->getOperand(1) && mdconst::hasa(M->getOperand(1)) && + mdconst::extract(M->getOperand(1))->getValue() == AccessSize && + M->getOperand(2) && isa(M->getOperand(2))) + New.TBAA = cast(M->getOperand(2)); + return New; +} diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index ed5d44757fbeb..56d1259e95519 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -172,20 +172,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) { // If the memcpy has metadata describing the members, see if we can get the // TBAA tag describing our copy. - AAMDNodes AACopyMD = MI->getAAMetadata(); - - if (MDNode *M = AACopyMD.TBAAStruct) { - AACopyMD.TBAAStruct = nullptr; - if (M->getNumOperands() == 3 && M->getOperand(0) && - mdconst::hasa(M->getOperand(0)) && - mdconst::extract(M->getOperand(0))->isZero() && - M->getOperand(1) && - mdconst::hasa(M->getOperand(1)) && - mdconst::extract(M->getOperand(1))->getValue() == - Size && - M->getOperand(2) && isa(M->getOperand(2))) - AACopyMD.TBAA = cast(M->getOperand(2)); - } + AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size); Value *Src = MI->getArgOperand(1); Value *Dest = MI->getArgOperand(0); From 99cf032dfabb21b820559bae61d2354e56336fdd Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 9 Feb 2024 16:25:32 +0000 Subject: [PATCH 2/2] [TBAA] Only clear TBAAStruct if field can be extracted. Retain TBAAStruct if we fail to match the access to a single field. All users at the moment use this when using the full size of the original access. SROA also retains the original TBAAStruct when accessing parts at offset 0. Motivation for this and follow-on patches is to improve codegen for libc++, where using memcpy limits optimizations, like vectorization for code iteration over std::vector>: https://godbolt.org/z/f3vqYos3c Depends on https://github.com/llvm/llvm-project/pull/81284 --- llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp | 8 +++++--- llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp index edc08cde686f1..bfd70414c0340 100644 --- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp +++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp @@ -821,13 +821,15 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) { AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) { AAMDNodes New = *this; MDNode *M = New.TBAAStruct; - New.TBAAStruct = nullptr; if (M && M->getNumOperands() == 3 && M->getOperand(0) && mdconst::hasa(M->getOperand(0)) && mdconst::extract(M->getOperand(0))->isZero() && M->getOperand(1) && mdconst::hasa(M->getOperand(1)) && - mdconst::extract(M->getOperand(1))->getValue() == AccessSize && - M->getOperand(2) && isa(M->getOperand(2))) + mdconst::extract(M->getOperand(1))->getValue() == + AccessSize && + M->getOperand(2) && isa(M->getOperand(2))) { + New.TBAAStruct = nullptr; New.TBAA = cast(M->getOperand(2)); + } return New; } diff --git a/llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll b/llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll index 1042c413fbb7b..996d2c0e67e16 100644 --- a/llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll +++ b/llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll @@ -38,8 +38,8 @@ define ptr @test2() { define void @test3_multiple_fields(ptr nocapture %a, ptr nocapture %b) { ; CHECK-LABEL: @test3_multiple_fields( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[B:%.*]], align 4 -; CHECK-NEXT: store i64 [[TMP0]], ptr [[A:%.*]], align 4 +; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[B:%.*]], align 4, !tbaa.struct [[TBAA_STRUCT3:![0-9]+]] +; CHECK-NEXT: store i64 [[TMP0]], ptr [[A:%.*]], align 4, !tbaa.struct [[TBAA_STRUCT3]] ; CHECK-NEXT: ret void ; entry: @@ -86,4 +86,5 @@ entry: ; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META1]], i64 0} ; CHECK: [[META1]] = !{!"float", [[META2:![0-9]+]]} ; CHECK: [[META2]] = !{!"Simple C/C++ TBAA"} +; CHECK: [[TBAA_STRUCT3]] = !{i64 0, i64 4, [[TBAA0]], i64 4, i64 4, [[TBAA0]]} ;.