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 "llvm/Transforms/Utils/ValueMapper.h"
16 #include "llvm/Constants.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Instruction.h"
21 Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
22 Value *&VMSlot = VM[V];
23 if (VMSlot) return VMSlot; // Does it exist in the map yet?
25 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
26 // DenseMap. This includes any recursive calls to MapValue.
28 // Global values do not need to be seeded into the ValueMap if they are using
29 // the identity mapping.
30 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
31 return VMSlot = const_cast<Value*>(V);
33 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
34 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
35 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
37 return VMSlot = C; // Primitive constants map directly
38 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
39 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
40 Value *MV = MapValue(CA->getOperand(i), VM);
41 if (MV != CA->getOperand(i)) {
42 // This array must contain a reference to a global, make a new array
45 std::vector<Constant*> Values;
46 Values.reserve(CA->getNumOperands());
47 for (unsigned j = 0; j != i; ++j)
48 Values.push_back(CA->getOperand(j));
49 Values.push_back(cast<Constant>(MV));
50 for (++i; i != e; ++i)
51 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
52 return VM[V] = ConstantArray::get(CA->getType(), Values);
57 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
58 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
59 Value *MV = MapValue(CS->getOperand(i), VM);
60 if (MV != CS->getOperand(i)) {
61 // This struct must contain a reference to a global, make a new struct
64 std::vector<Constant*> Values;
65 Values.reserve(CS->getNumOperands());
66 for (unsigned j = 0; j != i; ++j)
67 Values.push_back(CS->getOperand(j));
68 Values.push_back(cast<Constant>(MV));
69 for (++i; i != e; ++i)
70 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
71 return VM[V] = ConstantStruct::get(CS->getType(), Values);
76 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
77 std::vector<Constant*> Ops;
78 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
79 Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
80 return VM[V] = CE->getWithOperands(Ops);
81 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
82 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
83 Value *MV = MapValue(CP->getOperand(i), VM);
84 if (MV != CP->getOperand(i)) {
85 // This vector value must contain a reference to a global, make a new
86 // vector constant and return it.
88 std::vector<Constant*> Values;
89 Values.reserve(CP->getNumOperands());
90 for (unsigned j = 0; j != i; ++j)
91 Values.push_back(CP->getOperand(j));
92 Values.push_back(cast<Constant>(MV));
93 for (++i; i != e; ++i)
94 Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
95 return VM[V] = ConstantVector::get(Values);
101 assert(0 && "Unknown type of constant!");
108 /// RemapInstruction - Convert the instruction operands from referencing the
109 /// current values into those specified by ValueMap.
111 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
112 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
113 const Value *Op = I->getOperand(op);
114 Value *V = MapValue(Op, ValueMap);
115 assert(V && "Referenced value not in value map!");
116 I->setOperand(op, V);