Skip to content

Commit a792953

Browse files
[Metadata] Add TBAA struct metadata to AAMDNode
Summary: Make `AAMDNodes`' `getAAMetadata()` and `setAAMetadata()` to take `!tbaa.struct` into account as well as `!tbaa`. This impacts llvm.org/pr42022. This is a temprorary fix needed to keep `!tbaa.struct` tag by SROA pass. New field `TBAAStruct` should be deleted when `!tbaa` tag replaces `!tbaa.struct`. Merging two `!tbaa.struct`'s to one is conservatively considered to be `nullptr` (giving `MayAlias`) -- this could be enhanced, but relying on the said future replacement. Reviewers: RKSimon, spatel, vporpo Subscribers: hiraditya, kosarev, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70924
1 parent ce67db4 commit a792953

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

llvm/include/llvm/IR/Metadata.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -642,26 +642,32 @@ class MDString : public Metadata {
642642
/// A collection of metadata nodes that might be associated with a
643643
/// memory access used by the alias-analysis infrastructure.
644644
struct AAMDNodes {
645-
explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
646-
MDNode *N = nullptr)
647-
: TBAA(T), Scope(S), NoAlias(N) {}
645+
explicit AAMDNodes() = default;
646+
explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
647+
: TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N) {}
648648

649649
bool operator==(const AAMDNodes &A) const {
650-
return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
650+
return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
651+
NoAlias == A.NoAlias;
651652
}
652653

653654
bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
654655

655-
explicit operator bool() const { return TBAA || Scope || NoAlias; }
656+
explicit operator bool() const {
657+
return TBAA || TBAAStruct || Scope || NoAlias;
658+
}
656659

657660
/// The tag for type-based alias analysis.
658-
MDNode *TBAA;
661+
MDNode *TBAA = nullptr;
662+
663+
/// The tag for type-based alias analysis (tbaa struct).
664+
MDNode *TBAAStruct = nullptr;
659665

660666
/// The tag for alias scope specification (used with noalias).
661-
MDNode *Scope;
667+
MDNode *Scope = nullptr;
662668

663669
/// The tag specifying the noalias scope.
664-
MDNode *NoAlias;
670+
MDNode *NoAlias = nullptr;
665671

666672
/// Given two sets of AAMDNodes that apply to the same pointer,
667673
/// give the best AAMDNodes that are compatible with both (i.e. a set of
@@ -671,6 +677,7 @@ struct AAMDNodes {
671677
AAMDNodes intersect(const AAMDNodes &Other) {
672678
AAMDNodes Result;
673679
Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
680+
Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
674681
Result.Scope = Other.Scope == Scope ? Scope : nullptr;
675682
Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
676683
return Result;
@@ -682,16 +689,17 @@ template<>
682689
struct DenseMapInfo<AAMDNodes> {
683690
static inline AAMDNodes getEmptyKey() {
684691
return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
685-
nullptr, nullptr);
692+
nullptr, nullptr, nullptr);
686693
}
687694

688695
static inline AAMDNodes getTombstoneKey() {
689696
return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
690-
nullptr, nullptr);
697+
nullptr, nullptr, nullptr);
691698
}
692699

693700
static unsigned getHashValue(const AAMDNodes &Val) {
694701
return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
702+
DenseMapInfo<MDNode *>::getHashValue(Val.TBAAStruct) ^
695703
DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
696704
DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
697705
}

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -521,23 +521,20 @@ static const MDNode *getLeastCommonType(const MDNode *A, const MDNode *B) {
521521
}
522522

523523
void Instruction::getAAMetadata(AAMDNodes &N, bool Merge) const {
524-
if (Merge)
524+
if (Merge) {
525525
N.TBAA =
526526
MDNode::getMostGenericTBAA(N.TBAA, getMetadata(LLVMContext::MD_tbaa));
527-
else
528-
N.TBAA = getMetadata(LLVMContext::MD_tbaa);
529-
530-
if (Merge)
527+
N.TBAAStruct = nullptr;
531528
N.Scope = MDNode::getMostGenericAliasScope(
532529
N.Scope, getMetadata(LLVMContext::MD_alias_scope));
533-
else
534-
N.Scope = getMetadata(LLVMContext::MD_alias_scope);
535-
536-
if (Merge)
537530
N.NoAlias =
538531
MDNode::intersect(N.NoAlias, getMetadata(LLVMContext::MD_noalias));
539-
else
532+
} else {
533+
N.TBAA = getMetadata(LLVMContext::MD_tbaa);
534+
N.TBAAStruct = getMetadata(LLVMContext::MD_tbaa_struct);
535+
N.Scope = getMetadata(LLVMContext::MD_alias_scope);
540536
N.NoAlias = getMetadata(LLVMContext::MD_noalias);
537+
}
541538
}
542539

543540
static const MDNode *createAccessTag(const MDNode *AccessType) {

llvm/lib/IR/Metadata.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
12621262

12631263
void Instruction::setAAMetadata(const AAMDNodes &N) {
12641264
setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1265+
setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
12651266
setMetadata(LLVMContext::MD_alias_scope, N.Scope);
12661267
setMetadata(LLVMContext::MD_noalias, N.NoAlias);
12671268
}

llvm/test/Transforms/SROA/tbaa-struct.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define void @bar(%vector* %y2) {
1111
; CHECK-LABEL: @bar(
1212
; CHECK-NEXT: [[X14:%.*]] = call <2 x float> @foo(%vector* [[Y2:%.*]])
1313
; CHECK-NEXT: [[X7_SROA_0_0_X18_SROA_CAST:%.*]] = bitcast %vector* [[Y2]] to <2 x float>*
14-
; CHECK-NEXT: store <2 x float> [[X14]], <2 x float>* [[X7_SROA_0_0_X18_SROA_CAST]], align 4
14+
; CHECK-NEXT: store <2 x float> [[X14]], <2 x float>* [[X7_SROA_0_0_X18_SROA_CAST]], align 4, !tbaa.struct !0
1515
; CHECK-NEXT: ret void
1616
;
1717
%x7 = alloca %vector

0 commit comments

Comments
 (0)