LLVM  17.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"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/Argument.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Constant.h"
23 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalAlias.h"
28 #include "llvm/IR/GlobalIFunc.h"
29 #include "llvm/IR/GlobalObject.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/InlineAsm.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Metadata.h"
35 #include "llvm/IR/Operator.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/Debug.h"
40 #include <cassert>
41 #include <limits>
42 #include <memory>
43 #include <utility>
44 
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "value-mapper"
48 
49 // Out of line method to get vtable etc for class.
50 void ValueMapTypeRemapper::anchor() {}
51 void ValueMaterializer::anchor() {}
52 
53 namespace {
54 
55 /// A basic block used in a BlockAddress whose function body is not yet
56 /// materialized.
57 struct DelayedBasicBlock {
58  BasicBlock *OldBB;
59  std::unique_ptr<BasicBlock> TempBB;
60 
61  DelayedBasicBlock(const BlockAddress &Old)
62  : OldBB(Old.getBasicBlock()),
63  TempBB(BasicBlock::Create(Old.getContext())) {}
64 };
65 
66 struct WorklistEntry {
67  enum EntryKind {
68  MapGlobalInit,
69  MapAppendingVar,
70  MapAliasOrIFunc,
72  };
73  struct GVInitTy {
74  GlobalVariable *GV;
75  Constant *Init;
76  };
77  struct AppendingGVTy {
78  GlobalVariable *GV;
79  Constant *InitPrefix;
80  };
81  struct AliasOrIFuncTy {
82  GlobalValue *GV;
84  };
85 
86  unsigned Kind : 2;
87  unsigned MCID : 29;
88  unsigned AppendingGVIsOldCtorDtor : 1;
89  unsigned AppendingGVNumNewMembers;
90  union {
91  GVInitTy GVInit;
92  AppendingGVTy AppendingGV;
93  AliasOrIFuncTy AliasOrIFunc;
94  Function *RemapF;
95  } Data;
96 };
97 
98 struct MappingContext {
100  ValueMaterializer *Materializer = nullptr;
101 
102  /// Construct a MappingContext with a value map and materializer.
103  explicit MappingContext(ValueToValueMapTy &VM,
104  ValueMaterializer *Materializer = nullptr)
105  : VM(&VM), Materializer(Materializer) {}
106 };
107 
108 class Mapper {
109  friend class MDNodeMapper;
110 
111 #ifndef NDEBUG
112  DenseSet<GlobalValue *> AlreadyScheduled;
113 #endif
114 
116  ValueMapTypeRemapper *TypeMapper;
117  unsigned CurrentMCID = 0;
121  SmallVector<Constant *, 16> AppendingInits;
122 
123 public:
124  Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
125  ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
126  : Flags(Flags), TypeMapper(TypeMapper),
127  MCs(1, MappingContext(VM, Materializer)) {}
128 
129  /// ValueMapper should explicitly call \a flush() before destruction.
130  ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
131 
132  bool hasWorkToDo() const { return !Worklist.empty(); }
133 
134  unsigned
135  registerAlternateMappingContext(ValueToValueMapTy &VM,
136  ValueMaterializer *Materializer = nullptr) {
137  MCs.push_back(MappingContext(VM, Materializer));
138  return MCs.size() - 1;
139  }
140 
141  void addFlags(RemapFlags Flags);
142 
143  void remapGlobalObjectMetadata(GlobalObject &GO);
144 
145  Value *mapValue(const Value *V);
146  void remapInstruction(Instruction *I);
147  void remapFunction(Function &F);
148 
149  Constant *mapConstant(const Constant *C) {
150  return cast_or_null<Constant>(mapValue(C));
151  }
152 
153  /// Map metadata.
154  ///
155  /// Find the mapping for MD. Guarantees that the return will be resolved
156  /// (not an MDNode, or MDNode::isResolved() returns true).
157  Metadata *mapMetadata(const Metadata *MD);
158 
159  void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
160  unsigned MCID);
161  void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
162  bool IsOldCtorDtor,
163  ArrayRef<Constant *> NewMembers,
164  unsigned MCID);
165  void scheduleMapAliasOrIFunc(GlobalValue &GV, Constant &Target,
166  unsigned MCID);
167  void scheduleRemapFunction(Function &F, unsigned MCID);
168 
169  void flush();
170 
171 private:
172  void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
173  bool IsOldCtorDtor,
174  ArrayRef<Constant *> NewMembers);
175 
176  ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
177  ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
178 
179  Value *mapBlockAddress(const BlockAddress &BA);
180 
181  /// Map metadata that doesn't require visiting operands.
182  std::optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
183 
184  Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
185  Metadata *mapToSelf(const Metadata *MD);
186 };
187 
188 class MDNodeMapper {
189  Mapper &M;
190 
191  /// Data about a node in \a UniquedGraph.
192  struct Data {
193  bool HasChanged = false;
195  TempMDNode Placeholder;
196  };
197 
198  /// A graph of uniqued nodes.
199  struct UniquedGraph {
201  SmallVector<MDNode *, 16> POT; // Post-order traversal.
202 
203  /// Propagate changed operands through the post-order traversal.
204  ///
205  /// Iteratively update \a Data::HasChanged for each node based on \a
206  /// Data::HasChanged of its operands, until fixed point.
207  void propagateChanges();
208 
209  /// Get a forward reference to a node to use as an operand.
210  Metadata &getFwdReference(MDNode &Op);
211  };
212 
213  /// Worklist of distinct nodes whose operands need to be remapped.
214  SmallVector<MDNode *, 16> DistinctWorklist;
215 
216  // Storage for a UniquedGraph.
218  SmallVector<MDNode *, 16> POTStorage;
219 
220 public:
221  MDNodeMapper(Mapper &M) : M(M) {}
222 
223  /// Map a metadata node (and its transitive operands).
224  ///
225  /// Map all the (unmapped) nodes in the subgraph under \c N. The iterative
226  /// algorithm handles distinct nodes and uniqued node subgraphs using
227  /// different strategies.
228  ///
229  /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
230  /// using \a mapDistinctNode(). Their mapping can always be computed
231  /// immediately without visiting operands, even if their operands change.
232  ///
233  /// The mapping for uniqued nodes depends on whether their operands change.
234  /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
235  /// a node to calculate uniqued node mappings in bulk. Distinct leafs are
236  /// added to \a DistinctWorklist with \a mapDistinctNode().
237  ///
238  /// After mapping \c N itself, this function remaps the operands of the
239  /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
240  /// N has been mapped.
241  Metadata *map(const MDNode &N);
242 
243 private:
244  /// Map a top-level uniqued node and the uniqued subgraph underneath it.
245  ///
246  /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
247  /// underneath \c FirstN and calculates the nodes' mapping. Each node uses
248  /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
249  /// operands uses the identity mapping.
250  ///
251  /// The algorithm works as follows:
252  ///
253  /// 1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
254  /// save the post-order traversal in the given \a UniquedGraph, tracking
255  /// nodes' operands change.
256  ///
257  /// 2. \a UniquedGraph::propagateChanges(): propagate changed operands
258  /// through the \a UniquedGraph until fixed point, following the rule
259  /// that if a node changes, any node that references must also change.
260  ///
261  /// 3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
262  /// (referencing new operands) where necessary.
263  Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
264 
265  /// Try to map the operand of an \a MDNode.
266  ///
267  /// If \c Op is already mapped, return the mapping. If it's not an \a
268  /// MDNode, compute and return the mapping. If it's a distinct \a MDNode,
269  /// return the result of \a mapDistinctNode().
270  ///
271  /// \return std::nullopt if \c Op is an unmapped uniqued \a MDNode.
272  /// \post getMappedOp(Op) only returns std::nullopt if this returns
273  /// std::nullopt.
274  std::optional<Metadata *> tryToMapOperand(const Metadata *Op);
275 
276  /// Map a distinct node.
277  ///
278  /// Return the mapping for the distinct node \c N, saving the result in \a
279  /// DistinctWorklist for later remapping.
280  ///
281  /// \pre \c N is not yet mapped.
282  /// \pre \c N.isDistinct().
283  MDNode *mapDistinctNode(const MDNode &N);
284 
285  /// Get a previously mapped node.
286  std::optional<Metadata *> getMappedOp(const Metadata *Op) const;
287 
288  /// Create a post-order traversal of an unmapped uniqued node subgraph.
289  ///
290  /// This traverses the metadata graph deeply enough to map \c FirstN. It
291  /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
292  /// metadata that has already been mapped will not be part of the POT.
293  ///
294  /// Each node that has a changed operand from outside the graph (e.g., a
295  /// distinct node, an already-mapped uniqued node, or \a ConstantAsMetadata)
296  /// is marked with \a Data::HasChanged.
297  ///
298  /// \return \c true if any nodes in \c G have \a Data::HasChanged.
299  /// \post \c G.POT is a post-order traversal ending with \c FirstN.
300  /// \post \a Data::hasChanged in \c G.Info indicates whether any node needs
301  /// to change because of operands outside the graph.
302  bool createPOT(UniquedGraph &G, const MDNode &FirstN);
303 
304  /// Visit the operands of a uniqued node in the POT.
305  ///
306  /// Visit the operands in the range from \c I to \c E, returning the first
307  /// uniqued node we find that isn't yet in \c G. \c I is always advanced to
308  /// where to continue the loop through the operands.
309  ///
310  /// This sets \c HasChanged if any of the visited operands change.
311  MDNode *visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
312  MDNode::op_iterator E, bool &HasChanged);
313 
314  /// Map all the nodes in the given uniqued graph.
315  ///
316  /// This visits all the nodes in \c G in post-order, using the identity
317  /// mapping or creating a new node depending on \a Data::HasChanged.
318  ///
319  /// \pre \a getMappedOp() returns std::nullopt for nodes in \c G, but not for
320  /// any of their operands outside of \c G. \pre \a Data::HasChanged is true
321  /// for a node in \c G iff any of its operands have changed. \post \a
322  /// getMappedOp() returns the mapped node for every node in \c G.
323  void mapNodesInPOT(UniquedGraph &G);
324 
325  /// Remap a node's operands using the given functor.
326  ///
327  /// Iterate through the operands of \c N and update them in place using \c
328  /// mapOperand.
329  ///
330  /// \pre N.isDistinct() or N.isTemporary().
331  template <class OperandMapper>
332  void remapOperands(MDNode &N, OperandMapper mapOperand);
333 };
334 
335 } // end anonymous namespace
336 
337 Value *Mapper::mapValue(const Value *V) {
338  ValueToValueMapTy::iterator I = getVM().find(V);
339 
340  // If the value already exists in the map, use it.
341  if (I != getVM().end()) {
342  assert(I->second && "Unexpected null mapping");
343  return I->second;
344  }
345 
346  // If we have a materializer and it can materialize a value, use that.
347  if (auto *Materializer = getMaterializer()) {
348  if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
349  getVM()[V] = NewV;
350  return NewV;
351  }
352  }
353 
354  // Global values do not need to be seeded into the VM if they
355  // are using the identity mapping.
356  if (isa<GlobalValue>(V)) {
357  if (Flags & RF_NullMapMissingGlobalValues)
358  return nullptr;
359  return getVM()[V] = const_cast<Value *>(V);
360  }
361 
362  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
363  // Inline asm may need *type* remapping.
364  FunctionType *NewTy = IA->getFunctionType();
365  if (TypeMapper) {
366  NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
367 
368  if (NewTy != IA->getFunctionType())
369  V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
370  IA->hasSideEffects(), IA->isAlignStack(),
371  IA->getDialect(), IA->canThrow());
372  }
373 
374  return getVM()[V] = const_cast<Value *>(V);
375  }
376 
377  if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
378  const Metadata *MD = MDV->getMetadata();
379 
380  if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
381  // Look through to grab the local value.
382  if (Value *LV = mapValue(LAM->getValue())) {
383  if (V == LAM->getValue())
384  return const_cast<Value *>(V);
386  }
387 
388  // FIXME: always return nullptr once Verifier::verifyDominatesUse()
389  // ensures metadata operands only reference defined SSA values.
390  return (Flags & RF_IgnoreMissingLocals)
391  ? nullptr
393  V->getContext(),
394  MDTuple::get(V->getContext(), std::nullopt));
395  }
396  if (auto *AL = dyn_cast<DIArgList>(MD)) {
398  for (auto *VAM : AL->getArgs()) {
399  // Map both Local and Constant VAMs here; they will both ultimately
400  // be mapped via mapValue. The exceptions are constants when we have no
401  // module level changes and locals when they have no existing mapped
402  // value and RF_IgnoreMissingLocals is set; these have identity
403  // mappings.
404  if ((Flags & RF_NoModuleLevelChanges) && isa<ConstantAsMetadata>(VAM)) {
405  MappedArgs.push_back(VAM);
406  } else if (Value *LV = mapValue(VAM->getValue())) {
407  MappedArgs.push_back(
408  LV == VAM->getValue() ? VAM : ValueAsMetadata::get(LV));
409  } else if ((Flags & RF_IgnoreMissingLocals) && isa<LocalAsMetadata>(VAM)) {
410  MappedArgs.push_back(VAM);
411  } else {
412  // If we cannot map the value, set the argument as undef.
413  MappedArgs.push_back(ValueAsMetadata::get(
414  UndefValue::get(VAM->getValue()->getType())));
415  }
416  }
417  return MetadataAsValue::get(V->getContext(),
418  DIArgList::get(V->getContext(), MappedArgs));
419  }
420 
421  // If this is a module-level metadata and we know that nothing at the module
422  // level is changing, then use an identity mapping.
423  if (Flags & RF_NoModuleLevelChanges)
424  return getVM()[V] = const_cast<Value *>(V);
425 
426  // Map the metadata and turn it into a value.
427  auto *MappedMD = mapMetadata(MD);
428  if (MD == MappedMD)
429  return getVM()[V] = const_cast<Value *>(V);
430  return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
431  }
432 
433  // Okay, this either must be a constant (which may or may not be mappable) or
434  // is something that is not in the mapping table.
435  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
436  if (!C)
437  return nullptr;
438 
439  if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
440  return mapBlockAddress(*BA);
441 
442  if (const auto *E = dyn_cast<DSOLocalEquivalent>(C)) {
443  auto *Val = mapValue(E->getGlobalValue());
444  GlobalValue *GV = dyn_cast<GlobalValue>(Val);
445  if (GV)
446  return getVM()[E] = DSOLocalEquivalent::get(GV);
447 
448  auto *Func = cast<Function>(Val->stripPointerCastsAndAliases());
449  Type *NewTy = E->getType();
450  if (TypeMapper)
451  NewTy = TypeMapper->remapType(NewTy);
452  return getVM()[E] = llvm::ConstantExpr::getBitCast(
453  DSOLocalEquivalent::get(Func), NewTy);
454  }
455 
456  if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
457  auto *Val = mapValue(NC->getGlobalValue());
458  GlobalValue *GV = cast<GlobalValue>(Val);
459  return getVM()[NC] = NoCFIValue::get(GV);
460  }
461 
462  auto mapValueOrNull = [this](Value *V) {
463  auto Mapped = mapValue(V);
464  assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
465  "Unexpected null mapping for constant operand without "
466  "NullMapMissingGlobalValues flag");
467  return Mapped;
468  };
469 
470  // Otherwise, we have some other constant to remap. Start by checking to see
471  // if all operands have an identity remapping.
472  unsigned OpNo = 0, NumOperands = C->getNumOperands();
473  Value *Mapped = nullptr;
474  for (; OpNo != NumOperands; ++OpNo) {
475  Value *Op = C->getOperand(OpNo);
476  Mapped = mapValueOrNull(Op);
477  if (!Mapped)
478  return nullptr;
479  if (Mapped != Op)
480  break;
481  }
482 
483  // See if the type mapper wants to remap the type as well.
484  Type *NewTy = C->getType();
485  if (TypeMapper)
486  NewTy = TypeMapper->remapType(NewTy);
487 
488  // If the result type and all operands match up, then just insert an identity
489  // mapping.
490  if (OpNo == NumOperands && NewTy == C->getType())
491  return getVM()[V] = C;
492 
493  // Okay, we need to create a new constant. We've already processed some or
494  // all of the operands, set them all up now.
496  Ops.reserve(NumOperands);
497  for (unsigned j = 0; j != OpNo; ++j)
498  Ops.push_back(cast<Constant>(C->getOperand(j)));
499 
500  // If one of the operands mismatch, push it and the other mapped operands.
501  if (OpNo != NumOperands) {
502  Ops.push_back(cast<Constant>(Mapped));
503 
504  // Map the rest of the operands that aren't processed yet.
505  for (++OpNo; OpNo != NumOperands; ++OpNo) {
506  Mapped = mapValueOrNull(C->getOperand(OpNo));
507  if (!Mapped)
508  return nullptr;
509  Ops.push_back(cast<Constant>(Mapped));
510  }
511  }
512  Type *NewSrcTy = nullptr;
513  if (TypeMapper)
514  if (auto *GEPO = dyn_cast<GEPOperator>(C))
515  NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
516 
517  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
518  return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
519  if (isa<ConstantArray>(C))
520  return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
521  if (isa<ConstantStruct>(C))
522  return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
523  if (isa<ConstantVector>(C))
524  return getVM()[V] = ConstantVector::get(Ops);
525  // If this is a no-operand constant, it must be because the type was remapped.
526  if (isa<UndefValue>(C))
527  return getVM()[V] = UndefValue::get(NewTy);
528  if (isa<ConstantAggregateZero>(C))
529  return getVM()[V] = ConstantAggregateZero::get(NewTy);
530  assert(isa<ConstantPointerNull>(C));
531  return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
532 }
533 
534 Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
535  Function *F = cast<Function>(mapValue(BA.getFunction()));
536 
537  // F may not have materialized its initializer. In that case, create a
538  // dummy basic block for now, and replace it once we've materialized all
539  // the initializers.
540  BasicBlock *BB;
541  if (F->empty()) {
542  DelayedBBs.push_back(DelayedBasicBlock(BA));
543  BB = DelayedBBs.back().TempBB.get();
544  } else {
545  BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
546  }
547 
548  return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
549 }
550 
551 Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
552  getVM().MD()[Key].reset(Val);
553  return Val;
554 }
555 
556 Metadata *Mapper::mapToSelf(const Metadata *MD) {
557  return mapToMetadata(MD, const_cast<Metadata *>(MD));
558 }
559 
560 std::optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
561  if (!Op)
562  return nullptr;
563 
564  if (std::optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
565 #ifndef NDEBUG
566  if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
567  assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
568  M.getVM().getMappedMD(Op)) &&
569  "Expected Value to be memoized");
570  else
571  assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
572  "Expected result to be memoized");
573 #endif
574  return *MappedOp;
575  }
576 
577  const MDNode &N = *cast<MDNode>(Op);
578  if (N.isDistinct())
579  return mapDistinctNode(N);
580  return std::nullopt;
581 }
582 
583 MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
584  assert(N.isDistinct() && "Expected a distinct node");
585  assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
586  Metadata *NewM = nullptr;
587 
588  if (M.Flags & RF_ReuseAndMutateDistinctMDs) {
589  NewM = M.mapToSelf(&N);
590  } else {
591  NewM = MDNode::replaceWithDistinct(N.clone());
592  LLVM_DEBUG(dbgs() << "\nMap " << N << "\n"
593  << "To " << *NewM << "\n\n");
594  M.mapToMetadata(&N, NewM);
595  }
596  DistinctWorklist.push_back(cast<MDNode>(NewM));
597 
598  return DistinctWorklist.back();
599 }
600 
602  Value *MappedV) {
603  if (CMD.getValue() == MappedV)
604  return const_cast<ConstantAsMetadata *>(&CMD);
605  return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
606 }
607 
608 std::optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
609  if (!Op)
610  return nullptr;
611 
612  if (std::optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
613  return *MappedOp;
614 
615  if (isa<MDString>(Op))
616  return const_cast<Metadata *>(Op);
617 
618  if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
619  return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
620 
621  return std::nullopt;
622 }
623 
624 Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
625  auto Where = Info.find(&Op);
626  assert(Where != Info.end() && "Expected a valid reference");
627 
628  auto &OpD = Where->second;
629  if (!OpD.HasChanged)
630  return Op;
631 
632  // Lazily construct a temporary node.
633  if (!OpD.Placeholder)
634  OpD.Placeholder = Op.clone();
635 
636  return *OpD.Placeholder;
637 }
638 
639 template <class OperandMapper>
640 void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
641  assert(!N.isUniqued() && "Expected distinct or temporary nodes");
642  for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
643  Metadata *Old = N.getOperand(I);
644  Metadata *New = mapOperand(Old);
645  if (Old != New)
646  LLVM_DEBUG(dbgs() << "Replacing Op " << Old << " with " << New << " in "
647  << N << "\n");
648 
649  if (Old != New)
650  N.replaceOperandWith(I, New);
651  }
652 }
653 
654 namespace {
655 
656 /// An entry in the worklist for the post-order traversal.
657 struct POTWorklistEntry {
658  MDNode *N; ///< Current node.
659  MDNode::op_iterator Op; ///< Current operand of \c N.
660 
661  /// Keep a flag of whether operands have changed in the worklist to avoid
662  /// hitting the map in \a UniquedGraph.
663  bool HasChanged = false;
664 
665  POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
666 };
667 
668 } // end anonymous namespace
669 
670 bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
671  assert(G.Info.empty() && "Expected a fresh traversal");
672  assert(FirstN.isUniqued() && "Expected uniqued node in POT");
673 
674  // Construct a post-order traversal of the uniqued subgraph under FirstN.
675  bool AnyChanges = false;
677  Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
678  (void)G.Info[&FirstN];
679  while (!Worklist.empty()) {
680  // Start or continue the traversal through the this node's operands.
681  auto &WE = Worklist.back();
682  if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
683  // Push a new node to traverse first.
684  Worklist.push_back(POTWorklistEntry(*N));
685  continue;
686  }
687 
688  // Push the node onto the POT.
689  assert(WE.N->isUniqued() && "Expected only uniqued nodes");
690  assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
691  auto &D = G.Info[WE.N];
692  AnyChanges |= D.HasChanged = WE.HasChanged;
693  D.ID = G.POT.size();
694  G.POT.push_back(WE.N);
695 
696  // Pop the node off the worklist.
697  Worklist.pop_back();
698  }
699  return AnyChanges;
700 }
701 
702 MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
703  MDNode::op_iterator E, bool &HasChanged) {
704  while (I != E) {
705  Metadata *Op = *I++; // Increment even on early return.
706  if (std::optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
707  // Check if the operand changes.
708  HasChanged |= Op != *MappedOp;
709  continue;
710  }
711 
712  // A uniqued metadata node.
713  MDNode &OpN = *cast<MDNode>(Op);
714  assert(OpN.isUniqued() &&
715  "Only uniqued operands cannot be mapped immediately");
716  if (G.Info.insert(std::make_pair(&OpN, Data())).second)
717  return &OpN; // This is a new one. Return it.
718  }
719  return nullptr;
720 }
721 
722 void MDNodeMapper::UniquedGraph::propagateChanges() {
723  bool AnyChanges;
724  do {
725  AnyChanges = false;
726  for (MDNode *N : POT) {
727  auto &D = Info[N];
728  if (D.HasChanged)
729  continue;
730 
731  if (llvm::none_of(N->operands(), [&](const Metadata *Op) {
732  auto Where = Info.find(Op);
733  return Where != Info.end() && Where->second.HasChanged;
734  }))
735  continue;
736 
737  AnyChanges = D.HasChanged = true;
738  }
739  } while (AnyChanges);
740 }
741 
742 void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
743  // Construct uniqued nodes, building forward references as necessary.
744  SmallVector<MDNode *, 16> CyclicNodes;
745  for (auto *N : G.POT) {
746  auto &D = G.Info[N];
747  if (!D.HasChanged) {
748  // The node hasn't changed.
749  M.mapToSelf(N);
750  continue;
751  }
752 
753  // Remember whether this node had a placeholder.
754  bool HadPlaceholder(D.Placeholder);
755 
756  // Clone the uniqued node and remap the operands.
757  TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
758  remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
759  if (std::optional<Metadata *> MappedOp = getMappedOp(Old))
760  return *MappedOp;
761  (void)D;
762  assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
763  return &G.getFwdReference(*cast<MDNode>(Old));
764  });
765 
766  auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
767  if (N && NewN && N != NewN) {
768  LLVM_DEBUG(dbgs() << "\nMap " << *N << "\n"
769  << "To " << *NewN << "\n\n");
770  }
771 
772  M.mapToMetadata(N, NewN);
773 
774  // Nodes that were referenced out of order in the POT are involved in a
775  // uniquing cycle.
776  if (HadPlaceholder)
777  CyclicNodes.push_back(NewN);
778  }
779 
780  // Resolve cycles.
781  for (auto *N : CyclicNodes)
782  if (!N->isResolved())
783  N->resolveCycles();
784 }
785 
786 Metadata *MDNodeMapper::map(const MDNode &N) {
787  assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
788  assert(!(M.Flags & RF_NoModuleLevelChanges) &&
789  "MDNodeMapper::map assumes module-level changes");
790 
791  // Require resolved nodes whenever metadata might be remapped.
792  assert(N.isResolved() && "Unexpected unresolved node");
793 
794  Metadata *MappedN =
795  N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
796  while (!DistinctWorklist.empty())
797  remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
798  if (std::optional<Metadata *> MappedOp = tryToMapOperand(Old))
799  return *MappedOp;
800  return mapTopLevelUniquedNode(*cast<MDNode>(Old));
801  });
802  return MappedN;
803 }
804 
805 Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
806  assert(FirstN.isUniqued() && "Expected uniqued node");
807 
808  // Create a post-order traversal of uniqued nodes under FirstN.
809  UniquedGraph G;
810  if (!createPOT(G, FirstN)) {
811  // Return early if no nodes have changed.
812  for (const MDNode *N : G.POT)
813  M.mapToSelf(N);
814  return &const_cast<MDNode &>(FirstN);
815  }
816 
817  // Update graph with all nodes that have changed.
818  G.propagateChanges();
819 
820  // Map all the nodes in the graph.
821  mapNodesInPOT(G);
822 
823  // Return the original node, remapped.
824  return *getMappedOp(&FirstN);
825 }
826 
827 std::optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
828  // If the value already exists in the map, use it.
829  if (std::optional<Metadata *> NewMD = getVM().getMappedMD(MD))
830  return *NewMD;
831 
832  if (isa<MDString>(MD))
833  return const_cast<Metadata *>(MD);
834 
835  // This is a module-level metadata. If nothing at the module level is
836  // changing, use an identity mapping.
837  if ((Flags & RF_NoModuleLevelChanges))
838  return const_cast<Metadata *>(MD);
839 
840  if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
841  // Don't memoize ConstantAsMetadata. Instead of lasting until the
842  // LLVMContext is destroyed, they can be deleted when the GlobalValue they
843  // reference is destructed. These aren't super common, so the extra
844  // indirection isn't that expensive.
845  return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
846  }
847 
848  assert(isa<MDNode>(MD) && "Expected a metadata node");
849 
850  return std::nullopt;
851 }
852 
853 Metadata *Mapper::mapMetadata(const Metadata *MD) {
854  assert(MD && "Expected valid metadata");
855  assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
856 
857  if (std::optional<Metadata *> NewMD = mapSimpleMetadata(MD))
858  return *NewMD;
859 
860  return MDNodeMapper(*this).map(*cast<MDNode>(MD));
861 }
862 
863 void Mapper::flush() {
864  // Flush out the worklist of global values.
865  while (!Worklist.empty()) {
866  WorklistEntry E = Worklist.pop_back_val();
867  CurrentMCID = E.MCID;
868  switch (E.Kind) {
869  case WorklistEntry::MapGlobalInit:
870  E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
871  remapGlobalObjectMetadata(*E.Data.GVInit.GV);
872  break;
873  case WorklistEntry::MapAppendingVar: {
874  unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
875  // mapAppendingVariable call can change AppendingInits if initalizer for
876  // the variable depends on another appending global, because of that inits
877  // need to be extracted and updated before the call.
879  drop_begin(AppendingInits, PrefixSize));
880  AppendingInits.resize(PrefixSize);
881  mapAppendingVariable(*E.Data.AppendingGV.GV,
882  E.Data.AppendingGV.InitPrefix,
883  E.AppendingGVIsOldCtorDtor, ArrayRef(NewInits));
884  break;
885  }
886  case WorklistEntry::MapAliasOrIFunc: {
887  GlobalValue *GV = E.Data.AliasOrIFunc.GV;
888  Constant *Target = mapConstant(E.Data.AliasOrIFunc.Target);
889  if (auto *GA = dyn_cast<GlobalAlias>(GV))
890  GA->setAliasee(Target);
891  else if (auto *GI = dyn_cast<GlobalIFunc>(GV))
892  GI->setResolver(Target);
893  else
894  llvm_unreachable("Not alias or ifunc");
895  break;
896  }
898  remapFunction(*E.Data.RemapF);
899  break;
900  }
901  }
902  CurrentMCID = 0;
903 
904  // Finish logic for block addresses now that all global values have been
905  // handled.
906  while (!DelayedBBs.empty()) {
907  DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
908  BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
909  DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
910  }
911 }
912 
913 void Mapper::remapInstruction(Instruction *I) {
914  // Remap operands.
915  for (Use &Op : I->operands()) {
916  Value *V = mapValue(Op);
917  // If we aren't ignoring missing entries, assert that something happened.
918  if (V)
919  Op = V;
920  else
921  assert((Flags & RF_IgnoreMissingLocals) &&
922  "Referenced value not in value map!");
923  }
924 
925  // Remap phi nodes' incoming blocks.
926  if (PHINode *PN = dyn_cast<PHINode>(I)) {
927  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
928  Value *V = mapValue(PN->getIncomingBlock(i));
929  // If we aren't ignoring missing entries, assert that something happened.
930  if (V)
931  PN->setIncomingBlock(i, cast<BasicBlock>(V));
932  else
933  assert((Flags & RF_IgnoreMissingLocals) &&
934  "Referenced block not in value map!");
935  }
936  }
937 
938  // Remap attached metadata.
940  I->getAllMetadata(MDs);
941  for (const auto &MI : MDs) {
942  MDNode *Old = MI.second;
943  MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
944  if (New != Old)
945  I->setMetadata(MI.first, New);
946  }
947 
948  if (!TypeMapper)
949  return;
950 
951  // If the instruction's type is being remapped, do so now.
952  if (auto *CB = dyn_cast<CallBase>(I)) {
954  FunctionType *FTy = CB->getFunctionType();
955  Tys.reserve(FTy->getNumParams());
956  for (Type *Ty : FTy->params())
957  Tys.push_back(TypeMapper->remapType(Ty));
958  CB->mutateFunctionType(FunctionType::get(
959  TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
960 
961  LLVMContext &C = CB->getContext();
963  for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
964  for (int AttrIdx = Attribute::FirstTypeAttr;
965  AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
966  Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
967  if (Type *Ty =
968  Attrs.getAttributeAtIndex(i, TypedAttr).getValueAsType()) {
969  Attrs = Attrs.replaceAttributeTypeAtIndex(C, i, TypedAttr,
970  TypeMapper->remapType(Ty));
971  break;
972  }
973  }
974  }
975  CB->setAttributes(Attrs);
976  return;
977  }
978  if (auto *AI = dyn_cast<AllocaInst>(I))
979  AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
980  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
981  GEP->setSourceElementType(
982  TypeMapper->remapType(GEP->getSourceElementType()));
983  GEP->setResultElementType(
984  TypeMapper->remapType(GEP->getResultElementType()));
985  }
986  I->mutateType(TypeMapper->remapType(I->getType()));
987 }
988 
989 void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
991  GO.getAllMetadata(MDs);
992  GO.clearMetadata();
993  for (const auto &I : MDs)
994  GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
995 }
996 
997 void Mapper::remapFunction(Function &F) {
998  // Remap the operands.
999  for (Use &Op : F.operands())
1000  if (Op)
1001  Op = mapValue(Op);
1002 
1003  // Remap the metadata attachments.
1004  remapGlobalObjectMetadata(F);
1005 
1006  // Remap the argument types.
1007  if (TypeMapper)
1008  for (Argument &A : F.args())
1009  A.mutateType(TypeMapper->remapType(A.getType()));
1010 
1011  // Remap the instructions.
1012  for (BasicBlock &BB : F)
1013  for (Instruction &I : BB)
1014  remapInstruction(&I);
1015 }
1016 
1017 void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
1018  bool IsOldCtorDtor,
1019  ArrayRef<Constant *> NewMembers) {
1021  if (InitPrefix) {
1022  unsigned NumElements =
1023  cast<ArrayType>(InitPrefix->getType())->getNumElements();
1024  for (unsigned I = 0; I != NumElements; ++I)
1025  Elements.push_back(InitPrefix->getAggregateElement(I));
1026  }
1027 
1028  PointerType *VoidPtrTy;
1029  Type *EltTy;
1030  if (IsOldCtorDtor) {
1031  // FIXME: This upgrade is done during linking to support the C API. See
1032  // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
1033  VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
1034  auto &ST = *cast<StructType>(NewMembers.front()->getType());
1035  Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
1036  EltTy = StructType::get(GV.getContext(), Tys, false);
1037  }
1038 
1039  for (auto *V : NewMembers) {
1040  Constant *NewV;
1041  if (IsOldCtorDtor) {
1042  auto *S = cast<ConstantStruct>(V);
1043  auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
1044  auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
1045  Constant *Null = Constant::getNullValue(VoidPtrTy);
1046  NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
1047  } else {
1048  NewV = cast_or_null<Constant>(mapValue(V));
1049  }
1050  Elements.push_back(NewV);
1051  }
1052 
1053  GV.setInitializer(
1054  ConstantArray::get(cast<ArrayType>(GV.getValueType()), Elements));
1055 }
1056 
1057 void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1058  unsigned MCID) {
1059  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1060  assert(MCID < MCs.size() && "Invalid mapping context");
1061 
1062  WorklistEntry WE;
1063  WE.Kind = WorklistEntry::MapGlobalInit;
1064  WE.MCID = MCID;
1065  WE.Data.GVInit.GV = &GV;
1066  WE.Data.GVInit.Init = &Init;
1067  Worklist.push_back(WE);
1068 }
1069 
1070 void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1071  Constant *InitPrefix,
1072  bool IsOldCtorDtor,
1073  ArrayRef<Constant *> NewMembers,
1074  unsigned MCID) {
1075  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1076  assert(MCID < MCs.size() && "Invalid mapping context");
1077 
1078  WorklistEntry WE;
1079  WE.Kind = WorklistEntry::MapAppendingVar;
1080  WE.MCID = MCID;
1081  WE.Data.AppendingGV.GV = &GV;
1082  WE.Data.AppendingGV.InitPrefix = InitPrefix;
1083  WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1084  WE.AppendingGVNumNewMembers = NewMembers.size();
1085  Worklist.push_back(WE);
1086  AppendingInits.append(NewMembers.begin(), NewMembers.end());
1087 }
1088 
1089 void Mapper::scheduleMapAliasOrIFunc(GlobalValue &GV, Constant &Target,
1090  unsigned MCID) {
1091  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1092  assert((isa<GlobalAlias>(GV) || isa<GlobalIFunc>(GV)) &&
1093  "Should be alias or ifunc");
1094  assert(MCID < MCs.size() && "Invalid mapping context");
1095 
1096  WorklistEntry WE;
1097  WE.Kind = WorklistEntry::MapAliasOrIFunc;
1098  WE.MCID = MCID;
1099  WE.Data.AliasOrIFunc.GV = &GV;
1100  WE.Data.AliasOrIFunc.Target = &Target;
1101  Worklist.push_back(WE);
1102 }
1103 
1104 void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1105  assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1106  assert(MCID < MCs.size() && "Invalid mapping context");
1107 
1108  WorklistEntry WE;
1109  WE.Kind = WorklistEntry::RemapFunction;
1110  WE.MCID = MCID;
1111  WE.Data.RemapF = &F;
1112  Worklist.push_back(WE);
1113 }
1114 
1115 void Mapper::addFlags(RemapFlags Flags) {
1116  assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1117  this->Flags = this->Flags | Flags;
1118 }
1119 
1120 static Mapper *getAsMapper(void *pImpl) {
1121  return reinterpret_cast<Mapper *>(pImpl);
1122 }
1123 
1124 namespace {
1125 
1126 class FlushingMapper {
1127  Mapper &M;
1128 
1129 public:
1130  explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1131  assert(!M.hasWorkToDo() && "Expected to be flushed");
1132  }
1133 
1134  ~FlushingMapper() { M.flush(); }
1135 
1136  Mapper *operator->() const { return &M; }
1137 };
1138 
1139 } // end anonymous namespace
1140 
1142  ValueMapTypeRemapper *TypeMapper,
1143  ValueMaterializer *Materializer)
1144  : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1145 
1147 
1148 unsigned
1150  ValueMaterializer *Materializer) {
1151  return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1152 }
1153 
1155  FlushingMapper(pImpl)->addFlags(Flags);
1156 }
1157 
1159  return FlushingMapper(pImpl)->mapValue(&V);
1160 }
1161 
1163  return cast_or_null<Constant>(mapValue(C));
1164 }
1165 
1167  return FlushingMapper(pImpl)->mapMetadata(&MD);
1168 }
1169 
1171  return cast_or_null<MDNode>(mapMetadata(N));
1172 }
1173 
1175  FlushingMapper(pImpl)->remapInstruction(&I);
1176 }
1177 
1179  FlushingMapper(pImpl)->remapFunction(F);
1180 }
1181 
1183  Constant &Init,
1184  unsigned MCID) {
1185  getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1186 }
1187 
1189  Constant *InitPrefix,
1190  bool IsOldCtorDtor,
1191  ArrayRef<Constant *> NewMembers,
1192  unsigned MCID) {
1193  getAsMapper(pImpl)->scheduleMapAppendingVariable(
1194  GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1195 }
1196 
1198  unsigned MCID) {
1199  getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GA, Aliasee, MCID);
1200 }
1201 
1203  unsigned MCID) {
1204  getAsMapper(pImpl)->scheduleMapAliasOrIFunc(GI, Resolver, MCID);
1205 }
1206 
1208  getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1209 }
i
i
Definition: README.txt:29
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
GlobalIFunc.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::ValueMapper::scheduleMapAppendingVariable
void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, bool IsOldCtorDtor, ArrayRef< Constant * > NewMembers, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1188
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::AArch64CC::AL
@ AL
Definition: AArch64BaseInfo.h:269
llvm::none_of
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:1749
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:386
ValueMapper.h
llvm::StructType::get
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:408
Metadata.h
DebugInfoMetadata.h
while
while(!ItemsToConvert.empty())
Definition: NVPTXLowerArgs.cpp:203
llvm::RF_NullMapMissingGlobalValues
@ RF_NullMapMissingGlobalValues
Any global values not in value map are mapped to null instead of mapping to self.
Definition: ValueMapper.h:99
llvm::Function
Definition: Function.h:59
llvm::ConstantStruct::get
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1314
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
InlineAsm.h
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::SmallDenseMap
Definition: DenseMap.h:880
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2229
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:363
llvm::BlockAddress::getBasicBlock
BasicBlock * getBasicBlock() const
Definition: Constants.h:904
llvm::GlobalAlias
Definition: GlobalAlias.h:28
llvm::ValueMapper::ValueMapper
ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Definition: ValueMapper.cpp:1141
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
llvm::AttributeList
Definition: Attributes.h:432
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:235
llvm::ValueMapper::mapMDNode
MDNode * mapMDNode(const MDNode &N)
Definition: ValueMapper.cpp:1170
GlobalObject.h
Operator.h
llvm::ValueMapper::scheduleMapGlobalInitializer
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1182
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::ValueMapTypeRemapper
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition: ValueMapper.h:36
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
llvm::ValueAsMetadata::get
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:392
STLExtras.h
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:239
llvm::ValueMapper::mapMetadata
Metadata * mapMetadata(const Metadata &MD)
Definition: ValueMapper.cpp:1166
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::ValueMapper::registerAlternateMappingContext
unsigned registerAlternateMappingContext(ValueToValueMapTy &VM, ValueMaterializer *Materializer=nullptr)
Register an alternate mapping context.
Definition: ValueMapper.cpp:1149
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
new
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM ID Predecessors according to mbb< bb27, 0x8b0a7c0 > Note ADDri is not a two address instruction its result reg1037 is an operand of the PHI node in bb76 and its operand reg1039 is the result of the PHI node We should treat it as a two address code and make sure the ADDri is scheduled after any node that reads reg1039 Use info(i.e. register scavenger) to assign it a free register to allow reuse the collector could move the objects and invalidate the derived pointer This is bad enough in the first but safe points can crop up unpredictably **array_addr i32 n y store obj * new
Definition: README.txt:125
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1399
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::ConstantAsMetadata::getValue
Constant * getValue() const
Definition: Metadata.h:427
wrapConstantAsMetadata
static ConstantAsMetadata * wrapConstantAsMetadata(const ConstantAsMetadata &CMD, Value *MappedV)
Definition: ValueMapper.cpp:601
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2137
llvm::RF_ReuseAndMutateDistinctMDs
@ RF_ReuseAndMutateDistinctMDs
Instruct the remapper to reuse and mutate distinct metadata (remapping them in place) instead of clon...
Definition: ValueMapper.h:95
Instruction.h
llvm::ValueMapper::scheduleRemapFunction
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1207
llvm::pdb::PDB_LocType::Null
@ Null
Constants.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
llvm::RemapFlags
RemapFlags
These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:65
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
DenseSet.h
getMappedOp
static unsigned getMappedOp(unsigned PseudoOp)
Definition: ARCExpandPseudos.cpp:49
llvm::Instruction
Definition: Instruction.h:41
llvm::ValueMapper::~ValueMapper
~ValueMapper()
Definition: ValueMapper.cpp:1146
llvm::ValueMapper::mapValue
Value * mapValue(const Value &V)
Definition: ValueMapper.cpp:1158
llvm::GlobalObject::addMetadata
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1359
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:642
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1740
llvm::FunctionType::params
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:130
llvm::AArch64PACKey::IA
@ IA
Definition: AArch64BaseInfo.h:825
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:61
llvm::RF_NoModuleLevelChanges
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:71
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::InlineAsm::get
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
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::Function::getAttributes
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:313
getAsMapper
static Mapper * getAsMapper(void *pImpl)
Definition: ValueMapper.cpp:1120
llvm::InlineAsm
Definition: InlineAsm.h:33
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::ValueMapper::remapFunction
void remapFunction(Function &F)
Definition: ValueMapper.cpp:1178
BasicBlock.h
llvm::Function::getAttributeAtIndex
Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const
gets the attribute from the list of attributes.
Definition: Function.cpp:657
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::ValueMapper::remapInstruction
void remapInstruction(Instruction &I)
Definition: ValueMapper.cpp:1174
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::RemapFunction
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:267
llvm::ConstantPointerNull::get
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1707
llvm::ValueMapTypeRemapper::remapType
virtual Type * remapType(Type *SrcTy)=0
The client should implement this method if they want to remap types while mapping values.
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::MDNode::replaceWithUniqued
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:1167
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:39
llvm::logicalview::LVPrintKind::Elements
@ Elements
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::Attribute::AttrKind
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:632
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDTuple::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1356
llvm::WinEH::EncodingType::CE
@ CE
Windows NT (Windows on ARM)
llvm::MDNode
Metadata node.
Definition: Metadata.h:943
llvm::BlockAddress
The address of a basic block.
Definition: Constants.h:875
llvm::ValueMapper::addFlags
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
Definition: ValueMapper.cpp:1154
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:33
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Constant::getAggregateElement
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:418
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::MDNode::isUniqued
bool isUniqued() const
Definition: Metadata.h:1125
llvm::ms_demangle::IntrinsicFunctionKind::New
@ New
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
llvm::ValueMapper::mapConstant
Constant * mapConstant(const Constant &C)
Definition: ValueMapper.cpp:1162
llvm::ValueAsMetadata::getConstant
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:366
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::ConstantVector::get
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1356
llvm::ifs::IFSSymbolType::Func
@ Func
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::ArrayRef::front
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:166
llvm::Init
Definition: Record.h:282
LAM
LoopAnalysisManager LAM
Definition: PassBuilderBindings.cpp:58
Argument.h
llvm::RF_IgnoreMissingLocals
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:89
llvm::NoCFIValue::get
static NoCFIValue * get(GlobalValue *GV)
Return a NoCFIValue for the specified function.
Definition: Constants.cpp:1909
llvm::ConstantAsMetadata
Definition: Metadata.h:412
j
return j(j<< 16)
llvm::BlockAddress::getFunction
Function * getFunction() const
Definition: Constants.h:903
Constant.h
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:998
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:356
llvm::ValueMapIterator
Definition: ValueMap.h:48
NC
#define NC
Definition: regutils.h:42
llvm::MetadataAsValue::get
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:102
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:354
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
GlobalVariable.h
Casting.h
llvm::Attribute::getValueAsType
Type * getValueAsType() const
Return the attribute's value as a Type.
Definition: Attributes.cpp:319
Function.h
llvm::Type::getPointerTo
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:776
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1249
GlobalAlias.h
llvm::MDNode::replaceWithDistinct
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:1177
llvm::ValueMapper::scheduleMapGlobalIFunc
void scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1202
Instructions.h
SmallVector.h
N
#define N
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition: SmallVector.h:677
llvm::BlockAddress::get
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1778
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
llvm::PHINode
Definition: Instructions.h:2708
llvm::GlobalObject::getAllMetadata
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1314
DerivedTypes.h
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:292
llvm::omp::RTLDependInfoFields::Flags
@ Flags
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::ConstantAggregateZero::get
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1595
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:667
llvm::ValueMaterializer
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:49
Value.h
llvm::GlobalIFunc
Definition: GlobalIFunc.h:34
llvm::Function::setAttributes
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:316
llvm::DSOLocalEquivalent::get
static DSOLocalEquivalent * get(GlobalValue *GV)
Return a DSOLocalEquivalent for the specified global value.
Definition: Constants.cpp:1851
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:772
llvm::GlobalVariable::setInitializer
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:462
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
llvm::GlobalObject::clearMetadata
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1382
llvm::ValueMapper::scheduleMapGlobalAlias
void scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1197