Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions llvm/include/llvm/IR/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,19 @@ 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;
if (M && M->getNumOperands() == 3 && M->getOperand(0) &&
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
M->getOperand(1) && mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
AccessSize &&
M->getOperand(2) && isa<MDNode>(M->getOperand(2))) {
New.TBAAStruct = nullptr;
New.TBAA = cast<MDNode>(M->getOperand(2));
}
return New;
}
15 changes: 1 addition & 14 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConstantInt>(M->getOperand(0)) &&
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
M->getOperand(1) &&
mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
Size &&
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
AACopyMD.TBAA = cast<MDNode>(M->getOperand(2));
}
AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);

Value *Src = MI->getArgOperand(1);
Value *Dest = MI->getArgOperand(0);
Expand Down
5 changes: 3 additions & 2 deletions llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]]}
;.