[llvm] ae94890 - [NFC][Metadata] Define move constructor and move assignment operator for MDOperand.
Wolfgang Pieb via llvm-commits
llvm-commits at lists.llvm.org
Mon May 23 20:15:16 PDT 2022
Author: Wolfgang Pieb
Date: 2022-05-23T20:04:45-07:00
New Revision: ae9489025f1a8021e57a61e79666558ca17afd68
URL: https://github.com/llvm/llvm-project/commit/ae9489025f1a8021e57a61e79666558ca17afd68
DIFF: https://github.com/llvm/llvm-project/commit/ae9489025f1a8021e57a61e79666558ca17afd68.diff
LOG: [NFC][Metadata] Define move constructor and move assignment operator for MDOperand.
This is a preparatory patch for the MDNode resize functionality.
Reviewed By: dexonsmith
Differential Revision: https://reviews.llvm.org/D125994
Added:
Modified:
llvm/include/llvm/IR/Metadata.h
llvm/unittests/IR/MetadataTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index af3e5e967db66..0810a814da819 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -775,10 +775,21 @@ class MDOperand {
public:
MDOperand() = default;
- MDOperand(MDOperand &&) = delete;
MDOperand(const MDOperand &) = delete;
- MDOperand &operator=(MDOperand &&) = delete;
+ MDOperand(MDOperand &&Op) {
+ MD = Op.MD;
+ if (MD)
+ (void)MetadataTracking::retrack(Op.MD, MD);
+ Op.MD = nullptr;
+ }
MDOperand &operator=(const MDOperand &) = delete;
+ MDOperand &operator=(MDOperand &&Op) {
+ MD = Op.MD;
+ if (MD)
+ (void)MetadataTracking::retrack(Op.MD, MD);
+ Op.MD = nullptr;
+ return *this;
+ }
~MDOperand() { untrack(); }
Metadata *get() const { return MD; }
diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp
index 4297e3283338e..41ea3e9c2f5ca 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -3602,4 +3602,33 @@ TEST_F(DebugVariableTest, DenseMap) {
EXPECT_EQ(DebugVariableMap.find(DebugVariableFragB)->second, 12u);
}
+typedef MetadataTest MDTupleAllocationTest;
+TEST_F(MDTupleAllocationTest, Tracking) {
+ // Make sure that the move constructor and move assignment op
+ // for MDOperand correctly adjust tracking information.
+ auto *Value1 = getConstantAsMetadata();
+ MDTuple *A = MDTuple::getDistinct(Context, {Value1, Value1});
+ EXPECT_EQ(A->getOperand(0), Value1);
+ EXPECT_EQ(A->getOperand(1), Value1);
+
+ MDNode::op_range Ops = A->operands();
+
+ MDOperand NewOps1;
+ // Move assignment operator.
+ NewOps1 = std::move(*const_cast<MDOperand *>(Ops.begin()));
+ // Move constructor.
+ MDOperand NewOps2(std::move(*const_cast<MDOperand *>(Ops.begin() + 1)));
+
+ EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value1));
+ EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value1));
+
+ auto *Value2 = getConstantAsMetadata();
+ Value *V1 = Value1->getValue();
+ Value *V2 = Value2->getValue();
+ ValueAsMetadata::handleRAUW(V1, V2);
+
+ EXPECT_EQ(NewOps1.get(), static_cast<Metadata *>(Value2));
+ EXPECT_EQ(NewOps2.get(), static_cast<Metadata *>(Value2));
+}
+
} // end namespace
More information about the llvm-commits
mailing list