1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
3 // This file defines the MapValue function, which is shared by various parts of
4 // the lib/Transforms/Utils library.
6 //===----------------------------------------------------------------------===//
8 #include "ValueMapper.h"
9 #include "llvm/Constants.h"
10 #include "llvm/Instruction.h"
12 Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
13 Value *&VMSlot = VM[V];
14 if (VMSlot) return VMSlot; // Does it exist in the map yet?
16 // Global values do not need to be seeded into the ValueMap if they are using
17 // the identity mapping.
18 if (isa<GlobalValue>(V))
19 return VMSlot = const_cast<Value*>(V);
21 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
22 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
23 isa<ConstantPointerNull>(C))
24 return VMSlot = C; // Primitive constants map directly
25 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
26 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
27 return VMSlot = ConstantPointerRef::get(MV);
28 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
29 const std::vector<Use> &Vals = CA->getValues();
30 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
31 Value *MV = MapValue(Vals[i], VM);
33 // This array must contain a reference to a global, make a new array
36 std::vector<Constant*> Values;
37 Values.reserve(Vals.size());
38 for (unsigned j = 0; j != i; ++j)
39 Values.push_back(cast<Constant>(Vals[j]));
40 Values.push_back(cast<Constant>(MV));
41 for (++i; i != e; ++i)
42 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
43 return VMSlot = ConstantArray::get(CA->getType(), Values);
48 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
49 const std::vector<Use> &Vals = CS->getValues();
50 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
51 Value *MV = MapValue(Vals[i], VM);
53 // This struct must contain a reference to a global, make a new struct
56 std::vector<Constant*> Values;
57 Values.reserve(Vals.size());
58 for (unsigned j = 0; j != i; ++j)
59 Values.push_back(cast<Constant>(Vals[j]));
60 Values.push_back(cast<Constant>(MV));
61 for (++i; i != e; ++i)
62 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
63 return VMSlot = ConstantStruct::get(CS->getType(), Values);
68 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
69 if (CE->getOpcode() == Instruction::Cast) {
70 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
71 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
72 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
73 std::vector<Constant*> Idx;
74 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
75 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
76 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
77 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
79 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
80 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
81 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
82 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
86 assert(0 && "Unknown type of constant!");
91 assert(0 && "Unknown value type: why didn't it get resolved?!");