1292915Sdim//===- lib/Linker/IRMover.cpp ---------------------------------------------===//
2292915Sdim//
3292915Sdim//                     The LLVM Compiler Infrastructure
4292915Sdim//
5292915Sdim// This file is distributed under the University of Illinois Open Source
6292915Sdim// License. See LICENSE.TXT for details.
7292915Sdim//
8292915Sdim//===----------------------------------------------------------------------===//
9292915Sdim
10292915Sdim#include "llvm/Linker/IRMover.h"
11292915Sdim#include "LinkDiagnosticInfo.h"
12292915Sdim#include "llvm/ADT/SetVector.h"
13292915Sdim#include "llvm/ADT/SmallString.h"
14292915Sdim#include "llvm/ADT/Triple.h"
15292915Sdim#include "llvm/IR/Constants.h"
16292915Sdim#include "llvm/IR/DebugInfo.h"
17292915Sdim#include "llvm/IR/DiagnosticPrinter.h"
18292915Sdim#include "llvm/IR/GVMaterializer.h"
19292915Sdim#include "llvm/IR/TypeFinder.h"
20292915Sdim#include "llvm/Transforms/Utils/Cloning.h"
21292915Sdimusing namespace llvm;
22292915Sdim
23292915Sdim//===----------------------------------------------------------------------===//
24292915Sdim// TypeMap implementation.
25292915Sdim//===----------------------------------------------------------------------===//
26292915Sdim
27292915Sdimnamespace {
28292915Sdimclass TypeMapTy : public ValueMapTypeRemapper {
29292915Sdim  /// This is a mapping from a source type to a destination type to use.
30292915Sdim  DenseMap<Type *, Type *> MappedTypes;
31292915Sdim
32292915Sdim  /// When checking to see if two subgraphs are isomorphic, we speculatively
33292915Sdim  /// add types to MappedTypes, but keep track of them here in case we need to
34292915Sdim  /// roll back.
35292915Sdim  SmallVector<Type *, 16> SpeculativeTypes;
36292915Sdim
37292915Sdim  SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
38292915Sdim
39292915Sdim  /// This is a list of non-opaque structs in the source module that are mapped
40292915Sdim  /// to an opaque struct in the destination module.
41292915Sdim  SmallVector<StructType *, 16> SrcDefinitionsToResolve;
42292915Sdim
43292915Sdim  /// This is the set of opaque types in the destination modules who are
44292915Sdim  /// getting a body from the source module.
45292915Sdim  SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
46292915Sdim
47292915Sdimpublic:
48292915Sdim  TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
49292915Sdim      : DstStructTypesSet(DstStructTypesSet) {}
50292915Sdim
51292915Sdim  IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
52292915Sdim  /// Indicate that the specified type in the destination module is conceptually
53292915Sdim  /// equivalent to the specified type in the source module.
54292915Sdim  void addTypeMapping(Type *DstTy, Type *SrcTy);
55292915Sdim
56292915Sdim  /// Produce a body for an opaque type in the dest module from a type
57292915Sdim  /// definition in the source module.
58292915Sdim  void linkDefinedTypeBodies();
59292915Sdim
60292915Sdim  /// Return the mapped type to use for the specified input type from the
61292915Sdim  /// source module.
62292915Sdim  Type *get(Type *SrcTy);
63292915Sdim  Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
64292915Sdim
65292915Sdim  void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
66292915Sdim
67292915Sdim  FunctionType *get(FunctionType *T) {
68292915Sdim    return cast<FunctionType>(get((Type *)T));
69292915Sdim  }
70292915Sdim
71292915Sdimprivate:
72292915Sdim  Type *remapType(Type *SrcTy) override { return get(SrcTy); }
73292915Sdim
74292915Sdim  bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
75292915Sdim};
76292915Sdim}
77292915Sdim
78292915Sdimvoid TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
79292915Sdim  assert(SpeculativeTypes.empty());
80292915Sdim  assert(SpeculativeDstOpaqueTypes.empty());
81292915Sdim
82292915Sdim  // Check to see if these types are recursively isomorphic and establish a
83292915Sdim  // mapping between them if so.
84292915Sdim  if (!areTypesIsomorphic(DstTy, SrcTy)) {
85292915Sdim    // Oops, they aren't isomorphic.  Just discard this request by rolling out
86292915Sdim    // any speculative mappings we've established.
87292915Sdim    for (Type *Ty : SpeculativeTypes)
88292915Sdim      MappedTypes.erase(Ty);
89292915Sdim
90292915Sdim    SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
91292915Sdim                                   SpeculativeDstOpaqueTypes.size());
92292915Sdim    for (StructType *Ty : SpeculativeDstOpaqueTypes)
93292915Sdim      DstResolvedOpaqueTypes.erase(Ty);
94292915Sdim  } else {
95292915Sdim    for (Type *Ty : SpeculativeTypes)
96292915Sdim      if (auto *STy = dyn_cast<StructType>(Ty))
97292915Sdim        if (STy->hasName())
98292915Sdim          STy->setName("");
99292915Sdim  }
100292915Sdim  SpeculativeTypes.clear();
101292915Sdim  SpeculativeDstOpaqueTypes.clear();
102292915Sdim}
103292915Sdim
104292915Sdim/// Recursively walk this pair of types, returning true if they are isomorphic,
105292915Sdim/// false if they are not.
106292915Sdimbool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
107292915Sdim  // Two types with differing kinds are clearly not isomorphic.
108292915Sdim  if (DstTy->getTypeID() != SrcTy->getTypeID())
109292915Sdim    return false;
110292915Sdim
111292915Sdim  // If we have an entry in the MappedTypes table, then we have our answer.
112292915Sdim  Type *&Entry = MappedTypes[SrcTy];
113292915Sdim  if (Entry)
114292915Sdim    return Entry == DstTy;
115292915Sdim
116292915Sdim  // Two identical types are clearly isomorphic.  Remember this
117292915Sdim  // non-speculatively.
118292915Sdim  if (DstTy == SrcTy) {
119292915Sdim    Entry = DstTy;
120292915Sdim    return true;
121292915Sdim  }
122292915Sdim
123292915Sdim  // Okay, we have two types with identical kinds that we haven't seen before.
124292915Sdim
125292915Sdim  // If this is an opaque struct type, special case it.
126292915Sdim  if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
127292915Sdim    // Mapping an opaque type to any struct, just keep the dest struct.
128292915Sdim    if (SSTy->isOpaque()) {
129292915Sdim      Entry = DstTy;
130292915Sdim      SpeculativeTypes.push_back(SrcTy);
131292915Sdim      return true;
132292915Sdim    }
133292915Sdim
134292915Sdim    // Mapping a non-opaque source type to an opaque dest.  If this is the first
135292915Sdim    // type that we're mapping onto this destination type then we succeed.  Keep
136292915Sdim    // the dest, but fill it in later. If this is the second (different) type
137292915Sdim    // that we're trying to map onto the same opaque type then we fail.
138292915Sdim    if (cast<StructType>(DstTy)->isOpaque()) {
139292915Sdim      // We can only map one source type onto the opaque destination type.
140292915Sdim      if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
141292915Sdim        return false;
142292915Sdim      SrcDefinitionsToResolve.push_back(SSTy);
143292915Sdim      SpeculativeTypes.push_back(SrcTy);
144292915Sdim      SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
145292915Sdim      Entry = DstTy;
146292915Sdim      return true;
147292915Sdim    }
148292915Sdim  }
149292915Sdim
150292915Sdim  // If the number of subtypes disagree between the two types, then we fail.
151292915Sdim  if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
152292915Sdim    return false;
153292915Sdim
154292915Sdim  // Fail if any of the extra properties (e.g. array size) of the type disagree.
155292915Sdim  if (isa<IntegerType>(DstTy))
156292915Sdim    return false; // bitwidth disagrees.
157292915Sdim  if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
158292915Sdim    if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
159292915Sdim      return false;
160292915Sdim
161292915Sdim  } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
162292915Sdim    if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
163292915Sdim      return false;
164292915Sdim  } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
165292915Sdim    StructType *SSTy = cast<StructType>(SrcTy);
166292915Sdim    if (DSTy->isLiteral() != SSTy->isLiteral() ||
167292915Sdim        DSTy->isPacked() != SSTy->isPacked())
168292915Sdim      return false;
169292915Sdim  } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
170292915Sdim    if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
171292915Sdim      return false;
172292915Sdim  } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
173292915Sdim    if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
174292915Sdim      return false;
175292915Sdim  }
176292915Sdim
177292915Sdim  // Otherwise, we speculate that these two types will line up and recursively
178292915Sdim  // check the subelements.
179292915Sdim  Entry = DstTy;
180292915Sdim  SpeculativeTypes.push_back(SrcTy);
181292915Sdim
182292915Sdim  for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
183292915Sdim    if (!areTypesIsomorphic(DstTy->getContainedType(I),
184292915Sdim                            SrcTy->getContainedType(I)))
185292915Sdim      return false;
186292915Sdim
187292915Sdim  // If everything seems to have lined up, then everything is great.
188292915Sdim  return true;
189292915Sdim}
190292915Sdim
191292915Sdimvoid TypeMapTy::linkDefinedTypeBodies() {
192292915Sdim  SmallVector<Type *, 16> Elements;
193292915Sdim  for (StructType *SrcSTy : SrcDefinitionsToResolve) {
194292915Sdim    StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
195292915Sdim    assert(DstSTy->isOpaque());
196292915Sdim
197292915Sdim    // Map the body of the source type over to a new body for the dest type.
198292915Sdim    Elements.resize(SrcSTy->getNumElements());
199292915Sdim    for (unsigned I = 0, E = Elements.size(); I != E; ++I)
200292915Sdim      Elements[I] = get(SrcSTy->getElementType(I));
201292915Sdim
202292915Sdim    DstSTy->setBody(Elements, SrcSTy->isPacked());
203292915Sdim    DstStructTypesSet.switchToNonOpaque(DstSTy);
204292915Sdim  }
205292915Sdim  SrcDefinitionsToResolve.clear();
206292915Sdim  DstResolvedOpaqueTypes.clear();
207292915Sdim}
208292915Sdim
209292915Sdimvoid TypeMapTy::finishType(StructType *DTy, StructType *STy,
210292915Sdim                           ArrayRef<Type *> ETypes) {
211292915Sdim  DTy->setBody(ETypes, STy->isPacked());
212292915Sdim
213292915Sdim  // Steal STy's name.
214292915Sdim  if (STy->hasName()) {
215292915Sdim    SmallString<16> TmpName = STy->getName();
216292915Sdim    STy->setName("");
217292915Sdim    DTy->setName(TmpName);
218292915Sdim  }
219292915Sdim
220292915Sdim  DstStructTypesSet.addNonOpaque(DTy);
221292915Sdim}
222292915Sdim
223292915SdimType *TypeMapTy::get(Type *Ty) {
224292915Sdim  SmallPtrSet<StructType *, 8> Visited;
225292915Sdim  return get(Ty, Visited);
226292915Sdim}
227292915Sdim
228292915SdimType *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
229292915Sdim  // If we already have an entry for this type, return it.
230292915Sdim  Type **Entry = &MappedTypes[Ty];
231292915Sdim  if (*Entry)
232292915Sdim    return *Entry;
233292915Sdim
234292915Sdim  // These are types that LLVM itself will unique.
235292915Sdim  bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
236292915Sdim
237292915Sdim#ifndef NDEBUG
238292915Sdim  if (!IsUniqued) {
239292915Sdim    for (auto &Pair : MappedTypes) {
240292915Sdim      assert(!(Pair.first != Ty && Pair.second == Ty) &&
241292915Sdim             "mapping to a source type");
242292915Sdim    }
243292915Sdim  }
244292915Sdim#endif
245292915Sdim
246292915Sdim  if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
247292915Sdim    StructType *DTy = StructType::create(Ty->getContext());
248292915Sdim    return *Entry = DTy;
249292915Sdim  }
250292915Sdim
251292915Sdim  // If this is not a recursive type, then just map all of the elements and
252292915Sdim  // then rebuild the type from inside out.
253292915Sdim  SmallVector<Type *, 4> ElementTypes;
254292915Sdim
255292915Sdim  // If there are no element types to map, then the type is itself.  This is
256292915Sdim  // true for the anonymous {} struct, things like 'float', integers, etc.
257292915Sdim  if (Ty->getNumContainedTypes() == 0 && IsUniqued)
258292915Sdim    return *Entry = Ty;
259292915Sdim
260292915Sdim  // Remap all of the elements, keeping track of whether any of them change.
261292915Sdim  bool AnyChange = false;
262292915Sdim  ElementTypes.resize(Ty->getNumContainedTypes());
263292915Sdim  for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
264292915Sdim    ElementTypes[I] = get(Ty->getContainedType(I), Visited);
265292915Sdim    AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
266292915Sdim  }
267292915Sdim
268292915Sdim  // If we found our type while recursively processing stuff, just use it.
269292915Sdim  Entry = &MappedTypes[Ty];
270292915Sdim  if (*Entry) {
271292915Sdim    if (auto *DTy = dyn_cast<StructType>(*Entry)) {
272292915Sdim      if (DTy->isOpaque()) {
273292915Sdim        auto *STy = cast<StructType>(Ty);
274292915Sdim        finishType(DTy, STy, ElementTypes);
275292915Sdim      }
276292915Sdim    }
277292915Sdim    return *Entry;
278292915Sdim  }
279292915Sdim
280292915Sdim  // If all of the element types mapped directly over and the type is not
281292915Sdim  // a nomed struct, then the type is usable as-is.
282292915Sdim  if (!AnyChange && IsUniqued)
283292915Sdim    return *Entry = Ty;
284292915Sdim
285292915Sdim  // Otherwise, rebuild a modified type.
286292915Sdim  switch (Ty->getTypeID()) {
287292915Sdim  default:
288292915Sdim    llvm_unreachable("unknown derived type to remap");
289292915Sdim  case Type::ArrayTyID:
290292915Sdim    return *Entry = ArrayType::get(ElementTypes[0],
291292915Sdim                                   cast<ArrayType>(Ty)->getNumElements());
292292915Sdim  case Type::VectorTyID:
293292915Sdim    return *Entry = VectorType::get(ElementTypes[0],
294292915Sdim                                    cast<VectorType>(Ty)->getNumElements());
295292915Sdim  case Type::PointerTyID:
296292915Sdim    return *Entry = PointerType::get(ElementTypes[0],
297292915Sdim                                     cast<PointerType>(Ty)->getAddressSpace());
298292915Sdim  case Type::FunctionTyID:
299292915Sdim    return *Entry = FunctionType::get(ElementTypes[0],
300292915Sdim                                      makeArrayRef(ElementTypes).slice(1),
301292915Sdim                                      cast<FunctionType>(Ty)->isVarArg());
302292915Sdim  case Type::StructTyID: {
303292915Sdim    auto *STy = cast<StructType>(Ty);
304292915Sdim    bool IsPacked = STy->isPacked();
305292915Sdim    if (IsUniqued)
306292915Sdim      return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
307292915Sdim
308292915Sdim    // If the type is opaque, we can just use it directly.
309292915Sdim    if (STy->isOpaque()) {
310292915Sdim      DstStructTypesSet.addOpaque(STy);
311292915Sdim      return *Entry = Ty;
312292915Sdim    }
313292915Sdim
314292915Sdim    if (StructType *OldT =
315292915Sdim            DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
316292915Sdim      STy->setName("");
317292915Sdim      return *Entry = OldT;
318292915Sdim    }
319292915Sdim
320292915Sdim    if (!AnyChange) {
321292915Sdim      DstStructTypesSet.addNonOpaque(STy);
322292915Sdim      return *Entry = Ty;
323292915Sdim    }
324292915Sdim
325292915Sdim    StructType *DTy = StructType::create(Ty->getContext());
326292915Sdim    finishType(DTy, STy, ElementTypes);
327292915Sdim    return *Entry = DTy;
328292915Sdim  }
329292915Sdim  }
330292915Sdim}
331292915Sdim
332292915SdimLinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
333292915Sdim                                       const Twine &Msg)
334292915Sdim    : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
335292915Sdimvoid LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
336292915Sdim
337292915Sdim//===----------------------------------------------------------------------===//
338292915Sdim// IRLinker implementation.
339292915Sdim//===----------------------------------------------------------------------===//
340292915Sdim
341292915Sdimnamespace {
342292915Sdimclass IRLinker;
343292915Sdim
344292915Sdim/// Creates prototypes for functions that are lazily linked on the fly. This
345292915Sdim/// speeds up linking for modules with many/ lazily linked functions of which
346292915Sdim/// few get used.
347292915Sdimclass GlobalValueMaterializer final : public ValueMaterializer {
348292915Sdim  IRLinker *TheIRLinker;
349292915Sdim
350292915Sdimpublic:
351292915Sdim  GlobalValueMaterializer(IRLinker *TheIRLinker) : TheIRLinker(TheIRLinker) {}
352292915Sdim  Value *materializeDeclFor(Value *V) override;
353292915Sdim  void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
354292915Sdim  Metadata *mapTemporaryMetadata(Metadata *MD) override;
355292915Sdim  void replaceTemporaryMetadata(const Metadata *OrigMD,
356292915Sdim                                Metadata *NewMD) override;
357292915Sdim  bool isMetadataNeeded(Metadata *MD) override;
358292915Sdim};
359292915Sdim
360292915Sdimclass LocalValueMaterializer final : public ValueMaterializer {
361292915Sdim  IRLinker *TheIRLinker;
362292915Sdim
363292915Sdimpublic:
364292915Sdim  LocalValueMaterializer(IRLinker *TheIRLinker) : TheIRLinker(TheIRLinker) {}
365292915Sdim  Value *materializeDeclFor(Value *V) override;
366292915Sdim  void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
367292915Sdim  Metadata *mapTemporaryMetadata(Metadata *MD) override;
368292915Sdim  void replaceTemporaryMetadata(const Metadata *OrigMD,
369292915Sdim                                Metadata *NewMD) override;
370292915Sdim  bool isMetadataNeeded(Metadata *MD) override;
371292915Sdim};
372292915Sdim
373292915Sdim/// This is responsible for keeping track of the state used for moving data
374292915Sdim/// from SrcM to DstM.
375292915Sdimclass IRLinker {
376292915Sdim  Module &DstM;
377292915Sdim  Module &SrcM;
378292915Sdim
379292915Sdim  std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
380292915Sdim
381292915Sdim  TypeMapTy TypeMap;
382292915Sdim  GlobalValueMaterializer GValMaterializer;
383292915Sdim  LocalValueMaterializer LValMaterializer;
384292915Sdim
385292915Sdim  /// Mapping of values from what they used to be in Src, to what they are now
386292915Sdim  /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
387292915Sdim  /// due to the use of Value handles which the Linker doesn't actually need,
388292915Sdim  /// but this allows us to reuse the ValueMapper code.
389292915Sdim  ValueToValueMapTy ValueMap;
390292915Sdim  ValueToValueMapTy AliasValueMap;
391292915Sdim
392292915Sdim  DenseSet<GlobalValue *> ValuesToLink;
393292915Sdim  std::vector<GlobalValue *> Worklist;
394292915Sdim
395292915Sdim  void maybeAdd(GlobalValue *GV) {
396292915Sdim    if (ValuesToLink.insert(GV).second)
397292915Sdim      Worklist.push_back(GV);
398292915Sdim  }
399292915Sdim
400292915Sdim  /// Set to true when all global value body linking is complete (including
401292915Sdim  /// lazy linking). Used to prevent metadata linking from creating new
402292915Sdim  /// references.
403292915Sdim  bool DoneLinkingBodies = false;
404292915Sdim
405292915Sdim  bool HasError = false;
406292915Sdim
407292915Sdim  /// Flag indicating that we are just linking metadata (after function
408292915Sdim  /// importing).
409292915Sdim  bool IsMetadataLinkingPostpass;
410292915Sdim
411292915Sdim  /// Flags to pass to value mapper invocations.
412292915Sdim  RemapFlags ValueMapperFlags = RF_MoveDistinctMDs;
413292915Sdim
414292915Sdim  /// Association between metadata values created during bitcode parsing and
415292915Sdim  /// the value id. Used to correlate temporary metadata created during
416292915Sdim  /// function importing with the final metadata parsed during the subsequent
417292915Sdim  /// metadata linking postpass.
418292915Sdim  DenseMap<const Metadata *, unsigned> MetadataToIDs;
419292915Sdim
420292915Sdim  /// Association between metadata value id and temporary metadata that
421292915Sdim  /// remains unmapped after function importing. Saved during function
422292915Sdim  /// importing and consumed during the metadata linking postpass.
423292915Sdim  DenseMap<unsigned, MDNode *> *ValIDToTempMDMap;
424292915Sdim
425292915Sdim  /// Set of subprogram metadata that does not need to be linked into the
426292915Sdim  /// destination module, because the functions were not imported directly
427292915Sdim  /// or via an inlined body in an imported function.
428292915Sdim  SmallPtrSet<const Metadata *, 16> UnneededSubprograms;
429292915Sdim
430292915Sdim  /// Handles cloning of a global values from the source module into
431292915Sdim  /// the destination module, including setting the attributes and visibility.
432292915Sdim  GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
433292915Sdim
434292915Sdim  /// Helper method for setting a message and returning an error code.
435292915Sdim  bool emitError(const Twine &Message) {
436292915Sdim    SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
437292915Sdim    HasError = true;
438292915Sdim    return true;
439292915Sdim  }
440292915Sdim
441292915Sdim  void emitWarning(const Twine &Message) {
442292915Sdim    SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
443292915Sdim  }
444292915Sdim
445292915Sdim  /// Check whether we should be linking metadata from the source module.
446292915Sdim  bool shouldLinkMetadata() {
447292915Sdim    // ValIDToTempMDMap will be non-null when we are importing or otherwise want
448292915Sdim    // to link metadata lazily, and then when linking the metadata.
449292915Sdim    // We only want to return true for the former case.
450292915Sdim    return ValIDToTempMDMap == nullptr || IsMetadataLinkingPostpass;
451292915Sdim  }
452292915Sdim
453292915Sdim  /// Given a global in the source module, return the global in the
454292915Sdim  /// destination module that is being linked to, if any.
455292915Sdim  GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
456292915Sdim    // If the source has no name it can't link.  If it has local linkage,
457292915Sdim    // there is no name match-up going on.
458292915Sdim    if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
459292915Sdim      return nullptr;
460292915Sdim
461292915Sdim    // Otherwise see if we have a match in the destination module's symtab.
462292915Sdim    GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
463292915Sdim    if (!DGV)
464292915Sdim      return nullptr;
465292915Sdim
466292915Sdim    // If we found a global with the same name in the dest module, but it has
467292915Sdim    // internal linkage, we are really not doing any linkage here.
468292915Sdim    if (DGV->hasLocalLinkage())
469292915Sdim      return nullptr;
470292915Sdim
471292915Sdim    // Otherwise, we do in fact link to the destination global.
472292915Sdim    return DGV;
473292915Sdim  }
474292915Sdim
475292915Sdim  void computeTypeMapping();
476292915Sdim
477292915Sdim  Constant *linkAppendingVarProto(GlobalVariable *DstGV,
478292915Sdim                                  const GlobalVariable *SrcGV);
479292915Sdim
480292915Sdim  bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
481292915Sdim  Constant *linkGlobalValueProto(GlobalValue *GV, bool ForAlias);
482292915Sdim
483292915Sdim  bool linkModuleFlagsMetadata();
484292915Sdim
485292915Sdim  void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
486292915Sdim  bool linkFunctionBody(Function &Dst, Function &Src);
487292915Sdim  void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
488292915Sdim  bool linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
489292915Sdim
490292915Sdim  /// Functions that take care of cloning a specific global value type
491292915Sdim  /// into the destination module.
492292915Sdim  GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
493292915Sdim  Function *copyFunctionProto(const Function *SF);
494292915Sdim  GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
495292915Sdim
496292915Sdim  void linkNamedMDNodes();
497292915Sdim
498292915Sdim  /// Populate the UnneededSubprograms set with the DISubprogram metadata
499292915Sdim  /// from the source module that we don't need to link into the dest module,
500292915Sdim  /// because the functions were not imported directly or via an inlined body
501292915Sdim  /// in an imported function.
502292915Sdim  void findNeededSubprograms(ValueToValueMapTy &ValueMap);
503292915Sdim
504292915Sdim  /// The value mapper leaves nulls in the list of subprograms for any
505292915Sdim  /// in the UnneededSubprograms map. Strip those out after metadata linking.
506292915Sdim  void stripNullSubprograms();
507292915Sdim
508292915Sdimpublic:
509292915Sdim  IRLinker(Module &DstM, IRMover::IdentifiedStructTypeSet &Set, Module &SrcM,
510292915Sdim           ArrayRef<GlobalValue *> ValuesToLink,
511292915Sdim           std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
512292915Sdim           DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr,
513292915Sdim           bool IsMetadataLinkingPostpass = false)
514292915Sdim      : DstM(DstM), SrcM(SrcM), AddLazyFor(AddLazyFor), TypeMap(Set),
515292915Sdim        GValMaterializer(this), LValMaterializer(this),
516292915Sdim        IsMetadataLinkingPostpass(IsMetadataLinkingPostpass),
517292915Sdim        ValIDToTempMDMap(ValIDToTempMDMap) {
518292915Sdim    for (GlobalValue *GV : ValuesToLink)
519292915Sdim      maybeAdd(GV);
520292915Sdim
521292915Sdim    // If appropriate, tell the value mapper that it can expect to see
522292915Sdim    // temporary metadata.
523292915Sdim    if (!shouldLinkMetadata())
524292915Sdim      ValueMapperFlags = ValueMapperFlags | RF_HaveUnmaterializedMetadata;
525292915Sdim  }
526292915Sdim
527293265Sdim  ~IRLinker() {
528293265Sdim    // In the case where we are not linking metadata, we unset the CanReplace
529293265Sdim    // flag on all temporary metadata in the MetadataToIDs map to ensure
530293265Sdim    // none was replaced while being a map key. Now that we are destructing
531293265Sdim    // the map, set the flag back to true, so that it is replaceable during
532293265Sdim    // metadata linking.
533293265Sdim    if (!shouldLinkMetadata()) {
534293265Sdim      for (auto MDI : MetadataToIDs) {
535293265Sdim        Metadata *MD = const_cast<Metadata *>(MDI.first);
536293265Sdim        MDNode *Node = dyn_cast<MDNode>(MD);
537293265Sdim        assert((Node && Node->isTemporary()) &&
538293265Sdim               "Found non-temp metadata in map when not linking metadata");
539293265Sdim        Node->setCanReplace(true);
540293265Sdim      }
541293265Sdim    }
542293265Sdim  }
543293265Sdim
544292915Sdim  bool run();
545292915Sdim  Value *materializeDeclFor(Value *V, bool ForAlias);
546292915Sdim  void materializeInitFor(GlobalValue *New, GlobalValue *Old, bool ForAlias);
547292915Sdim
548292915Sdim  /// Save the mapping between the given temporary metadata and its metadata
549292915Sdim  /// value id. Used to support metadata linking as a postpass for function
550292915Sdim  /// importing.
551292915Sdim  Metadata *mapTemporaryMetadata(Metadata *MD);
552292915Sdim
553292915Sdim  /// Replace any temporary metadata saved for the source metadata's id with
554292915Sdim  /// the new non-temporary metadata. Used when metadata linking as a postpass
555292915Sdim  /// for function importing.
556292915Sdim  void replaceTemporaryMetadata(const Metadata *OrigMD, Metadata *NewMD);
557292915Sdim
558292915Sdim  /// Indicates whether we need to map the given metadata into the destination
559292915Sdim  /// module. Used to prevent linking of metadata only needed by functions not
560292915Sdim  /// linked into the dest module.
561292915Sdim  bool isMetadataNeeded(Metadata *MD);
562292915Sdim};
563292915Sdim}
564292915Sdim
565292915Sdim/// The LLVM SymbolTable class autorenames globals that conflict in the symbol
566292915Sdim/// table. This is good for all clients except for us. Go through the trouble
567292915Sdim/// to force this back.
568292915Sdimstatic void forceRenaming(GlobalValue *GV, StringRef Name) {
569292915Sdim  // If the global doesn't force its name or if it already has the right name,
570292915Sdim  // there is nothing for us to do.
571292915Sdim  if (GV->hasLocalLinkage() || GV->getName() == Name)
572292915Sdim    return;
573292915Sdim
574292915Sdim  Module *M = GV->getParent();
575292915Sdim
576292915Sdim  // If there is a conflict, rename the conflict.
577292915Sdim  if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
578292915Sdim    GV->takeName(ConflictGV);
579292915Sdim    ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
580292915Sdim    assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
581292915Sdim  } else {
582292915Sdim    GV->setName(Name); // Force the name back
583292915Sdim  }
584292915Sdim}
585292915Sdim
586292915SdimValue *GlobalValueMaterializer::materializeDeclFor(Value *V) {
587292915Sdim  return TheIRLinker->materializeDeclFor(V, false);
588292915Sdim}
589292915Sdim
590292915Sdimvoid GlobalValueMaterializer::materializeInitFor(GlobalValue *New,
591292915Sdim                                                 GlobalValue *Old) {
592292915Sdim  TheIRLinker->materializeInitFor(New, Old, false);
593292915Sdim}
594292915Sdim
595292915SdimMetadata *GlobalValueMaterializer::mapTemporaryMetadata(Metadata *MD) {
596292915Sdim  return TheIRLinker->mapTemporaryMetadata(MD);
597292915Sdim}
598292915Sdim
599292915Sdimvoid GlobalValueMaterializer::replaceTemporaryMetadata(const Metadata *OrigMD,
600292915Sdim                                                       Metadata *NewMD) {
601292915Sdim  TheIRLinker->replaceTemporaryMetadata(OrigMD, NewMD);
602292915Sdim}
603292915Sdim
604292915Sdimbool GlobalValueMaterializer::isMetadataNeeded(Metadata *MD) {
605292915Sdim  return TheIRLinker->isMetadataNeeded(MD);
606292915Sdim}
607292915Sdim
608292915SdimValue *LocalValueMaterializer::materializeDeclFor(Value *V) {
609292915Sdim  return TheIRLinker->materializeDeclFor(V, true);
610292915Sdim}
611292915Sdim
612292915Sdimvoid LocalValueMaterializer::materializeInitFor(GlobalValue *New,
613292915Sdim                                                GlobalValue *Old) {
614292915Sdim  TheIRLinker->materializeInitFor(New, Old, true);
615292915Sdim}
616292915Sdim
617292915SdimMetadata *LocalValueMaterializer::mapTemporaryMetadata(Metadata *MD) {
618292915Sdim  return TheIRLinker->mapTemporaryMetadata(MD);
619292915Sdim}
620292915Sdim
621292915Sdimvoid LocalValueMaterializer::replaceTemporaryMetadata(const Metadata *OrigMD,
622292915Sdim                                                      Metadata *NewMD) {
623292915Sdim  TheIRLinker->replaceTemporaryMetadata(OrigMD, NewMD);
624292915Sdim}
625292915Sdim
626292915Sdimbool LocalValueMaterializer::isMetadataNeeded(Metadata *MD) {
627292915Sdim  return TheIRLinker->isMetadataNeeded(MD);
628292915Sdim}
629292915Sdim
630292915SdimValue *IRLinker::materializeDeclFor(Value *V, bool ForAlias) {
631292915Sdim  auto *SGV = dyn_cast<GlobalValue>(V);
632292915Sdim  if (!SGV)
633292915Sdim    return nullptr;
634292915Sdim
635292915Sdim  return linkGlobalValueProto(SGV, ForAlias);
636292915Sdim}
637292915Sdim
638292915Sdimvoid IRLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old,
639292915Sdim                                  bool ForAlias) {
640292915Sdim  // If we already created the body, just return.
641292915Sdim  if (auto *F = dyn_cast<Function>(New)) {
642292915Sdim    if (!F->isDeclaration())
643292915Sdim      return;
644292915Sdim  } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
645292915Sdim    if (V->hasInitializer())
646292915Sdim      return;
647292915Sdim  } else {
648292915Sdim    auto *A = cast<GlobalAlias>(New);
649292915Sdim    if (A->getAliasee())
650292915Sdim      return;
651292915Sdim  }
652292915Sdim
653292915Sdim  if (ForAlias || shouldLink(New, *Old))
654292915Sdim    linkGlobalValueBody(*New, *Old);
655292915Sdim}
656292915Sdim
657292915SdimMetadata *IRLinker::mapTemporaryMetadata(Metadata *MD) {
658292915Sdim  if (!ValIDToTempMDMap)
659292915Sdim    return nullptr;
660292915Sdim  // If this temporary metadata has a value id recorded during function
661292915Sdim  // parsing, record that in the ValIDToTempMDMap if one was provided.
662292915Sdim  if (MetadataToIDs.count(MD)) {
663292915Sdim    unsigned Idx = MetadataToIDs[MD];
664292915Sdim    // Check if we created a temp MD when importing a different function from
665292915Sdim    // this module. If so, reuse it the same temporary metadata, otherwise
666292915Sdim    // add this temporary metadata to the map.
667292915Sdim    if (!ValIDToTempMDMap->count(Idx)) {
668292915Sdim      MDNode *Node = cast<MDNode>(MD);
669292915Sdim      assert(Node->isTemporary());
670292915Sdim      (*ValIDToTempMDMap)[Idx] = Node;
671292915Sdim    }
672292915Sdim    return (*ValIDToTempMDMap)[Idx];
673292915Sdim  }
674292915Sdim  return nullptr;
675292915Sdim}
676292915Sdim
677292915Sdimvoid IRLinker::replaceTemporaryMetadata(const Metadata *OrigMD,
678292915Sdim                                        Metadata *NewMD) {
679292915Sdim  if (!ValIDToTempMDMap)
680292915Sdim    return;
681292915Sdim#ifndef NDEBUG
682292915Sdim  auto *N = dyn_cast_or_null<MDNode>(NewMD);
683292915Sdim  assert(!N || !N->isTemporary());
684292915Sdim#endif
685292915Sdim  // If a mapping between metadata value ids and temporary metadata
686292915Sdim  // created during function importing was provided, and the source
687292915Sdim  // metadata has a value id recorded during metadata parsing, replace
688292915Sdim  // the temporary metadata with the final mapped metadata now.
689292915Sdim  if (MetadataToIDs.count(OrigMD)) {
690292915Sdim    unsigned Idx = MetadataToIDs[OrigMD];
691292915Sdim    // Nothing to do if we didn't need to create a temporary metadata during
692292915Sdim    // function importing.
693292915Sdim    if (!ValIDToTempMDMap->count(Idx))
694292915Sdim      return;
695292915Sdim    MDNode *TempMD = (*ValIDToTempMDMap)[Idx];
696292915Sdim    TempMD->replaceAllUsesWith(NewMD);
697292915Sdim    MDNode::deleteTemporary(TempMD);
698292915Sdim    ValIDToTempMDMap->erase(Idx);
699292915Sdim  }
700292915Sdim}
701292915Sdim
702292915Sdimbool IRLinker::isMetadataNeeded(Metadata *MD) {
703292915Sdim  // Currently only DISubprogram metadata is marked as being unneeded.
704292915Sdim  if (UnneededSubprograms.empty())
705292915Sdim    return true;
706292915Sdim  MDNode *Node = dyn_cast<MDNode>(MD);
707292915Sdim  if (!Node)
708292915Sdim    return true;
709292915Sdim  DISubprogram *SP = getDISubprogram(Node);
710292915Sdim  if (!SP)
711292915Sdim    return true;
712292915Sdim  return !UnneededSubprograms.count(SP);
713292915Sdim}
714292915Sdim
715292915Sdim/// Loop through the global variables in the src module and merge them into the
716292915Sdim/// dest module.
717292915SdimGlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
718292915Sdim  // No linking to be performed or linking from the source: simply create an
719292915Sdim  // identical version of the symbol over in the dest module... the
720292915Sdim  // initializer will be filled in later by LinkGlobalInits.
721292915Sdim  GlobalVariable *NewDGV =
722292915Sdim      new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),
723292915Sdim                         SGVar->isConstant(), GlobalValue::ExternalLinkage,
724292915Sdim                         /*init*/ nullptr, SGVar->getName(),
725292915Sdim                         /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
726292915Sdim                         SGVar->getType()->getAddressSpace());
727292915Sdim  NewDGV->setAlignment(SGVar->getAlignment());
728292915Sdim  return NewDGV;
729292915Sdim}
730292915Sdim
731292915Sdim/// Link the function in the source module into the destination module if
732292915Sdim/// needed, setting up mapping information.
733292915SdimFunction *IRLinker::copyFunctionProto(const Function *SF) {
734292915Sdim  // If there is no linkage to be performed or we are linking from the source,
735292915Sdim  // bring SF over.
736292915Sdim  return Function::Create(TypeMap.get(SF->getFunctionType()),
737292915Sdim                          GlobalValue::ExternalLinkage, SF->getName(), &DstM);
738292915Sdim}
739292915Sdim
740292915Sdim/// Set up prototypes for any aliases that come over from the source module.
741292915SdimGlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
742292915Sdim  // If there is no linkage to be performed or we're linking from the source,
743292915Sdim  // bring over SGA.
744292915Sdim  auto *Ty = TypeMap.get(SGA->getValueType());
745292915Sdim  return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
746292915Sdim                             GlobalValue::ExternalLinkage, SGA->getName(),
747292915Sdim                             &DstM);
748292915Sdim}
749292915Sdim
750292915SdimGlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
751292915Sdim                                            bool ForDefinition) {
752292915Sdim  GlobalValue *NewGV;
753292915Sdim  if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
754292915Sdim    NewGV = copyGlobalVariableProto(SGVar);
755292915Sdim  } else if (auto *SF = dyn_cast<Function>(SGV)) {
756292915Sdim    NewGV = copyFunctionProto(SF);
757292915Sdim  } else {
758292915Sdim    if (ForDefinition)
759292915Sdim      NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
760292915Sdim    else
761292915Sdim      NewGV = new GlobalVariable(
762292915Sdim          DstM, TypeMap.get(SGV->getType()->getElementType()),
763292915Sdim          /*isConstant*/ false, GlobalValue::ExternalLinkage,
764292915Sdim          /*init*/ nullptr, SGV->getName(),
765292915Sdim          /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
766292915Sdim          SGV->getType()->getAddressSpace());
767292915Sdim  }
768292915Sdim
769292915Sdim  if (ForDefinition)
770292915Sdim    NewGV->setLinkage(SGV->getLinkage());
771292915Sdim  else if (SGV->hasExternalWeakLinkage() || SGV->hasWeakLinkage() ||
772292915Sdim           SGV->hasLinkOnceLinkage())
773292915Sdim    NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
774292915Sdim
775292915Sdim  NewGV->copyAttributesFrom(SGV);
776294024Sdim
777294024Sdim  // Remove these copied constants in case this stays a declaration, since
778294024Sdim  // they point to the source module. If the def is linked the values will
779294024Sdim  // be mapped in during linkFunctionBody.
780294024Sdim  if (auto *NewF = dyn_cast<Function>(NewGV)) {
781294024Sdim    NewF->setPersonalityFn(nullptr);
782294024Sdim    NewF->setPrefixData(nullptr);
783294024Sdim    NewF->setPrologueData(nullptr);
784294024Sdim  }
785294024Sdim
786292915Sdim  return NewGV;
787292915Sdim}
788292915Sdim
789292915Sdim/// Loop over all of the linked values to compute type mappings.  For example,
790292915Sdim/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
791292915Sdim/// types 'Foo' but one got renamed when the module was loaded into the same
792292915Sdim/// LLVMContext.
793292915Sdimvoid IRLinker::computeTypeMapping() {
794292915Sdim  for (GlobalValue &SGV : SrcM.globals()) {
795292915Sdim    GlobalValue *DGV = getLinkedToGlobal(&SGV);
796292915Sdim    if (!DGV)
797292915Sdim      continue;
798292915Sdim
799292915Sdim    if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
800292915Sdim      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
801292915Sdim      continue;
802292915Sdim    }
803292915Sdim
804292915Sdim    // Unify the element type of appending arrays.
805292915Sdim    ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
806292915Sdim    ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
807292915Sdim    TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
808292915Sdim  }
809292915Sdim
810292915Sdim  for (GlobalValue &SGV : SrcM)
811292915Sdim    if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
812292915Sdim      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
813292915Sdim
814292915Sdim  for (GlobalValue &SGV : SrcM.aliases())
815292915Sdim    if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
816292915Sdim      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
817292915Sdim
818292915Sdim  // Incorporate types by name, scanning all the types in the source module.
819292915Sdim  // At this point, the destination module may have a type "%foo = { i32 }" for
820292915Sdim  // example.  When the source module got loaded into the same LLVMContext, if
821292915Sdim  // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
822292915Sdim  std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
823292915Sdim  for (StructType *ST : Types) {
824292915Sdim    if (!ST->hasName())
825292915Sdim      continue;
826292915Sdim
827292915Sdim    // Check to see if there is a dot in the name followed by a digit.
828292915Sdim    size_t DotPos = ST->getName().rfind('.');
829292915Sdim    if (DotPos == 0 || DotPos == StringRef::npos ||
830292915Sdim        ST->getName().back() == '.' ||
831292915Sdim        !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
832292915Sdim      continue;
833292915Sdim
834292915Sdim    // Check to see if the destination module has a struct with the prefix name.
835292915Sdim    StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
836292915Sdim    if (!DST)
837292915Sdim      continue;
838292915Sdim
839292915Sdim    // Don't use it if this actually came from the source module. They're in
840292915Sdim    // the same LLVMContext after all. Also don't use it unless the type is
841292915Sdim    // actually used in the destination module. This can happen in situations
842292915Sdim    // like this:
843292915Sdim    //
844292915Sdim    //      Module A                         Module B
845292915Sdim    //      --------                         --------
846292915Sdim    //   %Z = type { %A }                %B = type { %C.1 }
847292915Sdim    //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
848292915Sdim    //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
849292915Sdim    //   %C = type { i8* }               %B.3 = type { %C.1 }
850292915Sdim    //
851292915Sdim    // When we link Module B with Module A, the '%B' in Module B is
852292915Sdim    // used. However, that would then use '%C.1'. But when we process '%C.1',
853292915Sdim    // we prefer to take the '%C' version. So we are then left with both
854292915Sdim    // '%C.1' and '%C' being used for the same types. This leads to some
855292915Sdim    // variables using one type and some using the other.
856292915Sdim    if (TypeMap.DstStructTypesSet.hasType(DST))
857292915Sdim      TypeMap.addTypeMapping(DST, ST);
858292915Sdim  }
859292915Sdim
860292915Sdim  // Now that we have discovered all of the type equivalences, get a body for
861292915Sdim  // any 'opaque' types in the dest module that are now resolved.
862292915Sdim  TypeMap.linkDefinedTypeBodies();
863292915Sdim}
864292915Sdim
865292915Sdimstatic void getArrayElements(const Constant *C,
866292915Sdim                             SmallVectorImpl<Constant *> &Dest) {
867292915Sdim  unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
868292915Sdim
869292915Sdim  for (unsigned i = 0; i != NumElements; ++i)
870292915Sdim    Dest.push_back(C->getAggregateElement(i));
871292915Sdim}
872292915Sdim
873292915Sdim/// If there were any appending global variables, link them together now.
874292915Sdim/// Return true on error.
875292915SdimConstant *IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
876292915Sdim                                          const GlobalVariable *SrcGV) {
877292915Sdim  Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()))
878292915Sdim                    ->getElementType();
879292915Sdim
880292915Sdim  StringRef Name = SrcGV->getName();
881292915Sdim  bool IsNewStructor = false;
882292915Sdim  bool IsOldStructor = false;
883292915Sdim  if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
884292915Sdim    if (cast<StructType>(EltTy)->getNumElements() == 3)
885292915Sdim      IsNewStructor = true;
886292915Sdim    else
887292915Sdim      IsOldStructor = true;
888292915Sdim  }
889292915Sdim
890292915Sdim  PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
891292915Sdim  if (IsOldStructor) {
892292915Sdim    auto &ST = *cast<StructType>(EltTy);
893292915Sdim    Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
894292915Sdim    EltTy = StructType::get(SrcGV->getContext(), Tys, false);
895292915Sdim  }
896292915Sdim
897292915Sdim  if (DstGV) {
898292915Sdim    ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
899292915Sdim
900292915Sdim    if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) {
901292915Sdim      emitError(
902292915Sdim          "Linking globals named '" + SrcGV->getName() +
903292915Sdim          "': can only link appending global with another appending global!");
904292915Sdim      return nullptr;
905292915Sdim    }
906292915Sdim
907292915Sdim    // Check to see that they two arrays agree on type.
908292915Sdim    if (EltTy != DstTy->getElementType()) {
909292915Sdim      emitError("Appending variables with different element types!");
910292915Sdim      return nullptr;
911292915Sdim    }
912292915Sdim    if (DstGV->isConstant() != SrcGV->isConstant()) {
913292915Sdim      emitError("Appending variables linked with different const'ness!");
914292915Sdim      return nullptr;
915292915Sdim    }
916292915Sdim
917292915Sdim    if (DstGV->getAlignment() != SrcGV->getAlignment()) {
918292915Sdim      emitError(
919292915Sdim          "Appending variables with different alignment need to be linked!");
920292915Sdim      return nullptr;
921292915Sdim    }
922292915Sdim
923292915Sdim    if (DstGV->getVisibility() != SrcGV->getVisibility()) {
924292915Sdim      emitError(
925292915Sdim          "Appending variables with different visibility need to be linked!");
926292915Sdim      return nullptr;
927292915Sdim    }
928292915Sdim
929292915Sdim    if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) {
930292915Sdim      emitError(
931292915Sdim          "Appending variables with different unnamed_addr need to be linked!");
932292915Sdim      return nullptr;
933292915Sdim    }
934292915Sdim
935292915Sdim    if (StringRef(DstGV->getSection()) != SrcGV->getSection()) {
936292915Sdim      emitError(
937292915Sdim          "Appending variables with different section name need to be linked!");
938292915Sdim      return nullptr;
939292915Sdim    }
940292915Sdim  }
941292915Sdim
942292915Sdim  SmallVector<Constant *, 16> DstElements;
943292915Sdim  if (DstGV)
944292915Sdim    getArrayElements(DstGV->getInitializer(), DstElements);
945292915Sdim
946292915Sdim  SmallVector<Constant *, 16> SrcElements;
947292915Sdim  getArrayElements(SrcGV->getInitializer(), SrcElements);
948292915Sdim
949292915Sdim  if (IsNewStructor)
950292915Sdim    SrcElements.erase(
951292915Sdim        std::remove_if(SrcElements.begin(), SrcElements.end(),
952292915Sdim                       [this](Constant *E) {
953292915Sdim                         auto *Key = dyn_cast<GlobalValue>(
954292915Sdim                             E->getAggregateElement(2)->stripPointerCasts());
955292915Sdim                         if (!Key)
956292915Sdim                           return false;
957292915Sdim                         GlobalValue *DGV = getLinkedToGlobal(Key);
958292915Sdim                         return !shouldLink(DGV, *Key);
959292915Sdim                       }),
960292915Sdim        SrcElements.end());
961292915Sdim  uint64_t NewSize = DstElements.size() + SrcElements.size();
962292915Sdim  ArrayType *NewType = ArrayType::get(EltTy, NewSize);
963292915Sdim
964292915Sdim  // Create the new global variable.
965292915Sdim  GlobalVariable *NG = new GlobalVariable(
966292915Sdim      DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
967292915Sdim      /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
968292915Sdim      SrcGV->getType()->getAddressSpace());
969292915Sdim
970292915Sdim  NG->copyAttributesFrom(SrcGV);
971292915Sdim  forceRenaming(NG, SrcGV->getName());
972292915Sdim
973292915Sdim  Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
974292915Sdim
975292915Sdim  // Stop recursion.
976292915Sdim  ValueMap[SrcGV] = Ret;
977292915Sdim
978292915Sdim  for (auto *V : SrcElements) {
979292915Sdim    Constant *NewV;
980292915Sdim    if (IsOldStructor) {
981292915Sdim      auto *S = cast<ConstantStruct>(V);
982292915Sdim      auto *E1 = MapValue(S->getOperand(0), ValueMap, ValueMapperFlags,
983292915Sdim                          &TypeMap, &GValMaterializer);
984292915Sdim      auto *E2 = MapValue(S->getOperand(1), ValueMap, ValueMapperFlags,
985292915Sdim                          &TypeMap, &GValMaterializer);
986292915Sdim      Value *Null = Constant::getNullValue(VoidPtrTy);
987292915Sdim      NewV =
988292915Sdim          ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
989292915Sdim    } else {
990292915Sdim      NewV =
991292915Sdim          MapValue(V, ValueMap, ValueMapperFlags, &TypeMap, &GValMaterializer);
992292915Sdim    }
993292915Sdim    DstElements.push_back(NewV);
994292915Sdim  }
995292915Sdim
996292915Sdim  NG->setInitializer(ConstantArray::get(NewType, DstElements));
997292915Sdim
998292915Sdim  // Replace any uses of the two global variables with uses of the new
999292915Sdim  // global.
1000292915Sdim  if (DstGV) {
1001292915Sdim    DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
1002292915Sdim    DstGV->eraseFromParent();
1003292915Sdim  }
1004292915Sdim
1005292915Sdim  return Ret;
1006292915Sdim}
1007292915Sdim
1008292915Sdimstatic bool useExistingDest(GlobalValue &SGV, GlobalValue *DGV,
1009292915Sdim                            bool ShouldLink) {
1010292915Sdim  if (!DGV)
1011292915Sdim    return false;
1012292915Sdim
1013292915Sdim  if (SGV.isDeclaration())
1014292915Sdim    return true;
1015292915Sdim
1016292915Sdim  if (DGV->isDeclarationForLinker() && !SGV.isDeclarationForLinker())
1017292915Sdim    return false;
1018292915Sdim
1019292915Sdim  if (ShouldLink)
1020292915Sdim    return false;
1021292915Sdim
1022292915Sdim  return true;
1023292915Sdim}
1024292915Sdim
1025292915Sdimbool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
1026292915Sdim  // Already imported all the values. Just map to the Dest value
1027292915Sdim  // in case it is referenced in the metadata.
1028292915Sdim  if (IsMetadataLinkingPostpass) {
1029292915Sdim    assert(!ValuesToLink.count(&SGV) &&
1030292915Sdim           "Source value unexpectedly requested for link during metadata link");
1031292915Sdim    return false;
1032292915Sdim  }
1033292915Sdim
1034292915Sdim  if (ValuesToLink.count(&SGV))
1035292915Sdim    return true;
1036292915Sdim
1037292915Sdim  if (SGV.hasLocalLinkage())
1038292915Sdim    return true;
1039292915Sdim
1040292915Sdim  if (DGV && !DGV->isDeclaration())
1041292915Sdim    return false;
1042292915Sdim
1043292915Sdim  if (SGV.hasAvailableExternallyLinkage())
1044292915Sdim    return true;
1045292915Sdim
1046292915Sdim  if (DoneLinkingBodies)
1047292915Sdim    return false;
1048292915Sdim
1049292915Sdim  AddLazyFor(SGV, [this](GlobalValue &GV) { maybeAdd(&GV); });
1050292915Sdim  return ValuesToLink.count(&SGV);
1051292915Sdim}
1052292915Sdim
1053292915SdimConstant *IRLinker::linkGlobalValueProto(GlobalValue *SGV, bool ForAlias) {
1054292915Sdim  GlobalValue *DGV = getLinkedToGlobal(SGV);
1055292915Sdim
1056292915Sdim  bool ShouldLink = shouldLink(DGV, *SGV);
1057292915Sdim
1058292915Sdim  // just missing from map
1059292915Sdim  if (ShouldLink) {
1060292915Sdim    auto I = ValueMap.find(SGV);
1061292915Sdim    if (I != ValueMap.end())
1062292915Sdim      return cast<Constant>(I->second);
1063292915Sdim
1064292915Sdim    I = AliasValueMap.find(SGV);
1065292915Sdim    if (I != AliasValueMap.end())
1066292915Sdim      return cast<Constant>(I->second);
1067292915Sdim  }
1068292915Sdim
1069292915Sdim  DGV = nullptr;
1070292915Sdim  if (ShouldLink || !ForAlias)
1071292915Sdim    DGV = getLinkedToGlobal(SGV);
1072292915Sdim
1073292915Sdim  // Handle the ultra special appending linkage case first.
1074292915Sdim  assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
1075292915Sdim  if (SGV->hasAppendingLinkage())
1076292915Sdim    return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
1077292915Sdim                                 cast<GlobalVariable>(SGV));
1078292915Sdim
1079292915Sdim  GlobalValue *NewGV;
1080292915Sdim  if (useExistingDest(*SGV, DGV, ShouldLink)) {
1081292915Sdim    NewGV = DGV;
1082292915Sdim  } else {
1083292915Sdim    // If we are done linking global value bodies (i.e. we are performing
1084292915Sdim    // metadata linking), don't link in the global value due to this
1085292915Sdim    // reference, simply map it to null.
1086292915Sdim    if (DoneLinkingBodies)
1087292915Sdim      return nullptr;
1088292915Sdim
1089292915Sdim    NewGV = copyGlobalValueProto(SGV, ShouldLink);
1090292915Sdim    if (!ForAlias)
1091292915Sdim      forceRenaming(NewGV, SGV->getName());
1092292915Sdim  }
1093292915Sdim  if (ShouldLink || ForAlias) {
1094292915Sdim    if (const Comdat *SC = SGV->getComdat()) {
1095292915Sdim      if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
1096292915Sdim        Comdat *DC = DstM.getOrInsertComdat(SC->getName());
1097292915Sdim        DC->setSelectionKind(SC->getSelectionKind());
1098292915Sdim        GO->setComdat(DC);
1099292915Sdim      }
1100292915Sdim    }
1101292915Sdim  }
1102292915Sdim
1103292915Sdim  if (!ShouldLink && ForAlias)
1104292915Sdim    NewGV->setLinkage(GlobalValue::InternalLinkage);
1105292915Sdim
1106292915Sdim  Constant *C = NewGV;
1107292915Sdim  if (DGV)
1108292915Sdim    C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType()));
1109292915Sdim
1110292915Sdim  if (DGV && NewGV != DGV) {
1111292915Sdim    DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
1112292915Sdim    DGV->eraseFromParent();
1113292915Sdim  }
1114292915Sdim
1115292915Sdim  return C;
1116292915Sdim}
1117292915Sdim
1118292915Sdim/// Update the initializers in the Dest module now that all globals that may be
1119292915Sdim/// referenced are in Dest.
1120292915Sdimvoid IRLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1121292915Sdim  // Figure out what the initializer looks like in the dest module.
1122292915Sdim  Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, ValueMapperFlags,
1123292915Sdim                              &TypeMap, &GValMaterializer));
1124292915Sdim}
1125292915Sdim
1126292915Sdim/// Copy the source function over into the dest function and fix up references
1127292915Sdim/// to values. At this point we know that Dest is an external function, and
1128292915Sdim/// that Src is not.
1129292915Sdimbool IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1130292915Sdim  assert(Dst.isDeclaration() && !Src.isDeclaration());
1131292915Sdim
1132292915Sdim  // Materialize if needed.
1133292915Sdim  if (std::error_code EC = Src.materialize())
1134292915Sdim    return emitError(EC.message());
1135292915Sdim
1136292915Sdim  if (!shouldLinkMetadata())
1137292915Sdim    // This is only supported for lazy links. Do after materialization of
1138292915Sdim    // a function and before remapping metadata on instructions below
1139292915Sdim    // in RemapInstruction, as the saved mapping is used to handle
1140292915Sdim    // the temporary metadata hanging off instructions.
1141293265Sdim    SrcM.getMaterializer()->saveMetadataList(MetadataToIDs,
1142293265Sdim                                             /* OnlyTempMD = */ true);
1143292915Sdim
1144292915Sdim  // Link in the prefix data.
1145292915Sdim  if (Src.hasPrefixData())
1146292915Sdim    Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, ValueMapperFlags,
1147292915Sdim                               &TypeMap, &GValMaterializer));
1148292915Sdim
1149292915Sdim  // Link in the prologue data.
1150292915Sdim  if (Src.hasPrologueData())
1151292915Sdim    Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
1152292915Sdim                                 ValueMapperFlags, &TypeMap,
1153292915Sdim                                 &GValMaterializer));
1154292915Sdim
1155292915Sdim  // Link in the personality function.
1156292915Sdim  if (Src.hasPersonalityFn())
1157292915Sdim    Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
1158292915Sdim                                  ValueMapperFlags, &TypeMap,
1159292915Sdim                                  &GValMaterializer));
1160292915Sdim
1161292915Sdim  // Go through and convert function arguments over, remembering the mapping.
1162292915Sdim  Function::arg_iterator DI = Dst.arg_begin();
1163292915Sdim  for (Argument &Arg : Src.args()) {
1164292915Sdim    DI->setName(Arg.getName()); // Copy the name over.
1165292915Sdim
1166292915Sdim    // Add a mapping to our mapping.
1167292915Sdim    ValueMap[&Arg] = &*DI;
1168292915Sdim    ++DI;
1169292915Sdim  }
1170292915Sdim
1171292915Sdim  // Copy over the metadata attachments.
1172292915Sdim  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1173292915Sdim  Src.getAllMetadata(MDs);
1174292915Sdim  for (const auto &I : MDs)
1175292915Sdim    Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, ValueMapperFlags,
1176292915Sdim                                         &TypeMap, &GValMaterializer));
1177292915Sdim
1178292915Sdim  // Splice the body of the source function into the dest function.
1179292915Sdim  Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1180292915Sdim
1181292915Sdim  // At this point, all of the instructions and values of the function are now
1182292915Sdim  // copied over.  The only problem is that they are still referencing values in
1183292915Sdim  // the Source function as operands.  Loop through all of the operands of the
1184292915Sdim  // functions and patch them up to point to the local versions.
1185292915Sdim  for (BasicBlock &BB : Dst)
1186292915Sdim    for (Instruction &I : BB)
1187292915Sdim      RemapInstruction(&I, ValueMap, RF_IgnoreMissingEntries | ValueMapperFlags,
1188292915Sdim                       &TypeMap, &GValMaterializer);
1189292915Sdim
1190292915Sdim  // There is no need to map the arguments anymore.
1191292915Sdim  for (Argument &Arg : Src.args())
1192292915Sdim    ValueMap.erase(&Arg);
1193292915Sdim
1194292915Sdim  return false;
1195292915Sdim}
1196292915Sdim
1197292915Sdimvoid IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1198292915Sdim  Constant *Aliasee = Src.getAliasee();
1199292915Sdim  Constant *Val = MapValue(Aliasee, AliasValueMap, ValueMapperFlags, &TypeMap,
1200292915Sdim                           &LValMaterializer);
1201292915Sdim  Dst.setAliasee(Val);
1202292915Sdim}
1203292915Sdim
1204292915Sdimbool IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1205292915Sdim  if (auto *F = dyn_cast<Function>(&Src))
1206292915Sdim    return linkFunctionBody(cast<Function>(Dst), *F);
1207292915Sdim  if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1208292915Sdim    linkGlobalInit(cast<GlobalVariable>(Dst), *GVar);
1209292915Sdim    return false;
1210292915Sdim  }
1211292915Sdim  linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
1212292915Sdim  return false;
1213292915Sdim}
1214292915Sdim
1215292915Sdimvoid IRLinker::findNeededSubprograms(ValueToValueMapTy &ValueMap) {
1216292915Sdim  // Track unneeded nodes to make it simpler to handle the case
1217292915Sdim  // where we are checking if an already-mapped SP is needed.
1218292915Sdim  NamedMDNode *CompileUnits = SrcM.getNamedMetadata("llvm.dbg.cu");
1219292915Sdim  if (!CompileUnits)
1220292915Sdim    return;
1221292915Sdim  for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
1222292915Sdim    auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
1223292915Sdim    assert(CU && "Expected valid compile unit");
1224294024Sdim    // Ensure that we don't remove subprograms referenced by DIImportedEntity.
1225294024Sdim    // It is not legal to have a DIImportedEntity with a null entity or scope.
1226294024Sdim    // FIXME: The DISubprogram for functions not linked in but kept due to
1227294024Sdim    // being referenced by a DIImportedEntity should also get their
1228294024Sdim    // IsDefinition flag is unset.
1229294024Sdim    SmallPtrSet<DISubprogram *, 8> ImportedEntitySPs;
1230294024Sdim    for (auto *IE : CU->getImportedEntities()) {
1231294024Sdim      if (auto *SP = dyn_cast<DISubprogram>(IE->getEntity()))
1232294024Sdim        ImportedEntitySPs.insert(SP);
1233294024Sdim      if (auto *SP = dyn_cast<DISubprogram>(IE->getScope()))
1234294024Sdim        ImportedEntitySPs.insert(SP);
1235294024Sdim    }
1236292915Sdim    for (auto *Op : CU->getSubprograms()) {
1237292915Sdim      // Unless we were doing function importing and deferred metadata linking,
1238292915Sdim      // any needed SPs should have been mapped as they would be reached
1239292915Sdim      // from the function linked in (either on the function itself for linked
1240292915Sdim      // function bodies, or from DILocation on inlined instructions).
1241292915Sdim      assert(!(ValueMap.MD()[Op] && IsMetadataLinkingPostpass) &&
1242292915Sdim             "DISubprogram shouldn't be mapped yet");
1243294024Sdim      if (!ValueMap.MD()[Op] && !ImportedEntitySPs.count(Op))
1244292915Sdim        UnneededSubprograms.insert(Op);
1245292915Sdim    }
1246292915Sdim  }
1247292915Sdim  if (!IsMetadataLinkingPostpass)
1248292915Sdim    return;
1249292915Sdim  // In the case of metadata linking as a postpass (e.g. for function
1250292915Sdim  // importing), see which DISubprogram MD from the source has an associated
1251292915Sdim  // temporary metadata node, which means the SP was needed by an imported
1252292915Sdim  // function.
1253292915Sdim  for (auto MDI : MetadataToIDs) {
1254292915Sdim    const MDNode *Node = dyn_cast<MDNode>(MDI.first);
1255292915Sdim    if (!Node)
1256292915Sdim      continue;
1257292915Sdim    DISubprogram *SP = getDISubprogram(Node);
1258292915Sdim    if (!SP || !ValIDToTempMDMap->count(MDI.second))
1259292915Sdim      continue;
1260292915Sdim    UnneededSubprograms.erase(SP);
1261292915Sdim  }
1262292915Sdim}
1263292915Sdim
1264292915Sdim// Squash null subprograms from compile unit subprogram lists.
1265292915Sdimvoid IRLinker::stripNullSubprograms() {
1266292915Sdim  NamedMDNode *CompileUnits = DstM.getNamedMetadata("llvm.dbg.cu");
1267292915Sdim  if (!CompileUnits)
1268292915Sdim    return;
1269292915Sdim  for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
1270292915Sdim    auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
1271292915Sdim    assert(CU && "Expected valid compile unit");
1272292915Sdim
1273292915Sdim    SmallVector<Metadata *, 16> NewSPs;
1274292915Sdim    NewSPs.reserve(CU->getSubprograms().size());
1275292915Sdim    bool FoundNull = false;
1276292915Sdim    for (DISubprogram *SP : CU->getSubprograms()) {
1277292915Sdim      if (!SP) {
1278292915Sdim        FoundNull = true;
1279292915Sdim        continue;
1280292915Sdim      }
1281292915Sdim      NewSPs.push_back(SP);
1282292915Sdim    }
1283292915Sdim    if (FoundNull)
1284292915Sdim      CU->replaceSubprograms(MDTuple::get(CU->getContext(), NewSPs));
1285292915Sdim  }
1286292915Sdim}
1287292915Sdim
1288292915Sdim/// Insert all of the named MDNodes in Src into the Dest module.
1289292915Sdimvoid IRLinker::linkNamedMDNodes() {
1290292915Sdim  findNeededSubprograms(ValueMap);
1291292915Sdim  const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1292292915Sdim  for (const NamedMDNode &NMD : SrcM.named_metadata()) {
1293292915Sdim    // Don't link module flags here. Do them separately.
1294292915Sdim    if (&NMD == SrcModFlags)
1295292915Sdim      continue;
1296292915Sdim    NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1297292915Sdim    // Add Src elements into Dest node.
1298292915Sdim    for (const MDNode *op : NMD.operands())
1299292915Sdim      DestNMD->addOperand(MapMetadata(
1300292915Sdim          op, ValueMap, ValueMapperFlags | RF_NullMapMissingGlobalValues,
1301292915Sdim          &TypeMap, &GValMaterializer));
1302292915Sdim  }
1303292915Sdim  stripNullSubprograms();
1304292915Sdim}
1305292915Sdim
1306292915Sdim/// Merge the linker flags in Src into the Dest module.
1307292915Sdimbool IRLinker::linkModuleFlagsMetadata() {
1308292915Sdim  // If the source module has no module flags, we are done.
1309292915Sdim  const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1310292915Sdim  if (!SrcModFlags)
1311292915Sdim    return false;
1312292915Sdim
1313292915Sdim  // If the destination module doesn't have module flags yet, then just copy
1314292915Sdim  // over the source module's flags.
1315292915Sdim  NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1316292915Sdim  if (DstModFlags->getNumOperands() == 0) {
1317292915Sdim    for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1318292915Sdim      DstModFlags->addOperand(SrcModFlags->getOperand(I));
1319292915Sdim
1320292915Sdim    return false;
1321292915Sdim  }
1322292915Sdim
1323292915Sdim  // First build a map of the existing module flags and requirements.
1324292915Sdim  DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1325292915Sdim  SmallSetVector<MDNode *, 16> Requirements;
1326292915Sdim  for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1327292915Sdim    MDNode *Op = DstModFlags->getOperand(I);
1328292915Sdim    ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1329292915Sdim    MDString *ID = cast<MDString>(Op->getOperand(1));
1330292915Sdim
1331292915Sdim    if (Behavior->getZExtValue() == Module::Require) {
1332292915Sdim      Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1333292915Sdim    } else {
1334292915Sdim      Flags[ID] = std::make_pair(Op, I);
1335292915Sdim    }
1336292915Sdim  }
1337292915Sdim
1338292915Sdim  // Merge in the flags from the source module, and also collect its set of
1339292915Sdim  // requirements.
1340292915Sdim  for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1341292915Sdim    MDNode *SrcOp = SrcModFlags->getOperand(I);
1342292915Sdim    ConstantInt *SrcBehavior =
1343292915Sdim        mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1344292915Sdim    MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1345292915Sdim    MDNode *DstOp;
1346292915Sdim    unsigned DstIndex;
1347292915Sdim    std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1348292915Sdim    unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1349292915Sdim
1350292915Sdim    // If this is a requirement, add it and continue.
1351292915Sdim    if (SrcBehaviorValue == Module::Require) {
1352292915Sdim      // If the destination module does not already have this requirement, add
1353292915Sdim      // it.
1354292915Sdim      if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1355292915Sdim        DstModFlags->addOperand(SrcOp);
1356292915Sdim      }
1357292915Sdim      continue;
1358292915Sdim    }
1359292915Sdim
1360292915Sdim    // If there is no existing flag with this ID, just add it.
1361292915Sdim    if (!DstOp) {
1362292915Sdim      Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1363292915Sdim      DstModFlags->addOperand(SrcOp);
1364292915Sdim      continue;
1365292915Sdim    }
1366292915Sdim
1367292915Sdim    // Otherwise, perform a merge.
1368292915Sdim    ConstantInt *DstBehavior =
1369292915Sdim        mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1370292915Sdim    unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1371292915Sdim
1372292915Sdim    // If either flag has override behavior, handle it first.
1373292915Sdim    if (DstBehaviorValue == Module::Override) {
1374292915Sdim      // Diagnose inconsistent flags which both have override behavior.
1375292915Sdim      if (SrcBehaviorValue == Module::Override &&
1376292915Sdim          SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1377292915Sdim        emitError("linking module flags '" + ID->getString() +
1378292915Sdim                  "': IDs have conflicting override values");
1379292915Sdim      }
1380292915Sdim      continue;
1381292915Sdim    } else if (SrcBehaviorValue == Module::Override) {
1382292915Sdim      // Update the destination flag to that of the source.
1383292915Sdim      DstModFlags->setOperand(DstIndex, SrcOp);
1384292915Sdim      Flags[ID].first = SrcOp;
1385292915Sdim      continue;
1386292915Sdim    }
1387292915Sdim
1388292915Sdim    // Diagnose inconsistent merge behavior types.
1389292915Sdim    if (SrcBehaviorValue != DstBehaviorValue) {
1390292915Sdim      emitError("linking module flags '" + ID->getString() +
1391292915Sdim                "': IDs have conflicting behaviors");
1392292915Sdim      continue;
1393292915Sdim    }
1394292915Sdim
1395292915Sdim    auto replaceDstValue = [&](MDNode *New) {
1396292915Sdim      Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1397292915Sdim      MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1398292915Sdim      DstModFlags->setOperand(DstIndex, Flag);
1399292915Sdim      Flags[ID].first = Flag;
1400292915Sdim    };
1401292915Sdim
1402292915Sdim    // Perform the merge for standard behavior types.
1403292915Sdim    switch (SrcBehaviorValue) {
1404292915Sdim    case Module::Require:
1405292915Sdim    case Module::Override:
1406292915Sdim      llvm_unreachable("not possible");
1407292915Sdim    case Module::Error: {
1408292915Sdim      // Emit an error if the values differ.
1409292915Sdim      if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1410292915Sdim        emitError("linking module flags '" + ID->getString() +
1411292915Sdim                  "': IDs have conflicting values");
1412292915Sdim      }
1413292915Sdim      continue;
1414292915Sdim    }
1415292915Sdim    case Module::Warning: {
1416292915Sdim      // Emit a warning if the values differ.
1417292915Sdim      if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1418292915Sdim        emitWarning("linking module flags '" + ID->getString() +
1419292915Sdim                    "': IDs have conflicting values");
1420292915Sdim      }
1421292915Sdim      continue;
1422292915Sdim    }
1423292915Sdim    case Module::Append: {
1424292915Sdim      MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1425292915Sdim      MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1426292915Sdim      SmallVector<Metadata *, 8> MDs;
1427292915Sdim      MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1428292915Sdim      MDs.append(DstValue->op_begin(), DstValue->op_end());
1429292915Sdim      MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1430292915Sdim
1431292915Sdim      replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1432292915Sdim      break;
1433292915Sdim    }
1434292915Sdim    case Module::AppendUnique: {
1435292915Sdim      SmallSetVector<Metadata *, 16> Elts;
1436292915Sdim      MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1437292915Sdim      MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1438292915Sdim      Elts.insert(DstValue->op_begin(), DstValue->op_end());
1439292915Sdim      Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1440292915Sdim
1441292915Sdim      replaceDstValue(MDNode::get(DstM.getContext(),
1442292915Sdim                                  makeArrayRef(Elts.begin(), Elts.end())));
1443292915Sdim      break;
1444292915Sdim    }
1445292915Sdim    }
1446292915Sdim  }
1447292915Sdim
1448292915Sdim  // Check all of the requirements.
1449292915Sdim  for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1450292915Sdim    MDNode *Requirement = Requirements[I];
1451292915Sdim    MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1452292915Sdim    Metadata *ReqValue = Requirement->getOperand(1);
1453292915Sdim
1454292915Sdim    MDNode *Op = Flags[Flag].first;
1455292915Sdim    if (!Op || Op->getOperand(2) != ReqValue) {
1456292915Sdim      emitError("linking module flags '" + Flag->getString() +
1457292915Sdim                "': does not have the required value");
1458292915Sdim      continue;
1459292915Sdim    }
1460292915Sdim  }
1461292915Sdim
1462292915Sdim  return HasError;
1463292915Sdim}
1464292915Sdim
1465292915Sdim// This function returns true if the triples match.
1466292915Sdimstatic bool triplesMatch(const Triple &T0, const Triple &T1) {
1467292915Sdim  // If vendor is apple, ignore the version number.
1468292915Sdim  if (T0.getVendor() == Triple::Apple)
1469292915Sdim    return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() &&
1470292915Sdim           T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS();
1471292915Sdim
1472292915Sdim  return T0 == T1;
1473292915Sdim}
1474292915Sdim
1475292915Sdim// This function returns the merged triple.
1476292915Sdimstatic std::string mergeTriples(const Triple &SrcTriple,
1477292915Sdim                                const Triple &DstTriple) {
1478292915Sdim  // If vendor is apple, pick the triple with the larger version number.
1479292915Sdim  if (SrcTriple.getVendor() == Triple::Apple)
1480292915Sdim    if (DstTriple.isOSVersionLT(SrcTriple))
1481292915Sdim      return SrcTriple.str();
1482292915Sdim
1483292915Sdim  return DstTriple.str();
1484292915Sdim}
1485292915Sdim
1486292915Sdimbool IRLinker::run() {
1487292915Sdim  // Inherit the target data from the source module if the destination module
1488292915Sdim  // doesn't have one already.
1489292915Sdim  if (DstM.getDataLayout().isDefault())
1490292915Sdim    DstM.setDataLayout(SrcM.getDataLayout());
1491292915Sdim
1492292915Sdim  if (SrcM.getDataLayout() != DstM.getDataLayout()) {
1493292915Sdim    emitWarning("Linking two modules of different data layouts: '" +
1494292915Sdim                SrcM.getModuleIdentifier() + "' is '" +
1495292915Sdim                SrcM.getDataLayoutStr() + "' whereas '" +
1496292915Sdim                DstM.getModuleIdentifier() + "' is '" +
1497292915Sdim                DstM.getDataLayoutStr() + "'\n");
1498292915Sdim  }
1499292915Sdim
1500292915Sdim  // Copy the target triple from the source to dest if the dest's is empty.
1501292915Sdim  if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
1502292915Sdim    DstM.setTargetTriple(SrcM.getTargetTriple());
1503292915Sdim
1504292915Sdim  Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
1505292915Sdim
1506292915Sdim  if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
1507292915Sdim    emitWarning("Linking two modules of different target triples: " +
1508292915Sdim                SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
1509292915Sdim                "' whereas '" + DstM.getModuleIdentifier() + "' is '" +
1510292915Sdim                DstM.getTargetTriple() + "'\n");
1511292915Sdim
1512292915Sdim  DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
1513292915Sdim
1514292915Sdim  // Append the module inline asm string.
1515292915Sdim  if (!SrcM.getModuleInlineAsm().empty()) {
1516292915Sdim    if (DstM.getModuleInlineAsm().empty())
1517292915Sdim      DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
1518292915Sdim    else
1519292915Sdim      DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1520292915Sdim                              SrcM.getModuleInlineAsm());
1521292915Sdim  }
1522292915Sdim
1523292915Sdim  // Loop over all of the linked values to compute type mappings.
1524292915Sdim  computeTypeMapping();
1525292915Sdim
1526292915Sdim  std::reverse(Worklist.begin(), Worklist.end());
1527292915Sdim  while (!Worklist.empty()) {
1528292915Sdim    GlobalValue *GV = Worklist.back();
1529292915Sdim    Worklist.pop_back();
1530292915Sdim
1531292915Sdim    // Already mapped.
1532292915Sdim    if (ValueMap.find(GV) != ValueMap.end() ||
1533292915Sdim        AliasValueMap.find(GV) != AliasValueMap.end())
1534292915Sdim      continue;
1535292915Sdim
1536292915Sdim    assert(!GV->isDeclaration());
1537292915Sdim    MapValue(GV, ValueMap, ValueMapperFlags, &TypeMap, &GValMaterializer);
1538292915Sdim    if (HasError)
1539292915Sdim      return true;
1540292915Sdim  }
1541292915Sdim
1542292915Sdim  // Note that we are done linking global value bodies. This prevents
1543292915Sdim  // metadata linking from creating new references.
1544292915Sdim  DoneLinkingBodies = true;
1545292915Sdim
1546292915Sdim  // Remap all of the named MDNodes in Src into the DstM module. We do this
1547292915Sdim  // after linking GlobalValues so that MDNodes that reference GlobalValues
1548292915Sdim  // are properly remapped.
1549292915Sdim  if (shouldLinkMetadata()) {
1550292915Sdim    // Even if just linking metadata we should link decls above in case
1551292915Sdim    // any are referenced by metadata. IRLinker::shouldLink ensures that
1552292915Sdim    // we don't actually link anything from source.
1553292915Sdim    if (IsMetadataLinkingPostpass) {
1554292915Sdim      // Ensure metadata materialized
1555292915Sdim      if (SrcM.getMaterializer()->materializeMetadata())
1556292915Sdim        return true;
1557293265Sdim      SrcM.getMaterializer()->saveMetadataList(MetadataToIDs,
1558293265Sdim                                               /* OnlyTempMD = */ false);
1559292915Sdim    }
1560292915Sdim
1561292915Sdim    linkNamedMDNodes();
1562292915Sdim
1563292915Sdim    if (IsMetadataLinkingPostpass) {
1564292915Sdim      // Handle anything left in the ValIDToTempMDMap, such as metadata nodes
1565292915Sdim      // not reached by the dbg.cu NamedMD (i.e. only reached from
1566292915Sdim      // instructions).
1567292915Sdim      // Walk the MetadataToIDs once to find the set of new (imported) MD
1568292915Sdim      // that still has corresponding temporary metadata, and invoke metadata
1569292915Sdim      // mapping on each one.
1570292915Sdim      for (auto MDI : MetadataToIDs) {
1571292915Sdim        if (!ValIDToTempMDMap->count(MDI.second))
1572292915Sdim          continue;
1573292915Sdim        MapMetadata(MDI.first, ValueMap, ValueMapperFlags, &TypeMap,
1574292915Sdim                    &GValMaterializer);
1575292915Sdim      }
1576292915Sdim      assert(ValIDToTempMDMap->empty());
1577292915Sdim    }
1578292915Sdim
1579292915Sdim    // Merge the module flags into the DstM module.
1580292915Sdim    if (linkModuleFlagsMetadata())
1581292915Sdim      return true;
1582292915Sdim  }
1583292915Sdim
1584292915Sdim  return false;
1585292915Sdim}
1586292915Sdim
1587292915SdimIRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1588292915Sdim    : ETypes(E), IsPacked(P) {}
1589292915Sdim
1590292915SdimIRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1591292915Sdim    : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1592292915Sdim
1593292915Sdimbool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1594292915Sdim  if (IsPacked != That.IsPacked)
1595292915Sdim    return false;
1596292915Sdim  if (ETypes != That.ETypes)
1597292915Sdim    return false;
1598292915Sdim  return true;
1599292915Sdim}
1600292915Sdim
1601292915Sdimbool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1602292915Sdim  return !this->operator==(That);
1603292915Sdim}
1604292915Sdim
1605292915SdimStructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1606292915Sdim  return DenseMapInfo<StructType *>::getEmptyKey();
1607292915Sdim}
1608292915Sdim
1609292915SdimStructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1610292915Sdim  return DenseMapInfo<StructType *>::getTombstoneKey();
1611292915Sdim}
1612292915Sdim
1613292915Sdimunsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1614292915Sdim  return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1615292915Sdim                      Key.IsPacked);
1616292915Sdim}
1617292915Sdim
1618292915Sdimunsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1619292915Sdim  return getHashValue(KeyTy(ST));
1620292915Sdim}
1621292915Sdim
1622292915Sdimbool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1623292915Sdim                                         const StructType *RHS) {
1624292915Sdim  if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1625292915Sdim    return false;
1626292915Sdim  return LHS == KeyTy(RHS);
1627292915Sdim}
1628292915Sdim
1629292915Sdimbool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1630292915Sdim                                         const StructType *RHS) {
1631292915Sdim  if (RHS == getEmptyKey())
1632292915Sdim    return LHS == getEmptyKey();
1633292915Sdim
1634292915Sdim  if (RHS == getTombstoneKey())
1635292915Sdim    return LHS == getTombstoneKey();
1636292915Sdim
1637292915Sdim  return KeyTy(LHS) == KeyTy(RHS);
1638292915Sdim}
1639292915Sdim
1640292915Sdimvoid IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1641292915Sdim  assert(!Ty->isOpaque());
1642292915Sdim  NonOpaqueStructTypes.insert(Ty);
1643292915Sdim}
1644292915Sdim
1645292915Sdimvoid IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1646292915Sdim  assert(!Ty->isOpaque());
1647292915Sdim  NonOpaqueStructTypes.insert(Ty);
1648292915Sdim  bool Removed = OpaqueStructTypes.erase(Ty);
1649292915Sdim  (void)Removed;
1650292915Sdim  assert(Removed);
1651292915Sdim}
1652292915Sdim
1653292915Sdimvoid IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1654292915Sdim  assert(Ty->isOpaque());
1655292915Sdim  OpaqueStructTypes.insert(Ty);
1656292915Sdim}
1657292915Sdim
1658292915SdimStructType *
1659292915SdimIRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1660292915Sdim                                                bool IsPacked) {
1661292915Sdim  IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1662292915Sdim  auto I = NonOpaqueStructTypes.find_as(Key);
1663292915Sdim  if (I == NonOpaqueStructTypes.end())
1664292915Sdim    return nullptr;
1665292915Sdim  return *I;
1666292915Sdim}
1667292915Sdim
1668292915Sdimbool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1669292915Sdim  if (Ty->isOpaque())
1670292915Sdim    return OpaqueStructTypes.count(Ty);
1671292915Sdim  auto I = NonOpaqueStructTypes.find(Ty);
1672292915Sdim  if (I == NonOpaqueStructTypes.end())
1673292915Sdim    return false;
1674292915Sdim  return *I == Ty;
1675292915Sdim}
1676292915Sdim
1677292915SdimIRMover::IRMover(Module &M) : Composite(M) {
1678292915Sdim  TypeFinder StructTypes;
1679292915Sdim  StructTypes.run(M, true);
1680292915Sdim  for (StructType *Ty : StructTypes) {
1681292915Sdim    if (Ty->isOpaque())
1682292915Sdim      IdentifiedStructTypes.addOpaque(Ty);
1683292915Sdim    else
1684292915Sdim      IdentifiedStructTypes.addNonOpaque(Ty);
1685292915Sdim  }
1686292915Sdim}
1687292915Sdim
1688292915Sdimbool IRMover::move(
1689292915Sdim    Module &Src, ArrayRef<GlobalValue *> ValuesToLink,
1690292915Sdim    std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
1691292915Sdim    DenseMap<unsigned, MDNode *> *ValIDToTempMDMap,
1692292915Sdim    bool IsMetadataLinkingPostpass) {
1693292915Sdim  IRLinker TheIRLinker(Composite, IdentifiedStructTypes, Src, ValuesToLink,
1694292915Sdim                       AddLazyFor, ValIDToTempMDMap, IsMetadataLinkingPostpass);
1695292915Sdim  bool RetCode = TheIRLinker.run();
1696292915Sdim  Composite.dropTriviallyDeadConstantArrays();
1697292915Sdim  return RetCode;
1698292915Sdim}
1699