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/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/Metadata.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/ErrorHandling.h"
24 Value *llvm::MapValue(const Value *V, ValueMapTy &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 metadata do not need to be seeded into the ValueMap if
32 // they are using the identity mapping.
33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
34 return VMSlot = const_cast<Value*>(V);
36 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
39 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
40 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
41 isa<UndefValue>(C) || isa<MDString>(C))
42 return VMSlot = C; // Primitive constants map directly
44 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
45 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
47 Value *MV = MapValue(*i, VM);
49 // This array must contain a reference to a global, make a new array
52 std::vector<Constant*> Values;
53 Values.reserve(CA->getNumOperands());
54 for (User::op_iterator j = b; j != i; ++j)
55 Values.push_back(cast<Constant>(*j));
56 Values.push_back(cast<Constant>(MV));
57 for (++i; i != e; ++i)
58 Values.push_back(cast<Constant>(MapValue(*i, VM)));
59 return VM[V] = ConstantArray::get(CA->getType(), Values);
65 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
66 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
68 Value *MV = MapValue(*i, VM);
70 // This struct must contain a reference to a global, make a new struct
73 std::vector<Constant*> Values;
74 Values.reserve(CS->getNumOperands());
75 for (User::op_iterator j = b; j != i; ++j)
76 Values.push_back(cast<Constant>(*j));
77 Values.push_back(cast<Constant>(MV));
78 for (++i; i != e; ++i)
79 Values.push_back(cast<Constant>(MapValue(*i, VM)));
80 return VM[V] = ConstantStruct::get(CS->getType(), Values);
86 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 std::vector<Constant*> Ops;
88 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
89 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
90 return VM[V] = CE->getWithOperands(Ops);
93 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
94 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
96 Value *MV = MapValue(*i, VM);
98 // This vector value must contain a reference to a global, make a new
99 // vector constant and return it.
101 std::vector<Constant*> Values;
102 Values.reserve(CV->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] = ConstantVector::get(Values);
114 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
115 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
116 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
117 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
120 llvm_unreachable("Unknown type of constant!");
124 /// RemapInstruction - Convert the instruction operands from referencing the
125 /// current values into those specified by ValueMap.
127 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
128 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
129 Value *V = MapValue(*op, ValueMap);
130 assert(V && "Referenced value not in value map!");