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/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/InlineAsm.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Metadata.h"
23 // Out of line method to get vtable etc for class.
24 void ValueMapTypeRemapper::anchor() {}
26 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
27 ValueMapTypeRemapper *TypeMapper) {
28 ValueToValueMapTy::iterator I = VM.find(V);
30 // If the value already exists in the map, use it.
31 if (I != VM.end() && I->second) return I->second;
33 // Global values do not need to be seeded into the VM if they
34 // are using the identity mapping.
35 if (isa<GlobalValue>(V) || isa<MDString>(V))
36 return VM[V] = const_cast<Value*>(V);
38 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
39 // Inline asm may need *type* remapping.
40 FunctionType *NewTy = IA->getFunctionType();
42 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
44 if (NewTy != IA->getFunctionType())
45 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
46 IA->hasSideEffects(), IA->isAlignStack());
49 return VM[V] = const_cast<Value*>(V);
53 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
54 // If this is a module-level metadata and we know that nothing at the module
55 // level is changing, then use an identity mapping.
56 if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
57 return VM[V] = const_cast<Value*>(V);
59 // Create a dummy node in case we have a metadata cycle.
60 MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>());
63 // Check all operands to see if any need to be remapped.
64 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
65 Value *OP = MD->getOperand(i);
66 if (OP == 0) continue;
67 Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper);
68 // Use identity map if Mapped_Op is null and we can ignore missing
70 if (Mapped_OP == OP ||
71 (Mapped_OP == 0 && (Flags & RF_IgnoreMissingEntries)))
74 // Ok, at least one operand needs remapping.
75 SmallVector<Value*, 4> Elts;
76 Elts.reserve(MD->getNumOperands());
77 for (i = 0; i != e; ++i) {
78 Value *Op = MD->getOperand(i);
82 Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper);
83 // Use identity map if Mapped_Op is null and we can ignore missing
85 if (Mapped_Op == 0 && (Flags & RF_IgnoreMissingEntries))
87 Elts.push_back(Mapped_Op);
90 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
91 Dummy->replaceAllUsesWith(NewMD);
93 MDNode::deleteTemporary(Dummy);
97 VM[V] = const_cast<Value*>(V);
98 MDNode::deleteTemporary(Dummy);
100 // No operands needed remapping. Use an identity mapping.
101 return const_cast<Value*>(V);
104 // Okay, this either must be a constant (which may or may not be mappable) or
105 // is something that is not in the mapping table.
106 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
110 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
112 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper));
113 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
115 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
118 // Otherwise, we have some other constant to remap. Start by checking to see
119 // if all operands have an identity remapping.
120 unsigned OpNo = 0, NumOperands = C->getNumOperands();
122 for (; OpNo != NumOperands; ++OpNo) {
123 Value *Op = C->getOperand(OpNo);
124 Mapped = MapValue(Op, VM, Flags, TypeMapper);
125 if (Mapped != C) break;
128 // See if the type mapper wants to remap the type as well.
129 Type *NewTy = C->getType();
131 NewTy = TypeMapper->remapType(NewTy);
133 // If the result type and all operands match up, then just insert an identity
135 if (OpNo == NumOperands && NewTy == C->getType())
138 // Okay, we need to create a new constant. We've already processed some or
139 // all of the operands, set them all up now.
140 SmallVector<Constant*, 8> Ops;
141 Ops.reserve(NumOperands);
142 for (unsigned j = 0; j != OpNo; ++j)
143 Ops.push_back(cast<Constant>(C->getOperand(j)));
145 // If one of the operands mismatch, push it and the other mapped operands.
146 if (OpNo != NumOperands) {
147 Ops.push_back(cast<Constant>(Mapped));
149 // Map the rest of the operands that aren't processed yet.
150 for (++OpNo; OpNo != NumOperands; ++OpNo)
151 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
155 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
156 return VM[V] = CE->getWithOperands(Ops, NewTy);
157 if (isa<ConstantArray>(C))
158 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
159 if (isa<ConstantStruct>(C))
160 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
161 if (isa<ConstantVector>(C))
162 return VM[V] = ConstantVector::get(Ops);
163 // If this is a no-operand constant, it must be because the type was remapped.
164 if (isa<UndefValue>(C))
165 return VM[V] = UndefValue::get(NewTy);
166 if (isa<ConstantAggregateZero>(C))
167 return VM[V] = ConstantAggregateZero::get(NewTy);
168 assert(isa<ConstantPointerNull>(C));
169 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
172 /// RemapInstruction - Convert the instruction operands from referencing the
173 /// current values into those specified by VMap.
175 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
176 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){
178 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
179 Value *V = MapValue(*op, VMap, Flags, TypeMapper);
180 // If we aren't ignoring missing entries, assert that something happened.
184 assert((Flags & RF_IgnoreMissingEntries) &&
185 "Referenced value not in value map!");
188 // Remap phi nodes' incoming blocks.
189 if (PHINode *PN = dyn_cast<PHINode>(I)) {
190 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
191 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
192 // If we aren't ignoring missing entries, assert that something happened.
194 PN->setIncomingBlock(i, cast<BasicBlock>(V));
196 assert((Flags & RF_IgnoreMissingEntries) &&
197 "Referenced block not in value map!");
201 // Remap attached metadata.
202 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
203 I->getAllMetadata(MDs);
204 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
205 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
206 MDNode *Old = MI->second;
207 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper);
209 I->setMetadata(MI->first, New);
212 // If the instruction's type is being remapped, do so now.
214 I->mutateType(TypeMapper->remapType(I->getType()));