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 Value *&VMSlot = VM[V];
25 if (VMSlot) return VMSlot; // Does it exist in the map yet?
27 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
28 // DenseMap. This includes any recursive calls to MapValue.
30 // Global values do not need to be seeded into the VM if they
31 // are using the identity mapping.
32 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
33 return VMSlot = const_cast<Value*>(V);
35 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
36 // Start by assuming that we'll use the identity mapping.
37 VMSlot = const_cast<Value*>(V);
39 // Check all operands to see if any need to be remapped.
40 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
41 Value *OP = MD->getOperand(i);
42 if (!OP || MapValue(OP, VM) == OP) continue;
44 // Ok, at least one operand needs remapping.
45 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
47 SmallVector<Value*, 4> Elts;
48 Elts.reserve(MD->getNumOperands());
49 for (i = 0; i != e; ++i)
50 Elts.push_back(MD->getOperand(i) ?
51 MapValue(MD->getOperand(i), VM) : 0);
52 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
53 Dummy->replaceAllUsesWith(NewMD);
54 MDNode::deleteTemporary(Dummy);
58 // No operands needed remapping; keep the identity map.
59 return const_cast<Value*>(V);
62 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
65 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
66 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
68 return VMSlot = C; // Primitive constants map directly
70 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
71 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
73 Value *MV = MapValue(*i, VM);
75 // This array must contain a reference to a global, make a new array
78 std::vector<Constant*> Values;
79 Values.reserve(CA->getNumOperands());
80 for (User::op_iterator j = b; j != i; ++j)
81 Values.push_back(cast<Constant>(*j));
82 Values.push_back(cast<Constant>(MV));
83 for (++i; i != e; ++i)
84 Values.push_back(cast<Constant>(MapValue(*i, VM)));
85 return VM[V] = ConstantArray::get(CA->getType(), Values);
91 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
92 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
94 Value *MV = MapValue(*i, VM);
96 // This struct must contain a reference to a global, make a new struct
99 std::vector<Constant*> Values;
100 Values.reserve(CS->getNumOperands());
101 for (User::op_iterator j = b; j != i; ++j)
102 Values.push_back(cast<Constant>(*j));
103 Values.push_back(cast<Constant>(MV));
104 for (++i; i != e; ++i)
105 Values.push_back(cast<Constant>(MapValue(*i, VM)));
106 return VM[V] = ConstantStruct::get(CS->getType(), Values);
112 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
113 std::vector<Constant*> Ops;
114 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
115 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
116 return VM[V] = CE->getWithOperands(Ops);
119 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
120 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
122 Value *MV = MapValue(*i, VM);
124 // This vector value must contain a reference to a global, make a new
125 // vector constant and return it.
127 std::vector<Constant*> Values;
128 Values.reserve(CV->getNumOperands());
129 for (User::op_iterator j = b; j != i; ++j)
130 Values.push_back(cast<Constant>(*j));
131 Values.push_back(cast<Constant>(MV));
132 for (++i; i != e; ++i)
133 Values.push_back(cast<Constant>(MapValue(*i, VM)));
134 return VM[V] = ConstantVector::get(Values);
140 BlockAddress *BA = cast<BlockAddress>(C);
141 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
142 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
143 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
146 /// RemapInstruction - Convert the instruction operands from referencing the
147 /// current values into those specified by VMap.
149 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
150 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
151 Value *V = MapValue(*op, VMap);
152 assert(V && "Referenced value not in value map!");