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 "llvm/Transforms/Utils/ValueMapper.h"
16 #include "llvm/Type.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/Metadata.h"
20 #include "llvm/ADT/SmallVector.h"
23 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
24 bool ModuleLevelChanges) {
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 do not need to be seeded into the VM if they
32 // are using the identity mapping.
33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
34 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal() &&
36 return VMSlot = const_cast<Value*>(V);
38 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
39 // Start by assuming that we'll use the identity mapping.
40 VMSlot = const_cast<Value*>(V);
42 // Check all operands to see if any need to be remapped.
43 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
44 Value *OP = MD->getOperand(i);
45 if (!OP || MapValue(OP, VM, ModuleLevelChanges) == OP) continue;
47 // Ok, at least one operand needs remapping.
48 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
50 SmallVector<Value*, 4> Elts;
51 Elts.reserve(MD->getNumOperands());
52 for (i = 0; i != e; ++i)
53 Elts.push_back(MD->getOperand(i) ?
54 MapValue(MD->getOperand(i), VM, ModuleLevelChanges) : 0);
55 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
56 Dummy->replaceAllUsesWith(NewMD);
57 MDNode::deleteTemporary(Dummy);
61 // No operands needed remapping; keep the identity map.
62 return const_cast<Value*>(V);
65 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
69 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
70 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
72 return VMSlot = C; // Primitive constants map directly
74 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
75 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
77 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
79 // This array must contain a reference to a global, make a new array
82 std::vector<Constant*> Values;
83 Values.reserve(CA->getNumOperands());
84 for (User::op_iterator j = b; j != i; ++j)
85 Values.push_back(cast<Constant>(*j));
86 Values.push_back(cast<Constant>(MV));
87 for (++i; i != e; ++i)
88 Values.push_back(cast<Constant>(MapValue(*i, VM,
89 ModuleLevelChanges)));
90 return VM[V] = ConstantArray::get(CA->getType(), Values);
96 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
97 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
99 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
101 // This struct must contain a reference to a global, make a new struct
104 std::vector<Constant*> Values;
105 Values.reserve(CS->getNumOperands());
106 for (User::op_iterator j = b; j != i; ++j)
107 Values.push_back(cast<Constant>(*j));
108 Values.push_back(cast<Constant>(MV));
109 for (++i; i != e; ++i)
110 Values.push_back(cast<Constant>(MapValue(*i, VM,
111 ModuleLevelChanges)));
112 return VM[V] = ConstantStruct::get(CS->getType(), Values);
118 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
119 std::vector<Constant*> Ops;
120 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
121 Ops.push_back(cast<Constant>(MapValue(*i, VM, ModuleLevelChanges)));
122 return VM[V] = CE->getWithOperands(Ops);
125 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
126 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
128 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
130 // This vector value must contain a reference to a global, make a new
131 // vector constant and return it.
133 std::vector<Constant*> Values;
134 Values.reserve(CV->getNumOperands());
135 for (User::op_iterator j = b; j != i; ++j)
136 Values.push_back(cast<Constant>(*j));
137 Values.push_back(cast<Constant>(MV));
138 for (++i; i != e; ++i)
139 Values.push_back(cast<Constant>(MapValue(*i, VM,
140 ModuleLevelChanges)));
141 return VM[V] = ConstantVector::get(Values);
147 BlockAddress *BA = cast<BlockAddress>(C);
148 Function *F = cast<Function>(MapValue(BA->getFunction(), VM,
149 ModuleLevelChanges));
150 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM,
151 ModuleLevelChanges));
152 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
155 /// RemapInstruction - Convert the instruction operands from referencing the
156 /// current values into those specified by VMap.
158 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
159 bool ModuleLevelChanges) {
161 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
162 Value *V = MapValue(*op, VMap, ModuleLevelChanges);
163 assert(V && "Referenced value not in value map!");
167 // Remap attached metadata.
168 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
169 I->getAllMetadata(MDs);
170 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
171 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
172 Value *Old = MI->second;
173 Value *New = MapValue(Old, VMap, ModuleLevelChanges);
175 I->setMetadata(MI->first, cast<MDNode>(New));