ValueMapper: Don't memoize metadata when RF_NoModuleLevelChanges

Prevent the Metadata side-table in ValueMap from growing unnecessarily
when RF_NoModuleLevelChanges.  As a drive-by, make ValueMap::hasMD,
which apparently had no users until I used it here for testing, actually
compile.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265828 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2016-04-08 18:49:36 +00:00
parent ae29a03172
commit 81361659c5
3 changed files with 34 additions and 2 deletions

View File

@ -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);

View File

@ -658,7 +658,7 @@ Optional<Metadata *> 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<Metadata *>(MD);
if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
// Disallow recursion into metadata mapping through mapValue.

View File

@ -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 =