[llvm] [MergedLoadStore] Preserve common metadata when sinking stores. (PR #116382)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 15 05:18:41 PST 2024


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/116382

When sinking a store, preserve common metadata present on stores on both sides of the diamond.

>From 62332e998e5139737b1a35208a3129838a931482 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 15 Nov 2024 13:13:04 +0000
Subject: [PATCH] [MergedLoadStore] Preserve common metadata when sinking
 stores.

When sinking a store, preserve common metadata present on stores on both
sides of the diamond.
---
 .../Transforms/Scalar/MergedLoadStoreMotion.cpp    | 10 +++++++++-
 .../preserve-store-metadata.ll                     | 14 ++++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp b/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
index 299239fb70200b..2cdca867215c4b 100644
--- a/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
+++ b/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp
@@ -255,7 +255,15 @@ void MergedLoadStoreMotion::sinkStoresAndGEPs(BasicBlock *BB, StoreInst *S0,
   BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
   // Intersect optional metadata.
   S0->andIRFlags(S1);
-  S0->dropUnknownNonDebugMetadata();
+
+  // Keep metadata present on both instructions.
+  SmallVector<std::pair<unsigned, MDNode *>> MDs;
+  S0->getAllMetadataOtherThanDebugLoc(MDs);
+  SmallVector<unsigned> CommonMDs;
+  for (const auto &[Kind, MD] : MDs)
+    if (S1->getMetadata(Kind) == MD)
+      CommonMDs.push_back(Kind);
+  S0->dropUnknownNonDebugMetadata(CommonMDs);
   S0->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc());
   S0->mergeDIAssignID(S1);
 
diff --git a/llvm/test/Transforms/MergedLoadStoreMotion/preserve-store-metadata.ll b/llvm/test/Transforms/MergedLoadStoreMotion/preserve-store-metadata.ll
index f733003e90729b..03cb40fa4a609e 100644
--- a/llvm/test/Transforms/MergedLoadStoreMotion/preserve-store-metadata.ll
+++ b/llvm/test/Transforms/MergedLoadStoreMotion/preserve-store-metadata.ll
@@ -19,7 +19,7 @@ define void @perserve_common_metadata(i1 %c, ptr %dst, ptr %min) {
 ; CHECK-NEXT:    br label %[[RETURN]]
 ; CHECK:       [[RETURN]]:
 ; CHECK-NEXT:    [[DOTSINK:%.*]] = phi ptr [ [[DST]], %[[THEN]] ], [ null, %[[ELSE]] ]
-; CHECK-NEXT:    store ptr [[DOTSINK]], ptr [[GEP_DST_16]], align 8
+; 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]+]]
 ; CHECK-NEXT:    ret void
 ;
 entry:
@@ -48,7 +48,7 @@ define void @clear_different_metadata(i1 %c, ptr %dst, ptr %min) {
 ; CHECK-NEXT:    [[GEP_DST_16:%.*]] = getelementptr inbounds nuw i8, ptr [[DST]], i64 16
 ; CHECK-NEXT:    br i1 [[C]], label %[[THEN:.*]], label %[[ELSE:.*]]
 ; CHECK:       [[THEN]]:
-; CHECK-NEXT:    store ptr [[DST]], ptr [[MIN]], align 8, !tbaa [[TBAA4:![0-9]+]]
+; CHECK-NEXT:    store ptr [[DST]], ptr [[MIN]], align 8, !tbaa [[TBAA10:![0-9]+]]
 ; CHECK-NEXT:    br label %[[RETURN:.*]]
 ; CHECK:       [[ELSE]]:
 ; CHECK-NEXT:    [[GEP_DST_24:%.*]] = getelementptr inbounds nuw i8, ptr [[DST]], i64 24
@@ -99,6 +99,12 @@ return:
 ; CHECK: [[META2]] = !{!"omnipotent char", [[META3:![0-9]+]], i64 0}
 ; CHECK: [[META3]] = !{!"Simple C++ TBAA"}
 ; CHECK: [[TBAA4]] = !{[[META5:![0-9]+]], [[META5]], i64 0, i64 0}
-; CHECK: [[META5]] = !{!"p2 _Foo", [[META6:![0-9]+]]}
-; CHECK: [[META6]] = !{!"any pointer", [[META2]], i64 0}
+; CHECK: [[META5]] = !{!"long", [[META2]]}
+; CHECK: [[META6]] = !{[[META7:![0-9]+]]}
+; CHECK: [[META7]] = distinct !{[[META7]], [[META8:![0-9]+]]}
+; CHECK: [[META8]] = distinct !{[[META8]]}
+; CHECK: [[ACC_GRP9]] = distinct !{}
+; CHECK: [[TBAA10]] = !{[[META11:![0-9]+]], [[META11]], i64 0, i64 0}
+; CHECK: [[META11]] = !{!"p2 _Foo", [[META12:![0-9]+]]}
+; CHECK: [[META12]] = !{!"any pointer", [[META2]], i64 0}
 ;.



More information about the llvm-commits mailing list