blob: f396b42dabe0fa6ba0ad5f8d40947a257e399ac8 [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"
David Blaikie348de692015-04-23 21:36:23 +000016#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/InlineAsm.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Metadata.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000022using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000023
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000024// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000025void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000026void ValueMaterializer::anchor() {}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000027
28Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +000029 ValueMapTypeRemapper *TypeMapper,
30 ValueMaterializer *Materializer) {
Chris Lattner43f8d162011-01-08 08:15:20 +000031 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +000032
Chris Lattner43f8d162011-01-08 08:15:20 +000033 // If the value already exists in the map, use it.
34 if (I != VM.end() && I->second) return I->second;
35
James Molloyf6f121e2013-05-28 15:17:05 +000036 // If we have a materializer and it can materialize a value, use that.
37 if (Materializer) {
38 if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
39 return VM[V] = NewV;
40 }
41
Dan Gohmanca26f792010-08-26 15:41:53 +000042 // Global values do not need to be seeded into the VM if they
43 // are using the identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000044 if (isa<GlobalValue>(V))
Chris Lattner43f8d162011-01-08 08:15:20 +000045 return VM[V] = const_cast<Value*>(V);
Chris Lattner8b4cf5e2011-07-15 23:18:40 +000046
47 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
48 // Inline asm may need *type* remapping.
49 FunctionType *NewTy = IA->getFunctionType();
50 if (TypeMapper) {
51 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
52
53 if (NewTy != IA->getFunctionType())
54 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
55 IA->hasSideEffects(), IA->isAlignStack());
56 }
57
58 return VM[V] = const_cast<Value*>(V);
59 }
Chris Lattner6aa34b02003-10-06 15:23:43 +000060
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000061 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
62 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +000063 // If this is a module-level metadata and we know that nothing at the module
64 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
66 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000067
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +000068 auto *MappedMD = MapMetadata(MD, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000069 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
70 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000071
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000072 // FIXME: This assert crashes during bootstrap, but I think it should be
73 // correct. For now, just match behaviour from before the metadata/value
74 // split.
75 //
76 // assert(MappedMD && "Referenced metadata value not in value map");
77 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +000078 }
79
Chris Lattner43f8d162011-01-08 08:15:20 +000080 // Okay, this either must be a constant (which may or may not be mappable) or
81 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +000082 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +000083 if (!C)
84 return nullptr;
Chris Lattnercf5a47d2009-10-29 00:28:30 +000085
Chris Lattner43f8d162011-01-08 08:15:20 +000086 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000087 Function *F =
James Molloyf6f121e2013-05-28 15:17:05 +000088 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +000089 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
James Molloyf6f121e2013-05-28 15:17:05 +000090 Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +000091 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000092 }
Chris Lattnercf5a47d2009-10-29 00:28:30 +000093
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000094 // Otherwise, we have some other constant to remap. Start by checking to see
95 // if all operands have an identity remapping.
96 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +000097 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000098 for (; OpNo != NumOperands; ++OpNo) {
99 Value *Op = C->getOperand(OpNo);
James Molloyf6f121e2013-05-28 15:17:05 +0000100 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000101 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000102 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000103
104 // See if the type mapper wants to remap the type as well.
105 Type *NewTy = C->getType();
106 if (TypeMapper)
107 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000108
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000109 // If the result type and all operands match up, then just insert an identity
110 // mapping.
111 if (OpNo == NumOperands && NewTy == C->getType())
112 return VM[V] = C;
113
114 // Okay, we need to create a new constant. We've already processed some or
115 // all of the operands, set them all up now.
116 SmallVector<Constant*, 8> Ops;
117 Ops.reserve(NumOperands);
118 for (unsigned j = 0; j != OpNo; ++j)
119 Ops.push_back(cast<Constant>(C->getOperand(j)));
120
121 // If one of the operands mismatch, push it and the other mapped operands.
122 if (OpNo != NumOperands) {
123 Ops.push_back(cast<Constant>(Mapped));
124
125 // Map the rest of the operands that aren't processed yet.
126 for (++OpNo; OpNo != NumOperands; ++OpNo)
127 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
James Molloyf6f121e2013-05-28 15:17:05 +0000128 Flags, TypeMapper, Materializer));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000129 }
130
131 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
132 return VM[V] = CE->getWithOperands(Ops, NewTy);
133 if (isa<ConstantArray>(C))
134 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
135 if (isa<ConstantStruct>(C))
136 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
137 if (isa<ConstantVector>(C))
138 return VM[V] = ConstantVector::get(Ops);
139 // If this is a no-operand constant, it must be because the type was remapped.
140 if (isa<UndefValue>(C))
141 return VM[V] = UndefValue::get(NewTy);
142 if (isa<ConstantAggregateZero>(C))
143 return VM[V] = ConstantAggregateZero::get(NewTy);
144 assert(isa<ConstantPointerNull>(C));
145 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000146}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000147
Kaelyn Takata22324f32014-12-09 23:32:46 +0000148static Metadata *mapToMetadata(ValueToValueMapTy &VM, const Metadata *Key,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000149 Metadata *Val) {
150 VM.MD()[Key].reset(Val);
151 return Val;
152}
153
154static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
Kaelyn Takata22324f32014-12-09 23:32:46 +0000155 return mapToMetadata(VM, MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000156}
157
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000158static Metadata *MapMetadataImpl(const Metadata *MD,
159 SmallVectorImpl<MDNode *> &Cycles,
160 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000161 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000162 ValueMaterializer *Materializer);
163
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000164static Metadata *mapMetadataOp(Metadata *Op, SmallVectorImpl<MDNode *> &Cycles,
165 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith422e5c72015-01-19 22:16:01 +0000166 ValueMapTypeRemapper *TypeMapper,
167 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000168 if (!Op)
169 return nullptr;
170 if (Metadata *MappedOp =
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000171 MapMetadataImpl(Op, Cycles, VM, Flags, TypeMapper, Materializer))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000172 return MappedOp;
173 // Use identity map if MappedOp is null and we can ignore missing entries.
174 if (Flags & RF_IgnoreMissingEntries)
175 return Op;
176
177 // FIXME: This assert crashes during bootstrap, but I think it should be
178 // correct. For now, just match behaviour from before the metadata/value
179 // split.
180 //
181 // llvm_unreachable("Referenced metadata not in value map!");
182 return nullptr;
183}
184
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000185/// \brief Remap nodes.
186///
187/// Insert \c NewNode in the value map, and then remap \c OldNode's operands.
188/// Assumes that \c NewNode is already a clone of \c OldNode.
189///
190/// \pre \c NewNode is a clone of \c OldNode.
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000191static bool remap(const MDNode *OldNode, MDNode *NewNode,
192 SmallVectorImpl<MDNode *> &Cycles, ValueToValueMapTy &VM,
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000193 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000194 ValueMaterializer *Materializer) {
195 assert(OldNode->getNumOperands() == NewNode->getNumOperands() &&
196 "Expected nodes to match");
197 assert(OldNode->isResolved() && "Expected resolved node");
198 assert(!NewNode->isUniqued() && "Expected non-uniqued node");
199
200 // Map the node upfront so it's available for cyclic references.
201 mapToMetadata(VM, OldNode, NewNode);
202 bool AnyChanged = false;
203 for (unsigned I = 0, E = OldNode->getNumOperands(); I != E; ++I) {
204 Metadata *Old = OldNode->getOperand(I);
205 assert(NewNode->getOperand(I) == Old &&
206 "Expected old operands to already be in place");
207
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000208 Metadata *New = mapMetadataOp(OldNode->getOperand(I), Cycles, VM, Flags,
209 TypeMapper, Materializer);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000210 if (Old != New) {
211 AnyChanged = true;
212 NewNode->replaceOperandWith(I, New);
213 }
214 }
215
216 return AnyChanged;
217}
218
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000219/// \brief Map a distinct MDNode.
220///
221/// Distinct nodes are not uniqued, so they must always recreated.
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000222static Metadata *mapDistinctNode(const MDNode *Node,
223 SmallVectorImpl<MDNode *> &Cycles,
224 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000225 ValueMapTypeRemapper *TypeMapper,
226 ValueMaterializer *Materializer) {
227 assert(Node->isDistinct() && "Expected distinct node");
228
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000229 MDNode *NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000230 remap(Node, NewMD, Cycles, VM, Flags, TypeMapper, Materializer);
231
232 // Track any cycles beneath this node.
233 for (Metadata *Op : NewMD->operands())
234 if (auto *Node = dyn_cast_or_null<MDNode>(Op))
235 if (!Node->isResolved())
236 Cycles.push_back(Node);
237
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000238 return NewMD;
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000239}
240
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000241/// \brief Map a uniqued MDNode.
242///
243/// Uniqued nodes may not need to be recreated (they may map to themselves).
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000244static Metadata *mapUniquedNode(const MDNode *Node,
245 SmallVectorImpl<MDNode *> &Cycles,
246 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smithc862be82015-01-19 22:40:25 +0000247 ValueMapTypeRemapper *TypeMapper,
248 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000249 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000250
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000251 // Create a temporary node upfront in case we have a metadata cycle.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000252 auto ClonedMD = Node->clone();
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000253 if (!remap(Node, ClonedMD.get(), Cycles, VM, Flags, TypeMapper, Materializer))
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000254 // No operands changed, so use the identity mapping.
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000255 return mapToSelf(VM, Node);
256
257 // At least one operand has changed, so uniquify the cloned node.
258 return mapToMetadata(VM, Node,
259 MDNode::replaceWithUniqued(std::move(ClonedMD)));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000260}
261
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000262static Metadata *MapMetadataImpl(const Metadata *MD,
263 SmallVectorImpl<MDNode *> &Cycles,
264 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000265 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000266 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000267 // If the value already exists in the map, use it.
268 if (Metadata *NewMD = VM.MD().lookup(MD).get())
269 return NewMD;
270
271 if (isa<MDString>(MD))
272 return mapToSelf(VM, MD);
273
274 if (isa<ConstantAsMetadata>(MD))
275 if ((Flags & RF_NoModuleLevelChanges))
276 return mapToSelf(VM, MD);
277
278 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
279 Value *MappedV =
280 MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
281 if (VMD->getValue() == MappedV ||
282 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
283 return mapToSelf(VM, MD);
284
285 // FIXME: This assert crashes during bootstrap, but I think it should be
286 // correct. For now, just match behaviour from before the metadata/value
287 // split.
288 //
289 // assert(MappedV && "Referenced metadata not in value map!");
290 if (MappedV)
Kaelyn Takata22324f32014-12-09 23:32:46 +0000291 return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000292 return nullptr;
293 }
294
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000295 // Note: this cast precedes the Flags check so we always get its associated
296 // assertion.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000297 const MDNode *Node = cast<MDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000298
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000299 // If this is a module-level metadata and we know that nothing at the
300 // module level is changing, then use an identity mapping.
301 if (Flags & RF_NoModuleLevelChanges)
302 return mapToSelf(VM, MD);
303
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000304 // Require resolved nodes whenever metadata might be remapped.
305 assert(Node->isResolved() && "Unexpected unresolved node");
306
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000307 if (Node->isDistinct())
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000308 return mapDistinctNode(Node, Cycles, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000309
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000310 return mapUniquedNode(Node, Cycles, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000311}
312
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000313Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
314 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
315 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000316 SmallVector<MDNode *, 8> Cycles;
317 Metadata *NewMD =
318 MapMetadataImpl(MD, Cycles, VM, Flags, TypeMapper, Materializer);
319
320 // Resolve cycles underneath MD.
321 if (NewMD && NewMD != MD) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000322 if (auto *N = dyn_cast<MDNode>(NewMD))
323 if (!N->isResolved())
324 N->resolveCycles();
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000325
326 for (MDNode *N : Cycles)
327 if (!N->isResolved())
328 N->resolveCycles();
329 } else {
330 // Shouldn't get unresolved cycles if nothing was remapped.
331 assert(Cycles.empty() && "Expected no unresolved cycles");
332 }
333
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000334 return NewMD;
335}
336
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000337MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
338 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
339 ValueMaterializer *Materializer) {
340 return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags,
341 TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000342}
343
Brian Gaeke6182acf2004-05-19 09:08:12 +0000344/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000345/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000346///
Dan Gohmanca26f792010-08-26 15:41:53 +0000347void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000348 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
349 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000350 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000351 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000352 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000353 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000354 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000355 *op = V;
356 else
357 assert((Flags & RF_IgnoreMissingEntries) &&
358 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000359 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000360
Jay Foad61ea0e42011-06-23 09:09:15 +0000361 // Remap phi nodes' incoming blocks.
362 if (PHINode *PN = dyn_cast<PHINode>(I)) {
363 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
364 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
365 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000366 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000367 PN->setIncomingBlock(i, cast<BasicBlock>(V));
368 else
369 assert((Flags & RF_IgnoreMissingEntries) &&
370 "Referenced block not in value map!");
371 }
372 }
373
Devang Patelc0174042011-08-04 20:02:18 +0000374 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000375 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000376 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000377 for (SmallVectorImpl<std::pair<unsigned, MDNode *>>::iterator
378 MI = MDs.begin(),
379 ME = MDs.end();
380 MI != ME; ++MI) {
381 MDNode *Old = MI->second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000382 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000383 if (New != Old)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000384 I->setMetadata(MI->first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000385 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000386
David Blaikie348de692015-04-23 21:36:23 +0000387 if (!TypeMapper)
388 return;
389
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000390 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000391 if (auto CS = CallSite(I)) {
392 SmallVector<Type *, 3> Tys;
393 FunctionType *FTy = CS.getFunctionType();
394 Tys.reserve(FTy->getNumParams());
395 for (Type *Ty : FTy->params())
396 Tys.push_back(TypeMapper->remapType(Ty));
397 CS.mutateFunctionType(FunctionType::get(
398 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
399 } else
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000400 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000401}