blob: 64e91852a58709c8a9d16d2126fbd709aeacec80 [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
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000031namespace {
32
33class Mapper {
34 ValueToValueMapTy &VM;
35 RemapFlags Flags;
36 ValueMapTypeRemapper *TypeMapper;
37 ValueMaterializer *Materializer;
38
39 SmallVector<MDNode *, 8> DistinctWorklist;
40
41public:
42 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
43 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
44 : VM(VM), Flags(Flags), TypeMapper(TypeMapper),
45 Materializer(Materializer) {}
46
47 ~Mapper();
48
49 Value *mapValue(const Value *V);
50
51 /// Map metadata.
52 ///
53 /// Find the mapping for MD. Guarantees that the return will be resolved
54 /// (not an MDNode, or MDNode::isResolved() returns true).
55 Metadata *mapMetadata(const Metadata *MD);
56
57private:
58 /// Map metadata helper.
59 ///
60 /// Co-recursively finds the mapping for MD. If this returns an MDNode, it's
61 /// possible that MDNode::isResolved() will return false.
62 Metadata *mapMetadataImpl(const Metadata *MD);
63 Metadata *mapMetadataOp(Metadata *Op);
64
65 /// Remap the operands of an MDNode.
66 ///
67 /// If \c Node is temporary, uniquing cycles are ignored. If \c Node is
68 /// distinct, uniquing cycles are resolved as they're found.
69 ///
70 /// \pre \c Node.isDistinct() or \c Node.isTemporary().
71 bool remapOperands(MDNode &Node);
72
73 /// Map a distinct MDNode.
74 ///
75 /// Whether distinct nodes change is independent of their operands. If \a
76 /// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
77 /// place; effectively, they're moved from one graph to another. Otherwise,
78 /// they're cloned/duplicated, and the new copy's operands are remapped.
79 Metadata *mapDistinctNode(const MDNode *Node);
80
81 /// Map a uniqued MDNode.
82 ///
83 /// Uniqued nodes may not need to be recreated (they may map to themselves).
84 Metadata *mapUniquedNode(const MDNode *Node);
85
86 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
87 Metadata *mapToSelf(const Metadata *MD);
88};
89
90} // end namespace
91
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000092Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +000093 ValueMapTypeRemapper *TypeMapper,
94 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000095 return Mapper(VM, Flags, TypeMapper, Materializer).mapValue(V);
96}
97
98Value *Mapper::mapValue(const Value *V) {
Chris Lattner43f8d162011-01-08 08:15:20 +000099 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000100
Chris Lattner43f8d162011-01-08 08:15:20 +0000101 // If the value already exists in the map, use it.
102 if (I != VM.end() && I->second) return I->second;
103
James Molloyf6f121e2013-05-28 15:17:05 +0000104 // If we have a materializer and it can materialize a value, use that.
105 if (Materializer) {
Rafael Espindola19b52382015-11-27 20:28:19 +0000106 if (Value *NewV =
107 Materializer->materializeDeclFor(const_cast<Value *>(V))) {
108 VM[V] = NewV;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000109 if (auto *NewGV = dyn_cast<GlobalValue>(NewV))
110 Materializer->materializeInitFor(
111 NewGV, const_cast<GlobalValue *>(cast<GlobalValue>(V)));
Rafael Espindola19b52382015-11-27 20:28:19 +0000112 return NewV;
113 }
James Molloyf6f121e2013-05-28 15:17:05 +0000114 }
115
Dan Gohmanca26f792010-08-26 15:41:53 +0000116 // Global values do not need to be seeded into the VM if they
117 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000118 if (isa<GlobalValue>(V)) {
119 if (Flags & RF_NullMapMissingGlobalValues) {
120 assert(!(Flags & RF_IgnoreMissingEntries) &&
121 "Illegal to specify both RF_NullMapMissingGlobalValues and "
122 "RF_IgnoreMissingEntries");
123 return nullptr;
124 }
Chris Lattner43f8d162011-01-08 08:15:20 +0000125 return VM[V] = const_cast<Value*>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000126 }
127
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000128 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
129 // Inline asm may need *type* remapping.
130 FunctionType *NewTy = IA->getFunctionType();
131 if (TypeMapper) {
132 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
133
134 if (NewTy != IA->getFunctionType())
135 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
136 IA->hasSideEffects(), IA->isAlignStack());
137 }
138
139 return VM[V] = const_cast<Value*>(V);
140 }
Chris Lattner6aa34b02003-10-06 15:23:43 +0000141
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000142 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
143 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +0000144 // If this is a module-level metadata and we know that nothing at the module
145 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000146 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
147 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000148
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000149 auto *MappedMD = mapMetadata(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000150 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
151 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000152
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000153 // FIXME: This assert crashes during bootstrap, but I think it should be
154 // correct. For now, just match behaviour from before the metadata/value
155 // split.
156 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000157 // assert((MappedMD || (Flags & RF_NullMapMissingGlobalValues)) &&
158 // "Referenced metadata value not in value map");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000159 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +0000160 }
161
Chris Lattner43f8d162011-01-08 08:15:20 +0000162 // Okay, this either must be a constant (which may or may not be mappable) or
163 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000164 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000165 if (!C)
166 return nullptr;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000167
Chris Lattner43f8d162011-01-08 08:15:20 +0000168 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000169 Function *F = cast<Function>(mapValue(BA->getFunction()));
170 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(BA->getBasicBlock()));
Chris Lattner43f8d162011-01-08 08:15:20 +0000171 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000172 }
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000173
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000174 // Otherwise, we have some other constant to remap. Start by checking to see
175 // if all operands have an identity remapping.
176 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000177 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000178 for (; OpNo != NumOperands; ++OpNo) {
179 Value *Op = C->getOperand(OpNo);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000180 Mapped = mapValue(Op);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000181 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000182 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000183
184 // See if the type mapper wants to remap the type as well.
185 Type *NewTy = C->getType();
186 if (TypeMapper)
187 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000188
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000189 // If the result type and all operands match up, then just insert an identity
190 // mapping.
191 if (OpNo == NumOperands && NewTy == C->getType())
192 return VM[V] = C;
193
194 // Okay, we need to create a new constant. We've already processed some or
195 // all of the operands, set them all up now.
196 SmallVector<Constant*, 8> Ops;
197 Ops.reserve(NumOperands);
198 for (unsigned j = 0; j != OpNo; ++j)
199 Ops.push_back(cast<Constant>(C->getOperand(j)));
200
201 // If one of the operands mismatch, push it and the other mapped operands.
202 if (OpNo != NumOperands) {
203 Ops.push_back(cast<Constant>(Mapped));
204
205 // Map the rest of the operands that aren't processed yet.
206 for (++OpNo; OpNo != NumOperands; ++OpNo)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000207 Ops.push_back(cast<Constant>(mapValue(C->getOperand(OpNo))));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000208 }
David Blaikie88208842015-08-21 20:16:51 +0000209 Type *NewSrcTy = nullptr;
210 if (TypeMapper)
211 if (auto *GEPO = dyn_cast<GEPOperator>(C))
212 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
213
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000214 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
David Blaikie88208842015-08-21 20:16:51 +0000215 return VM[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000216 if (isa<ConstantArray>(C))
217 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
218 if (isa<ConstantStruct>(C))
219 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
220 if (isa<ConstantVector>(C))
221 return VM[V] = ConstantVector::get(Ops);
222 // If this is a no-operand constant, it must be because the type was remapped.
223 if (isa<UndefValue>(C))
224 return VM[V] = UndefValue::get(NewTy);
225 if (isa<ConstantAggregateZero>(C))
226 return VM[V] = ConstantAggregateZero::get(NewTy);
227 assert(isa<ConstantPointerNull>(C));
228 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000229}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000230
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000231Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000232 VM.MD()[Key].reset(Val);
233 return Val;
234}
235
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000236Metadata *Mapper::mapToSelf(const Metadata *MD) {
237 return mapToMetadata(MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000238}
239
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000240Metadata *Mapper::mapMetadataOp(Metadata *Op) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000241 if (!Op)
242 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000243
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000244 if (Metadata *MappedOp = mapMetadataImpl(Op))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000245 return MappedOp;
246 // Use identity map if MappedOp is null and we can ignore missing entries.
247 if (Flags & RF_IgnoreMissingEntries)
248 return Op;
249
250 // FIXME: This assert crashes during bootstrap, but I think it should be
251 // correct. For now, just match behaviour from before the metadata/value
252 // split.
253 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000254 // assert((Flags & RF_NullMapMissingGlobalValues) &&
255 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000256 return nullptr;
257}
258
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000259/// Resolve uniquing cycles involving the given metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000260static void resolveCycles(Metadata *MD) {
261 if (auto *N = dyn_cast_or_null<MDNode>(MD))
262 if (!N->isResolved())
263 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000264}
265
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000266bool Mapper::remapOperands(MDNode &Node) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000267 assert(!Node.isUniqued() && "Expected temporary or distinct node");
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000268 const bool IsDistinct = Node.isDistinct();
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000269
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000270 bool AnyChanged = false;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000271 for (unsigned I = 0, E = Node.getNumOperands(); I != E; ++I) {
272 Metadata *Old = Node.getOperand(I);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000273 Metadata *New = mapMetadataOp(Old);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000274 if (Old != New) {
275 AnyChanged = true;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000276 Node.replaceOperandWith(I, New);
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000277
278 // Resolve uniquing cycles underneath distinct nodes on the fly so they
279 // don't infect later operands.
280 if (IsDistinct)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000281 resolveCycles(New);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000282 }
283 }
284
285 return AnyChanged;
286}
287
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000288Metadata *Mapper::mapDistinctNode(const MDNode *Node) {
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000289 assert(Node->isDistinct() && "Expected distinct node");
290
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000291 MDNode *NewMD;
292 if (Flags & RF_MoveDistinctMDs)
293 NewMD = const_cast<MDNode *>(Node);
294 else
295 NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000296
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000297 // Remap operands later.
298 DistinctWorklist.push_back(NewMD);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000299 return mapToMetadata(Node, NewMD);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000300}
301
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000302Metadata *Mapper::mapUniquedNode(const MDNode *Node) {
Teresa Johnsonb703c772016-03-29 18:24:19 +0000303 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000304
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000305 // Create a temporary node and map it upfront in case we have a uniquing
306 // cycle. If necessary, this mapping will get updated by RAUW logic before
307 // returning.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000308 auto ClonedMD = Node->clone();
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000309 mapToMetadata(Node, ClonedMD.get());
310 if (!remapOperands(*ClonedMD)) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000311 // No operands changed, so use the original.
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000312 ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node));
Teresa Johnsonb703c772016-03-29 18:24:19 +0000313 return const_cast<MDNode *>(Node);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000314 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000315
Teresa Johnsonb703c772016-03-29 18:24:19 +0000316 // Uniquify the cloned node.
317 return MDNode::replaceWithUniqued(std::move(ClonedMD));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000318}
319
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000320Metadata *Mapper::mapMetadataImpl(const Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000321 // If the value already exists in the map, use it.
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000322 if (Optional<Metadata *> NewMD = VM.getMappedMD(MD))
323 return *NewMD;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000324
325 if (isa<MDString>(MD))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000326 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000327
328 if (isa<ConstantAsMetadata>(MD))
329 if ((Flags & RF_NoModuleLevelChanges))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000330 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000331
332 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000333 Value *MappedV = mapValue(VMD->getValue());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000334 if (VMD->getValue() == MappedV ||
335 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000336 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000337
338 // FIXME: This assert crashes during bootstrap, but I think it should be
339 // correct. For now, just match behaviour from before the metadata/value
340 // split.
341 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000342 // assert((MappedV || (Flags & RF_NullMapMissingGlobalValues)) &&
343 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000344 if (MappedV)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000345 return mapToMetadata(MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000346 return nullptr;
347 }
348
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000349 // Note: this cast precedes the Flags check so we always get its associated
350 // assertion.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000351 const MDNode *Node = cast<MDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000352
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000353 // If this is a module-level metadata and we know that nothing at the
354 // module level is changing, then use an identity mapping.
355 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000356 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000357
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000358 // Require resolved nodes whenever metadata might be remapped.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000359 assert(Node->isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000360
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000361 if (Node->isDistinct())
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000362 return mapDistinctNode(Node);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000363
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000364 return mapUniquedNode(Node);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000365}
366
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000367Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
368 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
369 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000370 return Mapper(VM, Flags, TypeMapper, Materializer).mapMetadata(MD);
371}
372
373Metadata *Mapper::mapMetadata(const Metadata *MD) {
374 Metadata *NewMD = mapMetadataImpl(MD);
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000375
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000376 // When there are no module-level changes, it's possible that the metadata
377 // graph has temporaries. Skip the logic to resolve cycles, since it's
378 // unnecessary (and invalid) in that case.
379 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000380 return NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000381
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000382 // Resolve cycles involving the entry metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000383 resolveCycles(NewMD);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000384
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000385 return NewMD;
386}
387
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000388Mapper::~Mapper() {
389 while (!DistinctWorklist.empty())
390 remapOperands(*DistinctWorklist.pop_back_val());
391}
392
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000393MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
394 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
395 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000396 return cast_or_null<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM,
397 Flags, TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000398}
399
Brian Gaeke6182acf2004-05-19 09:08:12 +0000400/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000401/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000402///
Dan Gohmanca26f792010-08-26 15:41:53 +0000403void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000404 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
405 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000406 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000407 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000408 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000409 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000410 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000411 *op = V;
412 else
413 assert((Flags & RF_IgnoreMissingEntries) &&
414 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000415 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000416
Jay Foad61ea0e42011-06-23 09:09:15 +0000417 // Remap phi nodes' incoming blocks.
418 if (PHINode *PN = dyn_cast<PHINode>(I)) {
419 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
420 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
421 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000422 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000423 PN->setIncomingBlock(i, cast<BasicBlock>(V));
424 else
425 assert((Flags & RF_IgnoreMissingEntries) &&
426 "Referenced block not in value map!");
427 }
428 }
429
Devang Patelc0174042011-08-04 20:02:18 +0000430 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000431 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000432 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000433 for (const auto &MI : MDs) {
434 MDNode *Old = MI.second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000435 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000436 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000437 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000438 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000439
David Blaikie348de692015-04-23 21:36:23 +0000440 if (!TypeMapper)
441 return;
442
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000443 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000444 if (auto CS = CallSite(I)) {
445 SmallVector<Type *, 3> Tys;
446 FunctionType *FTy = CS.getFunctionType();
447 Tys.reserve(FTy->getNumParams());
448 for (Type *Ty : FTy->params())
449 Tys.push_back(TypeMapper->remapType(Ty));
450 CS.mutateFunctionType(FunctionType::get(
451 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000452 return;
453 }
454 if (auto *AI = dyn_cast<AllocaInst>(I))
455 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000456 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000457 GEP->setSourceElementType(
458 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000459 GEP->setResultElementType(
460 TypeMapper->remapType(GEP->getResultElementType()));
461 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000462 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000463}