- new GlobalVariable(M2Init->getType(), false, GlobalValue::AppendingLinkage,
- M2Init, GlobalName, M2);
- }
-}
-
-//// RewriteUsesInNewModule - takes a Module and a reference to a globalvalue
-//// (OrigVal) in that module and changes the reference to a different
-//// globalvalue (NewVal) in a seperate module.
-static void RewriteUsesInNewModule(Constant *OrigVal, Constant *NewVal,
- Module *TargetMod) {
- assert(OrigVal->getType() == NewVal->getType() &&
- "Can't replace something with a different type");
- for (Value::use_iterator UI = OrigVal->use_begin(), E = OrigVal->use_end();
- UI != E; ) {
- Value::use_iterator TmpUI = UI++;
- User *U = *TmpUI;
- if (Instruction *Inst = dyn_cast<Instruction>(U)) {
- Module *InstM = Inst->getParent()->getParent()->getParent();
- if (InstM != TargetMod) {
- TmpUI.getUse() = NewVal;
- }
- } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(U)) {
- if (GV->getParent() != TargetMod) {
- TmpUI.getUse() = NewVal;
- }
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
- // If nothing uses this, don't bother making a copy.
- if (CE->use_empty()) continue;
- Constant *NewCE = CE->getWithOperandReplaced(TmpUI.getOperandNo(),
- NewVal);
- RewriteUsesInNewModule(CE, NewCE, TargetMod);
- } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(U)) {
- // If nothing uses this, don't bother making a copy.
- if (CS->use_empty()) continue;
- unsigned OpNo = TmpUI.getOperandNo();
- std::vector<Constant*> Ops;
- for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
- Ops.push_back(i == OpNo ? NewVal : CS->getOperand(i));
- Constant *NewStruct = ConstantStruct::get(Ops);
- RewriteUsesInNewModule(CS, NewStruct, TargetMod);
- } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(U)) {
- // If nothing uses this, don't bother making a copy.
- if (CP->use_empty()) continue;
- unsigned OpNo = TmpUI.getOperandNo();
- std::vector<Constant*> Ops;
- for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
- Ops.push_back(i == OpNo ? NewVal : CP->getOperand(i));
- Constant *NewPacked = ConstantPacked::get(Ops);
- RewriteUsesInNewModule(CP, NewPacked, TargetMod);
- } else if (ConstantArray *CA = dyn_cast<ConstantArray>(U)) {
- // If nothing uses this, don't bother making a copy.
- if (CA->use_empty()) continue;
- unsigned OpNo = TmpUI.getOperandNo();
- std::vector<Constant*> Ops;
- for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
- Ops.push_back(i == OpNo ? NewVal : CA->getOperand(i));
- }
- Constant *NewArray = ConstantArray::get(CA->getType(), Ops);
- RewriteUsesInNewModule(CA, NewArray, TargetMod);
- } else {
- assert(0 && "Unexpected user");
- }