Skip to content

Commit 62332e9

Browse files
committed
[MergedLoadStore] Preserve common metadata when sinking stores.
When sinking a store, preserve common metadata present on stores on both sides of the diamond.
1 parent a41ae90 commit 62332e9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,15 @@ void MergedLoadStoreMotion::sinkStoresAndGEPs(BasicBlock *BB, StoreInst *S0,
255255
BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
256256
// Intersect optional metadata.
257257
S0->andIRFlags(S1);
258-
S0->dropUnknownNonDebugMetadata();
258+
259+
// Keep metadata present on both instructions.
260+
SmallVector<std::pair<unsigned, MDNode *>> MDs;
261+
S0->getAllMetadataOtherThanDebugLoc(MDs);
262+
SmallVector<unsigned> CommonMDs;
263+
for (const auto &[Kind, MD] : MDs)
264+
if (S1->getMetadata(Kind) == MD)
265+
CommonMDs.push_back(Kind);
266+
S0->dropUnknownNonDebugMetadata(CommonMDs);
259267
S0->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc());
260268
S0->mergeDIAssignID(S1);
261269

llvm/test/Transforms/MergedLoadStoreMotion/preserve-store-metadata.ll

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define void @perserve_common_metadata(i1 %c, ptr %dst, ptr %min) {
1919
; CHECK-NEXT: br label %[[RETURN]]
2020
; CHECK: [[RETURN]]:
2121
; CHECK-NEXT: [[DOTSINK:%.*]] = phi ptr [ [[DST]], %[[THEN]] ], [ null, %[[ELSE]] ]
22-
; CHECK-NEXT: store ptr [[DOTSINK]], ptr [[GEP_DST_16]], align 8
22+
; CHECK-NEXT: store ptr [[DOTSINK]], ptr [[GEP_DST_16]], align 8, !tbaa [[TBAA4:![0-9]+]], !alias.scope [[META6:![0-9]+]], !noalias [[META6]], !llvm.access.group [[ACC_GRP9:![0-9]+]]
2323
; CHECK-NEXT: ret void
2424
;
2525
entry:
@@ -48,7 +48,7 @@ define void @clear_different_metadata(i1 %c, ptr %dst, ptr %min) {
4848
; CHECK-NEXT: [[GEP_DST_16:%.*]] = getelementptr inbounds nuw i8, ptr [[DST]], i64 16
4949
; CHECK-NEXT: br i1 [[C]], label %[[THEN:.*]], label %[[ELSE:.*]]
5050
; CHECK: [[THEN]]:
51-
; CHECK-NEXT: store ptr [[DST]], ptr [[MIN]], align 8, !tbaa [[TBAA4:![0-9]+]]
51+
; CHECK-NEXT: store ptr [[DST]], ptr [[MIN]], align 8, !tbaa [[TBAA10:![0-9]+]]
5252
; CHECK-NEXT: br label %[[RETURN:.*]]
5353
; CHECK: [[ELSE]]:
5454
; CHECK-NEXT: [[GEP_DST_24:%.*]] = getelementptr inbounds nuw i8, ptr [[DST]], i64 24
@@ -99,6 +99,12 @@ return:
9999
; CHECK: [[META2]] = !{!"omnipotent char", [[META3:![0-9]+]], i64 0}
100100
; CHECK: [[META3]] = !{!"Simple C++ TBAA"}
101101
; CHECK: [[TBAA4]] = !{[[META5:![0-9]+]], [[META5]], i64 0, i64 0}
102-
; CHECK: [[META5]] = !{!"p2 _Foo", [[META6:![0-9]+]]}
103-
; CHECK: [[META6]] = !{!"any pointer", [[META2]], i64 0}
102+
; CHECK: [[META5]] = !{!"long", [[META2]]}
103+
; CHECK: [[META6]] = !{[[META7:![0-9]+]]}
104+
; CHECK: [[META7]] = distinct !{[[META7]], [[META8:![0-9]+]]}
105+
; CHECK: [[META8]] = distinct !{[[META8]]}
106+
; CHECK: [[ACC_GRP9]] = distinct !{}
107+
; CHECK: [[TBAA10]] = !{[[META11:![0-9]+]], [[META11]], i64 0, i64 0}
108+
; CHECK: [[META11]] = !{!"p2 _Foo", [[META12:![0-9]+]]}
109+
; CHECK: [[META12]] = !{!"any pointer", [[META2]], i64 0}
104110
;.

0 commit comments

Comments
 (0)