-// 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.
-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.
-
- 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(CPA->getOperand(i), ValueMap));
- Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
- } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
- std::vector<Constant*> Operands(CPS->getNumOperands());
- for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
- Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap));
- Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
- } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) {
- Result = const_cast<Constant*>(CPV);
- } else if (isa<GlobalValue>(CPV)) {
- Result = cast<Constant>(RemapOperand(CPV, ValueMap));
- } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CPV)) {
- std::vector<Constant*> Operands(CP->getNumOperands());
- for (unsigned i = 0, e = CP->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->getOpcode() == Instruction::ExtractElement) {
- Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap);
- Value *Idx = RemapOperand(CE->getOperand(1), ValueMap);
- Result = ConstantExpr::getExtractElement(cast<Constant>(Ptr),
- cast<Constant>(Idx));
- } else if (CE->getOpcode() == Instruction::InsertElement) {
- Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap);
- Value *Elt = RemapOperand(CE->getOperand(1), ValueMap);
- Value *Idx = RemapOperand(CE->getOperand(2), ValueMap);
- Result = ConstantExpr::getInsertElement(cast<Constant>(Ptr),
- cast<Constant>(Elt),
- cast<Constant>(Idx));
- } else if (CE->getOpcode() == Instruction::ShuffleVector) {
- Value *V1 = RemapOperand(CE->getOperand(0), ValueMap);
- Value *V2 = RemapOperand(CE->getOperand(1), ValueMap);
- Result = ConstantExpr::getShuffleVector(cast<Constant>(V1),
- cast<Constant>(V2),
- cast<Constant>(CE->getOperand(2)));
- } 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!");
- }