diff --git a/llvm/include/llvm/IR/ValueMap.h b/llvm/include/llvm/IR/ValueMap.h index 063382996d40..23781dba7c31 100644 --- a/llvm/include/llvm/IR/ValueMap.h +++ b/llvm/include/llvm/IR/ValueMap.h @@ -103,7 +103,7 @@ public: explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) : Map(NumInitBuckets), Data(Data) {} - bool hasMD() const { return MDMap; } + bool hasMD() const { return bool(MDMap); } MDMapT &MD() { if (!MDMap) MDMap.reset(new MDMapT); diff --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp index b18af19a9810..472c2cab26d9 100644 --- a/llvm/lib/Transforms/Utils/ValueMapper.cpp +++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp @@ -658,7 +658,7 @@ Optional Mapper::mapSimpleMetadata(const Metadata *MD) { // This is a module-level metadata. If nothing at the module level is // changing, use an identity mapping. if ((Flags & RF_NoModuleLevelChanges)) - return mapToSelf(MD); + return const_cast(MD); if (auto *CMD = dyn_cast(MD)) { // Disallow recursion into metadata mapping through mapValue. diff --git a/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp b/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp index 3bccd503c1bd..5221ab705a30 100644 --- a/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp +++ b/llvm/unittests/Transforms/Utils/ValueMapperTest.cpp @@ -155,6 +155,38 @@ TEST(ValueMapperTest, MapMetadataMDString) { EXPECT_EQ(S2, MapMetadata(S1, VM)); } +TEST(ValueMapperTest, MapMetadataGetMappedMD) { + LLVMContext C; + auto *N0 = MDTuple::get(C, None); + auto *N1 = MDTuple::get(C, N0); + + // Make sure hasMD and getMappedMD work correctly. + ValueToValueMapTy VM; + EXPECT_FALSE(VM.hasMD()); + EXPECT_EQ(N0, MapMetadata(N0, VM)); + EXPECT_EQ(N1, MapMetadata(N1, VM)); + EXPECT_TRUE(VM.hasMD()); + ASSERT_NE(None, VM.getMappedMD(N0)); + ASSERT_NE(None, VM.getMappedMD(N1)); + EXPECT_EQ(N0, *VM.getMappedMD(N0)); + EXPECT_EQ(N1, *VM.getMappedMD(N1)); +} + +TEST(ValueMapperTest, MapMetadataNoModuleLevelChanges) { + LLVMContext C; + auto *N0 = MDTuple::get(C, None); + auto *N1 = MDTuple::get(C, N0); + + // Nothing should be memoized when RF_NoModuleLevelChanges. + ValueToValueMapTy VM; + EXPECT_FALSE(VM.hasMD()); + EXPECT_EQ(N0, MapMetadata(N0, VM, RF_NoModuleLevelChanges)); + EXPECT_EQ(N1, MapMetadata(N1, VM, RF_NoModuleLevelChanges)); + EXPECT_FALSE(VM.hasMD()); + EXPECT_EQ(None, VM.getMappedMD(N0)); + EXPECT_EQ(None, VM.getMappedMD(N1)); +} + TEST(ValueMapperTest, MapMetadataConstantAsMetadata) { LLVMContext C; FunctionType *FTy =