[llvm-branch-commits] [llvm-branch] r134394 - /llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp

Chris Lattner sabre at nondot.org
Mon Jul 4 15:00:44 PDT 2011


Author: lattner
Date: Mon Jul  4 17:00:44 2011
New Revision: 134394

URL: http://llvm.org/viewvc/llvm-project?rev=134394&view=rev
Log:
simplify linkGlobalProto and fix a bug in addTypeMappingRec, fixing testlink1.ll

Modified:
    llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp

Modified: llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp?rev=134394&r1=134393&r2=134394&view=diff
==============================================================================
--- llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp (original)
+++ llvm/branches/type-system-rewrite/lib/Linker/LinkModules.cpp Mon Jul  4 17:00:44 2011
@@ -37,23 +37,30 @@
   /// false.  If there is a hard type conflict (maybe merging "int x" with
   /// "extern float x") this returns true.
   bool addTypeMapping(Type *DstTy, Type *SrcTy);
-  
-  //void mapTypes(Value *Dst, Value *Src);
 
+  /// get - Return the mapped type to use for the specified input type from the
+  /// source module.
+  Type *get(Type *T);
+  
 private:
   bool addTypeMappingRec(Type *DstTy, Type *SrcTy);
 };
 }
 
 bool TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
-  if (DstTy == SrcTy) return false;       // If already equal, noop.
-
-  if (Type *T = MappedTypes[SrcTy])
+  Type *&T = MappedTypes[SrcTy];
+  if (T)
     return T != DstTy;
   
+  if (DstTy == SrcTy) {
+    T = DstTy;
+    return false;
+  }
+  
   return addTypeMappingRec(DstTy, SrcTy);
 }
 
+
 /// addTypeMappingRec - This is the implementation function for addTypeMapping,
 /// which optimizes out the map lookup in the recursive walk.  
 bool TypeMapTy::addTypeMappingRec(Type *DstTy, Type *SrcTy) {
@@ -79,9 +86,13 @@
         DstST->isPacked() != SrcST->isPacked())
       return true;
     
+    Type *&Entry = MappedTypes[SrcST];
+    if (Entry)
+      return Entry != DstTy;
+    
     // Otherwise, we speculatively assume that the structs can be merged, add an
     // entry to the type map so we don't infinitely recurse.
-    MappedTypes[SrcST] = DstST;
+    Entry = DstST;
 
     // Then call addTypeMapping on each entry (not "Rec") so that we get the
     // caching behavior of the map check for each element.
@@ -136,6 +147,25 @@
   return false;
 }
 
+/// get - Return the mapped type to use for the specified input type from the
+/// source module.
+Type *TypeMapTy::get(Type *Ty) {
+  // If we already have an entry for this type, return it.
+  DenseMap<Type*, Type*>::iterator I = MappedTypes.find(Ty);
+  if (I != MappedTypes.end()) return I->second;
+  
+  // If this is an unmapped non-anonymous struct type, then it will come across
+  // like it did before.  However, its elements can still be potentially mapped.
+  
+  return Ty;
+  
+  // Otherwise, this may be a type wrapped around a mapped type.  Recurse down
+  // the type graph until we find something friendly to use.
+   
+}
+
+
+
 //===----------------------------------------------------------------------===//
 // ModuleLinker implementation.
 //===----------------------------------------------------------------------===//
@@ -409,29 +439,23 @@
 
   // Check to see if may have to link the global with the global, alias or
   // function.
-  if (SGV->hasName() && !SGV->hasLocalLinkage())
+  if (SGV->hasName() && !SGV->hasLocalLinkage()) {
     DGV = DstM->getNamedValue(SGV->getName());
 
-  // 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 && DGV->hasLocalLinkage())
-    DGV = 0;
-
-  assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() ||
-          SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) &&
-         "Global must either be external or have an initializer!");
-
-  GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
-  bool LinkFromSrc = false;
-  if (getLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc))
-    return true;
+    // 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 && DGV->hasLocalLinkage())
+      DGV = 0;
+  }
 
+  // If this isn't linkage, we're just copying the global over to the new
+  // module.  Handle this easy case first.
   if (DGV == 0) {
     // No linking to be performed, simply create an identical version of the
     // symbol over in the dest module... the initializer will be filled in
     // later by LinkGlobalInits.
     GlobalVariable *NewDGV =
-      new GlobalVariable(*DstM, SGV->getType()->getElementType(),
+      new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
                          SGV->isConstant(), SGV->getLinkage(), /*init*/0,
                          SGV->getName(), 0, false,
                          SGV->getType()->getAddressSpace());
@@ -450,6 +474,11 @@
     return false;
   }
 
+  GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
+  bool LinkFromSrc = false;
+  if (getLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc))
+    return true;
+
   // If the visibilities of the symbols disagree and the destination is a
   // prototype, take the visibility of its input.
   if (DGV->isDeclaration())





More information about the llvm-branch-commits mailing list