[llvm-commits] CVS: llvm/lib/Linker/LinkModules.cpp

Reid Spencer reid at x10sys.com
Sat Feb 3 20:29:37 PST 2007



Changes in directory llvm/lib/Linker:

LinkModules.cpp updated: 1.132 -> 1.133
---
Log message:

Back out last revision which was committed by accident.


---
Diffs of the changes:  (+86 -102)

 LinkModules.cpp |  188 +++++++++++++++++++++++++-------------------------------
 1 files changed, 86 insertions(+), 102 deletions(-)


Index: llvm/lib/Linker/LinkModules.cpp
diff -u llvm/lib/Linker/LinkModules.cpp:1.132 llvm/lib/Linker/LinkModules.cpp:1.133
--- llvm/lib/Linker/LinkModules.cpp:1.132	Sat Feb  3 22:28:18 2007
+++ llvm/lib/Linker/LinkModules.cpp	Sat Feb  3 22:29:21 2007
@@ -20,7 +20,7 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
-#include "llvm/ValueSymbolTable.h"
+#include "llvm/SymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/Instructions.h"
 #include "llvm/Assembly/Writer.h"
@@ -270,14 +270,14 @@
 
 
 // RemapOperand - Use ValueMap to convert references from one module to another.
-// This is somewhat sophisticated in that it can automatically handle constants
-// correctly as well.
+// This is somewhat sophisticated in that it can automatically handle constant
+// references correctly as well.
 static Value *RemapOperand(const Value *In,
                            std::map<const Value*, Value*> &ValueMap) {
   std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In);
   if (I != ValueMap.end()) return I->second;
 
-  // Check to see if it's a constant that we are interested in transforming.
+  // Check to see if it's a constant that we are interesting in transforming.
   Value *Result = 0;
   if (const Constant *CPV = dyn_cast<Constant>(In)) {
     if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
@@ -296,6 +296,8 @@
       Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
     } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) {
       Result = const_cast<Constant*>(CPV);
+    } else if (isa<GlobalValue>(CPV)) {
+      Result = cast<Constant>(RemapOperand(CPV, ValueMap));
     } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CPV)) {
       std::vector<Constant*> Operands(CP->getNumOperands());
       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
@@ -306,10 +308,6 @@
       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
         Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap)));
       Result = CE->getWithOperands(Ops);
-    } else if (isa<Function>(CPV)) {
-      Result = const_cast<Constant*>(CPV);// Functions map to themselves.
-    } else if (isa<GlobalValue>(CPV)) {
-      assert(0 && "Unmapped global?");
     } else {
       assert(0 && "Unknown type of derived type constant value!");
     }
@@ -317,11 +315,12 @@
     Result = const_cast<Value*>(In);
   }
   
-  // Cache the mapping in our local map structure
+  // Cache the mapping in our local map structure...
   if (Result) {
     ValueMap.insert(std::make_pair(In, Result));
     return Result;
   }
+  
 
   cerr << "LinkModules ValueMap: \n";
   PrintMap(ValueMap);
@@ -331,22 +330,25 @@
   return 0;
 }
 
-/// ForceRenaming - The LLVM ValueSymbolTable class autorenames globals that 
-/// conflict in the symbol table.  This is good for all clients except for us.
-/// Go through the trouble to force this back.
+/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
+/// in the symbol table.  This is good for all clients except for us.  Go
+/// through the trouble to force this back.
 static void ForceRenaming(GlobalValue *GV, const std::string &Name) {
   assert(GV->getName() != Name && "Can't force rename to self");
-  ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable();
+  SymbolTable &ST = GV->getParent()->getValueSymbolTable();
 
   // If there is a conflict, rename the conflict.
-  GlobalValue *ConflictGV = cast<GlobalValue>(ST.lookup(Name));
-  if (ConflictGV) {
-    ConflictGV->setName("");      // Eliminate the conflict
-    GV->setName(Name);            // Force the name back
-    ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
-    assert(GV->getName() == Name && ConflictGV->getName() != Name &&
-           "ForceRenaming didn't work");
-  }
+  Value *ConflictVal = ST.lookup(GV->getType(), Name);
+  assert(ConflictVal&&"Why do we have to force rename if there is no conflic?");
+  GlobalValue *ConflictGV = cast<GlobalValue>(ConflictVal);
+  assert(ConflictGV->hasInternalLinkage() &&
+         "Not conflicting with a static global, should link instead!");
+
+  ConflictGV->setName("");          // Eliminate the conflict
+  GV->setName(Name);                // Force the name back
+  ConflictGV->setName(Name);        // This will cause ConflictGV to get renamed
+  assert(GV->getName() == Name && ConflictGV->getName() != Name &&
+         "ForceRenaming didn't work");
 }
 
 /// GetLinkageResult - This analyzes the two global values and determines what
@@ -391,8 +393,7 @@
     LinkFromSrc = true; // Special cased.
     LT = Src->getLinkage();
   } else if (Src->hasWeakLinkage() || Src->hasLinkOnceLinkage()) {
-    // At this point we know that Dest has LinkOnce, External*, Weak, DLL* 
-    // linkage.
+    // At this point we know that Dest has LinkOnce, External*, Weak, DLL* linkage.
     if ((Dest->hasLinkOnceLinkage() && Src->hasWeakLinkage()) ||
         Dest->hasExternalWeakLinkage()) {
       LinkFromSrc = true;
@@ -431,10 +432,11 @@
 static bool LinkGlobals(Module *Dest, Module *Src,
                         std::map<const Value*, Value*> &ValueMap,
                     std::multimap<std::string, GlobalVariable *> &AppendingVars,
+                        std::map<std::string, GlobalValue*> &GlobalsByName,
                         std::string *Err) {
   // We will need a module level symbol table if the src module has a module
   // level symbol table...
-  TypeSymbolTable *TyST = &Dest->getTypeSymbolTable();
+  TypeSymbolTable *TST = &Dest->getTypeSymbolTable();
 
   // Loop over all of the globals in the src module, mapping them over as we go
   for (Module::global_iterator I = Src->global_begin(), E = Src->global_end();
@@ -442,12 +444,17 @@
     GlobalVariable *SGV = I;
     GlobalVariable *DGV = 0;
     // Check to see if may have to link the global.
-    if (SGV->hasName() && !SGV->hasInternalLinkage()) {
-      // See if the gvar exists already in the destination module
-      if ((DGV = Dest->getGlobalVariable(SGV->getName())))
-        // If types don't agree due to opaque types, try to resolve them.
-        RecursiveResolveTypes(SGV->getType(), DGV->getType(), TyST, "");
-    }
+    if (SGV->hasName() && !SGV->hasInternalLinkage())
+      if (!(DGV = Dest->getGlobalVariable(SGV->getName(),
+                                          SGV->getType()->getElementType()))) {
+        std::map<std::string, GlobalValue*>::iterator EGV =
+          GlobalsByName.find(SGV->getName());
+        if (EGV != GlobalsByName.end())
+          DGV = dyn_cast<GlobalVariable>(EGV->second);
+        if (DGV)
+          // If types don't agree due to opaque types, try to resolve them.
+          RecursiveResolveTypes(SGV->getType(), DGV->getType(), TST, "");
+      }
 
     if (DGV && DGV->hasInternalLinkage())
       DGV = 0;
@@ -469,10 +476,11 @@
         new GlobalVariable(SGV->getType()->getElementType(),
                            SGV->isConstant(), SGV->getLinkage(), /*init*/0,
                            SGV->getName(), Dest);
-      // Propagate alignment, section and visibility
+      // Propagate alignment info.
       NewDGV->setAlignment(SGV->getAlignment());
+
+      // Propagate section info.
       NewDGV->setSection(SGV->getSection());
-      NewDGV->setVisibility(SGV->getVisibility());
 
       // If the LLVM runtime renamed the global, but it is an externally visible
       // symbol, DGV must be an existing global with internal linkage.  Rename
@@ -495,10 +503,11 @@
                            SGV->isConstant(), SGV->getLinkage(), /*init*/0,
                            "", Dest);
 
-      // Propagate alignment, section and visibility
+      // Propagate alignment info.
       NewDGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
+
+      // Propagate section info.
       NewDGV->setSection(SGV->getSection());
-      NewDGV->setVisibility(SGV->getVisibility());
 
       // Make sure to remember this mapping...
       ValueMap.insert(std::make_pair(SGV, NewDGV));
@@ -506,10 +515,11 @@
       // Keep track that this is an appending variable...
       AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
     } else {
-      // Propagate alignment, section and visibility info.
+      // Propagate alignment info.
       DGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
+
+      // Propagate section info.
       DGV->setSection(SGV->getSection());
-      DGV->setVisibility(SGV->getVisibility());
 
       // Otherwise, perform the mapping as instructed by GetLinkageResult.  If
       // the types don't match, and if we are to link from the source, nuke DGV
@@ -519,8 +529,6 @@
           new GlobalVariable(SGV->getType()->getElementType(),
                              DGV->isConstant(), DGV->getLinkage());
         NewDGV->setAlignment(DGV->getAlignment());
-        NewDGV->setVisibility(DGV->getVisibility());
-        NewDGV->setSection(DGV->getSection());
         Dest->getGlobalList().insert(DGV, NewDGV);
         DGV->replaceAllUsesWith(
             ConstantExpr::getBitCast(NewDGV, DGV->getType()));
@@ -535,7 +543,6 @@
         // Inherit const as appropriate
         DGV->setConstant(SGV->isConstant());
         DGV->setInitializer(0);
-        DGV->setVisibility(SGV->getVisibility());
       } else {
         if (SGV->isConstant() && !DGV->isConstant()) {
           if (DGV->isDeclaration())
@@ -602,64 +609,34 @@
 //
 static bool LinkFunctionProtos(Module *Dest, const Module *Src,
                                std::map<const Value*, Value*> &ValueMap,
+                               std::map<std::string, 
+                               GlobalValue*> &GlobalsByName,
                                std::string *Err) {
-  TypeSymbolTable *TyST = &Dest->getTypeSymbolTable();
+  TypeSymbolTable *TST = &Dest->getTypeSymbolTable();
 
   // Loop over all of the functions in the src module, mapping them over as we
   // go
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
     const Function *SF = I;   // SrcFunction
     Function *DF = 0;
-    if (SF->hasName() && !SF->hasInternalLinkage())
-      // See if there is a function with the same name in the destination
-      if ((DF = Dest->getFunction(SF->getName()))) {
-        // Resolve Opaque types, etc.
-        RecursiveResolveTypes(SF->getType(), DF->getType(), TyST, "");
-      }
-
-    // If we have two globals of the same name but different type
-    if (DF && DF->getType() != SF->getType()) {
-      if (DF->isDeclaration() && !SF->isDeclaration()) {
-        // We have a definition of the same name but different type in the
-        // source module. Copy the prototype to the destination and replace
-        // uses of the destination's prototype with the new prototype.
-        Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
-                                       SF->getName(), Dest);
-        NewDF->setCallingConv(SF->getCallingConv());
-        NewDF->setVisibility(SF->getVisibility());
-
-        // If the symbol table renamed the function, but it is an externally
-        // visible symbol, DF must be an existing function with internal 
-        // linkage.  Rename it.
-        if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
-          ForceRenaming(NewDF, SF->getName());
-
-        // Any uses of DF need to change to NewDF, with cast
-        DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType()));
-        DF->eraseFromParent();
-
-        // Remember this mapping so uses in the source module get remapped
-        // later by RemapOperand.
-        ValueMap.insert(std::make_pair(SF, NewDF));
-      } else if (SF->isDeclaration()) {
-        // The source is just a prototype, simply set up a mapping from the
-        // source to the destination function. We can't do the replacement
-        // now so its deferred to LinkFunctionBodies.
-        ValueMap.insert(std::make_pair(SF, DF));
-      } else {
-        // We have two definitions of different function types of the same
-        // name. This is a multiple definition error.
-        return Error(Err, "Function '" + DF->getName() + "' defined as both '" +
-                     ToStr(SF->getFunctionType(), Src) + "' and '" +
-                     ToStr(DF->getFunctionType(), Dest) + "'");
+    if (SF->hasName() && !SF->hasInternalLinkage()) {
+      // Check to see if may have to link the function.
+      if (!(DF = Dest->getFunction(SF->getName(), SF->getFunctionType()))) {
+        std::map<std::string, GlobalValue*>::iterator EF =
+          GlobalsByName.find(SF->getName());
+        if (EF != GlobalsByName.end())
+          DF = dyn_cast<Function>(EF->second);
+        if (DF && RecursiveResolveTypes(SF->getType(), DF->getType(), TST, ""))
+          DF = 0;  // FIXME: gross.
       }
-    } else if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
+    }
+
+    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
       // Function does not already exist, simply insert an function signature
       // identical to SF into the dest module...
       Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
                                      SF->getName(), Dest);
       NewDF->setCallingConv(SF->getCallingConv());
-      NewDF->setVisibility(SF->getVisibility());
 
       // If the LLVM runtime renamed the function, but it is an externally
       // visible symbol, DF must be an existing function with internal linkage.
@@ -679,7 +656,7 @@
         }        
       } else {
         ValueMap.insert(std::make_pair(SF, DF));
-      }
+      }      
     } else if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) {
       // If DF is external but SF is not...
       // Link the external functions, update linkage qualifiers
@@ -694,6 +671,8 @@
       if ((DF->hasLinkOnceLinkage() && SF->hasWeakLinkage()) ||
           DF->hasExternalWeakLinkage())
         DF->setLinkage(SF->getLinkage());
+
+
     } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) {
       // At this point we know that SF has LinkOnce or External* linkage.
       ValueMap.insert(std::make_pair(SF, DF));
@@ -764,24 +743,18 @@
                                std::map<const Value*, Value*> &ValueMap,
                                std::string *Err) {
 
-  // Loop over all of the functions in the src module, mapping them over
+  // Loop over all of the functions in the src module, mapping them over as we
+  // go
   for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) {
-    if (!SF->isDeclaration()) {               // No body if function is external
+    if (!SF->isDeclaration()) {                  // No body if function is external
       Function *DF = cast<Function>(ValueMap[SF]); // Destination function
 
       // DF not external SF external?
-      if (DF->isDeclaration())
+      if (DF->isDeclaration()) {
         // Only provide the function body if there isn't one already.
         if (LinkFunctionBody(DF, SF, ValueMap, Err))
           return true;
-    } else {
-      // The source function is a prototype. If there's a mapping for this,
-      // then replace all its uses with the mapped function, after cast. This
-      // occurs when two functions of the same name but different type
-      // are encountered.
-      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
-      if (DF)
-        SF->replaceAllUsesWith(ConstantExpr::getBitCast(DF, SF->getType()));
+      }
     }
   }
   return false;
@@ -905,7 +878,6 @@
     }
   }
 
-  // Copy the target triple from the source to dest if the dest is empty
   if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty())
     Dest->setTargetTriple(Src->getTargetTriple());
       
@@ -916,7 +888,6 @@
       Dest->getTargetTriple() != Src->getTargetTriple())
     cerr << "WARNING: Linking two modules of different target triples!\n";
 
-  // Append the module inline asm string
   if (!Src->getModuleInlineAsm().empty()) {
     if (Dest->getModuleInlineAsm().empty())
       Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
@@ -938,8 +909,7 @@
   // LinkTypes - Go through the symbol table of the Src module and see if any
   // types are named in the src module that are not named in the Dst module.
   // Make sure there are no type name conflicts.
-  if (LinkTypes(Dest, Src, ErrorMsg)) 
-    return true;
+  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
 
   // ValueMap - Mapping of values from what they used to be in Src, to what they
   // are now in Dest.
@@ -950,17 +920,31 @@
   // appended and the module is rewritten.
   std::multimap<std::string, GlobalVariable *> AppendingVars;
 
+  // GlobalsByName - The LLVM SymbolTable class fights our best efforts at
+  // linking by separating globals by type.  Until PR411 is fixed, we replicate
+  // it's functionality here.
+  std::map<std::string, GlobalValue*> GlobalsByName;
+
   for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
        I != E; ++I) {
     // Add all of the appending globals already in the Dest module to
     // AppendingVars.
     if (I->hasAppendingLinkage())
       AppendingVars.insert(std::make_pair(I->getName(), I));
+
+    // Keep track of all globals by name.
+    if (!I->hasInternalLinkage() && I->hasName())
+      GlobalsByName[I->getName()] = I;
   }
 
+  // Keep track of all globals by name.
+  for (Module::iterator I = Dest->begin(), E = Dest->end(); I != E; ++I)
+    if (!I->hasInternalLinkage() && I->hasName())
+      GlobalsByName[I->getName()] = I;
+
   // Insert all of the globals in src into the Dest module... without linking
   // initializers (which could refer to functions not yet mapped over).
-  if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg))
+  if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, GlobalsByName, ErrorMsg))
     return true;
 
   // Link the functions together between the two modules, without doing function
@@ -968,7 +952,7 @@
   // function...  We do this so that when we begin processing function bodies,
   // all of the global values that may be referenced are available in our
   // ValueMap.
-  if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg))
+  if (LinkFunctionProtos(Dest, Src, ValueMap, GlobalsByName, ErrorMsg))
     return true;
 
   // Update the initializers in the Dest module now that all globals that may






More information about the llvm-commits mailing list