- DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
- RF_None, &TypeMap, &ValMaterializer));
+ DestNMD->addOperand(MapMetadata(I->getOperand(i), ValueMap, RF_None,
+ &TypeMap, &ValMaterializer));
+ }
+}
+
+/// Drop DISubprograms that have been superseded.
+///
+/// FIXME: this creates an asymmetric result: we strip functions from losing
+/// subprograms in DstM, but leave losing subprograms in SrcM.
+/// TODO: Remove this logic once the backend can correctly determine canonical
+/// subprograms.
+void ModuleLinker::stripReplacedSubprograms() {
+ // Avoid quadratic runtime by returning early when there's nothing to do.
+ if (OverridingFunctions.empty())
+ return;
+
+ // Move the functions now, so the set gets cleared even on early returns.
+ auto Functions = std::move(OverridingFunctions);
+ OverridingFunctions.clear();
+
+ // Drop functions from subprograms if they've been overridden by the new
+ // compile unit.
+ NamedMDNode *CompileUnits = DstM->getNamedMetadata("llvm.dbg.cu");
+ if (!CompileUnits)
+ return;
+ for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
+ auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
+ assert(CU && "Expected valid compile unit");
+
+ for (DISubprogram *SP : CU->getSubprograms()) {
+ if (!SP || !SP->getFunction() || !Functions.count(SP->getFunction()))
+ continue;
+
+ // Prevent DebugInfoFinder from tagging this as the canonical subprogram,
+ // since the canonical one is in the incoming module.
+ SP->replaceFunction(nullptr);
+ }