1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MapValue function, which is shared by various parts of
11 // the lib/Transforms/Utils library.
13 //===----------------------------------------------------------------------===//
15 #include "ValueMapper.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instruction.h"
21 Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
22 Value *&VMSlot = VM[V];
23 if (VMSlot) return VMSlot; // Does it exist in the map yet?
25 // Global values do not need to be seeded into the ValueMap if they are using
26 // the identity mapping.
27 if (isa<GlobalValue>(V))
28 return VMSlot = const_cast<Value*>(V);
30 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
31 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
32 isa<ConstantPointerNull>(C))
33 return VMSlot = C; // Primitive constants map directly
34 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
35 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
36 return VMSlot = ConstantPointerRef::get(MV);
37 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
38 const std::vector<Use> &Vals = CA->getValues();
39 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
40 Value *MV = MapValue(Vals[i], VM);
42 // This array must contain a reference to a global, make a new array
45 std::vector<Constant*> Values;
46 Values.reserve(Vals.size());
47 for (unsigned j = 0; j != i; ++j)
48 Values.push_back(cast<Constant>(Vals[j]));
49 Values.push_back(cast<Constant>(MV));
50 for (++i; i != e; ++i)
51 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
52 return VMSlot = ConstantArray::get(CA->getType(), Values);
57 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
58 const std::vector<Use> &Vals = CS->getValues();
59 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
60 Value *MV = MapValue(Vals[i], VM);
62 // This struct must contain a reference to a global, make a new struct
65 std::vector<Constant*> Values;
66 Values.reserve(Vals.size());
67 for (unsigned j = 0; j != i; ++j)
68 Values.push_back(cast<Constant>(Vals[j]));
69 Values.push_back(cast<Constant>(MV));
70 for (++i; i != e; ++i)
71 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
72 return VMSlot = ConstantStruct::get(CS->getType(), Values);
77 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
78 if (CE->getOpcode() == Instruction::Cast) {
79 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
80 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
81 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
82 std::vector<Constant*> Idx;
83 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
84 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
85 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
86 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
87 } else if (CE->getOpcode() == Instruction::Shl ||
88 CE->getOpcode() == Instruction::Shr) {
89 assert(CE->getNumOperands() == 2 && "Must be a shift!");
90 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
91 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
92 return VMSlot = ConstantExpr::getShift(CE->getOpcode(), MV1, MV2);
94 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
95 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
96 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
97 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
101 assert(0 && "Unknown type of constant!");
106 assert(0 && "Unknown value type: why didn't it get resolved?!");
110 } // End llvm namespace