llvm.org GIT mirror llvm / 0b11282
ValueMapper: Stop memoizing ConstantAsMetadata Stop memoizing ConstantAsMetadata in ValueMapper::mapMetadata. Now we have to recompute it, but these metadata aren't particularly common, and it restricts the lifetime of the Metadata map unnecessarily. (The motivation is that I have a patch which uses a single Metadata map for the lifetime of IRMover. Mehdi profiled r266446 with the patch applied and we saw a pretty big speedup in lib/Linker.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266513 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
2 changed file(s) with 43 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
504504 return false;
505505
506506 if (Optional MappedOp = M.mapSimpleMetadata(Op)) {
507 assert((isa(Op) || M.getVM().getMappedMD(Op)) &&
508 "Expected result to be memoized");
507 if (auto *CMD = dyn_cast(Op))
508 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
509 M.getVM().getMappedMD(Op)) &&
510 "Expected Value to be memoized");
511 else
512 assert((isa(Op) || M.getVM().getMappedMD(Op)) &&
513 "Expected result to be memoized");
509514 return *MappedOp != Op;
510515 }
511516
512517 return push(*cast(Op)).HasChangedAddress;
518 }
519
520 static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
521 Value *MappedV) {
522 if (CMD.getValue() == MappedV)
523 return const_cast(&CMD);
524 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
513525 }
514526
515527 Optional MDNodeMapper::getMappedOp(const Metadata *Op) const {
521533
522534 if (isa(Op))
523535 return const_cast(Op);
536
537 if (auto *CMD = dyn_cast(Op))
538 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
524539
525540 return None;
526541 }
719734 return *getMappedOp(&FirstN);
720735 }
721736
737 namespace {
738
739 struct MapMetadataDisabler {
740 ValueToValueMapTy &VM;
741
742 MapMetadataDisabler(ValueToValueMapTy &VM) : VM(VM) {
743 VM.disableMapMetadata();
744 }
745 ~MapMetadataDisabler() { VM.enableMapMetadata(); }
746 };
747
748 } // end namespace
749
722750 Optional Mapper::mapSimpleMetadata(const Metadata *MD) {
723751 // If the value already exists in the map, use it.
724752 if (Optional NewMD = getVM().getMappedMD(MD))
734762
735763 if (auto *CMD = dyn_cast(MD)) {
736764 // Disallow recursion into metadata mapping through mapValue.
737 getVM().disableMapMetadata();
738 Value *MappedV = mapValue(CMD->getValue());
739 getVM().enableMapMetadata();
740
741 if (CMD->getValue() == MappedV)
742 return mapToSelf(MD);
743
744 return mapToMetadata(MD, MappedV ? ValueAsMetadata::get(MappedV) : nullptr);
765 MapMetadataDisabler MMD(getVM());
766
767 // Don't memoize ConstantAsMetadata. Instead of lasting until the
768 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
769 // reference is destructed. These aren't super common, so the extra
770 // indirection isn't that expensive.
771 return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
745772 }
746773
747774 assert(isa(MD) && "Expected a metadata node");
237237
238238 auto *CAM = ConstantAsMetadata::get(F.get());
239239 {
240 // ConstantAsMetadata shouldn't be memoized.
240241 ValueToValueMapTy VM;
241242 EXPECT_EQ(CAM, ValueMapper(VM).mapMetadata(*CAM));
242 EXPECT_TRUE(VM.MD().count(CAM));
243 VM.MD().erase(CAM);
243 EXPECT_FALSE(VM.MD().count(CAM));
244244 EXPECT_EQ(CAM, ValueMapper(VM, RF_IgnoreMissingLocals).mapMetadata(*CAM));
245 EXPECT_TRUE(VM.MD().count(CAM));
246
245 EXPECT_FALSE(VM.MD().count(CAM));
246
247 // But it should respect a mapping that gets seeded.
247248 auto *N = MDTuple::get(C, None);
248249 VM.MD()[CAM].reset(N);
249250 EXPECT_EQ(N, ValueMapper(VM).mapMetadata(*CAM));
255256 ValueToValueMapTy VM;
256257 VM[F.get()] = F2.get();
257258 auto *F2MD = ValueMapper(VM).mapMetadata(*CAM);
258 EXPECT_TRUE(VM.MD().count(CAM));
259 EXPECT_FALSE(VM.MD().count(CAM));
259260 EXPECT_TRUE(F2MD);
260261 EXPECT_EQ(F2.get(), cast(F2MD)->getValue());
261262 }