From 8bef0373f1b8fab20b9acd277377ca01d72bac7e Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Sun, 4 Feb 2007 04:29:21 +0000 Subject: [PATCH] Back out last revision which was committed by accident. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33871 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 186 +++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 101 deletions(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 8397b5e7602..8b1a06d4c69 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -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 @@ static void PrintMap(const std::map &M) { // 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 &ValueMap) { std::map::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(In)) { if ((!isa(CPV->getType()) && !isa(CPV)) || @@ -296,6 +296,8 @@ static Value *RemapOperand(const Value *In, Result = ConstantStruct::get(cast(CPS->getType()), Operands); } else if (isa(CPV) || isa(CPV)) { Result = const_cast(CPV); + } else if (isa(CPV)) { + Result = cast(RemapOperand(CPV, ValueMap)); } else if (const ConstantPacked *CP = dyn_cast(CPV)) { std::vector Operands(CP->getNumOperands()); for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) @@ -306,10 +308,6 @@ static Value *RemapOperand(const Value *In, for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap))); Result = CE->getWithOperands(Ops); - } else if (isa(CPV)) { - Result = const_cast(CPV);// Functions map to themselves. - } else if (isa(CPV)) { - assert(0 && "Unmapped global?"); } else { assert(0 && "Unknown type of derived type constant value!"); } @@ -317,11 +315,12 @@ static Value *RemapOperand(const Value *In, Result = const_cast(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 @@ static Value *RemapOperand(const Value *In, 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(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(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 @@ static bool GetLinkageResult(GlobalValue *Dest, GlobalValue *Src, 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 GetLinkageResult(GlobalValue *Dest, GlobalValue *Src, static bool LinkGlobals(Module *Dest, Module *Src, std::map &ValueMap, std::multimap &AppendingVars, + std::map &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 @@ static bool LinkGlobals(Module *Dest, Module *Src, 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::iterator EGV = + GlobalsByName.find(SGV->getName()); + if (EGV != GlobalsByName.end()) + DGV = dyn_cast(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 @@ static bool LinkGlobals(Module *Dest, Module *Src, 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 @@ static bool LinkGlobals(Module *Dest, Module *Src, 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 @@ static bool LinkGlobals(Module *Dest, Module *Src, // 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 @@ static bool LinkGlobals(Module *Dest, Module *Src, 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 @@ static bool LinkGlobals(Module *Dest, Module *Src, // 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 LinkGlobalInits(Module *Dest, const Module *Src, // static bool LinkFunctionProtos(Module *Dest, const Module *Src, std::map &ValueMap, + std::map &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 (SF->hasName() && !SF->hasInternalLinkage()) { + // Check to see if may have to link the function. + if (!(DF = Dest->getFunction(SF->getName(), SF->getFunctionType()))) { + std::map::iterator EF = + GlobalsByName.find(SF->getName()); + if (EF != GlobalsByName.end()) + DF = dyn_cast(EF->second); + if (DF && RecursiveResolveTypes(SF->getType(), DF->getType(), TST, "")) + DF = 0; // FIXME: gross. } + } - // 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) + "'"); - } - } 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 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, } } 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 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, 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 @@ static bool LinkFunctionBodies(Module *Dest, Module *Src, std::map &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(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(ValueMap[SF]); // Destination function - if (DF) - SF->replaceAllUsesWith(ConstantExpr::getBitCast(DF, SF->getType())); + } } } return false; @@ -905,7 +878,6 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { } } - // 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 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { 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 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { // 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 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { // appended and the module is rewritten. std::multimap 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 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 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { // 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