blob: c9e474da8af4cb2785d010e26f575335d402dbf0 [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,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000159 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000160 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 Smith5ed90c02015-08-04 13:23:30 +0000164static Metadata *mapMetadataOp(Metadata *Op,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000165 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000166 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith422e5c72015-01-19 22:16:01 +0000167 ValueMapTypeRemapper *TypeMapper,
168 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000169 if (!Op)
170 return nullptr;
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000171 if (Metadata *MappedOp = MapMetadataImpl(Op, DistinctWorklist, VM, Flags,
172 TypeMapper, Materializer))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000173 return MappedOp;
174 // Use identity map if MappedOp is null and we can ignore missing entries.
175 if (Flags & RF_IgnoreMissingEntries)
176 return Op;
177
178 // FIXME: This assert crashes during bootstrap, but I think it should be
179 // correct. For now, just match behaviour from before the metadata/value
180 // split.
181 //
182 // llvm_unreachable("Referenced metadata not in value map!");
183 return nullptr;
184}
185
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000186/// Resolve uniquing cycles involving the given metadata.
187static void resolveCycles(Metadata *MD) {
188 if (auto *N = dyn_cast_or_null<MDNode>(MD))
189 if (!N->isResolved())
190 N->resolveCycles();
191}
192
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000193/// Remap the operands of an MDNode.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000194///
195/// If \c Node is temporary, uniquing cycles are ignored. If \c Node is
196/// distinct, uniquing cycles are resolved as they're found.
197///
198/// \pre \c Node.isDistinct() or \c Node.isTemporary().
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000199static bool remapOperands(MDNode &Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000200 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000201 ValueToValueMapTy &VM, RemapFlags Flags,
202 ValueMapTypeRemapper *TypeMapper,
203 ValueMaterializer *Materializer) {
204 assert(!Node.isUniqued() && "Expected temporary or distinct node");
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000205 const bool IsDistinct = Node.isDistinct();
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000206
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000207 bool AnyChanged = false;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000208 for (unsigned I = 0, E = Node.getNumOperands(); I != E; ++I) {
209 Metadata *Old = Node.getOperand(I);
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000210 Metadata *New = mapMetadataOp(Old, DistinctWorklist, VM, Flags, TypeMapper,
211 Materializer);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000212 if (Old != New) {
213 AnyChanged = true;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000214 Node.replaceOperandWith(I, New);
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000215
216 // Resolve uniquing cycles underneath distinct nodes on the fly so they
217 // don't infect later operands.
218 if (IsDistinct)
219 resolveCycles(New);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000220 }
221 }
222
223 return AnyChanged;
224}
225
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000226/// Map a distinct MDNode.
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000227///
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000228/// Whether distinct nodes change is independent of their operands. If \a
229/// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
230/// place; effectively, they're moved from one graph to another. Otherwise,
231/// they're cloned/duplicated, and the new copy's operands are remapped.
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000232static Metadata *mapDistinctNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000233 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000234 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000235 ValueMapTypeRemapper *TypeMapper,
236 ValueMaterializer *Materializer) {
237 assert(Node->isDistinct() && "Expected distinct node");
238
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000239 MDNode *NewMD;
240 if (Flags & RF_MoveDistinctMDs)
241 NewMD = const_cast<MDNode *>(Node);
242 else
243 NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000244
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000245 // Remap operands later.
246 DistinctWorklist.push_back(NewMD);
247 return mapToMetadata(VM, Node, NewMD);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000248}
249
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000250/// \brief Map a uniqued MDNode.
251///
252/// Uniqued nodes may not need to be recreated (they may map to themselves).
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000253static Metadata *mapUniquedNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000254 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000255 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smithc862be82015-01-19 22:40:25 +0000256 ValueMapTypeRemapper *TypeMapper,
257 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000258 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000259
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000260 // Create a temporary node and map it upfront in case we have a uniquing
261 // cycle. If necessary, this mapping will get updated by RAUW logic before
262 // returning.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000263 auto ClonedMD = Node->clone();
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000264 mapToMetadata(VM, Node, ClonedMD.get());
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000265 if (!remapOperands(*ClonedMD, DistinctWorklist, VM, Flags, TypeMapper,
266 Materializer)) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000267 // No operands changed, so use the original.
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000268 ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node));
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000269 return const_cast<MDNode *>(Node);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000270 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000271
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000272 // Uniquify the cloned node.
273 return MDNode::replaceWithUniqued(std::move(ClonedMD));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000274}
275
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000276static Metadata *MapMetadataImpl(const Metadata *MD,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000277 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000278 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000279 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000280 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000281 // If the value already exists in the map, use it.
282 if (Metadata *NewMD = VM.MD().lookup(MD).get())
283 return NewMD;
284
285 if (isa<MDString>(MD))
286 return mapToSelf(VM, MD);
287
288 if (isa<ConstantAsMetadata>(MD))
289 if ((Flags & RF_NoModuleLevelChanges))
290 return mapToSelf(VM, MD);
291
292 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
293 Value *MappedV =
294 MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
295 if (VMD->getValue() == MappedV ||
296 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
297 return mapToSelf(VM, MD);
298
299 // FIXME: This assert crashes during bootstrap, but I think it should be
300 // correct. For now, just match behaviour from before the metadata/value
301 // split.
302 //
303 // assert(MappedV && "Referenced metadata not in value map!");
304 if (MappedV)
Kaelyn Takata22324f32014-12-09 23:32:46 +0000305 return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000306 return nullptr;
307 }
308
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000309 // Note: this cast precedes the Flags check so we always get its associated
310 // assertion.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000311 const MDNode *Node = cast<MDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000312
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000313 // If this is a module-level metadata and we know that nothing at the
314 // module level is changing, then use an identity mapping.
315 if (Flags & RF_NoModuleLevelChanges)
316 return mapToSelf(VM, MD);
317
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000318 // Require resolved nodes whenever metadata might be remapped.
319 assert(Node->isResolved() && "Unexpected unresolved node");
320
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000321 if (Node->isDistinct())
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000322 return mapDistinctNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
323 Materializer);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000324
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000325 return mapUniquedNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
326 Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000327}
328
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000329Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
330 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
331 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000332 SmallVector<MDNode *, 8> DistinctWorklist;
333 Metadata *NewMD = MapMetadataImpl(MD, DistinctWorklist, VM, Flags, TypeMapper,
334 Materializer);
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000335
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000336 // When there are no module-level changes, it's possible that the metadata
337 // graph has temporaries. Skip the logic to resolve cycles, since it's
338 // unnecessary (and invalid) in that case.
339 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000340 return NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000341
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000342 // Resolve cycles involving the entry metadata.
343 resolveCycles(NewMD);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000344
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000345 // Remap the operands of distinct MDNodes.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000346 while (!DistinctWorklist.empty())
347 remapOperands(*DistinctWorklist.pop_back_val(), DistinctWorklist, VM, Flags,
348 TypeMapper, Materializer);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000349
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000350 return NewMD;
351}
352
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000353MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
354 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
355 ValueMaterializer *Materializer) {
356 return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags,
357 TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000358}
359
Brian Gaeke6182acf2004-05-19 09:08:12 +0000360/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000361/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000362///
Dan Gohmanca26f792010-08-26 15:41:53 +0000363void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000364 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
365 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000366 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000367 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000368 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000369 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000370 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000371 *op = V;
372 else
373 assert((Flags & RF_IgnoreMissingEntries) &&
374 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000375 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000376
Jay Foad61ea0e42011-06-23 09:09:15 +0000377 // Remap phi nodes' incoming blocks.
378 if (PHINode *PN = dyn_cast<PHINode>(I)) {
379 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
380 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
381 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000382 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000383 PN->setIncomingBlock(i, cast<BasicBlock>(V));
384 else
385 assert((Flags & RF_IgnoreMissingEntries) &&
386 "Referenced block not in value map!");
387 }
388 }
389
Devang Patelc0174042011-08-04 20:02:18 +0000390 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000391 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000392 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000393 for (const auto &MI : MDs) {
394 MDNode *Old = MI.second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000395 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000396 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000397 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000398 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399
David Blaikie348de692015-04-23 21:36:23 +0000400 if (!TypeMapper)
401 return;
402
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000403 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000404 if (auto CS = CallSite(I)) {
405 SmallVector<Type *, 3> Tys;
406 FunctionType *FTy = CS.getFunctionType();
407 Tys.reserve(FTy->getNumParams());
408 for (Type *Ty : FTy->params())
409 Tys.push_back(TypeMapper->remapType(Ty));
410 CS.mutateFunctionType(FunctionType::get(
411 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000412 return;
413 }
414 if (auto *AI = dyn_cast<AllocaInst>(I))
415 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000416 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000417 GEP->setSourceElementType(
418 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000419 GEP->setResultElementType(
420 TypeMapper->remapType(GEP->getResultElementType()));
421 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000422 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000423}