blob: b658ffba0c271c9e39cb27c206d3bdf16856524e [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"
David Blaikie88208842015-08-21 20:16:51 +000022#include "llvm/IR/Operator.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000023using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000024
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000025// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000026void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000027void ValueMaterializer::anchor() {}
Rafael Espindola19b52382015-11-27 20:28:19 +000028void ValueMaterializer::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
29}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000030
31Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +000032 ValueMapTypeRemapper *TypeMapper,
33 ValueMaterializer *Materializer) {
Chris Lattner43f8d162011-01-08 08:15:20 +000034 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +000035
Chris Lattner43f8d162011-01-08 08:15:20 +000036 // If the value already exists in the map, use it.
37 if (I != VM.end() && I->second) return I->second;
38
James Molloyf6f121e2013-05-28 15:17:05 +000039 // If we have a materializer and it can materialize a value, use that.
40 if (Materializer) {
Rafael Espindola19b52382015-11-27 20:28:19 +000041 if (Value *NewV =
42 Materializer->materializeDeclFor(const_cast<Value *>(V))) {
43 VM[V] = NewV;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +000044 if (auto *NewGV = dyn_cast<GlobalValue>(NewV))
45 Materializer->materializeInitFor(
46 NewGV, const_cast<GlobalValue *>(cast<GlobalValue>(V)));
Rafael Espindola19b52382015-11-27 20:28:19 +000047 return NewV;
48 }
James Molloyf6f121e2013-05-28 15:17:05 +000049 }
50
Dan Gohmanca26f792010-08-26 15:41:53 +000051 // Global values do not need to be seeded into the VM if they
52 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +000053 if (isa<GlobalValue>(V)) {
54 if (Flags & RF_NullMapMissingGlobalValues) {
55 assert(!(Flags & RF_IgnoreMissingEntries) &&
56 "Illegal to specify both RF_NullMapMissingGlobalValues and "
57 "RF_IgnoreMissingEntries");
58 return nullptr;
59 }
Chris Lattner43f8d162011-01-08 08:15:20 +000060 return VM[V] = const_cast<Value*>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +000061 }
62
Chris Lattner8b4cf5e2011-07-15 23:18:40 +000063 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
64 // Inline asm may need *type* remapping.
65 FunctionType *NewTy = IA->getFunctionType();
66 if (TypeMapper) {
67 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
68
69 if (NewTy != IA->getFunctionType())
70 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
71 IA->hasSideEffects(), IA->isAlignStack());
72 }
73
74 return VM[V] = const_cast<Value*>(V);
75 }
Chris Lattner6aa34b02003-10-06 15:23:43 +000076
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000077 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
78 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +000079 // If this is a module-level metadata and we know that nothing at the module
80 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000081 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
82 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000083
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +000084 auto *MappedMD = MapMetadata(MD, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000085 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
86 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000087
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000088 // FIXME: This assert crashes during bootstrap, but I think it should be
89 // correct. For now, just match behaviour from before the metadata/value
90 // split.
91 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +000092 // assert((MappedMD || (Flags & RF_NullMapMissingGlobalValues)) &&
93 // "Referenced metadata value not in value map");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000094 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +000095 }
96
Chris Lattner43f8d162011-01-08 08:15:20 +000097 // Okay, this either must be a constant (which may or may not be mappable) or
98 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +000099 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000100 if (!C)
101 return nullptr;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000102
Chris Lattner43f8d162011-01-08 08:15:20 +0000103 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000104 Function *F =
James Molloyf6f121e2013-05-28 15:17:05 +0000105 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +0000106 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
James Molloyf6f121e2013-05-28 15:17:05 +0000107 Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +0000108 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000109 }
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000110
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000111 // Otherwise, we have some other constant to remap. Start by checking to see
112 // if all operands have an identity remapping.
113 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000114 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000115 for (; OpNo != NumOperands; ++OpNo) {
116 Value *Op = C->getOperand(OpNo);
James Molloyf6f121e2013-05-28 15:17:05 +0000117 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000118 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000119 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000120
121 // See if the type mapper wants to remap the type as well.
122 Type *NewTy = C->getType();
123 if (TypeMapper)
124 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000125
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000126 // If the result type and all operands match up, then just insert an identity
127 // mapping.
128 if (OpNo == NumOperands && NewTy == C->getType())
129 return VM[V] = C;
130
131 // Okay, we need to create a new constant. We've already processed some or
132 // all of the operands, set them all up now.
133 SmallVector<Constant*, 8> Ops;
134 Ops.reserve(NumOperands);
135 for (unsigned j = 0; j != OpNo; ++j)
136 Ops.push_back(cast<Constant>(C->getOperand(j)));
137
138 // If one of the operands mismatch, push it and the other mapped operands.
139 if (OpNo != NumOperands) {
140 Ops.push_back(cast<Constant>(Mapped));
141
142 // Map the rest of the operands that aren't processed yet.
143 for (++OpNo; OpNo != NumOperands; ++OpNo)
144 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
James Molloyf6f121e2013-05-28 15:17:05 +0000145 Flags, TypeMapper, Materializer));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146 }
David Blaikie88208842015-08-21 20:16:51 +0000147 Type *NewSrcTy = nullptr;
148 if (TypeMapper)
149 if (auto *GEPO = dyn_cast<GEPOperator>(C))
150 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
151
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000152 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
David Blaikie88208842015-08-21 20:16:51 +0000153 return VM[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000154 if (isa<ConstantArray>(C))
155 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
156 if (isa<ConstantStruct>(C))
157 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
158 if (isa<ConstantVector>(C))
159 return VM[V] = ConstantVector::get(Ops);
160 // If this is a no-operand constant, it must be because the type was remapped.
161 if (isa<UndefValue>(C))
162 return VM[V] = UndefValue::get(NewTy);
163 if (isa<ConstantAggregateZero>(C))
164 return VM[V] = ConstantAggregateZero::get(NewTy);
165 assert(isa<ConstantPointerNull>(C));
166 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000167}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000168
Kaelyn Takata22324f32014-12-09 23:32:46 +0000169static Metadata *mapToMetadata(ValueToValueMapTy &VM, const Metadata *Key,
Teresa Johnsonb703c772016-03-29 18:24:19 +0000170 Metadata *Val) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000171 VM.MD()[Key].reset(Val);
172 return Val;
173}
174
Teresa Johnsonb703c772016-03-29 18:24:19 +0000175static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
176 return mapToMetadata(VM, MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000177}
178
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000179static Metadata *MapMetadataImpl(const Metadata *MD,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000180 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000181 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000182 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000183 ValueMaterializer *Materializer);
184
Duncan P. N. Exon Smith5ed90c02015-08-04 13:23:30 +0000185static Metadata *mapMetadataOp(Metadata *Op,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000186 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000187 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith422e5c72015-01-19 22:16:01 +0000188 ValueMapTypeRemapper *TypeMapper,
189 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000190 if (!Op)
191 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000192
193 if (Materializer && !Materializer->isMetadataNeeded(Op))
194 return nullptr;
195
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000196 if (Metadata *MappedOp = MapMetadataImpl(Op, DistinctWorklist, VM, Flags,
197 TypeMapper, Materializer))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000198 return MappedOp;
199 // Use identity map if MappedOp is null and we can ignore missing entries.
200 if (Flags & RF_IgnoreMissingEntries)
201 return Op;
202
203 // FIXME: This assert crashes during bootstrap, but I think it should be
204 // correct. For now, just match behaviour from before the metadata/value
205 // split.
206 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000207 // assert((Flags & RF_NullMapMissingGlobalValues) &&
208 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000209 return nullptr;
210}
211
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000212/// Resolve uniquing cycles involving the given metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000213static void resolveCycles(Metadata *MD) {
214 if (auto *N = dyn_cast_or_null<MDNode>(MD))
215 if (!N->isResolved())
216 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000217}
218
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000219/// Remap the operands of an MDNode.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000220///
221/// If \c Node is temporary, uniquing cycles are ignored. If \c Node is
222/// distinct, uniquing cycles are resolved as they're found.
223///
224/// \pre \c Node.isDistinct() or \c Node.isTemporary().
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000225static bool remapOperands(MDNode &Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000226 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000227 ValueToValueMapTy &VM, RemapFlags Flags,
228 ValueMapTypeRemapper *TypeMapper,
229 ValueMaterializer *Materializer) {
230 assert(!Node.isUniqued() && "Expected temporary or distinct node");
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000231 const bool IsDistinct = Node.isDistinct();
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000232
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000233 bool AnyChanged = false;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000234 for (unsigned I = 0, E = Node.getNumOperands(); I != E; ++I) {
235 Metadata *Old = Node.getOperand(I);
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000236 Metadata *New = mapMetadataOp(Old, DistinctWorklist, VM, Flags, TypeMapper,
237 Materializer);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000238 if (Old != New) {
239 AnyChanged = true;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000240 Node.replaceOperandWith(I, New);
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000241
242 // Resolve uniquing cycles underneath distinct nodes on the fly so they
243 // don't infect later operands.
244 if (IsDistinct)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000245 resolveCycles(New);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000246 }
247 }
248
249 return AnyChanged;
250}
251
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000252/// Map a distinct MDNode.
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000253///
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000254/// Whether distinct nodes change is independent of their operands. If \a
255/// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
256/// place; effectively, they're moved from one graph to another. Otherwise,
257/// they're cloned/duplicated, and the new copy's operands are remapped.
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000258static Metadata *mapDistinctNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000259 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000260 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000261 ValueMapTypeRemapper *TypeMapper,
262 ValueMaterializer *Materializer) {
263 assert(Node->isDistinct() && "Expected distinct node");
264
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000265 MDNode *NewMD;
266 if (Flags & RF_MoveDistinctMDs)
267 NewMD = const_cast<MDNode *>(Node);
268 else
269 NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000270
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000271 // Remap operands later.
272 DistinctWorklist.push_back(NewMD);
Teresa Johnsonb703c772016-03-29 18:24:19 +0000273 return mapToMetadata(VM, Node, NewMD);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000274}
275
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000276/// \brief Map a uniqued MDNode.
277///
278/// Uniqued nodes may not need to be recreated (they may map to themselves).
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000279static Metadata *mapUniquedNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000280 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000281 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smithc862be82015-01-19 22:40:25 +0000282 ValueMapTypeRemapper *TypeMapper,
283 ValueMaterializer *Materializer) {
Teresa Johnsonb703c772016-03-29 18:24:19 +0000284 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000285
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000286 // Create a temporary node and map it upfront in case we have a uniquing
287 // cycle. If necessary, this mapping will get updated by RAUW logic before
288 // returning.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000289 auto ClonedMD = Node->clone();
Teresa Johnsonb703c772016-03-29 18:24:19 +0000290 mapToMetadata(VM, Node, ClonedMD.get());
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000291 if (!remapOperands(*ClonedMD, DistinctWorklist, VM, Flags, TypeMapper,
292 Materializer)) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000293 // No operands changed, so use the original.
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000294 ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node));
Teresa Johnsonb703c772016-03-29 18:24:19 +0000295 return const_cast<MDNode *>(Node);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000296 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000297
Teresa Johnsonb703c772016-03-29 18:24:19 +0000298 // Uniquify the cloned node.
299 return MDNode::replaceWithUniqued(std::move(ClonedMD));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000300}
301
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000302static Metadata *MapMetadataImpl(const Metadata *MD,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000303 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000304 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000305 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000306 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000307 // If the value already exists in the map, use it.
308 if (Metadata *NewMD = VM.MD().lookup(MD).get())
309 return NewMD;
310
311 if (isa<MDString>(MD))
Teresa Johnsonb703c772016-03-29 18:24:19 +0000312 return mapToSelf(VM, MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000313
314 if (isa<ConstantAsMetadata>(MD))
315 if ((Flags & RF_NoModuleLevelChanges))
Teresa Johnsonb703c772016-03-29 18:24:19 +0000316 return mapToSelf(VM, MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000317
318 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
319 Value *MappedV =
320 MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
321 if (VMD->getValue() == MappedV ||
322 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
Teresa Johnsonb703c772016-03-29 18:24:19 +0000323 return mapToSelf(VM, MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000324
325 // FIXME: This assert crashes during bootstrap, but I think it should be
326 // correct. For now, just match behaviour from before the metadata/value
327 // split.
328 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000329 // assert((MappedV || (Flags & RF_NullMapMissingGlobalValues)) &&
330 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000331 if (MappedV)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000332 return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000333 return nullptr;
334 }
335
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000336 // Note: this cast precedes the Flags check so we always get its associated
337 // assertion.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000338 const MDNode *Node = cast<MDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000339
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000340 // If this is a module-level metadata and we know that nothing at the
341 // module level is changing, then use an identity mapping.
342 if (Flags & RF_NoModuleLevelChanges)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000343 return mapToSelf(VM, MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000344
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000345 // Require resolved nodes whenever metadata might be remapped.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000346 assert(Node->isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000347
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000348 if (Node->isDistinct())
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000349 return mapDistinctNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
350 Materializer);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000351
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000352 return mapUniquedNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
353 Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000354}
355
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000356Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
357 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
358 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000359 SmallVector<MDNode *, 8> DistinctWorklist;
360 Metadata *NewMD = MapMetadataImpl(MD, DistinctWorklist, VM, Flags, TypeMapper,
361 Materializer);
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000362
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000363 // When there are no module-level changes, it's possible that the metadata
364 // graph has temporaries. Skip the logic to resolve cycles, since it's
365 // unnecessary (and invalid) in that case.
366 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000367 return NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000368
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000369 // Resolve cycles involving the entry metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000370 resolveCycles(NewMD);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000371
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000372 // Remap the operands of distinct MDNodes.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000373 while (!DistinctWorklist.empty())
374 remapOperands(*DistinctWorklist.pop_back_val(), DistinctWorklist, VM, Flags,
375 TypeMapper, Materializer);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000376
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000377 return NewMD;
378}
379
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000380MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
381 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
382 ValueMaterializer *Materializer) {
383 return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags,
384 TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000385}
386
Brian Gaeke6182acf2004-05-19 09:08:12 +0000387/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000388/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000389///
Dan Gohmanca26f792010-08-26 15:41:53 +0000390void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000391 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
392 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000393 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000394 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000395 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000396 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000397 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000398 *op = V;
399 else
400 assert((Flags & RF_IgnoreMissingEntries) &&
401 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000402 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000403
Jay Foad61ea0e42011-06-23 09:09:15 +0000404 // Remap phi nodes' incoming blocks.
405 if (PHINode *PN = dyn_cast<PHINode>(I)) {
406 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
407 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
408 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000409 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000410 PN->setIncomingBlock(i, cast<BasicBlock>(V));
411 else
412 assert((Flags & RF_IgnoreMissingEntries) &&
413 "Referenced block not in value map!");
414 }
415 }
416
Devang Patelc0174042011-08-04 20:02:18 +0000417 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000418 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000419 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000420 for (const auto &MI : MDs) {
421 MDNode *Old = MI.second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000422 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000423 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000424 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000425 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000426
David Blaikie348de692015-04-23 21:36:23 +0000427 if (!TypeMapper)
428 return;
429
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000430 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000431 if (auto CS = CallSite(I)) {
432 SmallVector<Type *, 3> Tys;
433 FunctionType *FTy = CS.getFunctionType();
434 Tys.reserve(FTy->getNumParams());
435 for (Type *Ty : FTy->params())
436 Tys.push_back(TypeMapper->remapType(Ty));
437 CS.mutateFunctionType(FunctionType::get(
438 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000439 return;
440 }
441 if (auto *AI = dyn_cast<AllocaInst>(I))
442 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000443 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000444 GEP->setSourceElementType(
445 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000446 GEP->setResultElementType(
447 TypeMapper->remapType(GEP->getResultElementType()));
448 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000449 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000450}