Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Linker/IRMover.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/Linker/IRMover.cpp ---------------------------------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "llvm/Linker/IRMover.h"
11
#include "LinkDiagnosticInfo.h"
12
#include "llvm/ADT/SetVector.h"
13
#include "llvm/ADT/SmallString.h"
14
#include "llvm/ADT/Triple.h"
15
#include "llvm/IR/Constants.h"
16
#include "llvm/IR/DebugInfo.h"
17
#include "llvm/IR/DiagnosticPrinter.h"
18
#include "llvm/IR/GVMaterializer.h"
19
#include "llvm/IR/Intrinsics.h"
20
#include "llvm/IR/TypeFinder.h"
21
#include "llvm/Support/Error.h"
22
#include "llvm/Transforms/Utils/Cloning.h"
23
#include <utility>
24
using namespace llvm;
25
26
//===----------------------------------------------------------------------===//
27
// TypeMap implementation.
28
//===----------------------------------------------------------------------===//
29
30
namespace {
31
class TypeMapTy : public ValueMapTypeRemapper {
32
  /// This is a mapping from a source type to a destination type to use.
33
  DenseMap<Type *, Type *> MappedTypes;
34
35
  /// When checking to see if two subgraphs are isomorphic, we speculatively
36
  /// add types to MappedTypes, but keep track of them here in case we need to
37
  /// roll back.
38
  SmallVector<Type *, 16> SpeculativeTypes;
39
40
  SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
41
42
  /// This is a list of non-opaque structs in the source module that are mapped
43
  /// to an opaque struct in the destination module.
44
  SmallVector<StructType *, 16> SrcDefinitionsToResolve;
45
46
  /// This is the set of opaque types in the destination modules who are
47
  /// getting a body from the source module.
48
  SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
49
50
public:
51
  TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
52
877
      : DstStructTypesSet(DstStructTypesSet) {}
53
54
  IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
55
  /// Indicate that the specified type in the destination module is conceptually
56
  /// equivalent to the specified type in the source module.
57
  void addTypeMapping(Type *DstTy, Type *SrcTy);
58
59
  /// Produce a body for an opaque type in the dest module from a type
60
  /// definition in the source module.
61
  void linkDefinedTypeBodies();
62
63
  /// Return the mapped type to use for the specified input type from the
64
  /// source module.
65
  Type *get(Type *SrcTy);
66
  Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
67
68
  void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
69
70
1.21k
  FunctionType *get(FunctionType *T) {
71
1.21k
    return cast<FunctionType>(get((Type *)T));
72
1.21k
  }
73
74
private:
75
3.65k
  Type *remapType(Type *SrcTy) override { return get(SrcTy); }
76
77
  bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
78
};
79
}
80
81
566
void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
82
566
  assert(SpeculativeTypes.empty());
83
566
  assert(SpeculativeDstOpaqueTypes.empty());
84
566
85
566
  // Check to see if these types are recursively isomorphic and establish a
86
566
  // mapping between them if so.
87
566
  if (
!areTypesIsomorphic(DstTy, SrcTy)566
) {
88
129
    // Oops, they aren't isomorphic.  Just discard this request by rolling out
89
129
    // any speculative mappings we've established.
90
129
    for (Type *Ty : SpeculativeTypes)
91
141
      MappedTypes.erase(Ty);
92
129
93
129
    SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
94
129
                                   SpeculativeDstOpaqueTypes.size());
95
129
    for (StructType *Ty : SpeculativeDstOpaqueTypes)
96
0
      DstResolvedOpaqueTypes.erase(Ty);
97
566
  } else {
98
437
    for (Type *Ty : SpeculativeTypes)
99
76
      
if (auto *76
STy76
= dyn_cast<StructType>(Ty))
100
47
        
if (47
STy->hasName()47
)
101
46
          STy->setName("");
102
437
  }
103
566
  SpeculativeTypes.clear();
104
566
  SpeculativeDstOpaqueTypes.clear();
105
566
}
106
107
/// Recursively walk this pair of types, returning true if they are isomorphic,
108
/// false if they are not.
109
824
bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
110
824
  // Two types with differing kinds are clearly not isomorphic.
111
824
  if (DstTy->getTypeID() != SrcTy->getTypeID())
112
8
    return false;
113
816
114
816
  // If we have an entry in the MappedTypes table, then we have our answer.
115
816
  Type *&Entry = MappedTypes[SrcTy];
116
816
  if (Entry)
117
208
    return Entry == DstTy;
118
608
119
608
  // Two identical types are clearly isomorphic.  Remember this
120
608
  // non-speculatively.
121
608
  
if (608
DstTy == SrcTy608
) {
122
274
    Entry = DstTy;
123
274
    return true;
124
274
  }
125
334
126
334
  // Okay, we have two types with identical kinds that we haven't seen before.
127
334
128
334
  // If this is an opaque struct type, special case it.
129
334
  
if (StructType *334
SSTy334
= dyn_cast<StructType>(SrcTy)) {
130
61
    // Mapping an opaque type to any struct, just keep the dest struct.
131
61
    if (
SSTy->isOpaque()61
) {
132
6
      Entry = DstTy;
133
6
      SpeculativeTypes.push_back(SrcTy);
134
6
      return true;
135
6
    }
136
55
137
55
    // Mapping a non-opaque source type to an opaque dest.  If this is the first
138
55
    // type that we're mapping onto this destination type then we succeed.  Keep
139
55
    // the dest, but fill it in later. If this is the second (different) type
140
55
    // that we're trying to map onto the same opaque type then we fail.
141
55
    
if (55
cast<StructType>(DstTy)->isOpaque()55
) {
142
5
      // We can only map one source type onto the opaque destination type.
143
5
      if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
144
0
        return false;
145
5
      SrcDefinitionsToResolve.push_back(SSTy);
146
5
      SpeculativeTypes.push_back(SrcTy);
147
5
      SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
148
5
      Entry = DstTy;
149
5
      return true;
150
5
    }
151
61
  }
152
323
153
323
  // If the number of subtypes disagree between the two types, then we fail.
154
323
  
if (323
SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()323
)
155
42
    return false;
156
281
157
281
  // Fail if any of the extra properties (e.g. array size) of the type disagree.
158
281
  
if (281
isa<IntegerType>(DstTy)281
)
159
15
    return false; // bitwidth disagrees.
160
266
  
if (PointerType *266
PT266
= dyn_cast<PointerType>(DstTy)) {
161
150
    if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
162
0
      return false;
163
266
164
116
  } else 
if (FunctionType *116
FT116
= dyn_cast<FunctionType>(DstTy)) {
165
68
    if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
166
59
      return false;
167
48
  } else 
if (StructType *48
DSTy48
= dyn_cast<StructType>(DstTy)) {
168
47
    StructType *SSTy = cast<StructType>(SrcTy);
169
47
    if (DSTy->isLiteral() != SSTy->isLiteral() ||
170
47
        DSTy->isPacked() != SSTy->isPacked())
171
0
      return false;
172
1
  } else 
if (auto *1
DSeqTy1
= dyn_cast<SequentialType>(DstTy)) {
173
1
    if (DSeqTy->getNumElements() !=
174
1
        cast<SequentialType>(SrcTy)->getNumElements())
175
1
      return false;
176
206
  }
177
206
178
206
  // Otherwise, we speculate that these two types will line up and recursively
179
206
  // check the subelements.
180
206
  Entry = DstTy;
181
206
  SpeculativeTypes.push_back(SrcTy);
182
206
183
332
  for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); 
I != E332
;
++I126
)
184
258
    
if (258
!areTypesIsomorphic(DstTy->getContainedType(I),
185
258
                            SrcTy->getContainedType(I)))
186
132
      return false;
187
206
188
206
  // If everything seems to have lined up, then everything is great.
189
74
  return true;
190
824
}
191
192
877
void TypeMapTy::linkDefinedTypeBodies() {
193
877
  SmallVector<Type *, 16> Elements;
194
5
  for (StructType *SrcSTy : SrcDefinitionsToResolve) {
195
5
    StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
196
5
    assert(DstSTy->isOpaque());
197
5
198
5
    // Map the body of the source type over to a new body for the dest type.
199
5
    Elements.resize(SrcSTy->getNumElements());
200
9
    for (unsigned I = 0, E = Elements.size(); 
I != E9
;
++I4
)
201
4
      Elements[I] = get(SrcSTy->getElementType(I));
202
5
203
5
    DstSTy->setBody(Elements, SrcSTy->isPacked());
204
5
    DstStructTypesSet.switchToNonOpaque(DstSTy);
205
5
  }
206
877
  SrcDefinitionsToResolve.clear();
207
877
  DstResolvedOpaqueTypes.clear();
208
877
}
209
210
void TypeMapTy::finishType(StructType *DTy, StructType *STy,
211
15
                           ArrayRef<Type *> ETypes) {
212
15
  DTy->setBody(ETypes, STy->isPacked());
213
15
214
15
  // Steal STy's name.
215
15
  if (
STy->hasName()15
) {
216
15
    SmallString<16> TmpName = STy->getName();
217
15
    STy->setName("");
218
15
    DTy->setName(TmpName);
219
15
  }
220
15
221
15
  DstStructTypesSet.addNonOpaque(DTy);
222
15
}
223
224
5.78k
Type *TypeMapTy::get(Type *Ty) {
225
5.78k
  SmallPtrSet<StructType *, 8> Visited;
226
5.78k
  return get(Ty, Visited);
227
5.78k
}
228
229
7.80k
Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
230
7.80k
  // If we already have an entry for this type, return it.
231
7.80k
  Type **Entry = &MappedTypes[Ty];
232
7.80k
  if (*Entry)
233
4.92k
    return *Entry;
234
2.88k
235
2.88k
  // These are types that LLVM itself will unique.
236
2.88k
  
bool IsUniqued = !isa<StructType>(Ty) || 2.88k
cast<StructType>(Ty)->isLiteral()141
;
237
2.88k
238
#ifndef NDEBUG
239
  if (!IsUniqued) {
240
    for (auto &Pair : MappedTypes) {
241
      assert(!(Pair.first != Ty && Pair.second == Ty) &&
242
             "mapping to a source type");
243
    }
244
  }
245
#endif
246
247
2.88k
  if (
!IsUniqued && 2.88k
!Visited.insert(cast<StructType>(Ty)).second101
) {
248
8
    StructType *DTy = StructType::create(Ty->getContext());
249
8
    return *Entry = DTy;
250
8
  }
251
2.87k
252
2.87k
  // If this is not a recursive type, then just map all of the elements and
253
2.87k
  // then rebuild the type from inside out.
254
2.87k
  SmallVector<Type *, 4> ElementTypes;
255
2.87k
256
2.87k
  // If there are no element types to map, then the type is itself.  This is
257
2.87k
  // true for the anonymous {} struct, things like 'float', integers, etc.
258
2.87k
  if (
Ty->getNumContainedTypes() == 0 && 2.87k
IsUniqued1.32k
)
259
1.30k
    return *Entry = Ty;
260
1.57k
261
1.57k
  // Remap all of the elements, keeping track of whether any of them change.
262
1.57k
  bool AnyChange = false;
263
1.57k
  ElementTypes.resize(Ty->getNumContainedTypes());
264
3.59k
  for (unsigned I = 0, E = Ty->getNumContainedTypes(); 
I != E3.59k
;
++I2.02k
) {
265
2.02k
    ElementTypes[I] = get(Ty->getContainedType(I), Visited);
266
2.02k
    AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
267
2.02k
  }
268
1.57k
269
1.57k
  // If we found our type while recursively processing stuff, just use it.
270
1.57k
  Entry = &MappedTypes[Ty];
271
1.57k
  if (
*Entry1.57k
) {
272
11
    if (auto *
DTy11
= dyn_cast<StructType>(*Entry)) {
273
8
      if (
DTy->isOpaque()8
) {
274
8
        auto *STy = cast<StructType>(Ty);
275
8
        finishType(DTy, STy, ElementTypes);
276
8
      }
277
8
    }
278
11
    return *Entry;
279
11
  }
280
1.56k
281
1.56k
  // If all of the element types mapped directly over and the type is not
282
1.56k
  // a named struct, then the type is usable as-is.
283
1.56k
  
if (1.56k
!AnyChange && 1.56k
IsUniqued1.49k
)
284
1.41k
    return *Entry = Ty;
285
147
286
147
  // Otherwise, rebuild a modified type.
287
147
  switch (Ty->getTypeID()) {
288
0
  default:
289
0
    llvm_unreachable("unknown derived type to remap");
290
0
  case Type::ArrayTyID:
291
0
    return *Entry = ArrayType::get(ElementTypes[0],
292
0
                                   cast<ArrayType>(Ty)->getNumElements());
293
0
  case Type::VectorTyID:
294
0
    return *Entry = VectorType::get(ElementTypes[0],
295
0
                                    cast<VectorType>(Ty)->getNumElements());
296
42
  case Type::PointerTyID:
297
42
    return *Entry = PointerType::get(ElementTypes[0],
298
42
                                     cast<PointerType>(Ty)->getAddressSpace());
299
19
  case Type::FunctionTyID:
300
19
    return *Entry = FunctionType::get(ElementTypes[0],
301
19
                                      makeArrayRef(ElementTypes).slice(1),
302
19
                                      cast<FunctionType>(Ty)->isVarArg());
303
86
  case Type::StructTyID: {
304
86
    auto *STy = cast<StructType>(Ty);
305
86
    bool IsPacked = STy->isPacked();
306
86
    if (IsUniqued)
307
1
      return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
308
85
309
85
    // If the type is opaque, we can just use it directly.
310
85
    
if (85
STy->isOpaque()85
) {
311
16
      DstStructTypesSet.addOpaque(STy);
312
16
      return *Entry = Ty;
313
16
    }
314
69
315
69
    
if (StructType *69
OldT69
=
316
3
            DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
317
3
      STy->setName("");
318
3
      return *Entry = OldT;
319
3
    }
320
66
321
66
    
if (66
!AnyChange66
) {
322
59
      DstStructTypesSet.addNonOpaque(STy);
323
59
      return *Entry = Ty;
324
59
    }
325
7
326
7
    StructType *DTy = StructType::create(Ty->getContext());
327
7
    finishType(DTy, STy, ElementTypes);
328
7
    return *Entry = DTy;
329
7
  }
330
7.80k
  }
331
7.80k
}
332
333
LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
334
                                       const Twine &Msg)
335
21
    : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
336
20
void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
337
338
//===----------------------------------------------------------------------===//
339
// IRLinker implementation.
340
//===----------------------------------------------------------------------===//
341
342
namespace {
343
class IRLinker;
344
345
/// Creates prototypes for functions that are lazily linked on the fly. This
346
/// speeds up linking for modules with many/ lazily linked functions of which
347
/// few get used.
348
class GlobalValueMaterializer final : public ValueMaterializer {
349
  IRLinker &TheIRLinker;
350
351
public:
352
877
  GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
353
  Value *materialize(Value *V) override;
354
};
355
356
class LocalValueMaterializer final : public ValueMaterializer {
357
  IRLinker &TheIRLinker;
358
359
public:
360
877
  LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
361
  Value *materialize(Value *V) override;
362
};
363
364
/// Type of the Metadata map in \a ValueToValueMapTy.
365
typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
366
367
/// This is responsible for keeping track of the state used for moving data
368
/// from SrcM to DstM.
369
class IRLinker {
370
  Module &DstM;
371
  std::unique_ptr<Module> SrcM;
372
373
  /// See IRMover::move().
374
  std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
375
376
  TypeMapTy TypeMap;
377
  GlobalValueMaterializer GValMaterializer;
378
  LocalValueMaterializer LValMaterializer;
379
380
  /// A metadata map that's shared between IRLinker instances.
381
  MDMapT &SharedMDs;
382
383
  /// Mapping of values from what they used to be in Src, to what they are now
384
  /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
385
  /// due to the use of Value handles which the Linker doesn't actually need,
386
  /// but this allows us to reuse the ValueMapper code.
387
  ValueToValueMapTy ValueMap;
388
  ValueToValueMapTy AliasValueMap;
389
390
  DenseSet<GlobalValue *> ValuesToLink;
391
  std::vector<GlobalValue *> Worklist;
392
393
1.30k
  void maybeAdd(GlobalValue *GV) {
394
1.30k
    if (ValuesToLink.insert(GV).second)
395
1.29k
      Worklist.push_back(GV);
396
1.30k
  }
397
398
  /// Whether we are importing globals for ThinLTO, as opposed to linking the
399
  /// source module. If this flag is set, it means that we can rely on some
400
  /// other object file to define any non-GlobalValue entities defined by the
401
  /// source module. This currently causes us to not link retained types in
402
  /// debug info metadata and module inline asm.
403
  bool IsPerformingImport;
404
405
  /// Set to true when all global value body linking is complete (including
406
  /// lazy linking). Used to prevent metadata linking from creating new
407
  /// references.
408
  bool DoneLinkingBodies = false;
409
410
  /// The Error encountered during materialization. We use an Optional here to
411
  /// avoid needing to manage an unconsumed success value.
412
  Optional<Error> FoundError;
413
1.34k
  void setError(Error E) {
414
1.34k
    if (E)
415
1
      FoundError = std::move(E);
416
1.34k
  }
417
418
  /// Most of the errors produced by this module are inconvertible StringErrors.
419
  /// This convenience function lets us return one of those more easily.
420
6
  Error stringErr(const Twine &T) {
421
6
    return make_error<StringError>(T, inconvertibleErrorCode());
422
6
  }
423
424
  /// Entry point for mapping values and alternate context for mapping aliases.
425
  ValueMapper Mapper;
426
  unsigned AliasMCID;
427
428
  /// Handles cloning of a global values from the source module into
429
  /// the destination module, including setting the attributes and visibility.
430
  GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
431
432
6
  void emitWarning(const Twine &Message) {
433
6
    SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
434
6
  }
435
436
  /// Given a global in the source module, return the global in the
437
  /// destination module that is being linked to, if any.
438
4.26k
  GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
439
4.26k
    // If the source has no name it can't link.  If it has local linkage,
440
4.26k
    // there is no name match-up going on.
441
4.26k
    if (
!SrcGV->hasName() || 4.26k
SrcGV->hasLocalLinkage()4.25k
)
442
250
      return nullptr;
443
4.01k
444
4.01k
    // Otherwise see if we have a match in the destination module's symtab.
445
4.01k
    GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
446
4.01k
    if (!DGV)
447
3.08k
      return nullptr;
448
922
449
922
    // If we found a global with the same name in the dest module, but it has
450
922
    // internal linkage, we are really not doing any linkage here.
451
922
    
if (922
DGV->hasLocalLinkage()922
)
452
14
      return nullptr;
453
908
454
908
    // Otherwise, we do in fact link to the destination global.
455
908
    return DGV;
456
908
  }
457
458
  void computeTypeMapping();
459
460
  Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
461
                                             const GlobalVariable *SrcGV);
462
463
  /// Given the GlobaValue \p SGV in the source module, and the matching
464
  /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
465
  /// into the destination module.
466
  ///
467
  /// Note this code may call the client-provided \p AddLazyFor.
468
  bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
469
  Expected<Constant *> linkGlobalValueProto(GlobalValue *GV, bool ForAlias);
470
471
  Error linkModuleFlagsMetadata();
472
473
  void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);
474
  Error linkFunctionBody(Function &Dst, Function &Src);
475
  void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
476
  Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
477
478
  /// Functions that take care of cloning a specific global value type
479
  /// into the destination module.
480
  GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
481
  Function *copyFunctionProto(const Function *SF);
482
  GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
483
484
  /// When importing for ThinLTO, prevent importing of types listed on
485
  /// the DICompileUnit that we don't need a copy of in the importing
486
  /// module.
487
  void prepareCompileUnitsForImport();
488
  void linkNamedMDNodes();
489
490
public:
491
  IRLinker(Module &DstM, MDMapT &SharedMDs,
492
           IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
493
           ArrayRef<GlobalValue *> ValuesToLink,
494
           std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
495
           bool IsPerformingImport)
496
      : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
497
        TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
498
        SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
499
        Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap,
500
               &GValMaterializer),
501
        AliasMCID(Mapper.registerAlternateMappingContext(AliasValueMap,
502
877
                                                         &LValMaterializer)) {
503
877
    ValueMap.getMDMap() = std::move(SharedMDs);
504
877
    for (GlobalValue *GV : ValuesToLink)
505
1.24k
      maybeAdd(GV);
506
877
    if (IsPerformingImport)
507
99
      prepareCompileUnitsForImport();
508
877
  }
509
877
  ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
510
511
  Error run();
512
  Value *materialize(Value *V, bool ForAlias);
513
};
514
}
515
516
/// The LLVM SymbolTable class autorenames globals that conflict in the symbol
517
/// table. This is good for all clients except for us. Go through the trouble
518
/// to force this back.
519
1.75k
static void forceRenaming(GlobalValue *GV, StringRef Name) {
520
1.75k
  // If the global doesn't force its name or if it already has the right name,
521
1.75k
  // there is nothing for us to do.
522
1.75k
  if (
GV->hasLocalLinkage() || 1.75k
GV->getName() == Name1.64k
)
523
1.43k
    return;
524
317
525
317
  Module *M = GV->getParent();
526
317
527
317
  // If there is a conflict, rename the conflict.
528
317
  if (GlobalValue *
ConflictGV317
= M->getNamedValue(Name)) {
529
270
    GV->takeName(ConflictGV);
530
270
    ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
531
270
    assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
532
317
  } else {
533
47
    GV->setName(Name); // Force the name back
534
47
  }
535
1.75k
}
536
537
3.66k
Value *GlobalValueMaterializer::materialize(Value *SGV) {
538
3.66k
  return TheIRLinker.materialize(SGV, false);
539
3.66k
}
540
541
85
Value *LocalValueMaterializer::materialize(Value *SGV) {
542
85
  return TheIRLinker.materialize(SGV, true);
543
85
}
544
545
3.74k
Value *IRLinker::materialize(Value *V, bool ForAlias) {
546
3.74k
  auto *SGV = dyn_cast<GlobalValue>(V);
547
3.74k
  if (!SGV)
548
1.87k
    return nullptr;
549
1.87k
550
1.87k
  Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForAlias);
551
1.87k
  if (
!NewProto1.87k
) {
552
1
    setError(NewProto.takeError());
553
1
    return nullptr;
554
1
  }
555
1.87k
  
if (1.87k
!*NewProto1.87k
)
556
17
    return nullptr;
557
1.85k
558
1.85k
  GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
559
1.85k
  if (!New)
560
26
    return *NewProto;
561
1.83k
562
1.83k
  // If we already created the body, just return.
563
1.83k
  
if (auto *1.83k
F1.83k
= dyn_cast<Function>(New)) {
564
1.26k
    if (!F->isDeclaration())
565
34
      return New;
566
562
  } else 
if (auto *562
V562
= dyn_cast<GlobalVariable>(New)) {
567
484
    if (
V->hasInitializer() || 484
V->hasAppendingLinkage()454
)
568
76
      return New;
569
78
  } else {
570
78
    auto *A = cast<GlobalAlias>(New);
571
78
    if (A->getAliasee())
572
7
      return New;
573
1.71k
  }
574
1.71k
575
1.71k
  // When linking a global for an alias, it will always be linked. However we
576
1.71k
  // need to check if it was not already scheduled to satisfy a reference from a
577
1.71k
  // regular global value initializer. We know if it has been schedule if the
578
1.71k
  // "New" GlobalValue that is mapped here for the alias is the same as the one
579
1.71k
  // already mapped. If there is an entry in the ValueMap but the value is
580
1.71k
  // different, it means that the value already had a definition in the
581
1.71k
  // destination module (linkonce for instance), but we need a new definition
582
1.71k
  // for the alias ("New" will be different.
583
1.71k
  
if (1.71k
ForAlias && 1.71k
ValueMap.lookup(SGV) == New19
)
584
1
    return New;
585
1.71k
586
1.71k
  
if (1.71k
ForAlias || 1.71k
shouldLink(New, *SGV)1.69k
)
587
1.34k
    setError(linkGlobalValueBody(*New, *SGV));
588
3.74k
589
3.74k
  return New;
590
3.74k
}
591
592
/// Loop through the global variables in the src module and merge them into the
593
/// dest module.
594
406
GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
595
406
  // No linking to be performed or linking from the source: simply create an
596
406
  // identical version of the symbol over in the dest module... the
597
406
  // initializer will be filled in later by LinkGlobalInits.
598
406
  GlobalVariable *NewDGV =
599
406
      new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
600
406
                         SGVar->isConstant(), GlobalValue::ExternalLinkage,
601
406
                         /*init*/ nullptr, SGVar->getName(),
602
406
                         /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
603
406
                         SGVar->getType()->getAddressSpace());
604
406
  NewDGV->setAlignment(SGVar->getAlignment());
605
406
  NewDGV->copyAttributesFrom(SGVar);
606
406
  return NewDGV;
607
406
}
608
609
/// Link the function in the source module into the destination module if
610
/// needed, setting up mapping information.
611
1.21k
Function *IRLinker::copyFunctionProto(const Function *SF) {
612
1.21k
  // If there is no linkage to be performed or we are linking from the source,
613
1.21k
  // bring SF over.
614
1.21k
  auto *F =
615
1.21k
      Function::Create(TypeMap.get(SF->getFunctionType()),
616
1.21k
                       GlobalValue::ExternalLinkage, SF->getName(), &DstM);
617
1.21k
  F->copyAttributesFrom(SF);
618
1.21k
  return F;
619
1.21k
}
620
621
/// Set up prototypes for any aliases that come over from the source module.
622
71
GlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
623
71
  // If there is no linkage to be performed or we're linking from the source,
624
71
  // bring over SGA.
625
71
  auto *Ty = TypeMap.get(SGA->getValueType());
626
71
  auto *GA =
627
71
      GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
628
71
                          GlobalValue::ExternalLinkage, SGA->getName(), &DstM);
629
71
  GA->copyAttributesFrom(SGA);
630
71
  return GA;
631
71
}
632
633
GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
634
1.69k
                                            bool ForDefinition) {
635
1.69k
  GlobalValue *NewGV;
636
1.69k
  if (auto *
SGVar1.69k
= dyn_cast<GlobalVariable>(SGV)) {
637
406
    NewGV = copyGlobalVariableProto(SGVar);
638
1.69k
  } else 
if (auto *1.29k
SF1.29k
= dyn_cast<Function>(SGV)) {
639
1.21k
    NewGV = copyFunctionProto(SF);
640
1.29k
  } else {
641
73
    if (ForDefinition)
642
71
      NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
643
2
    else 
if (2
SGV->getValueType()->isFunctionTy()2
)
644
1
      NewGV =
645
1
          Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),
646
1
                           GlobalValue::ExternalLinkage, SGV->getName(), &DstM);
647
2
    else
648
1
      NewGV = new GlobalVariable(
649
1
          DstM, TypeMap.get(SGV->getValueType()),
650
1
          /*isConstant*/ false, GlobalValue::ExternalLinkage,
651
1
          /*init*/ nullptr, SGV->getName(),
652
1
          /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
653
1
          SGV->getType()->getAddressSpace());
654
1.29k
  }
655
1.69k
656
1.69k
  if (ForDefinition)
657
1.33k
    NewGV->setLinkage(SGV->getLinkage());
658
359
  else 
if (359
SGV->hasExternalWeakLinkage()359
)
659
2
    NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
660
1.69k
661
1.69k
  if (auto *
NewGO1.69k
= dyn_cast<GlobalObject>(NewGV)) {
662
1.62k
    // Metadata for global variables and function declarations is copied eagerly.
663
1.62k
    if (
isa<GlobalVariable>(SGV) || 1.62k
SGV->isDeclaration()1.22k
)
664
666
      NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
665
1.62k
  }
666
1.69k
667
1.69k
  // Remove these copied constants in case this stays a declaration, since
668
1.69k
  // they point to the source module. If the def is linked the values will
669
1.69k
  // be mapped in during linkFunctionBody.
670
1.69k
  if (auto *
NewF1.69k
= dyn_cast<Function>(NewGV)) {
671
1.21k
    NewF->setPersonalityFn(nullptr);
672
1.21k
    NewF->setPrefixData(nullptr);
673
1.21k
    NewF->setPrologueData(nullptr);
674
1.21k
  }
675
1.69k
676
1.69k
  return NewGV;
677
1.69k
}
678
679
/// Loop over all of the linked values to compute type mappings.  For example,
680
/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
681
/// types 'Foo' but one got renamed when the module was loaded into the same
682
/// LLVMContext.
683
877
void IRLinker::computeTypeMapping() {
684
655
  for (GlobalValue &SGV : SrcM->globals()) {
685
655
    GlobalValue *DGV = getLinkedToGlobal(&SGV);
686
655
    if (!DGV)
687
550
      continue;
688
105
689
105
    
if (105
!DGV->hasAppendingLinkage() || 105
!SGV.hasAppendingLinkage()19
) {
690
86
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
691
86
      continue;
692
86
    }
693
19
694
19
    // Unify the element type of appending arrays.
695
19
    ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
696
19
    ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
697
19
    TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
698
19
  }
699
877
700
877
  for (GlobalValue &SGV : *SrcM)
701
1.59k
    
if (GlobalValue *1.59k
DGV1.59k
= getLinkedToGlobal(&SGV))
702
389
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
703
877
704
877
  for (GlobalValue &SGV : SrcM->aliases())
705
129
    
if (GlobalValue *129
DGV129
= getLinkedToGlobal(&SGV))
706
41
      TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
707
877
708
877
  // Incorporate types by name, scanning all the types in the source module.
709
877
  // At this point, the destination module may have a type "%foo = { i32 }" for
710
877
  // example.  When the source module got loaded into the same LLVMContext, if
711
877
  // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
712
877
  std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
713
149
  for (StructType *ST : Types) {
714
149
    if (!ST->hasName())
715
13
      continue;
716
136
717
136
    
if (136
TypeMap.DstStructTypesSet.hasType(ST)136
) {
718
2
      // This is actually a type from the destination module.
719
2
      // getIdentifiedStructTypes() can have found it by walking debug info
720
2
      // metadata nodes, some of which get linked by name when ODR Type Uniquing
721
2
      // is enabled on the Context, from the source to the destination module.
722
2
      continue;
723
2
    }
724
134
725
134
    // Check to see if there is a dot in the name followed by a digit.
726
134
    size_t DotPos = ST->getName().rfind('.');
727
134
    if (
DotPos == 0 || 134
DotPos == StringRef::npos134
||
728
59
        ST->getName().back() == '.' ||
729
59
        !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
730
94
      continue;
731
40
732
40
    // Check to see if the destination module has a struct with the prefix name.
733
40
    StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
734
40
    if (!DST)
735
0
      continue;
736
40
737
40
    // Don't use it if this actually came from the source module. They're in
738
40
    // the same LLVMContext after all. Also don't use it unless the type is
739
40
    // actually used in the destination module. This can happen in situations
740
40
    // like this:
741
40
    //
742
40
    //      Module A                         Module B
743
40
    //      --------                         --------
744
40
    //   %Z = type { %A }                %B = type { %C.1 }
745
40
    //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
746
40
    //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
747
40
    //   %C = type { i8* }               %B.3 = type { %C.1 }
748
40
    //
749
40
    // When we link Module B with Module A, the '%B' in Module B is
750
40
    // used. However, that would then use '%C.1'. But when we process '%C.1',
751
40
    // we prefer to take the '%C' version. So we are then left with both
752
40
    // '%C.1' and '%C' being used for the same types. This leads to some
753
40
    // variables using one type and some using the other.
754
40
    
if (40
TypeMap.DstStructTypesSet.hasType(DST)40
)
755
31
      TypeMap.addTypeMapping(DST, ST);
756
149
  }
757
877
758
877
  // Now that we have discovered all of the type equivalences, get a body for
759
877
  // any 'opaque' types in the dest module that are now resolved.
760
877
  TypeMap.linkDefinedTypeBodies();
761
877
}
762
763
static void getArrayElements(const Constant *C,
764
64
                             SmallVectorImpl<Constant *> &Dest) {
765
64
  unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
766
64
767
163
  for (unsigned i = 0; 
i != NumElements163
;
++i99
)
768
99
    Dest.push_back(C->getAggregateElement(i));
769
64
}
770
771
/// If there were any appending global variables, link them together now.
772
Expected<Constant *>
773
IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
774
65
                                const GlobalVariable *SrcGV) {
775
65
  Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
776
65
                    ->getElementType();
777
65
778
65
  // FIXME: This upgrade is done during linking to support the C API.  Once the
779
65
  // old form is deprecated, we should move this upgrade to
780
65
  // llvm::UpgradeGlobalVariable() and simplify the logic here and in
781
65
  // Mapper::mapAppendingVariable() in ValueMapper.cpp.
782
65
  StringRef Name = SrcGV->getName();
783
65
  bool IsNewStructor = false;
784
65
  bool IsOldStructor = false;
785
65
  if (
Name == "llvm.global_ctors" || 65
Name == "llvm.global_dtors"33
) {
786
44
    if (cast<StructType>(EltTy)->getNumElements() == 3)
787
41
      IsNewStructor = true;
788
44
    else
789
3
      IsOldStructor = true;
790
44
  }
791
65
792
65
  PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
793
65
  if (
IsOldStructor65
) {
794
3
    auto &ST = *cast<StructType>(EltTy);
795
3
    Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
796
3
    EltTy = StructType::get(SrcGV->getContext(), Tys, false);
797
3
  }
798
65
799
65
  uint64_t DstNumElements = 0;
800
65
  if (
DstGV65
) {
801
18
    ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
802
18
    DstNumElements = DstTy->getNumElements();
803
18
804
18
    if (
!SrcGV->hasAppendingLinkage() || 18
!DstGV->hasAppendingLinkage()18
)
805
0
      return stringErr(
806
0
          "Linking globals named '" + SrcGV->getName() +
807
0
          "': can only link appending global with another appending "
808
0
          "global!");
809
18
810
18
    // Check to see that they two arrays agree on type.
811
18
    
if (18
EltTy != DstTy->getElementType()18
)
812
0
      return stringErr("Appending variables with different element types!");
813
18
    
if (18
DstGV->isConstant() != SrcGV->isConstant()18
)
814
0
      return stringErr("Appending variables linked with different const'ness!");
815
18
816
18
    
if (18
DstGV->getAlignment() != SrcGV->getAlignment()18
)
817
0
      return stringErr(
818
0
          "Appending variables with different alignment need to be linked!");
819
18
820
18
    
if (18
DstGV->getVisibility() != SrcGV->getVisibility()18
)
821
0
      return stringErr(
822
0
          "Appending variables with different visibility need to be linked!");
823
18
824
18
    
if (18
DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr()18
)
825
1
      return stringErr(
826
1
          "Appending variables with different unnamed_addr need to be linked!");
827
17
828
17
    
if (17
DstGV->getSection() != SrcGV->getSection()17
)
829
0
      return stringErr(
830
0
          "Appending variables with different section name need to be linked!");
831
64
  }
832
64
833
64
  SmallVector<Constant *, 16> SrcElements;
834
64
  getArrayElements(SrcGV->getInitializer(), SrcElements);
835
64
836
64
  if (
IsNewStructor64
) {
837
59
    auto It = remove_if(SrcElements, [this](Constant *E) {
838
59
      auto *Key =
839
59
          dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());
840
59
      if (!Key)
841
49
        return false;
842
10
      GlobalValue *DGV = getLinkedToGlobal(Key);
843
10
      return !shouldLink(DGV, *Key);
844
10
    });
845
41
    SrcElements.erase(It, SrcElements.end());
846
41
  }
847
64
  uint64_t NewSize = DstNumElements + SrcElements.size();
848
64
  ArrayType *NewType = ArrayType::get(EltTy, NewSize);
849
64
850
64
  // Create the new global variable.
851
64
  GlobalVariable *NG = new GlobalVariable(
852
64
      DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
853
64
      /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
854
64
      SrcGV->getType()->getAddressSpace());
855
64
856
64
  NG->copyAttributesFrom(SrcGV);
857
64
  forceRenaming(NG, SrcGV->getName());
858
64
859
64
  Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
860
64
861
64
  Mapper.scheduleMapAppendingVariable(*NG,
862
64
                                      DstGV ? 
DstGV->getInitializer()17
:
nullptr47
,
863
64
                                      IsOldStructor, SrcElements);
864
64
865
64
  // Replace any uses of the two global variables with uses of the new
866
64
  // global.
867
64
  if (
DstGV64
) {
868
17
    DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
869
17
    DstGV->eraseFromParent();
870
17
  }
871
65
872
65
  return Ret;
873
65
}
874
875
3.57k
bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
876
3.57k
  if (
ValuesToLink.count(&SGV) || 3.57k
SGV.hasLocalLinkage()1.06k
)
877
2.71k
    return true;
878
860
879
860
  
if (860
DGV && 860
!DGV->isDeclarationForLinker()435
)
880
42
    return false;
881
818
882
818
  
if (818
SGV.isDeclaration() || 818
DoneLinkingBodies170
)
883
664
    return false;
884
154
885
154
  // Callback to the client to give a chance to lazily add the Global to the
886
154
  // list of value to link.
887
154
  bool LazilyAdded = false;
888
60
  AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
889
60
    maybeAdd(&GV);
890
60
    LazilyAdded = true;
891
60
  });
892
3.57k
  return LazilyAdded;
893
3.57k
}
894
895
Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
896
1.87k
                                                    bool ForAlias) {
897
1.87k
  GlobalValue *DGV = getLinkedToGlobal(SGV);
898
1.87k
899
1.87k
  bool ShouldLink = shouldLink(DGV, *SGV);
900
1.87k
901
1.87k
  // just missing from map
902
1.87k
  if (
ShouldLink1.87k
) {
903
1.44k
    auto I = ValueMap.find(SGV);
904
1.44k
    if (I != ValueMap.end())
905
39
      return cast<Constant>(I->second);
906
1.40k
907
1.40k
    I = AliasValueMap.find(SGV);
908
1.40k
    if (I != AliasValueMap.end())
909
1
      return cast<Constant>(I->second);
910
1.83k
  }
911
1.83k
912
1.83k
  
if (1.83k
!ShouldLink && 1.83k
ForAlias431
)
913
6
    DGV = nullptr;
914
1.83k
915
1.83k
  // Handle the ultra special appending linkage case first.
916
1.83k
  assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
917
1.83k
  if (SGV->hasAppendingLinkage())
918
65
    return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
919
65
                                 cast<GlobalVariable>(SGV));
920
1.76k
921
1.76k
  GlobalValue *NewGV;
922
1.76k
  if (
DGV && 1.76k
!ShouldLink302
) {
923
55
    NewGV = DGV;
924
1.76k
  } else {
925
1.71k
    // If we are done linking global value bodies (i.e. we are performing
926
1.71k
    // metadata linking), don't link in the global value due to this
927
1.71k
    // reference, simply map it to null.
928
1.71k
    if (DoneLinkingBodies)
929
17
      return nullptr;
930
1.69k
931
1.69k
    NewGV = copyGlobalValueProto(SGV, ShouldLink);
932
1.69k
    if (
ShouldLink || 1.69k
!ForAlias359
)
933
1.69k
      forceRenaming(NewGV, SGV->getName());
934
1.71k
  }
935
1.76k
936
1.76k
  // Overloaded intrinsics have overloaded types names as part of their
937
1.76k
  // names. If we renamed overloaded types we should rename the intrinsic
938
1.76k
  // as well.
939
1.75k
  
if (Function *1.75k
F1.75k
= dyn_cast<Function>(NewGV))
940
1.25k
    
if (auto 1.25k
Remangled1.25k
= Intrinsic::remangleIntrinsicFunction(F))
941
1
      NewGV = Remangled.getValue();
942
1.75k
943
1.75k
  if (
ShouldLink || 1.75k
ForAlias414
) {
944
1.34k
    if (const Comdat *
SC1.34k
= SGV->getComdat()) {
945
92
      if (auto *
GO92
= dyn_cast<GlobalObject>(NewGV)) {
946
63
        Comdat *DC = DstM.getOrInsertComdat(SC->getName());
947
63
        DC->setSelectionKind(SC->getSelectionKind());
948
63
        GO->setComdat(DC);
949
63
      }
950
92
    }
951
1.34k
  }
952
1.75k
953
1.75k
  if (
!ShouldLink && 1.75k
ForAlias414
)
954
6
    NewGV->setLinkage(GlobalValue::InternalLinkage);
955
1.75k
956
1.75k
  Constant *C = NewGV;
957
1.75k
  if (DGV)
958
302
    C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType()));
959
1.75k
960
1.75k
  if (
DGV && 1.75k
NewGV != DGV302
) {
961
247
    DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
962
247
    DGV->eraseFromParent();
963
247
  }
964
1.75k
965
1.75k
  return C;
966
1.87k
}
967
968
/// Update the initializers in the Dest module now that all globals that may be
969
/// referenced are in Dest.
970
329
void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
971
329
  // Figure out what the initializer looks like in the dest module.
972
329
  Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
973
329
}
974
975
/// Copy the source function over into the dest function and fix up references
976
/// to values. At this point we know that Dest is an external function, and
977
/// that Src is not.
978
944
Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
979
944
  assert(Dst.isDeclaration() && !Src.isDeclaration());
980
944
981
944
  // Materialize if needed.
982
944
  if (Error Err = Src.materialize())
983
0
    return Err;
984
944
985
944
  // Link in the operands without remapping.
986
944
  
if (944
Src.hasPrefixData()944
)
987
0
    Dst.setPrefixData(Src.getPrefixData());
988
944
  if (Src.hasPrologueData())
989
2
    Dst.setPrologueData(Src.getPrologueData());
990
944
  if (Src.hasPersonalityFn())
991
1
    Dst.setPersonalityFn(Src.getPersonalityFn());
992
944
993
944
  // Copy over the metadata attachments without remapping.
994
944
  Dst.copyMetadata(&Src, 0);
995
944
996
944
  // Steal arguments and splice the body of Src into Dst.
997
944
  Dst.stealArgumentListFrom(Src);
998
944
  Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
999
944
1000
944
  // Everything has been moved over.  Remap it.
1001
944
  Mapper.scheduleRemapFunction(Dst);
1002
944
  return Error::success();
1003
944
}
1004
1005
71
void IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1006
71
  Mapper.scheduleMapGlobalAliasee(Dst, *Src.getAliasee(), AliasMCID);
1007
71
}
1008
1009
1.34k
Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1010
1.34k
  if (auto *F = dyn_cast<Function>(&Src))
1011
944
    return linkFunctionBody(cast<Function>(Dst), *F);
1012
400
  
if (auto *400
GVar400
= dyn_cast<GlobalVariable>(&Src)) {
1013
329
    linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);
1014
329
    return Error::success();
1015
329
  }
1016
71
  linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
1017
71
  return Error::success();
1018
71
}
1019
1020
99
void IRLinker::prepareCompileUnitsForImport() {
1021
99
  NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
1022
99
  if (!SrcCompileUnits)
1023
86
    return;
1024
13
  // When importing for ThinLTO, prevent importing of types listed on
1025
13
  // the DICompileUnit that we don't need a copy of in the importing
1026
13
  // module. They will be emitted by the originating module.
1027
26
  
for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); 13
I != E26
;
++I13
) {
1028
13
    auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I));
1029
13
    assert(CU && "Expected valid compile unit");
1030
13
    // Enums, macros, and retained types don't need to be listed on the
1031
13
    // imported DICompileUnit. This means they will only be imported
1032
13
    // if reached from the mapped IR. Do this by setting their value map
1033
13
    // entries to nullptr, which will automatically prevent their importing
1034
13
    // when reached from the DICompileUnit during metadata mapping.
1035
13
    ValueMap.MD()[CU->getRawEnumTypes()].reset(nullptr);
1036
13
    ValueMap.MD()[CU->getRawMacros()].reset(nullptr);
1037
13
    ValueMap.MD()[CU->getRawRetainedTypes()].reset(nullptr);
1038
13
    // If we ever start importing global variable defs, we'll need to
1039
13
    // add their DIGlobalVariable to the globals list on the imported
1040
13
    // DICompileUnit. Confirm none are imported, and then we can
1041
13
    // map the list of global variables to nullptr.
1042
13
    assert(none_of(
1043
13
               ValuesToLink,
1044
13
               [](const GlobalValue *GV) { return isa<GlobalVariable>(GV); }) &&
1045
13
           "Unexpected importing of a GlobalVariable definition");
1046
13
    ValueMap.MD()[CU->getRawGlobalVariables()].reset(nullptr);
1047
13
1048
13
    // Imported entities only need to be mapped in if they have local
1049
13
    // scope, as those might correspond to an imported entity inside a
1050
13
    // function being imported (any locally scoped imported entities that
1051
13
    // don't end up referenced by an imported function will not be emitted
1052
13
    // into the object). Imported entities not in a local scope
1053
13
    // (e.g. on the namespace) only need to be emitted by the originating
1054
13
    // module. Create a list of the locally scoped imported entities, and
1055
13
    // replace the source CUs imported entity list with the new list, so
1056
13
    // only those are mapped in.
1057
13
    // FIXME: Locally-scoped imported entities could be moved to the
1058
13
    // functions they are local to instead of listing them on the CU, and
1059
13
    // we would naturally only link in those needed by function importing.
1060
13
    SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
1061
13
    bool ReplaceImportedEntities = false;
1062
2
    for (auto *IE : CU->getImportedEntities()) {
1063
2
      DIScope *Scope = IE->getScope();
1064
2
      assert(Scope && "Invalid Scope encoding!");
1065
2
      if (isa<DILocalScope>(Scope))
1066
1
        AllImportedModules.emplace_back(IE);
1067
2
      else
1068
1
        ReplaceImportedEntities = true;
1069
2
    }
1070
13
    if (
ReplaceImportedEntities13
) {
1071
1
      if (!AllImportedModules.empty())
1072
1
        CU->replaceImportedEntities(MDTuple::get(
1073
1
            CU->getContext(),
1074
1
            SmallVector<Metadata *, 16>(AllImportedModules.begin(),
1075
1
                                        AllImportedModules.end())));
1076
1
      else
1077
1
        // If there were no local scope imported entities, we can map
1078
1
        // the whole list to nullptr.
1079
0
        ValueMap.MD()[CU->getRawImportedEntities()].reset(nullptr);
1080
1
    }
1081
13
  }
1082
99
}
1083
1084
/// Insert all of the named MDNodes in Src into the Dest module.
1085
876
void IRLinker::linkNamedMDNodes() {
1086
876
  const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1087
269
  for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1088
269
    // Don't link module flags here. Do them separately.
1089
269
    if (&NMD == SrcModFlags)
1090
123
      continue;
1091
146
    NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1092
146
    // Add Src elements into Dest node.
1093
146
    for (const MDNode *Op : NMD.operands())
1094
239
      DestNMD->addOperand(Mapper.mapMDNode(*Op));
1095
269
  }
1096
876
}
1097
1098
/// Merge the linker flags in Src into the Dest module.
1099
876
Error IRLinker::linkModuleFlagsMetadata() {
1100
876
  // If the source module has no module flags, we are done.
1101
876
  const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1102
876
  if (!SrcModFlags)
1103
753
    return Error::success();
1104
123
1105
123
  // If the destination module doesn't have module flags yet, then just copy
1106
123
  // over the source module's flags.
1107
123
  NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1108
123
  if (
DstModFlags->getNumOperands() == 0123
) {
1109
156
    for (unsigned I = 0, E = SrcModFlags->getNumOperands(); 
I != E156
;
++I95
)
1110
95
      DstModFlags->addOperand(SrcModFlags->getOperand(I));
1111
61
1112
61
    return Error::success();
1113
61
  }
1114
62
1115
62
  // First build a map of the existing module flags and requirements.
1116
62
  DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1117
62
  SmallSetVector<MDNode *, 16> Requirements;
1118
159
  for (unsigned I = 0, E = DstModFlags->getNumOperands(); 
I != E159
;
++I97
) {
1119
97
    MDNode *Op = DstModFlags->getOperand(I);
1120
97
    ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1121
97
    MDString *ID = cast<MDString>(Op->getOperand(1));
1122
97
1123
97
    if (
Behavior->getZExtValue() == Module::Require97
) {
1124
0
      Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1125
97
    } else {
1126
97
      Flags[ID] = std::make_pair(Op, I);
1127
97
    }
1128
97
  }
1129
62
1130
62
  // Merge in the flags from the source module, and also collect its set of
1131
62
  // requirements.
1132
147
  for (unsigned I = 0, E = SrcModFlags->getNumOperands(); 
I != E147
;
++I85
) {
1133
89
    MDNode *SrcOp = SrcModFlags->getOperand(I);
1134
89
    ConstantInt *SrcBehavior =
1135
89
        mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1136
89
    MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1137
89
    MDNode *DstOp;
1138
89
    unsigned DstIndex;
1139
89
    std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1140
89
    unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1141
89
1142
89
    // If this is a requirement, add it and continue.
1143
89
    if (
SrcBehaviorValue == Module::Require89
) {
1144
2
      // If the destination module does not already have this requirement, add
1145
2
      // it.
1146
2
      if (
Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))2
) {
1147
2
        DstModFlags->addOperand(SrcOp);
1148
2
      }
1149
2
      continue;
1150
2
    }
1151
87
1152
87
    // If there is no existing flag with this ID, just add it.
1153
87
    
if (87
!DstOp87
) {
1154
1
      Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1155
1
      DstModFlags->addOperand(SrcOp);
1156
1
      continue;
1157
1
    }
1158
86
1159
86
    // Otherwise, perform a merge.
1160
86
    ConstantInt *DstBehavior =
1161
86
        mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1162
86
    unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1163
86
1164
5
    auto overrideDstValue = [&]() {
1165
5
      DstModFlags->setOperand(DstIndex, SrcOp);
1166
5
      Flags[ID].first = SrcOp;
1167
5
    };
1168
86
1169
86
    // If either flag has override behavior, handle it first.
1170
86
    if (
DstBehaviorValue == Module::Override86
) {
1171
3
      // Diagnose inconsistent flags which both have override behavior.
1172
3
      if (SrcBehaviorValue == Module::Override &&
1173
1
          SrcOp->getOperand(2) != DstOp->getOperand(2))
1174
1
        return stringErr("linking module flags '" + ID->getString() +
1175
1
                         "': IDs have conflicting override values");
1176
2
      continue;
1177
83
    } else 
if (83
SrcBehaviorValue == Module::Override83
) {
1178
3
      // Update the destination flag to that of the source.
1179
3
      overrideDstValue();
1180
3
      continue;
1181
3
    }
1182
80
1183
80
    // Diagnose inconsistent merge behavior types.
1184
80
    
if (80
SrcBehaviorValue != DstBehaviorValue80
)
1185
1
      return stringErr("linking module flags '" + ID->getString() +
1186
1
                       "': IDs have conflicting behaviors");
1187
79
1188
79
    
auto replaceDstValue = [&](MDNode *New) 79
{
1189
4
      Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1190
4
      MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1191
4
      DstModFlags->setOperand(DstIndex, Flag);
1192
4
      Flags[ID].first = Flag;
1193
4
    };
1194
79
1195
79
    // Perform the merge for standard behavior types.
1196
79
    switch (SrcBehaviorValue) {
1197
0
    case Module::Require:
1198
0
    case Module::Override:
1199
0
      llvm_unreachable("not possible");
1200
39
    case Module::Error: {
1201
39
      // Emit an error if the values differ.
1202
39
      if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1203
2
        return stringErr("linking module flags '" + ID->getString() +
1204
2
                         "': IDs have conflicting values");
1205
37
      continue;
1206
37
    }
1207
33
    case Module::Warning: {
1208
33
      // Emit a warning if the values differ.
1209
33
      if (
SrcOp->getOperand(2) != DstOp->getOperand(2)33
) {
1210
1
        emitWarning("linking module flags '" + ID->getString() +
1211
1
                    "': IDs have conflicting values");
1212
1
      }
1213
33
      continue;
1214
37
    }
1215
3
    case Module::Max: {
1216
3
      ConstantInt *DstValue =
1217
3
          mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1218
3
      ConstantInt *SrcValue =
1219
3
          mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1220
3
      if (SrcValue->getZExtValue() > DstValue->getZExtValue())
1221
2
        overrideDstValue();
1222
3
      break;
1223
37
    }
1224
2
    case Module::Append: {
1225
2
      MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1226
2
      MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1227
2
      SmallVector<Metadata *, 8> MDs;
1228
2
      MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1229
2
      MDs.append(DstValue->op_begin(), DstValue->op_end());
1230
2
      MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1231
2
1232
2
      replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1233
2
      break;
1234
37
    }
1235
2
    case Module::AppendUnique: {
1236
2
      SmallSetVector<Metadata *, 16> Elts;
1237
2
      MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1238
2
      MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1239
2
      Elts.insert(DstValue->op_begin(), DstValue->op_end());
1240
2
      Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1241
2
1242
2
      replaceDstValue(MDNode::get(DstM.getContext(),
1243
2
                                  makeArrayRef(Elts.begin(), Elts.end())));
1244
2
      break;
1245
0
    }
1246
89
    }
1247
89
  }
1248
62
1249
62
  // Check all of the requirements.
1250
59
  
for (unsigned I = 0, E = Requirements.size(); 58
I != E59
;
++I1
) {
1251
2
    MDNode *Requirement = Requirements[I];
1252
2
    MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1253
2
    Metadata *ReqValue = Requirement->getOperand(1);
1254
2
1255
2
    MDNode *Op = Flags[Flag].first;
1256
2
    if (
!Op || 2
Op->getOperand(2) != ReqValue2
)
1257
1
      return stringErr("linking module flags '" + Flag->getString() +
1258
1
                       "': does not have the required value");
1259
2
  }
1260
57
  return Error::success();
1261
876
}
1262
1263
/// Return InlineAsm adjusted with target-specific directives if required.
1264
/// For ARM and Thumb, we have to add directives to select the appropriate ISA
1265
/// to support mixing module-level inline assembly from ARM and Thumb modules.
1266
static std::string adjustInlineAsm(const std::string &InlineAsm,
1267
13
                                   const Triple &Triple) {
1268
13
  if (
Triple.getArch() == Triple::thumb || 13
Triple.getArch() == Triple::thumbeb12
)
1269
1
    return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1270
12
  
if (12
Triple.getArch() == Triple::arm || 12
Triple.getArch() == Triple::armeb10
)
1271
2
    return ".text\n.balign 4\n.arm\n" + InlineAsm;
1272
10
  return InlineAsm;
1273
10
}
1274
1275
877
Error IRLinker::run() {
1276
877
  // Ensure metadata materialized before value mapping.
1277
877
  if (SrcM->getMaterializer())
1278
489
    
if (Error 489
Err489
= SrcM->getMaterializer()->materializeMetadata())
1279
0
      return Err;
1280
877
1281
877
  // Inherit the target data from the source module if the destination module
1282
877
  // doesn't have one already.
1283
877
  
if (877
DstM.getDataLayout().isDefault()877
)
1284
709
    DstM.setDataLayout(SrcM->getDataLayout());
1285
877
1286
877
  if (
SrcM->getDataLayout() != DstM.getDataLayout()877
) {
1287
2
    emitWarning("Linking two modules of different data layouts: '" +
1288
2
                SrcM->getModuleIdentifier() + "' is '" +
1289
2
                SrcM->getDataLayoutStr() + "' whereas '" +
1290
2
                DstM.getModuleIdentifier() + "' is '" +
1291
2
                DstM.getDataLayoutStr() + "'\n");
1292
2
  }
1293
877
1294
877
  // Copy the target triple from the source to dest if the dest's is empty.
1295
877
  if (
DstM.getTargetTriple().empty() && 877
!SrcM->getTargetTriple().empty()704
)
1296
255
    DstM.setTargetTriple(SrcM->getTargetTriple());
1297
877
1298
877
  Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1299
877
1300
877
  if (!SrcM->getTargetTriple().empty()&&
1301
427
      !SrcTriple.isCompatibleWith(DstTriple))
1302
3
    emitWarning("Linking two modules of different target triples: " +
1303
3
                SrcM->getModuleIdentifier() + "' is '" +
1304
3
                SrcM->getTargetTriple() + "' whereas '" +
1305
3
                DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1306
3
                "'\n");
1307
877
1308
877
  DstM.setTargetTriple(SrcTriple.merge(DstTriple));
1309
877
1310
877
  // Append the module inline asm string.
1311
877
  if (
!IsPerformingImport && 877
!SrcM->getModuleInlineAsm().empty()778
) {
1312
13
    std::string SrcModuleInlineAsm = adjustInlineAsm(SrcM->getModuleInlineAsm(),
1313
13
                                                     SrcTriple);
1314
13
    if (DstM.getModuleInlineAsm().empty())
1315
12
      DstM.setModuleInlineAsm(SrcModuleInlineAsm);
1316
13
    else
1317
1
      DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1318
1
                              SrcModuleInlineAsm);
1319
13
  }
1320
877
1321
877
  // Loop over all of the linked values to compute type mappings.
1322
877
  computeTypeMapping();
1323
877
1324
877
  std::reverse(Worklist.begin(), Worklist.end());
1325
2.17k
  while (
!Worklist.empty()2.17k
) {
1326
1.29k
    GlobalValue *GV = Worklist.back();
1327
1.29k
    Worklist.pop_back();
1328
1.29k
1329
1.29k
    // Already mapped.
1330
1.29k
    if (ValueMap.find(GV) != ValueMap.end() ||
1331
1.16k
        AliasValueMap.find(GV) != AliasValueMap.end())
1332
141
      continue;
1333
1.15k
1334
1.29k
    assert(!GV->isDeclaration());
1335
1.15k
    Mapper.mapValue(*GV);
1336
1.15k
    if (FoundError)
1337
1
      return std::move(*FoundError);
1338
1.29k
  }
1339
877
1340
877
  // Note that we are done linking global value bodies. This prevents
1341
877
  // metadata linking from creating new references.
1342
876
  DoneLinkingBodies = true;
1343
876
  Mapper.addFlags(RF_NullMapMissingGlobalValues);
1344
876
1345
876
  // Remap all of the named MDNodes in Src into the DstM module. We do this
1346
876
  // after linking GlobalValues so that MDNodes that reference GlobalValues
1347
876
  // are properly remapped.
1348
876
  linkNamedMDNodes();
1349
876
1350
876
  // Merge the module flags into the DstM module.
1351
876
  return linkModuleFlagsMetadata();
1352
877
}
1353
1354
IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1355
69
    : ETypes(E), IsPacked(P) {}
1356
1357
IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1358
257
    : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1359
1360
47
bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1361
47
  return IsPacked == That.IsPacked && ETypes == That.ETypes;
1362
47
}
1363
1364
0
bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1365
0
  return !this->operator==(That);
1366
0
}
1367
1368
4.46k
StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1369
4.46k
  return DenseMapInfo<StructType *>::getEmptyKey();
1370
4.46k
}
1371
1372
400
StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1373
400
  return DenseMapInfo<StructType *>::getTombstoneKey();
1374
400
}
1375
1376
197
unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1377
197
  return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1378
197
                      Key.IsPacked);
1379
197
}
1380
1381
166
unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1382
166
  return getHashValue(KeyTy(ST));
1383
166
}
1384
1385
bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1386
32
                                         const StructType *RHS) {
1387
32
  if (
RHS == getEmptyKey() || 32
RHS == getTombstoneKey()3
)
1388
29
    return false;
1389
3
  return LHS == KeyTy(RHS);
1390
3
}
1391
1392
bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1393
4.02k
                                         const StructType *RHS) {
1394
4.02k
  if (
RHS == getEmptyKey() || 4.02k
RHS == getTombstoneKey()145
)
1395
3.98k
    return LHS == RHS;
1396
38
  return KeyTy(LHS) == KeyTy(RHS);
1397
38
}
1398
1399
96
void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1400
96
  assert(!Ty->isOpaque());
1401
96
  NonOpaqueStructTypes.insert(Ty);
1402
96
}
1403
1404
5
void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1405
5
  assert(!Ty->isOpaque());
1406
5
  NonOpaqueStructTypes.insert(Ty);
1407
5
  bool Removed = OpaqueStructTypes.erase(Ty);
1408
5
  (void)Removed;
1409
5
  assert(Removed);
1410
5
}
1411
1412
17
void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1413
17
  assert(Ty->isOpaque());
1414
17
  OpaqueStructTypes.insert(Ty);
1415
17
}
1416
1417
StructType *
1418
IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1419
69
                                                bool IsPacked) {
1420
69
  IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1421
69
  auto I = NonOpaqueStructTypes.find_as(Key);
1422
69
  return I == NonOpaqueStructTypes.end() ? 
nullptr66
:
*I3
;
1423
69
}
1424
1425
176
bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1426
176
  if (Ty->isOpaque())
1427
35
    return OpaqueStructTypes.count(Ty);
1428
141
  auto I = NonOpaqueStructTypes.find(Ty);
1429
141
  return I == NonOpaqueStructTypes.end() ? 
false97
:
*I == Ty44
;
1430
176
}
1431
1432
726
IRMover::IRMover(Module &M) : Composite(M) {
1433
726
  TypeFinder StructTypes;
1434
726
  StructTypes.run(M, /* OnlyNamed */ false);
1435
23
  for (StructType *Ty : StructTypes) {
1436
23
    if (Ty->isOpaque())
1437
1
      IdentifiedStructTypes.addOpaque(Ty);
1438
23
    else
1439
22
      IdentifiedStructTypes.addNonOpaque(Ty);
1440
23
  }
1441
726
  // Self-map metadatas in the destination module. This is needed when
1442
726
  // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1443
726
  // destination module may be reached from the source module.
1444
393
  for (auto *MD : StructTypes.getVisitedMetadata()) {
1445
393
    SharedMDs[MD].reset(const_cast<MDNode *>(MD));
1446
393
  }
1447
726
}
1448
1449
Error IRMover::move(
1450
    std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
1451
    std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
1452
877
    bool IsPerformingImport) {
1453
877
  IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1454
877
                       std::move(Src), ValuesToLink, std::move(AddLazyFor),
1455
877
                       IsPerformingImport);
1456
877
  Error E = TheIRLinker.run();
1457
877
  Composite.dropTriviallyDeadConstantArrays();
1458
877
  return E;
1459
877
}