-
-//===----------------------------------------------------------------------===//
-// TypeMap implementation.
-//===----------------------------------------------------------------------===//
-
-namespace {
-class TypeMapTy : public ValueMapTypeRemapper {
- /// This is a mapping from a source type to a destination type to use.
- DenseMap<Type*, Type*> MappedTypes;
-
- /// When checking to see if two subgraphs are isomorphic, we speculatively
- /// add types to MappedTypes, but keep track of them here in case we need to
- /// roll back.
- SmallVector<Type*, 16> SpeculativeTypes;
-
- SmallVector<StructType*, 16> SpeculativeDstOpaqueTypes;
-
- /// This is a list of non-opaque structs in the source module that are mapped
- /// to an opaque struct in the destination module.
- SmallVector<StructType*, 16> SrcDefinitionsToResolve;
-
- /// This is the set of opaque types in the destination modules who are
- /// getting a body from the source module.
- SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
-
-public:
- TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
- : DstStructTypesSet(DstStructTypesSet) {}
-
- Linker::IdentifiedStructTypeSet &DstStructTypesSet;
- /// Indicate that the specified type in the destination module is conceptually
- /// equivalent to the specified type in the source module.
- void addTypeMapping(Type *DstTy, Type *SrcTy);
-
- /// Produce a body for an opaque type in the dest module from a type
- /// definition in the source module.
- void linkDefinedTypeBodies();
-
- /// Return the mapped type to use for the specified input type from the
- /// source module.
- Type *get(Type *SrcTy);
- Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
-
- void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
-
- FunctionType *get(FunctionType *T) {
- return cast<FunctionType>(get((Type *)T));
- }
-
- /// Dump out the type map for debugging purposes.
- void dump() const {
- for (auto &Pair : MappedTypes) {
- dbgs() << "TypeMap: ";
- Pair.first->print(dbgs());
- dbgs() << " => ";
- Pair.second->print(dbgs());
- dbgs() << '\n';
- }
- }
-
-private:
- Type *remapType(Type *SrcTy) override { return get(SrcTy); }
-
- bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
-};
-}
-
-void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
- assert(SpeculativeTypes.empty());
- assert(SpeculativeDstOpaqueTypes.empty());
-
- // Check to see if these types are recursively isomorphic and establish a
- // mapping between them if so.
- if (!areTypesIsomorphic(DstTy, SrcTy)) {
- // Oops, they aren't isomorphic. Just discard this request by rolling out
- // any speculative mappings we've established.
- for (Type *Ty : SpeculativeTypes)
- MappedTypes.erase(Ty);
-
- SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
- SpeculativeDstOpaqueTypes.size());
- for (StructType *Ty : SpeculativeDstOpaqueTypes)
- DstResolvedOpaqueTypes.erase(Ty);
- } else {
- for (Type *Ty : SpeculativeTypes)
- if (auto *STy = dyn_cast<StructType>(Ty))
- if (STy->hasName())
- STy->setName("");
- }
- SpeculativeTypes.clear();
- SpeculativeDstOpaqueTypes.clear();
-}
-
-/// Recursively walk this pair of types, returning true if they are isomorphic,
-/// false if they are not.
-bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
- // Two types with differing kinds are clearly not isomorphic.
- if (DstTy->getTypeID() != SrcTy->getTypeID())
- return false;
-
- // If we have an entry in the MappedTypes table, then we have our answer.
- Type *&Entry = MappedTypes[SrcTy];
- if (Entry)
- return Entry == DstTy;
-
- // Two identical types are clearly isomorphic. Remember this
- // non-speculatively.
- if (DstTy == SrcTy) {
- Entry = DstTy;
- return true;
- }
-
- // Okay, we have two types with identical kinds that we haven't seen before.
-
- // If this is an opaque struct type, special case it.
- if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
- // Mapping an opaque type to any struct, just keep the dest struct.
- if (SSTy->isOpaque()) {
- Entry = DstTy;
- SpeculativeTypes.push_back(SrcTy);
- return true;
- }
-
- // Mapping a non-opaque source type to an opaque dest. If this is the first
- // type that we're mapping onto this destination type then we succeed. Keep
- // the dest, but fill it in later. If this is the second (different) type
- // that we're trying to map onto the same opaque type then we fail.
- if (cast<StructType>(DstTy)->isOpaque()) {
- // We can only map one source type onto the opaque destination type.
- if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
- return false;
- SrcDefinitionsToResolve.push_back(SSTy);
- SpeculativeTypes.push_back(SrcTy);
- SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
- Entry = DstTy;
- return true;
- }
- }
-
- // If the number of subtypes disagree between the two types, then we fail.
- if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
- return false;
-
- // Fail if any of the extra properties (e.g. array size) of the type disagree.
- if (isa<IntegerType>(DstTy))
- return false; // bitwidth disagrees.
- if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
- if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
- return false;
-
- } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
- if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
- return false;
- } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
- StructType *SSTy = cast<StructType>(SrcTy);
- if (DSTy->isLiteral() != SSTy->isLiteral() ||
- DSTy->isPacked() != SSTy->isPacked())
- return false;
- } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
- if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
- return false;
- } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
- if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
- return false;
- }
-
- // Otherwise, we speculate that these two types will line up and recursively
- // check the subelements.
- Entry = DstTy;
- SpeculativeTypes.push_back(SrcTy);
-
- for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
- if (!areTypesIsomorphic(DstTy->getContainedType(I),
- SrcTy->getContainedType(I)))
- return false;
-
- // If everything seems to have lined up, then everything is great.
- return true;
-}
-
-void TypeMapTy::linkDefinedTypeBodies() {
- SmallVector<Type*, 16> Elements;
- for (StructType *SrcSTy : SrcDefinitionsToResolve) {
- StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
- assert(DstSTy->isOpaque());
-
- // Map the body of the source type over to a new body for the dest type.
- Elements.resize(SrcSTy->getNumElements());
- for (unsigned I = 0, E = Elements.size(); I != E; ++I)
- Elements[I] = get(SrcSTy->getElementType(I));
-
- DstSTy->setBody(Elements, SrcSTy->isPacked());
- DstStructTypesSet.switchToNonOpaque(DstSTy);
- }
- SrcDefinitionsToResolve.clear();
- DstResolvedOpaqueTypes.clear();
-}
-
-void TypeMapTy::finishType(StructType *DTy, StructType *STy,
- ArrayRef<Type *> ETypes) {
- DTy->setBody(ETypes, STy->isPacked());
-
- // Steal STy's name.
- if (STy->hasName()) {
- SmallString<16> TmpName = STy->getName();
- STy->setName("");
- DTy->setName(TmpName);
- }
-
- DstStructTypesSet.addNonOpaque(DTy);
-}
-
-Type *TypeMapTy::get(Type *Ty) {
- SmallPtrSet<StructType *, 8> Visited;
- return get(Ty, Visited);
-}
-
-Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
- // If we already have an entry for this type, return it.
- Type **Entry = &MappedTypes[Ty];
- if (*Entry)
- return *Entry;
-
- // These are types that LLVM itself will unique.
- bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
-
-#ifndef NDEBUG
- if (!IsUniqued) {
- for (auto &Pair : MappedTypes) {
- assert(!(Pair.first != Ty && Pair.second == Ty) &&
- "mapping to a source type");
- }
- }
-#endif
-
- if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
- StructType *DTy = StructType::create(Ty->getContext());
- return *Entry = DTy;
- }
-
- // If this is not a recursive type, then just map all of the elements and
- // then rebuild the type from inside out.
- SmallVector<Type *, 4> ElementTypes;
-
- // If there are no element types to map, then the type is itself. This is
- // true for the anonymous {} struct, things like 'float', integers, etc.
- if (Ty->getNumContainedTypes() == 0 && IsUniqued)
- return *Entry = Ty;
-
- // Remap all of the elements, keeping track of whether any of them change.
- bool AnyChange = false;
- ElementTypes.resize(Ty->getNumContainedTypes());
- for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
- ElementTypes[I] = get(Ty->getContainedType(I), Visited);
- AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
- }
-
- // If we found our type while recursively processing stuff, just use it.
- Entry = &MappedTypes[Ty];
- if (*Entry) {
- if (auto *DTy = dyn_cast<StructType>(*Entry)) {
- if (DTy->isOpaque()) {
- auto *STy = cast<StructType>(Ty);
- finishType(DTy, STy, ElementTypes);
- }
- }
- return *Entry;
- }
-
- // If all of the element types mapped directly over and the type is not
- // a nomed struct, then the type is usable as-is.
- if (!AnyChange && IsUniqued)
- return *Entry = Ty;
-
- // Otherwise, rebuild a modified type.
- switch (Ty->getTypeID()) {
- default:
- llvm_unreachable("unknown derived type to remap");
- case Type::ArrayTyID:
- return *Entry = ArrayType::get(ElementTypes[0],
- cast<ArrayType>(Ty)->getNumElements());
- case Type::VectorTyID:
- return *Entry = VectorType::get(ElementTypes[0],
- cast<VectorType>(Ty)->getNumElements());
- case Type::PointerTyID:
- return *Entry = PointerType::get(ElementTypes[0],
- cast<PointerType>(Ty)->getAddressSpace());
- case Type::FunctionTyID:
- return *Entry = FunctionType::get(ElementTypes[0],
- makeArrayRef(ElementTypes).slice(1),
- cast<FunctionType>(Ty)->isVarArg());
- case Type::StructTyID: {
- auto *STy = cast<StructType>(Ty);
- bool IsPacked = STy->isPacked();
- if (IsUniqued)
- return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
-
- // If the type is opaque, we can just use it directly.
- if (STy->isOpaque()) {
- DstStructTypesSet.addOpaque(STy);
- return *Entry = Ty;
- }
-
- if (StructType *OldT =
- DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
- STy->setName("");
- return *Entry = OldT;
- }
-
- if (!AnyChange) {
- DstStructTypesSet.addNonOpaque(STy);
- return *Entry = Ty;
- }
-
- StructType *DTy = StructType::create(Ty->getContext());
- finishType(DTy, STy, ElementTypes);
- return *Entry = DTy;
- }
- }
-}
-
-//===----------------------------------------------------------------------===//
-// ModuleLinker implementation.
-//===----------------------------------------------------------------------===//
-