1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the MapValue function, which is shared by various parts of
10 // the lib/Transforms/Utils library.
12 //===----------------------------------------------------------------------===//
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/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/Argument.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DebugInfoMetadata.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GlobalIndirectSymbol.h"
30 #include "llvm/IR/GlobalObject.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InlineAsm.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.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"
48 #define DEBUG_TYPE "value-mapper"
50 // Out of line method to get vtable etc for class.
51 void ValueMapTypeRemapper::anchor() {}
52 void ValueMaterializer::anchor() {}
56 /// A basic block used in a BlockAddress whose function body is not yet
58 struct DelayedBasicBlock {
60 std::unique_ptr<BasicBlock> TempBB;
62 DelayedBasicBlock(const BlockAddress &Old)
63 : OldBB(Old.getBasicBlock()),
64 TempBB(BasicBlock::Create(Old.getContext())) {}
67 struct WorklistEntry {
71 MapGlobalIndirectSymbol,
78 struct AppendingGVTy {
82 struct GlobalIndirectSymbolTy {
83 GlobalIndirectSymbol *GIS;
89 unsigned AppendingGVIsOldCtorDtor : 1;
90 unsigned AppendingGVNumNewMembers;
93 AppendingGVTy AppendingGV;
94 GlobalIndirectSymbolTy GlobalIndirectSymbol;
99 struct MappingContext {
100 ValueToValueMapTy *VM;
101 ValueMaterializer *Materializer = nullptr;
103 /// Construct a MappingContext with a value map and materializer.
104 explicit MappingContext(ValueToValueMapTy &VM,
105 ValueMaterializer *Materializer = nullptr)
106 : VM(&VM), Materializer(Materializer) {}
110 friend class MDNodeMapper;
113 DenseSet<GlobalValue *> AlreadyScheduled;
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;
125 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
126 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
127 : Flags(Flags), TypeMapper(TypeMapper),
128 MCs(1, MappingContext(VM, Materializer)) {}
130 /// ValueMapper should explicitly call \a flush() before destruction.
131 ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
133 bool hasWorkToDo() const { return !Worklist.empty(); }
136 registerAlternateMappingContext(ValueToValueMapTy &VM,
137 ValueMaterializer *Materializer = nullptr) {
138 MCs.push_back(MappingContext(VM, Materializer));
139 return MCs.size() - 1;
142 void addFlags(RemapFlags Flags);
144 void remapGlobalObjectMetadata(GlobalObject &GO);
146 Value *mapValue(const Value *V);
147 void remapInstruction(Instruction *I);
148 void remapFunction(Function &F);
150 Constant *mapConstant(const Constant *C) {
151 return cast_or_null<Constant>(mapValue(C));
156 /// Find the mapping for MD. Guarantees that the return will be resolved
157 /// (not an MDNode, or MDNode::isResolved() returns true).
158 Metadata *mapMetadata(const Metadata *MD);
160 void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
162 void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
164 ArrayRef<Constant *> NewMembers,
166 void scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS, Constant &Target,
168 void scheduleRemapFunction(Function &F, unsigned MCID);
173 void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
175 ArrayRef<Constant *> NewMembers);
177 ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
178 ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
180 Value *mapBlockAddress(const BlockAddress &BA);
182 /// Map metadata that doesn't require visiting operands.
183 Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
185 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
186 Metadata *mapToSelf(const Metadata *MD);
192 /// Data about a node in \a UniquedGraph.
194 bool HasChanged = false;
195 unsigned ID = std::numeric_limits<unsigned>::max();
196 TempMDNode Placeholder;
199 /// A graph of uniqued nodes.
200 struct UniquedGraph {
201 SmallDenseMap<const Metadata *, Data, 32> Info; // Node properties.
202 SmallVector<MDNode *, 16> POT; // Post-order traversal.
204 /// Propagate changed operands through the post-order traversal.
206 /// Iteratively update \a Data::HasChanged for each node based on \a
207 /// Data::HasChanged of its operands, until fixed point.
208 void propagateChanges();
210 /// Get a forward reference to a node to use as an operand.
211 Metadata &getFwdReference(MDNode &Op);
214 /// Worklist of distinct nodes whose operands need to be remapped.
215 SmallVector<MDNode *, 16> DistinctWorklist;
217 // Storage for a UniquedGraph.
218 SmallDenseMap<const Metadata *, Data, 32> InfoStorage;
219 SmallVector<MDNode *, 16> POTStorage;
222 MDNodeMapper(Mapper &M) : M(M) {}
224 /// Map a metadata node (and its transitive operands).
226 /// Map all the (unmapped) nodes in the subgraph under \c N. The iterative
227 /// algorithm handles distinct nodes and uniqued node subgraphs using
228 /// different strategies.
230 /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
231 /// using \a mapDistinctNode(). Their mapping can always be computed
232 /// immediately without visiting operands, even if their operands change.
234 /// The mapping for uniqued nodes depends on whether their operands change.
235 /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
236 /// a node to calculate uniqued node mappings in bulk. Distinct leafs are
237 /// added to \a DistinctWorklist with \a mapDistinctNode().
239 /// After mapping \c N itself, this function remaps the operands of the
240 /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
241 /// N has been mapped.
242 Metadata *map(const MDNode &N);
245 /// Map a top-level uniqued node and the uniqued subgraph underneath it.
247 /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
248 /// underneath \c FirstN and calculates the nodes' mapping. Each node uses
249 /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
250 /// operands uses the identity mapping.
252 /// The algorithm works as follows:
254 /// 1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
255 /// save the post-order traversal in the given \a UniquedGraph, tracking
256 /// nodes' operands change.
258 /// 2. \a UniquedGraph::propagateChanges(): propagate changed operands
259 /// through the \a UniquedGraph until fixed point, following the rule
260 /// that if a node changes, any node that references must also change.
262 /// 3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
263 /// (referencing new operands) where necessary.
264 Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
266 /// Try to map the operand of an \a MDNode.
268 /// If \c Op is already mapped, return the mapping. If it's not an \a
269 /// MDNode, compute and return the mapping. If it's a distinct \a MDNode,
270 /// return the result of \a mapDistinctNode().
272 /// \return None if \c Op is an unmapped uniqued \a MDNode.
273 /// \post getMappedOp(Op) only returns None if this returns None.
274 Optional<Metadata *> tryToMapOperand(const Metadata *Op);
276 /// Map a distinct node.
278 /// Return the mapping for the distinct node \c N, saving the result in \a
279 /// DistinctWorklist for later remapping.
281 /// \pre \c N is not yet mapped.
282 /// \pre \c N.isDistinct().
283 MDNode *mapDistinctNode(const MDNode &N);
285 /// Get a previously mapped node.
286 Optional<Metadata *> getMappedOp(const Metadata *Op) const;
288 /// Create a post-order traversal of an unmapped uniqued node subgraph.
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.
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.
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);
304 /// Visit the operands of a uniqued node in the POT.
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.
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);
314 /// Map all the nodes in the given uniqued graph.
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.
319 /// \pre \a getMappedOp() returns None for nodes in \c G, but not for any of
320 /// their operands outside of \c G.
321 /// \pre \a Data::HasChanged is true for a node in \c G iff any of its
322 /// operands have changed.
323 /// \post \a getMappedOp() returns the mapped node for every node in \c G.
324 void mapNodesInPOT(UniquedGraph &G);
326 /// Remap a node's operands using the given functor.
328 /// Iterate through the operands of \c N and update them in place using \c
331 /// \pre N.isDistinct() or N.isTemporary().
332 template <class OperandMapper>
333 void remapOperands(MDNode &N, OperandMapper mapOperand);
336 } // end anonymous namespace
338 Value *Mapper::mapValue(const Value *V) {
339 ValueToValueMapTy::iterator I = getVM().find(V);
341 // If the value already exists in the map, use it.
342 if (I != getVM().end()) {
343 assert(I->second && "Unexpected null mapping");
347 // If we have a materializer and it can materialize a value, use that.
348 if (auto *Materializer = getMaterializer()) {
349 if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
355 // Global values do not need to be seeded into the VM if they
356 // are using the identity mapping.
357 if (isa<GlobalValue>(V)) {
358 if (Flags & RF_NullMapMissingGlobalValues)
360 return getVM()[V] = const_cast<Value *>(V);
363 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
364 // Inline asm may need *type* remapping.
365 FunctionType *NewTy = IA->getFunctionType();
367 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
369 if (NewTy != IA->getFunctionType())
370 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
371 IA->hasSideEffects(), IA->isAlignStack(),
372 IA->getDialect(), IA->canThrow());
375 return getVM()[V] = const_cast<Value *>(V);
378 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
379 const Metadata *MD = MDV->getMetadata();
381 if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
382 // Look through to grab the local value.
383 if (Value *LV = mapValue(LAM->getValue())) {
384 if (V == LAM->getValue())
385 return const_cast<Value *>(V);
386 return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV));
389 // FIXME: always return nullptr once Verifier::verifyDominatesUse()
390 // ensures metadata operands only reference defined SSA values.
391 return (Flags & RF_IgnoreMissingLocals)
393 : MetadataAsValue::get(V->getContext(),
394 MDTuple::get(V->getContext(), None));
396 if (auto *AL = dyn_cast<DIArgList>(MD)) {
397 SmallVector<ValueAsMetadata *, 4> MappedArgs;
398 for (auto *VAM : AL->getArgs()) {
399 // Map both Local and Constant VAMs here; they will both ultimately
400 // be mapped via mapValue (apart from constants when we have no
401 // module level changes, which have an identity mapping).
402 if ((Flags & RF_NoModuleLevelChanges) && isa<ConstantAsMetadata>(VAM)) {
403 MappedArgs.push_back(VAM);
404 } else if (Value *LV = mapValue(VAM->getValue())) {
405 MappedArgs.push_back(
406 LV == VAM->getValue() ? VAM : ValueAsMetadata::get(LV));
408 // If we cannot map the value, set the argument as undef.
409 MappedArgs.push_back(ValueAsMetadata::get(
410 UndefValue::get(VAM->getValue()->getType())));
413 return MetadataAsValue::get(V->getContext(),
414 DIArgList::get(V->getContext(), MappedArgs));
417 // If this is a module-level metadata and we know that nothing at the module
418 // level is changing, then use an identity mapping.
419 if (Flags & RF_NoModuleLevelChanges)
420 return getVM()[V] = const_cast<Value *>(V);
422 // Map the metadata and turn it into a value.
423 auto *MappedMD = mapMetadata(MD);
425 return getVM()[V] = const_cast<Value *>(V);
426 return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
429 // Okay, this either must be a constant (which may or may not be mappable) or
430 // is something that is not in the mapping table.
431 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
435 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
436 return mapBlockAddress(*BA);
438 if (const auto *E = dyn_cast<DSOLocalEquivalent>(C)) {
439 auto *Val = mapValue(E->getGlobalValue());
440 GlobalValue *GV = dyn_cast<GlobalValue>(Val);
442 return getVM()[E] = DSOLocalEquivalent::get(GV);
444 auto *Func = cast<Function>(Val->stripPointerCastsAndAliases());
445 Type *NewTy = E->getType();
447 NewTy = TypeMapper->remapType(NewTy);
448 return getVM()[E] = llvm::ConstantExpr::getBitCast(
449 DSOLocalEquivalent::get(Func), NewTy);
452 auto mapValueOrNull = [this](Value *V) {
453 auto Mapped = mapValue(V);
454 assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
455 "Unexpected null mapping for constant operand without "
456 "NullMapMissingGlobalValues flag");
460 // Otherwise, we have some other constant to remap. Start by checking to see
461 // if all operands have an identity remapping.
462 unsigned OpNo = 0, NumOperands = C->getNumOperands();
463 Value *Mapped = nullptr;
464 for (; OpNo != NumOperands; ++OpNo) {
465 Value *Op = C->getOperand(OpNo);
466 Mapped = mapValueOrNull(Op);
473 // See if the type mapper wants to remap the type as well.
474 Type *NewTy = C->getType();
476 NewTy = TypeMapper->remapType(NewTy);
478 // If the result type and all operands match up, then just insert an identity
480 if (OpNo == NumOperands && NewTy == C->getType())
481 return getVM()[V] = C;
483 // Okay, we need to create a new constant. We've already processed some or
484 // all of the operands, set them all up now.
485 SmallVector<Constant*, 8> Ops;
486 Ops.reserve(NumOperands);
487 for (unsigned j = 0; j != OpNo; ++j)
488 Ops.push_back(cast<Constant>(C->getOperand(j)));
490 // If one of the operands mismatch, push it and the other mapped operands.
491 if (OpNo != NumOperands) {
492 Ops.push_back(cast<Constant>(Mapped));
494 // Map the rest of the operands that aren't processed yet.
495 for (++OpNo; OpNo != NumOperands; ++OpNo) {
496 Mapped = mapValueOrNull(C->getOperand(OpNo));
499 Ops.push_back(cast<Constant>(Mapped));
502 Type *NewSrcTy = nullptr;
504 if (auto *GEPO = dyn_cast<GEPOperator>(C))
505 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
507 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
508 return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
509 if (isa<ConstantArray>(C))
510 return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
511 if (isa<ConstantStruct>(C))
512 return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
513 if (isa<ConstantVector>(C))
514 return getVM()[V] = ConstantVector::get(Ops);
515 // If this is a no-operand constant, it must be because the type was remapped.
516 if (isa<UndefValue>(C))
517 return getVM()[V] = UndefValue::get(NewTy);
518 if (isa<ConstantAggregateZero>(C))
519 return getVM()[V] = ConstantAggregateZero::get(NewTy);
520 assert(isa<ConstantPointerNull>(C));
521 return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
524 Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
525 Function *F = cast<Function>(mapValue(BA.getFunction()));
527 // F may not have materialized its initializer. In that case, create a
528 // dummy basic block for now, and replace it once we've materialized all
532 DelayedBBs.push_back(DelayedBasicBlock(BA));
533 BB = DelayedBBs.back().TempBB.get();
535 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
538 return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
541 Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
542 getVM().MD()[Key].reset(Val);
546 Metadata *Mapper::mapToSelf(const Metadata *MD) {
547 return mapToMetadata(MD, const_cast<Metadata *>(MD));
550 Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
554 if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
556 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
557 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
558 M.getVM().getMappedMD(Op)) &&
559 "Expected Value to be memoized");
561 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
562 "Expected result to be memoized");
567 const MDNode &N = *cast<MDNode>(Op);
569 return mapDistinctNode(N);
573 MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
574 assert(N.isDistinct() && "Expected a distinct node");
575 assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
576 Metadata *NewM = nullptr;
578 if (M.Flags & RF_ReuseAndMutateDistinctMDs) {
579 NewM = M.mapToSelf(&N);
581 NewM = MDNode::replaceWithDistinct(N.clone());
582 LLVM_DEBUG(dbgs() << "\nMap " << N << "\n"
583 << "To " << *NewM << "\n\n");
584 M.mapToMetadata(&N, NewM);
586 DistinctWorklist.push_back(cast<MDNode>(NewM));
588 return DistinctWorklist.back();
591 static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
593 if (CMD.getValue() == MappedV)
594 return const_cast<ConstantAsMetadata *>(&CMD);
595 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
598 Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
602 if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
605 if (isa<MDString>(Op))
606 return const_cast<Metadata *>(Op);
608 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
609 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
614 Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
615 auto Where = Info.find(&Op);
616 assert(Where != Info.end() && "Expected a valid reference");
618 auto &OpD = Where->second;
622 // Lazily construct a temporary node.
623 if (!OpD.Placeholder)
624 OpD.Placeholder = Op.clone();
626 return *OpD.Placeholder;
629 template <class OperandMapper>
630 void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
631 assert(!N.isUniqued() && "Expected distinct or temporary nodes");
632 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
633 Metadata *Old = N.getOperand(I);
634 Metadata *New = mapOperand(Old);
636 LLVM_DEBUG(dbgs() << "Replacing Op " << Old << " with " << New << " in "
640 N.replaceOperandWith(I, New);
646 /// An entry in the worklist for the post-order traversal.
647 struct POTWorklistEntry {
648 MDNode *N; ///< Current node.
649 MDNode::op_iterator Op; ///< Current operand of \c N.
651 /// Keep a flag of whether operands have changed in the worklist to avoid
652 /// hitting the map in \a UniquedGraph.
653 bool HasChanged = false;
655 POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
658 } // end anonymous namespace
660 bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
661 assert(G.Info.empty() && "Expected a fresh traversal");
662 assert(FirstN.isUniqued() && "Expected uniqued node in POT");
664 // Construct a post-order traversal of the uniqued subgraph under FirstN.
665 bool AnyChanges = false;
666 SmallVector<POTWorklistEntry, 16> Worklist;
667 Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
668 (void)G.Info[&FirstN];
669 while (!Worklist.empty()) {
670 // Start or continue the traversal through the this node's operands.
671 auto &WE = Worklist.back();
672 if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
673 // Push a new node to traverse first.
674 Worklist.push_back(POTWorklistEntry(*N));
678 // Push the node onto the POT.
679 assert(WE.N->isUniqued() && "Expected only uniqued nodes");
680 assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
681 auto &D = G.Info[WE.N];
682 AnyChanges |= D.HasChanged = WE.HasChanged;
684 G.POT.push_back(WE.N);
686 // Pop the node off the worklist.
692 MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
693 MDNode::op_iterator E, bool &HasChanged) {
695 Metadata *Op = *I++; // Increment even on early return.
696 if (Optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
697 // Check if the operand changes.
698 HasChanged |= Op != *MappedOp;
702 // A uniqued metadata node.
703 MDNode &OpN = *cast<MDNode>(Op);
704 assert(OpN.isUniqued() &&
705 "Only uniqued operands cannot be mapped immediately");
706 if (G.Info.insert(std::make_pair(&OpN, Data())).second)
707 return &OpN; // This is a new one. Return it.
712 void MDNodeMapper::UniquedGraph::propagateChanges() {
716 for (MDNode *N : POT) {
721 if (llvm::none_of(N->operands(), [&](const Metadata *Op) {
722 auto Where = Info.find(Op);
723 return Where != Info.end() && Where->second.HasChanged;
727 AnyChanges = D.HasChanged = true;
729 } while (AnyChanges);
732 void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
733 // Construct uniqued nodes, building forward references as necessary.
734 SmallVector<MDNode *, 16> CyclicNodes;
735 for (auto *N : G.POT) {
738 // The node hasn't changed.
743 // Remember whether this node had a placeholder.
744 bool HadPlaceholder(D.Placeholder);
746 // Clone the uniqued node and remap the operands.
747 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
748 remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
749 if (Optional<Metadata *> MappedOp = getMappedOp(Old))
752 assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
753 return &G.getFwdReference(*cast<MDNode>(Old));
756 auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
757 if (N && NewN && N != NewN) {
758 LLVM_DEBUG(dbgs() << "\nMap " << *N << "\n"
759 << "To " << *NewN << "\n\n");
762 M.mapToMetadata(N, NewN);
764 // Nodes that were referenced out of order in the POT are involved in a
767 CyclicNodes.push_back(NewN);
771 for (auto *N : CyclicNodes)
772 if (!N->isResolved())
776 Metadata *MDNodeMapper::map(const MDNode &N) {
777 assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
778 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
779 "MDNodeMapper::map assumes module-level changes");
781 // Require resolved nodes whenever metadata might be remapped.
782 assert(N.isResolved() && "Unexpected unresolved node");
785 N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
786 while (!DistinctWorklist.empty())
787 remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
788 if (Optional<Metadata *> MappedOp = tryToMapOperand(Old))
790 return mapTopLevelUniquedNode(*cast<MDNode>(Old));
795 Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
796 assert(FirstN.isUniqued() && "Expected uniqued node");
798 // Create a post-order traversal of uniqued nodes under FirstN.
800 if (!createPOT(G, FirstN)) {
801 // Return early if no nodes have changed.
802 for (const MDNode *N : G.POT)
804 return &const_cast<MDNode &>(FirstN);
807 // Update graph with all nodes that have changed.
808 G.propagateChanges();
810 // Map all the nodes in the graph.
813 // Return the original node, remapped.
814 return *getMappedOp(&FirstN);
817 Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
818 // If the value already exists in the map, use it.
819 if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
822 if (isa<MDString>(MD))
823 return const_cast<Metadata *>(MD);
825 // This is a module-level metadata. If nothing at the module level is
826 // changing, use an identity mapping.
827 if ((Flags & RF_NoModuleLevelChanges))
828 return const_cast<Metadata *>(MD);
830 if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
831 // Don't memoize ConstantAsMetadata. Instead of lasting until the
832 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
833 // reference is destructed. These aren't super common, so the extra
834 // indirection isn't that expensive.
835 return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
838 assert(isa<MDNode>(MD) && "Expected a metadata node");
843 Metadata *Mapper::mapMetadata(const Metadata *MD) {
844 assert(MD && "Expected valid metadata");
845 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
847 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
850 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
853 void Mapper::flush() {
854 // Flush out the worklist of global values.
855 while (!Worklist.empty()) {
856 WorklistEntry E = Worklist.pop_back_val();
857 CurrentMCID = E.MCID;
859 case WorklistEntry::MapGlobalInit:
860 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
861 remapGlobalObjectMetadata(*E.Data.GVInit.GV);
863 case WorklistEntry::MapAppendingVar: {
864 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
865 // mapAppendingVariable call can change AppendingInits if initalizer for
866 // the variable depends on another appending global, because of that inits
867 // need to be extracted and updated before the call.
868 SmallVector<Constant *, 8> NewInits(
869 drop_begin(AppendingInits, PrefixSize));
870 AppendingInits.resize(PrefixSize);
871 mapAppendingVariable(*E.Data.AppendingGV.GV,
872 E.Data.AppendingGV.InitPrefix,
873 E.AppendingGVIsOldCtorDtor, makeArrayRef(NewInits));
876 case WorklistEntry::MapGlobalIndirectSymbol:
877 E.Data.GlobalIndirectSymbol.GIS->setIndirectSymbol(
878 mapConstant(E.Data.GlobalIndirectSymbol.Target));
880 case WorklistEntry::RemapFunction:
881 remapFunction(*E.Data.RemapF);
887 // Finish logic for block addresses now that all global values have been
889 while (!DelayedBBs.empty()) {
890 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
891 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
892 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
896 void Mapper::remapInstruction(Instruction *I) {
898 for (Use &Op : I->operands()) {
899 Value *V = mapValue(Op);
900 // If we aren't ignoring missing entries, assert that something happened.
904 assert((Flags & RF_IgnoreMissingLocals) &&
905 "Referenced value not in value map!");
908 // Remap phi nodes' incoming blocks.
909 if (PHINode *PN = dyn_cast<PHINode>(I)) {
910 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
911 Value *V = mapValue(PN->getIncomingBlock(i));
912 // If we aren't ignoring missing entries, assert that something happened.
914 PN->setIncomingBlock(i, cast<BasicBlock>(V));
916 assert((Flags & RF_IgnoreMissingLocals) &&
917 "Referenced block not in value map!");
921 // Remap attached metadata.
922 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
923 I->getAllMetadata(MDs);
924 for (const auto &MI : MDs) {
925 MDNode *Old = MI.second;
926 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
928 I->setMetadata(MI.first, New);
934 // If the instruction's type is being remapped, do so now.
935 if (auto *CB = dyn_cast<CallBase>(I)) {
936 SmallVector<Type *, 3> Tys;
937 FunctionType *FTy = CB->getFunctionType();
938 Tys.reserve(FTy->getNumParams());
939 for (Type *Ty : FTy->params())
940 Tys.push_back(TypeMapper->remapType(Ty));
941 CB->mutateFunctionType(FunctionType::get(
942 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
944 LLVMContext &C = CB->getContext();
945 AttributeList Attrs = CB->getAttributes();
946 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
947 for (Attribute::AttrKind TypedAttr :
948 {Attribute::ByVal, Attribute::StructRet, Attribute::ByRef,
949 Attribute::InAlloca}) {
950 if (Type *Ty = Attrs.getAttribute(i, TypedAttr).getValueAsType()) {
951 Attrs = Attrs.replaceAttributeType(C, i, TypedAttr,
952 TypeMapper->remapType(Ty));
957 CB->setAttributes(Attrs);
960 if (auto *AI = dyn_cast<AllocaInst>(I))
961 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
962 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
963 GEP->setSourceElementType(
964 TypeMapper->remapType(GEP->getSourceElementType()));
965 GEP->setResultElementType(
966 TypeMapper->remapType(GEP->getResultElementType()));
968 I->mutateType(TypeMapper->remapType(I->getType()));
971 void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
972 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
973 GO.getAllMetadata(MDs);
975 for (const auto &I : MDs)
976 GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
979 void Mapper::remapFunction(Function &F) {
980 // Remap the operands.
981 for (Use &Op : F.operands())
985 // Remap the metadata attachments.
986 remapGlobalObjectMetadata(F);
988 // Remap the argument types.
990 for (Argument &A : F.args())
991 A.mutateType(TypeMapper->remapType(A.getType()));
993 // Remap the instructions.
994 for (BasicBlock &BB : F)
995 for (Instruction &I : BB)
996 remapInstruction(&I);
999 void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
1001 ArrayRef<Constant *> NewMembers) {
1002 SmallVector<Constant *, 16> Elements;
1004 unsigned NumElements =
1005 cast<ArrayType>(InitPrefix->getType())->getNumElements();
1006 for (unsigned I = 0; I != NumElements; ++I)
1007 Elements.push_back(InitPrefix->getAggregateElement(I));
1010 PointerType *VoidPtrTy;
1012 if (IsOldCtorDtor) {
1013 // FIXME: This upgrade is done during linking to support the C API. See
1014 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
1015 VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
1016 auto &ST = *cast<StructType>(NewMembers.front()->getType());
1017 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
1018 EltTy = StructType::get(GV.getContext(), Tys, false);
1021 for (auto *V : NewMembers) {
1023 if (IsOldCtorDtor) {
1024 auto *S = cast<ConstantStruct>(V);
1025 auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
1026 auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
1027 Constant *Null = Constant::getNullValue(VoidPtrTy);
1028 NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
1030 NewV = cast_or_null<Constant>(mapValue(V));
1032 Elements.push_back(NewV);
1036 ConstantArray::get(cast<ArrayType>(GV.getValueType()), Elements));
1039 void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1041 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1042 assert(MCID < MCs.size() && "Invalid mapping context");
1045 WE.Kind = WorklistEntry::MapGlobalInit;
1047 WE.Data.GVInit.GV = &GV;
1048 WE.Data.GVInit.Init = &Init;
1049 Worklist.push_back(WE);
1052 void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1053 Constant *InitPrefix,
1055 ArrayRef<Constant *> NewMembers,
1057 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1058 assert(MCID < MCs.size() && "Invalid mapping context");
1061 WE.Kind = WorklistEntry::MapAppendingVar;
1063 WE.Data.AppendingGV.GV = &GV;
1064 WE.Data.AppendingGV.InitPrefix = InitPrefix;
1065 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1066 WE.AppendingGVNumNewMembers = NewMembers.size();
1067 Worklist.push_back(WE);
1068 AppendingInits.append(NewMembers.begin(), NewMembers.end());
1071 void Mapper::scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS,
1072 Constant &Target, unsigned MCID) {
1073 assert(AlreadyScheduled.insert(&GIS).second && "Should not reschedule");
1074 assert(MCID < MCs.size() && "Invalid mapping context");
1077 WE.Kind = WorklistEntry::MapGlobalIndirectSymbol;
1079 WE.Data.GlobalIndirectSymbol.GIS = &GIS;
1080 WE.Data.GlobalIndirectSymbol.Target = &Target;
1081 Worklist.push_back(WE);
1084 void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1085 assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1086 assert(MCID < MCs.size() && "Invalid mapping context");
1089 WE.Kind = WorklistEntry::RemapFunction;
1091 WE.Data.RemapF = &F;
1092 Worklist.push_back(WE);
1095 void Mapper::addFlags(RemapFlags Flags) {
1096 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1097 this->Flags = this->Flags | Flags;
1100 static Mapper *getAsMapper(void *pImpl) {
1101 return reinterpret_cast<Mapper *>(pImpl);
1106 class FlushingMapper {
1110 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1111 assert(!M.hasWorkToDo() && "Expected to be flushed");
1114 ~FlushingMapper() { M.flush(); }
1116 Mapper *operator->() const { return &M; }
1119 } // end anonymous namespace
1121 ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1122 ValueMapTypeRemapper *TypeMapper,
1123 ValueMaterializer *Materializer)
1124 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1126 ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1129 ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1130 ValueMaterializer *Materializer) {
1131 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1134 void ValueMapper::addFlags(RemapFlags Flags) {
1135 FlushingMapper(pImpl)->addFlags(Flags);
1138 Value *ValueMapper::mapValue(const Value &V) {
1139 return FlushingMapper(pImpl)->mapValue(&V);
1142 Constant *ValueMapper::mapConstant(const Constant &C) {
1143 return cast_or_null<Constant>(mapValue(C));
1146 Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1147 return FlushingMapper(pImpl)->mapMetadata(&MD);
1150 MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1151 return cast_or_null<MDNode>(mapMetadata(N));
1154 void ValueMapper::remapInstruction(Instruction &I) {
1155 FlushingMapper(pImpl)->remapInstruction(&I);
1158 void ValueMapper::remapFunction(Function &F) {
1159 FlushingMapper(pImpl)->remapFunction(F);
1162 void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1165 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1168 void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1169 Constant *InitPrefix,
1171 ArrayRef<Constant *> NewMembers,
1173 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1174 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1177 void ValueMapper::scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS,
1180 getAsMapper(pImpl)->scheduleMapGlobalIndirectSymbol(GIS, Target, MCID);
1183 void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1184 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);