1 //===- lib/Linker/IRMover.cpp ---------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Linker/IRMover.h"
11 #include "LinkDiagnosticInfo.h"
12 #include "llvm/ADT/SetVector.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/IR/DiagnosticPrinter.h"
18 #include "llvm/IR/GVMaterializer.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/IR/TypeFinder.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
26 //===----------------------------------------------------------------------===//
27 // TypeMap implementation.
28 //===----------------------------------------------------------------------===//
31 class TypeMapTy : public ValueMapTypeRemapper {
32 /// This is a mapping from a source type to a destination type to use.
33 DenseMap<Type *, Type *> MappedTypes;
35 /// When checking to see if two subgraphs are isomorphic, we speculatively
36 /// add types to MappedTypes, but keep track of them here in case we need to
38 SmallVector<Type *, 16> SpeculativeTypes;
40 SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
42 /// This is a list of non-opaque structs in the source module that are mapped
43 /// to an opaque struct in the destination module.
44 SmallVector<StructType *, 16> SrcDefinitionsToResolve;
46 /// This is the set of opaque types in the destination modules who are
47 /// getting a body from the source module.
48 SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
51 TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
52 : DstStructTypesSet(DstStructTypesSet) {}
54 IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
55 /// Indicate that the specified type in the destination module is conceptually
56 /// equivalent to the specified type in the source module.
57 void addTypeMapping(Type *DstTy, Type *SrcTy);
59 /// Produce a body for an opaque type in the dest module from a type
60 /// definition in the source module.
61 void linkDefinedTypeBodies();
63 /// Return the mapped type to use for the specified input type from the
65 Type *get(Type *SrcTy);
66 Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
68 void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
70 FunctionType *get(FunctionType *T) {
71 return cast<FunctionType>(get((Type *)T));
75 Type *remapType(Type *SrcTy) override { return get(SrcTy); }
77 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
81 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
82 assert(SpeculativeTypes.empty());
83 assert(SpeculativeDstOpaqueTypes.empty());
85 // Check to see if these types are recursively isomorphic and establish a
86 // mapping between them if so.
87 if (!areTypesIsomorphic(DstTy, SrcTy)) {
88 // Oops, they aren't isomorphic. Just discard this request by rolling out
89 // any speculative mappings we've established.
90 for (Type *Ty : SpeculativeTypes)
91 MappedTypes.erase(Ty);
93 SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
94 SpeculativeDstOpaqueTypes.size());
95 for (StructType *Ty : SpeculativeDstOpaqueTypes)
96 DstResolvedOpaqueTypes.erase(Ty);
98 for (Type *Ty : SpeculativeTypes)
99 if (auto *STy = dyn_cast<StructType>(Ty))
103 SpeculativeTypes.clear();
104 SpeculativeDstOpaqueTypes.clear();
107 /// Recursively walk this pair of types, returning true if they are isomorphic,
108 /// false if they are not.
109 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
110 // Two types with differing kinds are clearly not isomorphic.
111 if (DstTy->getTypeID() != SrcTy->getTypeID())
114 // If we have an entry in the MappedTypes table, then we have our answer.
115 Type *&Entry = MappedTypes[SrcTy];
117 return Entry == DstTy;
119 // Two identical types are clearly isomorphic. Remember this
120 // non-speculatively.
121 if (DstTy == SrcTy) {
126 // Okay, we have two types with identical kinds that we haven't seen before.
128 // If this is an opaque struct type, special case it.
129 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
130 // Mapping an opaque type to any struct, just keep the dest struct.
131 if (SSTy->isOpaque()) {
133 SpeculativeTypes.push_back(SrcTy);
137 // Mapping a non-opaque source type to an opaque dest. If this is the first
138 // type that we're mapping onto this destination type then we succeed. Keep
139 // the dest, but fill it in later. If this is the second (different) type
140 // that we're trying to map onto the same opaque type then we fail.
141 if (cast<StructType>(DstTy)->isOpaque()) {
142 // We can only map one source type onto the opaque destination type.
143 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
145 SrcDefinitionsToResolve.push_back(SSTy);
146 SpeculativeTypes.push_back(SrcTy);
147 SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
153 // If the number of subtypes disagree between the two types, then we fail.
154 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
157 // Fail if any of the extra properties (e.g. array size) of the type disagree.
158 if (isa<IntegerType>(DstTy))
159 return false; // bitwidth disagrees.
160 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
161 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
164 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
165 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
167 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
168 StructType *SSTy = cast<StructType>(SrcTy);
169 if (DSTy->isLiteral() != SSTy->isLiteral() ||
170 DSTy->isPacked() != SSTy->isPacked())
172 } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
173 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
175 } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
176 if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
180 // Otherwise, we speculate that these two types will line up and recursively
181 // check the subelements.
183 SpeculativeTypes.push_back(SrcTy);
185 for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
186 if (!areTypesIsomorphic(DstTy->getContainedType(I),
187 SrcTy->getContainedType(I)))
190 // If everything seems to have lined up, then everything is great.
194 void TypeMapTy::linkDefinedTypeBodies() {
195 SmallVector<Type *, 16> Elements;
196 for (StructType *SrcSTy : SrcDefinitionsToResolve) {
197 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
198 assert(DstSTy->isOpaque());
200 // Map the body of the source type over to a new body for the dest type.
201 Elements.resize(SrcSTy->getNumElements());
202 for (unsigned I = 0, E = Elements.size(); I != E; ++I)
203 Elements[I] = get(SrcSTy->getElementType(I));
205 DstSTy->setBody(Elements, SrcSTy->isPacked());
206 DstStructTypesSet.switchToNonOpaque(DstSTy);
208 SrcDefinitionsToResolve.clear();
209 DstResolvedOpaqueTypes.clear();
212 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
213 ArrayRef<Type *> ETypes) {
214 DTy->setBody(ETypes, STy->isPacked());
217 if (STy->hasName()) {
218 SmallString<16> TmpName = STy->getName();
220 DTy->setName(TmpName);
223 DstStructTypesSet.addNonOpaque(DTy);
226 Type *TypeMapTy::get(Type *Ty) {
227 SmallPtrSet<StructType *, 8> Visited;
228 return get(Ty, Visited);
231 Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
232 // If we already have an entry for this type, return it.
233 Type **Entry = &MappedTypes[Ty];
237 // These are types that LLVM itself will unique.
238 bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
242 for (auto &Pair : MappedTypes) {
243 assert(!(Pair.first != Ty && Pair.second == Ty) &&
244 "mapping to a source type");
249 if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
250 StructType *DTy = StructType::create(Ty->getContext());
254 // If this is not a recursive type, then just map all of the elements and
255 // then rebuild the type from inside out.
256 SmallVector<Type *, 4> ElementTypes;
258 // If there are no element types to map, then the type is itself. This is
259 // true for the anonymous {} struct, things like 'float', integers, etc.
260 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
263 // Remap all of the elements, keeping track of whether any of them change.
264 bool AnyChange = false;
265 ElementTypes.resize(Ty->getNumContainedTypes());
266 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
267 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
268 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
271 // If we found our type while recursively processing stuff, just use it.
272 Entry = &MappedTypes[Ty];
274 if (auto *DTy = dyn_cast<StructType>(*Entry)) {
275 if (DTy->isOpaque()) {
276 auto *STy = cast<StructType>(Ty);
277 finishType(DTy, STy, ElementTypes);
283 // If all of the element types mapped directly over and the type is not
284 // a nomed struct, then the type is usable as-is.
285 if (!AnyChange && IsUniqued)
288 // Otherwise, rebuild a modified type.
289 switch (Ty->getTypeID()) {
291 llvm_unreachable("unknown derived type to remap");
292 case Type::ArrayTyID:
293 return *Entry = ArrayType::get(ElementTypes[0],
294 cast<ArrayType>(Ty)->getNumElements());
295 case Type::VectorTyID:
296 return *Entry = VectorType::get(ElementTypes[0],
297 cast<VectorType>(Ty)->getNumElements());
298 case Type::PointerTyID:
299 return *Entry = PointerType::get(ElementTypes[0],
300 cast<PointerType>(Ty)->getAddressSpace());
301 case Type::FunctionTyID:
302 return *Entry = FunctionType::get(ElementTypes[0],
303 makeArrayRef(ElementTypes).slice(1),
304 cast<FunctionType>(Ty)->isVarArg());
305 case Type::StructTyID: {
306 auto *STy = cast<StructType>(Ty);
307 bool IsPacked = STy->isPacked();
309 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
311 // If the type is opaque, we can just use it directly.
312 if (STy->isOpaque()) {
313 DstStructTypesSet.addOpaque(STy);
317 if (StructType *OldT =
318 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
320 return *Entry = OldT;
324 DstStructTypesSet.addNonOpaque(STy);
328 StructType *DTy = StructType::create(Ty->getContext());
329 finishType(DTy, STy, ElementTypes);
335 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
337 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
338 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
340 //===----------------------------------------------------------------------===//
341 // IRLinker implementation.
342 //===----------------------------------------------------------------------===//
347 /// Creates prototypes for functions that are lazily linked on the fly. This
348 /// speeds up linking for modules with many/ lazily linked functions of which
350 class GlobalValueMaterializer final : public ValueMaterializer {
351 IRLinker &TheIRLinker;
354 GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
355 Value *materialize(Value *V) override;
358 class LocalValueMaterializer final : public ValueMaterializer {
359 IRLinker &TheIRLinker;
362 LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
363 Value *materialize(Value *V) override;
366 /// Type of the Metadata map in \a ValueToValueMapTy.
367 typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
369 /// This is responsible for keeping track of the state used for moving data
370 /// from SrcM to DstM.
373 std::unique_ptr<Module> SrcM;
375 /// See IRMover::move().
376 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
379 GlobalValueMaterializer GValMaterializer;
380 LocalValueMaterializer LValMaterializer;
382 /// A metadata map that's shared between IRLinker instances.
385 /// Mapping of values from what they used to be in Src, to what they are now
386 /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
387 /// due to the use of Value handles which the Linker doesn't actually need,
388 /// but this allows us to reuse the ValueMapper code.
389 ValueToValueMapTy ValueMap;
390 ValueToValueMapTy AliasValueMap;
392 DenseSet<GlobalValue *> ValuesToLink;
393 std::vector<GlobalValue *> Worklist;
395 void maybeAdd(GlobalValue *GV) {
396 if (ValuesToLink.insert(GV).second)
397 Worklist.push_back(GV);
400 /// Set to true when all global value body linking is complete (including
401 /// lazy linking). Used to prevent metadata linking from creating new
403 bool DoneLinkingBodies = false;
405 /// The Error encountered during materialization. We use an Optional here to
406 /// avoid needing to manage an unconsumed success value.
407 Optional<Error> FoundError;
408 void setError(Error E) {
410 FoundError = std::move(E);
413 /// Most of the errors produced by this module are inconvertible StringErrors.
414 /// This convenience function lets us return one of those more easily.
415 Error stringErr(const Twine &T) {
416 return make_error<StringError>(T, inconvertibleErrorCode());
419 /// Entry point for mapping values and alternate context for mapping aliases.
423 /// Handles cloning of a global values from the source module into
424 /// the destination module, including setting the attributes and visibility.
425 GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
427 void emitWarning(const Twine &Message) {
428 SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
431 /// Given a global in the source module, return the global in the
432 /// destination module that is being linked to, if any.
433 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
434 // If the source has no name it can't link. If it has local linkage,
435 // there is no name match-up going on.
436 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
439 // Otherwise see if we have a match in the destination module's symtab.
440 GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
444 // If we found a global with the same name in the dest module, but it has
445 // internal linkage, we are really not doing any linkage here.
446 if (DGV->hasLocalLinkage())
449 // Otherwise, we do in fact link to the destination global.
453 void computeTypeMapping();
455 Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
456 const GlobalVariable *SrcGV);
458 /// Given the GlobaValue \p SGV in the source module, and the matching
459 /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
460 /// into the destination module.
462 /// Note this code may call the client-provided \p AddLazyFor.
463 bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
464 Expected<Constant *> linkGlobalValueProto(GlobalValue *GV, bool ForAlias);
466 Error linkModuleFlagsMetadata();
468 void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
469 Error linkFunctionBody(Function &Dst, Function &Src);
470 void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
471 Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
473 /// Functions that take care of cloning a specific global value type
474 /// into the destination module.
475 GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
476 Function *copyFunctionProto(const Function *SF);
477 GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
479 void linkNamedMDNodes();
482 IRLinker(Module &DstM, MDMapT &SharedMDs,
483 IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
484 ArrayRef<GlobalValue *> ValuesToLink,
485 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor)
486 : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
487 TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
488 SharedMDs(SharedMDs),
489 Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap,
491 AliasMCID(Mapper.registerAlternateMappingContext(AliasValueMap,
492 &LValMaterializer)) {
493 ValueMap.getMDMap() = std::move(SharedMDs);
494 for (GlobalValue *GV : ValuesToLink)
497 ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
500 Value *materialize(Value *V, bool ForAlias);
504 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
505 /// table. This is good for all clients except for us. Go through the trouble
506 /// to force this back.
507 static void forceRenaming(GlobalValue *GV, StringRef Name) {
508 // If the global doesn't force its name or if it already has the right name,
509 // there is nothing for us to do.
510 if (GV->hasLocalLinkage() || GV->getName() == Name)
513 Module *M = GV->getParent();
515 // If there is a conflict, rename the conflict.
516 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
517 GV->takeName(ConflictGV);
518 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
519 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
521 GV->setName(Name); // Force the name back
525 Value *GlobalValueMaterializer::materialize(Value *SGV) {
526 return TheIRLinker.materialize(SGV, false);
529 Value *LocalValueMaterializer::materialize(Value *SGV) {
530 return TheIRLinker.materialize(SGV, true);
533 Value *IRLinker::materialize(Value *V, bool ForAlias) {
534 auto *SGV = dyn_cast<GlobalValue>(V);
538 Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForAlias);
540 setError(NewProto.takeError());
546 GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
550 // If we already created the body, just return.
551 if (auto *F = dyn_cast<Function>(New)) {
552 if (!F->isDeclaration())
554 } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
555 if (V->hasInitializer() || V->hasAppendingLinkage())
558 auto *A = cast<GlobalAlias>(New);
563 // When linking a global for an alias, it will always be linked. However we
564 // need to check if it was not already scheduled to satify a reference from a
565 // regular global value initializer. We know if it has been schedule if the
566 // "New" GlobalValue that is mapped here for the alias is the same as the one
567 // already mapped. If there is an entry in the ValueMap but the value is
568 // different, it means that the value already had a definition in the
569 // destination module (linkonce for instance), but we need a new definition
570 // for the alias ("New" will be different.
571 if (ForAlias && ValueMap.lookup(SGV) == New)
574 if (ForAlias || shouldLink(New, *SGV))
575 setError(linkGlobalValueBody(*New, *SGV));
580 /// Loop through the global variables in the src module and merge them into the
582 GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
583 // No linking to be performed or linking from the source: simply create an
584 // identical version of the symbol over in the dest module... the
585 // initializer will be filled in later by LinkGlobalInits.
586 GlobalVariable *NewDGV =
587 new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
588 SGVar->isConstant(), GlobalValue::ExternalLinkage,
589 /*init*/ nullptr, SGVar->getName(),
590 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
591 SGVar->getType()->getAddressSpace());
592 NewDGV->setAlignment(SGVar->getAlignment());
596 /// Link the function in the source module into the destination module if
597 /// needed, setting up mapping information.
598 Function *IRLinker::copyFunctionProto(const Function *SF) {
599 // If there is no linkage to be performed or we are linking from the source,
601 return Function::Create(TypeMap.get(SF->getFunctionType()),
602 GlobalValue::ExternalLinkage, SF->getName(), &DstM);
605 /// Set up prototypes for any aliases that come over from the source module.
606 GlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
607 // If there is no linkage to be performed or we're linking from the source,
609 auto *Ty = TypeMap.get(SGA->getValueType());
610 return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
611 GlobalValue::ExternalLinkage, SGA->getName(),
615 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
616 bool ForDefinition) {
618 if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
619 NewGV = copyGlobalVariableProto(SGVar);
620 } else if (auto *SF = dyn_cast<Function>(SGV)) {
621 NewGV = copyFunctionProto(SF);
624 NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
626 NewGV = new GlobalVariable(
627 DstM, TypeMap.get(SGV->getValueType()),
628 /*isConstant*/ false, GlobalValue::ExternalLinkage,
629 /*init*/ nullptr, SGV->getName(),
630 /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
631 SGV->getType()->getAddressSpace());
635 NewGV->setLinkage(SGV->getLinkage());
636 else if (SGV->hasExternalWeakLinkage())
637 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
639 NewGV->copyAttributesFrom(SGV);
641 if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
642 // Metadata for global variables and function declarations is copied eagerly.
643 if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
644 NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
647 // Remove these copied constants in case this stays a declaration, since
648 // they point to the source module. If the def is linked the values will
649 // be mapped in during linkFunctionBody.
650 if (auto *NewF = dyn_cast<Function>(NewGV)) {
651 NewF->setPersonalityFn(nullptr);
652 NewF->setPrefixData(nullptr);
653 NewF->setPrologueData(nullptr);
659 /// Loop over all of the linked values to compute type mappings. For example,
660 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
661 /// types 'Foo' but one got renamed when the module was loaded into the same
663 void IRLinker::computeTypeMapping() {
664 for (GlobalValue &SGV : SrcM->globals()) {
665 GlobalValue *DGV = getLinkedToGlobal(&SGV);
669 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
670 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
674 // Unify the element type of appending arrays.
675 ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
676 ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
677 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
680 for (GlobalValue &SGV : *SrcM)
681 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
682 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
684 for (GlobalValue &SGV : SrcM->aliases())
685 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
686 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
688 // Incorporate types by name, scanning all the types in the source module.
689 // At this point, the destination module may have a type "%foo = { i32 }" for
690 // example. When the source module got loaded into the same LLVMContext, if
691 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
692 std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
693 for (StructType *ST : Types) {
697 // Check to see if there is a dot in the name followed by a digit.
698 size_t DotPos = ST->getName().rfind('.');
699 if (DotPos == 0 || DotPos == StringRef::npos ||
700 ST->getName().back() == '.' ||
701 !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
704 // Check to see if the destination module has a struct with the prefix name.
705 StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
709 // Don't use it if this actually came from the source module. They're in
710 // the same LLVMContext after all. Also don't use it unless the type is
711 // actually used in the destination module. This can happen in situations
716 // %Z = type { %A } %B = type { %C.1 }
717 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
718 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
719 // %C = type { i8* } %B.3 = type { %C.1 }
721 // When we link Module B with Module A, the '%B' in Module B is
722 // used. However, that would then use '%C.1'. But when we process '%C.1',
723 // we prefer to take the '%C' version. So we are then left with both
724 // '%C.1' and '%C' being used for the same types. This leads to some
725 // variables using one type and some using the other.
726 if (TypeMap.DstStructTypesSet.hasType(DST))
727 TypeMap.addTypeMapping(DST, ST);
730 // Now that we have discovered all of the type equivalences, get a body for
731 // any 'opaque' types in the dest module that are now resolved.
732 TypeMap.linkDefinedTypeBodies();
735 static void getArrayElements(const Constant *C,
736 SmallVectorImpl<Constant *> &Dest) {
737 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
739 for (unsigned i = 0; i != NumElements; ++i)
740 Dest.push_back(C->getAggregateElement(i));
743 /// If there were any appending global variables, link them together now.
745 IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
746 const GlobalVariable *SrcGV) {
747 Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
750 // FIXME: This upgrade is done during linking to support the C API. Once the
751 // old form is deprecated, we should move this upgrade to
752 // llvm::UpgradeGlobalVariable() and simplify the logic here and in
753 // Mapper::mapAppendingVariable() in ValueMapper.cpp.
754 StringRef Name = SrcGV->getName();
755 bool IsNewStructor = false;
756 bool IsOldStructor = false;
757 if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
758 if (cast<StructType>(EltTy)->getNumElements() == 3)
759 IsNewStructor = true;
761 IsOldStructor = true;
764 PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
766 auto &ST = *cast<StructType>(EltTy);
767 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
768 EltTy = StructType::get(SrcGV->getContext(), Tys, false);
771 uint64_t DstNumElements = 0;
773 ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
774 DstNumElements = DstTy->getNumElements();
776 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
778 "Linking globals named '" + SrcGV->getName() +
779 "': can only link appending global with another appending "
782 // Check to see that they two arrays agree on type.
783 if (EltTy != DstTy->getElementType())
784 return stringErr("Appending variables with different element types!");
785 if (DstGV->isConstant() != SrcGV->isConstant())
786 return stringErr("Appending variables linked with different const'ness!");
788 if (DstGV->getAlignment() != SrcGV->getAlignment())
790 "Appending variables with different alignment need to be linked!");
792 if (DstGV->getVisibility() != SrcGV->getVisibility())
794 "Appending variables with different visibility need to be linked!");
796 if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
798 "Appending variables with different unnamed_addr need to be linked!");
800 if (DstGV->getSection() != SrcGV->getSection())
802 "Appending variables with different section name need to be linked!");
805 SmallVector<Constant *, 16> SrcElements;
806 getArrayElements(SrcGV->getInitializer(), SrcElements);
810 std::remove_if(SrcElements.begin(), SrcElements.end(),
811 [this](Constant *E) {
812 auto *Key = dyn_cast<GlobalValue>(
813 E->getAggregateElement(2)->stripPointerCasts());
816 GlobalValue *DGV = getLinkedToGlobal(Key);
817 return !shouldLink(DGV, *Key);
820 uint64_t NewSize = DstNumElements + SrcElements.size();
821 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
823 // Create the new global variable.
824 GlobalVariable *NG = new GlobalVariable(
825 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
826 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
827 SrcGV->getType()->getAddressSpace());
829 NG->copyAttributesFrom(SrcGV);
830 forceRenaming(NG, SrcGV->getName());
832 Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
834 Mapper.scheduleMapAppendingVariable(*NG,
835 DstGV ? DstGV->getInitializer() : nullptr,
836 IsOldStructor, SrcElements);
838 // Replace any uses of the two global variables with uses of the new
841 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
842 DstGV->eraseFromParent();
848 bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
849 if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())
852 if (DGV && !DGV->isDeclarationForLinker())
855 if (SGV.hasAvailableExternallyLinkage())
858 if (SGV.isDeclaration() || DoneLinkingBodies)
861 // Callback to the client to give a chance to lazily add the Global to the
862 // list of value to link.
863 bool LazilyAdded = false;
864 AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
871 Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
873 GlobalValue *DGV = getLinkedToGlobal(SGV);
875 bool ShouldLink = shouldLink(DGV, *SGV);
877 // just missing from map
879 auto I = ValueMap.find(SGV);
880 if (I != ValueMap.end())
881 return cast<Constant>(I->second);
883 I = AliasValueMap.find(SGV);
884 if (I != AliasValueMap.end())
885 return cast<Constant>(I->second);
888 if (!ShouldLink && ForAlias)
891 // Handle the ultra special appending linkage case first.
892 assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
893 if (SGV->hasAppendingLinkage())
894 return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
895 cast<GlobalVariable>(SGV));
898 if (DGV && !ShouldLink) {
901 // If we are done linking global value bodies (i.e. we are performing
902 // metadata linking), don't link in the global value due to this
903 // reference, simply map it to null.
904 if (DoneLinkingBodies)
907 NewGV = copyGlobalValueProto(SGV, ShouldLink);
908 if (ShouldLink || !ForAlias)
909 forceRenaming(NewGV, SGV->getName());
912 // Overloaded intrinsics have overloaded types names as part of their
913 // names. If we renamed overloaded types we should rename the intrinsic
915 if (Function *F = dyn_cast<Function>(NewGV))
916 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F))
917 NewGV = Remangled.getValue();
919 if (ShouldLink || ForAlias) {
920 if (const Comdat *SC = SGV->getComdat()) {
921 if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
922 Comdat *DC = DstM.getOrInsertComdat(SC->getName());
923 DC->setSelectionKind(SC->getSelectionKind());
929 if (!ShouldLink && ForAlias)
930 NewGV->setLinkage(GlobalValue::InternalLinkage);
934 C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType()));
936 if (DGV && NewGV != DGV) {
937 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
938 DGV->eraseFromParent();
944 /// Update the initializers in the Dest module now that all globals that may be
945 /// referenced are in Dest.
946 void IRLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
947 // Figure out what the initializer looks like in the dest module.
948 Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
951 /// Copy the source function over into the dest function and fix up references
952 /// to values. At this point we know that Dest is an external function, and
954 Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
955 assert(Dst.isDeclaration() && !Src.isDeclaration());
957 // Materialize if needed.
958 if (std::error_code EC = Src.materialize())
959 return errorCodeToError(EC);
961 // Link in the operands without remapping.
962 if (Src.hasPrefixData())
963 Dst.setPrefixData(Src.getPrefixData());
964 if (Src.hasPrologueData())
965 Dst.setPrologueData(Src.getPrologueData());
966 if (Src.hasPersonalityFn())
967 Dst.setPersonalityFn(Src.getPersonalityFn());
969 // Copy over the metadata attachments without remapping.
970 Dst.copyMetadata(&Src, 0);
972 // Steal arguments and splice the body of Src into Dst.
973 Dst.stealArgumentListFrom(Src);
974 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
976 // Everything has been moved over. Remap it.
977 Mapper.scheduleRemapFunction(Dst);
978 return Error::success();
981 void IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
982 Mapper.scheduleMapGlobalAliasee(Dst, *Src.getAliasee(), AliasMCID);
985 Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
986 if (auto *F = dyn_cast<Function>(&Src))
987 return linkFunctionBody(cast<Function>(Dst), *F);
988 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
989 linkGlobalInit(cast<GlobalVariable>(Dst), *GVar);
990 return Error::success();
992 linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
993 return Error::success();
996 /// Insert all of the named MDNodes in Src into the Dest module.
997 void IRLinker::linkNamedMDNodes() {
998 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
999 for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1000 // Don't link module flags here. Do them separately.
1001 if (&NMD == SrcModFlags)
1003 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1004 // Add Src elements into Dest node.
1005 for (const MDNode *Op : NMD.operands())
1006 DestNMD->addOperand(Mapper.mapMDNode(*Op));
1010 /// Merge the linker flags in Src into the Dest module.
1011 Error IRLinker::linkModuleFlagsMetadata() {
1012 // If the source module has no module flags, we are done.
1013 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1015 return Error::success();
1017 // If the destination module doesn't have module flags yet, then just copy
1018 // over the source module's flags.
1019 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1020 if (DstModFlags->getNumOperands() == 0) {
1021 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1022 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1024 return Error::success();
1027 // First build a map of the existing module flags and requirements.
1028 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1029 SmallSetVector<MDNode *, 16> Requirements;
1030 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1031 MDNode *Op = DstModFlags->getOperand(I);
1032 ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1033 MDString *ID = cast<MDString>(Op->getOperand(1));
1035 if (Behavior->getZExtValue() == Module::Require) {
1036 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1038 Flags[ID] = std::make_pair(Op, I);
1042 // Merge in the flags from the source module, and also collect its set of
1044 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1045 MDNode *SrcOp = SrcModFlags->getOperand(I);
1046 ConstantInt *SrcBehavior =
1047 mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1048 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1051 std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1052 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1054 // If this is a requirement, add it and continue.
1055 if (SrcBehaviorValue == Module::Require) {
1056 // If the destination module does not already have this requirement, add
1058 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1059 DstModFlags->addOperand(SrcOp);
1064 // If there is no existing flag with this ID, just add it.
1066 Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1067 DstModFlags->addOperand(SrcOp);
1071 // Otherwise, perform a merge.
1072 ConstantInt *DstBehavior =
1073 mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1074 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1076 // If either flag has override behavior, handle it first.
1077 if (DstBehaviorValue == Module::Override) {
1078 // Diagnose inconsistent flags which both have override behavior.
1079 if (SrcBehaviorValue == Module::Override &&
1080 SrcOp->getOperand(2) != DstOp->getOperand(2))
1081 return stringErr("linking module flags '" + ID->getString() +
1082 "': IDs have conflicting override values");
1084 } else if (SrcBehaviorValue == Module::Override) {
1085 // Update the destination flag to that of the source.
1086 DstModFlags->setOperand(DstIndex, SrcOp);
1087 Flags[ID].first = SrcOp;
1091 // Diagnose inconsistent merge behavior types.
1092 if (SrcBehaviorValue != DstBehaviorValue)
1093 return stringErr("linking module flags '" + ID->getString() +
1094 "': IDs have conflicting behaviors");
1096 auto replaceDstValue = [&](MDNode *New) {
1097 Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1098 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1099 DstModFlags->setOperand(DstIndex, Flag);
1100 Flags[ID].first = Flag;
1103 // Perform the merge for standard behavior types.
1104 switch (SrcBehaviorValue) {
1105 case Module::Require:
1106 case Module::Override:
1107 llvm_unreachable("not possible");
1108 case Module::Error: {
1109 // Emit an error if the values differ.
1110 if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1111 return stringErr("linking module flags '" + ID->getString() +
1112 "': IDs have conflicting values");
1115 case Module::Warning: {
1116 // Emit a warning if the values differ.
1117 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1118 emitWarning("linking module flags '" + ID->getString() +
1119 "': IDs have conflicting values");
1123 case Module::Append: {
1124 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1125 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1126 SmallVector<Metadata *, 8> MDs;
1127 MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1128 MDs.append(DstValue->op_begin(), DstValue->op_end());
1129 MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1131 replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1134 case Module::AppendUnique: {
1135 SmallSetVector<Metadata *, 16> Elts;
1136 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1137 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1138 Elts.insert(DstValue->op_begin(), DstValue->op_end());
1139 Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1141 replaceDstValue(MDNode::get(DstM.getContext(),
1142 makeArrayRef(Elts.begin(), Elts.end())));
1148 // Check all of the requirements.
1149 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1150 MDNode *Requirement = Requirements[I];
1151 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1152 Metadata *ReqValue = Requirement->getOperand(1);
1154 MDNode *Op = Flags[Flag].first;
1155 if (!Op || Op->getOperand(2) != ReqValue)
1156 return stringErr("linking module flags '" + Flag->getString() +
1157 "': does not have the required value");
1159 return Error::success();
1162 // This function returns true if the triples match.
1163 static bool triplesMatch(const Triple &T0, const Triple &T1) {
1164 // If vendor is apple, ignore the version number.
1165 if (T0.getVendor() == Triple::Apple)
1166 return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() &&
1167 T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS();
1172 // This function returns the merged triple.
1173 static std::string mergeTriples(const Triple &SrcTriple,
1174 const Triple &DstTriple) {
1175 // If vendor is apple, pick the triple with the larger version number.
1176 if (SrcTriple.getVendor() == Triple::Apple)
1177 if (DstTriple.isOSVersionLT(SrcTriple))
1178 return SrcTriple.str();
1180 return DstTriple.str();
1183 Error IRLinker::run() {
1184 // Ensure metadata materialized before value mapping.
1185 if (SrcM->getMaterializer())
1186 if (std::error_code EC = SrcM->getMaterializer()->materializeMetadata())
1187 return errorCodeToError(EC);
1189 // Inherit the target data from the source module if the destination module
1190 // doesn't have one already.
1191 if (DstM.getDataLayout().isDefault())
1192 DstM.setDataLayout(SrcM->getDataLayout());
1194 if (SrcM->getDataLayout() != DstM.getDataLayout()) {
1195 emitWarning("Linking two modules of different data layouts: '" +
1196 SrcM->getModuleIdentifier() + "' is '" +
1197 SrcM->getDataLayoutStr() + "' whereas '" +
1198 DstM.getModuleIdentifier() + "' is '" +
1199 DstM.getDataLayoutStr() + "'\n");
1202 // Copy the target triple from the source to dest if the dest's is empty.
1203 if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1204 DstM.setTargetTriple(SrcM->getTargetTriple());
1206 Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1208 if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
1209 emitWarning("Linking two modules of different target triples: " +
1210 SrcM->getModuleIdentifier() + "' is '" +
1211 SrcM->getTargetTriple() + "' whereas '" +
1212 DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1215 DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
1217 // Append the module inline asm string.
1218 if (!SrcM->getModuleInlineAsm().empty()) {
1219 if (DstM.getModuleInlineAsm().empty())
1220 DstM.setModuleInlineAsm(SrcM->getModuleInlineAsm());
1222 DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1223 SrcM->getModuleInlineAsm());
1226 // Loop over all of the linked values to compute type mappings.
1227 computeTypeMapping();
1229 std::reverse(Worklist.begin(), Worklist.end());
1230 while (!Worklist.empty()) {
1231 GlobalValue *GV = Worklist.back();
1232 Worklist.pop_back();
1235 if (ValueMap.find(GV) != ValueMap.end() ||
1236 AliasValueMap.find(GV) != AliasValueMap.end())
1239 assert(!GV->isDeclaration());
1240 Mapper.mapValue(*GV);
1242 return std::move(*FoundError);
1245 // Note that we are done linking global value bodies. This prevents
1246 // metadata linking from creating new references.
1247 DoneLinkingBodies = true;
1248 Mapper.addFlags(RF_NullMapMissingGlobalValues);
1250 // Remap all of the named MDNodes in Src into the DstM module. We do this
1251 // after linking GlobalValues so that MDNodes that reference GlobalValues
1252 // are properly remapped.
1255 // Merge the module flags into the DstM module.
1256 return linkModuleFlagsMetadata();
1259 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1260 : ETypes(E), IsPacked(P) {}
1262 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1263 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1265 bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1266 return IsPacked == That.IsPacked && ETypes == That.ETypes;
1269 bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1270 return !this->operator==(That);
1273 StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1274 return DenseMapInfo<StructType *>::getEmptyKey();
1277 StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1278 return DenseMapInfo<StructType *>::getTombstoneKey();
1281 unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1282 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1286 unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1287 return getHashValue(KeyTy(ST));
1290 bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1291 const StructType *RHS) {
1292 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1294 return LHS == KeyTy(RHS);
1297 bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1298 const StructType *RHS) {
1299 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1301 return KeyTy(LHS) == KeyTy(RHS);
1304 void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1305 assert(!Ty->isOpaque());
1306 NonOpaqueStructTypes.insert(Ty);
1309 void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1310 assert(!Ty->isOpaque());
1311 NonOpaqueStructTypes.insert(Ty);
1312 bool Removed = OpaqueStructTypes.erase(Ty);
1317 void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1318 assert(Ty->isOpaque());
1319 OpaqueStructTypes.insert(Ty);
1323 IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1325 IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1326 auto I = NonOpaqueStructTypes.find_as(Key);
1327 return I == NonOpaqueStructTypes.end() ? nullptr : *I;
1330 bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1332 return OpaqueStructTypes.count(Ty);
1333 auto I = NonOpaqueStructTypes.find(Ty);
1334 return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
1337 IRMover::IRMover(Module &M) : Composite(M) {
1338 TypeFinder StructTypes;
1339 StructTypes.run(M, true);
1340 for (StructType *Ty : StructTypes) {
1342 IdentifiedStructTypes.addOpaque(Ty);
1344 IdentifiedStructTypes.addNonOpaque(Ty);
1348 Error IRMover::move(
1349 std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
1350 std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor) {
1351 IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1352 std::move(Src), ValuesToLink, std::move(AddLazyFor));
1353 Error E = TheIRLinker.run();
1354 Composite.dropTriviallyDeadConstantArrays();