blob: 8d3fc94d08ac0697a124c74d0506f51798a90d1e [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
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000033/// A GlobalValue whose initializer needs to be materialized.
34struct DelayedGlobalValueInit {
35 GlobalValue *Old;
36 GlobalValue *New;
37 DelayedGlobalValueInit(const GlobalValue *Old, GlobalValue *New)
38 : Old(const_cast<GlobalValue *>(Old)), New(New) {}
39};
40
41/// A basic block used in a BlockAddress whose function body is not yet
42/// materialized.
43struct DelayedBasicBlock {
44 BasicBlock *OldBB;
45 std::unique_ptr<BasicBlock> TempBB;
Duncan P. N. Exon Smitha9978562016-04-03 20:42:21 +000046
47 // Explicit move for MSVC.
48 DelayedBasicBlock(DelayedBasicBlock &&X)
49 : OldBB(std::move(X.OldBB)), TempBB(std::move(X.TempBB)) {}
50 DelayedBasicBlock &operator=(DelayedBasicBlock &&X) {
51 OldBB = std::move(X.OldBB);
52 TempBB = std::move(X.TempBB);
53 return *this;
54 }
55
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000056 DelayedBasicBlock(const BlockAddress &Old)
57 : OldBB(Old.getBasicBlock()),
58 TempBB(BasicBlock::Create(Old.getContext())) {}
59};
60
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000061class Mapper {
62 ValueToValueMapTy &VM;
63 RemapFlags Flags;
64 ValueMapTypeRemapper *TypeMapper;
65 ValueMaterializer *Materializer;
66
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000067 SmallVector<DelayedGlobalValueInit, 8> DelayedInits;
68 SmallVector<DelayedBasicBlock, 1> DelayedBBs;
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000069 SmallVector<MDNode *, 8> DistinctWorklist;
70
71public:
72 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
73 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
74 : VM(VM), Flags(Flags), TypeMapper(TypeMapper),
75 Materializer(Materializer) {}
76
77 ~Mapper();
78
79 Value *mapValue(const Value *V);
80
81 /// Map metadata.
82 ///
83 /// Find the mapping for MD. Guarantees that the return will be resolved
84 /// (not an MDNode, or MDNode::isResolved() returns true).
85 Metadata *mapMetadata(const Metadata *MD);
86
87private:
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000088 Value *mapBlockAddress(const BlockAddress &BA);
89
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000090 /// Map metadata helper.
91 ///
92 /// Co-recursively finds the mapping for MD. If this returns an MDNode, it's
93 /// possible that MDNode::isResolved() will return false.
94 Metadata *mapMetadataImpl(const Metadata *MD);
95 Metadata *mapMetadataOp(Metadata *Op);
96
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +000097 /// Map metadata that doesn't require visiting operands.
98 Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
99
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000100 /// Remap the operands of an MDNode.
101 ///
102 /// If \c Node is temporary, uniquing cycles are ignored. If \c Node is
103 /// distinct, uniquing cycles are resolved as they're found.
104 ///
105 /// \pre \c Node.isDistinct() or \c Node.isTemporary().
106 bool remapOperands(MDNode &Node);
107
108 /// Map a distinct MDNode.
109 ///
110 /// Whether distinct nodes change is independent of their operands. If \a
111 /// RF_MoveDistinctMDs, then they are reused, and their operands remapped in
112 /// place; effectively, they're moved from one graph to another. Otherwise,
113 /// they're cloned/duplicated, and the new copy's operands are remapped.
114 Metadata *mapDistinctNode(const MDNode *Node);
115
116 /// Map a uniqued MDNode.
117 ///
118 /// Uniqued nodes may not need to be recreated (they may map to themselves).
119 Metadata *mapUniquedNode(const MDNode *Node);
120
121 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
122 Metadata *mapToSelf(const Metadata *MD);
123};
124
125} // end namespace
126
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000127Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloyf6f121e2013-05-28 15:17:05 +0000128 ValueMapTypeRemapper *TypeMapper,
129 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000130 return Mapper(VM, Flags, TypeMapper, Materializer).mapValue(V);
131}
132
133Value *Mapper::mapValue(const Value *V) {
Chris Lattner43f8d162011-01-08 08:15:20 +0000134 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner1bfc7ab2007-02-03 00:08:31 +0000135
Chris Lattner43f8d162011-01-08 08:15:20 +0000136 // If the value already exists in the map, use it.
137 if (I != VM.end() && I->second) return I->second;
138
James Molloyf6f121e2013-05-28 15:17:05 +0000139 // If we have a materializer and it can materialize a value, use that.
140 if (Materializer) {
Rafael Espindola19b52382015-11-27 20:28:19 +0000141 if (Value *NewV =
142 Materializer->materializeDeclFor(const_cast<Value *>(V))) {
143 VM[V] = NewV;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000144 if (auto *NewGV = dyn_cast<GlobalValue>(NewV))
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000145 DelayedInits.push_back(
146 DelayedGlobalValueInit(cast<GlobalValue>(V), NewGV));
Rafael Espindola19b52382015-11-27 20:28:19 +0000147 return NewV;
148 }
James Molloyf6f121e2013-05-28 15:17:05 +0000149 }
150
Dan Gohmanca26f792010-08-26 15:41:53 +0000151 // Global values do not need to be seeded into the VM if they
152 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000153 if (isa<GlobalValue>(V)) {
154 if (Flags & RF_NullMapMissingGlobalValues) {
155 assert(!(Flags & RF_IgnoreMissingEntries) &&
156 "Illegal to specify both RF_NullMapMissingGlobalValues and "
157 "RF_IgnoreMissingEntries");
158 return nullptr;
159 }
Chris Lattner43f8d162011-01-08 08:15:20 +0000160 return VM[V] = const_cast<Value*>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000161 }
162
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000163 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
164 // Inline asm may need *type* remapping.
165 FunctionType *NewTy = IA->getFunctionType();
166 if (TypeMapper) {
167 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
168
169 if (NewTy != IA->getFunctionType())
170 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
171 IA->hasSideEffects(), IA->isAlignStack());
172 }
173
174 return VM[V] = const_cast<Value*>(V);
175 }
Chris Lattner6aa34b02003-10-06 15:23:43 +0000176
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000177 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
178 const Metadata *MD = MDV->getMetadata();
Chris Lattner43f8d162011-01-08 08:15:20 +0000179 // If this is a module-level metadata and we know that nothing at the module
180 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000181 if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
182 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000183
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000184 auto *MappedMD = mapMetadata(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000185 if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
186 return VM[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000187
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000188 return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +0000189 }
190
Chris Lattner43f8d162011-01-08 08:15:20 +0000191 // Okay, this either must be a constant (which may or may not be mappable) or
192 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000193 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000194 if (!C)
195 return nullptr;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000196
197 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
198 return mapBlockAddress(*BA);
199
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000200 // Otherwise, we have some other constant to remap. Start by checking to see
201 // if all operands have an identity remapping.
202 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000203 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000204 for (; OpNo != NumOperands; ++OpNo) {
205 Value *Op = C->getOperand(OpNo);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000206 Mapped = mapValue(Op);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000207 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000208 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000209
210 // See if the type mapper wants to remap the type as well.
211 Type *NewTy = C->getType();
212 if (TypeMapper)
213 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000214
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000215 // If the result type and all operands match up, then just insert an identity
216 // mapping.
217 if (OpNo == NumOperands && NewTy == C->getType())
218 return VM[V] = C;
219
220 // Okay, we need to create a new constant. We've already processed some or
221 // all of the operands, set them all up now.
222 SmallVector<Constant*, 8> Ops;
223 Ops.reserve(NumOperands);
224 for (unsigned j = 0; j != OpNo; ++j)
225 Ops.push_back(cast<Constant>(C->getOperand(j)));
226
227 // If one of the operands mismatch, push it and the other mapped operands.
228 if (OpNo != NumOperands) {
229 Ops.push_back(cast<Constant>(Mapped));
230
231 // Map the rest of the operands that aren't processed yet.
232 for (++OpNo; OpNo != NumOperands; ++OpNo)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000233 Ops.push_back(cast<Constant>(mapValue(C->getOperand(OpNo))));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000234 }
David Blaikie88208842015-08-21 20:16:51 +0000235 Type *NewSrcTy = nullptr;
236 if (TypeMapper)
237 if (auto *GEPO = dyn_cast<GEPOperator>(C))
238 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
239
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000240 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
David Blaikie88208842015-08-21 20:16:51 +0000241 return VM[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000242 if (isa<ConstantArray>(C))
243 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
244 if (isa<ConstantStruct>(C))
245 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
246 if (isa<ConstantVector>(C))
247 return VM[V] = ConstantVector::get(Ops);
248 // If this is a no-operand constant, it must be because the type was remapped.
249 if (isa<UndefValue>(C))
250 return VM[V] = UndefValue::get(NewTy);
251 if (isa<ConstantAggregateZero>(C))
252 return VM[V] = ConstantAggregateZero::get(NewTy);
253 assert(isa<ConstantPointerNull>(C));
254 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000255}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000256
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000257Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
258 Function *F = cast<Function>(mapValue(BA.getFunction()));
259
260 // F may not have materialized its initializer. In that case, create a
261 // dummy basic block for now, and replace it once we've materialized all
262 // the initializers.
263 BasicBlock *BB;
264 if (F->isDeclaration()) {
265 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
266 } else {
267 DelayedBBs.push_back(DelayedBasicBlock(BA));
268 BB = DelayedBBs.back().TempBB.get();
269 }
270
271 return VM[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
272}
273
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000274Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000275 VM.MD()[Key].reset(Val);
276 return Val;
277}
278
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000279Metadata *Mapper::mapToSelf(const Metadata *MD) {
280 return mapToMetadata(MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000281}
282
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000283Metadata *Mapper::mapMetadataOp(Metadata *Op) {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000284 if (!Op)
285 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000286
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000287 if (Metadata *MappedOp = mapMetadataImpl(Op))
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000288 return MappedOp;
289 // Use identity map if MappedOp is null and we can ignore missing entries.
290 if (Flags & RF_IgnoreMissingEntries)
291 return Op;
292
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000293 return nullptr;
294}
295
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000296/// Resolve uniquing cycles involving the given metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000297static void resolveCycles(Metadata *MD) {
298 if (auto *N = dyn_cast_or_null<MDNode>(MD))
299 if (!N->isResolved())
300 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000301}
302
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000303bool Mapper::remapOperands(MDNode &Node) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000304 assert(!Node.isUniqued() && "Expected temporary or distinct node");
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000305 const bool IsDistinct = Node.isDistinct();
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000306
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000307 bool AnyChanged = false;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000308 for (unsigned I = 0, E = Node.getNumOperands(); I != E; ++I) {
309 Metadata *Old = Node.getOperand(I);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000310 Metadata *New = mapMetadataOp(Old);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000311 if (Old != New) {
312 AnyChanged = true;
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000313 Node.replaceOperandWith(I, New);
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000314
315 // Resolve uniquing cycles underneath distinct nodes on the fly so they
316 // don't infect later operands.
317 if (IsDistinct)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000318 resolveCycles(New);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000319 }
320 }
321
322 return AnyChanged;
323}
324
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000325Metadata *Mapper::mapDistinctNode(const MDNode *Node) {
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000326 assert(Node->isDistinct() && "Expected distinct node");
327
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000328 MDNode *NewMD;
329 if (Flags & RF_MoveDistinctMDs)
330 NewMD = const_cast<MDNode *>(Node);
331 else
332 NewMD = MDNode::replaceWithDistinct(Node->clone());
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000333
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000334 // Remap operands later.
335 DistinctWorklist.push_back(NewMD);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000336 return mapToMetadata(Node, NewMD);
Duncan P. N. Exon Smithfb9d1282015-01-14 01:08:47 +0000337}
338
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000339Metadata *Mapper::mapUniquedNode(const MDNode *Node) {
Teresa Johnsonb703c772016-03-29 18:24:19 +0000340 assert(Node->isUniqued() && "Expected uniqued node");
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000341
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000342 // Create a temporary node and map it upfront in case we have a uniquing
343 // cycle. If necessary, this mapping will get updated by RAUW logic before
344 // returning.
Duncan P. N. Exon Smith03e05832015-01-20 02:56:57 +0000345 auto ClonedMD = Node->clone();
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000346 mapToMetadata(Node, ClonedMD.get());
347 if (!remapOperands(*ClonedMD)) {
Duncan P. N. Exon Smith27050972015-08-05 23:22:34 +0000348 // No operands changed, so use the original.
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000349 ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node));
Teresa Johnsonb703c772016-03-29 18:24:19 +0000350 return const_cast<MDNode *>(Node);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000351 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000352
Teresa Johnsonb703c772016-03-29 18:24:19 +0000353 // Uniquify the cloned node.
354 return MDNode::replaceWithUniqued(std::move(ClonedMD));
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000355}
356
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000357Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000358 // If the value already exists in the map, use it.
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000359 if (Optional<Metadata *> NewMD = VM.getMappedMD(MD))
360 return *NewMD;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000361
362 if (isa<MDString>(MD))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000363 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000364
365 if (isa<ConstantAsMetadata>(MD))
366 if ((Flags & RF_NoModuleLevelChanges))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000367 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000368
369 if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000370 // Disallow recursion into metadata mapping through mapValue.
371 VM.disableMapMetadata();
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000372 Value *MappedV = mapValue(VMD->getValue());
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000373 VM.enableMapMetadata();
374
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000375 if (VMD->getValue() == MappedV ||
376 (!MappedV && (Flags & RF_IgnoreMissingEntries)))
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000377 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000378
Duncan P. N. Exon Smith8e65f8d2016-04-04 04:59:56 +0000379 return mapToMetadata(MD, MappedV ? ValueAsMetadata::get(MappedV) : nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000380 }
381
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000382 assert(isa<MDNode>(MD) && "Expected a metadata node");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000383
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000384 // If this is a module-level metadata and we know that nothing at the
385 // module level is changing, then use an identity mapping.
386 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000387 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000388
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000389 return None;
390}
391
392Metadata *Mapper::mapMetadataImpl(const Metadata *MD) {
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000393 assert(VM.mayMapMetadata() && "Unexpected co-recursion through mapValue");
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000394 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
395 return *NewMD;
396
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000397 // Require resolved nodes whenever metadata might be remapped.
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000398 auto *Node = cast<MDNode>(MD);
Teresa Johnsonb703c772016-03-29 18:24:19 +0000399 assert(Node->isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith170c26d2015-03-17 01:14:40 +0000400
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000401 if (Node->isDistinct())
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000402 return mapDistinctNode(Node);
Duncan P. N. Exon Smith953e1a42015-01-08 22:42:30 +0000403
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000404 return mapUniquedNode(Node);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000405}
406
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000407Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
408 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
409 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000410 return Mapper(VM, Flags, TypeMapper, Materializer).mapMetadata(MD);
411}
412
413Metadata *Mapper::mapMetadata(const Metadata *MD) {
414 Metadata *NewMD = mapMetadataImpl(MD);
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000415
Duncan P. N. Exon Smith3115f752015-08-05 23:52:42 +0000416 // When there are no module-level changes, it's possible that the metadata
417 // graph has temporaries. Skip the logic to resolve cycles, since it's
418 // unnecessary (and invalid) in that case.
419 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000420 return NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000421
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000422 // Resolve cycles involving the entry metadata.
Teresa Johnsonb703c772016-03-29 18:24:19 +0000423 resolveCycles(NewMD);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000424
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000425 return NewMD;
426}
427
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000428Mapper::~Mapper() {
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000429 // Remap the operands of distinct MDNodes.
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000430 while (!DistinctWorklist.empty())
431 remapOperands(*DistinctWorklist.pop_back_val());
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000432
433 // Materialize global initializers.
434 while (!DelayedInits.empty()) {
435 auto Init = DelayedInits.pop_back_val();
436 Materializer->materializeInitFor(Init.New, Init.Old);
437 }
438
439 // Process block addresses delayed until global inits.
440 while (!DelayedBBs.empty()) {
441 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
442 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
443 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
444 }
445
446 // We don't expect any of these to grow after clearing.
447 assert(DistinctWorklist.empty());
448 assert(DelayedInits.empty());
449 assert(DelayedBBs.empty());
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000450}
451
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000452MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
453 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
454 ValueMaterializer *Materializer) {
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000455 return cast_or_null<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM,
456 Flags, TypeMapper, Materializer));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000457}
458
Brian Gaeke6182acf2004-05-19 09:08:12 +0000459/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patelb8f11de2010-06-23 23:55:51 +0000460/// current values into those specified by VMap.
Brian Gaeke6182acf2004-05-19 09:08:12 +0000461///
Dan Gohmanca26f792010-08-26 15:41:53 +0000462void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloyf6f121e2013-05-28 15:17:05 +0000463 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
464 ValueMaterializer *Materializer){
Dan Gohmanca26f792010-08-26 15:41:53 +0000465 // Remap operands.
Gabor Greif5df43262008-05-30 21:24:22 +0000466 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloyf6f121e2013-05-28 15:17:05 +0000467 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattner43f8d162011-01-08 08:15:20 +0000468 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000469 if (V)
Chris Lattner43f8d162011-01-08 08:15:20 +0000470 *op = V;
471 else
472 assert((Flags & RF_IgnoreMissingEntries) &&
473 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000474 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000475
Jay Foad61ea0e42011-06-23 09:09:15 +0000476 // Remap phi nodes' incoming blocks.
477 if (PHINode *PN = dyn_cast<PHINode>(I)) {
478 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
479 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
480 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000481 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000482 PN->setIncomingBlock(i, cast<BasicBlock>(V));
483 else
484 assert((Flags & RF_IgnoreMissingEntries) &&
485 "Referenced block not in value map!");
486 }
487 }
488
Devang Patelc0174042011-08-04 20:02:18 +0000489 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000490 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000491 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000492 for (const auto &MI : MDs) {
493 MDNode *Old = MI.second;
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000494 MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohmanca26f792010-08-26 15:41:53 +0000495 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000496 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000497 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000498
David Blaikie348de692015-04-23 21:36:23 +0000499 if (!TypeMapper)
500 return;
501
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000502 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000503 if (auto CS = CallSite(I)) {
504 SmallVector<Type *, 3> Tys;
505 FunctionType *FTy = CS.getFunctionType();
506 Tys.reserve(FTy->getNumParams());
507 for (Type *Ty : FTy->params())
508 Tys.push_back(TypeMapper->remapType(Ty));
509 CS.mutateFunctionType(FunctionType::get(
510 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000511 return;
512 }
513 if (auto *AI = dyn_cast<AllocaInst>(I))
514 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000515 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000516 GEP->setSourceElementType(
517 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000518 GEP->setResultElementType(
519 TypeMapper->remapType(GEP->getResultElementType()));
520 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000521 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000522}