blob: 04c5ff2b012410a6a45b38fe9018a77c9706dd4d [file] [log] [blame]
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00009//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
Dan Gohmana2095032010-08-24 18:50:07 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#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"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000021using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000022
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000023// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000024void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000025void ValueMaterializer::anchor() {}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000026
27Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +000028 ValueMapTypeRemapper *TypeMapper,
29 ValueMaterializer *Materializer) {
Chris Lattner43f8d162011-01-08 08:15:20 +000030 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +000031
Chris Lattner43f8d162011-01-08 08:15:20 +000032 // If the value already exists in the map, use it.
33 if (I != VM.end() && I->second) return I->second;
34
James Molloyf6f121e2013-05-28 15:17:05 +000035 // If we have a materializer and it can materialize a value, use that.
36 if (Materializer) {
37 if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
38 return VM[V] = NewV;
39 }
40
Dan Gohmanca26f792010-08-26 15:41:53 +000041 // Global values do not need to be seeded into the VM if they
42 // are using the identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000043 if (isa<GlobalValue>(V))
Chris Lattner43f8d162011-01-08 08:15:20 +000044 return VM[V] = const_cast<Value*>(V);
Chris Lattner8b4cf5e2011-07-15 23:18:40 +000045
46 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
47 // Inline asm may need *type* remapping.
48 FunctionType *NewTy = IA->getFunctionType();
49 if (TypeMapper) {
50 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
51
52 if (NewTy != IA->getFunctionType())
53 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
54 IA->hasSideEffects(), IA->isAlignStack());
55 }
56
57 return VM[V] = const_cast<Value*>(V);
58 }
Chris Lattner6aa34b02003-10-06 15:23:43 +000059
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000060 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
61 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +000062 // If this is a module-level metadata and we know that nothing at the module
63 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000064 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
65 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000066
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +000067 auto *MappedMD = MapMetadata(MD, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000068 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
69 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000070
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000071 // FIXME: This assert crashes during bootstrap, but I think it should be
72 // correct. For now, just match behaviour from before the metadata/value
73 // split.
74 //
75 // assert(MappedMD && "Referenced metadata value not in value map");
76 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +000077 }
78
Chris Lattner43f8d162011-01-08 08:15:20 +000079 // Okay, this either must be a constant (which may or may not be mappable) or
80 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +000081 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +000082 if (!C)
83 return nullptr;
Chris Lattnercf5a47d2009-10-29 00:28:30 +000084
Chris Lattner43f8d162011-01-08 08:15:20 +000085 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000086 Function *F =
James Molloyf6f121e2013-05-28 15:17:05 +000087 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +000088 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
James Molloyf6f121e2013-05-28 15:17:05 +000089 Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +000090 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000091 }
Chris Lattnercf5a47d2009-10-29 00:28:30 +000092
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000093 // Otherwise, we have some other constant to remap. Start by checking to see
94 // if all operands have an identity remapping.
95 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +000096 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000097 for (; OpNo != NumOperands; ++OpNo) {
98 Value *Op = C->getOperand(OpNo);
James Molloyf6f121e2013-05-28 15:17:05 +000099 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000100 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000101 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000102
103 // See if the type mapper wants to remap the type as well.
104 Type *NewTy = C->getType();
105 if (TypeMapper)
106 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000107
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000108 // If the result type and all operands match up, then just insert an identity
109 // mapping.
110 if (OpNo == NumOperands && NewTy == C->getType())
111 return VM[V] = C;
112
113 // Okay, we need to create a new constant. We've already processed some or
114 // all of the operands, set them all up now.
115 SmallVector<Constant*, 8> Ops;
116 Ops.reserve(NumOperands);
117 for (unsigned j = 0; j != OpNo; ++j)
118 Ops.push_back(cast<Constant>(C->getOperand(j)));
119
120 // If one of the operands mismatch, push it and the other mapped operands.
121 if (OpNo != NumOperands) {
122 Ops.push_back(cast<Constant>(Mapped));
123
124 // Map the rest of the operands that aren't processed yet.
125 for (++OpNo; OpNo != NumOperands; ++OpNo)
126 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
James Molloyf6f121e2013-05-28 15:17:05 +0000127 Flags, TypeMapper, Materializer));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000128 }
129
130 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
131 return VM[V] = CE->getWithOperands(Ops, NewTy);
132 if (isa<ConstantArray>(C))
133 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
134 if (isa<ConstantStruct>(C))
135 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
136 if (isa<ConstantVector>(C))
137 return VM[V] = ConstantVector::get(Ops);
138 // If this is a no-operand constant, it must be because the type was remapped.
139 if (isa<UndefValue>(C))
140 return VM[V] = UndefValue::get(NewTy);
141 if (isa<ConstantAggregateZero>(C))
142 return VM[V] = ConstantAggregateZero::get(NewTy);
143 assert(isa<ConstantPointerNull>(C));
144 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000145}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000146
Kaelyn Takata22324f32014-12-09 23:32:46 +0000147static Metadata *mapToMetadata(ValueToValueMapTy &VM, const Metadata *Key,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000148 Metadata *Val) {
149 VM.MD()[Key].reset(Val);
150 return Val;
151}
152
153static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
Kaelyn Takata22324f32014-12-09 23:32:46 +0000154 return mapToMetadata(VM, MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000155}
156
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000157static Metadata *MapMetadataImpl(const Metadata *MD, ValueToValueMapTy &VM,
158 RemapFlags Flags,
159 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000160 ValueMaterializer *Materializer);
161
162static Metadata *mapMetadataOp(Metadata *Op, ValueToValueMapTy &VM,
163 RemapFlags Flags,
164 ValueMapTypeRemapper *TypeMapper,
165 ValueMaterializer *Materializer) {
166 if (!Op)
167 return nullptr;
168 if (Metadata *MappedOp =
169 MapMetadataImpl(Op, VM, Flags, TypeMapper, Materializer))
170 return MappedOp;
171 // Use identity map if MappedOp is null and we can ignore missing entries.
172 if (Flags & RF_IgnoreMissingEntries)
173 return Op;
174
175 // FIXME: This assert crashes during bootstrap, but I think it should be
176 // correct. For now, just match behaviour from before the metadata/value
177 // split.
178 //
179 // llvm_unreachable("Referenced metadata not in value map!");
180 return nullptr;
181}
182
Duncan P. N. Exon Smithb6515d62015-01-14 01:21:24 +0000183static Metadata *cloneMDTuple(const MDTuple *Node, ValueToValueMapTy &VM,
184 RemapFlags Flags,
185 ValueMapTypeRemapper *TypeMapper,
186 ValueMaterializer *Materializer) {
187 SmallVector<Metadata *, 4> Elts;
188 Elts.reserve(Node->getNumOperands());
189 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
190 Elts.push_back(mapMetadataOp(Node->getOperand(I), VM, Flags, TypeMapper,
191 Materializer));
192
193 return MDTuple::get(Node->getContext(), Elts);
194}
195
196static Metadata *cloneMDLocation(const MDLocation *Node, ValueToValueMapTy &VM,
197 RemapFlags Flags,
198 ValueMapTypeRemapper *TypeMapper,
199 ValueMaterializer *Materializer) {
200 return MDLocation::get(
201 Node->getContext(), Node->getLine(), Node->getColumn(),
202 mapMetadataOp(Node->getScope(), VM, Flags, TypeMapper, Materializer),
203 mapMetadataOp(Node->getInlinedAt(), VM, Flags, TypeMapper, Materializer));
204}
205
Duncan P. N. Exon Smith7c69c1e2015-01-14 01:22:47 +0000206static Metadata *cloneMDNode(const UniquableMDNode *Node, ValueToValueMapTy &VM,
207 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
208 ValueMaterializer *Materializer) {
209 switch (Node->getMetadataID()) {
210 default:
211 llvm_unreachable("Invalid UniquableMDNode subclass");
212#define HANDLE_UNIQUABLE_LEAF(CLASS) \
213 case Metadata::CLASS##Kind: \
214 return clone##CLASS(cast<CLASS>(Node), VM, Flags, TypeMapper, \
215 Materializer); \
216 break;
217#include "llvm/IR/Metadata.def"
218 }
219}
220
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000221/// \brief Map a distinct MDNode.
222///
223/// Distinct nodes are not uniqued, so they must always recreated.
Duncan P. N. Exon Smith8725ca82015-01-14 01:05:17 +0000224static Metadata *mapDistinctNode(const UniquableMDNode *Node,
225 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000226 ValueMapTypeRemapper *TypeMapper,
227 ValueMaterializer *Materializer) {
228 assert(Node->isDistinct() && "Expected distinct node");
229
230 // Create the node first so it's available for cyclical references.
231 SmallVector<Metadata *, 4> EmptyOps(Node->getNumOperands());
232 MDTuple *NewMD = MDTuple::getDistinct(Node->getContext(), EmptyOps);
233 mapToMetadata(VM, Node, NewMD);
234
235 // Fix the operands.
236 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
237 NewMD->replaceOperandWith(I, mapMetadataOp(Node->getOperand(I), VM, Flags,
238 TypeMapper, Materializer));
239
240 return NewMD;
241}
242
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000243/// \brief Check whether a uniqued node needs to be remapped.
244///
245/// Check whether a uniqued node needs to be remapped (due to any operands
246/// changing).
247static bool shouldRemapUniquedNode(const UniquableMDNode *Node,
248 ValueToValueMapTy &VM, RemapFlags Flags,
249 ValueMapTypeRemapper *TypeMapper,
250 ValueMaterializer *Materializer) {
251 // Check all operands to see if any need to be remapped.
252 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
253 Metadata *Op = Node->getOperand(I);
254 if (Op != mapMetadataOp(Op, VM, Flags, TypeMapper, Materializer))
255 return true;
256 }
257 return false;
258}
259
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000260/// \brief Map a uniqued MDNode.
261///
262/// Uniqued nodes may not need to be recreated (they may map to themselves).
263static Metadata *mapUniquedNode(const UniquableMDNode *Node,
264 ValueToValueMapTy &VM, RemapFlags Flags,
265 ValueMapTypeRemapper *TypeMapper,
266 ValueMaterializer *Materializer) {
267 assert(!Node->isDistinct() && "Expected uniqued node");
268
269 // Create a dummy node in case we have a metadata cycle.
270 MDNodeFwdDecl *Dummy = MDNode::getTemporary(Node->getContext(), None);
271 mapToMetadata(VM, Node, Dummy);
272
273 // Check all operands to see if any need to be remapped.
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000274 if (!shouldRemapUniquedNode(Node, VM, Flags, TypeMapper, Materializer)) {
275 // Use an identity mapping.
276 mapToSelf(VM, Node);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000277 MDNode::deleteTemporary(Dummy);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000278 return const_cast<Metadata *>(static_cast<const Metadata *>(Node));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000279 }
280
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000281 // At least one operand needs remapping.
Duncan P. N. Exon Smith7c69c1e2015-01-14 01:22:47 +0000282 Metadata *NewMD = cloneMDNode(Node, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000283 Dummy->replaceAllUsesWith(NewMD);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000284 MDNode::deleteTemporary(Dummy);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000285 return mapToMetadata(VM, Node, NewMD);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000286}
287
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000288static Metadata *MapMetadataImpl(const Metadata *MD, ValueToValueMapTy &VM,
289 RemapFlags Flags,
290 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000291 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000292 // If the value already exists in the map, use it.
293 if (Metadata *NewMD = VM.MD().lookup(MD).get())
294 return NewMD;
295
296 if (isa<MDString>(MD))
297 return mapToSelf(VM, MD);
298
299 if (isa<ConstantAsMetadata>(MD))
300 if ((Flags & RF_NoModuleLevelChanges))
301 return mapToSelf(VM, MD);
302
303 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
304 Value *MappedV =
305 MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
306 if (VMD->getValue() == MappedV ||
307 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
308 return mapToSelf(VM, MD);
309
310 // FIXME: This assert crashes during bootstrap, but I think it should be
311 // correct. For now, just match behaviour from before the metadata/value
312 // split.
313 //
314 // assert(MappedV && "Referenced metadata not in value map!");
315 if (MappedV)
Kaelyn Takata22324f32014-12-09 23:32:46 +0000316 return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000317 return nullptr;
318 }
319
Duncan P. N. Exon Smith8725ca82015-01-14 01:05:17 +0000320 const UniquableMDNode *Node = cast<UniquableMDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000321 assert(Node->isResolved() && "Unexpected unresolved node");
322
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000323 // If this is a module-level metadata and we know that nothing at the
324 // module level is changing, then use an identity mapping.
325 if (Flags & RF_NoModuleLevelChanges)
326 return mapToSelf(VM, MD);
327
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000328 if (Node->isDistinct())
329 return mapDistinctNode(Node, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000330
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000331 return mapUniquedNode(Node, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000332}
333
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000334Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
335 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
336 ValueMaterializer *Materializer) {
337 Metadata *NewMD = MapMetadataImpl(MD, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000338 if (NewMD && NewMD != MD)
Duncan P. N. Exon Smith118632d2015-01-12 20:09:34 +0000339 if (auto *N = dyn_cast<UniquableMDNode>(NewMD))
340 N->resolveCycles();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000341 return NewMD;
342}
343
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000344MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
345 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
346 ValueMaterializer *Materializer) {
347 return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags,
348 TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000349}
350
Brian Gaeke6182acf2004-05-19 09:08:12 +0000351/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000352/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000353///
Dan Gohmanca26f792010-08-26 15:41:53 +0000354void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000355 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
356 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000357 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000358 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000359 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000360 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000361 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000362 *op = V;
363 else
364 assert((Flags & RF_IgnoreMissingEntries) &&
365 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000366 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000367
Jay Foad61ea0e42011-06-23 09:09:15 +0000368 // Remap phi nodes' incoming blocks.
369 if (PHINode *PN = dyn_cast<PHINode>(I)) {
370 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
371 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
372 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000373 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000374 PN->setIncomingBlock(i, cast<BasicBlock>(V));
375 else
376 assert((Flags & RF_IgnoreMissingEntries) &&
377 "Referenced block not in value map!");
378 }
379 }
380
Devang Patelc0174042011-08-04 20:02:18 +0000381 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000382 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000383 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000384 for (SmallVectorImpl<std::pair<unsigned, MDNode *>>::iterator
385 MI = MDs.begin(),
386 ME = MDs.end();
387 MI != ME; ++MI) {
388 MDNode *Old = MI->second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000389 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000390 if (New != Old)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000391 I->setMetadata(MI->first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000392 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000393
394 // If the instruction's type is being remapped, do so now.
395 if (TypeMapper)
396 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000397}