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