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