// 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<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 interesting in transforming.
+ Value *Result = 0;
if (const Constant *CPV = dyn_cast<Constant>(In)) {
if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
isa<ConstantAggregateZero>(CPV))
return const_cast<Constant*>(CPV); // Simple constants stay identical.
- Constant *Result = 0;
-
if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
std::vector<Constant*> Operands(CPA->getNumOperands());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap));
Result = ConstantPacked::get(Operands);
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
- if (CE->getOpcode() == Instruction::GetElementPtr) {
- Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap);
- std::vector<Constant*> Indices;
- Indices.reserve(CE->getNumOperands()-1);
- for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
- Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
- ValueMap)));
-
- Result = ConstantExpr::getGetElementPtr(cast<Constant>(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<Constant>(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<Constant>(V1), cast<Constant>(V2),
- cast<Constant>(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<Constant>(V1),
- cast<Constant>(V2));
- } else {
- assert(0 && "Unknown constant expr type!");
- }
-
+ std::vector<Constant*> Ops;
+ 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 {
assert(0 && "Unknown type of derived type constant value!");
}
-
- // Cache the mapping in our local map structure...
+ } else if (isa<InlineAsm>(In)) {
+ Result = const_cast<Value*>(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);
} 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();
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();
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!");
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.
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;
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.
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.
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();
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
//
static bool LinkFunctionProtos(Module *Dest, const Module *Src,
std::map<const Value*, Value*> &ValueMap,
- std::map<std::string, GlobalValue*> &GlobalsByName,
+ std::map<std::string, GlobalValue*> &GlobalsByName,
std::string *Err) {
SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
} 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));
*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;
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
// 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) {
+ 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())