Index: llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h =================================================================== --- llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h +++ llvm/trunk/include/llvm/Transforms/Utils/ValueMapper.h @@ -69,6 +69,10 @@ /// Instruct the remapper to move distinct metadata instead of duplicating /// it when there are module-level changes. RF_MoveDistinctMDs = 4, + + /// Any global values not in value map are mapped to null instead of + /// mapping to self. Illegal if RF_IgnoreMissingEntries is also set. + RF_NullMapMissingGlobalValues = 8, }; static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) { Index: llvm/trunk/lib/Linker/LinkModules.cpp =================================================================== --- llvm/trunk/lib/Linker/LinkModules.cpp +++ llvm/trunk/lib/Linker/LinkModules.cpp @@ -1628,8 +1628,9 @@ NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName()); // Add Src elements into Dest node. for (const MDNode *op : NMD.operands()) - DestNMD->addOperand(MapMetadata(op, ValueMap, RF_MoveDistinctMDs, - &TypeMap, &ValMaterializer)); + DestNMD->addOperand(MapMetadata( + op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues, + &TypeMap, &ValMaterializer)); } } Index: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp =================================================================== --- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp +++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp @@ -42,9 +42,16 @@ // Global values do not need to be seeded into the VM if they // are using the identity mapping. - if (isa(V)) + if (isa(V)) { + if (Flags & RF_NullMapMissingGlobalValues) { + assert(!(Flags & RF_IgnoreMissingEntries) && + "Illegal to specify both RF_NullMapMissingGlobalValues and " + "RF_IgnoreMissingEntries"); + return nullptr; + } return VM[V] = const_cast(V); - + } + if (const InlineAsm *IA = dyn_cast(V)) { // Inline asm may need *type* remapping. FunctionType *NewTy = IA->getFunctionType(); @@ -74,7 +81,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // assert(MappedMD && "Referenced metadata value not in value map"); + // assert((MappedMD || (Flags & RF_NullMapMissingGlobalValues)) && + // "Referenced metadata value not in value map"); return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD); } @@ -184,7 +192,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // llvm_unreachable("Referenced metadata not in value map!"); + // assert((Flags & RF_NullMapMissingGlobalValues) && + // "Referenced metadata not in value map!"); return nullptr; } @@ -305,7 +314,8 @@ // correct. For now, just match behaviour from before the metadata/value // split. // - // assert(MappedV && "Referenced metadata not in value map!"); + // assert((MappedV || (Flags & RF_NullMapMissingGlobalValues)) && + // "Referenced metadata not in value map!"); if (MappedV) return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV)); return nullptr;