LLVM  16.0.0git
IRMover.cpp
Go to the documentation of this file.
1 //===- lib/Linker/IRMover.cpp ---------------------------------------------===//
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 #include "llvm/Linker/IRMover.h"
10 #include "LinkDiagnosticInfo.h"
11 #include "llvm/ADT/SetVector.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/AutoUpgrade.h"
16 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GVMaterializer.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/PseudoProbe.h"
25 #include "llvm/IR/TypeFinder.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/Path.h"
30 #include <optional>
31 #include <utility>
32 using namespace llvm;
33 
34 //===----------------------------------------------------------------------===//
35 // TypeMap implementation.
36 //===----------------------------------------------------------------------===//
37 
38 namespace {
39 class TypeMapTy : public ValueMapTypeRemapper {
40  /// This is a mapping from a source type to a destination type to use.
41  DenseMap<Type *, Type *> MappedTypes;
42 
43  /// When checking to see if two subgraphs are isomorphic, we speculatively
44  /// add types to MappedTypes, but keep track of them here in case we need to
45  /// roll back.
46  SmallVector<Type *, 16> SpeculativeTypes;
47 
48  SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
49 
50  /// This is a list of non-opaque structs in the source module that are mapped
51  /// to an opaque struct in the destination module.
52  SmallVector<StructType *, 16> SrcDefinitionsToResolve;
53 
54  /// This is the set of opaque types in the destination modules who are
55  /// getting a body from the source module.
56  SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
57 
58 public:
59  TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
60  : DstStructTypesSet(DstStructTypesSet) {}
61 
62  IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
63  /// Indicate that the specified type in the destination module is conceptually
64  /// equivalent to the specified type in the source module.
65  void addTypeMapping(Type *DstTy, Type *SrcTy);
66 
67  /// Produce a body for an opaque type in the dest module from a type
68  /// definition in the source module.
69  void linkDefinedTypeBodies();
70 
71  /// Return the mapped type to use for the specified input type from the
72  /// source module.
73  Type *get(Type *SrcTy);
74  Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
75 
76  void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
77 
79  return cast<FunctionType>(get((Type *)T));
80  }
81 
82 private:
83  Type *remapType(Type *SrcTy) override { return get(SrcTy); }
84 
85  bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
86 };
87 }
88 
89 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
90  assert(SpeculativeTypes.empty());
91  assert(SpeculativeDstOpaqueTypes.empty());
92 
93  // Check to see if these types are recursively isomorphic and establish a
94  // mapping between them if so.
95  if (!areTypesIsomorphic(DstTy, SrcTy)) {
96  // Oops, they aren't isomorphic. Just discard this request by rolling out
97  // any speculative mappings we've established.
98  for (Type *Ty : SpeculativeTypes)
99  MappedTypes.erase(Ty);
100 
101  SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
102  SpeculativeDstOpaqueTypes.size());
103  for (StructType *Ty : SpeculativeDstOpaqueTypes)
104  DstResolvedOpaqueTypes.erase(Ty);
105  } else {
106  // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy
107  // and all its descendants to lower amount of renaming in LLVM context
108  // Renaming occurs because we load all source modules to the same context
109  // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
110  // As a result we may get several different types in the destination
111  // module, which are in fact the same.
112  for (Type *Ty : SpeculativeTypes)
113  if (auto *STy = dyn_cast<StructType>(Ty))
114  if (STy->hasName())
115  STy->setName("");
116  }
117  SpeculativeTypes.clear();
118  SpeculativeDstOpaqueTypes.clear();
119 }
120 
121 /// Recursively walk this pair of types, returning true if they are isomorphic,
122 /// false if they are not.
123 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
124  // Two types with differing kinds are clearly not isomorphic.
125  if (DstTy->getTypeID() != SrcTy->getTypeID())
126  return false;
127 
128  // If we have an entry in the MappedTypes table, then we have our answer.
129  Type *&Entry = MappedTypes[SrcTy];
130  if (Entry)
131  return Entry == DstTy;
132 
133  // Two identical types are clearly isomorphic. Remember this
134  // non-speculatively.
135  if (DstTy == SrcTy) {
136  Entry = DstTy;
137  return true;
138  }
139 
140  // Okay, we have two types with identical kinds that we haven't seen before.
141 
142  // If this is an opaque struct type, special case it.
143  if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
144  // Mapping an opaque type to any struct, just keep the dest struct.
145  if (SSTy->isOpaque()) {
146  Entry = DstTy;
147  SpeculativeTypes.push_back(SrcTy);
148  return true;
149  }
150 
151  // Mapping a non-opaque source type to an opaque dest. If this is the first
152  // type that we're mapping onto this destination type then we succeed. Keep
153  // the dest, but fill it in later. If this is the second (different) type
154  // that we're trying to map onto the same opaque type then we fail.
155  if (cast<StructType>(DstTy)->isOpaque()) {
156  // We can only map one source type onto the opaque destination type.
157  if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
158  return false;
159  SrcDefinitionsToResolve.push_back(SSTy);
160  SpeculativeTypes.push_back(SrcTy);
161  SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
162  Entry = DstTy;
163  return true;
164  }
165  }
166 
167  // If the number of subtypes disagree between the two types, then we fail.
168  if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
169  return false;
170 
171  // Fail if any of the extra properties (e.g. array size) of the type disagree.
172  if (isa<IntegerType>(DstTy))
173  return false; // bitwidth disagrees.
174  if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
175  if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
176  return false;
177  } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
178  if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
179  return false;
180  } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
181  StructType *SSTy = cast<StructType>(SrcTy);
182  if (DSTy->isLiteral() != SSTy->isLiteral() ||
183  DSTy->isPacked() != SSTy->isPacked())
184  return false;
185  } else if (auto *DArrTy = dyn_cast<ArrayType>(DstTy)) {
186  if (DArrTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
187  return false;
188  } else if (auto *DVecTy = dyn_cast<VectorType>(DstTy)) {
189  if (DVecTy->getElementCount() != cast<VectorType>(SrcTy)->getElementCount())
190  return false;
191  }
192 
193  // Otherwise, we speculate that these two types will line up and recursively
194  // check the subelements.
195  Entry = DstTy;
196  SpeculativeTypes.push_back(SrcTy);
197 
198  for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
199  if (!areTypesIsomorphic(DstTy->getContainedType(I),
200  SrcTy->getContainedType(I)))
201  return false;
202 
203  // If everything seems to have lined up, then everything is great.
204  return true;
205 }
206 
207 void TypeMapTy::linkDefinedTypeBodies() {
209  for (StructType *SrcSTy : SrcDefinitionsToResolve) {
210  StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
211  assert(DstSTy->isOpaque());
212 
213  // Map the body of the source type over to a new body for the dest type.
214  Elements.resize(SrcSTy->getNumElements());
215  for (unsigned I = 0, E = Elements.size(); I != E; ++I)
216  Elements[I] = get(SrcSTy->getElementType(I));
217 
218  DstSTy->setBody(Elements, SrcSTy->isPacked());
219  DstStructTypesSet.switchToNonOpaque(DstSTy);
220  }
221  SrcDefinitionsToResolve.clear();
222  DstResolvedOpaqueTypes.clear();
223 }
224 
225 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
226  ArrayRef<Type *> ETypes) {
227  DTy->setBody(ETypes, STy->isPacked());
228 
229  // Steal STy's name.
230  if (STy->hasName()) {
231  SmallString<16> TmpName = STy->getName();
232  STy->setName("");
233  DTy->setName(TmpName);
234  }
235 
236  DstStructTypesSet.addNonOpaque(DTy);
237 }
238 
239 Type *TypeMapTy::get(Type *Ty) {
241  return get(Ty, Visited);
242 }
243 
245  // If we already have an entry for this type, return it.
246  Type **Entry = &MappedTypes[Ty];
247  if (*Entry)
248  return *Entry;
249 
250  // These are types that LLVM itself will unique.
251  bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
252 
253  if (!IsUniqued) {
254 #ifndef NDEBUG
255  for (auto &Pair : MappedTypes) {
256  assert(!(Pair.first != Ty && Pair.second == Ty) &&
257  "mapping to a source type");
258  }
259 #endif
260 
261  if (!Visited.insert(cast<StructType>(Ty)).second) {
263  return *Entry = DTy;
264  }
265  }
266 
267  // If this is not a recursive type, then just map all of the elements and
268  // then rebuild the type from inside out.
269  SmallVector<Type *, 4> ElementTypes;
270 
271  // If there are no element types to map, then the type is itself. This is
272  // true for the anonymous {} struct, things like 'float', integers, etc.
273  if (Ty->getNumContainedTypes() == 0 && IsUniqued)
274  return *Entry = Ty;
275 
276  // Remap all of the elements, keeping track of whether any of them change.
277  bool AnyChange = false;
278  ElementTypes.resize(Ty->getNumContainedTypes());
279  for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
280  ElementTypes[I] = get(Ty->getContainedType(I), Visited);
281  AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
282  }
283 
284  // If we found our type while recursively processing stuff, just use it.
285  Entry = &MappedTypes[Ty];
286  if (*Entry) {
287  if (auto *DTy = dyn_cast<StructType>(*Entry)) {
288  if (DTy->isOpaque()) {
289  auto *STy = cast<StructType>(Ty);
290  finishType(DTy, STy, ElementTypes);
291  }
292  }
293  return *Entry;
294  }
295 
296  // If all of the element types mapped directly over and the type is not
297  // a named struct, then the type is usable as-is.
298  if (!AnyChange && IsUniqued)
299  return *Entry = Ty;
300 
301  // Otherwise, rebuild a modified type.
302  switch (Ty->getTypeID()) {
303  default:
304  llvm_unreachable("unknown derived type to remap");
305  case Type::ArrayTyID:
306  return *Entry = ArrayType::get(ElementTypes[0],
307  cast<ArrayType>(Ty)->getNumElements());
310  return *Entry = VectorType::get(ElementTypes[0],
311  cast<VectorType>(Ty)->getElementCount());
312  case Type::PointerTyID:
313  return *Entry = PointerType::get(ElementTypes[0],
314  cast<PointerType>(Ty)->getAddressSpace());
315  case Type::FunctionTyID:
316  return *Entry = FunctionType::get(ElementTypes[0],
317  makeArrayRef(ElementTypes).slice(1),
318  cast<FunctionType>(Ty)->isVarArg());
319  case Type::StructTyID: {
320  auto *STy = cast<StructType>(Ty);
321  bool IsPacked = STy->isPacked();
322  if (IsUniqued)
323  return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
324 
325  // If the type is opaque, we can just use it directly.
326  if (STy->isOpaque()) {
327  DstStructTypesSet.addOpaque(STy);
328  return *Entry = Ty;
329  }
330 
331  if (StructType *OldT =
332  DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
333  STy->setName("");
334  return *Entry = OldT;
335  }
336 
337  if (!AnyChange) {
338  DstStructTypesSet.addNonOpaque(STy);
339  return *Entry = Ty;
340  }
341 
343  finishType(DTy, STy, ElementTypes);
344  return *Entry = DTy;
345  }
346  }
347 }
348 
350  const Twine &Msg)
351  : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
353 
354 //===----------------------------------------------------------------------===//
355 // IRLinker implementation.
356 //===----------------------------------------------------------------------===//
357 
358 namespace {
359 class IRLinker;
360 
361 /// Creates prototypes for functions that are lazily linked on the fly. This
362 /// speeds up linking for modules with many/ lazily linked functions of which
363 /// few get used.
364 class GlobalValueMaterializer final : public ValueMaterializer {
365  IRLinker &TheIRLinker;
366 
367 public:
368  GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
369  Value *materialize(Value *V) override;
370 };
371 
372 class LocalValueMaterializer final : public ValueMaterializer {
373  IRLinker &TheIRLinker;
374 
375 public:
376  LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
377  Value *materialize(Value *V) override;
378 };
379 
380 /// Type of the Metadata map in \a ValueToValueMapTy.
382 
383 /// This is responsible for keeping track of the state used for moving data
384 /// from SrcM to DstM.
385 class IRLinker {
386  Module &DstM;
387  std::unique_ptr<Module> SrcM;
388 
389  /// See IRMover::move().
390  IRMover::LazyCallback AddLazyFor;
391 
392  TypeMapTy TypeMap;
393  GlobalValueMaterializer GValMaterializer;
394  LocalValueMaterializer LValMaterializer;
395 
396  /// A metadata map that's shared between IRLinker instances.
397  MDMapT &SharedMDs;
398 
399  /// Mapping of values from what they used to be in Src, to what they are now
400  /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
401  /// due to the use of Value handles which the Linker doesn't actually need,
402  /// but this allows us to reuse the ValueMapper code.
404  ValueToValueMapTy IndirectSymbolValueMap;
405 
406  DenseSet<GlobalValue *> ValuesToLink;
407  std::vector<GlobalValue *> Worklist;
408  std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;
409 
410  void maybeAdd(GlobalValue *GV) {
411  if (ValuesToLink.insert(GV).second)
412  Worklist.push_back(GV);
413  }
414 
415  /// Whether we are importing globals for ThinLTO, as opposed to linking the
416  /// source module. If this flag is set, it means that we can rely on some
417  /// other object file to define any non-GlobalValue entities defined by the
418  /// source module. This currently causes us to not link retained types in
419  /// debug info metadata and module inline asm.
420  bool IsPerformingImport;
421 
422  /// Set to true when all global value body linking is complete (including
423  /// lazy linking). Used to prevent metadata linking from creating new
424  /// references.
425  bool DoneLinkingBodies = false;
426 
427  /// The Error encountered during materialization. We use an Optional here to
428  /// avoid needing to manage an unconsumed success value.
429  std::optional<Error> FoundError;
430  void setError(Error E) {
431  if (E)
432  FoundError = std::move(E);
433  }
434 
435  /// Most of the errors produced by this module are inconvertible StringErrors.
436  /// This convenience function lets us return one of those more easily.
437  Error stringErr(const Twine &T) {
438  return make_error<StringError>(T, inconvertibleErrorCode());
439  }
440 
441  /// Entry point for mapping values and alternate context for mapping aliases.
442  ValueMapper Mapper;
443  unsigned IndirectSymbolMCID;
444 
445  /// Handles cloning of a global values from the source module into
446  /// the destination module, including setting the attributes and visibility.
447  GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
448 
449  void emitWarning(const Twine &Message) {
450  SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
451  }
452 
453  /// Given a global in the source module, return the global in the
454  /// destination module that is being linked to, if any.
455  GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
456  // If the source has no name it can't link. If it has local linkage,
457  // there is no name match-up going on.
458  if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
459  return nullptr;
460 
461  // Otherwise see if we have a match in the destination module's symtab.
462  GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
463  if (!DGV)
464  return nullptr;
465 
466  // If we found a global with the same name in the dest module, but it has
467  // internal linkage, we are really not doing any linkage here.
468  if (DGV->hasLocalLinkage())
469  return nullptr;
470 
471  // If we found an intrinsic declaration with mismatching prototypes, we
472  // probably had a nameclash. Don't use that version.
473  if (auto *FDGV = dyn_cast<Function>(DGV))
474  if (FDGV->isIntrinsic())
475  if (const auto *FSrcGV = dyn_cast<Function>(SrcGV))
476  if (FDGV->getFunctionType() != TypeMap.get(FSrcGV->getFunctionType()))
477  return nullptr;
478 
479  // Otherwise, we do in fact link to the destination global.
480  return DGV;
481  }
482 
483  void computeTypeMapping();
484 
485  Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
486  const GlobalVariable *SrcGV);
487 
488  /// Given the GlobaValue \p SGV in the source module, and the matching
489  /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
490  /// into the destination module.
491  ///
492  /// Note this code may call the client-provided \p AddLazyFor.
493  bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
494  Expected<Constant *> linkGlobalValueProto(GlobalValue *GV,
495  bool ForIndirectSymbol);
496 
497  Error linkModuleFlagsMetadata();
498 
499  void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);
500  Error linkFunctionBody(Function &Dst, Function &Src);
501  void linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src);
502  void linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src);
503  Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
504 
505  /// Replace all types in the source AttributeList with the
506  /// corresponding destination type.
507  AttributeList mapAttributeTypes(LLVMContext &C, AttributeList Attrs);
508 
509  /// Functions that take care of cloning a specific global value type
510  /// into the destination module.
511  GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
512  Function *copyFunctionProto(const Function *SF);
513  GlobalValue *copyIndirectSymbolProto(const GlobalValue *SGV);
514 
515  /// Perform "replace all uses with" operations. These work items need to be
516  /// performed as part of materialization, but we postpone them to happen after
517  /// materialization is done. The materializer called by ValueMapper is not
518  /// expected to delete constants, as ValueMapper is holding pointers to some
519  /// of them, but constant destruction may be indirectly triggered by RAUW.
520  /// Hence, the need to move this out of the materialization call chain.
521  void flushRAUWWorklist();
522 
523  /// When importing for ThinLTO, prevent importing of types listed on
524  /// the DICompileUnit that we don't need a copy of in the importing
525  /// module.
526  void prepareCompileUnitsForImport();
527  void linkNamedMDNodes();
528 
529 public:
530  IRLinker(Module &DstM, MDMapT &SharedMDs,
531  IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
532  ArrayRef<GlobalValue *> ValuesToLink,
533  IRMover::LazyCallback AddLazyFor, bool IsPerformingImport)
534  : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
535  TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
536  SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
538  &TypeMap, &GValMaterializer),
539  IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
540  IndirectSymbolValueMap, &LValMaterializer)) {
541  ValueMap.getMDMap() = std::move(SharedMDs);
542  for (GlobalValue *GV : ValuesToLink)
543  maybeAdd(GV);
544  if (IsPerformingImport)
545  prepareCompileUnitsForImport();
546  }
547  ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
548 
549  Error run();
550  Value *materialize(Value *V, bool ForIndirectSymbol);
551 };
552 }
553 
554 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
555 /// table. This is good for all clients except for us. Go through the trouble
556 /// to force this back.
557 static void forceRenaming(GlobalValue *GV, StringRef Name) {
558  // If the global doesn't force its name or if it already has the right name,
559  // there is nothing for us to do.
560  if (GV->hasLocalLinkage() || GV->getName() == Name)
561  return;
562 
563  Module *M = GV->getParent();
564 
565  // If there is a conflict, rename the conflict.
566  if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
567  GV->takeName(ConflictGV);
568  ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
569  assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
570  } else {
571  GV->setName(Name); // Force the name back
572  }
573 }
574 
575 Value *GlobalValueMaterializer::materialize(Value *SGV) {
576  return TheIRLinker.materialize(SGV, false);
577 }
578 
579 Value *LocalValueMaterializer::materialize(Value *SGV) {
580  return TheIRLinker.materialize(SGV, true);
581 }
582 
583 Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
584  auto *SGV = dyn_cast<GlobalValue>(V);
585  if (!SGV)
586  return nullptr;
587 
588  // When linking a global from other modules than source & dest, skip
589  // materializing it because it would be mapped later when its containing
590  // module is linked. Linking it now would potentially pull in many types that
591  // may not be mapped properly.
592  if (SGV->getParent() != &DstM && SGV->getParent() != SrcM.get())
593  return nullptr;
594 
595  Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);
596  if (!NewProto) {
597  setError(NewProto.takeError());
598  return nullptr;
599  }
600  if (!*NewProto)
601  return nullptr;
602 
603  GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
604  if (!New)
605  return *NewProto;
606 
607  // If we already created the body, just return.
608  if (auto *F = dyn_cast<Function>(New)) {
609  if (!F->isDeclaration())
610  return New;
611  } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
612  if (V->hasInitializer() || V->hasAppendingLinkage())
613  return New;
614  } else if (auto *GA = dyn_cast<GlobalAlias>(New)) {
615  if (GA->getAliasee())
616  return New;
617  } else if (auto *GI = dyn_cast<GlobalIFunc>(New)) {
618  if (GI->getResolver())
619  return New;
620  } else {
621  llvm_unreachable("Invalid GlobalValue type");
622  }
623 
624  // If the global is being linked for an indirect symbol, it may have already
625  // been scheduled to satisfy a regular symbol. Similarly, a global being linked
626  // for a regular symbol may have already been scheduled for an indirect
627  // symbol. Check for these cases by looking in the other value map and
628  // confirming the same value has been scheduled. If there is an entry in the
629  // ValueMap but the value is different, it means that the value already had a
630  // definition in the destination module (linkonce for instance), but we need a
631  // new definition for the indirect symbol ("New" will be different).
632  if ((ForIndirectSymbol && ValueMap.lookup(SGV) == New) ||
633  (!ForIndirectSymbol && IndirectSymbolValueMap.lookup(SGV) == New))
634  return New;
635 
636  if (ForIndirectSymbol || shouldLink(New, *SGV))
637  setError(linkGlobalValueBody(*New, *SGV));
638 
639  return New;
640 }
641 
642 /// Loop through the global variables in the src module and merge them into the
643 /// dest module.
644 GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
645  // No linking to be performed or linking from the source: simply create an
646  // identical version of the symbol over in the dest module... the
647  // initializer will be filled in later by LinkGlobalInits.
648  GlobalVariable *NewDGV =
649  new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
651  /*init*/ nullptr, SGVar->getName(),
652  /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
653  SGVar->getAddressSpace());
654  NewDGV->setAlignment(SGVar->getAlign());
655  NewDGV->copyAttributesFrom(SGVar);
656  return NewDGV;
657 }
658 
659 AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {
660  for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
661  for (int AttrIdx = Attribute::FirstTypeAttr;
662  AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
663  Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
664  if (Attrs.hasAttributeAtIndex(i, TypedAttr)) {
665  if (Type *Ty =
666  Attrs.getAttributeAtIndex(i, TypedAttr).getValueAsType()) {
667  Attrs = Attrs.replaceAttributeTypeAtIndex(C, i, TypedAttr,
668  TypeMap.get(Ty));
669  break;
670  }
671  }
672  }
673  }
674  return Attrs;
675 }
676 
677 /// Link the function in the source module into the destination module if
678 /// needed, setting up mapping information.
679 Function *IRLinker::copyFunctionProto(const Function *SF) {
680  // If there is no linkage to be performed or we are linking from the source,
681  // bring SF over.
682  auto *F = Function::Create(TypeMap.get(SF->getFunctionType()),
684  SF->getAddressSpace(), SF->getName(), &DstM);
685  F->copyAttributesFrom(SF);
686  F->setAttributes(mapAttributeTypes(F->getContext(), F->getAttributes()));
687  return F;
688 }
689 
690 /// Set up prototypes for any indirect symbols that come over from the source
691 /// module.
692 GlobalValue *IRLinker::copyIndirectSymbolProto(const GlobalValue *SGV) {
693  // If there is no linkage to be performed or we're linking from the source,
694  // bring over SGA.
695  auto *Ty = TypeMap.get(SGV->getValueType());
696 
697  if (auto *GA = dyn_cast<GlobalAlias>(SGV)) {
698  auto *DGA = GlobalAlias::create(Ty, SGV->getAddressSpace(),
700  SGV->getName(), &DstM);
701  DGA->copyAttributesFrom(GA);
702  return DGA;
703  }
704 
705  if (auto *GI = dyn_cast<GlobalIFunc>(SGV)) {
706  auto *DGI = GlobalIFunc::create(Ty, SGV->getAddressSpace(),
708  SGV->getName(), nullptr, &DstM);
709  DGI->copyAttributesFrom(GI);
710  return DGI;
711  }
712 
713  llvm_unreachable("Invalid source global value type");
714 }
715 
716 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
717  bool ForDefinition) {
718  GlobalValue *NewGV;
719  if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
720  NewGV = copyGlobalVariableProto(SGVar);
721  } else if (auto *SF = dyn_cast<Function>(SGV)) {
722  NewGV = copyFunctionProto(SF);
723  } else {
724  if (ForDefinition)
725  NewGV = copyIndirectSymbolProto(SGV);
726  else if (SGV->getValueType()->isFunctionTy())
727  NewGV =
728  Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),
730  SGV->getName(), &DstM);
731  else
732  NewGV =
733  new GlobalVariable(DstM, TypeMap.get(SGV->getValueType()),
734  /*isConstant*/ false, GlobalValue::ExternalLinkage,
735  /*init*/ nullptr, SGV->getName(),
736  /*insertbefore*/ nullptr,
737  SGV->getThreadLocalMode(), SGV->getAddressSpace());
738  }
739 
740  if (ForDefinition)
741  NewGV->setLinkage(SGV->getLinkage());
742  else if (SGV->hasExternalWeakLinkage())
744 
745  if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
746  // Metadata for global variables and function declarations is copied eagerly.
747  if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
748  NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
749  }
750 
751  // Remove these copied constants in case this stays a declaration, since
752  // they point to the source module. If the def is linked the values will
753  // be mapped in during linkFunctionBody.
754  if (auto *NewF = dyn_cast<Function>(NewGV)) {
755  NewF->setPersonalityFn(nullptr);
756  NewF->setPrefixData(nullptr);
757  NewF->setPrologueData(nullptr);
758  }
759 
760  return NewGV;
761 }
762 
764  size_t DotPos = Name.rfind('.');
765  return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
766  !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
767  ? Name
768  : Name.substr(0, DotPos);
769 }
770 
771 /// Loop over all of the linked values to compute type mappings. For example,
772 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
773 /// types 'Foo' but one got renamed when the module was loaded into the same
774 /// LLVMContext.
775 void IRLinker::computeTypeMapping() {
776  for (GlobalValue &SGV : SrcM->globals()) {
777  GlobalValue *DGV = getLinkedToGlobal(&SGV);
778  if (!DGV)
779  continue;
780 
781  if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
782  TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
783  continue;
784  }
785 
786  // Unify the element type of appending arrays.
787  ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
788  ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
789  TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
790  }
791 
792  for (GlobalValue &SGV : *SrcM)
793  if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) {
794  if (DGV->getType() == SGV.getType()) {
795  // If the types of DGV and SGV are the same, it means that DGV is from
796  // the source module and got added to DstM from a shared metadata. We
797  // shouldn't map this type to itself in case the type's components get
798  // remapped to a new type from DstM (for instance, during the loop over
799  // SrcM->getIdentifiedStructTypes() below).
800  continue;
801  }
802 
803  TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
804  }
805 
806  for (GlobalValue &SGV : SrcM->aliases())
807  if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
808  TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
809 
810  // Incorporate types by name, scanning all the types in the source module.
811  // At this point, the destination module may have a type "%foo = { i32 }" for
812  // example. When the source module got loaded into the same LLVMContext, if
813  // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
814  std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
815  for (StructType *ST : Types) {
816  if (!ST->hasName())
817  continue;
818 
819  if (TypeMap.DstStructTypesSet.hasType(ST)) {
820  // This is actually a type from the destination module.
821  // getIdentifiedStructTypes() can have found it by walking debug info
822  // metadata nodes, some of which get linked by name when ODR Type Uniquing
823  // is enabled on the Context, from the source to the destination module.
824  continue;
825  }
826 
827  auto STTypePrefix = getTypeNamePrefix(ST->getName());
828  if (STTypePrefix.size() == ST->getName().size())
829  continue;
830 
831  // Check to see if the destination module has a struct with the prefix name.
832  StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix);
833  if (!DST)
834  continue;
835 
836  // Don't use it if this actually came from the source module. They're in
837  // the same LLVMContext after all. Also don't use it unless the type is
838  // actually used in the destination module. This can happen in situations
839  // like this:
840  //
841  // Module A Module B
842  // -------- --------
843  // %Z = type { %A } %B = type { %C.1 }
844  // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
845  // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
846  // %C = type { i8* } %B.3 = type { %C.1 }
847  //
848  // When we link Module B with Module A, the '%B' in Module B is
849  // used. However, that would then use '%C.1'. But when we process '%C.1',
850  // we prefer to take the '%C' version. So we are then left with both
851  // '%C.1' and '%C' being used for the same types. This leads to some
852  // variables using one type and some using the other.
853  if (TypeMap.DstStructTypesSet.hasType(DST))
854  TypeMap.addTypeMapping(DST, ST);
855  }
856 
857  // Now that we have discovered all of the type equivalences, get a body for
858  // any 'opaque' types in the dest module that are now resolved.
859  TypeMap.linkDefinedTypeBodies();
860 }
861 
862 static void getArrayElements(const Constant *C,
864  unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
865 
866  for (unsigned i = 0; i != NumElements; ++i)
867  Dest.push_back(C->getAggregateElement(i));
868 }
869 
870 /// If there were any appending global variables, link them together now.
872 IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
873  const GlobalVariable *SrcGV) {
874  // Check that both variables have compatible properties.
875  if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) {
876  if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
877  return stringErr(
878  "Linking globals named '" + SrcGV->getName() +
879  "': can only link appending global with another appending "
880  "global!");
881 
882  if (DstGV->isConstant() != SrcGV->isConstant())
883  return stringErr("Appending variables linked with different const'ness!");
884 
885  if (DstGV->getAlign() != SrcGV->getAlign())
886  return stringErr(
887  "Appending variables with different alignment need to be linked!");
888 
889  if (DstGV->getVisibility() != SrcGV->getVisibility())
890  return stringErr(
891  "Appending variables with different visibility need to be linked!");
892 
893  if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
894  return stringErr(
895  "Appending variables with different unnamed_addr need to be linked!");
896 
897  if (DstGV->getSection() != SrcGV->getSection())
898  return stringErr(
899  "Appending variables with different section name need to be linked!");
900  }
901 
902  // Do not need to do anything if source is a declaration.
903  if (SrcGV->isDeclaration())
904  return DstGV;
905 
906  Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
907  ->getElementType();
908 
909  // FIXME: This upgrade is done during linking to support the C API. Once the
910  // old form is deprecated, we should move this upgrade to
911  // llvm::UpgradeGlobalVariable() and simplify the logic here and in
912  // Mapper::mapAppendingVariable() in ValueMapper.cpp.
913  StringRef Name = SrcGV->getName();
914  bool IsNewStructor = false;
915  bool IsOldStructor = false;
916  if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
917  if (cast<StructType>(EltTy)->getNumElements() == 3)
918  IsNewStructor = true;
919  else
920  IsOldStructor = true;
921  }
922 
923  PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
924  if (IsOldStructor) {
925  auto &ST = *cast<StructType>(EltTy);
926  Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
927  EltTy = StructType::get(SrcGV->getContext(), Tys, false);
928  }
929 
930  uint64_t DstNumElements = 0;
931  if (DstGV && !DstGV->isDeclaration()) {
932  ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
933  DstNumElements = DstTy->getNumElements();
934 
935  // Check to see that they two arrays agree on type.
936  if (EltTy != DstTy->getElementType())
937  return stringErr("Appending variables with different element types!");
938  }
939 
940  SmallVector<Constant *, 16> SrcElements;
941  getArrayElements(SrcGV->getInitializer(), SrcElements);
942 
943  if (IsNewStructor) {
944  erase_if(SrcElements, [this](Constant *E) {
945  auto *Key =
946  dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());
947  if (!Key)
948  return false;
949  GlobalValue *DGV = getLinkedToGlobal(Key);
950  return !shouldLink(DGV, *Key);
951  });
952  }
953  uint64_t NewSize = DstNumElements + SrcElements.size();
954  ArrayType *NewType = ArrayType::get(EltTy, NewSize);
955 
956  // Create the new global variable.
957  GlobalVariable *NG = new GlobalVariable(
958  DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
959  /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
960  SrcGV->getAddressSpace());
961 
962  NG->copyAttributesFrom(SrcGV);
963  forceRenaming(NG, SrcGV->getName());
964 
965  Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
966 
968  *NG,
969  (DstGV && !DstGV->isDeclaration()) ? DstGV->getInitializer() : nullptr,
970  IsOldStructor, SrcElements);
971 
972  // Replace any uses of the two global variables with uses of the new
973  // global.
974  if (DstGV) {
975  RAUWWorklist.push_back(
976  std::make_pair(DstGV, ConstantExpr::getBitCast(NG, DstGV->getType())));
977  }
978 
979  return Ret;
980 }
981 
982 bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
983  if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())
984  return true;
985 
986  if (DGV && !DGV->isDeclarationForLinker())
987  return false;
988 
989  if (SGV.isDeclaration() || DoneLinkingBodies)
990  return false;
991 
992  // Callback to the client to give a chance to lazily add the Global to the
993  // list of value to link.
994  bool LazilyAdded = false;
995  if (AddLazyFor)
996  AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
997  maybeAdd(&GV);
998  LazilyAdded = true;
999  });
1000  return LazilyAdded;
1001 }
1002 
1003 Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
1004  bool ForIndirectSymbol) {
1005  GlobalValue *DGV = getLinkedToGlobal(SGV);
1006 
1007  bool ShouldLink = shouldLink(DGV, *SGV);
1008 
1009  // just missing from map
1010  if (ShouldLink) {
1011  auto I = ValueMap.find(SGV);
1012  if (I != ValueMap.end())
1013  return cast<Constant>(I->second);
1014 
1015  I = IndirectSymbolValueMap.find(SGV);
1016  if (I != IndirectSymbolValueMap.end())
1017  return cast<Constant>(I->second);
1018  }
1019 
1020  if (!ShouldLink && ForIndirectSymbol)
1021  DGV = nullptr;
1022 
1023  // Handle the ultra special appending linkage case first.
1024  if (SGV->hasAppendingLinkage() || (DGV && DGV->hasAppendingLinkage()))
1025  return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
1026  cast<GlobalVariable>(SGV));
1027 
1028  bool NeedsRenaming = false;
1029  GlobalValue *NewGV;
1030  if (DGV && !ShouldLink) {
1031  NewGV = DGV;
1032  } else {
1033  // If we are done linking global value bodies (i.e. we are performing
1034  // metadata linking), don't link in the global value due to this
1035  // reference, simply map it to null.
1036  if (DoneLinkingBodies)
1037  return nullptr;
1038 
1039  NewGV = copyGlobalValueProto(SGV, ShouldLink || ForIndirectSymbol);
1040  if (ShouldLink || !ForIndirectSymbol)
1041  NeedsRenaming = true;
1042  }
1043 
1044  // Overloaded intrinsics have overloaded types names as part of their
1045  // names. If we renamed overloaded types we should rename the intrinsic
1046  // as well.
1047  if (Function *F = dyn_cast<Function>(NewGV))
1048  if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
1049  NewGV->eraseFromParent();
1050  NewGV = *Remangled;
1051  NeedsRenaming = false;
1052  }
1053 
1054  if (NeedsRenaming)
1055  forceRenaming(NewGV, SGV->getName());
1056 
1057  if (ShouldLink || ForIndirectSymbol) {
1058  if (const Comdat *SC = SGV->getComdat()) {
1059  if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
1060  Comdat *DC = DstM.getOrInsertComdat(SC->getName());
1061  DC->setSelectionKind(SC->getSelectionKind());
1062  GO->setComdat(DC);
1063  }
1064  }
1065  }
1066 
1067  if (!ShouldLink && ForIndirectSymbol)
1069 
1070  Constant *C = NewGV;
1071  // Only create a bitcast if necessary. In particular, with
1072  // DebugTypeODRUniquing we may reach metadata in the destination module
1073  // containing a GV from the source module, in which case SGV will be
1074  // the same as DGV and NewGV, and TypeMap.get() will assert since it
1075  // assumes it is being invoked on a type in the source module.
1076  if (DGV && NewGV != SGV) {
1078  NewGV, TypeMap.get(SGV->getType()));
1079  }
1080 
1081  if (DGV && NewGV != DGV) {
1082  // Schedule "replace all uses with" to happen after materializing is
1083  // done. It is not safe to do it now, since ValueMapper may be holding
1084  // pointers to constants that will get deleted if RAUW runs.
1085  RAUWWorklist.push_back(std::make_pair(
1086  DGV,
1088  }
1089 
1090  return C;
1091 }
1092 
1093 /// Update the initializers in the Dest module now that all globals that may be
1094 /// referenced are in Dest.
1095 void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
1096  // Figure out what the initializer looks like in the dest module.
1097  Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
1098 }
1099 
1100 /// Copy the source function over into the dest function and fix up references
1101 /// to values. At this point we know that Dest is an external function, and
1102 /// that Src is not.
1103 Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1104  assert(Dst.isDeclaration() && !Src.isDeclaration());
1105 
1106  // Materialize if needed.
1107  if (Error Err = Src.materialize())
1108  return Err;
1109 
1110  // Link in the operands without remapping.
1111  if (Src.hasPrefixData())
1112  Dst.setPrefixData(Src.getPrefixData());
1113  if (Src.hasPrologueData())
1114  Dst.setPrologueData(Src.getPrologueData());
1115  if (Src.hasPersonalityFn())
1116  Dst.setPersonalityFn(Src.getPersonalityFn());
1117 
1118  // Copy over the metadata attachments without remapping.
1119  Dst.copyMetadata(&Src, 0);
1120 
1121  // Steal arguments and splice the body of Src into Dst.
1122  Dst.stealArgumentListFrom(Src);
1123  Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1124 
1125  // Everything has been moved over. Remap it.
1126  Mapper.scheduleRemapFunction(Dst);
1127  return Error::success();
1128 }
1129 
1130 void IRLinker::linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src) {
1131  Mapper.scheduleMapGlobalAlias(Dst, *Src.getAliasee(), IndirectSymbolMCID);
1132 }
1133 
1134 void IRLinker::linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src) {
1135  Mapper.scheduleMapGlobalIFunc(Dst, *Src.getResolver(), IndirectSymbolMCID);
1136 }
1137 
1138 Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1139  if (auto *F = dyn_cast<Function>(&Src))
1140  return linkFunctionBody(cast<Function>(Dst), *F);
1141  if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1142  linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);
1143  return Error::success();
1144  }
1145  if (auto *GA = dyn_cast<GlobalAlias>(&Src)) {
1146  linkAliasAliasee(cast<GlobalAlias>(Dst), *GA);
1147  return Error::success();
1148  }
1149  linkIFuncResolver(cast<GlobalIFunc>(Dst), cast<GlobalIFunc>(Src));
1150  return Error::success();
1151 }
1152 
1153 void IRLinker::flushRAUWWorklist() {
1154  for (const auto &Elem : RAUWWorklist) {
1155  GlobalValue *Old;
1156  Value *New;
1157  std::tie(Old, New) = Elem;
1158 
1159  Old->replaceAllUsesWith(New);
1160  Old->eraseFromParent();
1161  }
1162  RAUWWorklist.clear();
1163 }
1164 
1165 void IRLinker::prepareCompileUnitsForImport() {
1166  NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
1167  if (!SrcCompileUnits)
1168  return;
1169  // When importing for ThinLTO, prevent importing of types listed on
1170  // the DICompileUnit that we don't need a copy of in the importing
1171  // module. They will be emitted by the originating module.
1172  for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); I != E; ++I) {
1173  auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I));
1174  assert(CU && "Expected valid compile unit");
1175  // Enums, macros, and retained types don't need to be listed on the
1176  // imported DICompileUnit. This means they will only be imported
1177  // if reached from the mapped IR.
1178  CU->replaceEnumTypes(nullptr);
1179  CU->replaceMacros(nullptr);
1180  CU->replaceRetainedTypes(nullptr);
1181 
1182  // The original definition (or at least its debug info - if the variable is
1183  // internalized and optimized away) will remain in the source module, so
1184  // there's no need to import them.
1185  // If LLVM ever does more advanced optimizations on global variables
1186  // (removing/localizing write operations, for instance) that can track
1187  // through debug info, this decision may need to be revisited - but do so
1188  // with care when it comes to debug info size. Emitting small CUs containing
1189  // only a few imported entities into every destination module may be very
1190  // size inefficient.
1191  CU->replaceGlobalVariables(nullptr);
1192 
1193  // Imported entities only need to be mapped in if they have local
1194  // scope, as those might correspond to an imported entity inside a
1195  // function being imported (any locally scoped imported entities that
1196  // don't end up referenced by an imported function will not be emitted
1197  // into the object). Imported entities not in a local scope
1198  // (e.g. on the namespace) only need to be emitted by the originating
1199  // module. Create a list of the locally scoped imported entities, and
1200  // replace the source CUs imported entity list with the new list, so
1201  // only those are mapped in.
1202  // FIXME: Locally-scoped imported entities could be moved to the
1203  // functions they are local to instead of listing them on the CU, and
1204  // we would naturally only link in those needed by function importing.
1205  SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
1206  bool ReplaceImportedEntities = false;
1207  for (auto *IE : CU->getImportedEntities()) {
1208  DIScope *Scope = IE->getScope();
1209  assert(Scope && "Invalid Scope encoding!");
1210  if (isa<DILocalScope>(Scope))
1211  AllImportedModules.emplace_back(IE);
1212  else
1213  ReplaceImportedEntities = true;
1214  }
1215  if (ReplaceImportedEntities) {
1216  if (!AllImportedModules.empty())
1217  CU->replaceImportedEntities(MDTuple::get(
1218  CU->getContext(),
1219  SmallVector<Metadata *, 16>(AllImportedModules.begin(),
1220  AllImportedModules.end())));
1221  else
1222  // If there were no local scope imported entities, we can map
1223  // the whole list to nullptr.
1224  CU->replaceImportedEntities(nullptr);
1225  }
1226  }
1227 }
1228 
1229 /// Insert all of the named MDNodes in Src into the Dest module.
1230 void IRLinker::linkNamedMDNodes() {
1231  const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1232  for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1233  // Don't link module flags here. Do them separately.
1234  if (&NMD == SrcModFlags)
1235  continue;
1236  // Don't import pseudo probe descriptors here for thinLTO. They will be
1237  // emitted by the originating module.
1238  if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) {
1239  if (!DstM.getNamedMetadata(NMD.getName()))
1240  emitWarning("Pseudo-probe ignored: source module '" +
1241  SrcM->getModuleIdentifier() +
1242  "' is compiled with -fpseudo-probe-for-profiling while "
1243  "destination module '" +
1244  DstM.getModuleIdentifier() + "' is not\n");
1245  continue;
1246  }
1247  // The stats are computed per module and will all be merged in the binary.
1248  // Importing the metadata will cause duplication of the stats.
1249  if (IsPerformingImport && NMD.getName() == "llvm.stats")
1250  continue;
1251 
1252  NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1253  // Add Src elements into Dest node.
1254  for (const MDNode *Op : NMD.operands())
1255  DestNMD->addOperand(Mapper.mapMDNode(*Op));
1256  }
1257 }
1258 
1259 /// Merge the linker flags in Src into the Dest module.
1260 Error IRLinker::linkModuleFlagsMetadata() {
1261  // If the source module has no module flags, we are done.
1262  const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1263  if (!SrcModFlags)
1264  return Error::success();
1265 
1266  // Check for module flag for updates before do anything.
1267  UpgradeModuleFlags(*SrcM);
1268 
1269  // If the destination module doesn't have module flags yet, then just copy
1270  // over the source module's flags.
1271  NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1272  if (DstModFlags->getNumOperands() == 0) {
1273  for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1274  DstModFlags->addOperand(SrcModFlags->getOperand(I));
1275 
1276  return Error::success();
1277  }
1278 
1279  // First build a map of the existing module flags and requirements.
1281  SmallSetVector<MDNode *, 16> Requirements;
1283  DenseSet<MDString *> SeenMin;
1284  for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1285  MDNode *Op = DstModFlags->getOperand(I);
1286  uint64_t Behavior =
1287  mdconst::extract<ConstantInt>(Op->getOperand(0))->getZExtValue();
1288  MDString *ID = cast<MDString>(Op->getOperand(1));
1289 
1290  if (Behavior == Module::Require) {
1291  Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1292  } else {
1293  if (Behavior == Module::Min)
1294  Mins.push_back(I);
1295  Flags[ID] = std::make_pair(Op, I);
1296  }
1297  }
1298 
1299  // Merge in the flags from the source module, and also collect its set of
1300  // requirements.
1301  for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1302  MDNode *SrcOp = SrcModFlags->getOperand(I);
1303  ConstantInt *SrcBehavior =
1304  mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1305  MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1306  MDNode *DstOp;
1307  unsigned DstIndex;
1308  std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1309  unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1310  SeenMin.insert(ID);
1311 
1312  // If this is a requirement, add it and continue.
1313  if (SrcBehaviorValue == Module::Require) {
1314  // If the destination module does not already have this requirement, add
1315  // it.
1316  if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1317  DstModFlags->addOperand(SrcOp);
1318  }
1319  continue;
1320  }
1321 
1322  // If there is no existing flag with this ID, just add it.
1323  if (!DstOp) {
1324  if (SrcBehaviorValue == Module::Min) {
1325  Mins.push_back(DstModFlags->getNumOperands());
1326  SeenMin.erase(ID);
1327  }
1328  Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1329  DstModFlags->addOperand(SrcOp);
1330  continue;
1331  }
1332 
1333  // Otherwise, perform a merge.
1334  ConstantInt *DstBehavior =
1335  mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1336  unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1337 
1338  auto overrideDstValue = [&]() {
1339  DstModFlags->setOperand(DstIndex, SrcOp);
1340  Flags[ID].first = SrcOp;
1341  };
1342 
1343  // If either flag has override behavior, handle it first.
1344  if (DstBehaviorValue == Module::Override) {
1345  // Diagnose inconsistent flags which both have override behavior.
1346  if (SrcBehaviorValue == Module::Override &&
1347  SrcOp->getOperand(2) != DstOp->getOperand(2))
1348  return stringErr("linking module flags '" + ID->getString() +
1349  "': IDs have conflicting override values in '" +
1350  SrcM->getModuleIdentifier() + "' and '" +
1351  DstM.getModuleIdentifier() + "'");
1352  continue;
1353  } else if (SrcBehaviorValue == Module::Override) {
1354  // Update the destination flag to that of the source.
1355  overrideDstValue();
1356  continue;
1357  }
1358 
1359  // Diagnose inconsistent merge behavior types.
1360  if (SrcBehaviorValue != DstBehaviorValue) {
1361  bool MinAndWarn = (SrcBehaviorValue == Module::Min &&
1362  DstBehaviorValue == Module::Warning) ||
1363  (DstBehaviorValue == Module::Min &&
1364  SrcBehaviorValue == Module::Warning);
1365  bool MaxAndWarn = (SrcBehaviorValue == Module::Max &&
1366  DstBehaviorValue == Module::Warning) ||
1367  (DstBehaviorValue == Module::Max &&
1368  SrcBehaviorValue == Module::Warning);
1369  if (!(MaxAndWarn || MinAndWarn))
1370  return stringErr("linking module flags '" + ID->getString() +
1371  "': IDs have conflicting behaviors in '" +
1372  SrcM->getModuleIdentifier() + "' and '" +
1373  DstM.getModuleIdentifier() + "'");
1374  }
1375 
1376  auto ensureDistinctOp = [&](MDNode *DstValue) {
1377  assert(isa<MDTuple>(DstValue) &&
1378  "Expected MDTuple when appending module flags");
1379  if (DstValue->isDistinct())
1380  return dyn_cast<MDTuple>(DstValue);
1381  ArrayRef<MDOperand> DstOperands = DstValue->operands();
1383  DstM.getContext(),
1384  SmallVector<Metadata *, 4>(DstOperands.begin(), DstOperands.end()));
1385  Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1386  MDNode *Flag = MDTuple::getDistinct(DstM.getContext(), FlagOps);
1387  DstModFlags->setOperand(DstIndex, Flag);
1388  Flags[ID].first = Flag;
1389  return New;
1390  };
1391 
1392  // Emit a warning if the values differ and either source or destination
1393  // request Warning behavior.
1394  if ((DstBehaviorValue == Module::Warning ||
1395  SrcBehaviorValue == Module::Warning) &&
1396  SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1397  std::string Str;
1398  raw_string_ostream(Str)
1399  << "linking module flags '" << ID->getString()
1400  << "': IDs have conflicting values ('" << *SrcOp->getOperand(2)
1401  << "' from " << SrcM->getModuleIdentifier() << " with '"
1402  << *DstOp->getOperand(2) << "' from " << DstM.getModuleIdentifier()
1403  << ')';
1404  emitWarning(Str);
1405  }
1406 
1407  // Choose the minimum if either source or destination request Min behavior.
1408  if (DstBehaviorValue == Module::Min || SrcBehaviorValue == Module::Min) {
1409  ConstantInt *DstValue =
1410  mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1411  ConstantInt *SrcValue =
1412  mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1413 
1414  // The resulting flag should have a Min behavior, and contain the minimum
1415  // value from between the source and destination values.
1416  Metadata *FlagOps[] = {
1417  (DstBehaviorValue != Module::Min ? SrcOp : DstOp)->getOperand(0), ID,
1418  (SrcValue->getZExtValue() < DstValue->getZExtValue() ? SrcOp : DstOp)
1419  ->getOperand(2)};
1420  MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1421  DstModFlags->setOperand(DstIndex, Flag);
1422  Flags[ID].first = Flag;
1423  continue;
1424  }
1425 
1426  // Choose the maximum if either source or destination request Max behavior.
1427  if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) {
1428  ConstantInt *DstValue =
1429  mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1430  ConstantInt *SrcValue =
1431  mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1432 
1433  // The resulting flag should have a Max behavior, and contain the maximum
1434  // value from between the source and destination values.
1435  Metadata *FlagOps[] = {
1436  (DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(0), ID,
1437  (SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp)
1438  ->getOperand(2)};
1439  MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1440  DstModFlags->setOperand(DstIndex, Flag);
1441  Flags[ID].first = Flag;
1442  continue;
1443  }
1444 
1445  // Perform the merge for standard behavior types.
1446  switch (SrcBehaviorValue) {
1447  case Module::Require:
1448  case Module::Override:
1449  llvm_unreachable("not possible");
1450  case Module::Error: {
1451  // Emit an error if the values differ.
1452  if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1453  return stringErr("linking module flags '" + ID->getString() +
1454  "': IDs have conflicting values in '" +
1455  SrcM->getModuleIdentifier() + "' and '" +
1456  DstM.getModuleIdentifier() + "'");
1457  continue;
1458  }
1459  case Module::Warning: {
1460  break;
1461  }
1462  case Module::Max: {
1463  break;
1464  }
1465  case Module::Append: {
1466  MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
1467  MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1468  for (const auto &O : SrcValue->operands())
1469  DstValue->push_back(O);
1470  break;
1471  }
1472  case Module::AppendUnique: {
1474  MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
1475  MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1476  Elts.insert(DstValue->op_begin(), DstValue->op_end());
1477  Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1478  for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++)
1479  DstValue->push_back(Elts[I]);
1480  break;
1481  }
1482  }
1483 
1484  }
1485 
1486  // For the Min behavior, set the value to 0 if either module does not have the
1487  // flag.
1488  for (auto Idx : Mins) {
1489  MDNode *Op = DstModFlags->getOperand(Idx);
1490  MDString *ID = cast<MDString>(Op->getOperand(1));
1491  if (!SeenMin.count(ID)) {
1492  ConstantInt *V = mdconst::extract<ConstantInt>(Op->getOperand(2));
1493  Metadata *FlagOps[] = {
1494  Op->getOperand(0), ID,
1496  DstModFlags->setOperand(Idx, MDNode::get(DstM.getContext(), FlagOps));
1497  }
1498  }
1499 
1500  // Check all of the requirements.
1501  for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1502  MDNode *Requirement = Requirements[I];
1503  MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1504  Metadata *ReqValue = Requirement->getOperand(1);
1505 
1506  MDNode *Op = Flags[Flag].first;
1507  if (!Op || Op->getOperand(2) != ReqValue)
1508  return stringErr("linking module flags '" + Flag->getString() +
1509  "': does not have the required value");
1510  }
1511  return Error::success();
1512 }
1513 
1514 /// Return InlineAsm adjusted with target-specific directives if required.
1515 /// For ARM and Thumb, we have to add directives to select the appropriate ISA
1516 /// to support mixing module-level inline assembly from ARM and Thumb modules.
1517 static std::string adjustInlineAsm(const std::string &InlineAsm,
1518  const Triple &Triple) {
1520  return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1522  return ".text\n.balign 4\n.arm\n" + InlineAsm;
1523  return InlineAsm;
1524 }
1525 
1526 Error IRLinker::run() {
1527  // Ensure metadata materialized before value mapping.
1528  if (SrcM->getMaterializer())
1529  if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1530  return Err;
1531 
1532  // Inherit the target data from the source module if the destination module
1533  // doesn't have one already.
1534  if (DstM.getDataLayout().isDefault())
1535  DstM.setDataLayout(SrcM->getDataLayout());
1536 
1537  // Copy the target triple from the source to dest if the dest's is empty.
1538  if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1539  DstM.setTargetTriple(SrcM->getTargetTriple());
1540 
1541  Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1542 
1543  // During CUDA compilation we have to link with the bitcode supplied with
1544  // CUDA. libdevice bitcode either has no data layout set (pre-CUDA-11), or has
1545  // the layout that is different from the one used by LLVM/clang (it does not
1546  // include i128). Issuing a warning is not very helpful as there's not much
1547  // the user can do about it.
1548  bool EnableDLWarning = true;
1549  bool EnableTripleWarning = true;
1550  if (SrcTriple.isNVPTX() && DstTriple.isNVPTX()) {
1551  std::string ModuleId = SrcM->getModuleIdentifier();
1552  StringRef FileName = llvm::sys::path::filename(ModuleId);
1553  bool SrcIsLibDevice =
1554  FileName.startswith("libdevice") && FileName.endswith(".10.bc");
1555  bool SrcHasLibDeviceDL =
1556  (SrcM->getDataLayoutStr().empty() ||
1557  SrcM->getDataLayoutStr() == "e-i64:64-v16:16-v32:32-n16:32:64");
1558  // libdevice bitcode uses nvptx64-nvidia-gpulibs or just
1559  // 'nvptx-unknown-unknown' triple (before CUDA-10.x) and is compatible with
1560  // all NVPTX variants.
1561  bool SrcHasLibDeviceTriple = (SrcTriple.getVendor() == Triple::NVIDIA &&
1562  SrcTriple.getOSName() == "gpulibs") ||
1563  (SrcTriple.getVendorName() == "unknown" &&
1564  SrcTriple.getOSName() == "unknown");
1565  EnableTripleWarning = !(SrcIsLibDevice && SrcHasLibDeviceTriple);
1566  EnableDLWarning = !(SrcIsLibDevice && SrcHasLibDeviceDL);
1567  }
1568 
1569  if (EnableDLWarning && (SrcM->getDataLayout() != DstM.getDataLayout())) {
1570  emitWarning("Linking two modules of different data layouts: '" +
1571  SrcM->getModuleIdentifier() + "' is '" +
1572  SrcM->getDataLayoutStr() + "' whereas '" +
1573  DstM.getModuleIdentifier() + "' is '" +
1574  DstM.getDataLayoutStr() + "'\n");
1575  }
1576 
1577  if (EnableTripleWarning && !SrcM->getTargetTriple().empty() &&
1578  !SrcTriple.isCompatibleWith(DstTriple))
1579  emitWarning("Linking two modules of different target triples: '" +
1580  SrcM->getModuleIdentifier() + "' is '" +
1581  SrcM->getTargetTriple() + "' whereas '" +
1582  DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1583  "'\n");
1584 
1585  DstM.setTargetTriple(SrcTriple.merge(DstTriple));
1586 
1587  // Loop over all of the linked values to compute type mappings.
1588  computeTypeMapping();
1589 
1590  std::reverse(Worklist.begin(), Worklist.end());
1591  while (!Worklist.empty()) {
1592  GlobalValue *GV = Worklist.back();
1593  Worklist.pop_back();
1594 
1595  // Already mapped.
1596  if (ValueMap.find(GV) != ValueMap.end() ||
1597  IndirectSymbolValueMap.find(GV) != IndirectSymbolValueMap.end())
1598  continue;
1599 
1600  assert(!GV->isDeclaration());
1601  Mapper.mapValue(*GV);
1602  if (FoundError)
1603  return std::move(*FoundError);
1604  flushRAUWWorklist();
1605  }
1606 
1607  // Note that we are done linking global value bodies. This prevents
1608  // metadata linking from creating new references.
1609  DoneLinkingBodies = true;
1611 
1612  // Remap all of the named MDNodes in Src into the DstM module. We do this
1613  // after linking GlobalValues so that MDNodes that reference GlobalValues
1614  // are properly remapped.
1615  linkNamedMDNodes();
1616 
1617  if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
1618  // Append the module inline asm string.
1619  DstM.appendModuleInlineAsm(adjustInlineAsm(SrcM->getModuleInlineAsm(),
1620  SrcTriple));
1621  } else if (IsPerformingImport) {
1622  // Import any symver directives for symbols in DstM.
1624  [&](StringRef Name, StringRef Alias) {
1625  if (DstM.getNamedValue(Name)) {
1626  SmallString<256> S(".symver ");
1627  S += Name;
1628  S += ", ";
1629  S += Alias;
1630  DstM.appendModuleInlineAsm(S);
1631  }
1632  });
1633  }
1634 
1635  // Reorder the globals just added to the destination module to match their
1636  // original order in the source module.
1637  Module::GlobalListType &Globals = DstM.getGlobalList();
1638  for (GlobalVariable &GV : SrcM->globals()) {
1639  if (GV.hasAppendingLinkage())
1640  continue;
1641  Value *NewValue = Mapper.mapValue(GV);
1642  if (NewValue) {
1643  auto *NewGV = dyn_cast<GlobalVariable>(NewValue->stripPointerCasts());
1644  if (NewGV)
1645  Globals.splice(Globals.end(), Globals, NewGV->getIterator());
1646  }
1647  }
1648 
1649  // Merge the module flags into the DstM module.
1650  return linkModuleFlagsMetadata();
1651 }
1652 
1654  : ETypes(E), IsPacked(P) {}
1655 
1657  : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1658 
1660  return IsPacked == That.IsPacked && ETypes == That.ETypes;
1661 }
1662 
1664  return !this->operator==(That);
1665 }
1666 
1667 StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1669 }
1670 
1671 StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1673 }
1674 
1675 unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1676  return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1677  Key.IsPacked);
1678 }
1679 
1680 unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1681  return getHashValue(KeyTy(ST));
1682 }
1683 
1684 bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1685  const StructType *RHS) {
1686  if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1687  return false;
1688  return LHS == KeyTy(RHS);
1689 }
1690 
1692  const StructType *RHS) {
1693  if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1694  return LHS == RHS;
1695  return KeyTy(LHS) == KeyTy(RHS);
1696 }
1697 
1699  assert(!Ty->isOpaque());
1700  NonOpaqueStructTypes.insert(Ty);
1701 }
1702 
1704  assert(!Ty->isOpaque());
1705  NonOpaqueStructTypes.insert(Ty);
1706  bool Removed = OpaqueStructTypes.erase(Ty);
1707  (void)Removed;
1708  assert(Removed);
1709 }
1710 
1712  assert(Ty->isOpaque());
1713  OpaqueStructTypes.insert(Ty);
1714 }
1715 
1716 StructType *
1718  bool IsPacked) {
1720  auto I = NonOpaqueStructTypes.find_as(Key);
1721  return I == NonOpaqueStructTypes.end() ? nullptr : *I;
1722 }
1723 
1725  if (Ty->isOpaque())
1726  return OpaqueStructTypes.count(Ty);
1727  auto I = NonOpaqueStructTypes.find(Ty);
1728  return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
1729 }
1730 
1731 IRMover::IRMover(Module &M) : Composite(M) {
1732  TypeFinder StructTypes;
1733  StructTypes.run(M, /* OnlyNamed */ false);
1734  for (StructType *Ty : StructTypes) {
1735  if (Ty->isOpaque())
1736  IdentifiedStructTypes.addOpaque(Ty);
1737  else
1738  IdentifiedStructTypes.addNonOpaque(Ty);
1739  }
1740  // Self-map metadatas in the destination module. This is needed when
1741  // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1742  // destination module may be reached from the source module.
1743  for (const auto *MD : StructTypes.getVisitedMetadata()) {
1744  SharedMDs[MD].reset(const_cast<MDNode *>(MD));
1745  }
1746 }
1747 
1748 Error IRMover::move(std::unique_ptr<Module> Src,
1749  ArrayRef<GlobalValue *> ValuesToLink,
1750  LazyCallback AddLazyFor, bool IsPerformingImport) {
1751  IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1752  std::move(Src), ValuesToLink, std::move(AddLazyFor),
1753  IsPerformingImport);
1754  Error E = TheIRLinker.run();
1755  Composite.dropTriviallyDeadConstantArrays();
1756  return E;
1757 }
i
i
Definition: README.txt:29
llvm::Type::ArrayTyID
@ ArrayTyID
Arrays.
Definition: Type.h:75
llvm::Module::appendModuleInlineAsm
void appendModuleInlineAsm(StringRef Asm)
Append to the module-scope inline assembly blocks.
Definition: Module.h:313
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm::Module::getOrInsertModuleFlagsMetadata
NamedMDNode * getOrInsertModuleFlagsMetadata()
Returns the NamedMDNode in the module that represents module-level flags.
Definition: Module.cpp:342
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:1190
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::Module::Min
@ Min
Takes the min of the two values, which are required to be integers.
Definition: Module.h:150
llvm::NamedMDNode
A tuple of MDNodes.
Definition: Metadata.h:1588
llvm::IRMover::StructTypeKeyInfo::KeyTy::ETypes
ArrayRef< Type * > ETypes
Definition: IRMover.h:29
llvm::SmallPtrSetImpl::erase
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:379
llvm::StructType::getName
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:581
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:406
llvm::NamedMDNode::getNumOperands
unsigned getNumOperands() const
Definition: Metadata.cpp:1212
llvm::ConstantInt::getType
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:173
llvm::unique_function
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
DebugInfoMetadata.h
llvm::ValueMap::end
iterator end()
Definition: ValueMap.h:136
llvm::GlobalVariable::copyAttributesFrom
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:486
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:541
T
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:60
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::IRMover::IdentifiedStructTypeSet::findNonOpaque
StructType * findNonOpaque(ArrayRef< Type * > ETypes, bool IsPacked)
Definition: IRMover.cpp:1717
llvm::IRMover::StructTypeKeyInfo::KeyTy
Definition: IRMover.h:28
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:628
llvm::PointerType::get
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Definition: Type.cpp:727
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:77
llvm::StructType::setBody
void setBody(ArrayRef< Type * > Elements, bool isPacked=false)
Specify a body for an opaque identified type.
Definition: Type.cpp:445
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:111
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:52
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::GlobalObject::getAlign
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:79
Path.h
llvm::Value::hasName
bool hasName() const
Definition: Value.h:261
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::erase_if
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:1997
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2202
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:361
llvm::GlobalAlias
Definition: GlobalAlias.h:28
Error.h
llvm::Type::getTypeID
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:136
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::erase
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
llvm::NamedMDNode::setOperand
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1224
llvm::DK_Linker
@ DK_Linker
Definition: DiagnosticInfo.h:64
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::AttributeList
Definition: Attributes.h:432
llvm::ValueMapper::mapMDNode
MDNode * mapMDNode(const MDNode &N)
Definition: ValueMapper.cpp:1172
llvm::StructType::getTypeByName
static StructType * getTypeByName(LLVMContext &C, StringRef Name)
Return the type with the specified name, or null if there is none by that name.
Definition: Type.cpp:623
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:420
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::IRMover::IdentifiedStructTypeSet::addNonOpaque
void addNonOpaque(StructType *Ty)
Definition: IRMover.cpp:1698
getTypeNamePrefix
static StringRef getTypeNamePrefix(StringRef Name)
Definition: IRMover.cpp:763
llvm::ValueMapper::scheduleMapGlobalInitializer
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1184
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::Intrinsic::remangleIntrinsicFunction
std::optional< Function * > remangleIntrinsicFunction(Function *F)
Definition: Function.cpp:1846
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::Triple::NVIDIA
@ NVIDIA
Definition: Triple.h:175
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:357
llvm::StructType::create
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:513
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::Module::Override
@ Override
Uses the specified value, regardless of the behavior or value of the other module.
Definition: Module.h:136
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::count
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
llvm::StructType::setName
void setName(StringRef Name)
Change the name of this type to the specified name, or to a name with a suffix if there is a collisio...
Definition: Type.cpp:462
llvm::Module::getGlobalList
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
Definition: Module.h:550
llvm::GlobalValue::hasExternalWeakLinkage
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:524
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1400
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1298
llvm::MDTuple::getDistinct
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition: Metadata.h:1368
llvm::AVR::getAddressSpace
AddressSpace getAddressSpace(T *V)
Definition: AVR.h:64
llvm::get
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Definition: PointerIntPair.h:234
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
llvm::NamedMDNode::addOperand
void addOperand(MDNode *M)
Definition: Metadata.cpp:1222
llvm::ValueMapper::scheduleRemapFunction
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1209
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: APInt.h:34
GlobalValue.h
llvm::Type::FunctionTyID
@ FunctionTyID
Functions.
Definition: Type.h:72
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:266
LinkDiagnosticInfo.h
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:420
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Definition: StringRef.h:260
llvm::ArrayType::getNumElements
uint64_t getNumElements() const
Definition: DerivedTypes.h:369
llvm::GlobalValue::hasAppendingLinkage
bool hasAppendingLinkage() const
Definition: GlobalValue.h:520
Constants.h
llvm::Module::Append
@ Append
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:139
SmallString.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DS_Warning
@ DS_Warning
Definition: DiagnosticInfo.h:51
llvm::StructType::isPacked
bool isPacked() const
Definition: DerivedTypes.h:273
llvm::IRMover::StructTypeKeyInfo::KeyTy::KeyTy
KeyTy(ArrayRef< Type * > E, bool P)
Definition: IRMover.cpp:1653
Intrinsics.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::IRMover::StructTypeKeyInfo::KeyTy::operator==
bool operator==(const KeyTy &that) const
Definition: IRMover.cpp:1659
llvm::IRMover::StructTypeKeyInfo::KeyTy::operator!=
bool operator!=(const KeyTy &that) const
Definition: IRMover.cpp:1663
false
Function Alias Analysis false
Definition: AliasAnalysis.cpp:772
llvm::GlobalValue::getThreadLocalMode
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:267
TypeFinder.h
llvm::MDNode::operands
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1290
llvm::MDTuple
Tuple of metadata.
Definition: Metadata.h:1329
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::PseudoProbeDescMetadataName
constexpr const char * PseudoProbeDescMetadataName
Definition: PseudoProbe.h:25
llvm::IRMover::IdentifiedStructTypeSet::addOpaque
void addOpaque(StructType *Ty)
Definition: IRMover.cpp:1711
GVMaterializer.h
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
llvm::DiagnosticSeverity
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
Definition: DiagnosticInfo.h:49
llvm::IRMover::move
Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue * > ValuesToLink, LazyCallback AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
Definition: IRMover.cpp:1748
llvm::ValueMapper::mapValue
Value * mapValue(const Value &V)
Definition: ValueMapper.cpp:1160
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:375
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:642
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:879
llvm::Module::getDataLayoutStr
const std::string & getDataLayoutStr() const
Get the data layout string for the module's target platform.
Definition: Module.h:249
SmallPtrSet.h
llvm::SIInstrFlags::IsPacked
@ IsPacked
Definition: SIDefines.h:102
llvm::DiagnosticInfo
This is the base abstract class for diagnostic reporting in the backend.
Definition: DiagnosticInfo.h:108
llvm::Type::PointerTyID
@ PointerTyID
Pointers.
Definition: Type.h:73
llvm::IRMover::IdentifiedStructTypeSet::hasType
bool hasType(StructType *Ty)
Definition: IRMover.cpp:1724
llvm::GlobalValue::hasGlobalUnnamedAddr
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:211
llvm::DiagnosticPrinter
Interface for custom diagnostic printing.
Definition: DiagnosticPrinter.h:31
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::Comdat
Definition: Comdat.h:33
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::Triple::getArch
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:354
llvm::Type::isFunctionTy
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:228
llvm::Module::setTargetTriple
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:301
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::SmallString< 16 >
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition: PointerTypeAnalysis.cpp:189
llvm::Module::AppendUnique
@ AppendUnique
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:144
llvm::ARM_PROC::IE
@ IE
Definition: ARMBaseInfo.h:27
llvm::ValueMapper
Context for (re-)mapping values (and metadata).
Definition: ValueMapper.h:143
llvm::InlineAsm
Definition: InlineAsm.h:33
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
llvm::Function::getAttributeAtIndex
Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const
gets the attribute from the list of attributes.
Definition: Function.cpp:646
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:135
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::IRMover::StructTypeKeyInfo::KeyTy::IsPacked
bool IsPacked
Definition: IRMover.h:30
uint64_t
llvm::GlobalValue::getVisibility
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:244
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
llvm::IRMover::IRMover
IRMover(Module &M)
Definition: IRMover.cpp:1731
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::StructType::isOpaque
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:281
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::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::logicalview::LVPrintKind::Elements
@ Elements
llvm::orc::Requirement
Requirement
Definition: DebugObjectManagerPlugin.cpp:116
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::DataLayout::isDefault
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:257
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:632
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:532
llvm::Module::getOrInsertComdat
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:582
llvm::LinkDiagnosticInfo::print
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
Definition: IRMover.cpp:352
llvm::Module::setDataLayout
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:392
llvm::NamedMDNode::getOperand
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1216
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1861
llvm::GlobalValue::getAddressSpace
unsigned getAddressSpace() const
Definition: GlobalValue.h:201
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:523
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:2010
llvm::Module::getModuleIdentifier
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:228
llvm::MDTuple::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1357
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:638
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
IRMover.h
llvm::Triple::armeb
@ armeb
Definition: Triple.h:50
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
Triple.h
ModuleSymbolTable.h
llvm::ValueMapper::addFlags
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
Definition: ValueMapper.cpp:1156
llvm::SmallPtrSetImplBase::clear
void clear()
Definition: SmallPtrSet.h:95
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::Triple::arm
@ arm
Definition: Triple.h:49
llvm::logicalview::LVCompareKind::Types
@ Types
elements
This compiles xmm1 mulss xmm1 xorps xmm0 movss xmm0 ret Because mulss doesn t modify the top elements
Definition: README-SSE.txt:221
llvm::SymbolTableList< GlobalVariable >
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
llvm::Module::Warning
@ Warning
Emits a warning if two values disagree.
Definition: Module.h:122
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::TypeFinder::run
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:34
llvm::TypeFinder
TypeFinder - Walk over a module, identifying all of the types that are used by the module.
Definition: TypeFinder.h:31
DP
So we should use XX3Form_Rcr to implement intrinsic Convert DP outs ins xscvdpsp No builtin are required Round &Convert QP DP(dword[1] is set to zero) No builtin are required Round to Quad Precision because you need to assign rounding mode in instruction Provide builtin(set f128:$vT,(int_ppc_vsx_xsrqpi f128:$vB))(set f128 yields< n x< ty > >< result > yields< ty >< result > No builtin are required Load Store load store see def memrix16 in PPCInstrInfo td Load Store Vector load store outs ins lxsdx set load store with conversion from to DP
Definition: README_P9.txt:520
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Type::getNumContainedTypes
unsigned getNumContainedTypes() const
Return the number of types in the derived type.
Definition: Type.h:363
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
llvm::ms_demangle::IntrinsicFunctionKind::New
@ New
llvm::Triple::thumb
@ thumb
Definition: Triple.h:83
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
getArrayElements
static void getArrayElements(const Constant *C, SmallVectorImpl< Constant * > &Dest)
Definition: IRMover.cpp:862
llvm::IRMover::IdentifiedStructTypeSet
Definition: IRMover.h:48
llvm::Module::Error
@ Error
Emits an error if two values disagree, otherwise the resulting value is that of the operands.
Definition: Module.h:118
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
llvm::GlobalIFunc::create
static GlobalIFunc * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Resolver, Module *Parent)
If a parent module is specified, the ifunc is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:572
llvm::GlobalValue::eraseFromParent
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:88
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::ConstantInt::getZExtValue
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:142
llvm::RF_IgnoreMissingLocals
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:89
llvm::StructType::hasName
bool hasName() const
Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:290
llvm::AArch64::Alias
StringRef Alias
Definition: AArch64TargetParser.h:140
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
std
Definition: BitVector.h:851
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
llvm::DIScope
Base class for scope-like contexts.
Definition: DebugInfoMetadata.h:509
llvm::GlobalAlias::create
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:511
llvm::ModuleSymbolTable::CollectAsmSymvers
static void CollectAsmSymvers(const Module &M, function_ref< void(StringRef, StringRef)> AsmSymver)
Parse inline ASM and collect the symvers directives that are defined in the current module.
Definition: ModuleSymbolTable.cpp:167
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:351
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
llvm::Type::FixedVectorTyID
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::StringRef::endswith
bool endswith(StringRef Suffix) const
Definition: StringRef.h:276
llvm::GlobalValue::getComdat
const Comdat * getComdat() const
Definition: Globals.cpp:176
llvm::Module::getContext
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:262
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:774
llvm::Module::getOrInsertNamedMetadata
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition: Module.cpp:260
llvm::Function::getFunctionType
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:175
llvm::ValueMap::find
iterator find(const KeyT &Val)
Definition: ValueMap.h:156
PseudoProbe.h
isEqual
static bool isEqual(const Function &Caller, const Function &Callee)
Definition: Attributes.cpp:1954
llvm::Module::Require
@ Require
Adds a requirement that another module flag be present and have a specified value after linking is pe...
Definition: Module.h:131
llvm::AMDGPU::VOPD::DST
@ DST
Definition: AMDGPUBaseInfo.h:523
llvm::LinkDiagnosticInfo::LinkDiagnosticInfo
LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg)
Definition: IRMover.cpp:349
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:475
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::ValueMapper::scheduleMapGlobalIFunc
void scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1204
llvm::Comdat::setSelectionKind
void setSelectionKind(SelectionKind Val)
Definition: Comdat.h:47
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:596
llvm::Type::StructTyID
@ StructTyID
Structures.
Definition: Type.h:74
forceRenaming
static void forceRenaming(GlobalValue *GV, StringRef Name)
The LLVM SymbolTable class autorenames globals that conflict in the symbol table.
Definition: IRMover.cpp:557
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2029
llvm::sys::path::filename
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
llvm::UpgradeModuleFlags
bool UpgradeModuleFlags(Module &M)
This checks for module flags which should be upgraded.
Definition: AutoUpgrade.cpp:4587
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:608
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:168
llvm::Triple::thumbeb
@ thumbeb
Definition: Triple.h:84
llvm::GlobalVariable::isConstant
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Definition: GlobalVariable.h:152
llvm::Module::getTargetTriple
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:258
llvm::DstOp
Definition: MachineIRBuilder.h:67
llvm::hash_combine_range
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:486
llvm::MDNode::op_end
op_iterator op_end() const
Definition: Metadata.h:1286
llvm::Module::getNamedMetadata
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:251
llvm::ValueMap::getMDMap
std::optional< MDMapT > & getMDMap()
Definition: ValueMap.h:120
llvm::MDTuple::push_back
void push_back(Metadata *MD)
Append an element to the tuple. This will resize the node.
Definition: Metadata.h:1386
llvm::GlobalValue::isDeclarationForLinker
bool isDeclarationForLinker() const
Definition: GlobalValue.h:614
llvm::LinkDiagnosticInfo
Definition: LinkDiagnosticInfo.h:15
llvm::Module::Max
@ Max
Takes the max of the two values, which are required to be integers.
Definition: Module.h:147
AutoUpgrade.h
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::erase
bool erase(const ValueT &V)
Definition: DenseSet.h:101
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::reverse
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:485
llvm::Type::ScalableVectorTyID
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:290
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:292
llvm::omp::RTLDependInfoFields::Flags
@ Flags
llvm::ValueMap::lookup
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: ValueMap.h:165
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:381
llvm::GlobalObject::setAlignment
void setAlignment(MaybeAlign Align)
Definition: Globals.cpp:121
llvm::GlobalValue::ExternalWeakLinkage
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:57
llvm::StructType::isLiteral
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
Definition: DerivedTypes.h:277
llvm::MDString
A single uniqued string.
Definition: Metadata.h:612
DiagnosticPrinter.h
llvm::ValueMaterializer
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:49
llvm::GlobalIFunc
Definition: GlobalIFunc.h:34
llvm::IRMover::IdentifiedStructTypeSet::switchToNonOpaque
void switchToNonOpaque(StructType *Ty)
Definition: IRMover.cpp:1703
CU
Definition: AArch64AsmBackend.cpp:505
llvm::MDNode::op_begin
op_iterator op_begin() const
Definition: Metadata.h:1282
llvm::Type::getContainedType
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition: Type.h:357
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::VectorType::get
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:668
adjustInlineAsm
static std::string adjustInlineAsm(const std::string &InlineAsm, const Triple &Triple)
Return InlineAsm adjusted with target-specific directives if required.
Definition: IRMover.cpp:1517
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::ArrayType::getElementType
Type * getElementType() const
Definition: DerivedTypes.h:370
llvm::SrcOp
Definition: MachineIRBuilder.h:128
llvm::Module::getNamedValue
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type.
Definition: Module.cpp:110
SetVector.h
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
llvm::ValueMapper::scheduleMapGlobalAlias
void scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee, unsigned MappingContextID=0)
Definition: ValueMapper.cpp:1199