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