159a5afa9SMikhail Glushenkov //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
2361e513dSReid Spencer //
3361e513dSReid Spencer //                     The LLVM Compiler Infrastructure
4361e513dSReid Spencer //
5f3ebc3f3SChris Lattner // This file is distributed under the University of Illinois Open Source
6f3ebc3f3SChris Lattner // License. See LICENSE.TXT for details.
7361e513dSReid Spencer //
8361e513dSReid Spencer //===----------------------------------------------------------------------===//
9361e513dSReid Spencer //
10361e513dSReid Spencer // This file implements the LLVM module linker.
11361e513dSReid Spencer //
12361e513dSReid Spencer //===----------------------------------------------------------------------===//
13361e513dSReid Spencer 
146cc07df4SChandler Carruth #include "llvm/Linker/Linker.h"
15ed0881b2SChandler Carruth #include "llvm-c/Linker.h"
1631ad468dSRafael Espindola #include "llvm/ADT/Hashing.h"
1723f8d64bSRafael Espindola #include "llvm/ADT/Optional.h"
1866f02413SBill Wendling #include "llvm/ADT/SetVector.h"
190f7fd36fSEli Bendersky #include "llvm/ADT/SmallString.h"
2031ad468dSRafael Espindola #include "llvm/ADT/Statistic.h"
21c43df518SAkira Hatanaka #include "llvm/ADT/Triple.h"
229fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
23fda0cee7SDuncan P. N. Exon Smith #include "llvm/IR/DebugInfo.h"
24d12b4a33SRafael Espindola #include "llvm/IR/DiagnosticInfo.h"
25d12b4a33SRafael Espindola #include "llvm/IR/DiagnosticPrinter.h"
26d12b4a33SRafael Espindola #include "llvm/IR/LLVMContext.h"
279fb823bbSChandler Carruth #include "llvm/IR/Module.h"
28dcb603feSChandler Carruth #include "llvm/IR/TypeFinder.h"
29e17f3708SEli Bendersky #include "llvm/Support/CommandLine.h"
30b6af2f36SBill Wendling #include "llvm/Support/Debug.h"
31b6af2f36SBill Wendling #include "llvm/Support/raw_ostream.h"
32cbb91408STanya Lattner #include "llvm/Transforms/Utils/Cloning.h"
33981af002SWill Dietz #include <cctype>
3482d6ff6bSDavid Majnemer #include <tuple>
35361e513dSReid Spencer using namespace llvm;
36361e513dSReid Spencer 
37e17f3708SEli Bendersky 
38b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
39b1ed91f3SChris Lattner // TypeMap implementation.
40b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
41b1ed91f3SChris Lattner 
42b1ed91f3SChris Lattner namespace {
43b1ed91f3SChris Lattner class TypeMapTy : public ValueMapTypeRemapper {
4418c89411SRafael Espindola   /// This is a mapping from a source type to a destination type to use.
45b1ed91f3SChris Lattner   DenseMap<Type*, Type*> MappedTypes;
46b1ed91f3SChris Lattner 
4718c89411SRafael Espindola   /// When checking to see if two subgraphs are isomorphic, we speculatively
4818c89411SRafael Espindola   /// add types to MappedTypes, but keep track of them here in case we need to
4918c89411SRafael Espindola   /// roll back.
50b1ed91f3SChris Lattner   SmallVector<Type*, 16> SpeculativeTypes;
51b1ed91f3SChris Lattner 
52a96f235cSRafael Espindola   SmallVector<StructType*, 16> SpeculativeDstOpaqueTypes;
53a96f235cSRafael Espindola 
5418c89411SRafael Espindola   /// This is a list of non-opaque structs in the source module that are mapped
5518c89411SRafael Espindola   /// to an opaque struct in the destination module.
565e3bd972SChris Lattner   SmallVector<StructType*, 16> SrcDefinitionsToResolve;
575e3bd972SChris Lattner 
5818c89411SRafael Espindola   /// This is the set of opaque types in the destination modules who are
5918c89411SRafael Espindola   /// getting a body from the source module.
605e3bd972SChris Lattner   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
61d48b7783SBill Wendling 
628c2cc416SBill Wendling public:
6331ad468dSRafael Espindola   TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
6431ad468dSRafael Espindola       : DstStructTypesSet(DstStructTypesSet) {}
65aa9918aaSRafael Espindola 
6631ad468dSRafael Espindola   Linker::IdentifiedStructTypeSet &DstStructTypesSet;
6718c89411SRafael Espindola   /// Indicate that the specified type in the destination module is conceptually
6818c89411SRafael Espindola   /// equivalent to the specified type in the source module.
69b1ed91f3SChris Lattner   void addTypeMapping(Type *DstTy, Type *SrcTy);
70b1ed91f3SChris Lattner 
71d2a13a2eSRafael Espindola   /// Produce a body for an opaque type in the dest module from a type
72d2a13a2eSRafael Espindola   /// definition in the source module.
73b1ed91f3SChris Lattner   void linkDefinedTypeBodies();
74b1ed91f3SChris Lattner 
7518c89411SRafael Espindola   /// Return the mapped type to use for the specified input type from the
76b1ed91f3SChris Lattner   /// source module.
77b1ed91f3SChris Lattner   Type *get(Type *SrcTy);
7831ad468dSRafael Espindola   Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
7931ad468dSRafael Espindola 
8031ad468dSRafael Espindola   void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
81b1ed91f3SChris Lattner 
82290e2cc5SRafael Espindola   FunctionType *get(FunctionType *T) {
83290e2cc5SRafael Espindola     return cast<FunctionType>(get((Type *)T));
84290e2cc5SRafael Espindola   }
85b1ed91f3SChris Lattner 
8618c89411SRafael Espindola   /// Dump out the type map for debugging purposes.
87b6af2f36SBill Wendling   void dump() const {
888f14471eSRafael Espindola     for (auto &Pair : MappedTypes) {
89b6af2f36SBill Wendling       dbgs() << "TypeMap: ";
908f14471eSRafael Espindola       Pair.first->print(dbgs());
91b6af2f36SBill Wendling       dbgs() << " => ";
928f14471eSRafael Espindola       Pair.second->print(dbgs());
93b6af2f36SBill Wendling       dbgs() << '\n';
94b6af2f36SBill Wendling     }
95b6af2f36SBill Wendling   }
96b6af2f36SBill Wendling 
97b1ed91f3SChris Lattner private:
98290e2cc5SRafael Espindola   Type *remapType(Type *SrcTy) override { return get(SrcTy); }
99b1ed91f3SChris Lattner 
100b1ed91f3SChris Lattner   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
101b1ed91f3SChris Lattner };
102f00654e3SAlexander Kornienko }
103b1ed91f3SChris Lattner 
104b1ed91f3SChris Lattner void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
1053d097410SRafael Espindola   assert(SpeculativeTypes.empty());
106a96f235cSRafael Espindola   assert(SpeculativeDstOpaqueTypes.empty());
1073d097410SRafael Espindola 
108b1ed91f3SChris Lattner   // Check to see if these types are recursively isomorphic and establish a
109b1ed91f3SChris Lattner   // mapping between them if so.
110c586eaa1SDuncan P. N. Exon Smith   if (!areTypesIsomorphic(DstTy, SrcTy)) {
111b1ed91f3SChris Lattner     // Oops, they aren't isomorphic.  Just discard this request by rolling out
112b1ed91f3SChris Lattner     // any speculative mappings we've established.
1133d097410SRafael Espindola     for (Type *Ty : SpeculativeTypes)
1143d097410SRafael Espindola       MappedTypes.erase(Ty);
115a96f235cSRafael Espindola 
116a96f235cSRafael Espindola     SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
117a96f235cSRafael Espindola                                    SpeculativeDstOpaqueTypes.size());
118a96f235cSRafael Espindola     for (StructType *Ty : SpeculativeDstOpaqueTypes)
119a96f235cSRafael Espindola       DstResolvedOpaqueTypes.erase(Ty);
12004a74af7SRafael Espindola   } else {
12104a74af7SRafael Espindola     for (Type *Ty : SpeculativeTypes)
12204a74af7SRafael Espindola       if (auto *STy = dyn_cast<StructType>(Ty))
12304a74af7SRafael Espindola         if (STy->hasName())
12404a74af7SRafael Espindola           STy->setName("");
125d48b7783SBill Wendling   }
126b1ed91f3SChris Lattner   SpeculativeTypes.clear();
127a96f235cSRafael Espindola   SpeculativeDstOpaqueTypes.clear();
128b1ed91f3SChris Lattner }
129b1ed91f3SChris Lattner 
13018c89411SRafael Espindola /// Recursively walk this pair of types, returning true if they are isomorphic,
13118c89411SRafael Espindola /// false if they are not.
132b1ed91f3SChris Lattner bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
133b1ed91f3SChris Lattner   // Two types with differing kinds are clearly not isomorphic.
134290e2cc5SRafael Espindola   if (DstTy->getTypeID() != SrcTy->getTypeID())
135290e2cc5SRafael Espindola     return false;
136b1ed91f3SChris Lattner 
137b1ed91f3SChris Lattner   // If we have an entry in the MappedTypes table, then we have our answer.
138b1ed91f3SChris Lattner   Type *&Entry = MappedTypes[SrcTy];
139b1ed91f3SChris Lattner   if (Entry)
140b1ed91f3SChris Lattner     return Entry == DstTy;
141b1ed91f3SChris Lattner 
142b1ed91f3SChris Lattner   // Two identical types are clearly isomorphic.  Remember this
143b1ed91f3SChris Lattner   // non-speculatively.
144b1ed91f3SChris Lattner   if (DstTy == SrcTy) {
145b1ed91f3SChris Lattner     Entry = DstTy;
146361e513dSReid Spencer     return true;
147361e513dSReid Spencer   }
148361e513dSReid Spencer 
149b1ed91f3SChris Lattner   // Okay, we have two types with identical kinds that we haven't seen before.
150361e513dSReid Spencer 
151b1ed91f3SChris Lattner   // If this is an opaque struct type, special case it.
152b1ed91f3SChris Lattner   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
153b1ed91f3SChris Lattner     // Mapping an opaque type to any struct, just keep the dest struct.
154b1ed91f3SChris Lattner     if (SSTy->isOpaque()) {
155b1ed91f3SChris Lattner       Entry = DstTy;
156b1ed91f3SChris Lattner       SpeculativeTypes.push_back(SrcTy);
157b1ed91f3SChris Lattner       return true;
158361e513dSReid Spencer     }
159b1ed91f3SChris Lattner 
1605e3bd972SChris Lattner     // Mapping a non-opaque source type to an opaque dest.  If this is the first
1615e3bd972SChris Lattner     // type that we're mapping onto this destination type then we succeed.  Keep
162a96f235cSRafael Espindola     // the dest, but fill it in later. If this is the second (different) type
163a96f235cSRafael Espindola     // that we're trying to map onto the same opaque type then we fail.
164b1ed91f3SChris Lattner     if (cast<StructType>(DstTy)->isOpaque()) {
1655e3bd972SChris Lattner       // We can only map one source type onto the opaque destination type.
16670573dcdSDavid Blaikie       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
1675e3bd972SChris Lattner         return false;
1685e3bd972SChris Lattner       SrcDefinitionsToResolve.push_back(SSTy);
169a96f235cSRafael Espindola       SpeculativeTypes.push_back(SrcTy);
170a96f235cSRafael Espindola       SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
171b1ed91f3SChris Lattner       Entry = DstTy;
172b1ed91f3SChris Lattner       return true;
173b1ed91f3SChris Lattner     }
174b1ed91f3SChris Lattner   }
175b1ed91f3SChris Lattner 
176b1ed91f3SChris Lattner   // If the number of subtypes disagree between the two types, then we fail.
177b1ed91f3SChris Lattner   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
178b1ed91f3SChris Lattner     return false;
179b1ed91f3SChris Lattner 
180b1ed91f3SChris Lattner   // Fail if any of the extra properties (e.g. array size) of the type disagree.
181b1ed91f3SChris Lattner   if (isa<IntegerType>(DstTy))
182b1ed91f3SChris Lattner     return false;  // bitwidth disagrees.
183b1ed91f3SChris Lattner   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
184b1ed91f3SChris Lattner     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
185b1ed91f3SChris Lattner       return false;
186eaf9b762SChris Lattner 
187b1ed91f3SChris Lattner   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
188b1ed91f3SChris Lattner     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
189b1ed91f3SChris Lattner       return false;
190b1ed91f3SChris Lattner   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
191b1ed91f3SChris Lattner     StructType *SSTy = cast<StructType>(SrcTy);
19244f7ab45SChris Lattner     if (DSTy->isLiteral() != SSTy->isLiteral() ||
193b1ed91f3SChris Lattner         DSTy->isPacked() != SSTy->isPacked())
194b1ed91f3SChris Lattner       return false;
195b1ed91f3SChris Lattner   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
196b1ed91f3SChris Lattner     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
197b1ed91f3SChris Lattner       return false;
198b1ed91f3SChris Lattner   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1995fad3e9aSJoey Gouly     if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
200361e513dSReid Spencer       return false;
201361e513dSReid Spencer   }
202361e513dSReid Spencer 
203b1ed91f3SChris Lattner   // Otherwise, we speculate that these two types will line up and recursively
204b1ed91f3SChris Lattner   // check the subelements.
205b1ed91f3SChris Lattner   Entry = DstTy;
206b1ed91f3SChris Lattner   SpeculativeTypes.push_back(SrcTy);
207eee6f995SChris Lattner 
208290e2cc5SRafael Espindola   for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
209290e2cc5SRafael Espindola     if (!areTypesIsomorphic(DstTy->getContainedType(I),
210290e2cc5SRafael Espindola                             SrcTy->getContainedType(I)))
211b1ed91f3SChris Lattner       return false;
212eee6f995SChris Lattner 
213b1ed91f3SChris Lattner   // If everything seems to have lined up, then everything is great.
214eee6f995SChris Lattner   return true;
215eee6f995SChris Lattner }
216eee6f995SChris Lattner 
217b1ed91f3SChris Lattner void TypeMapTy::linkDefinedTypeBodies() {
218b1ed91f3SChris Lattner   SmallVector<Type*, 16> Elements;
219c81c3f55SRafael Espindola   for (StructType *SrcSTy : SrcDefinitionsToResolve) {
220b1ed91f3SChris Lattner     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
221c81c3f55SRafael Espindola     assert(DstSTy->isOpaque());
222b1ed91f3SChris Lattner 
223b1ed91f3SChris Lattner     // Map the body of the source type over to a new body for the dest type.
224b1ed91f3SChris Lattner     Elements.resize(SrcSTy->getNumElements());
225290e2cc5SRafael Espindola     for (unsigned I = 0, E = Elements.size(); I != E; ++I)
226c81c3f55SRafael Espindola       Elements[I] = get(SrcSTy->getElementType(I));
227b1ed91f3SChris Lattner 
228b1ed91f3SChris Lattner     DstSTy->setBody(Elements, SrcSTy->isPacked());
229a5b9e1cfSRafael Espindola     DstStructTypesSet.switchToNonOpaque(DstSTy);
230b1ed91f3SChris Lattner   }
231c81c3f55SRafael Espindola   SrcDefinitionsToResolve.clear();
2325e3bd972SChris Lattner   DstResolvedOpaqueTypes.clear();
233eee6f995SChris Lattner }
234eee6f995SChris Lattner 
23531ad468dSRafael Espindola void TypeMapTy::finishType(StructType *DTy, StructType *STy,
23631ad468dSRafael Espindola                            ArrayRef<Type *> ETypes) {
23731ad468dSRafael Espindola   DTy->setBody(ETypes, STy->isPacked());
238a4e85e3dSRafael Espindola 
23931ad468dSRafael Espindola   // Steal STy's name.
24031ad468dSRafael Espindola   if (STy->hasName()) {
24131ad468dSRafael Espindola     SmallString<16> TmpName = STy->getName();
24231ad468dSRafael Espindola     STy->setName("");
24331ad468dSRafael Espindola     DTy->setName(TmpName);
24431ad468dSRafael Espindola   }
24531ad468dSRafael Espindola 
24631ad468dSRafael Espindola   DstStructTypesSet.addNonOpaque(DTy);
24731ad468dSRafael Espindola }
24831ad468dSRafael Espindola 
24931ad468dSRafael Espindola Type *TypeMapTy::get(Type *Ty) {
25031ad468dSRafael Espindola   SmallPtrSet<StructType *, 8> Visited;
25131ad468dSRafael Espindola   return get(Ty, Visited);
25231ad468dSRafael Espindola }
25331ad468dSRafael Espindola 
25431ad468dSRafael Espindola Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
255b1ed91f3SChris Lattner   // If we already have an entry for this type, return it.
256b1ed91f3SChris Lattner   Type **Entry = &MappedTypes[Ty];
257290e2cc5SRafael Espindola   if (*Entry)
258290e2cc5SRafael Espindola     return *Entry;
259b1ed91f3SChris Lattner 
26031ad468dSRafael Espindola   // These are types that LLVM itself will unique.
26131ad468dSRafael Espindola   bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
26231ad468dSRafael Espindola 
26331ad468dSRafael Espindola #ifndef NDEBUG
26431ad468dSRafael Espindola   if (!IsUniqued) {
26531ad468dSRafael Espindola     for (auto &Pair : MappedTypes) {
26631ad468dSRafael Espindola       assert(!(Pair.first != Ty && Pair.second == Ty) &&
26731ad468dSRafael Espindola              "mapping to a source type");
26831ad468dSRafael Espindola     }
26931ad468dSRafael Espindola   }
27031ad468dSRafael Espindola #endif
27131ad468dSRafael Espindola 
27231ad468dSRafael Espindola   if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
27331ad468dSRafael Espindola     StructType *DTy = StructType::create(Ty->getContext());
27431ad468dSRafael Espindola     return *Entry = DTy;
27531ad468dSRafael Espindola   }
27631ad468dSRafael Espindola 
27731ad468dSRafael Espindola   // If this is not a recursive type, then just map all of the elements and
278b1ed91f3SChris Lattner   // then rebuild the type from inside out.
27931ad468dSRafael Espindola   SmallVector<Type *, 4> ElementTypes;
28031ad468dSRafael Espindola 
281b1ed91f3SChris Lattner   // If there are no element types to map, then the type is itself.  This is
282b1ed91f3SChris Lattner   // true for the anonymous {} struct, things like 'float', integers, etc.
28331ad468dSRafael Espindola   if (Ty->getNumContainedTypes() == 0 && IsUniqued)
284b1ed91f3SChris Lattner     return *Entry = Ty;
285b1ed91f3SChris Lattner 
286b1ed91f3SChris Lattner   // Remap all of the elements, keeping track of whether any of them change.
287b1ed91f3SChris Lattner   bool AnyChange = false;
288b1ed91f3SChris Lattner   ElementTypes.resize(Ty->getNumContainedTypes());
289290e2cc5SRafael Espindola   for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
29031ad468dSRafael Espindola     ElementTypes[I] = get(Ty->getContainedType(I), Visited);
291290e2cc5SRafael Espindola     AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
292eee6f995SChris Lattner   }
293b1ed91f3SChris Lattner 
294b1ed91f3SChris Lattner   // If we found our type while recursively processing stuff, just use it.
295b1ed91f3SChris Lattner   Entry = &MappedTypes[Ty];
29631ad468dSRafael Espindola   if (*Entry) {
29731ad468dSRafael Espindola     if (auto *DTy = dyn_cast<StructType>(*Entry)) {
29831ad468dSRafael Espindola       if (DTy->isOpaque()) {
29931ad468dSRafael Espindola         auto *STy = cast<StructType>(Ty);
30031ad468dSRafael Espindola         finishType(DTy, STy, ElementTypes);
30131ad468dSRafael Espindola       }
30231ad468dSRafael Espindola     }
303290e2cc5SRafael Espindola     return *Entry;
30431ad468dSRafael Espindola   }
305b1ed91f3SChris Lattner 
30631ad468dSRafael Espindola   // If all of the element types mapped directly over and the type is not
30731ad468dSRafael Espindola   // a nomed struct, then the type is usable as-is.
30831ad468dSRafael Espindola   if (!AnyChange && IsUniqued)
309b1ed91f3SChris Lattner     return *Entry = Ty;
310b1ed91f3SChris Lattner 
311b1ed91f3SChris Lattner   // Otherwise, rebuild a modified type.
312b1ed91f3SChris Lattner   switch (Ty->getTypeID()) {
313290e2cc5SRafael Espindola   default:
314290e2cc5SRafael Espindola     llvm_unreachable("unknown derived type to remap");
315b1ed91f3SChris Lattner   case Type::ArrayTyID:
316b1ed91f3SChris Lattner     return *Entry = ArrayType::get(ElementTypes[0],
317b1ed91f3SChris Lattner                                    cast<ArrayType>(Ty)->getNumElements());
318b1ed91f3SChris Lattner   case Type::VectorTyID:
319b1ed91f3SChris Lattner     return *Entry = VectorType::get(ElementTypes[0],
320b1ed91f3SChris Lattner                                     cast<VectorType>(Ty)->getNumElements());
321b1ed91f3SChris Lattner   case Type::PointerTyID:
32231ad468dSRafael Espindola     return *Entry = PointerType::get(ElementTypes[0],
32331ad468dSRafael Espindola                                      cast<PointerType>(Ty)->getAddressSpace());
324b1ed91f3SChris Lattner   case Type::FunctionTyID:
325b1ed91f3SChris Lattner     return *Entry = FunctionType::get(ElementTypes[0],
326717d7eddSFrits van Bommel                                       makeArrayRef(ElementTypes).slice(1),
327b1ed91f3SChris Lattner                                       cast<FunctionType>(Ty)->isVarArg());
32831ad468dSRafael Espindola   case Type::StructTyID: {
32931ad468dSRafael Espindola     auto *STy = cast<StructType>(Ty);
33031ad468dSRafael Espindola     bool IsPacked = STy->isPacked();
33131ad468dSRafael Espindola     if (IsUniqued)
33231ad468dSRafael Espindola       return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
333b1ed91f3SChris Lattner 
334b1ed91f3SChris Lattner     // If the type is opaque, we can just use it directly.
335aa9918aaSRafael Espindola     if (STy->isOpaque()) {
33631ad468dSRafael Espindola       DstStructTypesSet.addOpaque(STy);
33731ad468dSRafael Espindola       return *Entry = Ty;
338aa9918aaSRafael Espindola     }
339b1ed91f3SChris Lattner 
34031ad468dSRafael Espindola     if (StructType *OldT =
34131ad468dSRafael Espindola             DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
342c81c3f55SRafael Espindola       STy->setName("");
34331ad468dSRafael Espindola       return *Entry = OldT;
344c81c3f55SRafael Espindola     }
345c81c3f55SRafael Espindola 
34631ad468dSRafael Espindola     if (!AnyChange) {
34731ad468dSRafael Espindola       DstStructTypesSet.addNonOpaque(STy);
34831ad468dSRafael Espindola       return *Entry = Ty;
34931ad468dSRafael Espindola     }
35031ad468dSRafael Espindola 
35131ad468dSRafael Espindola     StructType *DTy = StructType::create(Ty->getContext());
35231ad468dSRafael Espindola     finishType(DTy, STy, ElementTypes);
35331ad468dSRafael Espindola     return *Entry = DTy;
35431ad468dSRafael Espindola   }
35531ad468dSRafael Espindola   }
356b1ed91f3SChris Lattner }
357b1ed91f3SChris Lattner 
358b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
359b1ed91f3SChris Lattner // ModuleLinker implementation.
360b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
361b1ed91f3SChris Lattner 
362b1ed91f3SChris Lattner namespace {
363f6f121e2SJames Molloy class ModuleLinker;
364f6f121e2SJames Molloy 
36518c89411SRafael Espindola /// Creates prototypes for functions that are lazily linked on the fly. This
36618c89411SRafael Espindola /// speeds up linking for modules with many/ lazily linked functions of which
36718c89411SRafael Espindola /// few get used.
368437aafdaSDavid Blaikie class ValueMaterializerTy final : public ValueMaterializer {
369f6f121e2SJames Molloy   TypeMapTy &TypeMap;
370f6f121e2SJames Molloy   Module *DstM;
371ef23711eSRafael Espindola   std::vector<GlobalValue *> &LazilyLinkGlobalValues;
372c7ed52f2STeresa Johnson   ModuleLinker *ModLinker;
373c84f608bSRafael Espindola 
374f6f121e2SJames Molloy public:
375f6f121e2SJames Molloy   ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
376c7ed52f2STeresa Johnson                       std::vector<GlobalValue *> &LazilyLinkGlobalValues,
377c7ed52f2STeresa Johnson                       ModuleLinker *ModLinker)
378c84f608bSRafael Espindola       : ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
379c7ed52f2STeresa Johnson         LazilyLinkGlobalValues(LazilyLinkGlobalValues), ModLinker(ModLinker) {}
380f6f121e2SJames Molloy 
3818548299aSCraig Topper   Value *materializeValueFor(Value *V) override;
382f6f121e2SJames Molloy };
383f6f121e2SJames Molloy 
384d12b4a33SRafael Espindola class LinkDiagnosticInfo : public DiagnosticInfo {
385d12b4a33SRafael Espindola   const Twine &Msg;
386d12b4a33SRafael Espindola 
387d12b4a33SRafael Espindola public:
388d12b4a33SRafael Espindola   LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
389d12b4a33SRafael Espindola   void print(DiagnosticPrinter &DP) const override;
390d12b4a33SRafael Espindola };
391d12b4a33SRafael Espindola LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
392d12b4a33SRafael Espindola                                        const Twine &Msg)
393d12b4a33SRafael Espindola     : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
394d12b4a33SRafael Espindola void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
395d12b4a33SRafael Espindola 
39618c89411SRafael Espindola /// This is an implementation class for the LinkModules function, which is the
39718c89411SRafael Espindola /// entrypoint for this file.
398b1ed91f3SChris Lattner class ModuleLinker {
399b1ed91f3SChris Lattner   Module *DstM, *SrcM;
400b1ed91f3SChris Lattner 
401b1ed91f3SChris Lattner   TypeMapTy TypeMap;
402f6f121e2SJames Molloy   ValueMaterializerTy ValMaterializer;
403b1ed91f3SChris Lattner 
40418c89411SRafael Espindola   /// Mapping of values from what they used to be in Src, to what they are now
40518c89411SRafael Espindola   /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
40618c89411SRafael Espindola   /// due to the use of Value handles which the Linker doesn't actually need,
40718c89411SRafael Espindola   /// but this allows us to reuse the ValueMapper code.
408b1ed91f3SChris Lattner   ValueToValueMapTy ValueMap;
409b1ed91f3SChris Lattner 
410b1ed91f3SChris Lattner   struct AppendingVarInfo {
411b1ed91f3SChris Lattner     GlobalVariable *NewGV;   // New aggregate global in dest module.
4123e8bc6a8SRafael Espindola     const Constant *DstInit; // Old initializer from dest module.
4133e8bc6a8SRafael Espindola     const Constant *SrcInit; // Old initializer from src module.
414b1ed91f3SChris Lattner   };
415b1ed91f3SChris Lattner 
416b1ed91f3SChris Lattner   std::vector<AppendingVarInfo> AppendingVars;
417b1ed91f3SChris Lattner 
418cbb91408STanya Lattner   // Set of items not to link in from source.
419cbb91408STanya Lattner   SmallPtrSet<const Value *, 16> DoNotLinkFromSource;
420cbb91408STanya Lattner 
421ef23711eSRafael Espindola   // Vector of GlobalValues to lazily link in.
422ef23711eSRafael Espindola   std::vector<GlobalValue *> LazilyLinkGlobalValues;
4230a48b877STanya Lattner 
424d0b23befSRafael Espindola   DiagnosticHandlerFunction DiagnosticHandler;
4254160f5d3SRafael Espindola 
426e868123dSDuncan P. N. Exon Smith   /// For symbol clashes, prefer those from Src.
427020d4fb1SArtem Belevich   unsigned Flags;
428e868123dSDuncan P. N. Exon Smith 
429c7ed52f2STeresa Johnson   /// Function index passed into ModuleLinker for using in function
430c7ed52f2STeresa Johnson   /// importing/exporting handling.
431*8220e8a8SMehdi Amini   const FunctionInfoIndex *ImportIndex;
432c7ed52f2STeresa Johnson 
433c7ed52f2STeresa Johnson   /// Function to import from source module, all other functions are
434c7ed52f2STeresa Johnson   /// imported as declarations instead of definitions.
435c7ed52f2STeresa Johnson   Function *ImportFunction;
436c7ed52f2STeresa Johnson 
437c7ed52f2STeresa Johnson   /// Set to true if the given FunctionInfoIndex contains any functions
438c7ed52f2STeresa Johnson   /// from this source module, in which case we must conservatively assume
439c7ed52f2STeresa Johnson   /// that any of its functions may be imported into another module
440c7ed52f2STeresa Johnson   /// as part of a different backend compilation process.
441c7ed52f2STeresa Johnson   bool HasExportedFunctions;
442c7ed52f2STeresa Johnson 
4431063293aSTeresa Johnson   /// Set to true when all global value body linking is complete (including
4441063293aSTeresa Johnson   /// lazy linking). Used to prevent metadata linking from creating new
4451063293aSTeresa Johnson   /// references.
4461063293aSTeresa Johnson   bool DoneLinkingBodies;
4471063293aSTeresa Johnson 
448b1ed91f3SChris Lattner public:
44931ad468dSRafael Espindola   ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
450c7ed52f2STeresa Johnson                DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
451*8220e8a8SMehdi Amini                const FunctionInfoIndex *Index = nullptr,
452c7ed52f2STeresa Johnson                Function *FuncToImport = nullptr)
453a4e85e3dSRafael Espindola       : DstM(dstM), SrcM(srcM), TypeMap(Set),
454c7ed52f2STeresa Johnson         ValMaterializer(TypeMap, DstM, LazilyLinkGlobalValues, this),
455c7ed52f2STeresa Johnson         DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
4561063293aSTeresa Johnson         ImportFunction(FuncToImport), HasExportedFunctions(false),
4571063293aSTeresa Johnson         DoneLinkingBodies(false) {
458c7ed52f2STeresa Johnson     assert((ImportIndex || !ImportFunction) &&
459c7ed52f2STeresa Johnson            "Expect a FunctionInfoIndex when importing");
460c7ed52f2STeresa Johnson     // If we have a FunctionInfoIndex but no function to import,
461c7ed52f2STeresa Johnson     // then this is the primary module being compiled in a ThinLTO
462c7ed52f2STeresa Johnson     // backend compilation, and we need to see if it has functions that
463c7ed52f2STeresa Johnson     // may be exported to another backend compilation.
464c7ed52f2STeresa Johnson     if (ImportIndex && !ImportFunction)
465c7ed52f2STeresa Johnson       HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
466c7ed52f2STeresa Johnson   }
467b1ed91f3SChris Lattner 
468b1ed91f3SChris Lattner   bool run();
469b1ed91f3SChris Lattner 
470020d4fb1SArtem Belevich   bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
471020d4fb1SArtem Belevich   bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
472020d4fb1SArtem Belevich   bool shouldInternalizeLinkedSymbols() {
473020d4fb1SArtem Belevich     return Flags & Linker::InternalizeLinkedSymbols;
474020d4fb1SArtem Belevich   }
475020d4fb1SArtem Belevich 
476c7ed52f2STeresa Johnson   /// Handles cloning of a global values from the source module into
477c7ed52f2STeresa Johnson   /// the destination module, including setting the attributes and visibility.
478c7ed52f2STeresa Johnson   GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, const GlobalValue *SGV,
479c7ed52f2STeresa Johnson                                     const GlobalValue *DGV = nullptr);
480c7ed52f2STeresa Johnson 
481c7ed52f2STeresa Johnson   /// Check if we should promote the given local value to global scope.
482c7ed52f2STeresa Johnson   bool doPromoteLocalToGlobal(const GlobalValue *SGV);
483c7ed52f2STeresa Johnson 
4841063293aSTeresa Johnson   /// Check if all global value body linking is complete.
4851063293aSTeresa Johnson   bool doneLinkingBodies() { return DoneLinkingBodies; }
4861063293aSTeresa Johnson 
487b1ed91f3SChris Lattner private:
488d12b4a33SRafael Espindola   bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
489d12b4a33SRafael Espindola                             const GlobalValue &Src);
490dbb0bd1aSRafael Espindola 
491d12b4a33SRafael Espindola   /// Helper method for setting a message and returning an error code.
492b1ed91f3SChris Lattner   bool emitError(const Twine &Message) {
4934160f5d3SRafael Espindola     DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
494b1ed91f3SChris Lattner     return true;
495b1ed91f3SChris Lattner   }
496b1ed91f3SChris Lattner 
497d12b4a33SRafael Espindola   void emitWarning(const Twine &Message) {
4984160f5d3SRafael Espindola     DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
499d12b4a33SRafael Espindola   }
500d12b4a33SRafael Espindola 
501dad0a645SDavid Majnemer   bool getComdatLeader(Module *M, StringRef ComdatName,
502dad0a645SDavid Majnemer                        const GlobalVariable *&GVar);
503dad0a645SDavid Majnemer   bool computeResultingSelectionKind(StringRef ComdatName,
504dad0a645SDavid Majnemer                                      Comdat::SelectionKind Src,
505dad0a645SDavid Majnemer                                      Comdat::SelectionKind Dst,
506dad0a645SDavid Majnemer                                      Comdat::SelectionKind &Result,
507dad0a645SDavid Majnemer                                      bool &LinkFromSrc);
508dad0a645SDavid Majnemer   std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
509dad0a645SDavid Majnemer       ComdatsChosen;
510dad0a645SDavid Majnemer   bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
511dad0a645SDavid Majnemer                        bool &LinkFromSrc);
5122d5fb8caSTeresa Johnson   // Keep track of the global value members of each comdat in source.
5132d5fb8caSTeresa Johnson   DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
514dad0a645SDavid Majnemer 
51518c89411SRafael Espindola   /// Given a global in the source module, return the global in the
51618c89411SRafael Espindola   /// destination module that is being linked to, if any.
5173e8bc6a8SRafael Espindola   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
518b1ed91f3SChris Lattner     // If the source has no name it can't link.  If it has local linkage,
519b1ed91f3SChris Lattner     // there is no name match-up going on.
520b1ed91f3SChris Lattner     if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
5212617dcceSCraig Topper       return nullptr;
522b1ed91f3SChris Lattner 
523b1ed91f3SChris Lattner     // Otherwise see if we have a match in the destination module's symtab.
524b1ed91f3SChris Lattner     GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
525c84f608bSRafael Espindola     if (!DGV)
526c84f608bSRafael Espindola       return nullptr;
527b1ed91f3SChris Lattner 
528b1ed91f3SChris Lattner     // If we found a global with the same name in the dest module, but it has
529b1ed91f3SChris Lattner     // internal linkage, we are really not doing any linkage here.
530b1ed91f3SChris Lattner     if (DGV->hasLocalLinkage())
5312617dcceSCraig Topper       return nullptr;
532b1ed91f3SChris Lattner 
533b1ed91f3SChris Lattner     // Otherwise, we do in fact link to the destination global.
534b1ed91f3SChris Lattner     return DGV;
535b1ed91f3SChris Lattner   }
536b1ed91f3SChris Lattner 
537b1ed91f3SChris Lattner   void computeTypeMapping();
538b1ed91f3SChris Lattner 
53909d84addSDuncan P. N. Exon Smith   void upgradeMismatchedGlobalArray(StringRef Name);
54009d84addSDuncan P. N. Exon Smith   void upgradeMismatchedGlobals();
54109d84addSDuncan P. N. Exon Smith 
5423e8bc6a8SRafael Espindola   bool linkAppendingVarProto(GlobalVariable *DstGV,
5433e8bc6a8SRafael Espindola                              const GlobalVariable *SrcGV);
544778fcc77SRafael Espindola 
545778fcc77SRafael Espindola   bool linkGlobalValueProto(GlobalValue *GV);
54666f02413SBill Wendling   bool linkModuleFlagsMetadata();
547b1ed91f3SChris Lattner 
548b1ed91f3SChris Lattner   void linkAppendingVarInit(const AppendingVarInfo &AVI);
549ef23711eSRafael Espindola 
550ef23711eSRafael Espindola   void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
551ef23711eSRafael Espindola   bool linkFunctionBody(Function &Dst, Function &Src);
552ef23711eSRafael Espindola   void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
553ef23711eSRafael Espindola   bool linkGlobalValueBody(GlobalValue &Src);
554ef23711eSRafael Espindola 
555c7ed52f2STeresa Johnson   /// Functions that take care of cloning a specific global value type
556c7ed52f2STeresa Johnson   /// into the destination module.
557c7ed52f2STeresa Johnson   GlobalVariable *copyGlobalVariableProto(TypeMapTy &TypeMap,
558c7ed52f2STeresa Johnson                                           const GlobalVariable *SGVar);
559c7ed52f2STeresa Johnson   Function *copyFunctionProto(TypeMapTy &TypeMap, const Function *SF);
560c7ed52f2STeresa Johnson   GlobalValue *copyGlobalAliasProto(TypeMapTy &TypeMap, const GlobalAlias *SGA);
561c7ed52f2STeresa Johnson 
562c7ed52f2STeresa Johnson   /// Helper methods to check if we are importing from or potentially
563c7ed52f2STeresa Johnson   /// exporting from the current source module.
564c7ed52f2STeresa Johnson   bool isPerformingImport() { return ImportFunction != nullptr; }
565c7ed52f2STeresa Johnson   bool isModuleExporting() { return HasExportedFunctions; }
566c7ed52f2STeresa Johnson 
567c7ed52f2STeresa Johnson   /// If we are importing from the source module, checks if we should
568c7ed52f2STeresa Johnson   /// import SGV as a definition, otherwise import as a declaration.
569c7ed52f2STeresa Johnson   bool doImportAsDefinition(const GlobalValue *SGV);
570c7ed52f2STeresa Johnson 
571c7ed52f2STeresa Johnson   /// Get the name for SGV that should be used in the linked destination
572c7ed52f2STeresa Johnson   /// module. Specifically, this handles the case where we need to rename
573c7ed52f2STeresa Johnson   /// a local that is being promoted to global scope.
574c7ed52f2STeresa Johnson   std::string getName(const GlobalValue *SGV);
575c7ed52f2STeresa Johnson 
576c7ed52f2STeresa Johnson   /// Get the new linkage for SGV that should be used in the linked destination
577c7ed52f2STeresa Johnson   /// module. Specifically, for ThinLTO importing or exporting it may need
578c7ed52f2STeresa Johnson   /// to be adjusted.
579c7ed52f2STeresa Johnson   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
580c7ed52f2STeresa Johnson 
581c7ed52f2STeresa Johnson   /// Copies the necessary global value attributes and name from the source
582c7ed52f2STeresa Johnson   /// to the newly cloned global value.
583c7ed52f2STeresa Johnson   void copyGVAttributes(GlobalValue *NewGV, const GlobalValue *SrcGV);
584c7ed52f2STeresa Johnson 
585c7ed52f2STeresa Johnson   /// Updates the visibility for the new global cloned from the source
586c7ed52f2STeresa Johnson   /// and, if applicable, linked with an existing destination global.
587c7ed52f2STeresa Johnson   /// Handles visibility change required for promoted locals.
588c7ed52f2STeresa Johnson   void setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
589c7ed52f2STeresa Johnson                      const GlobalValue *DGV = nullptr);
590c7ed52f2STeresa Johnson 
591b1ed91f3SChris Lattner   void linkNamedMDNodes();
592eee6f995SChris Lattner };
593f00654e3SAlexander Kornienko }
594d48b7783SBill Wendling 
59518c89411SRafael Espindola /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
59618c89411SRafael Espindola /// table. This is good for all clients except for us. Go through the trouble
59718c89411SRafael Espindola /// to force this back.
598b1ed91f3SChris Lattner static void forceRenaming(GlobalValue *GV, StringRef Name) {
599b1ed91f3SChris Lattner   // If the global doesn't force its name or if it already has the right name,
600b1ed91f3SChris Lattner   // there is nothing for us to do.
601c7ed52f2STeresa Johnson   // Note that any required local to global promotion should already be done,
602c7ed52f2STeresa Johnson   // so promoted locals will not skip this handling as their linkage is no
603c7ed52f2STeresa Johnson   // longer local.
604b1ed91f3SChris Lattner   if (GV->hasLocalLinkage() || GV->getName() == Name)
605b1ed91f3SChris Lattner     return;
606b1ed91f3SChris Lattner 
607b1ed91f3SChris Lattner   Module *M = GV->getParent();
608361e513dSReid Spencer 
609361e513dSReid Spencer   // If there is a conflict, rename the conflict.
610b1ed91f3SChris Lattner   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
6112a8d2e0eSChris Lattner     GV->takeName(ConflictGV);
612361e513dSReid Spencer     ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
613b1ed91f3SChris Lattner     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
6142a8d2e0eSChris Lattner   } else {
6152a8d2e0eSChris Lattner     GV->setName(Name);              // Force the name back
6163aaaa0b2SReid Spencer   }
6173aaaa0b2SReid Spencer }
6183aaaa0b2SReid Spencer 
61918c89411SRafael Espindola /// copy additional attributes (those not needed to construct a GlobalValue)
62018c89411SRafael Espindola /// from the SrcGV to the DestGV.
621c7ed52f2STeresa Johnson void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,
622c7ed52f2STeresa Johnson                                     const GlobalValue *SrcGV) {
623c7ed52f2STeresa Johnson   auto *GA = dyn_cast<GlobalAlias>(SrcGV);
624c7ed52f2STeresa Johnson   // Check for the special case of converting an alias (definition) to a
625c7ed52f2STeresa Johnson   // non-alias (declaration). This can happen when we are importing and
626c7ed52f2STeresa Johnson   // encounter a weak_any alias (weak_any defs may not be imported, see
627c7ed52f2STeresa Johnson   // comments in ModuleLinker::getLinkage) or an alias whose base object is
628c7ed52f2STeresa Johnson   // being imported as a declaration. In that case copy the attributes from the
629c7ed52f2STeresa Johnson   // base object.
630c7ed52f2STeresa Johnson   if (GA && !dyn_cast<GlobalAlias>(NewGV)) {
6313cd8161cSTeresa Johnson     assert(isPerformingImport() && !doImportAsDefinition(GA));
632c7ed52f2STeresa Johnson     NewGV->copyAttributesFrom(GA->getBaseObject());
633c7ed52f2STeresa Johnson   } else
634c7ed52f2STeresa Johnson     NewGV->copyAttributesFrom(SrcGV);
635c7ed52f2STeresa Johnson   forceRenaming(NewGV, getName(SrcGV));
636361e513dSReid Spencer }
637361e513dSReid Spencer 
63823f8d64bSRafael Espindola static bool isLessConstraining(GlobalValue::VisibilityTypes a,
63923f8d64bSRafael Espindola                                GlobalValue::VisibilityTypes b) {
64023f8d64bSRafael Espindola   if (a == GlobalValue::HiddenVisibility)
64123f8d64bSRafael Espindola     return false;
64223f8d64bSRafael Espindola   if (b == GlobalValue::HiddenVisibility)
64323f8d64bSRafael Espindola     return true;
64423f8d64bSRafael Espindola   if (a == GlobalValue::ProtectedVisibility)
64523f8d64bSRafael Espindola     return false;
64623f8d64bSRafael Espindola   if (b == GlobalValue::ProtectedVisibility)
64723f8d64bSRafael Espindola     return true;
64823f8d64bSRafael Espindola   return false;
64923f8d64bSRafael Espindola }
65023f8d64bSRafael Espindola 
651c7ed52f2STeresa Johnson bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
652c7ed52f2STeresa Johnson   if (!isPerformingImport())
653c7ed52f2STeresa Johnson     return false;
6543cd8161cSTeresa Johnson   auto *GA = dyn_cast<GlobalAlias>(SGV);
6553cd8161cSTeresa Johnson   if (GA) {
6563cd8161cSTeresa Johnson     if (GA->hasWeakAnyLinkage())
6573cd8161cSTeresa Johnson       return false;
6583cd8161cSTeresa Johnson     return doImportAsDefinition(GA->getBaseObject());
6593cd8161cSTeresa Johnson   }
660dfbebc37STeresa Johnson   // Always import GlobalVariable definitions, except for the special
661dfbebc37STeresa Johnson   // case of WeakAny which are imported as ExternalWeak declarations
662dfbebc37STeresa Johnson   // (see comments in ModuleLinker::getLinkage). The linkage changes
663c7ed52f2STeresa Johnson   // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
664c7ed52f2STeresa Johnson   // global variables with external linkage are transformed to
6653cd8161cSTeresa Johnson   // available_externally definitions, which are ultimately turned into
6663cd8161cSTeresa Johnson   // declarations after the EliminateAvailableExternally pass).
66766059c9fSCraig Topper   if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
6683cd8161cSTeresa Johnson       !SGV->hasWeakAnyLinkage())
669c7ed52f2STeresa Johnson     return true;
670c7ed52f2STeresa Johnson   // Only import the function requested for importing.
671c7ed52f2STeresa Johnson   auto *SF = dyn_cast<Function>(SGV);
672c7ed52f2STeresa Johnson   if (SF && SF == ImportFunction)
673c7ed52f2STeresa Johnson     return true;
674c7ed52f2STeresa Johnson   // Otherwise no.
675c7ed52f2STeresa Johnson   return false;
676c7ed52f2STeresa Johnson }
677c7ed52f2STeresa Johnson 
678c7ed52f2STeresa Johnson bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
679c7ed52f2STeresa Johnson   assert(SGV->hasLocalLinkage());
680c7ed52f2STeresa Johnson   // Both the imported references and the original local variable must
681c7ed52f2STeresa Johnson   // be promoted.
682c7ed52f2STeresa Johnson   if (!isPerformingImport() && !isModuleExporting())
683c7ed52f2STeresa Johnson     return false;
684c7ed52f2STeresa Johnson 
685c7ed52f2STeresa Johnson   // Local const variables never need to be promoted unless they are address
686c7ed52f2STeresa Johnson   // taken. The imported uses can simply use the clone created in this module.
687c7ed52f2STeresa Johnson   // For now we are conservative in determining which variables are not
688c7ed52f2STeresa Johnson   // address taken by checking the unnamed addr flag. To be more aggressive,
689c7ed52f2STeresa Johnson   // the address taken information must be checked earlier during parsing
690c7ed52f2STeresa Johnson   // of the module and recorded in the function index for use when importing
691c7ed52f2STeresa Johnson   // from that module.
692c7ed52f2STeresa Johnson   auto *GVar = dyn_cast<GlobalVariable>(SGV);
693c7ed52f2STeresa Johnson   if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
694c7ed52f2STeresa Johnson     return false;
695c7ed52f2STeresa Johnson 
696c7ed52f2STeresa Johnson   // Eventually we only need to promote functions in the exporting module that
697c7ed52f2STeresa Johnson   // are referenced by a potentially exported function (i.e. one that is in the
698c7ed52f2STeresa Johnson   // function index).
699c7ed52f2STeresa Johnson   return true;
700c7ed52f2STeresa Johnson }
701c7ed52f2STeresa Johnson 
702c7ed52f2STeresa Johnson std::string ModuleLinker::getName(const GlobalValue *SGV) {
703c7ed52f2STeresa Johnson   // For locals that must be promoted to global scope, ensure that
704c7ed52f2STeresa Johnson   // the promoted name uniquely identifies the copy in the original module,
705c7ed52f2STeresa Johnson   // using the ID assigned during combined index creation. When importing,
706c7ed52f2STeresa Johnson   // we rename all locals (not just those that are promoted) in order to
707c7ed52f2STeresa Johnson   // avoid naming conflicts between locals imported from different modules.
708c7ed52f2STeresa Johnson   if (SGV->hasLocalLinkage() &&
709c7ed52f2STeresa Johnson       (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
710c7ed52f2STeresa Johnson     return FunctionInfoIndex::getGlobalNameForLocal(
711c7ed52f2STeresa Johnson         SGV->getName(),
712c7ed52f2STeresa Johnson         ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
713c7ed52f2STeresa Johnson   return SGV->getName();
714c7ed52f2STeresa Johnson }
715c7ed52f2STeresa Johnson 
716c7ed52f2STeresa Johnson GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
717c7ed52f2STeresa Johnson   // Any local variable that is referenced by an exported function needs
718c7ed52f2STeresa Johnson   // to be promoted to global scope. Since we don't currently know which
719c7ed52f2STeresa Johnson   // functions reference which local variables/functions, we must treat
720c7ed52f2STeresa Johnson   // all as potentially exported if this module is exporting anything.
721c7ed52f2STeresa Johnson   if (isModuleExporting()) {
722c7ed52f2STeresa Johnson     if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
723c7ed52f2STeresa Johnson       return GlobalValue::ExternalLinkage;
724c7ed52f2STeresa Johnson     return SGV->getLinkage();
725c7ed52f2STeresa Johnson   }
726c7ed52f2STeresa Johnson 
727c7ed52f2STeresa Johnson   // Otherwise, if we aren't importing, no linkage change is needed.
728c7ed52f2STeresa Johnson   if (!isPerformingImport())
729c7ed52f2STeresa Johnson     return SGV->getLinkage();
730c7ed52f2STeresa Johnson 
731c7ed52f2STeresa Johnson   switch (SGV->getLinkage()) {
732c7ed52f2STeresa Johnson   case GlobalValue::ExternalLinkage:
733c7ed52f2STeresa Johnson     // External defnitions are converted to available_externally
734c7ed52f2STeresa Johnson     // definitions upon import, so that they are available for inlining
735c7ed52f2STeresa Johnson     // and/or optimization, but are turned into declarations later
736c7ed52f2STeresa Johnson     // during the EliminateAvailableExternally pass.
7373cd8161cSTeresa Johnson     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
738c7ed52f2STeresa Johnson       return GlobalValue::AvailableExternallyLinkage;
739c7ed52f2STeresa Johnson     // An imported external declaration stays external.
740c7ed52f2STeresa Johnson     return SGV->getLinkage();
741c7ed52f2STeresa Johnson 
742c7ed52f2STeresa Johnson   case GlobalValue::AvailableExternallyLinkage:
743c7ed52f2STeresa Johnson     // An imported available_externally definition converts
744c7ed52f2STeresa Johnson     // to external if imported as a declaration.
745c7ed52f2STeresa Johnson     if (!doImportAsDefinition(SGV))
746c7ed52f2STeresa Johnson       return GlobalValue::ExternalLinkage;
747c7ed52f2STeresa Johnson     // An imported available_externally declaration stays that way.
748c7ed52f2STeresa Johnson     return SGV->getLinkage();
749c7ed52f2STeresa Johnson 
750c7ed52f2STeresa Johnson   case GlobalValue::LinkOnceAnyLinkage:
751c7ed52f2STeresa Johnson   case GlobalValue::LinkOnceODRLinkage:
752c7ed52f2STeresa Johnson     // These both stay the same when importing the definition.
753c7ed52f2STeresa Johnson     // The ThinLTO pass will eventually force-import their definitions.
754c7ed52f2STeresa Johnson     return SGV->getLinkage();
755c7ed52f2STeresa Johnson 
756c7ed52f2STeresa Johnson   case GlobalValue::WeakAnyLinkage:
757c7ed52f2STeresa Johnson     // Can't import weak_any definitions correctly, or we might change the
758c7ed52f2STeresa Johnson     // program semantics, since the linker will pick the first weak_any
759c7ed52f2STeresa Johnson     // definition and importing would change the order they are seen by the
760c7ed52f2STeresa Johnson     // linker. The module linking caller needs to enforce this.
761c7ed52f2STeresa Johnson     assert(!doImportAsDefinition(SGV));
762c7ed52f2STeresa Johnson     // If imported as a declaration, it becomes external_weak.
763c7ed52f2STeresa Johnson     return GlobalValue::ExternalWeakLinkage;
764c7ed52f2STeresa Johnson 
765c7ed52f2STeresa Johnson   case GlobalValue::WeakODRLinkage:
766c7ed52f2STeresa Johnson     // For weak_odr linkage, there is a guarantee that all copies will be
767c7ed52f2STeresa Johnson     // equivalent, so the issue described above for weak_any does not exist,
768c7ed52f2STeresa Johnson     // and the definition can be imported. It can be treated similarly
769c7ed52f2STeresa Johnson     // to an imported externally visible global value.
7703cd8161cSTeresa Johnson     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
771c7ed52f2STeresa Johnson       return GlobalValue::AvailableExternallyLinkage;
772c7ed52f2STeresa Johnson     else
773c7ed52f2STeresa Johnson       return GlobalValue::ExternalLinkage;
774c7ed52f2STeresa Johnson 
775c7ed52f2STeresa Johnson   case GlobalValue::AppendingLinkage:
776c7ed52f2STeresa Johnson     // It would be incorrect to import an appending linkage variable,
777c7ed52f2STeresa Johnson     // since it would cause global constructors/destructors to be
778c7ed52f2STeresa Johnson     // executed multiple times. This should have already been handled
779c7ed52f2STeresa Johnson     // by linkGlobalValueProto.
780f3d2329dSDavide Italiano     llvm_unreachable("Cannot import appending linkage variable");
781c7ed52f2STeresa Johnson 
782c7ed52f2STeresa Johnson   case GlobalValue::InternalLinkage:
783c7ed52f2STeresa Johnson   case GlobalValue::PrivateLinkage:
784c7ed52f2STeresa Johnson     // If we are promoting the local to global scope, it is handled
785c7ed52f2STeresa Johnson     // similarly to a normal externally visible global.
786c7ed52f2STeresa Johnson     if (doPromoteLocalToGlobal(SGV)) {
7873cd8161cSTeresa Johnson       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
788c7ed52f2STeresa Johnson         return GlobalValue::AvailableExternallyLinkage;
789c7ed52f2STeresa Johnson       else
790c7ed52f2STeresa Johnson         return GlobalValue::ExternalLinkage;
791c7ed52f2STeresa Johnson     }
792c7ed52f2STeresa Johnson     // A non-promoted imported local definition stays local.
793c7ed52f2STeresa Johnson     // The ThinLTO pass will eventually force-import their definitions.
794c7ed52f2STeresa Johnson     return SGV->getLinkage();
795c7ed52f2STeresa Johnson 
796c7ed52f2STeresa Johnson   case GlobalValue::ExternalWeakLinkage:
797c7ed52f2STeresa Johnson     // External weak doesn't apply to definitions, must be a declaration.
798c7ed52f2STeresa Johnson     assert(!doImportAsDefinition(SGV));
799c7ed52f2STeresa Johnson     // Linkage stays external_weak.
800c7ed52f2STeresa Johnson     return SGV->getLinkage();
801c7ed52f2STeresa Johnson 
802c7ed52f2STeresa Johnson   case GlobalValue::CommonLinkage:
803c7ed52f2STeresa Johnson     // Linkage stays common on definitions.
804c7ed52f2STeresa Johnson     // The ThinLTO pass will eventually force-import their definitions.
805c7ed52f2STeresa Johnson     return SGV->getLinkage();
806c7ed52f2STeresa Johnson   }
807c7ed52f2STeresa Johnson 
808c7ed52f2STeresa Johnson   llvm_unreachable("unknown linkage type");
809c7ed52f2STeresa Johnson }
810c7ed52f2STeresa Johnson 
811ef23711eSRafael Espindola /// Loop through the global variables in the src module and merge them into the
812ef23711eSRafael Espindola /// dest module.
813c7ed52f2STeresa Johnson GlobalVariable *
814c7ed52f2STeresa Johnson ModuleLinker::copyGlobalVariableProto(TypeMapTy &TypeMap,
815ef23711eSRafael Espindola                                       const GlobalVariable *SGVar) {
816ef23711eSRafael Espindola   // No linking to be performed or linking from the source: simply create an
817ef23711eSRafael Espindola   // identical version of the symbol over in the dest module... the
818ef23711eSRafael Espindola   // initializer will be filled in later by LinkGlobalInits.
819ef23711eSRafael Espindola   GlobalVariable *NewDGV = new GlobalVariable(
820c7ed52f2STeresa Johnson       *DstM, TypeMap.get(SGVar->getType()->getElementType()),
821c7ed52f2STeresa Johnson       SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),
822c7ed52f2STeresa Johnson       /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
823ef23711eSRafael Espindola       SGVar->getType()->getAddressSpace());
824f6f121e2SJames Molloy 
825ef23711eSRafael Espindola   return NewDGV;
8263931c28bSRafael Espindola }
8273931c28bSRafael Espindola 
828ef23711eSRafael Espindola /// Link the function in the source module into the destination module if
829ef23711eSRafael Espindola /// needed, setting up mapping information.
830c7ed52f2STeresa Johnson Function *ModuleLinker::copyFunctionProto(TypeMapTy &TypeMap,
831ef23711eSRafael Espindola                                           const Function *SF) {
832ef23711eSRafael Espindola   // If there is no linkage to be performed or we are linking from the source,
833ef23711eSRafael Espindola   // bring SF over.
834c7ed52f2STeresa Johnson   return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
835c7ed52f2STeresa Johnson                           getName(SF), DstM);
836ef23711eSRafael Espindola }
837ef23711eSRafael Espindola 
838ef23711eSRafael Espindola /// Set up prototypes for any aliases that come over from the source module.
839c7ed52f2STeresa Johnson GlobalValue *ModuleLinker::copyGlobalAliasProto(TypeMapTy &TypeMap,
840ef23711eSRafael Espindola                                                 const GlobalAlias *SGA) {
841c7ed52f2STeresa Johnson   // If we are importing and encounter a weak_any alias, or an alias to
842c7ed52f2STeresa Johnson   // an object being imported as a declaration, we must import the alias
843c7ed52f2STeresa Johnson   // as a declaration as well, which involves converting it to a non-alias.
844c7ed52f2STeresa Johnson   // See comments in ModuleLinker::getLinkage for why we cannot import
845c7ed52f2STeresa Johnson   // weak_any defintions.
8463cd8161cSTeresa Johnson   if (isPerformingImport() && !doImportAsDefinition(SGA)) {
847c7ed52f2STeresa Johnson     // Need to convert to declaration. All aliases must be definitions.
848c7ed52f2STeresa Johnson     const GlobalValue *GVal = SGA->getBaseObject();
849c7ed52f2STeresa Johnson     GlobalValue *NewGV;
850c7ed52f2STeresa Johnson     if (auto *GVar = dyn_cast<GlobalVariable>(GVal))
851c7ed52f2STeresa Johnson       NewGV = copyGlobalVariableProto(TypeMap, GVar);
852c7ed52f2STeresa Johnson     else {
853c7ed52f2STeresa Johnson       auto *F = dyn_cast<Function>(GVal);
854c7ed52f2STeresa Johnson       assert(F);
855c7ed52f2STeresa Johnson       NewGV = copyFunctionProto(TypeMap, F);
856c7ed52f2STeresa Johnson     }
857f1b0a6e3STeresa Johnson     // Set the linkage to External or ExternalWeak (see comments in
858f1b0a6e3STeresa Johnson     // ModuleLinker::getLinkage for why WeakAny is converted to ExternalWeak).
859c7ed52f2STeresa Johnson     if (SGA->hasWeakAnyLinkage())
860c7ed52f2STeresa Johnson       NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
861f1b0a6e3STeresa Johnson     else
862f1b0a6e3STeresa Johnson       NewGV->setLinkage(GlobalValue::ExternalLinkage);
863c7ed52f2STeresa Johnson     return NewGV;
864c7ed52f2STeresa Johnson   }
865ef23711eSRafael Espindola   // If there is no linkage to be performed or we're linking from the source,
866ef23711eSRafael Espindola   // bring over SGA.
8676614d8d2SDavid Blaikie   auto *Ty = TypeMap.get(SGA->getValueType());
8686614d8d2SDavid Blaikie   return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
869c7ed52f2STeresa Johnson                              getLinkage(SGA), getName(SGA), DstM);
870ef23711eSRafael Espindola }
871ef23711eSRafael Espindola 
872c7ed52f2STeresa Johnson void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
873c7ed52f2STeresa Johnson                                  const GlobalValue *DGV) {
874c7ed52f2STeresa Johnson   GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
875c7ed52f2STeresa Johnson   if (DGV)
876c7ed52f2STeresa Johnson     Visibility = isLessConstraining(Visibility, DGV->getVisibility())
877c7ed52f2STeresa Johnson                      ? DGV->getVisibility()
878c7ed52f2STeresa Johnson                      : Visibility;
879c7ed52f2STeresa Johnson   // For promoted locals, mark them hidden so that they can later be
880c7ed52f2STeresa Johnson   // stripped from the symbol table to reduce bloat.
881c7ed52f2STeresa Johnson   if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
882c7ed52f2STeresa Johnson     Visibility = GlobalValue::HiddenVisibility;
883c7ed52f2STeresa Johnson   NewGV->setVisibility(Visibility);
884c7ed52f2STeresa Johnson }
885c7ed52f2STeresa Johnson 
886c7ed52f2STeresa Johnson GlobalValue *ModuleLinker::copyGlobalValueProto(TypeMapTy &TypeMap,
887c7ed52f2STeresa Johnson                                                 const GlobalValue *SGV,
888c7ed52f2STeresa Johnson                                                 const GlobalValue *DGV) {
889ef23711eSRafael Espindola   GlobalValue *NewGV;
890ef23711eSRafael Espindola   if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))
891c7ed52f2STeresa Johnson     NewGV = copyGlobalVariableProto(TypeMap, SGVar);
892ef23711eSRafael Espindola   else if (auto *SF = dyn_cast<Function>(SGV))
893c7ed52f2STeresa Johnson     NewGV = copyFunctionProto(TypeMap, SF);
894ef23711eSRafael Espindola   else
895c7ed52f2STeresa Johnson     NewGV = copyGlobalAliasProto(TypeMap, cast<GlobalAlias>(SGV));
896ef23711eSRafael Espindola   copyGVAttributes(NewGV, SGV);
897c7ed52f2STeresa Johnson   setVisibility(NewGV, SGV, DGV);
898ef23711eSRafael Espindola   return NewGV;
899ef23711eSRafael Espindola }
900ef23711eSRafael Espindola 
901ef23711eSRafael Espindola Value *ValueMaterializerTy::materializeValueFor(Value *V) {
902ef23711eSRafael Espindola   auto *SGV = dyn_cast<GlobalValue>(V);
903ef23711eSRafael Espindola   if (!SGV)
904ef23711eSRafael Espindola     return nullptr;
905ef23711eSRafael Espindola 
9061063293aSTeresa Johnson   // If we are done linking global value bodies (i.e. we are performing
9071063293aSTeresa Johnson   // metadata linking), don't link in the global value due to this
9081063293aSTeresa Johnson   // reference, simply map it to null.
9091063293aSTeresa Johnson   if (ModLinker->doneLinkingBodies())
9101063293aSTeresa Johnson     return nullptr;
9111063293aSTeresa Johnson 
912c7ed52f2STeresa Johnson   GlobalValue *DGV = ModLinker->copyGlobalValueProto(TypeMap, SGV);
913ef23711eSRafael Espindola 
914ef23711eSRafael Espindola   if (Comdat *SC = SGV->getComdat()) {
915ef23711eSRafael Espindola     if (auto *DGO = dyn_cast<GlobalObject>(DGV)) {
916ef23711eSRafael Espindola       Comdat *DC = DstM->getOrInsertComdat(SC->getName());
917ef23711eSRafael Espindola       DGO->setComdat(DC);
918ef23711eSRafael Espindola     }
919ef23711eSRafael Espindola   }
920ef23711eSRafael Espindola 
921ef23711eSRafael Espindola   LazilyLinkGlobalValues.push_back(SGV);
922ef23711eSRafael Espindola   return DGV;
923f6f121e2SJames Molloy }
924f6f121e2SJames Molloy 
925dad0a645SDavid Majnemer bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
926dad0a645SDavid Majnemer                                    const GlobalVariable *&GVar) {
927dad0a645SDavid Majnemer   const GlobalValue *GVal = M->getNamedValue(ComdatName);
928dad0a645SDavid Majnemer   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
929dad0a645SDavid Majnemer     GVal = GA->getBaseObject();
930dad0a645SDavid Majnemer     if (!GVal)
931dad0a645SDavid Majnemer       // We cannot resolve the size of the aliasee yet.
932dad0a645SDavid Majnemer       return emitError("Linking COMDATs named '" + ComdatName +
933dad0a645SDavid Majnemer                        "': COMDAT key involves incomputable alias size.");
934dad0a645SDavid Majnemer   }
935dad0a645SDavid Majnemer 
936dad0a645SDavid Majnemer   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
937dad0a645SDavid Majnemer   if (!GVar)
938dad0a645SDavid Majnemer     return emitError(
939dad0a645SDavid Majnemer         "Linking COMDATs named '" + ComdatName +
940dad0a645SDavid Majnemer         "': GlobalVariable required for data dependent selection!");
941dad0a645SDavid Majnemer 
942dad0a645SDavid Majnemer   return false;
943dad0a645SDavid Majnemer }
944dad0a645SDavid Majnemer 
945dad0a645SDavid Majnemer bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
946dad0a645SDavid Majnemer                                                  Comdat::SelectionKind Src,
947dad0a645SDavid Majnemer                                                  Comdat::SelectionKind Dst,
948dad0a645SDavid Majnemer                                                  Comdat::SelectionKind &Result,
949dad0a645SDavid Majnemer                                                  bool &LinkFromSrc) {
950dad0a645SDavid Majnemer   // The ability to mix Comdat::SelectionKind::Any with
951dad0a645SDavid Majnemer   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
952dad0a645SDavid Majnemer   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
953dad0a645SDavid Majnemer                          Dst == Comdat::SelectionKind::Largest;
954dad0a645SDavid Majnemer   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
955dad0a645SDavid Majnemer                          Src == Comdat::SelectionKind::Largest;
956dad0a645SDavid Majnemer   if (DstAnyOrLargest && SrcAnyOrLargest) {
957dad0a645SDavid Majnemer     if (Dst == Comdat::SelectionKind::Largest ||
958dad0a645SDavid Majnemer         Src == Comdat::SelectionKind::Largest)
959dad0a645SDavid Majnemer       Result = Comdat::SelectionKind::Largest;
960dad0a645SDavid Majnemer     else
961dad0a645SDavid Majnemer       Result = Comdat::SelectionKind::Any;
962dad0a645SDavid Majnemer   } else if (Src == Dst) {
963dad0a645SDavid Majnemer     Result = Dst;
964dad0a645SDavid Majnemer   } else {
965dad0a645SDavid Majnemer     return emitError("Linking COMDATs named '" + ComdatName +
966dad0a645SDavid Majnemer                      "': invalid selection kinds!");
967dad0a645SDavid Majnemer   }
968dad0a645SDavid Majnemer 
969dad0a645SDavid Majnemer   switch (Result) {
970dad0a645SDavid Majnemer   case Comdat::SelectionKind::Any:
971dad0a645SDavid Majnemer     // Go with Dst.
972dad0a645SDavid Majnemer     LinkFromSrc = false;
973dad0a645SDavid Majnemer     break;
974dad0a645SDavid Majnemer   case Comdat::SelectionKind::NoDuplicates:
975dad0a645SDavid Majnemer     return emitError("Linking COMDATs named '" + ComdatName +
976dad0a645SDavid Majnemer                      "': noduplicates has been violated!");
977dad0a645SDavid Majnemer   case Comdat::SelectionKind::ExactMatch:
978dad0a645SDavid Majnemer   case Comdat::SelectionKind::Largest:
979dad0a645SDavid Majnemer   case Comdat::SelectionKind::SameSize: {
980dad0a645SDavid Majnemer     const GlobalVariable *DstGV;
981dad0a645SDavid Majnemer     const GlobalVariable *SrcGV;
982dad0a645SDavid Majnemer     if (getComdatLeader(DstM, ComdatName, DstGV) ||
983dad0a645SDavid Majnemer         getComdatLeader(SrcM, ComdatName, SrcGV))
984dad0a645SDavid Majnemer       return true;
985dad0a645SDavid Majnemer 
98646a43556SMehdi Amini     const DataLayout &DstDL = DstM->getDataLayout();
98746a43556SMehdi Amini     const DataLayout &SrcDL = SrcM->getDataLayout();
988dad0a645SDavid Majnemer     uint64_t DstSize =
98946a43556SMehdi Amini         DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
990dad0a645SDavid Majnemer     uint64_t SrcSize =
99146a43556SMehdi Amini         SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
992dad0a645SDavid Majnemer     if (Result == Comdat::SelectionKind::ExactMatch) {
993dad0a645SDavid Majnemer       if (SrcGV->getInitializer() != DstGV->getInitializer())
994dad0a645SDavid Majnemer         return emitError("Linking COMDATs named '" + ComdatName +
995dad0a645SDavid Majnemer                          "': ExactMatch violated!");
996dad0a645SDavid Majnemer       LinkFromSrc = false;
997dad0a645SDavid Majnemer     } else if (Result == Comdat::SelectionKind::Largest) {
998dad0a645SDavid Majnemer       LinkFromSrc = SrcSize > DstSize;
999dad0a645SDavid Majnemer     } else if (Result == Comdat::SelectionKind::SameSize) {
1000dad0a645SDavid Majnemer       if (SrcSize != DstSize)
1001dad0a645SDavid Majnemer         return emitError("Linking COMDATs named '" + ComdatName +
1002dad0a645SDavid Majnemer                          "': SameSize violated!");
1003dad0a645SDavid Majnemer       LinkFromSrc = false;
1004dad0a645SDavid Majnemer     } else {
1005dad0a645SDavid Majnemer       llvm_unreachable("unknown selection kind");
1006dad0a645SDavid Majnemer     }
1007dad0a645SDavid Majnemer     break;
1008dad0a645SDavid Majnemer   }
1009dad0a645SDavid Majnemer   }
1010dad0a645SDavid Majnemer 
1011dad0a645SDavid Majnemer   return false;
1012dad0a645SDavid Majnemer }
1013dad0a645SDavid Majnemer 
1014dad0a645SDavid Majnemer bool ModuleLinker::getComdatResult(const Comdat *SrcC,
1015dad0a645SDavid Majnemer                                    Comdat::SelectionKind &Result,
1016dad0a645SDavid Majnemer                                    bool &LinkFromSrc) {
1017b16196a3SRafael Espindola   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
1018dad0a645SDavid Majnemer   StringRef ComdatName = SrcC->getName();
1019dad0a645SDavid Majnemer   Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
1020dad0a645SDavid Majnemer   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
10212ef3f299SRafael Espindola 
1022b16196a3SRafael Espindola   if (DstCI == ComdatSymTab.end()) {
1023b16196a3SRafael Espindola     // Use the comdat if it is only available in one of the modules.
1024b16196a3SRafael Espindola     LinkFromSrc = true;
1025b16196a3SRafael Espindola     Result = SSK;
10262ef3f299SRafael Espindola     return false;
1027b16196a3SRafael Espindola   }
10282ef3f299SRafael Espindola 
1029dad0a645SDavid Majnemer   const Comdat *DstC = &DstCI->second;
1030dad0a645SDavid Majnemer   Comdat::SelectionKind DSK = DstC->getSelectionKind();
10312ef3f299SRafael Espindola   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
10322ef3f299SRafael Espindola                                        LinkFromSrc);
1033dad0a645SDavid Majnemer }
1034f6f121e2SJames Molloy 
1035d12b4a33SRafael Espindola bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
1036d12b4a33SRafael Espindola                                         const GlobalValue &Dest,
1037dbb0bd1aSRafael Espindola                                         const GlobalValue &Src) {
1038e868123dSDuncan P. N. Exon Smith   // Should we unconditionally use the Src?
1039020d4fb1SArtem Belevich   if (shouldOverrideFromSrc()) {
1040e868123dSDuncan P. N. Exon Smith     LinkFromSrc = true;
1041e868123dSDuncan P. N. Exon Smith     return false;
1042e868123dSDuncan P. N. Exon Smith   }
1043e868123dSDuncan P. N. Exon Smith 
1044778fcc77SRafael Espindola   // We always have to add Src if it has appending linkage.
1045778fcc77SRafael Espindola   if (Src.hasAppendingLinkage()) {
1046c7ed52f2STeresa Johnson     // Caller should have already determined that we can't link from source
1047c7ed52f2STeresa Johnson     // when importing (see comments in linkGlobalValueProto).
1048c7ed52f2STeresa Johnson     assert(!isPerformingImport());
1049778fcc77SRafael Espindola     LinkFromSrc = true;
1050778fcc77SRafael Espindola     return false;
1051778fcc77SRafael Espindola   }
1052778fcc77SRafael Espindola 
1053d4bcefc7SRafael Espindola   bool SrcIsDeclaration = Src.isDeclarationForLinker();
1054d4bcefc7SRafael Espindola   bool DestIsDeclaration = Dest.isDeclarationForLinker();
1055dbb0bd1aSRafael Espindola 
1056c7ed52f2STeresa Johnson   if (isPerformingImport()) {
1057c7ed52f2STeresa Johnson     if (isa<Function>(&Src)) {
1058c7ed52f2STeresa Johnson       // For functions, LinkFromSrc iff this is the function requested
1059c7ed52f2STeresa Johnson       // for importing. For variables, decide below normally.
1060c7ed52f2STeresa Johnson       LinkFromSrc = (&Src == ImportFunction);
1061c7ed52f2STeresa Johnson       return false;
1062c7ed52f2STeresa Johnson     }
1063c7ed52f2STeresa Johnson 
1064c7ed52f2STeresa Johnson     // Check if this is an alias with an already existing definition
1065c7ed52f2STeresa Johnson     // in Dest, which must have come from a prior importing pass from
1066c7ed52f2STeresa Johnson     // the same Src module. Unlike imported function and variable
1067c7ed52f2STeresa Johnson     // definitions, which are imported as available_externally and are
1068c7ed52f2STeresa Johnson     // not definitions for the linker, that is not a valid linkage for
1069c7ed52f2STeresa Johnson     // imported aliases which must be definitions. Simply use the existing
1070c7ed52f2STeresa Johnson     // Dest copy.
1071c7ed52f2STeresa Johnson     if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
1072c7ed52f2STeresa Johnson       assert(isa<GlobalAlias>(&Dest));
1073c7ed52f2STeresa Johnson       LinkFromSrc = false;
1074c7ed52f2STeresa Johnson       return false;
1075c7ed52f2STeresa Johnson     }
1076c7ed52f2STeresa Johnson   }
1077c7ed52f2STeresa Johnson 
1078dbb0bd1aSRafael Espindola   if (SrcIsDeclaration) {
1079dbb0bd1aSRafael Espindola     // If Src is external or if both Src & Dest are external..  Just link the
1080dbb0bd1aSRafael Espindola     // external globals, we aren't adding anything.
1081d12b4a33SRafael Espindola     if (Src.hasDLLImportStorageClass()) {
1082dbb0bd1aSRafael Espindola       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
1083d12b4a33SRafael Espindola       LinkFromSrc = DestIsDeclaration;
1084d12b4a33SRafael Espindola       return false;
1085d12b4a33SRafael Espindola     }
1086dbb0bd1aSRafael Espindola     // If the Dest is weak, use the source linkage.
1087d12b4a33SRafael Espindola     LinkFromSrc = Dest.hasExternalWeakLinkage();
1088d12b4a33SRafael Espindola     return false;
1089dbb0bd1aSRafael Espindola   }
1090dbb0bd1aSRafael Espindola 
1091d12b4a33SRafael Espindola   if (DestIsDeclaration) {
1092dbb0bd1aSRafael Espindola     // If Dest is external but Src is not:
1093d12b4a33SRafael Espindola     LinkFromSrc = true;
1094d12b4a33SRafael Espindola     return false;
1095d12b4a33SRafael Espindola   }
1096dbb0bd1aSRafael Espindola 
10970910605aSRafael Espindola   if (Src.hasCommonLinkage()) {
1098d12b4a33SRafael Espindola     if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
1099d12b4a33SRafael Espindola       LinkFromSrc = true;
11000910605aSRafael Espindola       return false;
1101d12b4a33SRafael Espindola     }
1102d12b4a33SRafael Espindola 
1103d12b4a33SRafael Espindola     if (!Dest.hasCommonLinkage()) {
1104d12b4a33SRafael Espindola       LinkFromSrc = false;
1105d12b4a33SRafael Espindola       return false;
1106d12b4a33SRafael Espindola     }
11070910605aSRafael Espindola 
1108a28d91d8SMehdi Amini     const DataLayout &DL = Dest.getParent()->getDataLayout();
11090910605aSRafael Espindola     uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
11100910605aSRafael Espindola     uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
1111d12b4a33SRafael Espindola     LinkFromSrc = SrcSize > DestSize;
1112d12b4a33SRafael Espindola     return false;
11130910605aSRafael Espindola   }
11140910605aSRafael Espindola 
1115dbb0bd1aSRafael Espindola   if (Src.isWeakForLinker()) {
1116dbb0bd1aSRafael Espindola     assert(!Dest.hasExternalWeakLinkage());
1117dbb0bd1aSRafael Espindola     assert(!Dest.hasAvailableExternallyLinkage());
11180910605aSRafael Espindola 
1119d12b4a33SRafael Espindola     if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
1120d12b4a33SRafael Espindola       LinkFromSrc = true;
1121d12b4a33SRafael Espindola       return false;
1122d12b4a33SRafael Espindola     }
1123dbb0bd1aSRafael Espindola 
1124d12b4a33SRafael Espindola     LinkFromSrc = false;
11250910605aSRafael Espindola     return false;
1126dbb0bd1aSRafael Espindola   }
1127dbb0bd1aSRafael Espindola 
1128dbb0bd1aSRafael Espindola   if (Dest.isWeakForLinker()) {
1129dbb0bd1aSRafael Espindola     assert(Src.hasExternalLinkage());
1130d12b4a33SRafael Espindola     LinkFromSrc = true;
1131d12b4a33SRafael Espindola     return false;
1132dbb0bd1aSRafael Espindola   }
1133dbb0bd1aSRafael Espindola 
1134dbb0bd1aSRafael Espindola   assert(!Src.hasExternalWeakLinkage());
1135dbb0bd1aSRafael Espindola   assert(!Dest.hasExternalWeakLinkage());
1136dbb0bd1aSRafael Espindola   assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
1137dbb0bd1aSRafael Espindola          "Unexpected linkage type!");
1138dbb0bd1aSRafael Espindola   return emitError("Linking globals named '" + Src.getName() +
1139dbb0bd1aSRafael Espindola                    "': symbol multiply defined!");
1140dbb0bd1aSRafael Espindola }
1141dbb0bd1aSRafael Espindola 
114218c89411SRafael Espindola /// Loop over all of the linked values to compute type mappings.  For example,
114318c89411SRafael Espindola /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
114418c89411SRafael Espindola /// types 'Foo' but one got renamed when the module was loaded into the same
114518c89411SRafael Espindola /// LLVMContext.
1146b1ed91f3SChris Lattner void ModuleLinker::computeTypeMapping() {
1147c8a476eeSRafael Espindola   for (GlobalValue &SGV : SrcM->globals()) {
1148c8a476eeSRafael Espindola     GlobalValue *DGV = getLinkedToGlobal(&SGV);
1149c8a476eeSRafael Espindola     if (!DGV)
1150c8a476eeSRafael Espindola       continue;
1151b1ed91f3SChris Lattner 
1152c8a476eeSRafael Espindola     if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
1153c8a476eeSRafael Espindola       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1154b1ed91f3SChris Lattner       continue;
11555c310be0SDevang Patel     }
11565c310be0SDevang Patel 
1157b1ed91f3SChris Lattner     // Unify the element type of appending arrays.
1158b1ed91f3SChris Lattner     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
1159c8a476eeSRafael Espindola     ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
1160b1ed91f3SChris Lattner     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
1161b1ed91f3SChris Lattner   }
116234fc5a0eSChris Lattner 
1163c8a476eeSRafael Espindola   for (GlobalValue &SGV : *SrcM) {
1164c8a476eeSRafael Espindola     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1165c8a476eeSRafael Espindola       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1166b1ed91f3SChris Lattner   }
1167e79f4c77SAnton Korobeynikov 
1168e96d7eb8SRafael Espindola   for (GlobalValue &SGV : SrcM->aliases()) {
1169e96d7eb8SRafael Espindola     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1170e96d7eb8SRafael Espindola       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1171e96d7eb8SRafael Espindola   }
1172e96d7eb8SRafael Espindola 
1173d48b7783SBill Wendling   // Incorporate types by name, scanning all the types in the source module.
1174d48b7783SBill Wendling   // At this point, the destination module may have a type "%foo = { i32 }" for
11752b3f61afSBill Wendling   // example.  When the source module got loaded into the same LLVMContext, if
11762b3f61afSBill Wendling   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
11772fa1e43aSRafael Espindola   std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
11782fa1e43aSRafael Espindola   for (StructType *ST : Types) {
11794d4c9387SRafael Espindola     if (!ST->hasName())
11804d4c9387SRafael Espindola       continue;
11812b3f61afSBill Wendling 
11822b3f61afSBill Wendling     // Check to see if there is a dot in the name followed by a digit.
1183d48b7783SBill Wendling     size_t DotPos = ST->getName().rfind('.');
1184d48b7783SBill Wendling     if (DotPos == 0 || DotPos == StringRef::npos ||
118583c74e9fSGuy Benyei         ST->getName().back() == '.' ||
118683c74e9fSGuy Benyei         !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
1187d48b7783SBill Wendling       continue;
11882b3f61afSBill Wendling 
11892b3f61afSBill Wendling     // Check to see if the destination module has a struct with the prefix name.
1190973b3612SRafael Espindola     StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos));
1191973b3612SRafael Espindola     if (!DST)
1192973b3612SRafael Espindola       continue;
1193973b3612SRafael Espindola 
11942b3f61afSBill Wendling     // Don't use it if this actually came from the source module. They're in
11958737480dSBill Wendling     // the same LLVMContext after all. Also don't use it unless the type is
11968737480dSBill Wendling     // actually used in the destination module. This can happen in situations
11978737480dSBill Wendling     // like this:
11988737480dSBill Wendling     //
11998737480dSBill Wendling     //      Module A                         Module B
12008737480dSBill Wendling     //      --------                         --------
12018737480dSBill Wendling     //   %Z = type { %A }                %B = type { %C.1 }
12028737480dSBill Wendling     //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
12038737480dSBill Wendling     //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
12048737480dSBill Wendling     //   %C = type { i8* }               %B.3 = type { %C.1 }
12058737480dSBill Wendling     //
12068737480dSBill Wendling     // When we link Module B with Module A, the '%B' in Module B is
12078737480dSBill Wendling     // used. However, that would then use '%C.1'. But when we process '%C.1',
12088737480dSBill Wendling     // we prefer to take the '%C' version. So we are then left with both
12098737480dSBill Wendling     // '%C.1' and '%C' being used for the same types. This leads to some
12108737480dSBill Wendling     // variables using one type and some using the other.
121131ad468dSRafael Espindola     if (TypeMap.DstStructTypesSet.hasType(DST))
12122b3f61afSBill Wendling       TypeMap.addTypeMapping(DST, ST);
12132b3f61afSBill Wendling   }
12142b3f61afSBill Wendling 
1215b1ed91f3SChris Lattner   // Now that we have discovered all of the type equivalences, get a body for
1216b1ed91f3SChris Lattner   // any 'opaque' types in the dest module that are now resolved.
1217b1ed91f3SChris Lattner   TypeMap.linkDefinedTypeBodies();
1218b1ed91f3SChris Lattner }
1219c08e7a00SChris Lattner 
122009d84addSDuncan P. N. Exon Smith static void upgradeGlobalArray(GlobalVariable *GV) {
122109d84addSDuncan P. N. Exon Smith   ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType());
122209d84addSDuncan P. N. Exon Smith   StructType *OldTy = cast<StructType>(ATy->getElementType());
122309d84addSDuncan P. N. Exon Smith   assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements");
122409d84addSDuncan P. N. Exon Smith 
122509d84addSDuncan P. N. Exon Smith   // Get the upgraded 3 element type.
122609d84addSDuncan P. N. Exon Smith   PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
122709d84addSDuncan P. N. Exon Smith   Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1),
122809d84addSDuncan P. N. Exon Smith                   VoidPtrTy};
122909d84addSDuncan P. N. Exon Smith   StructType *NewTy = StructType::get(GV->getContext(), Tys, false);
123009d84addSDuncan P. N. Exon Smith 
123109d84addSDuncan P. N. Exon Smith   // Build new constants with a null third field filled in.
123209d84addSDuncan P. N. Exon Smith   Constant *OldInitC = GV->getInitializer();
123309d84addSDuncan P. N. Exon Smith   ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC);
123409d84addSDuncan P. N. Exon Smith   if (!OldInit && !isa<ConstantAggregateZero>(OldInitC))
123509d84addSDuncan P. N. Exon Smith     // Invalid initializer; give up.
123609d84addSDuncan P. N. Exon Smith     return;
123709d84addSDuncan P. N. Exon Smith   std::vector<Constant *> Initializers;
123809d84addSDuncan P. N. Exon Smith   if (OldInit && OldInit->getNumOperands()) {
123909d84addSDuncan P. N. Exon Smith     Value *Null = Constant::getNullValue(VoidPtrTy);
124009d84addSDuncan P. N. Exon Smith     for (Use &U : OldInit->operands()) {
124109d84addSDuncan P. N. Exon Smith       ConstantStruct *Init = cast<ConstantStruct>(U.get());
124209d84addSDuncan P. N. Exon Smith       Initializers.push_back(ConstantStruct::get(
124309d84addSDuncan P. N. Exon Smith           NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr));
124409d84addSDuncan P. N. Exon Smith     }
124509d84addSDuncan P. N. Exon Smith   }
124609d84addSDuncan P. N. Exon Smith   assert(Initializers.size() == ATy->getNumElements() &&
124709d84addSDuncan P. N. Exon Smith          "Failed to copy all array elements");
124809d84addSDuncan P. N. Exon Smith 
124909d84addSDuncan P. N. Exon Smith   // Replace the old GV with a new one.
125009d84addSDuncan P. N. Exon Smith   ATy = ArrayType::get(NewTy, Initializers.size());
125109d84addSDuncan P. N. Exon Smith   Constant *NewInit = ConstantArray::get(ATy, Initializers);
125209d84addSDuncan P. N. Exon Smith   GlobalVariable *NewGV = new GlobalVariable(
125309d84addSDuncan P. N. Exon Smith       *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
125409d84addSDuncan P. N. Exon Smith       GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
125509d84addSDuncan P. N. Exon Smith       GV->isExternallyInitialized());
125609d84addSDuncan P. N. Exon Smith   NewGV->copyAttributesFrom(GV);
125709d84addSDuncan P. N. Exon Smith   NewGV->takeName(GV);
125809d84addSDuncan P. N. Exon Smith   assert(GV->use_empty() && "program cannot use initializer list");
125909d84addSDuncan P. N. Exon Smith   GV->eraseFromParent();
126009d84addSDuncan P. N. Exon Smith }
126109d84addSDuncan P. N. Exon Smith 
126209d84addSDuncan P. N. Exon Smith void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
126309d84addSDuncan P. N. Exon Smith   // Look for the global arrays.
126409d84addSDuncan P. N. Exon Smith   auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name));
126509d84addSDuncan P. N. Exon Smith   if (!DstGV)
126609d84addSDuncan P. N. Exon Smith     return;
126709d84addSDuncan P. N. Exon Smith   auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
126809d84addSDuncan P. N. Exon Smith   if (!SrcGV)
126909d84addSDuncan P. N. Exon Smith     return;
127009d84addSDuncan P. N. Exon Smith 
127109d84addSDuncan P. N. Exon Smith   // Check if the types already match.
127209d84addSDuncan P. N. Exon Smith   auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
127309d84addSDuncan P. N. Exon Smith   auto *SrcTy =
127409d84addSDuncan P. N. Exon Smith       cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
127509d84addSDuncan P. N. Exon Smith   if (DstTy == SrcTy)
127609d84addSDuncan P. N. Exon Smith     return;
127709d84addSDuncan P. N. Exon Smith 
127809d84addSDuncan P. N. Exon Smith   // Grab the element types.  We can only upgrade an array of a two-field
127909d84addSDuncan P. N. Exon Smith   // struct.  Only bother if the other one has three-fields.
128009d84addSDuncan P. N. Exon Smith   auto *DstEltTy = cast<StructType>(DstTy->getElementType());
128109d84addSDuncan P. N. Exon Smith   auto *SrcEltTy = cast<StructType>(SrcTy->getElementType());
128209d84addSDuncan P. N. Exon Smith   if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) {
128309d84addSDuncan P. N. Exon Smith     upgradeGlobalArray(DstGV);
128409d84addSDuncan P. N. Exon Smith     return;
128509d84addSDuncan P. N. Exon Smith   }
128609d84addSDuncan P. N. Exon Smith   if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2)
128709d84addSDuncan P. N. Exon Smith     upgradeGlobalArray(SrcGV);
128809d84addSDuncan P. N. Exon Smith 
128909d84addSDuncan P. N. Exon Smith   // We can't upgrade any other differences.
129009d84addSDuncan P. N. Exon Smith }
129109d84addSDuncan P. N. Exon Smith 
129209d84addSDuncan P. N. Exon Smith void ModuleLinker::upgradeMismatchedGlobals() {
129309d84addSDuncan P. N. Exon Smith   upgradeMismatchedGlobalArray("llvm.global_ctors");
129409d84addSDuncan P. N. Exon Smith   upgradeMismatchedGlobalArray("llvm.global_dtors");
129509d84addSDuncan P. N. Exon Smith }
129609d84addSDuncan P. N. Exon Smith 
129718c89411SRafael Espindola /// If there were any appending global variables, link them together now.
129818c89411SRafael Espindola /// Return true on error.
1299b1ed91f3SChris Lattner bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
13003e8bc6a8SRafael Espindola                                          const GlobalVariable *SrcGV) {
1301d48b7783SBill Wendling 
1302b1ed91f3SChris Lattner   if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
1303b1ed91f3SChris Lattner     return emitError("Linking globals named '" + SrcGV->getName() +
1304b1ed91f3SChris Lattner            "': can only link appending global with another appending global!");
1305361e513dSReid Spencer 
1306b1ed91f3SChris Lattner   ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
1307b1ed91f3SChris Lattner   ArrayType *SrcTy =
1308b1ed91f3SChris Lattner     cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
1309b1ed91f3SChris Lattner   Type *EltTy = DstTy->getElementType();
1310b1ed91f3SChris Lattner 
1311b1ed91f3SChris Lattner   // Check to see that they two arrays agree on type.
1312b1ed91f3SChris Lattner   if (EltTy != SrcTy->getElementType())
1313b1ed91f3SChris Lattner     return emitError("Appending variables with different element types!");
1314b1ed91f3SChris Lattner   if (DstGV->isConstant() != SrcGV->isConstant())
1315b1ed91f3SChris Lattner     return emitError("Appending variables linked with different const'ness!");
1316b1ed91f3SChris Lattner 
1317b1ed91f3SChris Lattner   if (DstGV->getAlignment() != SrcGV->getAlignment())
1318b1ed91f3SChris Lattner     return emitError(
1319b1ed91f3SChris Lattner              "Appending variables with different alignment need to be linked!");
1320b1ed91f3SChris Lattner 
1321b1ed91f3SChris Lattner   if (DstGV->getVisibility() != SrcGV->getVisibility())
1322b1ed91f3SChris Lattner     return emitError(
1323b1ed91f3SChris Lattner             "Appending variables with different visibility need to be linked!");
1324b1ed91f3SChris Lattner 
1325fac3a018SRafael Espindola   if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
1326fac3a018SRafael Espindola     return emitError(
1327fac3a018SRafael Espindola         "Appending variables with different unnamed_addr need to be linked!");
1328fac3a018SRafael Espindola 
132964c1e180SRafael Espindola   if (StringRef(DstGV->getSection()) != SrcGV->getSection())
1330b1ed91f3SChris Lattner     return emitError(
1331b1ed91f3SChris Lattner           "Appending variables with different section name need to be linked!");
1332b1ed91f3SChris Lattner 
1333b1ed91f3SChris Lattner   uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
1334b1ed91f3SChris Lattner   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
1335b1ed91f3SChris Lattner 
1336b1ed91f3SChris Lattner   // Create the new global variable.
1337b1ed91f3SChris Lattner   GlobalVariable *NG =
1338b1ed91f3SChris Lattner     new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
13392617dcceSCraig Topper                        DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV,
1340cbe34b4cSHans Wennborg                        DstGV->getThreadLocalMode(),
1341b1ed91f3SChris Lattner                        DstGV->getType()->getAddressSpace());
1342b1ed91f3SChris Lattner 
1343b1ed91f3SChris Lattner   // Propagate alignment, visibility and section info.
1344b6af2f36SBill Wendling   copyGVAttributes(NG, DstGV);
1345b1ed91f3SChris Lattner 
1346b1ed91f3SChris Lattner   AppendingVarInfo AVI;
1347b1ed91f3SChris Lattner   AVI.NewGV = NG;
1348b1ed91f3SChris Lattner   AVI.DstInit = DstGV->getInitializer();
1349b1ed91f3SChris Lattner   AVI.SrcInit = SrcGV->getInitializer();
1350b1ed91f3SChris Lattner   AppendingVars.push_back(AVI);
1351b1ed91f3SChris Lattner 
1352b1ed91f3SChris Lattner   // Replace any uses of the two global variables with uses of the new
1353b1ed91f3SChris Lattner   // global.
1354b1ed91f3SChris Lattner   ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
1355b1ed91f3SChris Lattner 
1356b1ed91f3SChris Lattner   DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
1357b1ed91f3SChris Lattner   DstGV->eraseFromParent();
1358b1ed91f3SChris Lattner 
1359cbb91408STanya Lattner   // Track the source variable so we don't try to link it.
1360cbb91408STanya Lattner   DoNotLinkFromSource.insert(SrcGV);
1361cbb91408STanya Lattner 
1362b1ed91f3SChris Lattner   return false;
1363b1ed91f3SChris Lattner }
1364b1ed91f3SChris Lattner 
1365778fcc77SRafael Espindola bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1366b1ed91f3SChris Lattner   GlobalValue *DGV = getLinkedToGlobal(SGV);
1367b1ed91f3SChris Lattner 
1368778fcc77SRafael Espindola   // Handle the ultra special appending linkage case first.
1369c7ed52f2STeresa Johnson   assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
1370c7ed52f2STeresa Johnson   if (SGV->hasAppendingLinkage() && isPerformingImport()) {
1371c7ed52f2STeresa Johnson     // Don't want to append to global_ctors list, for example, when we
1372c7ed52f2STeresa Johnson     // are importing for ThinLTO, otherwise the global ctors and dtors
1373c7ed52f2STeresa Johnson     // get executed multiple times for local variables (the latter causing
1374c7ed52f2STeresa Johnson     // double frees).
1375c7ed52f2STeresa Johnson     DoNotLinkFromSource.insert(SGV);
1376c7ed52f2STeresa Johnson     return false;
1377c7ed52f2STeresa Johnson   }
1378778fcc77SRafael Espindola   if (DGV && DGV->hasAppendingLinkage())
1379778fcc77SRafael Espindola     return linkAppendingVarProto(cast<GlobalVariable>(DGV),
1380778fcc77SRafael Espindola                                  cast<GlobalVariable>(SGV));
1381778fcc77SRafael Espindola 
1382778fcc77SRafael Espindola   bool LinkFromSrc = true;
1383778fcc77SRafael Espindola   Comdat *C = nullptr;
1384778fcc77SRafael Espindola   bool HasUnnamedAddr = SGV->hasUnnamedAddr();
1385778fcc77SRafael Espindola 
1386dad0a645SDavid Majnemer   if (const Comdat *SC = SGV->getComdat()) {
1387dad0a645SDavid Majnemer     Comdat::SelectionKind SK;
1388dad0a645SDavid Majnemer     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1389778fcc77SRafael Espindola     C = DstM->getOrInsertComdat(SC->getName());
1390778fcc77SRafael Espindola     C->setSelectionKind(SK);
13912d5fb8caSTeresa Johnson     ComdatMembers[SC].push_back(SGV);
1392778fcc77SRafael Espindola   } else if (DGV) {
1393778fcc77SRafael Espindola     if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
1394fc61de39SChris Lattner       return true;
1395dad0a645SDavid Majnemer   }
139634fc5a0eSChris Lattner 
1397dad0a645SDavid Majnemer   if (!LinkFromSrc) {
1398cbb91408STanya Lattner     // Track the source global so that we don't attempt to copy it over when
1399cbb91408STanya Lattner     // processing global initializers.
1400cbb91408STanya Lattner     DoNotLinkFromSource.insert(SGV);
1401cbb91408STanya Lattner 
1402778fcc77SRafael Espindola     if (DGV)
1403778fcc77SRafael Espindola       // Make sure to remember this mapping.
1404778fcc77SRafael Espindola       ValueMap[SGV] =
1405778fcc77SRafael Espindola           ConstantExpr::getBitCast(DGV, TypeMap.get(SGV->getType()));
1406778fcc77SRafael Espindola   }
1407778fcc77SRafael Espindola 
1408c7ed52f2STeresa Johnson   if (DGV)
1409778fcc77SRafael Espindola     HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1410778fcc77SRafael Espindola 
1411778fcc77SRafael Espindola   if (!LinkFromSrc && !DGV)
1412dad0a645SDavid Majnemer     return false;
1413778fcc77SRafael Espindola 
1414778fcc77SRafael Espindola   GlobalValue *NewGV;
141526c29511SRafael Espindola   if (!LinkFromSrc) {
141626c29511SRafael Espindola     NewGV = DGV;
1417c7ed52f2STeresa Johnson     // When linking from source we setVisibility from copyGlobalValueProto.
1418c7ed52f2STeresa Johnson     setVisibility(NewGV, SGV, DGV);
141926c29511SRafael Espindola   } else {
1420ef23711eSRafael Espindola     // If the GV is to be lazily linked, don't create it just yet.
1421ef23711eSRafael Espindola     // The ValueMaterializerTy will deal with creating it if it's used.
1422c7ed52f2STeresa Johnson     if (!DGV && !shouldOverrideFromSrc() && SGV != ImportFunction &&
1423e868123dSDuncan P. N. Exon Smith         (SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() ||
1424ef23711eSRafael Espindola          SGV->hasAvailableExternallyLinkage())) {
1425ef23711eSRafael Espindola       DoNotLinkFromSource.insert(SGV);
1426ef23711eSRafael Espindola       return false;
142726c29511SRafael Espindola     }
1428778fcc77SRafael Espindola 
1429020d4fb1SArtem Belevich     // When we only want to link in unresolved dependencies, blacklist
1430020d4fb1SArtem Belevich     // the symbol unless unless DestM has a matching declaration (DGV).
1431020d4fb1SArtem Belevich     if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration())) {
1432020d4fb1SArtem Belevich       DoNotLinkFromSource.insert(SGV);
1433020d4fb1SArtem Belevich       return false;
1434020d4fb1SArtem Belevich     }
1435020d4fb1SArtem Belevich 
1436c7ed52f2STeresa Johnson     NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
14373cd8161cSTeresa Johnson 
14383cd8161cSTeresa Johnson     if (isPerformingImport() && !doImportAsDefinition(SGV))
14393cd8161cSTeresa Johnson       DoNotLinkFromSource.insert(SGV);
1440ef23711eSRafael Espindola   }
1441778fcc77SRafael Espindola 
1442778fcc77SRafael Espindola   NewGV->setUnnamedAddr(HasUnnamedAddr);
1443778fcc77SRafael Espindola 
1444778fcc77SRafael Espindola   if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
1445778fcc77SRafael Espindola     if (C)
1446778fcc77SRafael Espindola       NewGO->setComdat(C);
1447439835a6SRafael Espindola 
1448439835a6SRafael Espindola     if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
1449439835a6SRafael Espindola       NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
1450778fcc77SRafael Espindola   }
1451778fcc77SRafael Espindola 
1452879aeb77SRafael Espindola   if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) {
1453879aeb77SRafael Espindola     auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV);
1454879aeb77SRafael Espindola     auto *SGVar = dyn_cast<GlobalVariable>(SGV);
1455879aeb77SRafael Espindola     if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() &&
1456879aeb77SRafael Espindola         (!DGVar->isConstant() || !SGVar->isConstant()))
1457879aeb77SRafael Espindola       NewGVar->setConstant(false);
1458879aeb77SRafael Espindola   }
1459879aeb77SRafael Espindola 
1460778fcc77SRafael Espindola   // Make sure to remember this mapping.
1461778fcc77SRafael Espindola   if (NewGV != DGV) {
1462778fcc77SRafael Espindola     if (DGV) {
1463ad9d0ca8SRafael Espindola       DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
1464778fcc77SRafael Espindola       DGV->eraseFromParent();
1465778fcc77SRafael Espindola     }
1466778fcc77SRafael Espindola     ValueMap[SGV] = NewGV;
1467dad0a645SDavid Majnemer   }
14684e27567aSRafael Espindola 
1469fd38af2dSChandler Carruth   return false;
14704e27567aSRafael Espindola }
14714e27567aSRafael Espindola 
14723e8bc6a8SRafael Espindola static void getArrayElements(const Constant *C,
14733e8bc6a8SRafael Espindola                              SmallVectorImpl<Constant *> &Dest) {
14746705883aSChris Lattner   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
147500245f42SChris Lattner 
14766705883aSChris Lattner   for (unsigned i = 0; i != NumElements; ++i)
14776705883aSChris Lattner     Dest.push_back(C->getAggregateElement(i));
147800245f42SChris Lattner }
147900245f42SChris Lattner 
1480b1ed91f3SChris Lattner void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
1481b1ed91f3SChris Lattner   // Merge the initializer.
1482d31dc048SRafael Espindola   SmallVector<Constant *, 16> DstElements;
1483d31dc048SRafael Espindola   getArrayElements(AVI.DstInit, DstElements);
1484b1ed91f3SChris Lattner 
1485d31dc048SRafael Espindola   SmallVector<Constant *, 16> SrcElements;
1486d31dc048SRafael Espindola   getArrayElements(AVI.SrcInit, SrcElements);
148700245f42SChris Lattner 
1488b1ed91f3SChris Lattner   ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
1489d31dc048SRafael Espindola 
1490d31dc048SRafael Espindola   StringRef Name = AVI.NewGV->getName();
1491d31dc048SRafael Espindola   bool IsNewStructor =
1492d31dc048SRafael Espindola       (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") &&
1493d31dc048SRafael Espindola       cast<StructType>(NewType->getElementType())->getNumElements() == 3;
1494d31dc048SRafael Espindola 
1495d31dc048SRafael Espindola   for (auto *V : SrcElements) {
1496d31dc048SRafael Espindola     if (IsNewStructor) {
1497d31dc048SRafael Espindola       Constant *Key = V->getAggregateElement(2);
1498d31dc048SRafael Espindola       if (DoNotLinkFromSource.count(Key))
1499d31dc048SRafael Espindola         continue;
1500d31dc048SRafael Espindola     }
1501d31dc048SRafael Espindola     DstElements.push_back(
15024fb46cb8SDuncan P. N. Exon Smith         MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1503d31dc048SRafael Espindola   }
1504d31dc048SRafael Espindola   if (IsNewStructor) {
1505d31dc048SRafael Espindola     NewType = ArrayType::get(NewType->getElementType(), DstElements.size());
1506d31dc048SRafael Espindola     AVI.NewGV->mutateType(PointerType::get(NewType, 0));
1507d31dc048SRafael Espindola   }
1508d31dc048SRafael Espindola 
1509d31dc048SRafael Espindola   AVI.NewGV->setInitializer(ConstantArray::get(NewType, DstElements));
1510b1ed91f3SChris Lattner }
1511b1ed91f3SChris Lattner 
151218c89411SRafael Espindola /// Update the initializers in the Dest module now that all globals that may be
151318c89411SRafael Espindola /// referenced are in Dest.
1514ef23711eSRafael Espindola void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1515b1ed91f3SChris Lattner   // Figure out what the initializer looks like in the dest module.
15164fb46cb8SDuncan P. N. Exon Smith   Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap,
15174fb46cb8SDuncan P. N. Exon Smith                               RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1518b1ed91f3SChris Lattner }
1519b1ed91f3SChris Lattner 
152018c89411SRafael Espindola /// Copy the source function over into the dest function and fix up references
152118c89411SRafael Espindola /// to values. At this point we know that Dest is an external function, and
152218c89411SRafael Espindola /// that Src is not.
1523ef23711eSRafael Espindola bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
1524ef23711eSRafael Espindola   assert(Dst.isDeclaration() && !Src.isDeclaration());
1525361e513dSReid Spencer 
152640d7ebedSRafael Espindola   // Materialize if needed.
15273519da82SRafael Espindola   if (std::error_code EC = Src.materialize())
152840d7ebedSRafael Espindola     return emitError(EC.message());
152940d7ebedSRafael Espindola 
1530869d1ce8SRafael Espindola   // Link in the prefix data.
15313519da82SRafael Espindola   if (Src.hasPrefixData())
15324fb46cb8SDuncan P. N. Exon Smith     Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap,
15334fb46cb8SDuncan P. N. Exon Smith                                RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1534869d1ce8SRafael Espindola 
1535869d1ce8SRafael Espindola   // Link in the prologue data.
15363519da82SRafael Espindola   if (Src.hasPrologueData())
15374fb46cb8SDuncan P. N. Exon Smith     Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
15384fb46cb8SDuncan P. N. Exon Smith                                  RF_MoveDistinctMDs, &TypeMap,
15394fb46cb8SDuncan P. N. Exon Smith                                  &ValMaterializer));
1540869d1ce8SRafael Espindola 
15417fddeccbSDavid Majnemer   // Link in the personality function.
15427fddeccbSDavid Majnemer   if (Src.hasPersonalityFn())
15434fb46cb8SDuncan P. N. Exon Smith     Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
15444fb46cb8SDuncan P. N. Exon Smith                                   RF_MoveDistinctMDs, &TypeMap,
15454fb46cb8SDuncan P. N. Exon Smith                                   &ValMaterializer));
15467fddeccbSDavid Majnemer 
15477391dde1SChris Lattner   // Go through and convert function arguments over, remembering the mapping.
1548ef23711eSRafael Espindola   Function::arg_iterator DI = Dst.arg_begin();
15493519da82SRafael Espindola   for (Argument &Arg : Src.args()) {
1550a314d1acSRafael Espindola     DI->setName(Arg.getName());  // Copy the name over.
1551361e513dSReid Spencer 
1552b1ed91f3SChris Lattner     // Add a mapping to our mapping.
15539934b26bSDuncan P. N. Exon Smith     ValueMap[&Arg] = &*DI;
1554a314d1acSRafael Espindola     ++DI;
1555361e513dSReid Spencer   }
1556361e513dSReid Spencer 
1557c8d987b1SDuncan P. N. Exon Smith   // Copy over the metadata attachments.
1558c8d987b1SDuncan P. N. Exon Smith   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1559c8d987b1SDuncan P. N. Exon Smith   Src.getAllMetadata(MDs);
1560c8d987b1SDuncan P. N. Exon Smith   for (const auto &I : MDs)
15614fb46cb8SDuncan P. N. Exon Smith     Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs,
15624fb46cb8SDuncan P. N. Exon Smith                                          &TypeMap, &ValMaterializer));
1563c8d987b1SDuncan P. N. Exon Smith 
15642f0557d0SChris Lattner   // Splice the body of the source function into the dest function.
1565ef23711eSRafael Espindola   Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1566361e513dSReid Spencer 
1567361e513dSReid Spencer   // At this point, all of the instructions and values of the function are now
1568361e513dSReid Spencer   // copied over.  The only problem is that they are still referencing values in
1569361e513dSReid Spencer   // the Source function as operands.  Loop through all of the operands of the
157043f8d164SChris Lattner   // functions and patch them up to point to the local versions.
1571ef23711eSRafael Espindola   for (BasicBlock &BB : Dst)
1572a314d1acSRafael Espindola     for (Instruction &I : BB)
15734fb46cb8SDuncan P. N. Exon Smith       RemapInstruction(&I, ValueMap,
15744fb46cb8SDuncan P. N. Exon Smith                        RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap,
15759f8eff31SRafael Espindola                        &ValMaterializer);
1576cbb91408STanya Lattner 
15777391dde1SChris Lattner   // There is no need to map the arguments anymore.
15783519da82SRafael Espindola   for (Argument &Arg : Src.args())
1579a314d1acSRafael Espindola     ValueMap.erase(&Arg);
1580cbb91408STanya Lattner 
158197cb5657SEric Christopher   Src.dematerialize();
158240d7ebedSRafael Espindola   return false;
1583361e513dSReid Spencer }
1584361e513dSReid Spencer 
1585ef23711eSRafael Espindola void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1586ef23711eSRafael Espindola   Constant *Aliasee = Src.getAliasee();
15874fb46cb8SDuncan P. N. Exon Smith   Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap,
15884fb46cb8SDuncan P. N. Exon Smith                            &ValMaterializer);
1589ef23711eSRafael Espindola   Dst.setAliasee(Val);
15902c4a34aeSDavid Chisnall }
1591ef23711eSRafael Espindola 
1592ef23711eSRafael Espindola bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) {
1593ef23711eSRafael Espindola   Value *Dst = ValueMap[&Src];
1594ef23711eSRafael Espindola   assert(Dst);
15952d5fb8caSTeresa Johnson   if (const Comdat *SC = Src.getComdat()) {
15962d5fb8caSTeresa Johnson     // To ensure that we don't generate an incomplete comdat group,
15972d5fb8caSTeresa Johnson     // we must materialize and map in any other members that are not
15982d5fb8caSTeresa Johnson     // yet materialized in Dst, which also ensures their definitions
15992d5fb8caSTeresa Johnson     // are linked in. Otherwise, linkonce and other lazy linked GVs will
16002d5fb8caSTeresa Johnson     // not be materialized if they aren't referenced.
16012d5fb8caSTeresa Johnson     for (auto *SGV : ComdatMembers[SC]) {
16022d5fb8caSTeresa Johnson       if (ValueMap[SGV])
16032d5fb8caSTeresa Johnson         continue;
16042d5fb8caSTeresa Johnson       Value *NewV = ValMaterializer.materializeValueFor(SGV);
16052d5fb8caSTeresa Johnson       ValueMap[SGV] = NewV;
16062d5fb8caSTeresa Johnson     }
16072d5fb8caSTeresa Johnson   }
1608020d4fb1SArtem Belevich   if (shouldInternalizeLinkedSymbols())
1609020d4fb1SArtem Belevich     if (auto *DGV = dyn_cast<GlobalValue>(Dst))
1610020d4fb1SArtem Belevich       DGV->setLinkage(GlobalValue::InternalLinkage);
1611ef23711eSRafael Espindola   if (auto *F = dyn_cast<Function>(&Src))
1612ef23711eSRafael Espindola     return linkFunctionBody(cast<Function>(*Dst), *F);
1613ef23711eSRafael Espindola   if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1614ef23711eSRafael Espindola     linkGlobalInit(cast<GlobalVariable>(*Dst), *GVar);
1615ef23711eSRafael Espindola     return false;
1616b1ed91f3SChris Lattner   }
1617ef23711eSRafael Espindola   linkAliasBody(cast<GlobalAlias>(*Dst), cast<GlobalAlias>(Src));
1618ef23711eSRafael Espindola   return false;
1619cbb91408STanya Lattner }
1620b1ed91f3SChris Lattner 
162118c89411SRafael Espindola /// Insert all of the named MDNodes in Src into the Dest module.
1622b1ed91f3SChris Lattner void ModuleLinker::linkNamedMDNodes() {
162366f02413SBill Wendling   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
16243bf3f1f5SYaron Keren   for (const NamedMDNode &NMD : SrcM->named_metadata()) {
162566f02413SBill Wendling     // Don't link module flags here. Do them separately.
16263bf3f1f5SYaron Keren     if (&NMD == SrcModFlags)
16273bf3f1f5SYaron Keren       continue;
16283bf3f1f5SYaron Keren     NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName());
1629b1ed91f3SChris Lattner     // Add Src elements into Dest node.
16303bf3f1f5SYaron Keren     for (const MDNode *op : NMD.operands())
163183d03ddbSTeresa Johnson       DestNMD->addOperand(MapMetadata(
163283d03ddbSTeresa Johnson           op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues,
16334fb46cb8SDuncan P. N. Exon Smith           &TypeMap, &ValMaterializer));
1634b1ed91f3SChris Lattner   }
1635b1ed91f3SChris Lattner }
1636b1ed91f3SChris Lattner 
163718c89411SRafael Espindola /// Merge the linker flags in Src into the Dest module.
163866f02413SBill Wendling bool ModuleLinker::linkModuleFlagsMetadata() {
16390ec72bbcSDaniel Dunbar   // If the source module has no module flags, we are done.
164066f02413SBill Wendling   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
164166f02413SBill Wendling   if (!SrcModFlags) return false;
164266f02413SBill Wendling 
164366f02413SBill Wendling   // If the destination module doesn't have module flags yet, then just copy
164466f02413SBill Wendling   // over the source module's flags.
16450ec72bbcSDaniel Dunbar   NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
164666f02413SBill Wendling   if (DstModFlags->getNumOperands() == 0) {
164766f02413SBill Wendling     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
164866f02413SBill Wendling       DstModFlags->addOperand(SrcModFlags->getOperand(I));
164966f02413SBill Wendling 
165066f02413SBill Wendling     return false;
165166f02413SBill Wendling   }
165266f02413SBill Wendling 
16530ec72bbcSDaniel Dunbar   // First build a map of the existing module flags and requirements.
1654df55d8baSDuncan P. N. Exon Smith   DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
16550ec72bbcSDaniel Dunbar   SmallSetVector<MDNode*, 16> Requirements;
16560ec72bbcSDaniel Dunbar   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1657de36e804SDuncan P. N. Exon Smith     MDNode *Op = DstModFlags->getOperand(I);
16585bf8fef5SDuncan P. N. Exon Smith     ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
16590ec72bbcSDaniel Dunbar     MDString *ID = cast<MDString>(Op->getOperand(1));
16600ec72bbcSDaniel Dunbar 
16610ec72bbcSDaniel Dunbar     if (Behavior->getZExtValue() == Module::Require) {
16620ec72bbcSDaniel Dunbar       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
16630ec72bbcSDaniel Dunbar     } else {
1664df55d8baSDuncan P. N. Exon Smith       Flags[ID] = std::make_pair(Op, I);
16650ec72bbcSDaniel Dunbar     }
16660ec72bbcSDaniel Dunbar   }
16670ec72bbcSDaniel Dunbar 
16680ec72bbcSDaniel Dunbar   // Merge in the flags from the source module, and also collect its set of
16690ec72bbcSDaniel Dunbar   // requirements.
167066f02413SBill Wendling   bool HasErr = false;
16710ec72bbcSDaniel Dunbar   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1672de36e804SDuncan P. N. Exon Smith     MDNode *SrcOp = SrcModFlags->getOperand(I);
16735bf8fef5SDuncan P. N. Exon Smith     ConstantInt *SrcBehavior =
16745bf8fef5SDuncan P. N. Exon Smith         mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
16750ec72bbcSDaniel Dunbar     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1676df55d8baSDuncan P. N. Exon Smith     MDNode *DstOp;
1677df55d8baSDuncan P. N. Exon Smith     unsigned DstIndex;
1678df55d8baSDuncan P. N. Exon Smith     std::tie(DstOp, DstIndex) = Flags.lookup(ID);
16790ec72bbcSDaniel Dunbar     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
168066f02413SBill Wendling 
16810ec72bbcSDaniel Dunbar     // If this is a requirement, add it and continue.
16820ec72bbcSDaniel Dunbar     if (SrcBehaviorValue == Module::Require) {
16830ec72bbcSDaniel Dunbar       // If the destination module does not already have this requirement, add
16840ec72bbcSDaniel Dunbar       // it.
16850ec72bbcSDaniel Dunbar       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
16860ec72bbcSDaniel Dunbar         DstModFlags->addOperand(SrcOp);
16870ec72bbcSDaniel Dunbar       }
16880ec72bbcSDaniel Dunbar       continue;
16890ec72bbcSDaniel Dunbar     }
169066f02413SBill Wendling 
16910ec72bbcSDaniel Dunbar     // If there is no existing flag with this ID, just add it.
16920ec72bbcSDaniel Dunbar     if (!DstOp) {
1693df55d8baSDuncan P. N. Exon Smith       Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
16940ec72bbcSDaniel Dunbar       DstModFlags->addOperand(SrcOp);
16950ec72bbcSDaniel Dunbar       continue;
16960ec72bbcSDaniel Dunbar     }
169766f02413SBill Wendling 
16980ec72bbcSDaniel Dunbar     // Otherwise, perform a merge.
16995bf8fef5SDuncan P. N. Exon Smith     ConstantInt *DstBehavior =
17005bf8fef5SDuncan P. N. Exon Smith         mdconst::extract<ConstantInt>(DstOp->getOperand(0));
17010ec72bbcSDaniel Dunbar     unsigned DstBehaviorValue = DstBehavior->getZExtValue();
17020ec72bbcSDaniel Dunbar 
17030ec72bbcSDaniel Dunbar     // If either flag has override behavior, handle it first.
17040ec72bbcSDaniel Dunbar     if (DstBehaviorValue == Module::Override) {
17050ec72bbcSDaniel Dunbar       // Diagnose inconsistent flags which both have override behavior.
17060ec72bbcSDaniel Dunbar       if (SrcBehaviorValue == Module::Override &&
17070ec72bbcSDaniel Dunbar           SrcOp->getOperand(2) != DstOp->getOperand(2)) {
17080ec72bbcSDaniel Dunbar         HasErr |= emitError("linking module flags '" + ID->getString() +
17090ec72bbcSDaniel Dunbar                             "': IDs have conflicting override values");
17100ec72bbcSDaniel Dunbar       }
17110ec72bbcSDaniel Dunbar       continue;
17120ec72bbcSDaniel Dunbar     } else if (SrcBehaviorValue == Module::Override) {
17130ec72bbcSDaniel Dunbar       // Update the destination flag to that of the source.
1714df55d8baSDuncan P. N. Exon Smith       DstModFlags->setOperand(DstIndex, SrcOp);
1715df55d8baSDuncan P. N. Exon Smith       Flags[ID].first = SrcOp;
17160ec72bbcSDaniel Dunbar       continue;
17170ec72bbcSDaniel Dunbar     }
17180ec72bbcSDaniel Dunbar 
17190ec72bbcSDaniel Dunbar     // Diagnose inconsistent merge behavior types.
17200ec72bbcSDaniel Dunbar     if (SrcBehaviorValue != DstBehaviorValue) {
17210ec72bbcSDaniel Dunbar       HasErr |= emitError("linking module flags '" + ID->getString() +
172266f02413SBill Wendling                           "': IDs have conflicting behaviors");
17230ec72bbcSDaniel Dunbar       continue;
172466f02413SBill Wendling     }
172566f02413SBill Wendling 
1726df55d8baSDuncan P. N. Exon Smith     auto replaceDstValue = [&](MDNode *New) {
1727df55d8baSDuncan P. N. Exon Smith       Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1728df55d8baSDuncan P. N. Exon Smith       MDNode *Flag = MDNode::get(DstM->getContext(), FlagOps);
1729df55d8baSDuncan P. N. Exon Smith       DstModFlags->setOperand(DstIndex, Flag);
1730df55d8baSDuncan P. N. Exon Smith       Flags[ID].first = Flag;
1731df55d8baSDuncan P. N. Exon Smith     };
1732df55d8baSDuncan P. N. Exon Smith 
17330ec72bbcSDaniel Dunbar     // Perform the merge for standard behavior types.
17340ec72bbcSDaniel Dunbar     switch (SrcBehaviorValue) {
17350ec72bbcSDaniel Dunbar     case Module::Require:
17362a30d788SCraig Topper     case Module::Override: llvm_unreachable("not possible");
17370ec72bbcSDaniel Dunbar     case Module::Error: {
17380ec72bbcSDaniel Dunbar       // Emit an error if the values differ.
17390ec72bbcSDaniel Dunbar       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
17400ec72bbcSDaniel Dunbar         HasErr |= emitError("linking module flags '" + ID->getString() +
17410ec72bbcSDaniel Dunbar                             "': IDs have conflicting values");
174266f02413SBill Wendling       }
17430ec72bbcSDaniel Dunbar       continue;
174466f02413SBill Wendling     }
17450ec72bbcSDaniel Dunbar     case Module::Warning: {
17460ec72bbcSDaniel Dunbar       // Emit a warning if the values differ.
17470ec72bbcSDaniel Dunbar       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1748d12b4a33SRafael Espindola         emitWarning("linking module flags '" + ID->getString() +
1749d12b4a33SRafael Espindola                     "': IDs have conflicting values");
1750e17f3708SEli Bendersky       }
17510ec72bbcSDaniel Dunbar       continue;
17520ec72bbcSDaniel Dunbar     }
1753d77d9fb0SDaniel Dunbar     case Module::Append: {
1754d77d9fb0SDaniel Dunbar       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1755d77d9fb0SDaniel Dunbar       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
17565bf8fef5SDuncan P. N. Exon Smith       SmallVector<Metadata *, 8> MDs;
17575bf8fef5SDuncan P. N. Exon Smith       MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
17586cd780ffSBenjamin Kramer       MDs.append(DstValue->op_begin(), DstValue->op_end());
17596cd780ffSBenjamin Kramer       MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1760df55d8baSDuncan P. N. Exon Smith 
1761df55d8baSDuncan P. N. Exon Smith       replaceDstValue(MDNode::get(DstM->getContext(), MDs));
1762d77d9fb0SDaniel Dunbar       break;
1763d77d9fb0SDaniel Dunbar     }
1764d77d9fb0SDaniel Dunbar     case Module::AppendUnique: {
17655bf8fef5SDuncan P. N. Exon Smith       SmallSetVector<Metadata *, 16> Elts;
1766d77d9fb0SDaniel Dunbar       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1767d77d9fb0SDaniel Dunbar       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
17686cd780ffSBenjamin Kramer       Elts.insert(DstValue->op_begin(), DstValue->op_end());
17696cd780ffSBenjamin Kramer       Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1770df55d8baSDuncan P. N. Exon Smith 
1771df55d8baSDuncan P. N. Exon Smith       replaceDstValue(MDNode::get(DstM->getContext(),
17725bf8fef5SDuncan P. N. Exon Smith                                   makeArrayRef(Elts.begin(), Elts.end())));
1773d77d9fb0SDaniel Dunbar       break;
1774d77d9fb0SDaniel Dunbar     }
177566f02413SBill Wendling     }
177666f02413SBill Wendling   }
177766f02413SBill Wendling 
17780ec72bbcSDaniel Dunbar   // Check all of the requirements.
17790ec72bbcSDaniel Dunbar   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
17800ec72bbcSDaniel Dunbar     MDNode *Requirement = Requirements[I];
17810ec72bbcSDaniel Dunbar     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
17825bf8fef5SDuncan P. N. Exon Smith     Metadata *ReqValue = Requirement->getOperand(1);
17830ec72bbcSDaniel Dunbar 
1784df55d8baSDuncan P. N. Exon Smith     MDNode *Op = Flags[Flag].first;
17850ec72bbcSDaniel Dunbar     if (!Op || Op->getOperand(2) != ReqValue) {
17860ec72bbcSDaniel Dunbar       HasErr |= emitError("linking module flags '" + Flag->getString() +
17879273b539SBill Wendling                           "': does not have the required value");
17880ec72bbcSDaniel Dunbar       continue;
178966f02413SBill Wendling     }
179066f02413SBill Wendling   }
179166f02413SBill Wendling 
179266f02413SBill Wendling   return HasErr;
179366f02413SBill Wendling }
179466f02413SBill Wendling 
1795c43df518SAkira Hatanaka // This function returns true if the triples match.
1796c43df518SAkira Hatanaka static bool triplesMatch(const Triple &T0, const Triple &T1) {
1797c43df518SAkira Hatanaka   // If vendor is apple, ignore the version number.
1798c43df518SAkira Hatanaka   if (T0.getVendor() == Triple::Apple)
1799c43df518SAkira Hatanaka     return T0.getArch() == T1.getArch() &&
1800c43df518SAkira Hatanaka            T0.getSubArch() == T1.getSubArch() &&
1801c43df518SAkira Hatanaka            T0.getVendor() == T1.getVendor() &&
1802c43df518SAkira Hatanaka            T0.getOS() == T1.getOS();
1803c43df518SAkira Hatanaka 
1804c43df518SAkira Hatanaka   return T0 == T1;
1805c43df518SAkira Hatanaka }
1806c43df518SAkira Hatanaka 
1807c43df518SAkira Hatanaka // This function returns the merged triple.
1808c43df518SAkira Hatanaka static std::string mergeTriples(const Triple &SrcTriple, const Triple &DstTriple) {
1809c43df518SAkira Hatanaka   // If vendor is apple, pick the triple with the larger version number.
1810c43df518SAkira Hatanaka   if (SrcTriple.getVendor() == Triple::Apple)
1811c43df518SAkira Hatanaka     if (DstTriple.isOSVersionLT(SrcTriple))
1812c43df518SAkira Hatanaka       return SrcTriple.str();
1813c43df518SAkira Hatanaka 
1814c43df518SAkira Hatanaka   return DstTriple.str();
1815c43df518SAkira Hatanaka }
1816c43df518SAkira Hatanaka 
1817b1ed91f3SChris Lattner bool ModuleLinker::run() {
181866f02413SBill Wendling   assert(DstM && "Null destination module");
181966f02413SBill Wendling   assert(SrcM && "Null source module");
1820b1ed91f3SChris Lattner 
1821b1ed91f3SChris Lattner   // Inherit the target data from the source module if the destination module
1822b1ed91f3SChris Lattner   // doesn't have one already.
182346a43556SMehdi Amini   if (DstM->getDataLayout().isDefault())
1824b1ed91f3SChris Lattner     DstM->setDataLayout(SrcM->getDataLayout());
1825b1ed91f3SChris Lattner 
182646a43556SMehdi Amini   if (SrcM->getDataLayout() != DstM->getDataLayout()) {
1827d12b4a33SRafael Espindola     emitWarning("Linking two modules of different data layouts: '" +
1828d12b4a33SRafael Espindola                 SrcM->getModuleIdentifier() + "' is '" +
1829d12b4a33SRafael Espindola                 SrcM->getDataLayoutStr() + "' whereas '" +
1830d12b4a33SRafael Espindola                 DstM->getModuleIdentifier() + "' is '" +
1831d12b4a33SRafael Espindola                 DstM->getDataLayoutStr() + "'\n");
1832e17f3708SEli Bendersky   }
1833c43df518SAkira Hatanaka 
1834c43df518SAkira Hatanaka   // Copy the target triple from the source to dest if the dest's is empty.
1835c43df518SAkira Hatanaka   if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1836c43df518SAkira Hatanaka     DstM->setTargetTriple(SrcM->getTargetTriple());
1837c43df518SAkira Hatanaka 
1838c43df518SAkira Hatanaka   Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM->getTargetTriple());
1839c43df518SAkira Hatanaka 
1840c43df518SAkira Hatanaka   if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
1841d12b4a33SRafael Espindola     emitWarning("Linking two modules of different target triples: " +
1842d12b4a33SRafael Espindola                 SrcM->getModuleIdentifier() + "' is '" +
1843d12b4a33SRafael Espindola                 SrcM->getTargetTriple() + "' whereas '" +
1844d12b4a33SRafael Espindola                 DstM->getModuleIdentifier() + "' is '" +
1845d12b4a33SRafael Espindola                 DstM->getTargetTriple() + "'\n");
1846c43df518SAkira Hatanaka 
1847c43df518SAkira Hatanaka   DstM->setTargetTriple(mergeTriples(SrcTriple, DstTriple));
1848b1ed91f3SChris Lattner 
1849b1ed91f3SChris Lattner   // Append the module inline asm string.
1850b1ed91f3SChris Lattner   if (!SrcM->getModuleInlineAsm().empty()) {
1851b1ed91f3SChris Lattner     if (DstM->getModuleInlineAsm().empty())
1852b1ed91f3SChris Lattner       DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1853b1ed91f3SChris Lattner     else
1854b1ed91f3SChris Lattner       DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
1855b1ed91f3SChris Lattner                                SrcM->getModuleInlineAsm());
1856b1ed91f3SChris Lattner   }
1857b1ed91f3SChris Lattner 
1858b1ed91f3SChris Lattner   // Loop over all of the linked values to compute type mappings.
1859b1ed91f3SChris Lattner   computeTypeMapping();
1860b1ed91f3SChris Lattner 
1861dad0a645SDavid Majnemer   ComdatsChosen.clear();
18625106ce78SDavid Blaikie   for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
1863dad0a645SDavid Majnemer     const Comdat &C = SMEC.getValue();
1864dad0a645SDavid Majnemer     if (ComdatsChosen.count(&C))
1865dad0a645SDavid Majnemer       continue;
1866dad0a645SDavid Majnemer     Comdat::SelectionKind SK;
1867dad0a645SDavid Majnemer     bool LinkFromSrc;
1868dad0a645SDavid Majnemer     if (getComdatResult(&C, SK, LinkFromSrc))
1869dad0a645SDavid Majnemer       return true;
1870dad0a645SDavid Majnemer     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1871dad0a645SDavid Majnemer   }
1872dad0a645SDavid Majnemer 
187309d84addSDuncan P. N. Exon Smith   // Upgrade mismatched global arrays.
187409d84addSDuncan P. N. Exon Smith   upgradeMismatchedGlobals();
187509d84addSDuncan P. N. Exon Smith 
1876b1ed91f3SChris Lattner   // Insert all of the globals in src into the DstM module... without linking
1877b1ed91f3SChris Lattner   // initializers (which could refer to functions not yet mapped over).
18783bf3f1f5SYaron Keren   for (GlobalVariable &GV : SrcM->globals())
18793bf3f1f5SYaron Keren     if (linkGlobalValueProto(&GV))
1880b1ed91f3SChris Lattner       return true;
1881b1ed91f3SChris Lattner 
1882b1ed91f3SChris Lattner   // Link the functions together between the two modules, without doing function
1883b1ed91f3SChris Lattner   // bodies... this just adds external function prototypes to the DstM
1884b1ed91f3SChris Lattner   // function...  We do this so that when we begin processing function bodies,
1885b1ed91f3SChris Lattner   // all of the global values that may be referenced are available in our
1886b1ed91f3SChris Lattner   // ValueMap.
18873bf3f1f5SYaron Keren   for (Function &F :*SrcM)
18883bf3f1f5SYaron Keren     if (linkGlobalValueProto(&F))
1889b1ed91f3SChris Lattner       return true;
1890b1ed91f3SChris Lattner 
1891b1ed91f3SChris Lattner   // If there were any aliases, link them now.
18923bf3f1f5SYaron Keren   for (GlobalAlias &GA : SrcM->aliases())
18933bf3f1f5SYaron Keren     if (linkGlobalValueProto(&GA))
1894b1ed91f3SChris Lattner       return true;
1895b1ed91f3SChris Lattner 
18963bf3f1f5SYaron Keren   for (const AppendingVarInfo &AppendingVar : AppendingVars)
18973bf3f1f5SYaron Keren     linkAppendingVarInit(AppendingVar);
1898b1ed91f3SChris Lattner 
1899beadd56aSRafael Espindola   for (const auto &Entry : DstM->getComdatSymbolTable()) {
1900beadd56aSRafael Espindola     const Comdat &C = Entry.getValue();
1901beadd56aSRafael Espindola     if (C.getSelectionKind() == Comdat::Any)
1902beadd56aSRafael Espindola       continue;
1903beadd56aSRafael Espindola     const GlobalValue *GV = SrcM->getNamedValue(C.getName());
1904ea45d834SPeter Collingbourne     if (GV)
19054fb46cb8SDuncan P. N. Exon Smith       MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1906beadd56aSRafael Espindola   }
1907beadd56aSRafael Espindola 
1908b1ed91f3SChris Lattner   // Link in the function bodies that are defined in the source module into
1909b1ed91f3SChris Lattner   // DstM.
1910f97d0cbeSRafael Espindola   for (Function &SF : *SrcM) {
1911d4bcefc7SRafael Espindola     // Skip if no body (function is external).
1912f97d0cbeSRafael Espindola     if (SF.isDeclaration())
1913d4bcefc7SRafael Espindola       continue;
1914d4bcefc7SRafael Espindola 
1915f97d0cbeSRafael Espindola     // Skip if not linking from source.
1916f97d0cbeSRafael Espindola     if (DoNotLinkFromSource.count(&SF))
1917f97d0cbeSRafael Espindola       continue;
1918f97d0cbeSRafael Espindola 
1919ef23711eSRafael Espindola     if (linkGlobalValueBody(SF))
192040d7ebedSRafael Espindola       return true;
1921b1ed91f3SChris Lattner   }
1922b1ed91f3SChris Lattner 
1923b1ed91f3SChris Lattner   // Resolve all uses of aliases with aliasees.
1924ef23711eSRafael Espindola   for (GlobalAlias &Src : SrcM->aliases()) {
1925ef23711eSRafael Espindola     if (DoNotLinkFromSource.count(&Src))
1926ef23711eSRafael Espindola       continue;
1927ef23711eSRafael Espindola     linkGlobalValueBody(Src);
1928ef23711eSRafael Espindola   }
1929b1ed91f3SChris Lattner 
193091686d6dSBill Wendling   // Update the initializers in the DstM module now that all globals that may
193191686d6dSBill Wendling   // be referenced are in DstM.
1932ef23711eSRafael Espindola   for (GlobalVariable &Src : SrcM->globals()) {
1933ef23711eSRafael Espindola     // Only process initialized GV's or ones not already in dest.
1934ef23711eSRafael Espindola     if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
1935ef23711eSRafael Espindola       continue;
1936ef23711eSRafael Espindola     linkGlobalValueBody(Src);
1937ef23711eSRafael Espindola   }
193891686d6dSBill Wendling 
19390a48b877STanya Lattner   // Process vector of lazily linked in functions.
1940ef23711eSRafael Espindola   while (!LazilyLinkGlobalValues.empty()) {
1941ef23711eSRafael Espindola     GlobalValue *SGV = LazilyLinkGlobalValues.back();
1942ef23711eSRafael Espindola     LazilyLinkGlobalValues.pop_back();
1943c7ed52f2STeresa Johnson     if (isPerformingImport() && !doImportAsDefinition(SGV))
1944c7ed52f2STeresa Johnson       continue;
19450a48b877STanya Lattner 
1946020d4fb1SArtem Belevich     // Skip declarations that ValueMaterializer may have created in
1947020d4fb1SArtem Belevich     // case we link in only some of SrcM.
1948020d4fb1SArtem Belevich     if (shouldLinkOnlyNeeded() && SGV->isDeclaration())
1949020d4fb1SArtem Belevich       continue;
1950020d4fb1SArtem Belevich 
19519573a9cfSRafael Espindola     assert(!SGV->isDeclaration() && "users should not pass down decls");
1952ef23711eSRafael Espindola     if (linkGlobalValueBody(*SGV))
195340d7ebedSRafael Espindola       return true;
19540a48b877STanya Lattner   }
19550a48b877STanya Lattner 
19561063293aSTeresa Johnson   // Note that we are done linking global value bodies. This prevents
19571063293aSTeresa Johnson   // metadata linking from creating new references.
19581063293aSTeresa Johnson   DoneLinkingBodies = true;
19591063293aSTeresa Johnson 
1960189b2526STeresa Johnson   // Remap all of the named MDNodes in Src into the DstM module. We do this
1961189b2526STeresa Johnson   // after linking GlobalValues so that MDNodes that reference GlobalValues
1962189b2526STeresa Johnson   // are properly remapped.
1963189b2526STeresa Johnson   linkNamedMDNodes();
1964189b2526STeresa Johnson 
1965189b2526STeresa Johnson   // Merge the module flags into the DstM module.
1966189b2526STeresa Johnson   if (linkModuleFlagsMetadata())
1967189b2526STeresa Johnson     return true;
1968189b2526STeresa Johnson 
19692609888fSAnton Korobeynikov   return false;
19702609888fSAnton Korobeynikov }
1971361e513dSReid Spencer 
197231ad468dSRafael Espindola Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
197331ad468dSRafael Espindola     : ETypes(E), IsPacked(P) {}
197431ad468dSRafael Espindola 
197531ad468dSRafael Espindola Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
197631ad468dSRafael Espindola     : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
197731ad468dSRafael Espindola 
197831ad468dSRafael Espindola bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
197931ad468dSRafael Espindola   if (IsPacked != That.IsPacked)
198031ad468dSRafael Espindola     return false;
198131ad468dSRafael Espindola   if (ETypes != That.ETypes)
198231ad468dSRafael Espindola     return false;
198331ad468dSRafael Espindola   return true;
198431ad468dSRafael Espindola }
198531ad468dSRafael Espindola 
198631ad468dSRafael Espindola bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
198731ad468dSRafael Espindola   return !this->operator==(That);
198831ad468dSRafael Espindola }
198931ad468dSRafael Espindola 
199031ad468dSRafael Espindola StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
199131ad468dSRafael Espindola   return DenseMapInfo<StructType *>::getEmptyKey();
199231ad468dSRafael Espindola }
199331ad468dSRafael Espindola 
199431ad468dSRafael Espindola StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
199531ad468dSRafael Espindola   return DenseMapInfo<StructType *>::getTombstoneKey();
199631ad468dSRafael Espindola }
199731ad468dSRafael Espindola 
199831ad468dSRafael Espindola unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
199931ad468dSRafael Espindola   return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
200031ad468dSRafael Espindola                       Key.IsPacked);
200131ad468dSRafael Espindola }
200231ad468dSRafael Espindola 
200331ad468dSRafael Espindola unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
200431ad468dSRafael Espindola   return getHashValue(KeyTy(ST));
200531ad468dSRafael Espindola }
200631ad468dSRafael Espindola 
200731ad468dSRafael Espindola bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
200831ad468dSRafael Espindola                                         const StructType *RHS) {
200931ad468dSRafael Espindola   if (RHS == getEmptyKey() || RHS == getTombstoneKey())
201031ad468dSRafael Espindola     return false;
201131ad468dSRafael Espindola   return LHS == KeyTy(RHS);
201231ad468dSRafael Espindola }
201331ad468dSRafael Espindola 
201431ad468dSRafael Espindola bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
201531ad468dSRafael Espindola                                         const StructType *RHS) {
201631ad468dSRafael Espindola   if (RHS == getEmptyKey())
201731ad468dSRafael Espindola     return LHS == getEmptyKey();
201831ad468dSRafael Espindola 
201931ad468dSRafael Espindola   if (RHS == getTombstoneKey())
202031ad468dSRafael Espindola     return LHS == getTombstoneKey();
202131ad468dSRafael Espindola 
202231ad468dSRafael Espindola   return KeyTy(LHS) == KeyTy(RHS);
202331ad468dSRafael Espindola }
202431ad468dSRafael Espindola 
202531ad468dSRafael Espindola void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
202631ad468dSRafael Espindola   assert(!Ty->isOpaque());
20273280a5d9SBenjamin Kramer   NonOpaqueStructTypes.insert(Ty);
202831ad468dSRafael Espindola }
202931ad468dSRafael Espindola 
2030a5b9e1cfSRafael Espindola void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
2031a5b9e1cfSRafael Espindola   assert(!Ty->isOpaque());
2032a5b9e1cfSRafael Espindola   NonOpaqueStructTypes.insert(Ty);
2033a5b9e1cfSRafael Espindola   bool Removed = OpaqueStructTypes.erase(Ty);
2034a5b9e1cfSRafael Espindola   (void)Removed;
2035a5b9e1cfSRafael Espindola   assert(Removed);
2036a5b9e1cfSRafael Espindola }
2037a5b9e1cfSRafael Espindola 
203831ad468dSRafael Espindola void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
203931ad468dSRafael Espindola   assert(Ty->isOpaque());
204031ad468dSRafael Espindola   OpaqueStructTypes.insert(Ty);
204131ad468dSRafael Espindola }
204231ad468dSRafael Espindola 
204331ad468dSRafael Espindola StructType *
204431ad468dSRafael Espindola Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
204531ad468dSRafael Espindola                                                bool IsPacked) {
204631ad468dSRafael Espindola   Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
204731ad468dSRafael Espindola   auto I = NonOpaqueStructTypes.find_as(Key);
204831ad468dSRafael Espindola   if (I == NonOpaqueStructTypes.end())
204931ad468dSRafael Espindola     return nullptr;
20503280a5d9SBenjamin Kramer   return *I;
205131ad468dSRafael Espindola }
205231ad468dSRafael Espindola 
205331ad468dSRafael Espindola bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
205431ad468dSRafael Espindola   if (Ty->isOpaque())
205531ad468dSRafael Espindola     return OpaqueStructTypes.count(Ty);
205631ad468dSRafael Espindola   auto I = NonOpaqueStructTypes.find(Ty);
205731ad468dSRafael Espindola   if (I == NonOpaqueStructTypes.end())
205831ad468dSRafael Espindola     return false;
20593280a5d9SBenjamin Kramer   return *I == Ty;
206031ad468dSRafael Espindola }
206131ad468dSRafael Espindola 
20625cb9c82aSRafael Espindola void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
20635cb9c82aSRafael Espindola   this->Composite = M;
20645cb9c82aSRafael Espindola   this->DiagnosticHandler = DiagnosticHandler;
2065a4e85e3dSRafael Espindola 
2066a4e85e3dSRafael Espindola   TypeFinder StructTypes;
2067a4e85e3dSRafael Espindola   StructTypes.run(*M, true);
206831ad468dSRafael Espindola   for (StructType *Ty : StructTypes) {
206931ad468dSRafael Espindola     if (Ty->isOpaque())
207031ad468dSRafael Espindola       IdentifiedStructTypes.addOpaque(Ty);
207131ad468dSRafael Espindola     else
207231ad468dSRafael Espindola       IdentifiedStructTypes.addNonOpaque(Ty);
207331ad468dSRafael Espindola   }
2074aa9918aaSRafael Espindola }
20753df61b7bSRafael Espindola 
20765cb9c82aSRafael Espindola Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
20775cb9c82aSRafael Espindola   init(M, DiagnosticHandler);
20785cb9c82aSRafael Espindola }
20795cb9c82aSRafael Espindola 
20805cb9c82aSRafael Espindola Linker::Linker(Module *M) {
20815cb9c82aSRafael Espindola   init(M, [this](const DiagnosticInfo &DI) {
20825cb9c82aSRafael Espindola     Composite->getContext().diagnose(DI);
20835cb9c82aSRafael Espindola   });
20845cb9c82aSRafael Espindola }
20855cb9c82aSRafael Espindola 
208691e6f6e1SBill Wendling void Linker::deleteModule() {
208791e6f6e1SBill Wendling   delete Composite;
20882617dcceSCraig Topper   Composite = nullptr;
208991e6f6e1SBill Wendling }
209091e6f6e1SBill Wendling 
2091*8220e8a8SMehdi Amini bool Linker::linkInModule(Module *Src, unsigned Flags,
2092*8220e8a8SMehdi Amini                           const FunctionInfoIndex *Index,
2093c7ed52f2STeresa Johnson                           Function *FuncToImport) {
2094a4e85e3dSRafael Espindola   ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
2095c7ed52f2STeresa Johnson                          DiagnosticHandler, Flags, Index, FuncToImport);
2096dab999d5SManman Ren   bool RetCode = TheLinker.run();
2097dab999d5SManman Ren   Composite->dropTriviallyDeadConstantArrays();
2098dab999d5SManman Ren   return RetCode;
20993df61b7bSRafael Espindola }
21003df61b7bSRafael Espindola 
21016487ce95SManman Ren void Linker::setModule(Module *Dst) {
21026487ce95SManman Ren   init(Dst, DiagnosticHandler);
21036487ce95SManman Ren }
21046487ce95SManman Ren 
2105b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
2106b1ed91f3SChris Lattner // LinkModules entrypoint.
2107b1ed91f3SChris Lattner //===----------------------------------------------------------------------===//
2108b1ed91f3SChris Lattner 
210918c89411SRafael Espindola /// This function links two modules together, with the resulting Dest module
211018c89411SRafael Espindola /// modified to be the composite of the two input modules. If an error occurs,
211118c89411SRafael Espindola /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
211218c89411SRafael Espindola /// Upon failure, the Dest module could be in a modified state, and shouldn't be
211318c89411SRafael Espindola /// relied on to be consistent.
21149f8eff31SRafael Espindola bool Linker::LinkModules(Module *Dest, Module *Src,
2115020d4fb1SArtem Belevich                          DiagnosticHandlerFunction DiagnosticHandler,
2116020d4fb1SArtem Belevich                          unsigned Flags) {
21174160f5d3SRafael Espindola   Linker L(Dest, DiagnosticHandler);
2118020d4fb1SArtem Belevich   return L.linkInModule(Src, Flags);
21194160f5d3SRafael Espindola }
21204160f5d3SRafael Espindola 
2121020d4fb1SArtem Belevich bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Flags) {
2122287f18b4SRafael Espindola   Linker L(Dest);
2123020d4fb1SArtem Belevich   return L.linkInModule(Src, Flags);
2124361e513dSReid Spencer }
2125a3aeb980SBill Wendling 
2126a3aeb980SBill Wendling //===----------------------------------------------------------------------===//
2127a3aeb980SBill Wendling // C API.
2128a3aeb980SBill Wendling //===----------------------------------------------------------------------===//
2129a3aeb980SBill Wendling 
2130a3aeb980SBill Wendling LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
2131a57d588cSJuergen Ributzka                          LLVMLinkerMode Unused, char **OutMessages) {
213298ab63ceSRafael Espindola   Module *D = unwrap(Dest);
213398ab63ceSRafael Espindola   std::string Message;
21344160f5d3SRafael Espindola   raw_string_ostream Stream(Message);
21354160f5d3SRafael Espindola   DiagnosticPrinterRawOStream DP(Stream);
21364160f5d3SRafael Espindola 
21374160f5d3SRafael Espindola   LLVMBool Result = Linker::LinkModules(
21389f8eff31SRafael Espindola       D, unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
213998ab63ceSRafael Espindola 
2140ff715e2dSEli Bendersky   if (OutMessages && Result) {
2141ff715e2dSEli Bendersky     Stream.flush();
214298ab63ceSRafael Espindola     *OutMessages = strdup(Message.c_str());
2143ff715e2dSEli Bendersky   }
2144a3aeb980SBill Wendling   return Result;
2145a3aeb980SBill Wendling }
2146