1//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the MapValue function, which is shared by various parts of
10// the lib/Transforms/Utils library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/ValueMapper.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/IR/Argument.h"
21#include "llvm/IR/BasicBlock.h"
22#include "llvm/IR/Constant.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/DebugInfoMetadata.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/GlobalAlias.h"
28#include "llvm/IR/GlobalIFunc.h"
29#include "llvm/IR/GlobalObject.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/InlineAsm.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Metadata.h"
36#include "llvm/IR/Operator.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/Value.h"
39#include "llvm/Support/Casting.h"
40#include "llvm/Support/Debug.h"
41#include <cassert>
42#include <limits>
43#include <memory>
44#include <utility>
45
46using namespace llvm;
47
48#define DEBUG_TYPE "value-mapper"
49
50// Out of line method to get vtable etc for class.
51void ValueMapTypeRemapper::anchor() {}
52void ValueMaterializer::anchor() {}
53
54namespace {
55
56/// A basic block used in a BlockAddress whose function body is not yet
57/// materialized.
58struct DelayedBasicBlock {
59 BasicBlock *OldBB;
60 std::unique_ptr<BasicBlock> TempBB;
61
62 DelayedBasicBlock(const BlockAddress &Old)
63 : OldBB(Old.getBasicBlock()),
64 TempBB(BasicBlock::Create(Context&: Old.getContext())) {}
65};
66
67struct WorklistEntry {
68 enum EntryKind {
69 MapGlobalInit,
70 MapAppendingVar,
71 MapAliasOrIFunc,
72 RemapFunction
73 };
74 struct GVInitTy {
75 GlobalVariable *GV;
76 Constant *Init;
77 };
78 struct AppendingGVTy {
79 GlobalVariable *GV;
80 GlobalVariable *OldGV;
81 };
82 struct AliasOrIFuncTy {
83 GlobalValue *GV;
84 Constant *Target;
85 };
86
87 unsigned Kind : 2;
88 unsigned MCID : 29;
89 unsigned AppendingGVIsOldCtorDtor : 1;
90 unsigned AppendingGVNumNewMembers;
91 union {
92 GVInitTy GVInit;
93 AppendingGVTy AppendingGV;
94 AliasOrIFuncTy AliasOrIFunc;
95 Function *RemapF;
96 } Data;
97};
98
99struct MappingContext {
100 ValueToValueMapTy *VM;
101 ValueMaterializer *Materializer = nullptr;
102
103 /// Construct a MappingContext with a value map and materializer.
104 explicit MappingContext(ValueToValueMapTy &VM,
105 ValueMaterializer *Materializer = nullptr)
106 : VM(&VM), Materializer(Materializer) {}
107};
108
109class Mapper {
110 friend class MDNodeMapper;
111
112#ifndef NDEBUG
113 DenseSet<GlobalValue *> AlreadyScheduled;
114#endif
115
116 RemapFlags Flags;
117 ValueMapTypeRemapper *TypeMapper;
118 unsigned CurrentMCID = 0;
119 SmallVector<MappingContext, 2> MCs;
120 SmallVector<WorklistEntry, 4> Worklist;
121 SmallVector<DelayedBasicBlock, 1> DelayedBBs;
122 SmallVector<Constant *, 16> AppendingInits;
123 const MetadataPredicate *IdentityMD;
124
125public:
126 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
127 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer,
128 const MetadataPredicate *IdentityMD)
129 : Flags(Flags), TypeMapper(TypeMapper),
130 MCs(1, MappingContext(VM, Materializer)), IdentityMD(IdentityMD) {}
131
132 /// ValueMapper should explicitly call \a flush() before destruction.
133 ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
134
135 bool hasWorkToDo() const { return !Worklist.empty(); }
136
137 unsigned
138 registerAlternateMappingContext(ValueToValueMapTy &VM,
139 ValueMaterializer *Materializer = nullptr) {
140 MCs.push_back(Elt: MappingContext(VM, Materializer));
141 return MCs.size() - 1;
142 }
143
144 void addFlags(RemapFlags Flags);
145
146 void remapGlobalObjectMetadata(GlobalObject &GO);
147
148 Value *mapValue(const Value *V);
149 void remapInstruction(Instruction *I);
150 void remapFunction(Function &F);
151 void remapDbgRecord(DbgRecord &DVR);
152
153 Constant *mapConstant(const Constant *C) {
154 return cast_or_null<Constant>(Val: mapValue(V: C));
155 }
156
157 /// Map metadata.
158 ///
159 /// Find the mapping for MD. Guarantees that the return will be resolved
160 /// (not an MDNode, or MDNode::isResolved() returns true).
161 Metadata *mapMetadata(const Metadata *MD);
162
163 void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
164 unsigned MCID);
165 void scheduleMapAppendingVariable(GlobalVariable &GV, GlobalVariable *OldGV,
166 bool IsOldCtorDtor,
167 ArrayRef<Constant *> NewMembers,
168 unsigned MCID);
169 void scheduleMapAliasOrIFunc(GlobalValue &GV, Constant &Target,
170 unsigned MCID);
171 void scheduleRemapFunction(Function &F, unsigned MCID);
172
173 void flush();
174
175private:
176 void mapAppendingVariable(GlobalVariable &GV, GlobalVariable *OldGV,
177 bool IsOldCtorDtor,
178 ArrayRef<Constant *> NewMembers);
179
180 ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
181 ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
182
183 Value *mapBlockAddress(const BlockAddress &BA);
184
185 /// Map metadata that doesn't require visiting operands.
186 std::optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
187
188 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
189 Metadata *mapToSelf(const Metadata *MD);
190};
191
192class MDNodeMapper {
193 Mapper &M;
194
195 /// Data about a node in \a UniquedGraph.
196 struct Data {
197 bool HasChanged = false;
198 unsigned ID = std::numeric_limits<unsigned>::max();
199 TempMDNode Placeholder;
200 };
201
202 /// A graph of uniqued nodes.
203 struct UniquedGraph {
204 SmallDenseMap<const Metadata *, Data, 32> Info; // Node properties.
205 SmallVector<MDNode *, 16> POT; // Post-order traversal.
206
207 /// Propagate changed operands through the post-order traversal.
208 ///
209 /// Iteratively update \a Data::HasChanged for each node based on \a
210 /// Data::HasChanged of its operands, until fixed point.
211 void propagateChanges();
212
213 /// Get a forward reference to a node to use as an operand.
214 Metadata &getFwdReference(MDNode &Op);
215 };
216
217 /// Worklist of distinct nodes whose operands need to be remapped.
218 SmallVector<MDNode *, 16> DistinctWorklist;
219
220public:
221 MDNodeMapper(Mapper &M) : M(M) {}
222
223 /// Map a metadata node (and its transitive operands).
224 ///
225 /// Map all the (unmapped) nodes in the subgraph under \c N. The iterative
226 /// algorithm handles distinct nodes and uniqued node subgraphs using
227 /// different strategies.
228 ///
229 /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
230 /// using \a mapDistinctNode(). Their mapping can always be computed
231 /// immediately without visiting operands, even if their operands change.
232 ///
233 /// The mapping for uniqued nodes depends on whether their operands change.
234 /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
235 /// a node to calculate uniqued node mappings in bulk. Distinct leafs are
236 /// added to \a DistinctWorklist with \a mapDistinctNode().
237 ///
238 /// After mapping \c N itself, this function remaps the operands of the
239 /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
240 /// N has been mapped.
241 Metadata *map(const MDNode &N);
242
243private:
244 /// Map a top-level uniqued node and the uniqued subgraph underneath it.
245 ///
246 /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
247 /// underneath \c FirstN and calculates the nodes' mapping. Each node uses
248 /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
249 /// operands uses the identity mapping.
250 ///
251 /// The algorithm works as follows:
252 ///
253 /// 1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
254 /// save the post-order traversal in the given \a UniquedGraph, tracking
255 /// nodes' operands change.
256 ///
257 /// 2. \a UniquedGraph::propagateChanges(): propagate changed operands
258 /// through the \a UniquedGraph until fixed point, following the rule
259 /// that if a node changes, any node that references must also change.
260 ///
261 /// 3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
262 /// (referencing new operands) where necessary.
263 Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
264
265 /// Try to map the operand of an \a MDNode.
266 ///
267 /// If \c Op is already mapped, return the mapping. If it's not an \a
268 /// MDNode, compute and return the mapping. If it's a distinct \a MDNode,
269 /// return the result of \a mapDistinctNode().
270 ///
271 /// \return std::nullopt if \c Op is an unmapped uniqued \a MDNode.
272 /// \post getMappedOp(Op) only returns std::nullopt if this returns
273 /// std::nullopt.
274 std::optional<Metadata *> tryToMapOperand(const Metadata *Op);
275
276 /// Map a distinct node.
277 ///
278 /// Return the mapping for the distinct node \c N, saving the result in \a
279 /// DistinctWorklist for later remapping.
280 ///
281 /// \pre \c N is not yet mapped.
282 /// \pre \c N.isDistinct().
283 MDNode *mapDistinctNode(const MDNode &N);
284
285 /// Get a previously mapped node.
286 std::optional<Metadata *> getMappedOp(const Metadata *Op) const;
287
288 /// Create a post-order traversal of an unmapped uniqued node subgraph.
289 ///
290 /// This traverses the metadata graph deeply enough to map \c FirstN. It
291 /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
292 /// metadata that has already been mapped will not be part of the POT.
293 ///
294 /// Each node that has a changed operand from outside the graph (e.g., a
295 /// distinct node, an already-mapped uniqued node, or \a ConstantAsMetadata)
296 /// is marked with \a Data::HasChanged.
297 ///
298 /// \return \c true if any nodes in \c G have \a Data::HasChanged.
299 /// \post \c G.POT is a post-order traversal ending with \c FirstN.
300 /// \post \a Data::hasChanged in \c G.Info indicates whether any node needs
301 /// to change because of operands outside the graph.
302 bool createPOT(UniquedGraph &G, const MDNode &FirstN);
303
304 /// Visit the operands of a uniqued node in the POT.
305 ///
306 /// Visit the operands in the range from \c I to \c E, returning the first
307 /// uniqued node we find that isn't yet in \c G. \c I is always advanced to
308 /// where to continue the loop through the operands.
309 ///
310 /// This sets \c HasChanged if any of the visited operands change.
311 MDNode *visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
312 MDNode::op_iterator E, bool &HasChanged);
313
314 /// Map all the nodes in the given uniqued graph.
315 ///
316 /// This visits all the nodes in \c G in post-order, using the identity
317 /// mapping or creating a new node depending on \a Data::HasChanged.
318 ///
319 /// \pre \a getMappedOp() returns std::nullopt for nodes in \c G, but not for
320 /// any of their operands outside of \c G. \pre \a Data::HasChanged is true
321 /// for a node in \c G iff any of its operands have changed. \post \a
322 /// getMappedOp() returns the mapped node for every node in \c G.
323 void mapNodesInPOT(UniquedGraph &G);
324
325 /// Remap a node's operands using the given functor.
326 ///
327 /// Iterate through the operands of \c N and update them in place using \c
328 /// mapOperand.
329 ///
330 /// \pre N.isDistinct() or N.isTemporary().
331 template <class OperandMapper>
332 void remapOperands(MDNode &N, OperandMapper mapOperand);
333};
334
335} // end anonymous namespace
336
337Value *Mapper::mapValue(const Value *V) {
338 ValueToValueMapTy::iterator I = getVM().find(Val: V);
339
340 // If the value already exists in the map, use it.
341 if (I != getVM().end()) {
342 assert(I->second && "Unexpected null mapping");
343 return I->second;
344 }
345
346 // If we have a materializer and it can materialize a value, use that.
347 if (auto *Materializer = getMaterializer()) {
348 if (Value *NewV = Materializer->materialize(V: const_cast<Value *>(V))) {
349 getVM()[V] = NewV;
350 return NewV;
351 }
352 }
353
354 // Global values do not need to be seeded into the VM if they
355 // are using the identity mapping.
356 if (isa<GlobalValue>(Val: V)) {
357 if (Flags & RF_NullMapMissingGlobalValues)
358 return nullptr;
359 return getVM()[V] = const_cast<Value *>(V);
360 }
361
362 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Val: V)) {
363 // Inline asm may need *type* remapping.
364 FunctionType *NewTy = IA->getFunctionType();
365 if (TypeMapper) {
366 NewTy = cast<FunctionType>(Val: TypeMapper->remapType(SrcTy: NewTy));
367
368 if (NewTy != IA->getFunctionType())
369 V = InlineAsm::get(Ty: NewTy, AsmString: IA->getAsmString(), Constraints: IA->getConstraintString(),
370 hasSideEffects: IA->hasSideEffects(), isAlignStack: IA->isAlignStack(),
371 asmDialect: IA->getDialect(), canThrow: IA->canThrow());
372 }
373
374 return getVM()[V] = const_cast<Value *>(V);
375 }
376
377 if (const auto *MDV = dyn_cast<MetadataAsValue>(Val: V)) {
378 const Metadata *MD = MDV->getMetadata();
379
380 if (auto *LAM = dyn_cast<LocalAsMetadata>(Val: MD)) {
381 // Look through to grab the local value.
382 if (Value *LV = mapValue(V: LAM->getValue())) {
383 if (V == LAM->getValue())
384 return const_cast<Value *>(V);
385 return MetadataAsValue::get(Context&: V->getContext(), MD: ValueAsMetadata::get(V: LV));
386 }
387
388 // FIXME: always return nullptr once Verifier::verifyDominatesUse()
389 // ensures metadata operands only reference defined SSA values.
390 return (Flags & RF_IgnoreMissingLocals)
391 ? nullptr
392 : MetadataAsValue::get(Context&: V->getContext(),
393 MD: MDTuple::get(Context&: V->getContext(), MDs: {}));
394 }
395 if (auto *AL = dyn_cast<DIArgList>(Val: MD)) {
396 SmallVector<ValueAsMetadata *, 4> MappedArgs;
397 for (auto *VAM : AL->getArgs()) {
398 // Map both Local and Constant VAMs here; they will both ultimately
399 // be mapped via mapValue. The exceptions are constants when we have no
400 // module level changes and locals when they have no existing mapped
401 // value and RF_IgnoreMissingLocals is set; these have identity
402 // mappings.
403 if ((Flags & RF_NoModuleLevelChanges) && isa<ConstantAsMetadata>(Val: VAM)) {
404 MappedArgs.push_back(Elt: VAM);
405 } else if (Value *LV = mapValue(V: VAM->getValue())) {
406 MappedArgs.push_back(
407 Elt: LV == VAM->getValue() ? VAM : ValueAsMetadata::get(V: LV));
408 } else if ((Flags & RF_IgnoreMissingLocals) && isa<LocalAsMetadata>(Val: VAM)) {
409 MappedArgs.push_back(Elt: VAM);
410 } else {
411 // If we cannot map the value, set the argument as poison.
412 MappedArgs.push_back(Elt: ValueAsMetadata::get(
413 V: PoisonValue::get(T: VAM->getValue()->getType())));
414 }
415 }
416 return MetadataAsValue::get(Context&: V->getContext(),
417 MD: DIArgList::get(Context&: V->getContext(), Args: MappedArgs));
418 }
419
420 // If this is a module-level metadata and we know that nothing at the module
421 // level is changing, then use an identity mapping.
422 if (Flags & RF_NoModuleLevelChanges)
423 return getVM()[V] = const_cast<Value *>(V);
424
425 // Map the metadata and turn it into a value.
426 auto *MappedMD = mapMetadata(MD);
427 if (MD == MappedMD)
428 return getVM()[V] = const_cast<Value *>(V);
429 return getVM()[V] = MetadataAsValue::get(Context&: V->getContext(), MD: MappedMD);
430 }
431
432 // Okay, this either must be a constant (which may or may not be mappable) or
433 // is something that is not in the mapping table.
434 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val: V));
435 if (!C)
436 return nullptr;
437
438 if (BlockAddress *BA = dyn_cast<BlockAddress>(Val: C))
439 return mapBlockAddress(BA: *BA);
440
441 if (const auto *E = dyn_cast<DSOLocalEquivalent>(Val: C)) {
442 auto *Val = mapValue(V: E->getGlobalValue());
443 GlobalValue *GV = dyn_cast<GlobalValue>(Val);
444 if (GV)
445 return getVM()[E] = DSOLocalEquivalent::get(GV);
446
447 auto *Func = cast<Function>(Val: Val->stripPointerCastsAndAliases());
448 Type *NewTy = E->getType();
449 if (TypeMapper)
450 NewTy = TypeMapper->remapType(SrcTy: NewTy);
451 return getVM()[E] = llvm::ConstantExpr::getBitCast(
452 C: DSOLocalEquivalent::get(GV: Func), Ty: NewTy);
453 }
454
455 if (const auto *NC = dyn_cast<NoCFIValue>(Val: C)) {
456 auto *Val = mapValue(V: NC->getGlobalValue());
457 GlobalValue *GV = cast<GlobalValue>(Val);
458 return getVM()[NC] = NoCFIValue::get(GV);
459 }
460
461 auto mapValueOrNull = [this](Value *V) {
462 auto Mapped = mapValue(V);
463 assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
464 "Unexpected null mapping for constant operand without "
465 "NullMapMissingGlobalValues flag");
466 return Mapped;
467 };
468
469 // Otherwise, we have some other constant to remap. Start by checking to see
470 // if all operands have an identity remapping.
471 unsigned OpNo = 0, NumOperands = C->getNumOperands();
472 Value *Mapped = nullptr;
473 for (; OpNo != NumOperands; ++OpNo) {
474 Value *Op = C->getOperand(i: OpNo);
475 Mapped = mapValueOrNull(Op);
476 if (!Mapped)
477 return nullptr;
478 if (Mapped != Op)
479 break;
480 }
481
482 // See if the type mapper wants to remap the type as well.
483 Type *NewTy = C->getType();
484 if (TypeMapper)
485 NewTy = TypeMapper->remapType(SrcTy: NewTy);
486
487 // If the result type and all operands match up, then just insert an identity
488 // mapping.
489 if (OpNo == NumOperands && NewTy == C->getType())
490 return getVM()[V] = C;
491
492 // Okay, we need to create a new constant. We've already processed some or
493 // all of the operands, set them all up now.
494 SmallVector<Constant*, 8> Ops;
495 Ops.reserve(N: NumOperands);
496 for (unsigned j = 0; j != OpNo; ++j)
497 Ops.push_back(Elt: cast<Constant>(Val: C->getOperand(i: j)));
498
499 // If one of the operands mismatch, push it and the other mapped operands.
500 if (OpNo != NumOperands) {
501 Ops.push_back(Elt: cast<Constant>(Val: Mapped));
502
503 // Map the rest of the operands that aren't processed yet.
504 for (++OpNo; OpNo != NumOperands; ++OpNo) {
505 Mapped = mapValueOrNull(C->getOperand(i: OpNo));
506 if (!Mapped)
507 return nullptr;
508 Ops.push_back(Elt: cast<Constant>(Val: Mapped));
509 }
510 }
511 Type *NewSrcTy = nullptr;
512 if (TypeMapper)
513 if (auto *GEPO = dyn_cast<GEPOperator>(Val: C))
514 NewSrcTy = TypeMapper->remapType(SrcTy: GEPO->getSourceElementType());
515
516 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: C))
517 return getVM()[V] = CE->getWithOperands(Ops, Ty: NewTy, OnlyIfReduced: false, SrcTy: NewSrcTy);
518 if (isa<ConstantArray>(Val: C))
519 return getVM()[V] = ConstantArray::get(T: cast<ArrayType>(Val: NewTy), V: Ops);
520 if (isa<ConstantStruct>(Val: C))
521 return getVM()[V] = ConstantStruct::get(T: cast<StructType>(Val: NewTy), V: Ops);
522 if (isa<ConstantVector>(Val: C))
523 return getVM()[V] = ConstantVector::get(V: Ops);
524 if (isa<ConstantPtrAuth>(Val: C))
525 return getVM()[V] =
526 ConstantPtrAuth::get(Ptr: Ops[0], Key: cast<ConstantInt>(Val: Ops[1]),
527 Disc: cast<ConstantInt>(Val: Ops[2]), AddrDisc: Ops[3], DeactivationSymbol: Ops[4]);
528 // If this is a no-operand constant, it must be because the type was remapped.
529 if (isa<PoisonValue>(Val: C))
530 return getVM()[V] = PoisonValue::get(T: NewTy);
531 if (isa<UndefValue>(Val: C))
532 return getVM()[V] = UndefValue::get(T: NewTy);
533 if (isa<ConstantAggregateZero>(Val: C))
534 return getVM()[V] = ConstantAggregateZero::get(Ty: NewTy);
535 if (isa<ConstantTargetNone>(Val: C))
536 return getVM()[V] = Constant::getNullValue(Ty: NewTy);
537 assert(isa<ConstantPointerNull>(C));
538 return getVM()[V] = ConstantPointerNull::get(T: NewTy);
539}
540
541void Mapper::remapDbgRecord(DbgRecord &DR) {
542 // Remap DILocations.
543 auto *MappedDILoc = mapMetadata(MD: DR.getDebugLoc());
544 DR.setDebugLoc(DebugLoc(cast<DILocation>(Val: MappedDILoc)));
545
546 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(Val: &DR)) {
547 // Remap labels.
548 DLR->setLabel(cast<DILabel>(Val: mapMetadata(MD: DLR->getLabel())));
549 return;
550 }
551
552 DbgVariableRecord &V = cast<DbgVariableRecord>(Val&: DR);
553 // Remap variables.
554 auto *MappedVar = mapMetadata(MD: V.getVariable());
555 V.setVariable(cast<DILocalVariable>(Val: MappedVar));
556
557 bool IgnoreMissingLocals = Flags & RF_IgnoreMissingLocals;
558
559 if (V.isDbgAssign()) {
560 auto *NewAddr = mapValue(V: V.getAddress());
561 if (!IgnoreMissingLocals && !NewAddr)
562 V.setKillAddress();
563 else if (NewAddr)
564 V.setAddress(NewAddr);
565 V.setAssignId(cast<DIAssignID>(Val: mapMetadata(MD: V.getAssignID())));
566 }
567
568 // Find Value operands and remap those.
569 SmallVector<Value *, 4> Vals(V.location_ops());
570 SmallVector<Value *, 4> NewVals;
571 for (Value *Val : Vals)
572 NewVals.push_back(Elt: mapValue(V: Val));
573
574 // If there are no changes to the Value operands, finished.
575 if (Vals == NewVals)
576 return;
577
578 // Otherwise, do some replacement.
579 if (!IgnoreMissingLocals && llvm::is_contained(Range&: NewVals, Element: nullptr)) {
580 V.setKillLocation();
581 } else {
582 // Either we have all non-empty NewVals, or we're permitted to ignore
583 // missing locals.
584 for (unsigned int I = 0; I < Vals.size(); ++I)
585 if (NewVals[I])
586 V.replaceVariableLocationOp(OpIdx: I, NewValue: NewVals[I]);
587 }
588}
589
590Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
591 Function *F = cast<Function>(Val: mapValue(V: BA.getFunction()));
592
593 // F may not have materialized its initializer. In that case, create a
594 // dummy basic block for now, and replace it once we've materialized all
595 // the initializers.
596 BasicBlock *BB;
597 if (F->empty()) {
598 DelayedBBs.push_back(Elt: DelayedBasicBlock(BA));
599 BB = DelayedBBs.back().TempBB.get();
600 } else {
601 BB = cast_or_null<BasicBlock>(Val: mapValue(V: BA.getBasicBlock()));
602 }
603
604 return getVM()[&BA] = BlockAddress::get(F, BB: BB ? BB : BA.getBasicBlock());
605}
606
607Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
608 getVM().MD()[Key].reset(MD: Val);
609 return Val;
610}
611
612Metadata *Mapper::mapToSelf(const Metadata *MD) {
613 return mapToMetadata(Key: MD, Val: const_cast<Metadata *>(MD));
614}
615
616std::optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
617 if (!Op)
618 return nullptr;
619
620 if (std::optional<Metadata *> MappedOp = M.mapSimpleMetadata(MD: Op)) {
621#ifndef NDEBUG
622 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
623 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
624 M.getVM().getMappedMD(Op)) &&
625 "Expected Value to be memoized");
626 else
627 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
628 "Expected result to be memoized");
629#endif
630 return *MappedOp;
631 }
632
633 const MDNode &N = *cast<MDNode>(Val: Op);
634 if (N.isDistinct())
635 return mapDistinctNode(N);
636 return std::nullopt;
637}
638
639MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
640 assert(N.isDistinct() && "Expected a distinct node");
641 assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
642 Metadata *NewM = nullptr;
643
644 if (M.Flags & RF_ReuseAndMutateDistinctMDs) {
645 NewM = M.mapToSelf(MD: &N);
646 } else {
647 NewM = MDNode::replaceWithDistinct(N: N.clone());
648 LLVM_DEBUG(dbgs() << "\nMap " << N << "\n"
649 << "To " << *NewM << "\n\n");
650 M.mapToMetadata(Key: &N, Val: NewM);
651 }
652 DistinctWorklist.push_back(Elt: cast<MDNode>(Val: NewM));
653
654 return DistinctWorklist.back();
655}
656
657static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
658 Value *MappedV) {
659 if (CMD.getValue() == MappedV)
660 return const_cast<ConstantAsMetadata *>(&CMD);
661 return MappedV ? ConstantAsMetadata::getConstant(C: MappedV) : nullptr;
662}
663
664std::optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
665 if (!Op)
666 return nullptr;
667
668 if (std::optional<Metadata *> MappedOp = M.getVM().getMappedMD(MD: Op))
669 return *MappedOp;
670
671 if (isa<MDString>(Val: Op))
672 return const_cast<Metadata *>(Op);
673
674 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Val: Op))
675 return wrapConstantAsMetadata(CMD: *CMD, MappedV: M.getVM().lookup(Val: CMD->getValue()));
676
677 return std::nullopt;
678}
679
680Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
681 auto Where = Info.find(Val: &Op);
682 assert(Where != Info.end() && "Expected a valid reference");
683
684 auto &OpD = Where->second;
685 if (!OpD.HasChanged)
686 return Op;
687
688 // Lazily construct a temporary node.
689 if (!OpD.Placeholder)
690 OpD.Placeholder = Op.clone();
691
692 return *OpD.Placeholder;
693}
694
695template <class OperandMapper>
696void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
697 assert(!N.isUniqued() && "Expected distinct or temporary nodes");
698 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
699 Metadata *Old = N.getOperand(I);
700 Metadata *New = mapOperand(Old);
701 if (Old != New)
702 LLVM_DEBUG(dbgs() << "Replacing Op " << Old << " with " << New << " in "
703 << N << "\n");
704
705 if (Old != New)
706 N.replaceOperandWith(I, New);
707 }
708}
709
710namespace {
711
712/// An entry in the worklist for the post-order traversal.
713struct POTWorklistEntry {
714 MDNode *N; ///< Current node.
715 MDNode::op_iterator Op; ///< Current operand of \c N.
716
717 /// Keep a flag of whether operands have changed in the worklist to avoid
718 /// hitting the map in \a UniquedGraph.
719 bool HasChanged = false;
720
721 POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
722};
723
724} // end anonymous namespace
725
726bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
727 assert(G.Info.empty() && "Expected a fresh traversal");
728 assert(FirstN.isUniqued() && "Expected uniqued node in POT");
729
730 // Construct a post-order traversal of the uniqued subgraph under FirstN.
731 bool AnyChanges = false;
732 SmallVector<POTWorklistEntry, 16> Worklist;
733 Worklist.push_back(Elt: POTWorklistEntry(const_cast<MDNode &>(FirstN)));
734 (void)G.Info[&FirstN];
735 while (!Worklist.empty()) {
736 // Start or continue the traversal through the this node's operands.
737 auto &WE = Worklist.back();
738 if (MDNode *N = visitOperands(G, I&: WE.Op, E: WE.N->op_end(), HasChanged&: WE.HasChanged)) {
739 // Push a new node to traverse first.
740 Worklist.push_back(Elt: POTWorklistEntry(*N));
741 continue;
742 }
743
744 // Push the node onto the POT.
745 assert(WE.N->isUniqued() && "Expected only uniqued nodes");
746 assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
747 auto &D = G.Info[WE.N];
748 AnyChanges |= D.HasChanged = WE.HasChanged;
749 D.ID = G.POT.size();
750 G.POT.push_back(Elt: WE.N);
751
752 // Pop the node off the worklist.
753 Worklist.pop_back();
754 }
755 return AnyChanges;
756}
757
758MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
759 MDNode::op_iterator E, bool &HasChanged) {
760 while (I != E) {
761 Metadata *Op = *I++; // Increment even on early return.
762 if (std::optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
763 // Check if the operand changes.
764 HasChanged |= Op != *MappedOp;
765 continue;
766 }
767
768 // A uniqued metadata node.
769 MDNode &OpN = *cast<MDNode>(Val: Op);
770 assert(OpN.isUniqued() &&
771 "Only uniqued operands cannot be mapped immediately");
772 if (G.Info.try_emplace(Key: &OpN).second)
773 return &OpN; // This is a new one. Return it.
774 }
775 return nullptr;
776}
777
778void MDNodeMapper::UniquedGraph::propagateChanges() {
779 bool AnyChanges;
780 do {
781 AnyChanges = false;
782 for (MDNode *N : POT) {
783 auto &D = Info[N];
784 if (D.HasChanged)
785 continue;
786
787 if (llvm::none_of(Range: N->operands(), P: [&](const Metadata *Op) {
788 auto Where = Info.find(Val: Op);
789 return Where != Info.end() && Where->second.HasChanged;
790 }))
791 continue;
792
793 AnyChanges = D.HasChanged = true;
794 }
795 } while (AnyChanges);
796}
797
798void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
799 // Construct uniqued nodes, building forward references as necessary.
800 SmallVector<MDNode *, 16> CyclicNodes;
801 for (auto *N : G.POT) {
802 auto &D = G.Info[N];
803 if (!D.HasChanged) {
804 // The node hasn't changed.
805 M.mapToSelf(MD: N);
806 continue;
807 }
808
809 // Remember whether this node had a placeholder.
810 bool HadPlaceholder(D.Placeholder);
811
812 // Clone the uniqued node and remap the operands.
813 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
814 remapOperands(N&: *ClonedN, mapOperand: [this, &D, &G](Metadata *Old) {
815 if (std::optional<Metadata *> MappedOp = getMappedOp(Op: Old))
816 return *MappedOp;
817 (void)D;
818 assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
819 return &G.getFwdReference(Op&: *cast<MDNode>(Val: Old));
820 });
821
822 auto *NewN = MDNode::replaceWithUniqued(N: std::move(ClonedN));
823 if (N && NewN && N != NewN) {
824 LLVM_DEBUG(dbgs() << "\nMap " << *N << "\n"
825 << "To " << *NewN << "\n\n");
826 }
827
828 M.mapToMetadata(Key: N, Val: NewN);
829
830 // Nodes that were referenced out of order in the POT are involved in a
831 // uniquing cycle.
832 if (HadPlaceholder)
833 CyclicNodes.push_back(Elt: NewN);
834 }
835
836 // Resolve cycles.
837 for (auto *N : CyclicNodes)
838 if (!N->isResolved())
839 N->resolveCycles();
840}
841
842Metadata *MDNodeMapper::map(const MDNode &N) {
843 assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
844 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
845 "MDNodeMapper::map assumes module-level changes");
846
847 // Require resolved nodes whenever metadata might be remapped.
848 assert(N.isResolved() && "Unexpected unresolved node");
849
850 Metadata *MappedN =
851 N.isUniqued() ? mapTopLevelUniquedNode(FirstN: N) : mapDistinctNode(N);
852 while (!DistinctWorklist.empty())
853 remapOperands(N&: *DistinctWorklist.pop_back_val(), mapOperand: [this](Metadata *Old) {
854 if (std::optional<Metadata *> MappedOp = tryToMapOperand(Op: Old))
855 return *MappedOp;
856 return mapTopLevelUniquedNode(FirstN: *cast<MDNode>(Val: Old));
857 });
858 return MappedN;
859}
860
861Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
862 assert(FirstN.isUniqued() && "Expected uniqued node");
863
864 // Create a post-order traversal of uniqued nodes under FirstN.
865 UniquedGraph G;
866 if (!createPOT(G, FirstN)) {
867 // Return early if no nodes have changed.
868 for (const MDNode *N : G.POT)
869 M.mapToSelf(MD: N);
870 return &const_cast<MDNode &>(FirstN);
871 }
872
873 // Update graph with all nodes that have changed.
874 G.propagateChanges();
875
876 // Map all the nodes in the graph.
877 mapNodesInPOT(G);
878
879 // Return the original node, remapped.
880 return *getMappedOp(Op: &FirstN);
881}
882
883std::optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
884 // If the value already exists in the map, use it.
885 if (std::optional<Metadata *> NewMD = getVM().getMappedMD(MD))
886 return *NewMD;
887
888 if (isa<MDString>(Val: MD))
889 return const_cast<Metadata *>(MD);
890
891 // This is a module-level metadata. If nothing at the module level is
892 // changing, use an identity mapping.
893 if ((Flags & RF_NoModuleLevelChanges))
894 return const_cast<Metadata *>(MD);
895
896 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Val: MD)) {
897 // Don't memoize ConstantAsMetadata. Instead of lasting until the
898 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
899 // reference is destructed. These aren't super common, so the extra
900 // indirection isn't that expensive.
901 return wrapConstantAsMetadata(CMD: *CMD, MappedV: mapValue(V: CMD->getValue()));
902 }
903
904 // Map metadata matching IdentityMD predicate on first use. We need to add
905 // these nodes to the mapping as otherwise metadata nodes numbering gets
906 // messed up.
907 if (IdentityMD && (*IdentityMD)(MD))
908 return getVM().MD()[MD] = TrackingMDRef(const_cast<Metadata *>(MD));
909
910 assert(isa<MDNode>(MD) && "Expected a metadata node");
911
912 return std::nullopt;
913}
914
915Metadata *Mapper::mapMetadata(const Metadata *MD) {
916 assert(MD && "Expected valid metadata");
917 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
918
919 if (std::optional<Metadata *> NewMD = mapSimpleMetadata(MD))
920 return *NewMD;
921
922 return MDNodeMapper(*this).map(N: *cast<MDNode>(Val: MD));
923}
924
925void Mapper::flush() {
926 // Flush out the worklist of global values.
927 while (!Worklist.empty()) {
928 WorklistEntry E = Worklist.pop_back_val();
929 CurrentMCID = E.MCID;
930 switch (E.Kind) {
931 case WorklistEntry::MapGlobalInit:
932 E.Data.GVInit.GV->setInitializer(mapConstant(C: E.Data.GVInit.Init));
933 remapGlobalObjectMetadata(GO&: *E.Data.GVInit.GV);
934 break;
935 case WorklistEntry::MapAppendingVar: {
936 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
937 // mapAppendingVariable call can change AppendingInits if initalizer for
938 // the variable depends on another appending global, because of that inits
939 // need to be extracted and updated before the call.
940 SmallVector<Constant *, 8> NewInits(
941 drop_begin(RangeOrContainer&: AppendingInits, N: PrefixSize));
942 AppendingInits.resize(N: PrefixSize);
943 mapAppendingVariable(GV&: *E.Data.AppendingGV.GV,
944 OldGV: E.Data.AppendingGV.OldGV,
945 IsOldCtorDtor: E.AppendingGVIsOldCtorDtor, NewMembers: ArrayRef(NewInits));
946 break;
947 }
948 case WorklistEntry::MapAliasOrIFunc: {
949 GlobalValue *GV = E.Data.AliasOrIFunc.GV;
950 Constant *Target = mapConstant(C: E.Data.AliasOrIFunc.Target);
951 if (auto *GA = dyn_cast<GlobalAlias>(Val: GV))
952 GA->setAliasee(Target);
953 else if (auto *GI = dyn_cast<GlobalIFunc>(Val: GV))
954 GI->setResolver(Target);
955 else
956 llvm_unreachable("Not alias or ifunc");
957 break;
958 }
959 case WorklistEntry::RemapFunction:
960 remapFunction(F&: *E.Data.RemapF);
961 break;
962 }
963 }
964 CurrentMCID = 0;
965
966 // Finish logic for block addresses now that all global values have been
967 // handled.
968 while (!DelayedBBs.empty()) {
969 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
970 BasicBlock *BB = cast_or_null<BasicBlock>(Val: mapValue(V: DBB.OldBB));
971 DBB.TempBB->replaceAllUsesWith(V: BB ? BB : DBB.OldBB);
972 }
973}
974
975void Mapper::remapInstruction(Instruction *I) {
976 // Remap operands.
977 for (Use &Op : I->operands()) {
978 Value *V = mapValue(V: Op);
979 // If we aren't ignoring missing entries, assert that something happened.
980 if (V)
981 Op = V;
982 else
983 assert((Flags & RF_IgnoreMissingLocals) &&
984 "Referenced value not in value map!");
985 }
986
987 // Drop callee_type metadata from calls that were remapped
988 // into a direct call from an indirect one.
989 if (auto *CB = dyn_cast<CallBase>(Val: I)) {
990 if (CB->getMetadata(KindID: LLVMContext::MD_callee_type) && !CB->isIndirectCall())
991 CB->setMetadata(KindID: LLVMContext::MD_callee_type, Node: nullptr);
992 // !inline_history metadata can contain references to other functions that
993 // we don't want to map because ThinLTO doesn't track metadata references
994 // to functions.
995 if ((Flags & RF_Importing) &&
996 CB->getMetadata(KindID: LLVMContext::MD_inline_history))
997 CB->setMetadata(KindID: LLVMContext::MD_inline_history, Node: nullptr);
998 }
999
1000 // Remap phi nodes' incoming blocks.
1001 if (PHINode *PN = dyn_cast<PHINode>(Val: I)) {
1002 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1003 Value *V = mapValue(V: PN->getIncomingBlock(i));
1004 // If we aren't ignoring missing entries, assert that something happened.
1005 if (V)
1006 PN->setIncomingBlock(i, BB: cast<BasicBlock>(Val: V));
1007 else
1008 assert((Flags & RF_IgnoreMissingLocals) &&
1009 "Referenced block not in value map!");
1010 }
1011 }
1012
1013 // Remap attached metadata.
1014 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1015 I->getAllMetadata(MDs);
1016 for (const auto &MI : MDs) {
1017 MDNode *Old = MI.second;
1018 MDNode *New = cast_or_null<MDNode>(Val: mapMetadata(MD: Old));
1019 if (New != Old)
1020 I->setMetadata(KindID: MI.first, Node: New);
1021 }
1022
1023 // Remap source location atom instance.
1024 if (!(Flags & RF_DoNotRemapAtoms))
1025 RemapSourceAtom(I, VM&: getVM());
1026
1027 if (!TypeMapper)
1028 return;
1029
1030 // If the instruction's type is being remapped, do so now.
1031 if (auto *CB = dyn_cast<CallBase>(Val: I)) {
1032 SmallVector<Type *, 3> Tys;
1033 FunctionType *FTy = CB->getFunctionType();
1034 Tys.reserve(N: FTy->getNumParams());
1035 for (Type *Ty : FTy->params())
1036 Tys.push_back(Elt: TypeMapper->remapType(SrcTy: Ty));
1037 CB->mutateFunctionType(FTy: FunctionType::get(
1038 Result: TypeMapper->remapType(SrcTy: I->getType()), Params: Tys, isVarArg: FTy->isVarArg()));
1039
1040 LLVMContext &C = CB->getContext();
1041 AttributeList Attrs = CB->getAttributes();
1042 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
1043 for (unsigned AttrIdx = Attribute::FirstTypeAttr;
1044 AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
1045 Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
1046 if (Type *Ty =
1047 Attrs.getAttributeAtIndex(Index: i, Kind: TypedAttr).getValueAsType()) {
1048 Attrs = Attrs.replaceAttributeTypeAtIndex(C, ArgNo: i, Kind: TypedAttr,
1049 ReplacementTy: TypeMapper->remapType(SrcTy: Ty));
1050 break;
1051 }
1052 }
1053 }
1054 CB->setAttributes(Attrs);
1055 return;
1056 }
1057 if (auto *AI = dyn_cast<AllocaInst>(Val: I))
1058 AI->setAllocatedType(TypeMapper->remapType(SrcTy: AI->getAllocatedType()));
1059 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: I)) {
1060 GEP->setSourceElementType(
1061 TypeMapper->remapType(SrcTy: GEP->getSourceElementType()));
1062 GEP->setResultElementType(
1063 TypeMapper->remapType(SrcTy: GEP->getResultElementType()));
1064 }
1065 I->mutateType(Ty: TypeMapper->remapType(SrcTy: I->getType()));
1066}
1067
1068void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
1069 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1070 GO.getAllMetadata(MDs);
1071 GO.clearMetadata();
1072 for (const auto &I : MDs)
1073 GO.addMetadata(KindID: I.first, MD&: *cast<MDNode>(Val: mapMetadata(MD: I.second)));
1074}
1075
1076void Mapper::remapFunction(Function &F) {
1077 // Remap the operands.
1078 for (Use &Op : F.operands())
1079 if (Op)
1080 Op = mapValue(V: Op);
1081
1082 // Remap the metadata attachments.
1083 remapGlobalObjectMetadata(GO&: F);
1084
1085 // Remap the argument types.
1086 if (TypeMapper)
1087 for (Argument &A : F.args())
1088 A.mutateType(Ty: TypeMapper->remapType(SrcTy: A.getType()));
1089
1090 // Remap the instructions.
1091 for (BasicBlock &BB : F) {
1092 for (Instruction &I : BB) {
1093 remapInstruction(I: &I);
1094 for (DbgRecord &DR : I.getDbgRecordRange())
1095 remapDbgRecord(DR);
1096 }
1097 }
1098}
1099
1100void Mapper::mapAppendingVariable(GlobalVariable &GV, GlobalVariable *OldGV,
1101 bool IsOldCtorDtor,
1102 ArrayRef<Constant *> NewMembers) {
1103 Constant *InitPrefix =
1104 (OldGV && !OldGV->isDeclaration()) ? OldGV->getInitializer() : nullptr;
1105
1106 SmallVector<Constant *, 16> Elements;
1107 if (InitPrefix) {
1108 unsigned NumElements =
1109 cast<ArrayType>(Val: InitPrefix->getType())->getNumElements();
1110 for (unsigned I = 0; I != NumElements; ++I)
1111 Elements.push_back(Elt: InitPrefix->getAggregateElement(Elt: I));
1112 OldGV->setInitializer(nullptr);
1113 if (InitPrefix->hasUseList() && InitPrefix->use_empty())
1114 InitPrefix->destroyConstant();
1115 }
1116
1117 PointerType *VoidPtrTy;
1118 Type *EltTy;
1119 if (IsOldCtorDtor) {
1120 // FIXME: This upgrade is done during linking to support the C API. See
1121 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
1122 VoidPtrTy = PointerType::getUnqual(C&: GV.getContext());
1123 auto &ST = *cast<StructType>(Val: NewMembers.front()->getType());
1124 Type *Tys[3] = {ST.getElementType(N: 0), ST.getElementType(N: 1), VoidPtrTy};
1125 EltTy = StructType::get(Context&: GV.getContext(), Elements: Tys, isPacked: false);
1126 }
1127
1128 for (auto *V : NewMembers) {
1129 Constant *NewV;
1130 if (IsOldCtorDtor) {
1131 auto *S = cast<ConstantStruct>(Val: V);
1132 auto *E1 = cast<Constant>(Val: mapValue(V: S->getOperand(i_nocapture: 0)));
1133 auto *E2 = cast<Constant>(Val: mapValue(V: S->getOperand(i_nocapture: 1)));
1134 Constant *Null = Constant::getNullValue(Ty: VoidPtrTy);
1135 NewV = ConstantStruct::get(T: cast<StructType>(Val: EltTy), Vs: E1, Vs: E2, Vs: Null);
1136 } else {
1137 NewV = cast_or_null<Constant>(Val: mapValue(V));
1138 }
1139 Elements.push_back(Elt: NewV);
1140 }
1141
1142 GV.setInitializer(
1143 ConstantArray::get(T: cast<ArrayType>(Val: GV.getValueType()), V: Elements));
1144}
1145
1146void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1147 unsigned MCID) {
1148 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1149 assert(MCID < MCs.size() && "Invalid mapping context");
1150
1151 WorklistEntry WE;
1152 WE.Kind = WorklistEntry::MapGlobalInit;
1153 WE.MCID = MCID;
1154 WE.Data.GVInit.GV = &GV;
1155 WE.Data.GVInit.Init = &Init;
1156 Worklist.push_back(Elt: WE);
1157}
1158
1159void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1160 GlobalVariable *OldGV,
1161 bool IsOldCtorDtor,
1162 ArrayRef<Constant *> NewMembers,
1163 unsigned MCID) {
1164 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1165 assert(MCID < MCs.size() && "Invalid mapping context");
1166
1167 WorklistEntry WE;
1168 WE.Kind = WorklistEntry::MapAppendingVar;
1169 WE.MCID = MCID;
1170 WE.Data.AppendingGV.GV = &GV;
1171 WE.Data.AppendingGV.OldGV = OldGV;
1172 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1173 WE.AppendingGVNumNewMembers = NewMembers.size();
1174 Worklist.push_back(Elt: WE);
1175 AppendingInits.append(in_start: NewMembers.begin(), in_end: NewMembers.end());
1176}
1177
1178void Mapper::scheduleMapAliasOrIFunc(GlobalValue &GV, Constant &Target,
1179 unsigned MCID) {
1180 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1181 assert((isa<GlobalAlias>(GV) || isa<GlobalIFunc>(GV)) &&
1182 "Should be alias or ifunc");
1183 assert(MCID < MCs.size() && "Invalid mapping context");
1184
1185 WorklistEntry WE;
1186 WE.Kind = WorklistEntry::MapAliasOrIFunc;
1187 WE.MCID = MCID;
1188 WE.Data.AliasOrIFunc.GV = &GV;
1189 WE.Data.AliasOrIFunc.Target = &Target;
1190 Worklist.push_back(Elt: WE);
1191}
1192
1193void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1194 assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1195 assert(MCID < MCs.size() && "Invalid mapping context");
1196
1197 WorklistEntry WE;
1198 WE.Kind = WorklistEntry::RemapFunction;
1199 WE.MCID = MCID;
1200 WE.Data.RemapF = &F;
1201 Worklist.push_back(Elt: WE);
1202}
1203
1204void Mapper::addFlags(RemapFlags Flags) {
1205 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1206 this->Flags = this->Flags | Flags;
1207}
1208
1209static Mapper *getAsMapper(void *pImpl) {
1210 return reinterpret_cast<Mapper *>(pImpl);
1211}
1212
1213namespace {
1214
1215class FlushingMapper {
1216 Mapper &M;
1217
1218public:
1219 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1220 assert(!M.hasWorkToDo() && "Expected to be flushed");
1221 }
1222
1223 ~FlushingMapper() { M.flush(); }
1224
1225 Mapper *operator->() const { return &M; }
1226};
1227
1228} // end anonymous namespace
1229
1230ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1231 ValueMapTypeRemapper *TypeMapper,
1232 ValueMaterializer *Materializer,
1233 const MetadataPredicate *IdentityMD)
1234 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer, IdentityMD)) {}
1235
1236ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1237
1238unsigned
1239ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1240 ValueMaterializer *Materializer) {
1241 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1242}
1243
1244void ValueMapper::addFlags(RemapFlags Flags) {
1245 FlushingMapper(pImpl)->addFlags(Flags);
1246}
1247
1248Value *ValueMapper::mapValue(const Value &V) {
1249 return FlushingMapper(pImpl)->mapValue(V: &V);
1250}
1251
1252Constant *ValueMapper::mapConstant(const Constant &C) {
1253 return cast_or_null<Constant>(Val: mapValue(V: C));
1254}
1255
1256Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1257 return FlushingMapper(pImpl)->mapMetadata(MD: &MD);
1258}
1259
1260MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1261 return cast_or_null<MDNode>(Val: mapMetadata(MD: N));
1262}
1263
1264void ValueMapper::remapInstruction(Instruction &I) {
1265 FlushingMapper(pImpl)->remapInstruction(I: &I);
1266}
1267
1268void ValueMapper::remapDbgRecord(Module *M, DbgRecord &DR) {
1269 FlushingMapper(pImpl)->remapDbgRecord(DR);
1270}
1271
1272void ValueMapper::remapDbgRecordRange(
1273 Module *M, iterator_range<DbgRecord::self_iterator> Range) {
1274 for (DbgRecord &DR : Range) {
1275 remapDbgRecord(M, DR);
1276 }
1277}
1278
1279void ValueMapper::remapFunction(Function &F) {
1280 FlushingMapper(pImpl)->remapFunction(F);
1281}
1282
1283void ValueMapper::remapGlobalObjectMetadata(GlobalObject &GO) {
1284 FlushingMapper(pImpl)->remapGlobalObjectMetadata(GO);
1285}
1286
1287void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1288 Constant &Init,
1289 unsigned MCID) {
1290 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1291}
1292
1293void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1294 GlobalVariable *OldGV,
1295 bool IsOldCtorDtor,
1296 ArrayRef<Constant *> NewMembers,
1297 unsigned MCID) {
1298 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1299 GV, OldGV, IsOldCtorDtor, NewMembers, MCID);
1300}
1301
1302void ValueMapper::scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee,
1303 unsigned MCID) {
1304 getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GV&: GA, Target&: Aliasee, MCID);
1305}
1306
1307void ValueMapper::scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver,
1308 unsigned MCID) {
1309 getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GV&: GI, Target&: Resolver, MCID);
1310}
1311
1312void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1313 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1314}
1315
1316void llvm::RemapSourceAtom(Instruction *I, ValueToValueMapTy &VM) {
1317 const DebugLoc &DL = I->getDebugLoc();
1318 if (!DL)
1319 return;
1320
1321 auto AtomGroup = DL->getAtomGroup();
1322 if (!AtomGroup)
1323 return;
1324
1325 auto R = VM.AtomMap.find(Val: {DL->getInlinedAt(), AtomGroup});
1326 if (R == VM.AtomMap.end())
1327 return;
1328 AtomGroup = R->second;
1329
1330 // Remap the atom group and copy all other fields.
1331 DILocation *New = DILocation::get(
1332 Context&: I->getContext(), Line: DL.getLine(), Column: DL.getCol(), Scope: DL.getScope(),
1333 InlinedAt: DL.getInlinedAt(), ImplicitCode: DL.isImplicitCode(), AtomGroup, AtomRank: DL->getAtomRank());
1334 I->setDebugLoc(New);
1335}
1336