// 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);
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.
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
*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())