blob: c8d13385b1d491456851ec2e9e94c3b8232c0611 [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() {}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000028
29Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +000030 ValueMapTypeRemapper *TypeMapper,
31 ValueMaterializer *Materializer) {
Chris Lattner43f8d162011-01-08 08:15:20 +000032 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +000033
Chris Lattner43f8d162011-01-08 08:15:20 +000034 // If the value already exists in the map, use it.
35 if (I != VM.end() && I->second) return I->second;
36
James Molloyf6f121e2013-05-28 15:17:05 +000037 // If we have a materializer and it can materialize a value, use that.
38 if (Materializer) {
39 if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
40 return VM[V] = NewV;
41 }
42
Dan Gohmanca26f792010-08-26 15:41:53 +000043 // Global values do not need to be seeded into the VM if they
44 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +000045 if (isa<GlobalValue>(V)) {
46 if (Flags & RF_NullMapMissingGlobalValues) {
47 assert(!(Flags & RF_IgnoreMissingEntries) &&
48 "Illegal to specify both RF_NullMapMissingGlobalValues and "
49 "RF_IgnoreMissingEntries");
50 return nullptr;
51 }
Chris Lattner43f8d162011-01-08 08:15:20 +000052 return VM[V] = const_cast<Value*>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +000053 }
54
Chris Lattner8b4cf5e2011-07-15 23:18:40 +000055 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
56 // Inline asm may need *type* remapping.
57 FunctionType *NewTy = IA->getFunctionType();
58 if (TypeMapper) {
59 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
60
61 if (NewTy != IA->getFunctionType())
62 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
63 IA->hasSideEffects(), IA->isAlignStack());
64 }
65
66 return VM[V] = const_cast<Value*>(V);
67 }
Chris Lattner6aa34b02003-10-06 15:23:43 +000068
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000069 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
70 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +000071 // If this is a module-level metadata and we know that nothing at the module
72 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000073 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
74 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000075
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +000076 auto *MappedMD = MapMetadata(MD, VM, Flags, TypeMapper, Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000077 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
78 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +000079
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000080 // FIXME: This assert crashes during bootstrap, but I think it should be
81 // correct. For now, just match behaviour from before the metadata/value
82 // split.
83 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +000084 // assert((MappedMD || (Flags & RF_NullMapMissingGlobalValues)) &&
85 // "Referenced metadata value not in value map");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000086 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +000087 }
88
Chris Lattner43f8d162011-01-08 08:15:20 +000089 // Okay, this either must be a constant (which may or may not be mappable) or
90 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +000091 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +000092 if (!C)
93 return nullptr;
Chris Lattnercf5a47d2009-10-29 00:28:30 +000094
Chris Lattner43f8d162011-01-08 08:15:20 +000095 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000096 Function *F =
James Molloyf6f121e2013-05-28 15:17:05 +000097 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +000098 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
James Molloyf6f121e2013-05-28 15:17:05 +000099 Flags, TypeMapper, Materializer));
Chris Lattner43f8d162011-01-08 08:15:20 +0000100 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000101 }
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000102
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000103 // Otherwise, we have some other constant to remap. Start by checking to see
104 // if all operands have an identity remapping.
105 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000106 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000107 for (; OpNo != NumOperands; ++OpNo) {
108 Value *Op = C->getOperand(OpNo);
James Molloyf6f121e2013-05-28 15:17:05 +0000109 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000110 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000111 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000112
113 // See if the type mapper wants to remap the type as well.
114 Type *NewTy = C->getType();
115 if (TypeMapper)
116 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000117
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000118 // If the result type and all operands match up, then just insert an identity
119 // mapping.
120 if (OpNo == NumOperands && NewTy == C->getType())
121 return VM[V] = C;
122
123 // Okay, we need to create a new constant. We've already processed some or
124 // all of the operands, set them all up now.
125 SmallVector<Constant*, 8> Ops;
126 Ops.reserve(NumOperands);
127 for (unsigned j = 0; j != OpNo; ++j)
128 Ops.push_back(cast<Constant>(C->getOperand(j)));
129
130 // If one of the operands mismatch, push it and the other mapped operands.
131 if (OpNo != NumOperands) {
132 Ops.push_back(cast<Constant>(Mapped));
133
134 // Map the rest of the operands that aren't processed yet.
135 for (++OpNo; OpNo != NumOperands; ++OpNo)
136 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
James Molloyf6f121e2013-05-28 15:17:05 +0000137 Flags, TypeMapper, Materializer));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000138 }
David Blaikie88208842015-08-21 20:16:51 +0000139 Type *NewSrcTy = nullptr;
140 if (TypeMapper)
141 if (auto *GEPO = dyn_cast<GEPOperator>(C))
142 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
143
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000144 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
David Blaikie88208842015-08-21 20:16:51 +0000145 return VM[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146 if (isa<ConstantArray>(C))
147 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
148 if (isa<ConstantStruct>(C))
149 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
150 if (isa<ConstantVector>(C))
151 return VM[V] = ConstantVector::get(Ops);
152 // If this is a no-operand constant, it must be because the type was remapped.
153 if (isa<UndefValue>(C))
154 return VM[V] = UndefValue::get(NewTy);
155 if (isa<ConstantAggregateZero>(C))
156 return VM[V] = ConstantAggregateZero::get(NewTy);
157 assert(isa<ConstantPointerNull>(C));
158 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000159}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000160
Kaelyn Takata22324f32014-12-09 23:32:46 +0000161static Metadata *mapToMetadata(ValueToValueMapTy &VM, const Metadata *Key,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000162 Metadata *Val) {
163 VM.MD()[Key].reset(Val);
164 return Val;
165}
166
167static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
Kaelyn Takata22324f32014-12-09 23:32:46 +0000168 return mapToMetadata(VM, MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000169}
170
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000171static Metadata *MapMetadataImpl(const Metadata *MD,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000172 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000173 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000174 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000175 ValueMaterializer *Materializer);
176
Duncan P. N. Exon Smith5ed90c02015-08-04 13:23:30 +0000177static Metadata *mapMetadataOp(Metadata *Op,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000178 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000179 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith422e5c72015-01-19 22:16:01 +0000180 ValueMapTypeRemapper *TypeMapper,
181 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000182 if (!Op)
183 return nullptr;
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000184 if (Metadata *MappedOp = MapMetadataImpl(Op, DistinctWorklist, VM, Flags,
185 TypeMapper, Materializer))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000186 return MappedOp;
187 // Use identity map if MappedOp is null and we can ignore missing entries.
188 if (Flags & RF_IgnoreMissingEntries)
189 return Op;
190
191 // FIXME: This assert crashes during bootstrap, but I think it should be
192 // correct. For now, just match behaviour from before the metadata/value
193 // split.
194 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000195 // assert((Flags & RF_NullMapMissingGlobalValues) &&
196 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000197 return nullptr;
198}
199
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000200/// Resolve uniquing cycles involving the given metadata.
201static void resolveCycles(Metadata *MD) {
202 if (auto *N = dyn_cast_or_null<MDNode>(MD))
203 if (!N->isResolved())
204 N->resolveCycles();
205}
206
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000207/// Remap the operands of an MDNode.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000208///
209/// If \c Node is temporary, uniquing cycles are ignored. If \c Node is
210/// distinct, uniquing cycles are resolved as they're found.
211///
212/// \pre \c Node.isDistinct() or \c Node.isTemporary().
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000213static bool remapOperands(MDNode &Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000214 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000215 ValueToValueMapTy &VM, RemapFlags Flags,
216 ValueMapTypeRemapper *TypeMapper,
217 ValueMaterializer *Materializer) {
218 assert(!Node.isUniqued() && "Expected temporary or distinct node");
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000219 const bool IsDistinct = Node.isDistinct();
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000220
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000221 bool AnyChanged = false;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000222 for (unsigned I = 0, E = Node.getNumOperands(); I != E; ++I) {
223 Metadata *Old = Node.getOperand(I);
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000224 Metadata *New = mapMetadataOp(Old, DistinctWorklist, VM, Flags, TypeMapper,
225 Materializer);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000226 if (Old != New) {
227 AnyChanged = true;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000228 Node.replaceOperandWith(I, New);
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000229
230 // Resolve uniquing cycles underneath distinct nodes on the fly so they
231 // don't infect later operands.
232 if (IsDistinct)
233 resolveCycles(New);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000234 }
235 }
236
237 return AnyChanged;
238}
239
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000240/// Map a distinct MDNode.
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000241///
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000242/// Whether distinct nodes change is independent of their operands. If \a
243/// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
244/// place; effectively, they're moved from one graph to another. Otherwise,
245/// they're cloned/duplicated, and the new copy's operands are remapped.
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000246static Metadata *mapDistinctNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000247 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000248 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000249 ValueMapTypeRemapper *TypeMapper,
250 ValueMaterializer *Materializer) {
251 assert(Node->isDistinct() && "Expected distinct node");
252
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000253 MDNode *NewMD;
254 if (Flags & RF_MoveDistinctMDs)
255 NewMD = const_cast<MDNode *>(Node);
256 else
257 NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000258
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000259 // Remap operands later.
260 DistinctWorklist.push_back(NewMD);
261 return mapToMetadata(VM, Node, NewMD);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000262}
263
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000264/// \brief Map a uniqued MDNode.
265///
266/// Uniqued nodes may not need to be recreated (they may map to themselves).
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000267static Metadata *mapUniquedNode(const MDNode *Node,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000268 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000269 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smithc862be82015-01-19 22:40:25 +0000270 ValueMapTypeRemapper *TypeMapper,
271 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000272 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000273
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000274 // Create a temporary node and map it upfront in case we have a uniquing
275 // cycle. If necessary, this mapping will get updated by RAUW logic before
276 // returning.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000277 auto ClonedMD = Node->clone();
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000278 mapToMetadata(VM, Node, ClonedMD.get());
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000279 if (!remapOperands(*ClonedMD, DistinctWorklist, VM, Flags, TypeMapper,
280 Materializer)) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000281 // No operands changed, so use the original.
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000282 ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node));
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000283 return const_cast<MDNode *>(Node);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000284 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000285
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000286 // Uniquify the cloned node.
287 return MDNode::replaceWithUniqued(std::move(ClonedMD));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000288}
289
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000290static Metadata *MapMetadataImpl(const Metadata *MD,
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000291 SmallVectorImpl<MDNode *> &DistinctWorklist,
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000292 ValueToValueMapTy &VM, RemapFlags Flags,
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000293 ValueMapTypeRemapper *TypeMapper,
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000294 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000295 // If the value already exists in the map, use it.
296 if (Metadata *NewMD = VM.MD().lookup(MD).get())
297 return NewMD;
298
299 if (isa<MDString>(MD))
300 return mapToSelf(VM, MD);
301
302 if (isa<ConstantAsMetadata>(MD))
303 if ((Flags & RF_NoModuleLevelChanges))
304 return mapToSelf(VM, MD);
305
306 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
307 Value *MappedV =
308 MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
309 if (VMD->getValue() == MappedV ||
310 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
311 return mapToSelf(VM, MD);
312
313 // FIXME: This assert crashes during bootstrap, but I think it should be
314 // correct. For now, just match behaviour from before the metadata/value
315 // split.
316 //
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000317 // assert((MappedV || (Flags & RF_NullMapMissingGlobalValues)) &&
318 // "Referenced metadata not in value map!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000319 if (MappedV)
Kaelyn Takata22324f32014-12-09 23:32:46 +0000320 return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000321 return nullptr;
322 }
323
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000324 // Note: this cast precedes the Flags check so we always get its associated
325 // assertion.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +0000326 const MDNode *Node = cast<MDNode>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000327
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000328 // If this is a module-level metadata and we know that nothing at the
329 // module level is changing, then use an identity mapping.
330 if (Flags & RF_NoModuleLevelChanges)
331 return mapToSelf(VM, MD);
332
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000333 // Require resolved nodes whenever metadata might be remapped.
334 assert(Node->isResolved() && "Unexpected unresolved node");
335
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000336 if (Node->isDistinct())
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000337 return mapDistinctNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
338 Materializer);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000339
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000340 return mapUniquedNode(Node, DistinctWorklist, VM, Flags, TypeMapper,
341 Materializer);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000342}
343
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000344Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
345 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
346 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000347 SmallVector<MDNode *, 8> DistinctWorklist;
348 Metadata *NewMD = MapMetadataImpl(MD, DistinctWorklist, VM, Flags, TypeMapper,
349 Materializer);
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000350
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000351 // When there are no module-level changes, it's possible that the metadata
352 // graph has temporaries. Skip the logic to resolve cycles, since it's
353 // unnecessary (and invalid) in that case.
354 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000355 return NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000356
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000357 // Resolve cycles involving the entry metadata.
358 resolveCycles(NewMD);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000359
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000360 // Remap the operands of distinct MDNodes.
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000361 while (!DistinctWorklist.empty())
362 remapOperands(*DistinctWorklist.pop_back_val(), DistinctWorklist, VM, Flags,
363 TypeMapper, Materializer);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000364
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000365 return NewMD;
366}
367
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000368MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
369 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
370 ValueMaterializer *Materializer) {
371 return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags,
372 TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000373}
374
Brian Gaeke6182acf2004-05-19 09:08:12 +0000375/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000376/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000377///
Dan Gohmanca26f792010-08-26 15:41:53 +0000378void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000379 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
380 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000381 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000382 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000383 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000384 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000385 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000386 *op = V;
387 else
388 assert((Flags & RF_IgnoreMissingEntries) &&
389 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000390 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000391
Jay Foad61ea0e42011-06-23 09:09:15 +0000392 // Remap phi nodes' incoming blocks.
393 if (PHINode *PN = dyn_cast<PHINode>(I)) {
394 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
395 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
396 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000397 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000398 PN->setIncomingBlock(i, cast<BasicBlock>(V));
399 else
400 assert((Flags & RF_IgnoreMissingEntries) &&
401 "Referenced block not in value map!");
402 }
403 }
404
Devang Patelc0174042011-08-04 20:02:18 +0000405 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000406 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000407 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000408 for (const auto &MI : MDs) {
409 MDNode *Old = MI.second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000410 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000411 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000412 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000413 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000414
David Blaikie348de692015-04-23 21:36:23 +0000415 if (!TypeMapper)
416 return;
417
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000418 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000419 if (auto CS = CallSite(I)) {
420 SmallVector<Type *, 3> Tys;
421 FunctionType *FTy = CS.getFunctionType();
422 Tys.reserve(FTy->getNumParams());
423 for (Type *Ty : FTy->params())
424 Tys.push_back(TypeMapper->remapType(Ty));
425 CS.mutateFunctionType(FunctionType::get(
426 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000427 return;
428 }
429 if (auto *AI = dyn_cast<AllocaInst>(I))
430 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000431 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000432 GEP->setSourceElementType(
433 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000434 GEP->setResultElementType(
435 TypeMapper->remapType(GEP->getResultElementType()));
436 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000437 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000438}