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/GlobalValue.h"
18 #include "llvm/Instruction.h"
21 Value *llvm::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) || isa<ConstantAggregateZero>(C) ||
33 isa<UndefValue>(C) || isa<InlineAsm>(V))
34 return VMSlot = C; // Primitive constants map directly
35 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
36 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
37 Value *MV = MapValue(CA->getOperand(i), VM);
38 if (MV != CA->getOperand(i)) {
39 // This array must contain a reference to a global, make a new array
42 std::vector<Constant*> Values;
43 Values.reserve(CA->getNumOperands());
44 for (unsigned j = 0; j != i; ++j)
45 Values.push_back(CA->getOperand(j));
46 Values.push_back(cast<Constant>(MV));
47 for (++i; i != e; ++i)
48 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
49 return VMSlot = ConstantArray::get(CA->getType(), Values);
54 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
55 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
56 Value *MV = MapValue(CS->getOperand(i), VM);
57 if (MV != CS->getOperand(i)) {
58 // This struct must contain a reference to a global, make a new struct
61 std::vector<Constant*> Values;
62 Values.reserve(CS->getNumOperands());
63 for (unsigned j = 0; j != i; ++j)
64 Values.push_back(CS->getOperand(j));
65 Values.push_back(cast<Constant>(MV));
66 for (++i; i != e; ++i)
67 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
68 return VMSlot = ConstantStruct::get(CS->getType(), Values);
73 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
74 if (CE->getOpcode() == Instruction::Cast) {
75 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
76 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
77 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
78 std::vector<Constant*> Idx;
79 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
80 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
81 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
82 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
83 } else if (CE->getOpcode() == Instruction::Select) {
84 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
85 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
86 Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
87 return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3);
89 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
90 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
91 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
92 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
96 assert(0 && "Unknown type of constant!");
101 assert(0 && "Unknown value type: why didn't it get resolved?!");
105 /// RemapInstruction - Convert the instruction operands from referencing the
106 /// current values into those specified by ValueMap.
108 void llvm::RemapInstruction(Instruction *I,
109 std::map<const Value *, Value*> &ValueMap) {
110 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
111 const Value *Op = I->getOperand(op);
112 Value *V = MapValue(Op, ValueMap);
113 assert(V && "Referenced value not in value map!");
114 I->setOperand(op, V);