1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Type.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Function.h"
20 #include "llvm/Metadata.h"
21 #include "llvm/ADT/SmallVector.h"
24 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
25 Value *&VMSlot = VM[V];
26 if (VMSlot) return VMSlot; // Does it exist in the map yet?
28 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
29 // DenseMap. This includes any recursive calls to MapValue.
31 // Global values and non-function-local metadata do not need to be seeded into
32 // the VM if they are using the identity mapping.
33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
34 return VMSlot = const_cast<Value*>(V);
36 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
37 Value *Dummy = new GlobalAlias(V->getType(), GlobalValue::ExternalLinkage);
39 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
40 Value *OP = MD->getOperand(i);
42 Value *MV = MapValue(OP, VM);
44 // This MDNode contain a reference to mapped value. Make a new
45 // MDNode and return it.
46 SmallVector<Value*, 4> Elts;
47 Elts.reserve(MD->getNumOperands());
48 for (unsigned j = 0; j != i; ++j)
49 Elts.push_back(MD->getOperand(j));
51 for (++i; i != e; ++i)
52 Elts.push_back(MD->getOperand(i) ?
53 MapValue(MD->getOperand(i), VM) : 0);
54 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
55 Dummy->uncheckedReplaceAllUsesWith(NewMD);
61 return VM[V] = const_cast<Value*>(V);
64 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
67 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
68 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
70 return VMSlot = C; // Primitive constants map directly
72 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
73 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
75 Value *MV = MapValue(*i, VM);
77 // This array must contain a reference to a global, make a new array
80 std::vector<Constant*> Values;
81 Values.reserve(CA->getNumOperands());
82 for (User::op_iterator j = b; j != i; ++j)
83 Values.push_back(cast<Constant>(*j));
84 Values.push_back(cast<Constant>(MV));
85 for (++i; i != e; ++i)
86 Values.push_back(cast<Constant>(MapValue(*i, VM)));
87 return VM[V] = ConstantArray::get(CA->getType(), Values);
93 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
94 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
96 Value *MV = MapValue(*i, VM);
98 // This struct must contain a reference to a global, make a new struct
101 std::vector<Constant*> Values;
102 Values.reserve(CS->getNumOperands());
103 for (User::op_iterator j = b; j != i; ++j)
104 Values.push_back(cast<Constant>(*j));
105 Values.push_back(cast<Constant>(MV));
106 for (++i; i != e; ++i)
107 Values.push_back(cast<Constant>(MapValue(*i, VM)));
108 return VM[V] = ConstantStruct::get(CS->getType(), Values);
114 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
115 std::vector<Constant*> Ops;
116 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
117 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
118 return VM[V] = CE->getWithOperands(Ops);
121 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
122 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
124 Value *MV = MapValue(*i, VM);
126 // This vector value must contain a reference to a global, make a new
127 // vector constant and return it.
129 std::vector<Constant*> Values;
130 Values.reserve(CV->getNumOperands());
131 for (User::op_iterator j = b; j != i; ++j)
132 Values.push_back(cast<Constant>(*j));
133 Values.push_back(cast<Constant>(MV));
134 for (++i; i != e; ++i)
135 Values.push_back(cast<Constant>(MapValue(*i, VM)));
136 return VM[V] = ConstantVector::get(Values);
142 BlockAddress *BA = cast<BlockAddress>(C);
143 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
144 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
145 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
148 /// RemapInstruction - Convert the instruction operands from referencing the
149 /// current values into those specified by VMap.
151 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
152 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
153 Value *V = MapValue(*op, VMap);
154 assert(V && "Referenced value not in value map!");