LLVM 20.0.0git
ValueMapper.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/STLExtras.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"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/GlobalAlias.h"
28#include "llvm/IR/GlobalIFunc.h"
31#include "llvm/IR/InlineAsm.h"
32#include "llvm/IR/Instruction.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"
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(Old.getContext())) {}
65};
66
67struct WorklistEntry {
68 enum EntryKind {
69 MapGlobalInit,
70 MapAppendingVar,
71 MapAliasOrIFunc,
73 };
74 struct GVInitTy {
77 };
78 struct AppendingGVTy {
80 Constant *InitPrefix;
81 };
82 struct AliasOrIFuncTy {
83 GlobalValue *GV;
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 {
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
117 ValueMapTypeRemapper *TypeMapper;
118 unsigned CurrentMCID = 0;
122 SmallVector<Constant *, 16> AppendingInits;
123
124public:
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
173private:
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
190class 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 {
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.
220 SmallVector<MDNode *, 16> POTStorage;
221
222public:
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
245private:
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
339Value *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)) {
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
395 V->getContext(),
396 MDTuple::get(V->getContext(), std::nullopt));
397 }
398 if (auto *AL = dyn_cast<DIArgList>(MD)) {
400 for (auto *VAM : AL->getArgs()) {
401 // Map both Local and Constant VAMs here; they will both ultimately
402 // be mapped via mapValue. The exceptions are constants when we have no
403 // module level changes and locals when they have no existing mapped
404 // value and RF_IgnoreMissingLocals is set; these have identity
405 // mappings.
406 if ((Flags & RF_NoModuleLevelChanges) && isa<ConstantAsMetadata>(VAM)) {
407 MappedArgs.push_back(VAM);
408 } else if (Value *LV = mapValue(VAM->getValue())) {
409 MappedArgs.push_back(
410 LV == VAM->getValue() ? VAM : ValueAsMetadata::get(LV));
411 } else if ((Flags & RF_IgnoreMissingLocals) && isa<LocalAsMetadata>(VAM)) {
412 MappedArgs.push_back(VAM);
413 } else {
414 // If we cannot map the value, set the argument as undef.
416 UndefValue::get(VAM->getValue()->getType())));
417 }
418 }
419 return MetadataAsValue::get(V->getContext(),
420 DIArgList::get(V->getContext(), MappedArgs));
421 }
422
423 // If this is a module-level metadata and we know that nothing at the module
424 // level is changing, then use an identity mapping.
425 if (Flags & RF_NoModuleLevelChanges)
426 return getVM()[V] = const_cast<Value *>(V);
427
428 // Map the metadata and turn it into a value.
429 auto *MappedMD = mapMetadata(MD);
430 if (MD == MappedMD)
431 return getVM()[V] = const_cast<Value *>(V);
432 return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
433 }
434
435 // Okay, this either must be a constant (which may or may not be mappable) or
436 // is something that is not in the mapping table.
437 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
438 if (!C)
439 return nullptr;
440
441 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
442 return mapBlockAddress(*BA);
443
444 if (const auto *E = dyn_cast<DSOLocalEquivalent>(C)) {
445 auto *Val = mapValue(E->getGlobalValue());
446 GlobalValue *GV = dyn_cast<GlobalValue>(Val);
447 if (GV)
448 return getVM()[E] = DSOLocalEquivalent::get(GV);
449
450 auto *Func = cast<Function>(Val->stripPointerCastsAndAliases());
451 Type *NewTy = E->getType();
452 if (TypeMapper)
453 NewTy = TypeMapper->remapType(NewTy);
454 return getVM()[E] = llvm::ConstantExpr::getBitCast(
455 DSOLocalEquivalent::get(Func), NewTy);
456 }
457
458 if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
459 auto *Val = mapValue(NC->getGlobalValue());
460 GlobalValue *GV = cast<GlobalValue>(Val);
461 return getVM()[NC] = NoCFIValue::get(GV);
462 }
463
464 auto mapValueOrNull = [this](Value *V) {
465 auto Mapped = mapValue(V);
466 assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
467 "Unexpected null mapping for constant operand without "
468 "NullMapMissingGlobalValues flag");
469 return Mapped;
470 };
471
472 // Otherwise, we have some other constant to remap. Start by checking to see
473 // if all operands have an identity remapping.
474 unsigned OpNo = 0, NumOperands = C->getNumOperands();
475 Value *Mapped = nullptr;
476 for (; OpNo != NumOperands; ++OpNo) {
477 Value *Op = C->getOperand(OpNo);
478 Mapped = mapValueOrNull(Op);
479 if (!Mapped)
480 return nullptr;
481 if (Mapped != Op)
482 break;
483 }
484
485 // See if the type mapper wants to remap the type as well.
486 Type *NewTy = C->getType();
487 if (TypeMapper)
488 NewTy = TypeMapper->remapType(NewTy);
489
490 // If the result type and all operands match up, then just insert an identity
491 // mapping.
492 if (OpNo == NumOperands && NewTy == C->getType())
493 return getVM()[V] = C;
494
495 // Okay, we need to create a new constant. We've already processed some or
496 // all of the operands, set them all up now.
498 Ops.reserve(NumOperands);
499 for (unsigned j = 0; j != OpNo; ++j)
500 Ops.push_back(cast<Constant>(C->getOperand(j)));
501
502 // If one of the operands mismatch, push it and the other mapped operands.
503 if (OpNo != NumOperands) {
504 Ops.push_back(cast<Constant>(Mapped));
505
506 // Map the rest of the operands that aren't processed yet.
507 for (++OpNo; OpNo != NumOperands; ++OpNo) {
508 Mapped = mapValueOrNull(C->getOperand(OpNo));
509 if (!Mapped)
510 return nullptr;
511 Ops.push_back(cast<Constant>(Mapped));
512 }
513 }
514 Type *NewSrcTy = nullptr;
515 if (TypeMapper)
516 if (auto *GEPO = dyn_cast<GEPOperator>(C))
517 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
518
519 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
520 return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
521 if (isa<ConstantArray>(C))
522 return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
523 if (isa<ConstantStruct>(C))
524 return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
525 if (isa<ConstantVector>(C))
526 return getVM()[V] = ConstantVector::get(Ops);
527 // If this is a no-operand constant, it must be because the type was remapped.
528 if (isa<PoisonValue>(C))
529 return getVM()[V] = PoisonValue::get(NewTy);
530 if (isa<UndefValue>(C))
531 return getVM()[V] = UndefValue::get(NewTy);
532 if (isa<ConstantAggregateZero>(C))
533 return getVM()[V] = ConstantAggregateZero::get(NewTy);
534 if (isa<ConstantTargetNone>(C))
535 return getVM()[V] = Constant::getNullValue(NewTy);
536 assert(isa<ConstantPointerNull>(C));
537 return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
538}
539
540void Mapper::remapDbgRecord(DbgRecord &DR) {
541 // Remap DILocations.
542 auto *MappedDILoc = mapMetadata(DR.getDebugLoc());
543 DR.setDebugLoc(DebugLoc(cast<DILocation>(MappedDILoc)));
544
545 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
546 // Remap labels.
547 DLR->setLabel(cast<DILabel>(mapMetadata(DLR->getLabel())));
548 return;
549 }
550
551 DbgVariableRecord &V = cast<DbgVariableRecord>(DR);
552 // Remap variables.
553 auto *MappedVar = mapMetadata(V.getVariable());
554 V.setVariable(cast<DILocalVariable>(MappedVar));
555
556 bool IgnoreMissingLocals = Flags & RF_IgnoreMissingLocals;
557
558 if (V.isDbgAssign()) {
559 auto *NewAddr = mapValue(V.getAddress());
560 if (!IgnoreMissingLocals && !NewAddr)
561 V.setKillAddress();
562 else if (NewAddr)
563 V.setAddress(NewAddr);
564 V.setAssignId(cast<DIAssignID>(mapMetadata(V.getAssignID())));
565 }
566
567 // Find Value operands and remap those.
568 SmallVector<Value *, 4> Vals(V.location_ops());
570 for (Value *Val : Vals)
571 NewVals.push_back(mapValue(Val));
572
573 // If there are no changes to the Value operands, finished.
574 if (Vals == NewVals)
575 return;
576
577 // Otherwise, do some replacement.
578 if (!IgnoreMissingLocals && llvm::is_contained(NewVals, nullptr)) {
579 V.setKillLocation();
580 } else {
581 // Either we have all non-empty NewVals, or we're permitted to ignore
582 // missing locals.
583 for (unsigned int I = 0; I < Vals.size(); ++I)
584 if (NewVals[I])
585 V.replaceVariableLocationOp(I, NewVals[I]);
586 }
587}
588
589Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
590 Function *F = cast<Function>(mapValue(BA.getFunction()));
591
592 // F may not have materialized its initializer. In that case, create a
593 // dummy basic block for now, and replace it once we've materialized all
594 // the initializers.
595 BasicBlock *BB;
596 if (F->empty()) {
597 DelayedBBs.push_back(DelayedBasicBlock(BA));
598 BB = DelayedBBs.back().TempBB.get();
599 } else {
600 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
601 }
602
603 return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
604}
605
606Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
607 getVM().MD()[Key].reset(Val);
608 return Val;
609}
610
611Metadata *Mapper::mapToSelf(const Metadata *MD) {
612 return mapToMetadata(MD, const_cast<Metadata *>(MD));
613}
614
615std::optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
616 if (!Op)
617 return nullptr;
618
619 if (std::optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
620#ifndef NDEBUG
621 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
622 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
623 M.getVM().getMappedMD(Op)) &&
624 "Expected Value to be memoized");
625 else
626 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
627 "Expected result to be memoized");
628#endif
629 return *MappedOp;
630 }
631
632 const MDNode &N = *cast<MDNode>(Op);
633 if (N.isDistinct())
634 return mapDistinctNode(N);
635 return std::nullopt;
636}
637
638MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
639 assert(N.isDistinct() && "Expected a distinct node");
640 assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
641 Metadata *NewM = nullptr;
642
643 if (M.Flags & RF_ReuseAndMutateDistinctMDs) {
644 NewM = M.mapToSelf(&N);
645 } else {
646 NewM = MDNode::replaceWithDistinct(N.clone());
647 LLVM_DEBUG(dbgs() << "\nMap " << N << "\n"
648 << "To " << *NewM << "\n\n");
649 M.mapToMetadata(&N, NewM);
650 }
651 DistinctWorklist.push_back(cast<MDNode>(NewM));
652
653 return DistinctWorklist.back();
654}
655
657 Value *MappedV) {
658 if (CMD.getValue() == MappedV)
659 return const_cast<ConstantAsMetadata *>(&CMD);
660 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
661}
662
663std::optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
664 if (!Op)
665 return nullptr;
666
667 if (std::optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
668 return *MappedOp;
669
670 if (isa<MDString>(Op))
671 return const_cast<Metadata *>(Op);
672
673 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
674 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
675
676 return std::nullopt;
677}
678
679Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
680 auto Where = Info.find(&Op);
681 assert(Where != Info.end() && "Expected a valid reference");
682
683 auto &OpD = Where->second;
684 if (!OpD.HasChanged)
685 return Op;
686
687 // Lazily construct a temporary node.
688 if (!OpD.Placeholder)
689 OpD.Placeholder = Op.clone();
690
691 return *OpD.Placeholder;
692}
693
694template <class OperandMapper>
695void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
696 assert(!N.isUniqued() && "Expected distinct or temporary nodes");
697 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
698 Metadata *Old = N.getOperand(I);
699 Metadata *New = mapOperand(Old);
700 if (Old != New)
701 LLVM_DEBUG(dbgs() << "Replacing Op " << Old << " with " << New << " in "
702 << N << "\n");
703
704 if (Old != New)
705 N.replaceOperandWith(I, New);
706 }
707}
708
709namespace {
710
711/// An entry in the worklist for the post-order traversal.
712struct POTWorklistEntry {
713 MDNode *N; ///< Current node.
714 MDNode::op_iterator Op; ///< Current operand of \c N.
715
716 /// Keep a flag of whether operands have changed in the worklist to avoid
717 /// hitting the map in \a UniquedGraph.
718 bool HasChanged = false;
719
720 POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
721};
722
723} // end anonymous namespace
724
725bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
726 assert(G.Info.empty() && "Expected a fresh traversal");
727 assert(FirstN.isUniqued() && "Expected uniqued node in POT");
728
729 // Construct a post-order traversal of the uniqued subgraph under FirstN.
730 bool AnyChanges = false;
732 Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
733 (void)G.Info[&FirstN];
734 while (!Worklist.empty()) {
735 // Start or continue the traversal through the this node's operands.
736 auto &WE = Worklist.back();
737 if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
738 // Push a new node to traverse first.
739 Worklist.push_back(POTWorklistEntry(*N));
740 continue;
741 }
742
743 // Push the node onto the POT.
744 assert(WE.N->isUniqued() && "Expected only uniqued nodes");
745 assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
746 auto &D = G.Info[WE.N];
747 AnyChanges |= D.HasChanged = WE.HasChanged;
748 D.ID = G.POT.size();
749 G.POT.push_back(WE.N);
750
751 // Pop the node off the worklist.
752 Worklist.pop_back();
753 }
754 return AnyChanges;
755}
756
757MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
758 MDNode::op_iterator E, bool &HasChanged) {
759 while (I != E) {
760 Metadata *Op = *I++; // Increment even on early return.
761 if (std::optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
762 // Check if the operand changes.
763 HasChanged |= Op != *MappedOp;
764 continue;
765 }
766
767 // A uniqued metadata node.
768 MDNode &OpN = *cast<MDNode>(Op);
769 assert(OpN.isUniqued() &&
770 "Only uniqued operands cannot be mapped immediately");
771 if (G.Info.insert(std::make_pair(&OpN, Data())).second)
772 return &OpN; // This is a new one. Return it.
773 }
774 return nullptr;
775}
776
777void MDNodeMapper::UniquedGraph::propagateChanges() {
778 bool AnyChanges;
779 do {
780 AnyChanges = false;
781 for (MDNode *N : POT) {
782 auto &D = Info[N];
783 if (D.HasChanged)
784 continue;
785
786 if (llvm::none_of(N->operands(), [&](const Metadata *Op) {
787 auto Where = Info.find(Op);
788 return Where != Info.end() && Where->second.HasChanged;
789 }))
790 continue;
791
792 AnyChanges = D.HasChanged = true;
793 }
794 } while (AnyChanges);
795}
796
797void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
798 // Construct uniqued nodes, building forward references as necessary.
799 SmallVector<MDNode *, 16> CyclicNodes;
800 for (auto *N : G.POT) {
801 auto &D = G.Info[N];
802 if (!D.HasChanged) {
803 // The node hasn't changed.
804 M.mapToSelf(N);
805 continue;
806 }
807
808 // Remember whether this node had a placeholder.
809 bool HadPlaceholder(D.Placeholder);
810
811 // Clone the uniqued node and remap the operands.
812 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
813 remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
814 if (std::optional<Metadata *> MappedOp = getMappedOp(Old))
815 return *MappedOp;
816 (void)D;
817 assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
818 return &G.getFwdReference(*cast<MDNode>(Old));
819 });
820
821 auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
822 if (N && NewN && N != NewN) {
823 LLVM_DEBUG(dbgs() << "\nMap " << *N << "\n"
824 << "To " << *NewN << "\n\n");
825 }
826
827 M.mapToMetadata(N, NewN);
828
829 // Nodes that were referenced out of order in the POT are involved in a
830 // uniquing cycle.
831 if (HadPlaceholder)
832 CyclicNodes.push_back(NewN);
833 }
834
835 // Resolve cycles.
836 for (auto *N : CyclicNodes)
837 if (!N->isResolved())
838 N->resolveCycles();
839}
840
841Metadata *MDNodeMapper::map(const MDNode &N) {
842 assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
843 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
844 "MDNodeMapper::map assumes module-level changes");
845
846 // Require resolved nodes whenever metadata might be remapped.
847 assert(N.isResolved() && "Unexpected unresolved node");
848
849 Metadata *MappedN =
850 N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
851 while (!DistinctWorklist.empty())
852 remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
853 if (std::optional<Metadata *> MappedOp = tryToMapOperand(Old))
854 return *MappedOp;
855 return mapTopLevelUniquedNode(*cast<MDNode>(Old));
856 });
857 return MappedN;
858}
859
860Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
861 assert(FirstN.isUniqued() && "Expected uniqued node");
862
863 // Create a post-order traversal of uniqued nodes under FirstN.
864 UniquedGraph G;
865 if (!createPOT(G, FirstN)) {
866 // Return early if no nodes have changed.
867 for (const MDNode *N : G.POT)
868 M.mapToSelf(N);
869 return &const_cast<MDNode &>(FirstN);
870 }
871
872 // Update graph with all nodes that have changed.
873 G.propagateChanges();
874
875 // Map all the nodes in the graph.
876 mapNodesInPOT(G);
877
878 // Return the original node, remapped.
879 return *getMappedOp(&FirstN);
880}
881
882std::optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
883 // If the value already exists in the map, use it.
884 if (std::optional<Metadata *> NewMD = getVM().getMappedMD(MD))
885 return *NewMD;
886
887 if (isa<MDString>(MD))
888 return const_cast<Metadata *>(MD);
889
890 // This is a module-level metadata. If nothing at the module level is
891 // changing, use an identity mapping.
892 if ((Flags & RF_NoModuleLevelChanges))
893 return const_cast<Metadata *>(MD);
894
895 if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
896 // Don't memoize ConstantAsMetadata. Instead of lasting until the
897 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
898 // reference is destructed. These aren't super common, so the extra
899 // indirection isn't that expensive.
900 return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
901 }
902
903 assert(isa<MDNode>(MD) && "Expected a metadata node");
904
905 return std::nullopt;
906}
907
908Metadata *Mapper::mapMetadata(const Metadata *MD) {
909 assert(MD && "Expected valid metadata");
910 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
911
912 if (std::optional<Metadata *> NewMD = mapSimpleMetadata(MD))
913 return *NewMD;
914
915 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
916}
917
918void Mapper::flush() {
919 // Flush out the worklist of global values.
920 while (!Worklist.empty()) {
921 WorklistEntry E = Worklist.pop_back_val();
922 CurrentMCID = E.MCID;
923 switch (E.Kind) {
924 case WorklistEntry::MapGlobalInit:
925 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
926 remapGlobalObjectMetadata(*E.Data.GVInit.GV);
927 break;
928 case WorklistEntry::MapAppendingVar: {
929 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
930 // mapAppendingVariable call can change AppendingInits if initalizer for
931 // the variable depends on another appending global, because of that inits
932 // need to be extracted and updated before the call.
934 drop_begin(AppendingInits, PrefixSize));
935 AppendingInits.resize(PrefixSize);
936 mapAppendingVariable(*E.Data.AppendingGV.GV,
937 E.Data.AppendingGV.InitPrefix,
938 E.AppendingGVIsOldCtorDtor, ArrayRef(NewInits));
939 break;
940 }
941 case WorklistEntry::MapAliasOrIFunc: {
942 GlobalValue *GV = E.Data.AliasOrIFunc.GV;
943 Constant *Target = mapConstant(E.Data.AliasOrIFunc.Target);
944 if (auto *GA = dyn_cast<GlobalAlias>(GV))
945 GA->setAliasee(Target);
946 else if (auto *GI = dyn_cast<GlobalIFunc>(GV))
947 GI->setResolver(Target);
948 else
949 llvm_unreachable("Not alias or ifunc");
950 break;
951 }
952 case WorklistEntry::RemapFunction:
953 remapFunction(*E.Data.RemapF);
954 break;
955 }
956 }
957 CurrentMCID = 0;
958
959 // Finish logic for block addresses now that all global values have been
960 // handled.
961 while (!DelayedBBs.empty()) {
962 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
963 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
964 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
965 }
966}
967
968void Mapper::remapInstruction(Instruction *I) {
969 // Remap operands.
970 for (Use &Op : I->operands()) {
971 Value *V = mapValue(Op);
972 // If we aren't ignoring missing entries, assert that something happened.
973 if (V)
974 Op = V;
975 else
976 assert((Flags & RF_IgnoreMissingLocals) &&
977 "Referenced value not in value map!");
978 }
979
980 // Remap phi nodes' incoming blocks.
981 if (PHINode *PN = dyn_cast<PHINode>(I)) {
982 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
983 Value *V = mapValue(PN->getIncomingBlock(i));
984 // If we aren't ignoring missing entries, assert that something happened.
985 if (V)
986 PN->setIncomingBlock(i, cast<BasicBlock>(V));
987 else
988 assert((Flags & RF_IgnoreMissingLocals) &&
989 "Referenced block not in value map!");
990 }
991 }
992
993 // Remap attached metadata.
995 I->getAllMetadata(MDs);
996 for (const auto &MI : MDs) {
997 MDNode *Old = MI.second;
998 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
999 if (New != Old)
1000 I->setMetadata(MI.first, New);
1001 }
1002
1003 if (!TypeMapper)
1004 return;
1005
1006 // If the instruction's type is being remapped, do so now.
1007 if (auto *CB = dyn_cast<CallBase>(I)) {
1009 FunctionType *FTy = CB->getFunctionType();
1010 Tys.reserve(FTy->getNumParams());
1011 for (Type *Ty : FTy->params())
1012 Tys.push_back(TypeMapper->remapType(Ty));
1013 CB->mutateFunctionType(FunctionType::get(
1014 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
1015
1016 LLVMContext &C = CB->getContext();
1017 AttributeList Attrs = CB->getAttributes();
1018 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
1019 for (int AttrIdx = Attribute::FirstTypeAttr;
1020 AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
1021 Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
1022 if (Type *Ty =
1023 Attrs.getAttributeAtIndex(i, TypedAttr).getValueAsType()) {
1024 Attrs = Attrs.replaceAttributeTypeAtIndex(C, i, TypedAttr,
1025 TypeMapper->remapType(Ty));
1026 break;
1027 }
1028 }
1029 }
1030 CB->setAttributes(Attrs);
1031 return;
1032 }
1033 if (auto *AI = dyn_cast<AllocaInst>(I))
1034 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
1035 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
1036 GEP->setSourceElementType(
1037 TypeMapper->remapType(GEP->getSourceElementType()));
1038 GEP->setResultElementType(
1039 TypeMapper->remapType(GEP->getResultElementType()));
1040 }
1041 I->mutateType(TypeMapper->remapType(I->getType()));
1042}
1043
1044void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
1046 GO.getAllMetadata(MDs);
1047 GO.clearMetadata();
1048 for (const auto &I : MDs)
1049 GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
1050}
1051
1052void Mapper::remapFunction(Function &F) {
1053 // Remap the operands.
1054 for (Use &Op : F.operands())
1055 if (Op)
1056 Op = mapValue(Op);
1057
1058 // Remap the metadata attachments.
1059 remapGlobalObjectMetadata(F);
1060
1061 // Remap the argument types.
1062 if (TypeMapper)
1063 for (Argument &A : F.args())
1064 A.mutateType(TypeMapper->remapType(A.getType()));
1065
1066 // Remap the instructions.
1067 for (BasicBlock &BB : F) {
1068 for (Instruction &I : BB) {
1069 remapInstruction(&I);
1070 for (DbgRecord &DR : I.getDbgRecordRange())
1071 remapDbgRecord(DR);
1072 }
1073 }
1074}
1075
1076void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
1077 bool IsOldCtorDtor,
1078 ArrayRef<Constant *> NewMembers) {
1080 if (InitPrefix) {
1081 unsigned NumElements =
1082 cast<ArrayType>(InitPrefix->getType())->getNumElements();
1083 for (unsigned I = 0; I != NumElements; ++I)
1084 Elements.push_back(InitPrefix->getAggregateElement(I));
1085 }
1086
1087 PointerType *VoidPtrTy;
1088 Type *EltTy;
1089 if (IsOldCtorDtor) {
1090 // FIXME: This upgrade is done during linking to support the C API. See
1091 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
1092 VoidPtrTy = PointerType::getUnqual(GV.getContext());
1093 auto &ST = *cast<StructType>(NewMembers.front()->getType());
1094 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
1095 EltTy = StructType::get(GV.getContext(), Tys, false);
1096 }
1097
1098 for (auto *V : NewMembers) {
1099 Constant *NewV;
1100 if (IsOldCtorDtor) {
1101 auto *S = cast<ConstantStruct>(V);
1102 auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
1103 auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
1104 Constant *Null = Constant::getNullValue(VoidPtrTy);
1105 NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
1106 } else {
1107 NewV = cast_or_null<Constant>(mapValue(V));
1108 }
1109 Elements.push_back(NewV);
1110 }
1111
1112 GV.setInitializer(
1113 ConstantArray::get(cast<ArrayType>(GV.getValueType()), Elements));
1114}
1115
1116void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1117 unsigned MCID) {
1118 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1119 assert(MCID < MCs.size() && "Invalid mapping context");
1120
1121 WorklistEntry WE;
1122 WE.Kind = WorklistEntry::MapGlobalInit;
1123 WE.MCID = MCID;
1124 WE.Data.GVInit.GV = &GV;
1125 WE.Data.GVInit.Init = &Init;
1126 Worklist.push_back(WE);
1127}
1128
1129void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1130 Constant *InitPrefix,
1131 bool IsOldCtorDtor,
1132 ArrayRef<Constant *> NewMembers,
1133 unsigned MCID) {
1134 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1135 assert(MCID < MCs.size() && "Invalid mapping context");
1136
1137 WorklistEntry WE;
1138 WE.Kind = WorklistEntry::MapAppendingVar;
1139 WE.MCID = MCID;
1140 WE.Data.AppendingGV.GV = &GV;
1141 WE.Data.AppendingGV.InitPrefix = InitPrefix;
1142 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1143 WE.AppendingGVNumNewMembers = NewMembers.size();
1144 Worklist.push_back(WE);
1145 AppendingInits.append(NewMembers.begin(), NewMembers.end());
1146}
1147
1148void Mapper::scheduleMapAliasOrIFunc(GlobalValue &GV, Constant &Target,
1149 unsigned MCID) {
1150 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1151 assert((isa<GlobalAlias>(GV) || isa<GlobalIFunc>(GV)) &&
1152 "Should be alias or ifunc");
1153 assert(MCID < MCs.size() && "Invalid mapping context");
1154
1155 WorklistEntry WE;
1156 WE.Kind = WorklistEntry::MapAliasOrIFunc;
1157 WE.MCID = MCID;
1158 WE.Data.AliasOrIFunc.GV = &GV;
1159 WE.Data.AliasOrIFunc.Target = &Target;
1160 Worklist.push_back(WE);
1161}
1162
1163void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1164 assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1165 assert(MCID < MCs.size() && "Invalid mapping context");
1166
1167 WorklistEntry WE;
1168 WE.Kind = WorklistEntry::RemapFunction;
1169 WE.MCID = MCID;
1170 WE.Data.RemapF = &F;
1171 Worklist.push_back(WE);
1172}
1173
1174void Mapper::addFlags(RemapFlags Flags) {
1175 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1176 this->Flags = this->Flags | Flags;
1177}
1178
1179static Mapper *getAsMapper(void *pImpl) {
1180 return reinterpret_cast<Mapper *>(pImpl);
1181}
1182
1183namespace {
1184
1185class FlushingMapper {
1186 Mapper &M;
1187
1188public:
1189 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1190 assert(!M.hasWorkToDo() && "Expected to be flushed");
1191 }
1192
1193 ~FlushingMapper() { M.flush(); }
1194
1195 Mapper *operator->() const { return &M; }
1196};
1197
1198} // end anonymous namespace
1199
1201 ValueMapTypeRemapper *TypeMapper,
1202 ValueMaterializer *Materializer)
1203 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1204
1206
1207unsigned
1209 ValueMaterializer *Materializer) {
1210 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1211}
1212
1214 FlushingMapper(pImpl)->addFlags(Flags);
1215}
1216
1218 return FlushingMapper(pImpl)->mapValue(&V);
1219}
1220
1222 return cast_or_null<Constant>(mapValue(C));
1223}
1224
1226 return FlushingMapper(pImpl)->mapMetadata(&MD);
1227}
1228
1230 return cast_or_null<MDNode>(mapMetadata(N));
1231}
1232
1234 FlushingMapper(pImpl)->remapInstruction(&I);
1235}
1236
1238 FlushingMapper(pImpl)->remapDbgRecord(DR);
1239}
1240
1243 for (DbgRecord &DR : Range) {
1244 remapDbgRecord(M, DR);
1245 }
1246}
1247
1249 FlushingMapper(pImpl)->remapFunction(F);
1250}
1251
1253 FlushingMapper(pImpl)->remapGlobalObjectMetadata(GO);
1254}
1255
1257 Constant &Init,
1258 unsigned MCID) {
1259 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1260}
1261
1263 Constant *InitPrefix,
1264 bool IsOldCtorDtor,
1265 ArrayRef<Constant *> NewMembers,
1266 unsigned MCID) {
1267 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1268 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1269}
1270
1272 unsigned MCID) {
1273 getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GA, Aliasee, MCID);
1274}
1275
1277 unsigned MCID) {
1278 getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GI, Resolver, MCID);
1279}
1280
1282 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1283}
static unsigned getMappedOp(unsigned PseudoOp)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Hexagon Common GEP
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
while(!ToSimplify.empty())
LoopAnalysisManager LAM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry, DenseMap< VPValue *, VPValue * > &Old2NewVPValues)
Definition: VPlan.cpp:1187
static Mapper * getAsMapper(void *pImpl)
static ConstantAsMetadata * wrapConstantAsMetadata(const ConstantAsMetadata &CMD, Value *MappedV)
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
The address of a basic block.
Definition: Constants.h:890
Function * getFunction() const
Definition: Constants.h:918
BasicBlock * getBasicBlock() const
Definition: Constants.h:919
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1871
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1650
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1292
Constant * getValue() const
Definition: Metadata.h:536
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1097
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2295
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1800
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1357
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1399
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
static DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
static DSOLocalEquivalent * get(GlobalValue *GV)
Return a DSOLocalEquivalent for the specified global value.
Definition: Constants.cpp:1944
This class represents an Operation in the Expression.
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
void setDebugLoc(DebugLoc Loc)
Record of a variable value-assignment, aka a non instruction representation of the dbg....
A debug info location.
Definition: DebugLoc.h:33
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1484
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1528
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1566
Type * getValueType() const
Definition: GlobalValue.h:296
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:485
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition: InlineAsm.cpp:43
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithDistinct(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a distinct one.
Definition: Metadata.h:1311
bool isUniqued() const
Definition: Metadata.h:1251
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithUniqued(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a uniqued one.
Definition: Metadata.h:1301
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1499
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static NoCFIValue * get(GlobalValue *GV)
Return a NoCFIValue for the specified function.
Definition: Constants.cpp:2002
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1852
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2212
bool empty() const
Definition: SmallVector.h:95
size_t size() const
Definition: SmallVector.h:92
void reserve(size_type N)
Definition: SmallVector.h:677
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:697
void resize(size_type N)
Definition: SmallVector.h:652
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:361
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1833
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:501
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:472
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition: ValueMapper.h:41
virtual Type * remapType(Type *SrcTy)=0
The client should implement this method if they want to remap types while mapping values.
void remapDbgRecord(Module *M, DbgRecord &V)
void remapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range)
MDNode * mapMDNode(const MDNode &N)
Metadata * mapMetadata(const Metadata &MD)
void remapInstruction(Instruction &I)
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
void scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver, unsigned MappingContextID=0)
unsigned registerAlternateMappingContext(ValueToValueMapTy &VM, ValueMaterializer *Materializer=nullptr)
Register an alternate mapping context.
void remapFunction(Function &F)
Constant * mapConstant(const Constant &C)
ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, bool IsOldCtorDtor, ArrayRef< Constant * > NewMembers, unsigned MappingContextID=0)
void scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee, unsigned MappingContextID=0)
void remapGlobalObjectMetadata(GlobalObject &GO)
Value * mapValue(const Value &V)
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:54
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
A range adaptor for a pair of iterators.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Key
PAL metadata keys.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ CE
Windows NT (Windows on ARM)
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:236
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
RemapFlags
These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:70
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:94
@ RF_NullMapMissingGlobalValues
Any global values not in value map are mapped to null instead of mapping to self.
Definition: ValueMapper.h:104
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:76
@ RF_ReuseAndMutateDistinctMDs
Instruct the remapper to reuse and mutate distinct metadata (remapping them in place) instead of clon...
Definition: ValueMapper.h:100
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
DWARFExpression::Operation Op
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
void RemapFunction(Function &F, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Remap the operands, metadata, arguments, and instructions of a function.
Definition: ValueMapper.h:297
#define N
#define NC
Definition: regutils.h:42