blob: afbc57abfcc08da06c0afe775e550353af5ce4cb [file] [log] [blame]
Rafael Espindolada513202015-12-10 14:19:35 +00001//===- 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"
Teresa Johnsone8df2342015-12-18 17:51:37 +000016#include "llvm/IR/DebugInfo.h"
Rafael Espindolada513202015-12-10 14:19:35 +000017#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnsond0f8afa2015-12-17 17:14:09 +000018#include "llvm/IR/GVMaterializer.h"
Artur Pilipenko140d9e62016-06-24 15:10:29 +000019#include "llvm/IR/Intrinsics.h"
Rafael Espindolada513202015-12-10 14:19:35 +000020#include "llvm/IR/TypeFinder.h"
Peter Collingbourneb7bfc992016-05-27 05:21:35 +000021#include "llvm/Support/Error.h"
Rafael Espindolada513202015-12-10 14:19:35 +000022#include "llvm/Transforms/Utils/Cloning.h"
Benjamin Kramer14aae012016-05-27 14:27:24 +000023#include <utility>
Rafael Espindolada513202015-12-10 14:19:35 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// TypeMap implementation.
28//===----------------------------------------------------------------------===//
29
30namespace {
31class 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
50public:
51 TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
52 : DstStructTypesSet(DstStructTypesSet) {}
53
54 IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
55 /// Indicate that the specified type in the destination module is conceptually
56 /// equivalent to the specified type in the source module.
57 void addTypeMapping(Type *DstTy, Type *SrcTy);
58
59 /// Produce a body for an opaque type in the dest module from a type
60 /// definition in the source module.
61 void linkDefinedTypeBodies();
62
63 /// Return the mapped type to use for the specified input type from the
64 /// source module.
65 Type *get(Type *SrcTy);
66 Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
67
68 void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
69
70 FunctionType *get(FunctionType *T) {
71 return cast<FunctionType>(get((Type *)T));
72 }
73
74private:
75 Type *remapType(Type *SrcTy) override { return get(SrcTy); }
76
77 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
78};
79}
80
81void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
82 assert(SpeculativeTypes.empty());
83 assert(SpeculativeDstOpaqueTypes.empty());
84
85 // Check to see if these types are recursively isomorphic and establish a
86 // mapping between them if so.
87 if (!areTypesIsomorphic(DstTy, SrcTy)) {
88 // Oops, they aren't isomorphic. Just discard this request by rolling out
89 // any speculative mappings we've established.
90 for (Type *Ty : SpeculativeTypes)
91 MappedTypes.erase(Ty);
92
93 SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
94 SpeculativeDstOpaqueTypes.size());
95 for (StructType *Ty : SpeculativeDstOpaqueTypes)
96 DstResolvedOpaqueTypes.erase(Ty);
97 } else {
Eugene Leviant266aad52018-01-25 08:35:52 +000098 // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy
99 // and all its descendants to lower amount of renaming in LLVM context
100 // Renaming occurs because we load all source modules to the same context
101 // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
102 // As a result we may get several different types in the destination
103 // module, which are in fact the same.
Rafael Espindolada513202015-12-10 14:19:35 +0000104 for (Type *Ty : SpeculativeTypes)
105 if (auto *STy = dyn_cast<StructType>(Ty))
106 if (STy->hasName())
107 STy->setName("");
108 }
109 SpeculativeTypes.clear();
110 SpeculativeDstOpaqueTypes.clear();
111}
112
113/// Recursively walk this pair of types, returning true if they are isomorphic,
114/// false if they are not.
115bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
116 // Two types with differing kinds are clearly not isomorphic.
117 if (DstTy->getTypeID() != SrcTy->getTypeID())
118 return false;
119
120 // If we have an entry in the MappedTypes table, then we have our answer.
121 Type *&Entry = MappedTypes[SrcTy];
122 if (Entry)
123 return Entry == DstTy;
124
125 // Two identical types are clearly isomorphic. Remember this
126 // non-speculatively.
127 if (DstTy == SrcTy) {
128 Entry = DstTy;
129 return true;
130 }
131
132 // Okay, we have two types with identical kinds that we haven't seen before.
133
134 // If this is an opaque struct type, special case it.
135 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
136 // Mapping an opaque type to any struct, just keep the dest struct.
137 if (SSTy->isOpaque()) {
138 Entry = DstTy;
139 SpeculativeTypes.push_back(SrcTy);
140 return true;
141 }
142
143 // Mapping a non-opaque source type to an opaque dest. If this is the first
144 // type that we're mapping onto this destination type then we succeed. Keep
145 // the dest, but fill it in later. If this is the second (different) type
146 // that we're trying to map onto the same opaque type then we fail.
147 if (cast<StructType>(DstTy)->isOpaque()) {
148 // We can only map one source type onto the opaque destination type.
149 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
150 return false;
151 SrcDefinitionsToResolve.push_back(SSTy);
152 SpeculativeTypes.push_back(SrcTy);
153 SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
154 Entry = DstTy;
155 return true;
156 }
157 }
158
159 // If the number of subtypes disagree between the two types, then we fail.
160 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
161 return false;
162
163 // Fail if any of the extra properties (e.g. array size) of the type disagree.
164 if (isa<IntegerType>(DstTy))
165 return false; // bitwidth disagrees.
166 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
167 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
168 return false;
Rafael Espindolada513202015-12-10 14:19:35 +0000169 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
170 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
171 return false;
172 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
173 StructType *SSTy = cast<StructType>(SrcTy);
174 if (DSTy->isLiteral() != SSTy->isLiteral() ||
175 DSTy->isPacked() != SSTy->isPacked())
176 return false;
Peter Collingbournea705e0e2016-12-02 03:20:58 +0000177 } else if (auto *DSeqTy = dyn_cast<SequentialType>(DstTy)) {
178 if (DSeqTy->getNumElements() !=
179 cast<SequentialType>(SrcTy)->getNumElements())
Rafael Espindolada513202015-12-10 14:19:35 +0000180 return false;
181 }
182
183 // Otherwise, we speculate that these two types will line up and recursively
184 // check the subelements.
185 Entry = DstTy;
186 SpeculativeTypes.push_back(SrcTy);
187
188 for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
189 if (!areTypesIsomorphic(DstTy->getContainedType(I),
190 SrcTy->getContainedType(I)))
191 return false;
192
193 // If everything seems to have lined up, then everything is great.
194 return true;
195}
196
197void TypeMapTy::linkDefinedTypeBodies() {
198 SmallVector<Type *, 16> Elements;
199 for (StructType *SrcSTy : SrcDefinitionsToResolve) {
200 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
201 assert(DstSTy->isOpaque());
202
203 // Map the body of the source type over to a new body for the dest type.
204 Elements.resize(SrcSTy->getNumElements());
205 for (unsigned I = 0, E = Elements.size(); I != E; ++I)
206 Elements[I] = get(SrcSTy->getElementType(I));
207
208 DstSTy->setBody(Elements, SrcSTy->isPacked());
209 DstStructTypesSet.switchToNonOpaque(DstSTy);
210 }
211 SrcDefinitionsToResolve.clear();
212 DstResolvedOpaqueTypes.clear();
213}
214
215void TypeMapTy::finishType(StructType *DTy, StructType *STy,
216 ArrayRef<Type *> ETypes) {
217 DTy->setBody(ETypes, STy->isPacked());
218
219 // Steal STy's name.
220 if (STy->hasName()) {
221 SmallString<16> TmpName = STy->getName();
222 STy->setName("");
223 DTy->setName(TmpName);
224 }
225
226 DstStructTypesSet.addNonOpaque(DTy);
227}
228
229Type *TypeMapTy::get(Type *Ty) {
230 SmallPtrSet<StructType *, 8> Visited;
231 return get(Ty, Visited);
232}
233
234Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
235 // If we already have an entry for this type, return it.
236 Type **Entry = &MappedTypes[Ty];
237 if (*Entry)
238 return *Entry;
239
240 // These are types that LLVM itself will unique.
241 bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
242
Rafael Espindolada513202015-12-10 14:19:35 +0000243 if (!IsUniqued) {
Vlad Tsyrklevich444617b2018-06-20 16:50:56 +0000244 StructType *STy = cast<StructType>(Ty);
245 // This is actually a type from the destination module, this can be reached
246 // when this type is loaded in another module, added to DstStructTypesSet,
247 // and then we reach the same type in another module where it has not been
248 // added to MappedTypes. (PR37684)
249 if (STy->getContext().isODRUniquingDebugTypes() && !STy->isOpaque() &&
250 DstStructTypesSet.hasType(STy))
251 return *Entry = STy;
252
253#ifndef NDEBUG
Rafael Espindolada513202015-12-10 14:19:35 +0000254 for (auto &Pair : MappedTypes) {
255 assert(!(Pair.first != Ty && Pair.second == Ty) &&
256 "mapping to a source type");
257 }
Rafael Espindolada513202015-12-10 14:19:35 +0000258#endif
259
Vlad Tsyrklevich444617b2018-06-20 16:50:56 +0000260 if (!Visited.insert(STy).second) {
261 StructType *DTy = StructType::create(Ty->getContext());
262 return *Entry = DTy;
263 }
Rafael Espindolada513202015-12-10 14:19:35 +0000264 }
265
266 // If this is not a recursive type, then just map all of the elements and
267 // then rebuild the type from inside out.
268 SmallVector<Type *, 4> ElementTypes;
269
270 // If there are no element types to map, then the type is itself. This is
271 // true for the anonymous {} struct, things like 'float', integers, etc.
272 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
273 return *Entry = Ty;
274
275 // Remap all of the elements, keeping track of whether any of them change.
276 bool AnyChange = false;
277 ElementTypes.resize(Ty->getNumContainedTypes());
278 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
279 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
280 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
281 }
282
283 // If we found our type while recursively processing stuff, just use it.
284 Entry = &MappedTypes[Ty];
285 if (*Entry) {
286 if (auto *DTy = dyn_cast<StructType>(*Entry)) {
287 if (DTy->isOpaque()) {
288 auto *STy = cast<StructType>(Ty);
289 finishType(DTy, STy, ElementTypes);
290 }
291 }
292 return *Entry;
293 }
294
295 // If all of the element types mapped directly over and the type is not
Hans Wennborgd673d6f2016-10-19 20:10:03 +0000296 // a named struct, then the type is usable as-is.
Rafael Espindolada513202015-12-10 14:19:35 +0000297 if (!AnyChange && IsUniqued)
298 return *Entry = Ty;
299
300 // Otherwise, rebuild a modified type.
301 switch (Ty->getTypeID()) {
302 default:
303 llvm_unreachable("unknown derived type to remap");
304 case Type::ArrayTyID:
305 return *Entry = ArrayType::get(ElementTypes[0],
306 cast<ArrayType>(Ty)->getNumElements());
307 case Type::VectorTyID:
308 return *Entry = VectorType::get(ElementTypes[0],
309 cast<VectorType>(Ty)->getNumElements());
310 case Type::PointerTyID:
311 return *Entry = PointerType::get(ElementTypes[0],
312 cast<PointerType>(Ty)->getAddressSpace());
313 case Type::FunctionTyID:
314 return *Entry = FunctionType::get(ElementTypes[0],
315 makeArrayRef(ElementTypes).slice(1),
316 cast<FunctionType>(Ty)->isVarArg());
317 case Type::StructTyID: {
318 auto *STy = cast<StructType>(Ty);
319 bool IsPacked = STy->isPacked();
320 if (IsUniqued)
321 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
322
323 // If the type is opaque, we can just use it directly.
324 if (STy->isOpaque()) {
325 DstStructTypesSet.addOpaque(STy);
326 return *Entry = Ty;
327 }
328
Rafael Espindola2d01b0d2018-02-21 20:12:18 +0000329 if (StructType *OldT =
330 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
Rafael Espindolada513202015-12-10 14:19:35 +0000331 STy->setName("");
332 return *Entry = OldT;
333 }
334
335 if (!AnyChange) {
336 DstStructTypesSet.addNonOpaque(STy);
337 return *Entry = Ty;
338 }
339
340 StructType *DTy = StructType::create(Ty->getContext());
341 finishType(DTy, STy, ElementTypes);
342 return *Entry = DTy;
343 }
344 }
345}
346
347LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
348 const Twine &Msg)
349 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
350void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
351
352//===----------------------------------------------------------------------===//
Teresa Johnson18f59c52015-12-18 19:28:59 +0000353// IRLinker implementation.
Rafael Espindolada513202015-12-10 14:19:35 +0000354//===----------------------------------------------------------------------===//
355
356namespace {
357class IRLinker;
358
359/// Creates prototypes for functions that are lazily linked on the fly. This
360/// speeds up linking for modules with many/ lazily linked functions of which
361/// few get used.
362class GlobalValueMaterializer final : public ValueMaterializer {
Mehdi Amini194378d2016-03-11 22:19:06 +0000363 IRLinker &TheIRLinker;
Rafael Espindolada513202015-12-10 14:19:35 +0000364
365public:
Mehdi Amini194378d2016-03-11 22:19:06 +0000366 GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
Mehdi Amini055a0c92016-05-25 21:03:21 +0000367 Value *materialize(Value *V) override;
Rafael Espindolada513202015-12-10 14:19:35 +0000368};
369
370class LocalValueMaterializer final : public ValueMaterializer {
Mehdi Amini194378d2016-03-11 22:19:06 +0000371 IRLinker &TheIRLinker;
Rafael Espindolada513202015-12-10 14:19:35 +0000372
373public:
Mehdi Amini194378d2016-03-11 22:19:06 +0000374 LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
Mehdi Amini055a0c92016-05-25 21:03:21 +0000375 Value *materialize(Value *V) override;
Rafael Espindolada513202015-12-10 14:19:35 +0000376};
377
Duncan P. N. Exon Smith034f0ee2016-04-17 23:30:31 +0000378/// Type of the Metadata map in \a ValueToValueMapTy.
379typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
380
Rafael Espindolada513202015-12-10 14:19:35 +0000381/// This is responsible for keeping track of the state used for moving data
382/// from SrcM to DstM.
383class IRLinker {
384 Module &DstM;
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000385 std::unique_ptr<Module> SrcM;
Rafael Espindolada513202015-12-10 14:19:35 +0000386
Mehdi Amini194378d2016-03-11 22:19:06 +0000387 /// See IRMover::move().
Rafael Espindolada513202015-12-10 14:19:35 +0000388 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
389
390 TypeMapTy TypeMap;
391 GlobalValueMaterializer GValMaterializer;
392 LocalValueMaterializer LValMaterializer;
393
Duncan P. N. Exon Smith034f0ee2016-04-17 23:30:31 +0000394 /// A metadata map that's shared between IRLinker instances.
395 MDMapT &SharedMDs;
396
Rafael Espindolada513202015-12-10 14:19:35 +0000397 /// Mapping of values from what they used to be in Src, to what they are now
398 /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
399 /// due to the use of Value handles which the Linker doesn't actually need,
400 /// but this allows us to reuse the ValueMapper code.
401 ValueToValueMapTy ValueMap;
402 ValueToValueMapTy AliasValueMap;
403
404 DenseSet<GlobalValue *> ValuesToLink;
405 std::vector<GlobalValue *> Worklist;
406
407 void maybeAdd(GlobalValue *GV) {
408 if (ValuesToLink.insert(GV).second)
409 Worklist.push_back(GV);
410 }
411
Peter Collingbournea06bb012017-02-03 17:01:14 +0000412 /// Whether we are importing globals for ThinLTO, as opposed to linking the
413 /// source module. If this flag is set, it means that we can rely on some
414 /// other object file to define any non-GlobalValue entities defined by the
415 /// source module. This currently causes us to not link retained types in
416 /// debug info metadata and module inline asm.
417 bool IsPerformingImport;
Teresa Johnson256d6fe2016-10-12 18:39:29 +0000418
Rafael Espindolada513202015-12-10 14:19:35 +0000419 /// Set to true when all global value body linking is complete (including
420 /// lazy linking). Used to prevent metadata linking from creating new
421 /// references.
422 bool DoneLinkingBodies = false;
423
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000424 /// The Error encountered during materialization. We use an Optional here to
425 /// avoid needing to manage an unconsumed success value.
426 Optional<Error> FoundError;
427 void setError(Error E) {
428 if (E)
429 FoundError = std::move(E);
430 }
431
432 /// Most of the errors produced by this module are inconvertible StringErrors.
433 /// This convenience function lets us return one of those more easily.
434 Error stringErr(const Twine &T) {
435 return make_error<StringError>(T, inconvertibleErrorCode());
436 }
Rafael Espindolada513202015-12-10 14:19:35 +0000437
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000438 /// Entry point for mapping values and alternate context for mapping aliases.
439 ValueMapper Mapper;
440 unsigned AliasMCID;
Teresa Johnsond0f8afa2015-12-17 17:14:09 +0000441
Rafael Espindolada513202015-12-10 14:19:35 +0000442 /// Handles cloning of a global values from the source module into
443 /// the destination module, including setting the attributes and visibility.
444 GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
445
Rafael Espindolada513202015-12-10 14:19:35 +0000446 void emitWarning(const Twine &Message) {
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000447 SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
Rafael Espindolada513202015-12-10 14:19:35 +0000448 }
449
450 /// Given a global in the source module, return the global in the
451 /// destination module that is being linked to, if any.
452 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
453 // If the source has no name it can't link. If it has local linkage,
454 // there is no name match-up going on.
455 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
456 return nullptr;
457
458 // Otherwise see if we have a match in the destination module's symtab.
459 GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
460 if (!DGV)
461 return nullptr;
462
463 // If we found a global with the same name in the dest module, but it has
464 // internal linkage, we are really not doing any linkage here.
465 if (DGV->hasLocalLinkage())
466 return nullptr;
467
468 // Otherwise, we do in fact link to the destination global.
469 return DGV;
470 }
471
472 void computeTypeMapping();
473
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000474 Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
475 const GlobalVariable *SrcGV);
Rafael Espindolada513202015-12-10 14:19:35 +0000476
Mehdi Amini194378d2016-03-11 22:19:06 +0000477 /// Given the GlobaValue \p SGV in the source module, and the matching
478 /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
479 /// into the destination module.
480 ///
481 /// Note this code may call the client-provided \p AddLazyFor.
Rafael Espindolada513202015-12-10 14:19:35 +0000482 bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000483 Expected<Constant *> linkGlobalValueProto(GlobalValue *GV, bool ForAlias);
Rafael Espindolada513202015-12-10 14:19:35 +0000484
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000485 Error linkModuleFlagsMetadata();
Rafael Espindolada513202015-12-10 14:19:35 +0000486
Peter Collingbourne5420de32016-09-13 01:12:59 +0000487 void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000488 Error linkFunctionBody(Function &Dst, Function &Src);
Rafael Espindolada513202015-12-10 14:19:35 +0000489 void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000490 Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
Rafael Espindolada513202015-12-10 14:19:35 +0000491
492 /// Functions that take care of cloning a specific global value type
493 /// into the destination module.
494 GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
495 Function *copyFunctionProto(const Function *SF);
496 GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
497
Teresa Johnson23137e52016-12-12 16:09:30 +0000498 /// When importing for ThinLTO, prevent importing of types listed on
499 /// the DICompileUnit that we don't need a copy of in the importing
500 /// module.
501 void prepareCompileUnitsForImport();
Rafael Espindolada513202015-12-10 14:19:35 +0000502 void linkNamedMDNodes();
503
504public:
Duncan P. N. Exon Smith034f0ee2016-04-17 23:30:31 +0000505 IRLinker(Module &DstM, MDMapT &SharedMDs,
506 IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
507 ArrayRef<GlobalValue *> ValuesToLink,
Teresa Johnson256d6fe2016-10-12 18:39:29 +0000508 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
Peter Collingbournea06bb012017-02-03 17:01:14 +0000509 bool IsPerformingImport)
Benjamin Kramer14aae012016-05-27 14:27:24 +0000510 : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
511 TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
Peter Collingbournea06bb012017-02-03 17:01:14 +0000512 SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000513 Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap,
514 &GValMaterializer),
515 AliasMCID(Mapper.registerAlternateMappingContext(AliasValueMap,
516 &LValMaterializer)) {
Duncan P. N. Exon Smith001edd02016-04-19 16:57:24 +0000517 ValueMap.getMDMap() = std::move(SharedMDs);
Rafael Espindolada513202015-12-10 14:19:35 +0000518 for (GlobalValue *GV : ValuesToLink)
519 maybeAdd(GV);
Teresa Johnson23137e52016-12-12 16:09:30 +0000520 if (IsPerformingImport)
521 prepareCompileUnitsForImport();
Teresa Johnsonba0fc202015-12-30 19:32:24 +0000522 }
Duncan P. N. Exon Smith001edd02016-04-19 16:57:24 +0000523 ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
Teresa Johnsonba0fc202015-12-30 19:32:24 +0000524
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000525 Error run();
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000526 Value *materialize(Value *V, bool ForAlias);
Rafael Espindolada513202015-12-10 14:19:35 +0000527};
528}
529
530/// The LLVM SymbolTable class autorenames globals that conflict in the symbol
531/// table. This is good for all clients except for us. Go through the trouble
532/// to force this back.
533static void forceRenaming(GlobalValue *GV, StringRef Name) {
534 // If the global doesn't force its name or if it already has the right name,
535 // there is nothing for us to do.
536 if (GV->hasLocalLinkage() || GV->getName() == Name)
537 return;
538
539 Module *M = GV->getParent();
540
541 // If there is a conflict, rename the conflict.
542 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
543 GV->takeName(ConflictGV);
544 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
545 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
546 } else {
547 GV->setName(Name); // Force the name back
548 }
549}
550
Mehdi Amini055a0c92016-05-25 21:03:21 +0000551Value *GlobalValueMaterializer::materialize(Value *SGV) {
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000552 return TheIRLinker.materialize(SGV, false);
Rafael Espindolada513202015-12-10 14:19:35 +0000553}
554
Mehdi Amini055a0c92016-05-25 21:03:21 +0000555Value *LocalValueMaterializer::materialize(Value *SGV) {
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000556 return TheIRLinker.materialize(SGV, true);
Rafael Espindolada513202015-12-10 14:19:35 +0000557}
558
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000559Value *IRLinker::materialize(Value *V, bool ForAlias) {
Rafael Espindolada513202015-12-10 14:19:35 +0000560 auto *SGV = dyn_cast<GlobalValue>(V);
561 if (!SGV)
562 return nullptr;
563
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000564 Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForAlias);
565 if (!NewProto) {
566 setError(NewProto.takeError());
567 return nullptr;
568 }
569 if (!*NewProto)
570 return nullptr;
Rafael Espindolada513202015-12-10 14:19:35 +0000571
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000572 GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000573 if (!New)
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000574 return *NewProto;
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000575
Rafael Espindolada513202015-12-10 14:19:35 +0000576 // If we already created the body, just return.
577 if (auto *F = dyn_cast<Function>(New)) {
578 if (!F->isDeclaration())
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000579 return New;
Rafael Espindolada513202015-12-10 14:19:35 +0000580 } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
Duncan P. N. Exon Smith31d315e2016-04-17 19:40:20 +0000581 if (V->hasInitializer() || V->hasAppendingLinkage())
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000582 return New;
Rafael Espindolada513202015-12-10 14:19:35 +0000583 } else {
584 auto *A = cast<GlobalAlias>(New);
585 if (A->getAliasee())
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000586 return New;
Rafael Espindolada513202015-12-10 14:19:35 +0000587 }
588
Mehdi Aminif31f6032016-05-25 21:00:44 +0000589 // When linking a global for an alias, it will always be linked. However we
Adrian Prantlb91d4c22016-11-14 17:26:32 +0000590 // need to check if it was not already scheduled to satisfy a reference from a
Mehdi Aminif31f6032016-05-25 21:00:44 +0000591 // regular global value initializer. We know if it has been schedule if the
592 // "New" GlobalValue that is mapped here for the alias is the same as the one
593 // already mapped. If there is an entry in the ValueMap but the value is
594 // different, it means that the value already had a definition in the
595 // destination module (linkonce for instance), but we need a new definition
596 // for the alias ("New" will be different.
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000597 if (ForAlias && ValueMap.lookup(SGV) == New)
598 return New;
Mehdi Aminif31f6032016-05-25 21:00:44 +0000599
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000600 if (ForAlias || shouldLink(New, *SGV))
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000601 setError(linkGlobalValueBody(*New, *SGV));
Mehdi Aminif29dafd2016-05-25 21:01:51 +0000602
603 return New;
Rafael Espindolada513202015-12-10 14:19:35 +0000604}
605
606/// Loop through the global variables in the src module and merge them into the
607/// dest module.
608GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
609 // No linking to be performed or linking from the source: simply create an
610 // identical version of the symbol over in the dest module... the
611 // initializer will be filled in later by LinkGlobalInits.
612 GlobalVariable *NewDGV =
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000613 new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
Rafael Espindolada513202015-12-10 14:19:35 +0000614 SGVar->isConstant(), GlobalValue::ExternalLinkage,
615 /*init*/ nullptr, SGVar->getName(),
616 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
617 SGVar->getType()->getAddressSpace());
618 NewDGV->setAlignment(SGVar->getAlignment());
Reid Klecknerc130a202017-05-11 21:14:29 +0000619 NewDGV->copyAttributesFrom(SGVar);
Rafael Espindolada513202015-12-10 14:19:35 +0000620 return NewDGV;
621}
622
623/// Link the function in the source module into the destination module if
624/// needed, setting up mapping information.
625Function *IRLinker::copyFunctionProto(const Function *SF) {
626 // If there is no linkage to be performed or we are linking from the source,
627 // bring SF over.
Reid Klecknerc130a202017-05-11 21:14:29 +0000628 auto *F =
629 Function::Create(TypeMap.get(SF->getFunctionType()),
630 GlobalValue::ExternalLinkage, SF->getName(), &DstM);
631 F->copyAttributesFrom(SF);
632 return F;
Rafael Espindolada513202015-12-10 14:19:35 +0000633}
634
635/// Set up prototypes for any aliases that come over from the source module.
636GlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
637 // If there is no linkage to be performed or we're linking from the source,
638 // bring over SGA.
639 auto *Ty = TypeMap.get(SGA->getValueType());
Reid Klecknerc130a202017-05-11 21:14:29 +0000640 auto *GA =
641 GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
642 GlobalValue::ExternalLinkage, SGA->getName(), &DstM);
643 GA->copyAttributesFrom(SGA);
644 return GA;
Rafael Espindolada513202015-12-10 14:19:35 +0000645}
646
647GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
648 bool ForDefinition) {
649 GlobalValue *NewGV;
650 if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
651 NewGV = copyGlobalVariableProto(SGVar);
652 } else if (auto *SF = dyn_cast<Function>(SGV)) {
653 NewGV = copyFunctionProto(SF);
654 } else {
655 if (ForDefinition)
656 NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
Peter Collingbourne9e6aa3e2017-08-10 01:07:44 +0000657 else if (SGV->getValueType()->isFunctionTy())
658 NewGV =
659 Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),
660 GlobalValue::ExternalLinkage, SGV->getName(), &DstM);
Rafael Espindolada513202015-12-10 14:19:35 +0000661 else
662 NewGV = new GlobalVariable(
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000663 DstM, TypeMap.get(SGV->getValueType()),
Rafael Espindolada513202015-12-10 14:19:35 +0000664 /*isConstant*/ false, GlobalValue::ExternalLinkage,
665 /*init*/ nullptr, SGV->getName(),
666 /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
667 SGV->getType()->getAddressSpace());
668 }
669
670 if (ForDefinition)
671 NewGV->setLinkage(SGV->getLinkage());
Mehdi Amini5c025fc2016-04-19 16:11:05 +0000672 else if (SGV->hasExternalWeakLinkage())
Rafael Espindolada513202015-12-10 14:19:35 +0000673 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
674
Peter Collingbourne43c51392016-06-24 17:42:21 +0000675 if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
676 // Metadata for global variables and function declarations is copied eagerly.
677 if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
Peter Collingbournedba91462016-06-24 21:21:32 +0000678 NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
Peter Collingbourne43c51392016-06-24 17:42:21 +0000679 }
680
Teresa Johnson132f8672016-01-12 00:24:24 +0000681 // Remove these copied constants in case this stays a declaration, since
682 // they point to the source module. If the def is linked the values will
683 // be mapped in during linkFunctionBody.
684 if (auto *NewF = dyn_cast<Function>(NewGV)) {
685 NewF->setPersonalityFn(nullptr);
686 NewF->setPrefixData(nullptr);
687 NewF->setPrologueData(nullptr);
688 }
689
Rafael Espindolada513202015-12-10 14:19:35 +0000690 return NewGV;
691}
692
Eugene Leviantc8650a42018-02-14 10:32:47 +0000693static StringRef getTypeNamePrefix(StringRef Name) {
694 size_t DotPos = Name.rfind('.');
695 return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
696 !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
697 ? Name
698 : Name.substr(0, DotPos);
699}
700
Rafael Espindolada513202015-12-10 14:19:35 +0000701/// Loop over all of the linked values to compute type mappings. For example,
702/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
703/// types 'Foo' but one got renamed when the module was loaded into the same
704/// LLVMContext.
705void IRLinker::computeTypeMapping() {
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000706 for (GlobalValue &SGV : SrcM->globals()) {
Rafael Espindolada513202015-12-10 14:19:35 +0000707 GlobalValue *DGV = getLinkedToGlobal(&SGV);
708 if (!DGV)
709 continue;
710
711 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
712 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
713 continue;
714 }
715
716 // Unify the element type of appending arrays.
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000717 ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
718 ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
Rafael Espindolada513202015-12-10 14:19:35 +0000719 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
720 }
721
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000722 for (GlobalValue &SGV : *SrcM)
Rafael Espindolada513202015-12-10 14:19:35 +0000723 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
724 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
725
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000726 for (GlobalValue &SGV : SrcM->aliases())
Rafael Espindolada513202015-12-10 14:19:35 +0000727 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
728 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
729
730 // Incorporate types by name, scanning all the types in the source module.
731 // At this point, the destination module may have a type "%foo = { i32 }" for
732 // example. When the source module got loaded into the same LLVMContext, if
733 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
Rafael Espindola2dcb92d2016-02-16 18:50:12 +0000734 std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
Rafael Espindolada513202015-12-10 14:19:35 +0000735 for (StructType *ST : Types) {
736 if (!ST->hasName())
737 continue;
738
Hans Wennborg3e367e62016-11-18 17:33:05 +0000739 if (TypeMap.DstStructTypesSet.hasType(ST)) {
740 // This is actually a type from the destination module.
741 // getIdentifiedStructTypes() can have found it by walking debug info
742 // metadata nodes, some of which get linked by name when ODR Type Uniquing
743 // is enabled on the Context, from the source to the destination module.
744 continue;
745 }
746
Eugene Leviantc8650a42018-02-14 10:32:47 +0000747 auto STTypePrefix = getTypeNamePrefix(ST->getName());
748 if (STTypePrefix.size()== ST->getName().size())
Rafael Espindolada513202015-12-10 14:19:35 +0000749 continue;
750
751 // Check to see if the destination module has a struct with the prefix name.
Eugene Leviantc8650a42018-02-14 10:32:47 +0000752 StructType *DST = DstM.getTypeByName(STTypePrefix);
Rafael Espindolada513202015-12-10 14:19:35 +0000753 if (!DST)
754 continue;
755
756 // Don't use it if this actually came from the source module. They're in
757 // the same LLVMContext after all. Also don't use it unless the type is
758 // actually used in the destination module. This can happen in situations
759 // like this:
760 //
761 // Module A Module B
762 // -------- --------
763 // %Z = type { %A } %B = type { %C.1 }
764 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
765 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
766 // %C = type { i8* } %B.3 = type { %C.1 }
767 //
768 // When we link Module B with Module A, the '%B' in Module B is
769 // used. However, that would then use '%C.1'. But when we process '%C.1',
770 // we prefer to take the '%C' version. So we are then left with both
771 // '%C.1' and '%C' being used for the same types. This leads to some
772 // variables using one type and some using the other.
773 if (TypeMap.DstStructTypesSet.hasType(DST))
774 TypeMap.addTypeMapping(DST, ST);
775 }
776
777 // Now that we have discovered all of the type equivalences, get a body for
778 // any 'opaque' types in the dest module that are now resolved.
779 TypeMap.linkDefinedTypeBodies();
780}
781
782static void getArrayElements(const Constant *C,
783 SmallVectorImpl<Constant *> &Dest) {
784 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
785
786 for (unsigned i = 0; i != NumElements; ++i)
787 Dest.push_back(C->getAggregateElement(i));
788}
789
790/// If there were any appending global variables, link them together now.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000791Expected<Constant *>
792IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
793 const GlobalVariable *SrcGV) {
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000794 Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
Rafael Espindolada513202015-12-10 14:19:35 +0000795 ->getElementType();
796
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000797 // FIXME: This upgrade is done during linking to support the C API. Once the
798 // old form is deprecated, we should move this upgrade to
799 // llvm::UpgradeGlobalVariable() and simplify the logic here and in
800 // Mapper::mapAppendingVariable() in ValueMapper.cpp.
Rafael Espindolada513202015-12-10 14:19:35 +0000801 StringRef Name = SrcGV->getName();
802 bool IsNewStructor = false;
803 bool IsOldStructor = false;
804 if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
805 if (cast<StructType>(EltTy)->getNumElements() == 3)
806 IsNewStructor = true;
807 else
808 IsOldStructor = true;
809 }
810
811 PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
812 if (IsOldStructor) {
813 auto &ST = *cast<StructType>(EltTy);
814 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
815 EltTy = StructType::get(SrcGV->getContext(), Tys, false);
816 }
817
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000818 uint64_t DstNumElements = 0;
Rafael Espindolada513202015-12-10 14:19:35 +0000819 if (DstGV) {
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000820 ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000821 DstNumElements = DstTy->getNumElements();
Rafael Espindolada513202015-12-10 14:19:35 +0000822
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000823 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
824 return stringErr(
Rafael Espindolada513202015-12-10 14:19:35 +0000825 "Linking globals named '" + SrcGV->getName() +
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000826 "': can only link appending global with another appending "
827 "global!");
Rafael Espindolada513202015-12-10 14:19:35 +0000828
829 // Check to see that they two arrays agree on type.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000830 if (EltTy != DstTy->getElementType())
831 return stringErr("Appending variables with different element types!");
832 if (DstGV->isConstant() != SrcGV->isConstant())
833 return stringErr("Appending variables linked with different const'ness!");
Rafael Espindolada513202015-12-10 14:19:35 +0000834
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000835 if (DstGV->getAlignment() != SrcGV->getAlignment())
836 return stringErr(
Rafael Espindolada513202015-12-10 14:19:35 +0000837 "Appending variables with different alignment need to be linked!");
Rafael Espindolada513202015-12-10 14:19:35 +0000838
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000839 if (DstGV->getVisibility() != SrcGV->getVisibility())
840 return stringErr(
Rafael Espindolada513202015-12-10 14:19:35 +0000841 "Appending variables with different visibility need to be linked!");
Rafael Espindolada513202015-12-10 14:19:35 +0000842
Peter Collingbourne63b34cd2016-06-14 21:01:22 +0000843 if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000844 return stringErr(
Rafael Espindolada513202015-12-10 14:19:35 +0000845 "Appending variables with different unnamed_addr need to be linked!");
Rafael Espindolada513202015-12-10 14:19:35 +0000846
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000847 if (DstGV->getSection() != SrcGV->getSection())
848 return stringErr(
Rafael Espindolada513202015-12-10 14:19:35 +0000849 "Appending variables with different section name need to be linked!");
Rafael Espindolada513202015-12-10 14:19:35 +0000850 }
851
Rafael Espindolada513202015-12-10 14:19:35 +0000852 SmallVector<Constant *, 16> SrcElements;
853 getArrayElements(SrcGV->getInitializer(), SrcElements);
854
Justin Bognerd6a48b12016-08-15 22:41:42 +0000855 if (IsNewStructor) {
856 auto It = remove_if(SrcElements, [this](Constant *E) {
857 auto *Key =
858 dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());
859 if (!Key)
860 return false;
861 GlobalValue *DGV = getLinkedToGlobal(Key);
862 return !shouldLink(DGV, *Key);
863 });
864 SrcElements.erase(It, SrcElements.end());
865 }
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000866 uint64_t NewSize = DstNumElements + SrcElements.size();
Rafael Espindolada513202015-12-10 14:19:35 +0000867 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
868
869 // Create the new global variable.
870 GlobalVariable *NG = new GlobalVariable(
871 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
872 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
873 SrcGV->getType()->getAddressSpace());
874
875 NG->copyAttributesFrom(SrcGV);
876 forceRenaming(NG, SrcGV->getName());
877
878 Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
879
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000880 Mapper.scheduleMapAppendingVariable(*NG,
881 DstGV ? DstGV->getInitializer() : nullptr,
882 IsOldStructor, SrcElements);
Rafael Espindolada513202015-12-10 14:19:35 +0000883
884 // Replace any uses of the two global variables with uses of the new
885 // global.
886 if (DstGV) {
887 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
888 DstGV->eraseFromParent();
889 }
890
891 return Ret;
892}
893
Rafael Espindolada513202015-12-10 14:19:35 +0000894bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
Davide Italiano007ac992016-06-07 14:55:04 +0000895 if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())
Rafael Espindolada513202015-12-10 14:19:35 +0000896 return true;
897
Rafael Espindola74569532016-01-20 22:38:23 +0000898 if (DGV && !DGV->isDeclarationForLinker())
Rafael Espindolada513202015-12-10 14:19:35 +0000899 return false;
900
Davide Italiano007ac992016-06-07 14:55:04 +0000901 if (SGV.isDeclaration() || DoneLinkingBodies)
Rafael Espindola8b7f4162016-04-21 14:56:33 +0000902 return false;
Mehdi Amini194378d2016-03-11 22:19:06 +0000903
904 // Callback to the client to give a chance to lazily add the Global to the
905 // list of value to link.
906 bool LazilyAdded = false;
907 AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
908 maybeAdd(&GV);
909 LazilyAdded = true;
910 });
911 return LazilyAdded;
Rafael Espindolada513202015-12-10 14:19:35 +0000912}
913
Peter Collingbourneb7bfc992016-05-27 05:21:35 +0000914Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
915 bool ForAlias) {
Rafael Espindolada513202015-12-10 14:19:35 +0000916 GlobalValue *DGV = getLinkedToGlobal(SGV);
Rafael Espindola2d01b0d2018-02-21 20:12:18 +0000917
Rafael Espindolada513202015-12-10 14:19:35 +0000918 bool ShouldLink = shouldLink(DGV, *SGV);
919
920 // just missing from map
921 if (ShouldLink) {
922 auto I = ValueMap.find(SGV);
923 if (I != ValueMap.end())
924 return cast<Constant>(I->second);
925
926 I = AliasValueMap.find(SGV);
927 if (I != AliasValueMap.end())
928 return cast<Constant>(I->second);
929 }
930
Mehdi Amini194378d2016-03-11 22:19:06 +0000931 if (!ShouldLink && ForAlias)
932 DGV = nullptr;
Rafael Espindolada513202015-12-10 14:19:35 +0000933
934 // Handle the ultra special appending linkage case first.
935 assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
936 if (SGV->hasAppendingLinkage())
937 return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
938 cast<GlobalVariable>(SGV));
939
940 GlobalValue *NewGV;
Rafael Espindola74569532016-01-20 22:38:23 +0000941 if (DGV && !ShouldLink) {
Rafael Espindolada513202015-12-10 14:19:35 +0000942 NewGV = DGV;
943 } else {
944 // If we are done linking global value bodies (i.e. we are performing
945 // metadata linking), don't link in the global value due to this
946 // reference, simply map it to null.
947 if (DoneLinkingBodies)
948 return nullptr;
949
Eli Friedman5119a482018-07-13 21:58:55 +0000950 NewGV = copyGlobalValueProto(SGV, ShouldLink || ForAlias);
Evgeniy Stepanovc97b2652016-01-20 22:05:50 +0000951 if (ShouldLink || !ForAlias)
Rafael Espindolada513202015-12-10 14:19:35 +0000952 forceRenaming(NewGV, SGV->getName());
953 }
Artur Pilipenko140d9e62016-06-24 15:10:29 +0000954
955 // Overloaded intrinsics have overloaded types names as part of their
956 // names. If we renamed overloaded types we should rename the intrinsic
957 // as well.
958 if (Function *F = dyn_cast<Function>(NewGV))
959 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F))
960 NewGV = Remangled.getValue();
961
Rafael Espindolada513202015-12-10 14:19:35 +0000962 if (ShouldLink || ForAlias) {
963 if (const Comdat *SC = SGV->getComdat()) {
964 if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
965 Comdat *DC = DstM.getOrInsertComdat(SC->getName());
966 DC->setSelectionKind(SC->getSelectionKind());
967 GO->setComdat(DC);
968 }
969 }
970 }
971
972 if (!ShouldLink && ForAlias)
973 NewGV->setLinkage(GlobalValue::InternalLinkage);
974
975 Constant *C = NewGV;
Teresa Johnson5e57c182018-01-09 18:32:53 +0000976 // Only create a bitcast if necessary. In particular, with
977 // DebugTypeODRUniquing we may reach metadata in the destination module
978 // containing a GV from the source module, in which case SGV will be
979 // the same as DGV and NewGV, and TypeMap.get() will assert since it
980 // assumes it is being invoked on a type in the source module.
Matt Arsenaulta24408a2018-09-24 04:42:14 +0000981 if (DGV && NewGV != SGV) {
982 C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
983 NewGV, TypeMap.get(SGV->getType()));
984 }
Rafael Espindolada513202015-12-10 14:19:35 +0000985
986 if (DGV && NewGV != DGV) {
Matt Arsenaulta24408a2018-09-24 04:42:14 +0000987 DGV->replaceAllUsesWith(
988 ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType()));
Rafael Espindolada513202015-12-10 14:19:35 +0000989 DGV->eraseFromParent();
990 }
991
992 return C;
993}
994
995/// Update the initializers in the Dest module now that all globals that may be
996/// referenced are in Dest.
Peter Collingbourne5420de32016-09-13 01:12:59 +0000997void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
Rafael Espindolada513202015-12-10 14:19:35 +0000998 // Figure out what the initializer looks like in the dest module.
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +0000999 Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
Rafael Espindolada513202015-12-10 14:19:35 +00001000}
1001
1002/// Copy the source function over into the dest function and fix up references
1003/// to values. At this point we know that Dest is an external function, and
1004/// that Src is not.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001005Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
Rafael Espindolada513202015-12-10 14:19:35 +00001006 assert(Dst.isDeclaration() && !Src.isDeclaration());
1007
1008 // Materialize if needed.
Peter Collingbourne76c218e2016-11-09 17:49:19 +00001009 if (Error Err = Src.materialize())
1010 return Err;
Rafael Espindolada513202015-12-10 14:19:35 +00001011
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001012 // Link in the operands without remapping.
Rafael Espindolada513202015-12-10 14:19:35 +00001013 if (Src.hasPrefixData())
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001014 Dst.setPrefixData(Src.getPrefixData());
Rafael Espindolada513202015-12-10 14:19:35 +00001015 if (Src.hasPrologueData())
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001016 Dst.setPrologueData(Src.getPrologueData());
Rafael Espindolada513202015-12-10 14:19:35 +00001017 if (Src.hasPersonalityFn())
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001018 Dst.setPersonalityFn(Src.getPersonalityFn());
Rafael Espindolada513202015-12-10 14:19:35 +00001019
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001020 // Copy over the metadata attachments without remapping.
Peter Collingbournedba91462016-06-24 21:21:32 +00001021 Dst.copyMetadata(&Src, 0);
Rafael Espindolada513202015-12-10 14:19:35 +00001022
Duncan P. N. Exon Smithe49dfa72016-04-06 06:38:15 +00001023 // Steal arguments and splice the body of Src into Dst.
1024 Dst.stealArgumentListFrom(Src);
Rafael Espindolada513202015-12-10 14:19:35 +00001025 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1026
Duncan P. N. Exon Smith287cd842016-04-08 19:26:32 +00001027 // Everything has been moved over. Remap it.
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +00001028 Mapper.scheduleRemapFunction(Dst);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001029 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001030}
1031
1032void IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +00001033 Mapper.scheduleMapGlobalAliasee(Dst, *Src.getAliasee(), AliasMCID);
Rafael Espindolada513202015-12-10 14:19:35 +00001034}
1035
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001036Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
Rafael Espindolada513202015-12-10 14:19:35 +00001037 if (auto *F = dyn_cast<Function>(&Src))
1038 return linkFunctionBody(cast<Function>(Dst), *F);
1039 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
Peter Collingbourne5420de32016-09-13 01:12:59 +00001040 linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001041 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001042 }
1043 linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001044 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001045}
1046
Teresa Johnson23137e52016-12-12 16:09:30 +00001047void IRLinker::prepareCompileUnitsForImport() {
1048 NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
1049 if (!SrcCompileUnits)
1050 return;
1051 // When importing for ThinLTO, prevent importing of types listed on
1052 // the DICompileUnit that we don't need a copy of in the importing
1053 // module. They will be emitted by the originating module.
1054 for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); I != E; ++I) {
1055 auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I));
1056 assert(CU && "Expected valid compile unit");
1057 // Enums, macros, and retained types don't need to be listed on the
1058 // imported DICompileUnit. This means they will only be imported
1059 // if reached from the mapped IR. Do this by setting their value map
1060 // entries to nullptr, which will automatically prevent their importing
1061 // when reached from the DICompileUnit during metadata mapping.
1062 ValueMap.MD()[CU->getRawEnumTypes()].reset(nullptr);
1063 ValueMap.MD()[CU->getRawMacros()].reset(nullptr);
1064 ValueMap.MD()[CU->getRawRetainedTypes()].reset(nullptr);
David Blaikie08bc3d82018-12-05 21:42:17 +00001065 // The original definition (or at least its debug info - if the variable is
1066 // internalized an optimized away) will remain in the source module, so
1067 // there's no need to import them.
1068 // If LLVM ever does more advanced optimizations on global variables
1069 // (removing/localizing write operations, for instance) that can track
1070 // through debug info, this decision may need to be revisited - but do so
1071 // with care when it comes to debug info size. Emitting small CUs containing
1072 // only a few imported entities into every destination module may be very
1073 // size inefficient.
1074 ValueMap.MD()[CU->getRawGlobalVariables()].reset(nullptr);
Teresa Johnson23137e52016-12-12 16:09:30 +00001075
1076 // Imported entities only need to be mapped in if they have local
1077 // scope, as those might correspond to an imported entity inside a
1078 // function being imported (any locally scoped imported entities that
1079 // don't end up referenced by an imported function will not be emitted
1080 // into the object). Imported entities not in a local scope
1081 // (e.g. on the namespace) only need to be emitted by the originating
1082 // module. Create a list of the locally scoped imported entities, and
1083 // replace the source CUs imported entity list with the new list, so
1084 // only those are mapped in.
1085 // FIXME: Locally-scoped imported entities could be moved to the
1086 // functions they are local to instead of listing them on the CU, and
1087 // we would naturally only link in those needed by function importing.
1088 SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
1089 bool ReplaceImportedEntities = false;
1090 for (auto *IE : CU->getImportedEntities()) {
1091 DIScope *Scope = IE->getScope();
1092 assert(Scope && "Invalid Scope encoding!");
1093 if (isa<DILocalScope>(Scope))
1094 AllImportedModules.emplace_back(IE);
1095 else
1096 ReplaceImportedEntities = true;
1097 }
1098 if (ReplaceImportedEntities) {
1099 if (!AllImportedModules.empty())
1100 CU->replaceImportedEntities(MDTuple::get(
1101 CU->getContext(),
1102 SmallVector<Metadata *, 16>(AllImportedModules.begin(),
1103 AllImportedModules.end())));
1104 else
1105 // If there were no local scope imported entities, we can map
1106 // the whole list to nullptr.
1107 ValueMap.MD()[CU->getRawImportedEntities()].reset(nullptr);
1108 }
1109 }
1110}
1111
Rafael Espindolada513202015-12-10 14:19:35 +00001112/// Insert all of the named MDNodes in Src into the Dest module.
1113void IRLinker::linkNamedMDNodes() {
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001114 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1115 for (const NamedMDNode &NMD : SrcM->named_metadata()) {
Rafael Espindolada513202015-12-10 14:19:35 +00001116 // Don't link module flags here. Do them separately.
1117 if (&NMD == SrcModFlags)
1118 continue;
1119 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1120 // Add Src elements into Dest node.
Duncan P. N. Exon Smithfef9f1f2016-04-15 23:32:44 +00001121 for (const MDNode *Op : NMD.operands())
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +00001122 DestNMD->addOperand(Mapper.mapMDNode(*Op));
Rafael Espindolada513202015-12-10 14:19:35 +00001123 }
1124}
1125
1126/// Merge the linker flags in Src into the Dest module.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001127Error IRLinker::linkModuleFlagsMetadata() {
Rafael Espindolada513202015-12-10 14:19:35 +00001128 // If the source module has no module flags, we are done.
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001129 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
Rafael Espindolada513202015-12-10 14:19:35 +00001130 if (!SrcModFlags)
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001131 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001132
1133 // If the destination module doesn't have module flags yet, then just copy
1134 // over the source module's flags.
1135 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1136 if (DstModFlags->getNumOperands() == 0) {
1137 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1138 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1139
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001140 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001141 }
1142
1143 // First build a map of the existing module flags and requirements.
1144 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1145 SmallSetVector<MDNode *, 16> Requirements;
1146 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1147 MDNode *Op = DstModFlags->getOperand(I);
1148 ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1149 MDString *ID = cast<MDString>(Op->getOperand(1));
1150
1151 if (Behavior->getZExtValue() == Module::Require) {
1152 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1153 } else {
1154 Flags[ID] = std::make_pair(Op, I);
1155 }
1156 }
1157
1158 // Merge in the flags from the source module, and also collect its set of
1159 // requirements.
1160 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1161 MDNode *SrcOp = SrcModFlags->getOperand(I);
1162 ConstantInt *SrcBehavior =
1163 mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1164 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1165 MDNode *DstOp;
1166 unsigned DstIndex;
1167 std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1168 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1169
1170 // If this is a requirement, add it and continue.
1171 if (SrcBehaviorValue == Module::Require) {
1172 // If the destination module does not already have this requirement, add
1173 // it.
1174 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1175 DstModFlags->addOperand(SrcOp);
1176 }
1177 continue;
1178 }
1179
1180 // If there is no existing flag with this ID, just add it.
1181 if (!DstOp) {
1182 Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1183 DstModFlags->addOperand(SrcOp);
1184 continue;
1185 }
1186
1187 // Otherwise, perform a merge.
1188 ConstantInt *DstBehavior =
1189 mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1190 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1191
Teresa Johnson1fd5bb92017-05-23 00:08:00 +00001192 auto overrideDstValue = [&]() {
1193 DstModFlags->setOperand(DstIndex, SrcOp);
1194 Flags[ID].first = SrcOp;
1195 };
1196
Rafael Espindolada513202015-12-10 14:19:35 +00001197 // If either flag has override behavior, handle it first.
1198 if (DstBehaviorValue == Module::Override) {
1199 // Diagnose inconsistent flags which both have override behavior.
1200 if (SrcBehaviorValue == Module::Override &&
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001201 SrcOp->getOperand(2) != DstOp->getOperand(2))
1202 return stringErr("linking module flags '" + ID->getString() +
1203 "': IDs have conflicting override values");
Rafael Espindolada513202015-12-10 14:19:35 +00001204 continue;
1205 } else if (SrcBehaviorValue == Module::Override) {
1206 // Update the destination flag to that of the source.
Teresa Johnson1fd5bb92017-05-23 00:08:00 +00001207 overrideDstValue();
Rafael Espindolada513202015-12-10 14:19:35 +00001208 continue;
1209 }
1210
1211 // Diagnose inconsistent merge behavior types.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001212 if (SrcBehaviorValue != DstBehaviorValue)
1213 return stringErr("linking module flags '" + ID->getString() +
1214 "': IDs have conflicting behaviors");
Rafael Espindolada513202015-12-10 14:19:35 +00001215
1216 auto replaceDstValue = [&](MDNode *New) {
1217 Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1218 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1219 DstModFlags->setOperand(DstIndex, Flag);
1220 Flags[ID].first = Flag;
1221 };
1222
1223 // Perform the merge for standard behavior types.
1224 switch (SrcBehaviorValue) {
1225 case Module::Require:
1226 case Module::Override:
1227 llvm_unreachable("not possible");
1228 case Module::Error: {
1229 // Emit an error if the values differ.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001230 if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1231 return stringErr("linking module flags '" + ID->getString() +
1232 "': IDs have conflicting values");
Rafael Espindolada513202015-12-10 14:19:35 +00001233 continue;
1234 }
1235 case Module::Warning: {
1236 // Emit a warning if the values differ.
1237 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
David Blaikieaba277a2018-10-09 01:17:27 +00001238 std::string str;
1239 raw_string_ostream(str)
1240 << "linking module flags '" << ID->getString()
1241 << "': IDs have conflicting values ('" << *SrcOp->getOperand(2)
1242 << "' from " << SrcM->getModuleIdentifier() << " with '"
1243 << *DstOp->getOperand(2) << "' from " << DstM.getModuleIdentifier()
1244 << ')';
1245 emitWarning(str);
Rafael Espindolada513202015-12-10 14:19:35 +00001246 }
1247 continue;
1248 }
Teresa Johnson1fd5bb92017-05-23 00:08:00 +00001249 case Module::Max: {
1250 ConstantInt *DstValue =
1251 mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1252 ConstantInt *SrcValue =
1253 mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1254 if (SrcValue->getZExtValue() > DstValue->getZExtValue())
1255 overrideDstValue();
1256 break;
1257 }
Rafael Espindolada513202015-12-10 14:19:35 +00001258 case Module::Append: {
1259 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1260 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1261 SmallVector<Metadata *, 8> MDs;
1262 MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1263 MDs.append(DstValue->op_begin(), DstValue->op_end());
1264 MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1265
1266 replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1267 break;
1268 }
1269 case Module::AppendUnique: {
1270 SmallSetVector<Metadata *, 16> Elts;
1271 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1272 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1273 Elts.insert(DstValue->op_begin(), DstValue->op_end());
1274 Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1275
1276 replaceDstValue(MDNode::get(DstM.getContext(),
1277 makeArrayRef(Elts.begin(), Elts.end())));
1278 break;
1279 }
1280 }
1281 }
1282
1283 // Check all of the requirements.
1284 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1285 MDNode *Requirement = Requirements[I];
1286 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1287 Metadata *ReqValue = Requirement->getOperand(1);
1288
1289 MDNode *Op = Flags[Flag].first;
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001290 if (!Op || Op->getOperand(2) != ReqValue)
1291 return stringErr("linking module flags '" + Flag->getString() +
1292 "': does not have the required value");
Rafael Espindolada513202015-12-10 14:19:35 +00001293 }
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001294 return Error::success();
Rafael Espindolada513202015-12-10 14:19:35 +00001295}
1296
Florian Hahndba40dd2017-07-12 11:52:28 +00001297/// Return InlineAsm adjusted with target-specific directives if required.
1298/// For ARM and Thumb, we have to add directives to select the appropriate ISA
1299/// to support mixing module-level inline assembly from ARM and Thumb modules.
1300static std::string adjustInlineAsm(const std::string &InlineAsm,
1301 const Triple &Triple) {
1302 if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb)
1303 return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1304 if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb)
1305 return ".text\n.balign 4\n.arm\n" + InlineAsm;
1306 return InlineAsm;
1307}
1308
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001309Error IRLinker::run() {
Teresa Johnsonb738b282016-03-10 18:47:03 +00001310 // Ensure metadata materialized before value mapping.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001311 if (SrcM->getMaterializer())
Peter Collingbourne76c218e2016-11-09 17:49:19 +00001312 if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1313 return Err;
Teresa Johnsonb738b282016-03-10 18:47:03 +00001314
Rafael Espindolada513202015-12-10 14:19:35 +00001315 // Inherit the target data from the source module if the destination module
1316 // doesn't have one already.
1317 if (DstM.getDataLayout().isDefault())
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001318 DstM.setDataLayout(SrcM->getDataLayout());
Rafael Espindolada513202015-12-10 14:19:35 +00001319
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001320 if (SrcM->getDataLayout() != DstM.getDataLayout()) {
Rafael Espindolada513202015-12-10 14:19:35 +00001321 emitWarning("Linking two modules of different data layouts: '" +
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001322 SrcM->getModuleIdentifier() + "' is '" +
1323 SrcM->getDataLayoutStr() + "' whereas '" +
Rafael Espindolada513202015-12-10 14:19:35 +00001324 DstM.getModuleIdentifier() + "' is '" +
1325 DstM.getDataLayoutStr() + "'\n");
1326 }
1327
1328 // Copy the target triple from the source to dest if the dest's is empty.
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001329 if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1330 DstM.setTargetTriple(SrcM->getTargetTriple());
Rafael Espindolada513202015-12-10 14:19:35 +00001331
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001332 Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
Rafael Espindolada513202015-12-10 14:19:35 +00001333
Akira Hatanaka343e5352017-05-18 03:52:29 +00001334 if (!SrcM->getTargetTriple().empty()&&
1335 !SrcTriple.isCompatibleWith(DstTriple))
Rafael Espindolada513202015-12-10 14:19:35 +00001336 emitWarning("Linking two modules of different target triples: " +
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001337 SrcM->getModuleIdentifier() + "' is '" +
1338 SrcM->getTargetTriple() + "' whereas '" +
1339 DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1340 "'\n");
Rafael Espindolada513202015-12-10 14:19:35 +00001341
Akira Hatanaka343e5352017-05-18 03:52:29 +00001342 DstM.setTargetTriple(SrcTriple.merge(DstTriple));
Rafael Espindolada513202015-12-10 14:19:35 +00001343
1344 // Append the module inline asm string.
Peter Collingbournea06bb012017-02-03 17:01:14 +00001345 if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
Florian Hahndba40dd2017-07-12 11:52:28 +00001346 std::string SrcModuleInlineAsm = adjustInlineAsm(SrcM->getModuleInlineAsm(),
1347 SrcTriple);
Rafael Espindolada513202015-12-10 14:19:35 +00001348 if (DstM.getModuleInlineAsm().empty())
Florian Hahndba40dd2017-07-12 11:52:28 +00001349 DstM.setModuleInlineAsm(SrcModuleInlineAsm);
Rafael Espindolada513202015-12-10 14:19:35 +00001350 else
1351 DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
Florian Hahndba40dd2017-07-12 11:52:28 +00001352 SrcModuleInlineAsm);
Rafael Espindolada513202015-12-10 14:19:35 +00001353 }
1354
1355 // Loop over all of the linked values to compute type mappings.
1356 computeTypeMapping();
1357
1358 std::reverse(Worklist.begin(), Worklist.end());
1359 while (!Worklist.empty()) {
1360 GlobalValue *GV = Worklist.back();
1361 Worklist.pop_back();
1362
1363 // Already mapped.
1364 if (ValueMap.find(GV) != ValueMap.end() ||
1365 AliasValueMap.find(GV) != AliasValueMap.end())
1366 continue;
1367
1368 assert(!GV->isDeclaration());
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +00001369 Mapper.mapValue(*GV);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001370 if (FoundError)
1371 return std::move(*FoundError);
Rafael Espindolada513202015-12-10 14:19:35 +00001372 }
1373
1374 // Note that we are done linking global value bodies. This prevents
1375 // metadata linking from creating new references.
1376 DoneLinkingBodies = true;
Duncan P. N. Exon Smith1f036192016-04-16 02:29:55 +00001377 Mapper.addFlags(RF_NullMapMissingGlobalValues);
Rafael Espindolada513202015-12-10 14:19:35 +00001378
1379 // Remap all of the named MDNodes in Src into the DstM module. We do this
1380 // after linking GlobalValues so that MDNodes that reference GlobalValues
1381 // are properly remapped.
Teresa Johnson40275202016-03-29 18:24:19 +00001382 linkNamedMDNodes();
Rafael Espindolada513202015-12-10 14:19:35 +00001383
Teresa Johnson40275202016-03-29 18:24:19 +00001384 // Merge the module flags into the DstM module.
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001385 return linkModuleFlagsMetadata();
Rafael Espindolada513202015-12-10 14:19:35 +00001386}
1387
1388IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1389 : ETypes(E), IsPacked(P) {}
1390
1391IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1392 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1393
1394bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
Davide Italiano007ac992016-06-07 14:55:04 +00001395 return IsPacked == That.IsPacked && ETypes == That.ETypes;
Rafael Espindolada513202015-12-10 14:19:35 +00001396}
1397
1398bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1399 return !this->operator==(That);
1400}
1401
1402StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1403 return DenseMapInfo<StructType *>::getEmptyKey();
1404}
1405
1406StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1407 return DenseMapInfo<StructType *>::getTombstoneKey();
1408}
1409
1410unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1411 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1412 Key.IsPacked);
1413}
1414
1415unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1416 return getHashValue(KeyTy(ST));
1417}
1418
1419bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1420 const StructType *RHS) {
1421 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1422 return false;
1423 return LHS == KeyTy(RHS);
1424}
1425
1426bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1427 const StructType *RHS) {
Davide Italiano007ac992016-06-07 14:55:04 +00001428 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1429 return LHS == RHS;
Rafael Espindolada513202015-12-10 14:19:35 +00001430 return KeyTy(LHS) == KeyTy(RHS);
1431}
1432
1433void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1434 assert(!Ty->isOpaque());
1435 NonOpaqueStructTypes.insert(Ty);
1436}
1437
1438void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
Rafael Espindola2d01b0d2018-02-21 20:12:18 +00001439 assert(!Ty->isOpaque());
1440 NonOpaqueStructTypes.insert(Ty);
Rafael Espindolada513202015-12-10 14:19:35 +00001441 bool Removed = OpaqueStructTypes.erase(Ty);
1442 (void)Removed;
1443 assert(Removed);
1444}
1445
1446void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1447 assert(Ty->isOpaque());
1448 OpaqueStructTypes.insert(Ty);
1449}
1450
1451StructType *
1452IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
Rafael Espindola2d01b0d2018-02-21 20:12:18 +00001453 bool IsPacked) {
Rafael Espindolada513202015-12-10 14:19:35 +00001454 IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1455 auto I = NonOpaqueStructTypes.find_as(Key);
Rafael Espindola2d01b0d2018-02-21 20:12:18 +00001456 return I == NonOpaqueStructTypes.end() ? nullptr : *I;
Rafael Espindolada513202015-12-10 14:19:35 +00001457}
1458
1459bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1460 if (Ty->isOpaque())
1461 return OpaqueStructTypes.count(Ty);
1462 auto I = NonOpaqueStructTypes.find(Ty);
Davide Italiano007ac992016-06-07 14:55:04 +00001463 return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
Rafael Espindolada513202015-12-10 14:19:35 +00001464}
1465
Rafael Espindola7a1fc2d2015-12-14 23:17:03 +00001466IRMover::IRMover(Module &M) : Composite(M) {
Rafael Espindolada513202015-12-10 14:19:35 +00001467 TypeFinder StructTypes;
Mehdi Aminic695a0b2016-11-19 18:44:16 +00001468 StructTypes.run(M, /* OnlyNamed */ false);
Rafael Espindolada513202015-12-10 14:19:35 +00001469 for (StructType *Ty : StructTypes) {
1470 if (Ty->isOpaque())
1471 IdentifiedStructTypes.addOpaque(Ty);
1472 else
1473 IdentifiedStructTypes.addNonOpaque(Ty);
1474 }
Mehdi Amini1c224c02016-09-03 21:12:33 +00001475 // Self-map metadatas in the destination module. This is needed when
1476 // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1477 // destination module may be reached from the source module.
1478 for (auto *MD : StructTypes.getVisitedMetadata()) {
1479 SharedMDs[MD].reset(const_cast<MDNode *>(MD));
1480 }
Rafael Espindolada513202015-12-10 14:19:35 +00001481}
1482
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001483Error IRMover::move(
Rafael Espindola2dcb92d2016-02-16 18:50:12 +00001484 std::unique_ptr<Module> Src, ArrayRef<GlobalValue *> ValuesToLink,
Teresa Johnson256d6fe2016-10-12 18:39:29 +00001485 std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
Peter Collingbournea06bb012017-02-03 17:01:14 +00001486 bool IsPerformingImport) {
Duncan P. N. Exon Smith034f0ee2016-04-17 23:30:31 +00001487 IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
Teresa Johnson256d6fe2016-10-12 18:39:29 +00001488 std::move(Src), ValuesToLink, std::move(AddLazyFor),
Peter Collingbournea06bb012017-02-03 17:01:14 +00001489 IsPerformingImport);
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001490 Error E = TheIRLinker.run();
Rafael Espindolada513202015-12-10 14:19:35 +00001491 Composite.dropTriviallyDeadConstantArrays();
Peter Collingbourneb7bfc992016-05-27 05:21:35 +00001492 return E;
Rafael Espindolada513202015-12-10 14:19:35 +00001493}