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/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 (User::op_iterator b
= CA
->op_begin(), i
= b
, e
= CA
->op_end();
41 Value
*MV
= MapValue(*i
, VM
);
43 // This array must contain a reference to a global, make a new array
46 std::vector
<Constant
*> Values
;
47 Values
.reserve(CA
->getNumOperands());
48 for (User::op_iterator j
= b
; j
!= i
; ++j
)
49 Values
.push_back(cast
<Constant
>(*j
));
50 Values
.push_back(cast
<Constant
>(MV
));
51 for (++i
; i
!= e
; ++i
)
52 Values
.push_back(cast
<Constant
>(MapValue(*i
, VM
)));
53 return VM
[V
] = ConstantArray::get(CA
->getType(), Values
);
58 } else if (ConstantStruct
*CS
= dyn_cast
<ConstantStruct
>(C
)) {
59 for (User::op_iterator b
= CS
->op_begin(), i
= b
, e
= CS
->op_end();
61 Value
*MV
= MapValue(*i
, VM
);
63 // This struct must contain a reference to a global, make a new struct
66 std::vector
<Constant
*> Values
;
67 Values
.reserve(CS
->getNumOperands());
68 for (User::op_iterator j
= b
; j
!= i
; ++j
)
69 Values
.push_back(cast
<Constant
>(*j
));
70 Values
.push_back(cast
<Constant
>(MV
));
71 for (++i
; i
!= e
; ++i
)
72 Values
.push_back(cast
<Constant
>(MapValue(*i
, VM
)));
73 return VM
[V
] = ConstantStruct::get(CS
->getType(), Values
);
78 } else if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
79 std::vector
<Constant
*> Ops
;
80 for (User::op_iterator i
= CE
->op_begin(), e
= CE
->op_end(); i
!= e
; ++i
)
81 Ops
.push_back(cast
<Constant
>(MapValue(*i
, VM
)));
82 return VM
[V
] = CE
->getWithOperands(Ops
);
83 } else if (ConstantVector
*CP
= dyn_cast
<ConstantVector
>(C
)) {
84 for (User::op_iterator b
= CP
->op_begin(), i
= b
, e
= CP
->op_end();
86 Value
*MV
= MapValue(*i
, VM
);
88 // This vector value must contain a reference to a global, make a new
89 // vector constant and return it.
91 std::vector
<Constant
*> Values
;
92 Values
.reserve(CP
->getNumOperands());
93 for (User::op_iterator j
= b
; j
!= i
; ++j
)
94 Values
.push_back(cast
<Constant
>(*j
));
95 Values
.push_back(cast
<Constant
>(MV
));
96 for (++i
; i
!= e
; ++i
)
97 Values
.push_back(cast
<Constant
>(MapValue(*i
, VM
)));
98 return VM
[V
] = ConstantVector::get(Values
);
104 assert(0 && "Unknown type of constant!");
111 /// RemapInstruction - Convert the instruction operands from referencing the
112 /// current values into those specified by ValueMap.
114 void llvm::RemapInstruction(Instruction
*I
, ValueMapTy
&ValueMap
) {
115 for (User::op_iterator op
= I
->op_begin(), E
= I
->op_end(); op
!= E
; ++op
) {
116 Value
*V
= MapValue(*op
, ValueMap
);
117 assert(V
&& "Referenced value not in value map!");