LLVM  12.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/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"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #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 <cassert>
41 #include <limits>
42 #include <memory>
43 #include <utility>
44 
45 using namespace llvm;
46 
47 // Out of line method to get vtable etc for class.
48 void ValueMapTypeRemapper::anchor() {}
49 void ValueMaterializer::anchor() {}
50 
51 namespace {
52 
53 /// A basic block used in a BlockAddress whose function body is not yet
54 /// materialized.
55 struct DelayedBasicBlock {
56  BasicBlock *OldBB;
57  std::unique_ptr<BasicBlock> TempBB;
58 
59  DelayedBasicBlock(const BlockAddress &Old)
60  : OldBB(Old.getBasicBlock()),
61  TempBB(BasicBlock::Create(Old.getContext())) {}
62 };
63 
64 struct WorklistEntry {
65  enum EntryKind {
66  MapGlobalInit,
67  MapAppendingVar,
68  MapGlobalIndirectSymbol,
70  };
71  struct GVInitTy {
72  GlobalVariable *GV;
73  Constant *Init;
74  };
75  struct AppendingGVTy {
76  GlobalVariable *GV;
77  Constant *InitPrefix;
78  };
79  struct GlobalIndirectSymbolTy {
82  };
83 
84  unsigned Kind : 2;
85  unsigned MCID : 29;
86  unsigned AppendingGVIsOldCtorDtor : 1;
87  unsigned AppendingGVNumNewMembers;
88  union {
89  GVInitTy GVInit;
90  AppendingGVTy AppendingGV;
91  GlobalIndirectSymbolTy GlobalIndirectSymbol;
92  Function *RemapF;
93  } Data;
94 };
95 
96 struct MappingContext {
98  ValueMaterializer *Materializer = nullptr;
99 
100  /// Construct a MappingContext with a value map and materializer.
101  explicit MappingContext(ValueToValueMapTy &VM,
102  ValueMaterializer *Materializer = nullptr)
103  : VM(&VM), Materializer(Materializer) {}
104 };
105 
106 class Mapper {
107  friend class MDNodeMapper;
108 
109 #ifndef NDEBUG
110  DenseSet<GlobalValue *> AlreadyScheduled;
111 #endif
112 
113  RemapFlags Flags;
114  ValueMapTypeRemapper *TypeMapper;
115  unsigned CurrentMCID = 0;
119  SmallVector<Constant *, 16> AppendingInits;
120 
121 public:
122  Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
123  ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
124  : Flags(Flags), TypeMapper(TypeMapper),
125  MCs(1, MappingContext(VM, Materializer)) {}
126 
127  /// ValueMapper should explicitly call \a flush() before destruction.
128  ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
129 
130  bool hasWorkToDo() const { return !Worklist.empty(); }
131 
132  unsigned
133  registerAlternateMappingContext(ValueToValueMapTy &VM,
134  ValueMaterializer *Materializer = nullptr) {
135  MCs.push_back(MappingContext(VM, Materializer));
136  return MCs.size() - 1;
137  }
138 
139  void addFlags(RemapFlags Flags);
140 
141  void remapGlobalObjectMetadata(GlobalObject &GO);
142 
143  Value *mapValue(const Value *V);
144  void remapInstruction(Instruction *I);
145  void remapFunction(Function &F);
146 
147  Constant *mapConstant(const Constant *C) {
148  return cast_or_null<Constant>(mapValue(C));
149  }
150 
151  /// Map metadata.
152  ///
153  /// Find the mapping for MD. Guarantees that the return will be resolved
154  /// (not an MDNode, or MDNode::isResolved() returns true).
155  Metadata *mapMetadata(const Metadata *MD);
156 
157  void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
158  unsigned MCID);
159  void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
160  bool IsOldCtorDtor,
161  ArrayRef<Constant *> NewMembers,
162  unsigned MCID);
163  void scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS, Constant &Target,
164  unsigned MCID);
165  void scheduleRemapFunction(Function &F, unsigned MCID);
166 
167  void flush();
168 
169 private:
170  void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
171  bool IsOldCtorDtor,
172  ArrayRef<Constant *> NewMembers);
173 
174  ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
175  ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
176 
177  Value *mapBlockAddress(const BlockAddress &BA);
178 
179  /// Map metadata that doesn't require visiting operands.
180  Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
181 
182  Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
183  Metadata *mapToSelf(const Metadata *MD);
184 };
185 
186 class MDNodeMapper {
187  Mapper &M;
188 
189  /// Data about a node in \a UniquedGraph.
190  struct Data {
191  bool HasChanged = false;
193  TempMDNode Placeholder;
194  };
195 
196  /// A graph of uniqued nodes.
197  struct UniquedGraph {
199  SmallVector<MDNode *, 16> POT; // Post-order traversal.
200 
201  /// Propagate changed operands through the post-order traversal.
202  ///
203  /// Iteratively update \a Data::HasChanged for each node based on \a
204  /// Data::HasChanged of its operands, until fixed point.
205  void propagateChanges();
206 
207  /// Get a forward reference to a node to use as an operand.
208  Metadata &getFwdReference(MDNode &Op);
209  };
210 
211  /// Worklist of distinct nodes whose operands need to be remapped.
212  SmallVector<MDNode *, 16> DistinctWorklist;
213 
214  // Storage for a UniquedGraph.
216  SmallVector<MDNode *, 16> POTStorage;
217 
218 public:
219  MDNodeMapper(Mapper &M) : M(M) {}
220 
221  /// Map a metadata node (and its transitive operands).
222  ///
223  /// Map all the (unmapped) nodes in the subgraph under \c N. The iterative
224  /// algorithm handles distinct nodes and uniqued node subgraphs using
225  /// different strategies.
226  ///
227  /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
228  /// using \a mapDistinctNode(). Their mapping can always be computed
229  /// immediately without visiting operands, even if their operands change.
230  ///
231  /// The mapping for uniqued nodes depends on whether their operands change.
232  /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
233  /// a node to calculate uniqued node mappings in bulk. Distinct leafs are
234  /// added to \a DistinctWorklist with \a mapDistinctNode().
235  ///
236  /// After mapping \c N itself, this function remaps the operands of the
237  /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
238  /// N has been mapped.
239  Metadata *map(const MDNode &N);
240 
241 private:
242  /// Map a top-level uniqued node and the uniqued subgraph underneath it.
243  ///
244  /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
245  /// underneath \c FirstN and calculates the nodes' mapping. Each node uses
246  /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
247  /// operands uses the identity mapping.
248  ///
249  /// The algorithm works as follows:
250  ///
251  /// 1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
252  /// save the post-order traversal in the given \a UniquedGraph, tracking
253  /// nodes' operands change.
254  ///
255  /// 2. \a UniquedGraph::propagateChanges(): propagate changed operands
256  /// through the \a UniquedGraph until fixed point, following the rule
257  /// that if a node changes, any node that references must also change.
258  ///
259  /// 3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
260  /// (referencing new operands) where necessary.
261  Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
262 
263  /// Try to map the operand of an \a MDNode.
264  ///
265  /// If \c Op is already mapped, return the mapping. If it's not an \a
266  /// MDNode, compute and return the mapping. If it's a distinct \a MDNode,
267  /// return the result of \a mapDistinctNode().
268  ///
269  /// \return None if \c Op is an unmapped uniqued \a MDNode.
270  /// \post getMappedOp(Op) only returns None if this returns None.
271  Optional<Metadata *> tryToMapOperand(const Metadata *Op);
272 
273  /// Map a distinct node.
274  ///
275  /// Return the mapping for the distinct node \c N, saving the result in \a
276  /// DistinctWorklist for later remapping.
277  ///
278  /// \pre \c N is not yet mapped.
279  /// \pre \c N.isDistinct().
280  MDNode *mapDistinctNode(const MDNode &N);
281 
282  /// Get a previously mapped node.
284 
285  /// Create a post-order traversal of an unmapped uniqued node subgraph.
286  ///
287  /// This traverses the metadata graph deeply enough to map \c FirstN. It
288  /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
289  /// metadata that has already been mapped will not be part of the POT.
290  ///
291  /// Each node that has a changed operand from outside the graph (e.g., a
292  /// distinct node, an already-mapped uniqued node, or \a ConstantAsMetadata)
293  /// is marked with \a Data::HasChanged.
294  ///
295  /// \return \c true if any nodes in \c G have \a Data::HasChanged.
296  /// \post \c G.POT is a post-order traversal ending with \c FirstN.
297  /// \post \a Data::hasChanged in \c G.Info indicates whether any node needs
298  /// to change because of operands outside the graph.
299  bool createPOT(UniquedGraph &G, const MDNode &FirstN);
300 
301  /// Visit the operands of a uniqued node in the POT.
302  ///
303  /// Visit the operands in the range from \c I to \c E, returning the first
304  /// uniqued node we find that isn't yet in \c G. \c I is always advanced to
305  /// where to continue the loop through the operands.
306  ///
307  /// This sets \c HasChanged if any of the visited operands change.
308  MDNode *visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
309  MDNode::op_iterator E, bool &HasChanged);
310 
311  /// Map all the nodes in the given uniqued graph.
312  ///
313  /// This visits all the nodes in \c G in post-order, using the identity
314  /// mapping or creating a new node depending on \a Data::HasChanged.
315  ///
316  /// \pre \a getMappedOp() returns None for nodes in \c G, but not for any of
317  /// their operands outside of \c G.
318  /// \pre \a Data::HasChanged is true for a node in \c G iff any of its
319  /// operands have changed.
320  /// \post \a getMappedOp() returns the mapped node for every node in \c G.
321  void mapNodesInPOT(UniquedGraph &G);
322 
323  /// Remap a node's operands using the given functor.
324  ///
325  /// Iterate through the operands of \c N and update them in place using \c
326  /// mapOperand.
327  ///
328  /// \pre N.isDistinct() or N.isTemporary().
329  template <class OperandMapper>
330  void remapOperands(MDNode &N, OperandMapper mapOperand);
331 };
332 
333 } // end anonymous namespace
334 
335 Value *Mapper::mapValue(const Value *V) {
336  ValueToValueMapTy::iterator I = getVM().find(V);
337 
338  // If the value already exists in the map, use it.
339  if (I != getVM().end()) {
340  assert(I->second && "Unexpected null mapping");
341  return I->second;
342  }
343 
344  // If we have a materializer and it can materialize a value, use that.
345  if (auto *Materializer = getMaterializer()) {
346  if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
347  getVM()[V] = NewV;
348  return NewV;
349  }
350  }
351 
352  // Global values do not need to be seeded into the VM if they
353  // are using the identity mapping.
354  if (isa<GlobalValue>(V)) {
355  if (Flags & RF_NullMapMissingGlobalValues)
356  return nullptr;
357  return getVM()[V] = const_cast<Value *>(V);
358  }
359 
360  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
361  // Inline asm may need *type* remapping.
362  FunctionType *NewTy = IA->getFunctionType();
363  if (TypeMapper) {
364  NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
365 
366  if (NewTy != IA->getFunctionType())
367  V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
368  IA->hasSideEffects(), IA->isAlignStack(),
369  IA->getDialect());
370  }
371 
372  return getVM()[V] = const_cast<Value *>(V);
373  }
374 
375  if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
376  const Metadata *MD = MDV->getMetadata();
377 
378  if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
379  // Look through to grab the local value.
380  if (Value *LV = mapValue(LAM->getValue())) {
381  if (V == LAM->getValue())
382  return const_cast<Value *>(V);
384  }
385 
386  // FIXME: always return nullptr once Verifier::verifyDominatesUse()
387  // ensures metadata operands only reference defined SSA values.
388  return (Flags & RF_IgnoreMissingLocals)
389  ? nullptr
391  MDTuple::get(V->getContext(), None));
392  }
393 
394  // If this is a module-level metadata and we know that nothing at the module
395  // level is changing, then use an identity mapping.
396  if (Flags & RF_NoModuleLevelChanges)
397  return getVM()[V] = const_cast<Value *>(V);
398 
399  // Map the metadata and turn it into a value.
400  auto *MappedMD = mapMetadata(MD);
401  if (MD == MappedMD)
402  return getVM()[V] = const_cast<Value *>(V);
403  return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
404  }
405 
406  // Okay, this either must be a constant (which may or may not be mappable) or
407  // is something that is not in the mapping table.
408  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
409  if (!C)
410  return nullptr;
411 
412  if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
413  return mapBlockAddress(*BA);
414 
415  auto mapValueOrNull = [this](Value *V) {
416  auto Mapped = mapValue(V);
417  assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
418  "Unexpected null mapping for constant operand without "
419  "NullMapMissingGlobalValues flag");
420  return Mapped;
421  };
422 
423  // Otherwise, we have some other constant to remap. Start by checking to see
424  // if all operands have an identity remapping.
425  unsigned OpNo = 0, NumOperands = C->getNumOperands();
426  Value *Mapped = nullptr;
427  for (; OpNo != NumOperands; ++OpNo) {
428  Value *Op = C->getOperand(OpNo);
429  Mapped = mapValueOrNull(Op);
430  if (!Mapped)
431  return nullptr;
432  if (Mapped != Op)
433  break;
434  }
435 
436  // See if the type mapper wants to remap the type as well.
437  Type *NewTy = C->getType();
438  if (TypeMapper)
439  NewTy = TypeMapper->remapType(NewTy);
440 
441  // If the result type and all operands match up, then just insert an identity
442  // mapping.
443  if (OpNo == NumOperands && NewTy == C->getType())
444  return getVM()[V] = C;
445 
446  // Okay, we need to create a new constant. We've already processed some or
447  // all of the operands, set them all up now.
449  Ops.reserve(NumOperands);
450  for (unsigned j = 0; j != OpNo; ++j)
451  Ops.push_back(cast<Constant>(C->getOperand(j)));
452 
453  // If one of the operands mismatch, push it and the other mapped operands.
454  if (OpNo != NumOperands) {
455  Ops.push_back(cast<Constant>(Mapped));
456 
457  // Map the rest of the operands that aren't processed yet.
458  for (++OpNo; OpNo != NumOperands; ++OpNo) {
459  Mapped = mapValueOrNull(C->getOperand(OpNo));
460  if (!Mapped)
461  return nullptr;
462  Ops.push_back(cast<Constant>(Mapped));
463  }
464  }
465  Type *NewSrcTy = nullptr;
466  if (TypeMapper)
467  if (auto *GEPO = dyn_cast<GEPOperator>(C))
468  NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
469 
470  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
471  return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
472  if (isa<ConstantArray>(C))
473  return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
474  if (isa<ConstantStruct>(C))
475  return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
476  if (isa<ConstantVector>(C))
477  return getVM()[V] = ConstantVector::get(Ops);
478  // If this is a no-operand constant, it must be because the type was remapped.
479  if (isa<UndefValue>(C))
480  return getVM()[V] = UndefValue::get(NewTy);
481  if (isa<ConstantAggregateZero>(C))
482  return getVM()[V] = ConstantAggregateZero::get(NewTy);
483  assert(isa<ConstantPointerNull>(C));
484  return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
485 }
486 
487 Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
488  Function *F = cast<Function>(mapValue(BA.getFunction()));
489 
490  // F may not have materialized its initializer. In that case, create a
491  // dummy basic block for now, and replace it once we've materialized all
492  // the initializers.
493  BasicBlock *BB;
494  if (F->empty()) {
495  DelayedBBs.push_back(DelayedBasicBlock(BA));
496  BB = DelayedBBs.back().TempBB.get();
497  } else {
498  BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
499  }
500 
501  return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
502 }
503 
504 Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
505  getVM().MD()[Key].reset(Val);
506  return Val;
507 }
508 
509 Metadata *Mapper::mapToSelf(const Metadata *MD) {
510  return mapToMetadata(MD, const_cast<Metadata *>(MD));
511 }
512 
513 Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
514  if (!Op)
515  return nullptr;
516 
517  if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
518 #ifndef NDEBUG
519  if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
520  assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
521  M.getVM().getMappedMD(Op)) &&
522  "Expected Value to be memoized");
523  else
524  assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
525  "Expected result to be memoized");
526 #endif
527  return *MappedOp;
528  }
529 
530  const MDNode &N = *cast<MDNode>(Op);
531  if (N.isDistinct())
532  return mapDistinctNode(N);
533  return None;
534 }
535 
536 static Metadata *cloneOrBuildODR(const MDNode &N) {
537  auto *CT = dyn_cast<DICompositeType>(&N);
538  // If ODR type uniquing is enabled, we would have uniqued composite types
539  // with identifiers during bitcode reading, so we can just use CT.
540  if (CT && CT->getContext().isODRUniquingDebugTypes() &&
541  CT->getIdentifier() != "")
542  return const_cast<DICompositeType *>(CT);
543  return MDNode::replaceWithDistinct(N.clone());
544 }
545 
546 MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
547  assert(N.isDistinct() && "Expected a distinct node");
548  assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
549  DistinctWorklist.push_back(
550  cast<MDNode>((M.Flags & RF_MoveDistinctMDs)
551  ? M.mapToSelf(&N)
552  : M.mapToMetadata(&N, cloneOrBuildODR(N))));
553  return DistinctWorklist.back();
554 }
555 
557  Value *MappedV) {
558  if (CMD.getValue() == MappedV)
559  return const_cast<ConstantAsMetadata *>(&CMD);
560  return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
561 }
562 
564  if (!Op)
565  return nullptr;
566 
567  if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
568  return *MappedOp;
569 
570  if (isa<MDString>(Op))
571  return const_cast<Metadata *>(Op);
572 
573  if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
574  return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
575 
576  return None;
577 }
578 
579 Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
580  auto Where = Info.find(&Op);
581  assert(Where != Info.end() && "Expected a valid reference");
582 
583  auto &OpD = Where->second;
584  if (!OpD.HasChanged)
585  return Op;
586 
587  // Lazily construct a temporary node.
588  if (!OpD.Placeholder)
589  OpD.Placeholder = Op.clone();
590 
591  return *OpD.Placeholder;
592 }
593 
594 template <class OperandMapper>
595 void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
596  assert(!N.isUniqued() && "Expected distinct or temporary nodes");
597  for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
598  Metadata *Old = N.getOperand(I);
599  Metadata *New = mapOperand(Old);
600 
601  if (Old != New)
602  N.replaceOperandWith(I, New);
603  }
604 }
605 
606 namespace {
607 
608 /// An entry in the worklist for the post-order traversal.
609 struct POTWorklistEntry {
610  MDNode *N; ///< Current node.
611  MDNode::op_iterator Op; ///< Current operand of \c N.
612 
613  /// Keep a flag of whether operands have changed in the worklist to avoid
614  /// hitting the map in \a UniquedGraph.
615  bool HasChanged = false;
616 
617  POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
618 };
619 
620 } // end anonymous namespace
621 
622 bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
623  assert(G.Info.empty() && "Expected a fresh traversal");
624  assert(FirstN.isUniqued() && "Expected uniqued node in POT");
625 
626  // Construct a post-order traversal of the uniqued subgraph under FirstN.
627  bool AnyChanges = false;
629  Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
630  (void)G.Info[&FirstN];
631  while (!Worklist.empty()) {
632  // Start or continue the traversal through the this node's operands.
633  auto &WE = Worklist.back();
634  if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
635  // Push a new node to traverse first.
636  Worklist.push_back(POTWorklistEntry(*N));
637  continue;
638  }
639 
640  // Push the node onto the POT.
641  assert(WE.N->isUniqued() && "Expected only uniqued nodes");
642  assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
643  auto &D = G.Info[WE.N];
644  AnyChanges |= D.HasChanged = WE.HasChanged;
645  D.ID = G.POT.size();
646  G.POT.push_back(WE.N);
647 
648  // Pop the node off the worklist.
649  Worklist.pop_back();
650  }
651  return AnyChanges;
652 }
653 
654 MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
655  MDNode::op_iterator E, bool &HasChanged) {
656  while (I != E) {
657  Metadata *Op = *I++; // Increment even on early return.
658  if (Optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
659  // Check if the operand changes.
660  HasChanged |= Op != *MappedOp;
661  continue;
662  }
663 
664  // A uniqued metadata node.
665  MDNode &OpN = *cast<MDNode>(Op);
666  assert(OpN.isUniqued() &&
667  "Only uniqued operands cannot be mapped immediately");
668  if (G.Info.insert(std::make_pair(&OpN, Data())).second)
669  return &OpN; // This is a new one. Return it.
670  }
671  return nullptr;
672 }
673 
674 void MDNodeMapper::UniquedGraph::propagateChanges() {
675  bool AnyChanges;
676  do {
677  AnyChanges = false;
678  for (MDNode *N : POT) {
679  auto &D = Info[N];
680  if (D.HasChanged)
681  continue;
682 
683  if (llvm::none_of(N->operands(), [&](const Metadata *Op) {
684  auto Where = Info.find(Op);
685  return Where != Info.end() && Where->second.HasChanged;
686  }))
687  continue;
688 
689  AnyChanges = D.HasChanged = true;
690  }
691  } while (AnyChanges);
692 }
693 
694 void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
695  // Construct uniqued nodes, building forward references as necessary.
696  SmallVector<MDNode *, 16> CyclicNodes;
697  for (auto *N : G.POT) {
698  auto &D = G.Info[N];
699  if (!D.HasChanged) {
700  // The node hasn't changed.
701  M.mapToSelf(N);
702  continue;
703  }
704 
705  // Remember whether this node had a placeholder.
706  bool HadPlaceholder(D.Placeholder);
707 
708  // Clone the uniqued node and remap the operands.
709  TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
710  remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
711  if (Optional<Metadata *> MappedOp = getMappedOp(Old))
712  return *MappedOp;
713  (void)D;
714  assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
715  return &G.getFwdReference(*cast<MDNode>(Old));
716  });
717 
718  auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
719  M.mapToMetadata(N, NewN);
720 
721  // Nodes that were referenced out of order in the POT are involved in a
722  // uniquing cycle.
723  if (HadPlaceholder)
724  CyclicNodes.push_back(NewN);
725  }
726 
727  // Resolve cycles.
728  for (auto *N : CyclicNodes)
729  if (!N->isResolved())
730  N->resolveCycles();
731 }
732 
733 Metadata *MDNodeMapper::map(const MDNode &N) {
734  assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
735  assert(!(M.Flags & RF_NoModuleLevelChanges) &&
736  "MDNodeMapper::map assumes module-level changes");
737 
738  // Require resolved nodes whenever metadata might be remapped.
739  assert(N.isResolved() && "Unexpected unresolved node");
740 
741  Metadata *MappedN =
742  N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
743  while (!DistinctWorklist.empty())
744  remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
745  if (Optional<Metadata *> MappedOp = tryToMapOperand(Old))
746  return *MappedOp;
747  return mapTopLevelUniquedNode(*cast<MDNode>(Old));
748  });
749  return MappedN;
750 }
751 
752 Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
753  assert(FirstN.isUniqued() && "Expected uniqued node");
754 
755  // Create a post-order traversal of uniqued nodes under FirstN.
756  UniquedGraph G;
757  if (!createPOT(G, FirstN)) {
758  // Return early if no nodes have changed.
759  for (const MDNode *N : G.POT)
760  M.mapToSelf(N);
761  return &const_cast<MDNode &>(FirstN);
762  }
763 
764  // Update graph with all nodes that have changed.
765  G.propagateChanges();
766 
767  // Map all the nodes in the graph.
768  mapNodesInPOT(G);
769 
770  // Return the original node, remapped.
771  return *getMappedOp(&FirstN);
772 }
773 
774 Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
775  // If the value already exists in the map, use it.
776  if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
777  return *NewMD;
778 
779  if (isa<MDString>(MD))
780  return const_cast<Metadata *>(MD);
781 
782  // This is a module-level metadata. If nothing at the module level is
783  // changing, use an identity mapping.
784  if ((Flags & RF_NoModuleLevelChanges))
785  return const_cast<Metadata *>(MD);
786 
787  if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
788  // Don't memoize ConstantAsMetadata. Instead of lasting until the
789  // LLVMContext is destroyed, they can be deleted when the GlobalValue they
790  // reference is destructed. These aren't super common, so the extra
791  // indirection isn't that expensive.
792  return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
793  }
794 
795  assert(isa<MDNode>(MD) && "Expected a metadata node");
796 
797  return None;
798 }
799 
800 Metadata *Mapper::mapMetadata(const Metadata *MD) {
801  assert(MD && "Expected valid metadata");
802  assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
803 
804  if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
805  return *NewMD;
806 
807  return MDNodeMapper(*this).map(*cast<MDNode>(MD));
808 }
809 
810 void Mapper::flush() {
811  // Flush out the worklist of global values.
812  while (!Worklist.empty()) {
813  WorklistEntry E = Worklist.pop_back_val();
814  CurrentMCID = E.MCID;
815  switch (E.Kind) {
816  case WorklistEntry::MapGlobalInit:
817  E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
818  remapGlobalObjectMetadata(*E.Data.GVInit.GV);
819  break;
820  case WorklistEntry::MapAppendingVar: {
821  unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
822  mapAppendingVariable(*E.Data.AppendingGV.GV,
823  E.Data.AppendingGV.InitPrefix,
824  E.AppendingGVIsOldCtorDtor,
825  makeArrayRef(AppendingInits).slice(PrefixSize));
826  AppendingInits.resize(PrefixSize);
827  break;
828  }
829  case WorklistEntry::MapGlobalIndirectSymbol:
830  E.Data.GlobalIndirectSymbol.GIS->setIndirectSymbol(
831  mapConstant(E.Data.GlobalIndirectSymbol.Target));
832  break;
834  remapFunction(*E.Data.RemapF);
835  break;
836  }
837  }
838  CurrentMCID = 0;
839 
840  // Finish logic for block addresses now that all global values have been
841  // handled.
842  while (!DelayedBBs.empty()) {
843  DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
844  BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
845  DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
846  }
847 }
848 
849 void Mapper::remapInstruction(Instruction *I) {
850  // Remap operands.
851  for (Use &Op : I->operands()) {
852  Value *V = mapValue(Op);
853  // If we aren't ignoring missing entries, assert that something happened.
854  if (V)
855  Op = V;
856  else
857  assert((Flags & RF_IgnoreMissingLocals) &&
858  "Referenced value not in value map!");
859  }
860 
861  // Remap phi nodes' incoming blocks.
862  if (PHINode *PN = dyn_cast<PHINode>(I)) {
863  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
864  Value *V = mapValue(PN->getIncomingBlock(i));
865  // If we aren't ignoring missing entries, assert that something happened.
866  if (V)
867  PN->setIncomingBlock(i, cast<BasicBlock>(V));
868  else
869  assert((Flags & RF_IgnoreMissingLocals) &&
870  "Referenced block not in value map!");
871  }
872  }
873 
874  // Remap attached metadata.
876  I->getAllMetadata(MDs);
877  for (const auto &MI : MDs) {
878  MDNode *Old = MI.second;
879  MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
880  if (New != Old)
881  I->setMetadata(MI.first, New);
882  }
883 
884  if (!TypeMapper)
885  return;
886 
887  // If the instruction's type is being remapped, do so now.
888  if (auto *CB = dyn_cast<CallBase>(I)) {
890  FunctionType *FTy = CB->getFunctionType();
891  Tys.reserve(FTy->getNumParams());
892  for (Type *Ty : FTy->params())
893  Tys.push_back(TypeMapper->remapType(Ty));
894  CB->mutateFunctionType(FunctionType::get(
895  TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
896 
897  LLVMContext &C = CB->getContext();
898  AttributeList Attrs = CB->getAttributes();
899  for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
900  for (Attribute::AttrKind TypedAttr :
901  {Attribute::ByVal, Attribute::StructRet, Attribute::ByRef}) {
902  if (Type *Ty = Attrs.getAttribute(i, TypedAttr).getValueAsType()) {
903  Attrs = Attrs.replaceAttributeType(C, i, TypedAttr,
904  TypeMapper->remapType(Ty));
905  break;
906  }
907  }
908  }
909  CB->setAttributes(Attrs);
910  return;
911  }
912  if (auto *AI = dyn_cast<AllocaInst>(I))
913  AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
914  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
915  GEP->setSourceElementType(
916  TypeMapper->remapType(GEP->getSourceElementType()));
917  GEP->setResultElementType(
918  TypeMapper->remapType(GEP->getResultElementType()));
919  }
920  I->mutateType(TypeMapper->remapType(I->getType()));
921 }
922 
923 void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
925  GO.getAllMetadata(MDs);
926  GO.clearMetadata();
927  for (const auto &I : MDs)
928  GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
929 }
930 
931 void Mapper::remapFunction(Function &F) {
932  // Remap the operands.
933  for (Use &Op : F.operands())
934  if (Op)
935  Op = mapValue(Op);
936 
937  // Remap the metadata attachments.
938  remapGlobalObjectMetadata(F);
939 
940  // Remap the argument types.
941  if (TypeMapper)
942  for (Argument &A : F.args())
943  A.mutateType(TypeMapper->remapType(A.getType()));
944 
945  // Remap the instructions.
946  for (BasicBlock &BB : F)
947  for (Instruction &I : BB)
948  remapInstruction(&I);
949 }
950 
951 void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
952  bool IsOldCtorDtor,
953  ArrayRef<Constant *> NewMembers) {
955  if (InitPrefix) {
956  unsigned NumElements =
957  cast<ArrayType>(InitPrefix->getType())->getNumElements();
958  for (unsigned I = 0; I != NumElements; ++I)
959  Elements.push_back(InitPrefix->getAggregateElement(I));
960  }
961 
962  PointerType *VoidPtrTy;
963  Type *EltTy;
964  if (IsOldCtorDtor) {
965  // FIXME: This upgrade is done during linking to support the C API. See
966  // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
967  VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
968  auto &ST = *cast<StructType>(NewMembers.front()->getType());
969  Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
970  EltTy = StructType::get(GV.getContext(), Tys, false);
971  }
972 
973  for (auto *V : NewMembers) {
974  Constant *NewV;
975  if (IsOldCtorDtor) {
976  auto *S = cast<ConstantStruct>(V);
977  auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
978  auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
979  Constant *Null = Constant::getNullValue(VoidPtrTy);
980  NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
981  } else {
982  NewV = cast_or_null<Constant>(mapValue(V));
983  }
984  Elements.push_back(NewV);
985  }
986 
988  cast<ArrayType>(GV.getType()->getElementType()), Elements));
989 }
990 
991 void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
992  unsigned MCID) {
993  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
994  assert(MCID < MCs.size() && "Invalid mapping context");
995 
996  WorklistEntry WE;
997  WE.Kind = WorklistEntry::MapGlobalInit;
998  WE.MCID = MCID;
999  WE.Data.GVInit.GV = &GV;
1000  WE.Data.GVInit.Init = &Init;
1001  Worklist.push_back(WE);
1002 }
1003 
1004 void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1005  Constant *InitPrefix,
1006  bool IsOldCtorDtor,
1007  ArrayRef<Constant *> NewMembers,
1008  unsigned MCID) {
1009  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1010  assert(MCID < MCs.size() && "Invalid mapping context");
1011 
1012  WorklistEntry WE;
1013  WE.Kind = WorklistEntry::MapAppendingVar;
1014  WE.MCID = MCID;
1015  WE.Data.AppendingGV.GV = &GV;
1016  WE.Data.AppendingGV.InitPrefix = InitPrefix;
1017  WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1018  WE.AppendingGVNumNewMembers = NewMembers.size();
1019  Worklist.push_back(WE);
1020  AppendingInits.append(NewMembers.begin(), NewMembers.end());
1021 }
1022 
1023 void Mapper::scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS,
1024  Constant &Target, unsigned MCID) {
1025  assert(AlreadyScheduled.insert(&GIS).second && "Should not reschedule");
1026  assert(MCID < MCs.size() && "Invalid mapping context");
1027 
1028  WorklistEntry WE;
1029  WE.Kind = WorklistEntry::MapGlobalIndirectSymbol;
1030  WE.MCID = MCID;
1031  WE.Data.GlobalIndirectSymbol.GIS = &GIS;
1032  WE.Data.GlobalIndirectSymbol.Target = &Target;
1033  Worklist.push_back(WE);
1034 }
1035 
1036 void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1037  assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1038  assert(MCID < MCs.size() && "Invalid mapping context");
1039 
1040  WorklistEntry WE;
1041  WE.Kind = WorklistEntry::RemapFunction;
1042  WE.MCID = MCID;
1043  WE.Data.RemapF = &F;
1044  Worklist.push_back(WE);
1045 }
1046 
1047 void Mapper::addFlags(RemapFlags Flags) {
1048  assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1049  this->Flags = this->Flags | Flags;
1050 }
1051 
1052 static Mapper *getAsMapper(void *pImpl) {
1053  return reinterpret_cast<Mapper *>(pImpl);
1054 }
1055 
1056 namespace {
1057 
1058 class FlushingMapper {
1059  Mapper &M;
1060 
1061 public:
1062  explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1063  assert(!M.hasWorkToDo() && "Expected to be flushed");
1064  }
1065 
1066  ~FlushingMapper() { M.flush(); }
1067 
1068  Mapper *operator->() const { return &M; }
1069 };
1070 
1071 } // end anonymous namespace
1072 
1074  ValueMapTypeRemapper *TypeMapper,
1075  ValueMaterializer *Materializer)
1076  : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1077 
1079 
1080 unsigned
1082  ValueMaterializer *Materializer) {
1083  return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1084 }
1085 
1087  FlushingMapper(pImpl)->addFlags(Flags);
1088 }
1089 
1091  return FlushingMapper(pImpl)->mapValue(&V);
1092 }
1093 
1095  return cast_or_null<Constant>(mapValue(C));
1096 }
1097 
1099  return FlushingMapper(pImpl)->mapMetadata(&MD);
1100 }
1101 
1103  return cast_or_null<MDNode>(mapMetadata(N));
1104 }
1105 
1107  FlushingMapper(pImpl)->remapInstruction(&I);
1108 }
1109 
1111  FlushingMapper(pImpl)->remapFunction(F);
1112 }
1113 
1115  Constant &Init,
1116  unsigned MCID) {
1117  getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1118 }
1119 
1121  Constant *InitPrefix,
1122  bool IsOldCtorDtor,
1123  ArrayRef<Constant *> NewMembers,
1124  unsigned MCID) {
1125  getAsMapper(pImpl)->scheduleMapAppendingVariable(
1126  GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1127 }
1128 
1130  Constant &Target,
1131  unsigned MCID) {
1132  getAsMapper(pImpl)->scheduleMapGlobalIndirectSymbol(GIS, Target, MCID);
1133 }
1134 
1136  getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1137 }
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:159
const NoneType None
Definition: None.h:23
static unsigned getMappedOp(unsigned PseudoOp)
uint64_t CallInst * C
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
bool isUniqued() const
Definition: Metadata.h:948
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:717
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator begin() const
Definition: ArrayRef.h:144
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:264
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
Any global values not in value map are mapped to null instead of mapping to self.
Definition: ValueMapper.h:98
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1621
This file contains the declarations for metadata subclasses.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:868
Metadata node.
Definition: Metadata.h:870
F(f)
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
Hexagon Common GEP
void reserve(size_type N)
Definition: SmallVector.h:584
static Mapper * getAsMapper(void *pImpl)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:347
Function * getFunction() const
Definition: Constants.h:877
The address of a basic block.
Definition: Constants.h:851
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:458
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:389
void remapInstruction(Instruction &I)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
Constant * mapConstant(const Constant &C)
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1326
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:1512
Windows NT (Windows on ARM)
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1272
Key
PAL metadata keys.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
Class to represent function types.
Definition: DerivedTypes.h:102
Metadata * mapMetadata(const Metadata &MD)
Instruct the remapper to move distinct metadata instead of duplicating it when there are module-level...
Definition: ValueMapper.h:94
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
bool isVarArg() const
Definition: DerivedTypes.h:122
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:321
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
BasicBlock * getBasicBlock() const
Definition: Constants.h:878
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Value * getOperand(unsigned i) const
Definition: User.h:169
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:72
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
Class to represent pointers.
Definition: DerivedTypes.h:655
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:421
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:106
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1204
static Metadata * cloneOrBuildODR(const MDNode &N)
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:50
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1728
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static ConstantAsMetadata * wrapConstantAsMetadata(const ConstantAsMetadata &CMD, Value *MappedV)
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1780
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1138
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1249
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1230
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:129
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
constexpr double e
Definition: MathExtras.h:58
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1291
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1742
RemapFlags
These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:66
unsigned getNumOperands() const
Definition: User.h:191
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
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:1000
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:366
void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, bool IsOldCtorDtor, ArrayRef< Constant * > NewMembers, unsigned MappingContextID=0)
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:361
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:349
Constant * getValue() const
Definition: Metadata.h:418
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
iterator end() const
Definition: ArrayRef.h:145
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:595
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void remapFunction(Function &F)
Target - Wrapper for Target specific information.
ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:608
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition: ValueMapper.h:37
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:90
#define I(x, y, z)
Definition: MD5.cpp:59
#define N
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition: InlineAsm.cpp:42
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:990
Value * mapValue(const Value &V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:75
GlobalIndirectSymbol(Type *Ty, ValueTy VTy, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Symbol)
Definition: Globals.cpp:427
unsigned registerAlternateMappingContext(ValueToValueMapTy &VM, ValueMaterializer *Materializer=nullptr)
Register an alternate mapping context.
virtual Type * remapType(Type *SrcTy)=0
The client should implement this method if they want to remap types while mapping values.
IRTranslator LLVM IR MI
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
Root of the metadata hierarchy.
Definition: Metadata.h:58
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:195
void scheduleMapGlobalIndirectSymbol(GlobalIndirectSymbol &GIS, Constant &Target, unsigned MappingContextID=0)
Type * getElementType() const
Definition: DerivedTypes.h:674
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:70
MDNode * mapMDNode(const MDNode &N)
void resize(size_type N)
Definition: SmallVector.h:566