[llvm] r265828 - ValueMapper: Don't memoize metadata when RF_NoModuleLevelChanges

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 8 11:49:36 PDT 2016


Author: dexonsmith
Date: Fri Apr  8 13:49:36 2016
New Revision: 265828

URL: http://llvm.org/viewvc/llvm-project?rev=265828&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/IR/ValueMap.h
    llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
    llvm/trunk/unittests/Transforms/Utils/ValueMapperTest.cpp

Modified: llvm/trunk/include/llvm/IR/ValueMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/ValueMap.h?rev=265828&r1=265827&r2=265828&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/ValueMap.h (original)
+++ llvm/trunk/include/llvm/IR/ValueMap.h Fri Apr  8 13:49:36 2016
@@ -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);

Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=265828&r1=265827&r2=265828&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Fri Apr  8 13:49:36 2016
@@ -658,7 +658,7 @@ Optional<Metadata *> Mapper::mapSimpleMe
   // 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.

Modified: llvm/trunk/unittests/Transforms/Utils/ValueMapperTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Utils/ValueMapperTest.cpp?rev=265828&r1=265827&r2=265828&view=diff
==============================================================================
--- llvm/trunk/unittests/Transforms/Utils/ValueMapperTest.cpp (original)
+++ llvm/trunk/unittests/Transforms/Utils/ValueMapperTest.cpp Fri Apr  8 13:49:36 2016
@@ -155,6 +155,38 @@ TEST(ValueMapperTest, MapMetadataMDStrin
   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 =




More information about the llvm-commits mailing list