X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=lib%2FLinker%2FLinkModules.cpp;h=90b45028bc11e8b7f2eefdc3b95f9a843a4a357c;hb=b74ed07bfd3af42331b1964c24c39912610a08f4;hp=482a63abdc7d4bbe2c643c0c0c790acbb5231bd2;hpb=e1b2e14f92e8e9da1eb4854511a881cf9c22af3c;p=oota-llvm.git diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 482a63abdc7..90b45028bc1 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -262,20 +262,19 @@ 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 constant -// references correctly as well... +// 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 interesting in transforming. + Value *Result = 0; if (const Constant *CPV = dyn_cast(In)) { if ((!isa(CPV->getType()) && !isa(CPV)) || isa(CPV)) return const_cast(CPV); // Simple constants stay identical. - Constant *Result = 0; - if (const ConstantArray *CPA = dyn_cast(CPV)) { std::vector Operands(CPA->getNumOperands()); for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) @@ -296,47 +295,23 @@ static Value *RemapOperand(const Value *In, Operands[i] = cast(RemapOperand(CP->getOperand(i), ValueMap)); Result = ConstantPacked::get(Operands); } else if (const ConstantExpr *CE = dyn_cast(CPV)) { - if (CE->getOpcode() == Instruction::GetElementPtr) { - Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap); - std::vector Indices; - Indices.reserve(CE->getNumOperands()-1); - for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) - Indices.push_back(cast(RemapOperand(CE->getOperand(i), - ValueMap))); - - Result = ConstantExpr::getGetElementPtr(cast(Ptr), Indices); - } else if (CE->getNumOperands() == 1) { - // Cast instruction - assert(CE->getOpcode() == Instruction::Cast); - Value *V = RemapOperand(CE->getOperand(0), ValueMap); - Result = ConstantExpr::getCast(cast(V), CE->getType()); - } else if (CE->getNumOperands() == 3) { - // Select instruction - assert(CE->getOpcode() == Instruction::Select); - Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); - Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); - Value *V3 = RemapOperand(CE->getOperand(2), ValueMap); - Result = ConstantExpr::getSelect(cast(V1), cast(V2), - cast(V3)); - } else if (CE->getNumOperands() == 2) { - // Binary operator... - Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); - Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); - - Result = ConstantExpr::get(CE->getOpcode(), cast(V1), - cast(V2)); - } else { - assert(0 && "Unknown constant expr type!"); - } - + std::vector Ops; + for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) + Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap))); + Result = CE->getWithOperands(Ops); } else { assert(0 && "Unknown type of derived type constant value!"); } - - // Cache the mapping in our local map structure... + } else if (isa(In)) { + Result = const_cast(In); + } + + // Cache the mapping in our local map structure... + if (Result) { ValueMap.insert(std::make_pair(In, Result)); return Result; } + std::cerr << "LinkModules ValueMap: \n"; PrintMap(ValueMap); @@ -384,9 +359,16 @@ static bool GetLinkageResult(GlobalValue *Dest, GlobalValue *Src, } else if (Src->isExternal()) { // If Src is external or if both Src & Drc are external.. Just link the // external globals, we aren't adding anything. - LinkFromSrc = false; - LT = Dest->getLinkage(); - } else if (Dest->isExternal()) { + if (Src->hasDLLImportLinkage()) { + if (Dest->isExternal()) { + LinkFromSrc = true; + LT = Src->getLinkage(); + } + } else { + LinkFromSrc = false; + LT = Dest->getLinkage(); + } + } else if (Dest->isExternal() && !Dest->hasDLLImportLinkage()) { // If Dest is external but Src is not: LinkFromSrc = true; LT = Src->getLinkage(); @@ -397,7 +379,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 or Weak linkage. + // At this point we know that Dest has LinkOnce, External, Weak, DLL* linkage. if (Dest->hasLinkOnceLinkage() && Src->hasWeakLinkage()) { LinkFromSrc = true; LT = Src->getLinkage(); @@ -406,11 +388,16 @@ static bool GetLinkageResult(GlobalValue *Dest, GlobalValue *Src, LT = Dest->getLinkage(); } } else if (Dest->hasWeakLinkage() || Dest->hasLinkOnceLinkage()) { - // At this point we know that Src has External linkage. + // At this point we know that Src has External or DLL* linkage. LinkFromSrc = true; LT = GlobalValue::ExternalLinkage; } else { - assert(Dest->hasExternalLinkage() && Src->hasExternalLinkage() && + assert((Dest->hasExternalLinkage() || + Dest->hasDLLImportLinkage() || + Dest->hasDLLExportLinkage()) && + (Src->hasExternalLinkage() || + Src->hasDLLImportLinkage() || + Src->hasDLLExportLinkage()) && "Unexpected linkage type!"); return Error(Err, "Linking globals named '" + Src->getName() + "': symbol multiply defined!"); @@ -430,7 +417,8 @@ static bool LinkGlobals(Module *Dest, Module *Src, SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); // 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(); I != E; ++I) { + for (Module::global_iterator I = Src->global_begin(), E = Src->global_end(); + I != E; ++I) { GlobalVariable *SGV = I; GlobalVariable *DGV = 0; // Check to see if may have to link the global. @@ -449,7 +437,8 @@ static bool LinkGlobals(Module *Dest, Module *Src, if (DGV && DGV->hasInternalLinkage()) DGV = 0; - assert(SGV->hasInitializer() || SGV->hasExternalLinkage() && + assert(SGV->hasInitializer() || + SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage() && "Global must either be external or have an initializer!"); GlobalValue::LinkageTypes NewLinkage; @@ -465,7 +454,9 @@ static bool LinkGlobals(Module *Dest, Module *Src, new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(), SGV->getLinkage(), /*init*/0, SGV->getName(), Dest); - + // Propagate alignment info. + NewDGV->setAlignment(SGV->getAlignment()); + // If the LLVM runtime renamed the global, but it is an externally visible // symbol, DGV must be an existing global with internal linkage. Rename // it. @@ -487,12 +478,18 @@ static bool LinkGlobals(Module *Dest, Module *Src, SGV->isConstant(), SGV->getLinkage(), /*init*/0, "", Dest); + // Propagate alignment info. + NewDGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment())); + // Make sure to remember this mapping... ValueMap.insert(std::make_pair(SGV, NewDGV)); // Keep track that this is an appending variable... AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); } else { + // Propagate alignment info. + DGV->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment())); + // 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 // and create a new one of the appropriate type. @@ -500,6 +497,7 @@ static bool LinkGlobals(Module *Dest, Module *Src, GlobalVariable *NewDGV = new GlobalVariable(SGV->getType()->getElementType(), DGV->isConstant(), DGV->getLinkage()); + NewDGV->setAlignment(DGV->getAlignment()); Dest->getGlobalList().insert(DGV, NewDGV); DGV->replaceAllUsesWith(ConstantExpr::getCast(NewDGV, DGV->getType())); DGV->eraseFromParent(); @@ -538,7 +536,8 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src, std::string *Err) { // Loop over all of the globals in the src module, mapping them over as we go - for (Module::const_global_iterator I = Src->global_begin(), E = Src->global_end(); I != E; ++I){ + for (Module::const_global_iterator I = Src->global_begin(), + E = Src->global_end(); I != E; ++I) { const GlobalVariable *SGV = I; if (SGV->hasInitializer()) { // Only process initialized GV's @@ -579,7 +578,7 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src, // static bool LinkFunctionProtos(Module *Dest, const Module *Src, std::map &ValueMap, - std::map &GlobalsByName, + std::map &GlobalsByName, std::string *Err) { SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable(); @@ -618,12 +617,19 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, } else if (SF->isExternal()) { // If SF is external or if both SF & DF are external.. Just link the // external functions, we aren't adding anything. - ValueMap.insert(std::make_pair(SF, DF)); - } else if (DF->isExternal()) { // If DF is external but SF is not... + if (SF->hasDLLImportLinkage()) { + if (DF->isExternal()) { + ValueMap.insert(std::make_pair(SF, DF)); + DF->setLinkage(SF->getLinkage()); + } + } else { + ValueMap.insert(std::make_pair(SF, DF)); + } + } else if (DF->isExternal() && !DF->hasDLLImportLinkage()) { + // If DF is external but SF is not... // Link the external functions, update linkage qualifiers ValueMap.insert(std::make_pair(SF, DF)); DF->setLinkage(SF->getLinkage()); - } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) { // At this point we know that DF has LinkOnce, Weak, or External linkage. ValueMap.insert(std::make_pair(SF, DF)); @@ -687,7 +693,8 @@ static bool LinkFunctionBody(Function *Dest, Function *Src, *OI = RemapOperand(*OI, GlobalMap); // There is no need to map the arguments anymore. - for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); I != E; ++I) + for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); + I != E; ++I) GlobalMap.erase(I); return false; @@ -833,11 +840,12 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { Dest->getTargetTriple() != Src->getTargetTriple()) std::cerr << "WARNING: Linking two modules of different target triples!\n"; - if (!Src->getInlineAsm().empty()) { - if (Dest->getInlineAsm().empty()) - Dest->setInlineAsm(Src->getInlineAsm()); + if (!Src->getModuleInlineAsm().empty()) { + if (Dest->getModuleInlineAsm().empty()) + Dest->setModuleInlineAsm(Src->getModuleInlineAsm()); else - Dest->setInlineAsm(Dest->getInlineAsm()+"\n"+Src->getInlineAsm()); + Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+ + Src->getModuleInlineAsm()); } // Update the destination module's dependent libraries list with the libraries @@ -869,7 +877,8 @@ Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { // it's functionality here. std::map GlobalsByName; - for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end(); I != E; ++I) { + 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())