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