diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 1f089e4c8f4..8af2c88db03 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -24,8 +24,6 @@ #include "llvm/Support/Path.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/ValueMapper.h" - -#include "llvm/Support/Debug.h" using namespace llvm; //===----------------------------------------------------------------------===// @@ -33,7 +31,6 @@ using namespace llvm; //===----------------------------------------------------------------------===// namespace { - class TypeMapTy : public ValueMapTypeRemapper { /// MappedTypes - This is a mapping from a source type to a destination type /// to use. @@ -52,8 +49,8 @@ class TypeMapTy : public ValueMapTypeRemapper { /// DstResolvedOpaqueTypes - This is the set of opaque types in the /// destination modules who are getting a body from the source module. SmallPtrSet DstResolvedOpaqueTypes; - public: + /// addTypeMapping - Indicate that the specified type in the destination /// module is conceptually equivalent to the specified type in the source /// module. @@ -78,15 +75,6 @@ private: bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); }; - -} // end anonymous namespace - -/// endsInDotNumber - Check to see if there is a dot in the name followed by a -/// digit. -static bool endsInDotNumber(StructType *Ty) { - size_t DotPos = Ty->getName().rfind('.'); - return DotPos != 0 && DotPos != StringRef::npos && - Ty->getName().back() != '.' && isdigit(Ty->getName()[DotPos + 1]); } void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { @@ -97,15 +85,15 @@ void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { Entry = DstTy; return; } - + // Check to see if these types are recursively isomorphic and establish a // mapping between them if so. - if (!areTypesIsomorphic(DstTy, SrcTy)) + if (!areTypesIsomorphic(DstTy, SrcTy)) { // Oops, they aren't isomorphic. Just discard this request by rolling out // any speculative mappings we've established. for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i) MappedTypes.erase(SpeculativeTypes[i]); - + } SpeculativeTypes.clear(); } @@ -126,7 +114,7 @@ bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *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. @@ -185,17 +173,10 @@ bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { Entry = DstTy; SpeculativeTypes.push_back(SrcTy); - for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i) { - Type *SrcSubTy = SrcTy->getContainedType(i); - Type *DstSubTy = DstTy->getContainedType(i); - - if (StructType *DST = dyn_cast(DstSubTy)) - if (DST->hasName() && endsInDotNumber(DST)) - std::swap(SrcSubTy, DstSubTy); - - if (!areTypesIsomorphic(DstSubTy, SrcSubTy)) + 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; @@ -242,6 +223,7 @@ void TypeMapTy::linkDefinedTypeBodies() { DstResolvedOpaqueTypes.clear(); } + /// get - Return the mapped type to use for the specified input type from the /// source module. Type *TypeMapTy::get(Type *Ty) { @@ -258,7 +240,7 @@ Type *TypeMapTy::getImpl(Type *Ty) { // If we already have an entry for this type, return it. Type **Entry = &MappedTypes[Ty]; if (*Entry) return *Entry; - + // If this is not a named struct type, then just map all of the elements and // then rebuild the type from inside out. if (!isa(Ty) || cast(Ty)->isLiteral()) { @@ -336,7 +318,7 @@ Type *TypeMapTy::getImpl(Type *Ty) { // If the type is opaque, we can just use it directly. if (STy->isOpaque()) return *Entry = STy; - + // Otherwise we create a new type and resolve its body later. This will be // resolved by the top level of get(). SrcDefinitionsToResolve.push_back(STy); @@ -345,6 +327,8 @@ Type *TypeMapTy::getImpl(Type *Ty) { return *Entry = DTy; } + + //===----------------------------------------------------------------------===// // ModuleLinker implementation. //===----------------------------------------------------------------------===// @@ -409,11 +393,11 @@ namespace { // there is no name match-up going on. if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) return 0; - + // Otherwise see if we have a match in the destination module's symtab. GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName()); if (DGV == 0) return 0; - + // If we found a global with the same name in the dest module, but it has // internal linkage, we are really not doing any linkage here. if (DGV->hasLocalLinkage()) @@ -444,7 +428,9 @@ namespace { void linkAliasBodies(); void linkNamedMDNodes(); }; -} // end anonymous namespace +} + + /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict /// in the symbol table. This is good for all clients except for us. Go @@ -572,7 +558,6 @@ bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, /// we have two struct types 'Foo' but one got renamed when the module was /// loaded into the same LLVMContext. void ModuleLinker::computeTypeMapping() { - return; // Incorporate globals. for (Module::global_iterator I = SrcM->global_begin(), E = SrcM->global_end(); I != E; ++I) { @@ -596,11 +581,12 @@ void ModuleLinker::computeTypeMapping() { TypeMap.addTypeMapping(DGV->getType(), I->getType()); } - // Incorporate types by name, scanning all the types in the source module. At - // this point, the destination module may have a type "%foo = { i32 }" for + // Incorporate types by name, scanning all the types in the source module. + // At this point, the destination module may have a type "%foo = { i32 }" for // example. When the source module got loaded into the same LLVMContext, if // it had the same type, it would have been renamed to "%foo.42 = { i32 }". - // Attempt to link these up to clean up the IR. + // Though it isn't required for correctness, attempt to link these up to clean + // up the IR. std::vector SrcStructTypes; SrcM->findUsedStructTypes(SrcStructTypes); @@ -612,23 +598,21 @@ void ModuleLinker::computeTypeMapping() { if (!ST->hasName()) continue; // Check to see if there is a dot in the name followed by a digit. - if (endsInDotNumber(ST)) continue; - - if (endsInDotNumber(ST)) - DstM->dump(); + size_t DotPos = ST->getName().rfind('.'); + if (DotPos == 0 || DotPos == StringRef::npos || + ST->getName().back() == '.' || !isdigit(ST->getName()[DotPos+1])) + continue; // Check to see if the destination module has a struct with the prefix name. - size_t DotPos = ST->getName().rfind('.'); - if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0,DotPos))) { + if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos))) // Don't use it if this actually came from the source module. They're in // the same LLVMContext after all. if (!SrcStructTypesSet.count(DST)) TypeMap.addTypeMapping(DST, ST); - } } // Don't bother incorporating aliases, they aren't generally typed well. - + // Now that we have discovered all of the type equivalences, get a body for // any 'opaque' types in the dest module that are now resolved. TypeMap.linkDefinedTypeBodies(); @@ -638,6 +622,7 @@ void ModuleLinker::computeTypeMapping() { /// them together now. Return true on error. bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV) { + if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) return emitError("Linking globals named '" + SrcGV->getName() + "': can only link appending global with another appending global!");