Skip to content

Commit edb3726

Browse files
antoniofrighettoyuxuanchen1997
authored andcommitted
[TBAA] Do not rewrite TBAA if exists, always null out !tbaa.struct
Summary: Retrieve `!tbaa` metadata via `!tbaa.struct` in `adjustForAccess` unless it already exists, as struct-path aware `MDNodes` emitted via `new-struct-path-tbaa` may be leveraged. As `!tbaa.struct` carries memcpy padding semantics among struct fields and `!tbaa` is already meant to aid to alias semantics, it should be possible to zero out `!tbaa.struct` once the memcpy has been simplified. `SROA/tbaa-struct.ll` test has gone out of scope, as `!tbaa` has already replaced `!tbaa.struct` in SROA. Fixes: #95661. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250561
1 parent f8211c6 commit edb3726

File tree

7 files changed

+45
-86
lines changed

7 files changed

+45
-86
lines changed

llvm/include/llvm/IR/Metadata.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,10 @@ struct AAMDNodes {
846846
AAMDNodes concat(const AAMDNodes &Other) const;
847847

848848
/// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode.
849-
/// If his AAMDNode has !tbaa.struct and \p AccessSize matches the size of the
850-
/// field at offset 0, get the TBAA tag describing the accessed field.
849+
/// If this AAMDNode has !tbaa.struct and \p AccessSize matches the size of
850+
/// the field at offset 0, get the TBAA tag describing the accessed field.
851+
/// If such an AAMDNode already embeds !tbaa, the existing one is retrieved.
852+
/// Finally, !tbaa.struct is zeroed out.
851853
AAMDNodes adjustForAccess(unsigned AccessSize);
852854
AAMDNodes adjustForAccess(size_t Offset, Type *AccessTy,
853855
const DataLayout &DL);

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,16 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) {
822822
AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) {
823823
AAMDNodes New = *this;
824824
MDNode *M = New.TBAAStruct;
825-
if (M && M->getNumOperands() >= 3 && M->getOperand(0) &&
825+
if (!New.TBAA && M && M->getNumOperands() >= 3 && M->getOperand(0) &&
826826
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
827827
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
828828
M->getOperand(1) && mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
829829
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
830830
AccessSize &&
831-
M->getOperand(2) && isa<MDNode>(M->getOperand(2))) {
832-
New.TBAAStruct = nullptr;
831+
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
833832
New.TBAA = cast<MDNode>(M->getOperand(2));
834-
}
833+
834+
New.TBAAStruct = nullptr;
835835
return New;
836836
}
837837

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
171171
IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
172172

173173
// If the memcpy has metadata describing the members, see if we can get the
174-
// TBAA tag describing our copy.
174+
// TBAA, scope and noalias tags describing our copy.
175175
AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
176176

177177
Value *Src = MI->getArgOperand(1);

llvm/test/Transforms/InstCombine/struct-assign-tbaa-2.ll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ define void @test1(ptr %a1, ptr %a2) {
1515
; CHECK-NEXT: [[TMP0:%.*]] = load i16, ptr [[A2]], align 2, !tbaa [[TBAA0:![0-9]+]]
1616
; CHECK-NEXT: store i16 [[TMP0]], ptr [[A1:%.*]], align 2, !tbaa [[TBAA0]]
1717
; CHECK-NEXT: [[B2:%.*]] = getelementptr inbounds i8, ptr [[A1]], i64 2
18-
; CHECK-NEXT: [[TMP1:%.*]] = load i16, ptr [[B]], align 2, !tbaa [[TBAA0]]
19-
; CHECK-NEXT: store i16 [[TMP1]], ptr [[B2]], align 2, !tbaa [[TBAA0]]
18+
; CHECK-NEXT: [[TMP1:%.*]] = load i16, ptr [[B]], align 2, !tbaa [[TBAA6:![0-9]+]]
19+
; CHECK-NEXT: store i16 [[TMP1]], ptr [[B2]], align 2, !tbaa [[TBAA6]]
2020
; CHECK-NEXT: ret void
2121
;
2222
entry:
@@ -38,8 +38,11 @@ entry:
3838
!8 = !{!1, !4, i64 2, i64 2}
3939

4040
;.
41-
; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META1]], i64 0, i64 2}
42-
; CHECK: [[META1]] = !{[[META2:![0-9]+]], i64 2, !"short"}
41+
; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META4:![0-9]+]], i64 0, i64 2}
42+
; CHECK: [[META1]] = !{[[META2:![0-9]+]], i64 4, !"_ZTS1T", [[META4]], i64 0, i64 2, [[META4]], i64 2, i64 2}
4343
; CHECK: [[META2]] = !{[[META3:![0-9]+]], i64 1, !"omnipotent char"}
4444
; CHECK: [[META3]] = !{!"Simple C++ TBAA"}
45+
; CHECK: [[META4]] = !{[[META2]], i64 2, !"_ZTS7Wrapper", [[META5:![0-9]+]], i64 0, i64 2}
46+
; CHECK: [[META5]] = !{[[META2]], i64 2, !"short"}
47+
; CHECK: [[TBAA6]] = !{[[META1]], [[META4]], i64 2, i64 2}
4548
;.

llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ define ptr @test2() {
3838
define void @test3_multiple_fields(ptr nocapture %a, ptr nocapture %b) {
3939
; CHECK-LABEL: @test3_multiple_fields(
4040
; CHECK-NEXT: entry:
41-
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[B:%.*]], align 4, !tbaa.struct [[TBAA_STRUCT3:![0-9]+]]
42-
; CHECK-NEXT: store i64 [[TMP0]], ptr [[A:%.*]], align 4, !tbaa.struct [[TBAA_STRUCT3]]
41+
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[B:%.*]], align 4
42+
; CHECK-NEXT: store i64 [[TMP0]], ptr [[A:%.*]], align 4
4343
; CHECK-NEXT: ret void
4444
;
4545
entry:
@@ -50,8 +50,8 @@ entry:
5050
define void @test4_multiple_copy_first_field(ptr nocapture %a, ptr nocapture %b) {
5151
; CHECK-LABEL: @test4_multiple_copy_first_field(
5252
; CHECK-NEXT: entry:
53-
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[B:%.*]], align 4
54-
; CHECK-NEXT: store i32 [[TMP0]], ptr [[A:%.*]], align 4
53+
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[B:%.*]], align 4, !tbaa [[TBAA0]]
54+
; CHECK-NEXT: store i32 [[TMP0]], ptr [[A:%.*]], align 4, !tbaa [[TBAA0]]
5555
; CHECK-NEXT: ret void
5656
;
5757
entry:
@@ -86,5 +86,4 @@ entry:
8686
; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META1]], i64 0}
8787
; CHECK: [[META1]] = !{!"float", [[META2:![0-9]+]]}
8888
; CHECK: [[META2]] = !{!"Simple C/C++ TBAA"}
89-
; CHECK: [[TBAA_STRUCT3]] = !{i64 0, i64 4, [[TBAA0]], i64 4, i64 4, [[TBAA0]]}
9089
;.

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)