drop unneeded config.h includes
[oota-llvm.git] / lib / Linker / LinkModules.cpp
1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM module linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
23 #include "llvm/Transforms/Utils/ValueMapper.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // TypeMap implementation.
28 //===----------------------------------------------------------------------===//
29
30 namespace {
31 class TypeMapTy : public ValueMapTypeRemapper {
32   /// MappedTypes - This is a mapping from a source type to a destination type
33   /// to use.
34   DenseMap<Type*, Type*> MappedTypes;
35
36   /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
37   /// we speculatively add types to MappedTypes, but keep track of them here in
38   /// case we need to roll back.
39   SmallVector<Type*, 16> SpeculativeTypes;
40   
41   /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the
42   /// source module that are mapped to an opaque struct in the destination
43   /// module.
44   SmallVector<StructType*, 16> SrcDefinitionsToResolve;
45   
46   /// DstResolvedOpaqueTypes - This is the set of opaque types in the
47   /// destination modules who are getting a body from the source module.
48   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
49 public:
50   
51   /// addTypeMapping - Indicate that the specified type in the destination
52   /// module is conceptually equivalent to the specified type in the source
53   /// module.
54   void addTypeMapping(Type *DstTy, Type *SrcTy);
55
56   /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
57   /// module from a type definition in the source module.
58   void linkDefinedTypeBodies();
59   
60   /// get - Return the mapped type to use for the specified input type from the
61   /// source module.
62   Type *get(Type *SrcTy);
63
64   FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
65
66 private:
67   Type *getImpl(Type *T);
68   /// remapType - Implement the ValueMapTypeRemapper interface.
69   Type *remapType(Type *SrcTy) {
70     return get(SrcTy);
71   }
72   
73   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
74 };
75 }
76
77 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
78   Type *&Entry = MappedTypes[SrcTy];
79   if (Entry) return;
80   
81   if (DstTy == SrcTy) {
82     Entry = DstTy;
83     return;
84   }
85   
86   // Check to see if these types are recursively isomorphic and establish a
87   // mapping between them if so.
88   if (!areTypesIsomorphic(DstTy, SrcTy)) {
89     // Oops, they aren't isomorphic.  Just discard this request by rolling out
90     // any speculative mappings we've established.
91     for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
92       MappedTypes.erase(SpeculativeTypes[i]);
93   }
94   SpeculativeTypes.clear();
95 }
96
97 /// areTypesIsomorphic - Recursively walk this pair of types, returning true
98 /// if they are isomorphic, false if they are not.
99 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
100   // Two types with differing kinds are clearly not isomorphic.
101   if (DstTy->getTypeID() != SrcTy->getTypeID()) return false;
102
103   // If we have an entry in the MappedTypes table, then we have our answer.
104   Type *&Entry = MappedTypes[SrcTy];
105   if (Entry)
106     return Entry == DstTy;
107
108   // Two identical types are clearly isomorphic.  Remember this
109   // non-speculatively.
110   if (DstTy == SrcTy) {
111     Entry = DstTy;
112     return true;
113   }
114   
115   // Okay, we have two types with identical kinds that we haven't seen before.
116
117   // If this is an opaque struct type, special case it.
118   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
119     // Mapping an opaque type to any struct, just keep the dest struct.
120     if (SSTy->isOpaque()) {
121       Entry = DstTy;
122       SpeculativeTypes.push_back(SrcTy);
123       return true;
124     }
125
126     // Mapping a non-opaque source type to an opaque dest.  If this is the first
127     // type that we're mapping onto this destination type then we succeed.  Keep
128     // the dest, but fill it in later.  This doesn't need to be speculative.  If
129     // this is the second (different) type that we're trying to map onto the
130     // same opaque type then we fail.
131     if (cast<StructType>(DstTy)->isOpaque()) {
132       // We can only map one source type onto the opaque destination type.
133       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)))
134         return false;
135       SrcDefinitionsToResolve.push_back(SSTy);
136       Entry = DstTy;
137       return true;
138     }
139   }
140   
141   // If the number of subtypes disagree between the two types, then we fail.
142   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
143     return false;
144   
145   // Fail if any of the extra properties (e.g. array size) of the type disagree.
146   if (isa<IntegerType>(DstTy))
147     return false;  // bitwidth disagrees.
148   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
149     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
150       return false;
151     
152   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
153     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
154       return false;
155   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
156     StructType *SSTy = cast<StructType>(SrcTy);
157     if (DSTy->isLiteral() != SSTy->isLiteral() ||
158         DSTy->isPacked() != SSTy->isPacked())
159       return false;
160   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
161     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
162       return false;
163   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
164     if (DVTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
165       return false;
166   }
167
168   // Otherwise, we speculate that these two types will line up and recursively
169   // check the subelements.
170   Entry = DstTy;
171   SpeculativeTypes.push_back(SrcTy);
172
173   for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i)
174     if (!areTypesIsomorphic(DstTy->getContainedType(i),
175                             SrcTy->getContainedType(i)))
176       return false;
177   
178   // If everything seems to have lined up, then everything is great.
179   return true;
180 }
181
182 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
183 /// module from a type definition in the source module.
184 void TypeMapTy::linkDefinedTypeBodies() {
185   SmallVector<Type*, 16> Elements;
186   SmallString<16> TmpName;
187   
188   // Note that processing entries in this loop (calling 'get') can add new
189   // entries to the SrcDefinitionsToResolve vector.
190   while (!SrcDefinitionsToResolve.empty()) {
191     StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val();
192     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
193     
194     // TypeMap is a many-to-one mapping, if there were multiple types that
195     // provide a body for DstSTy then previous iterations of this loop may have
196     // already handled it.  Just ignore this case.
197     if (!DstSTy->isOpaque()) continue;
198     assert(!SrcSTy->isOpaque() && "Not resolving a definition?");
199     
200     // Map the body of the source type over to a new body for the dest type.
201     Elements.resize(SrcSTy->getNumElements());
202     for (unsigned i = 0, e = Elements.size(); i != e; ++i)
203       Elements[i] = getImpl(SrcSTy->getElementType(i));
204     
205     DstSTy->setBody(Elements, SrcSTy->isPacked());
206     
207     // If DstSTy has no name or has a longer name than STy, then viciously steal
208     // STy's name.
209     if (!SrcSTy->hasName()) continue;
210     StringRef SrcName = SrcSTy->getName();
211     
212     if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) {
213       TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end());
214       SrcSTy->setName("");
215       DstSTy->setName(TmpName.str());
216       TmpName.clear();
217     }
218   }
219   
220   DstResolvedOpaqueTypes.clear();
221 }
222
223
224 /// get - Return the mapped type to use for the specified input type from the
225 /// source module.
226 Type *TypeMapTy::get(Type *Ty) {
227   Type *Result = getImpl(Ty);
228   
229   // If this caused a reference to any struct type, resolve it before returning.
230   if (!SrcDefinitionsToResolve.empty())
231     linkDefinedTypeBodies();
232   return Result;
233 }
234
235 /// getImpl - This is the recursive version of get().
236 Type *TypeMapTy::getImpl(Type *Ty) {
237   // If we already have an entry for this type, return it.
238   Type **Entry = &MappedTypes[Ty];
239   if (*Entry) return *Entry;
240   
241   // If this is not a named struct type, then just map all of the elements and
242   // then rebuild the type from inside out.
243   if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
244     // If there are no element types to map, then the type is itself.  This is
245     // true for the anonymous {} struct, things like 'float', integers, etc.
246     if (Ty->getNumContainedTypes() == 0)
247       return *Entry = Ty;
248     
249     // Remap all of the elements, keeping track of whether any of them change.
250     bool AnyChange = false;
251     SmallVector<Type*, 4> ElementTypes;
252     ElementTypes.resize(Ty->getNumContainedTypes());
253     for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) {
254       ElementTypes[i] = getImpl(Ty->getContainedType(i));
255       AnyChange |= ElementTypes[i] != Ty->getContainedType(i);
256     }
257     
258     // If we found our type while recursively processing stuff, just use it.
259     Entry = &MappedTypes[Ty];
260     if (*Entry) return *Entry;
261     
262     // If all of the element types mapped directly over, then the type is usable
263     // as-is.
264     if (!AnyChange)
265       return *Entry = Ty;
266     
267     // Otherwise, rebuild a modified type.
268     switch (Ty->getTypeID()) {
269     default: assert(0 && "unknown derived type to remap");
270     case Type::ArrayTyID:
271       return *Entry = ArrayType::get(ElementTypes[0],
272                                      cast<ArrayType>(Ty)->getNumElements());
273     case Type::VectorTyID: 
274       return *Entry = VectorType::get(ElementTypes[0],
275                                       cast<VectorType>(Ty)->getNumElements());
276     case Type::PointerTyID:
277       return *Entry = PointerType::get(ElementTypes[0],
278                                       cast<PointerType>(Ty)->getAddressSpace());
279     case Type::FunctionTyID:
280       return *Entry = FunctionType::get(ElementTypes[0],
281                                         makeArrayRef(ElementTypes).slice(1),
282                                         cast<FunctionType>(Ty)->isVarArg());
283     case Type::StructTyID:
284       // Note that this is only reached for anonymous structs.
285       return *Entry = StructType::get(Ty->getContext(), ElementTypes,
286                                       cast<StructType>(Ty)->isPacked());
287     }
288   }
289
290   // Otherwise, this is an unmapped named struct.  If the struct can be directly
291   // mapped over, just use it as-is.  This happens in a case when the linked-in
292   // module has something like:
293   //   %T = type {%T*, i32}
294   //   @GV = global %T* null
295   // where T does not exist at all in the destination module.
296   //
297   // The other case we watch for is when the type is not in the destination
298   // module, but that it has to be rebuilt because it refers to something that
299   // is already mapped.  For example, if the destination module has:
300   //  %A = type { i32 }
301   // and the source module has something like
302   //  %A' = type { i32 }
303   //  %B = type { %A'* }
304   //  @GV = global %B* null
305   // then we want to create a new type: "%B = type { %A*}" and have it take the
306   // pristine "%B" name from the source module.
307   //
308   // To determine which case this is, we have to recursively walk the type graph
309   // speculating that we'll be able to reuse it unmodified.  Only if this is
310   // safe would we map the entire thing over.  Because this is an optimization,
311   // and is not required for the prettiness of the linked module, we just skip
312   // it and always rebuild a type here.
313   StructType *STy = cast<StructType>(Ty);
314   
315   // If the type is opaque, we can just use it directly.
316   if (STy->isOpaque())
317     return *Entry = STy;
318   
319   // Otherwise we create a new type and resolve its body later.  This will be
320   // resolved by the top level of get().
321   SrcDefinitionsToResolve.push_back(STy);
322   StructType *DTy = StructType::create(STy->getContext());
323   DstResolvedOpaqueTypes.insert(DTy);
324   return *Entry = DTy;
325 }
326
327
328
329 //===----------------------------------------------------------------------===//
330 // ModuleLinker implementation.
331 //===----------------------------------------------------------------------===//
332
333 namespace {
334   /// ModuleLinker - This is an implementation class for the LinkModules
335   /// function, which is the entrypoint for this file.
336   class ModuleLinker {
337     Module *DstM, *SrcM;
338     
339     TypeMapTy TypeMap; 
340
341     /// ValueMap - Mapping of values from what they used to be in Src, to what
342     /// they are now in DstM.  ValueToValueMapTy is a ValueMap, which involves
343     /// some overhead due to the use of Value handles which the Linker doesn't
344     /// actually need, but this allows us to reuse the ValueMapper code.
345     ValueToValueMapTy ValueMap;
346     
347     struct AppendingVarInfo {
348       GlobalVariable *NewGV;  // New aggregate global in dest module.
349       Constant *DstInit;      // Old initializer from dest module.
350       Constant *SrcInit;      // Old initializer from src module.
351     };
352     
353     std::vector<AppendingVarInfo> AppendingVars;
354     
355     unsigned Mode; // Mode to treat source module.
356     
357     // Set of items not to link in from source.
358     SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
359     
360     // Vector of functions to lazily link in.
361     std::vector<Function*> LazilyLinkFunctions;
362     
363   public:
364     std::string ErrorMsg;
365     
366     ModuleLinker(Module *dstM, Module *srcM, unsigned mode)
367       : DstM(dstM), SrcM(srcM), Mode(mode) { }
368     
369     bool run();
370     
371   private:
372     /// emitError - Helper method for setting a message and returning an error
373     /// code.
374     bool emitError(const Twine &Message) {
375       ErrorMsg = Message.str();
376       return true;
377     }
378     
379     /// getLinkageResult - This analyzes the two global values and determines
380     /// what the result will look like in the destination module.
381     bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
382                           GlobalValue::LinkageTypes &LT, bool &LinkFromSrc);
383
384     /// getLinkedToGlobal - Given a global in the source module, return the
385     /// global in the destination module that is being linked to, if any.
386     GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) {
387       // If the source has no name it can't link.  If it has local linkage,
388       // there is no name match-up going on.
389       if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
390         return 0;
391       
392       // Otherwise see if we have a match in the destination module's symtab.
393       GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
394       if (DGV == 0) return 0;
395         
396       // If we found a global with the same name in the dest module, but it has
397       // internal linkage, we are really not doing any linkage here.
398       if (DGV->hasLocalLinkage())
399         return 0;
400
401       // Otherwise, we do in fact link to the destination global.
402       return DGV;
403     }
404     
405     void computeTypeMapping();
406     
407     bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
408     bool linkGlobalProto(GlobalVariable *SrcGV);
409     bool linkFunctionProto(Function *SrcF);
410     bool linkAliasProto(GlobalAlias *SrcA);
411     
412     void linkAppendingVarInit(const AppendingVarInfo &AVI);
413     void linkGlobalInits();
414     void linkFunctionBody(Function *Dst, Function *Src);
415     void linkAliasBodies();
416     void linkNamedMDNodes();
417   };
418 }
419
420
421
422 /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict
423 /// in the symbol table.  This is good for all clients except for us.  Go
424 /// through the trouble to force this back.
425 static void forceRenaming(GlobalValue *GV, StringRef Name) {
426   // If the global doesn't force its name or if it already has the right name,
427   // there is nothing for us to do.
428   if (GV->hasLocalLinkage() || GV->getName() == Name)
429     return;
430
431   Module *M = GV->getParent();
432
433   // If there is a conflict, rename the conflict.
434   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
435     GV->takeName(ConflictGV);
436     ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
437     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
438   } else {
439     GV->setName(Name);              // Force the name back
440   }
441 }
442
443 /// CopyGVAttributes - copy additional attributes (those not needed to construct
444 /// a GlobalValue) from the SrcGV to the DestGV.
445 static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
446   // Use the maximum alignment, rather than just copying the alignment of SrcGV.
447   unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
448   DestGV->copyAttributesFrom(SrcGV);
449   DestGV->setAlignment(Alignment);
450   
451   forceRenaming(DestGV, SrcGV->getName());
452 }
453
454 /// getLinkageResult - This analyzes the two global values and determines what
455 /// the result will look like in the destination module.  In particular, it
456 /// computes the resultant linkage type, computes whether the global in the
457 /// source should be copied over to the destination (replacing the existing
458 /// one), and computes whether this linkage is an error or not. It also performs
459 /// visibility checks: we cannot link together two symbols with different
460 /// visibilities.
461 bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
462                                     GlobalValue::LinkageTypes &LT, 
463                                     bool &LinkFromSrc) {
464   assert(Dest && "Must have two globals being queried");
465   assert(!Src->hasLocalLinkage() &&
466          "If Src has internal linkage, Dest shouldn't be set!");
467   
468   bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable();
469   bool DestIsDeclaration = Dest->isDeclaration();
470   
471   if (SrcIsDeclaration) {
472     // If Src is external or if both Src & Dest are external..  Just link the
473     // external globals, we aren't adding anything.
474     if (Src->hasDLLImportLinkage()) {
475       // If one of GVs has DLLImport linkage, result should be dllimport'ed.
476       if (DestIsDeclaration) {
477         LinkFromSrc = true;
478         LT = Src->getLinkage();
479       }
480     } else if (Dest->hasExternalWeakLinkage()) {
481       // If the Dest is weak, use the source linkage.
482       LinkFromSrc = true;
483       LT = Src->getLinkage();
484     } else {
485       LinkFromSrc = false;
486       LT = Dest->getLinkage();
487     }
488   } else if (DestIsDeclaration && !Dest->hasDLLImportLinkage()) {
489     // If Dest is external but Src is not:
490     LinkFromSrc = true;
491     LT = Src->getLinkage();
492   } else if (Src->isWeakForLinker()) {
493     // At this point we know that Dest has LinkOnce, External*, Weak, Common,
494     // or DLL* linkage.
495     if (Dest->hasExternalWeakLinkage() ||
496         Dest->hasAvailableExternallyLinkage() ||
497         (Dest->hasLinkOnceLinkage() &&
498          (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
499       LinkFromSrc = true;
500       LT = Src->getLinkage();
501     } else {
502       LinkFromSrc = false;
503       LT = Dest->getLinkage();
504     }
505   } else if (Dest->isWeakForLinker()) {
506     // At this point we know that Src has External* or DLL* linkage.
507     if (Src->hasExternalWeakLinkage()) {
508       LinkFromSrc = false;
509       LT = Dest->getLinkage();
510     } else {
511       LinkFromSrc = true;
512       LT = GlobalValue::ExternalLinkage;
513     }
514   } else {
515     assert((Dest->hasExternalLinkage()  || Dest->hasDLLImportLinkage() ||
516             Dest->hasDLLExportLinkage() || Dest->hasExternalWeakLinkage()) &&
517            (Src->hasExternalLinkage()   || Src->hasDLLImportLinkage() ||
518             Src->hasDLLExportLinkage()  || Src->hasExternalWeakLinkage()) &&
519            "Unexpected linkage type!");
520     return emitError("Linking globals named '" + Src->getName() +
521                  "': symbol multiply defined!");
522   }
523
524   // Check visibility
525   if (Src->getVisibility() != Dest->getVisibility() &&
526       !SrcIsDeclaration && !DestIsDeclaration &&
527       !Src->hasAvailableExternallyLinkage() &&
528       !Dest->hasAvailableExternallyLinkage())
529     return emitError("Linking globals named '" + Src->getName() +
530                    "': symbols have different visibilities!");
531   return false;
532 }
533
534 /// computeTypeMapping - Loop over all of the linked values to compute type
535 /// mappings.  For example, if we link "extern Foo *x" and "Foo *x = NULL", then
536 /// we have two struct types 'Foo' but one got renamed when the module was
537 /// loaded into the same LLVMContext.
538 void ModuleLinker::computeTypeMapping() {
539   // Incorporate globals.
540   for (Module::global_iterator I = SrcM->global_begin(),
541        E = SrcM->global_end(); I != E; ++I) {
542     GlobalValue *DGV = getLinkedToGlobal(I);
543     if (DGV == 0) continue;
544     
545     if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) {
546       TypeMap.addTypeMapping(DGV->getType(), I->getType());
547       continue;      
548     }
549     
550     // Unify the element type of appending arrays.
551     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
552     ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType());
553     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
554   }
555   
556   // Incorporate functions.
557   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) {
558     if (GlobalValue *DGV = getLinkedToGlobal(I))
559       TypeMap.addTypeMapping(DGV->getType(), I->getType());
560   }
561   
562   // Incorporate types by name, scanning all the types in the source module.
563   // At this point, the destination module may have a type "%foo = { i32 }" for
564   // example.  When the source module got loaded into the same LLVMContext, if
565   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
566   // Though it isn't required for correctness, attempt to link these up to clean
567   // up the IR.
568   std::vector<StructType*> SrcStructTypes;
569   SrcM->findUsedStructTypes(SrcStructTypes);
570   
571   SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(),
572                                                  SrcStructTypes.end());
573   
574   for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) {
575     StructType *ST = SrcStructTypes[i];
576     if (!ST->hasName()) continue;
577     
578     // Check to see if there is a dot in the name followed by a digit.
579     size_t DotPos = ST->getName().rfind('.');
580     if (DotPos == 0 || DotPos == StringRef::npos ||
581         ST->getName().back() == '.' || !isdigit(ST->getName()[DotPos+1]))
582       continue;
583     
584     // Check to see if the destination module has a struct with the prefix name.
585     if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos)))
586       // Don't use it if this actually came from the source module.  They're in
587       // the same LLVMContext after all.
588       if (!SrcStructTypesSet.count(DST))
589         TypeMap.addTypeMapping(DST, ST);
590   }
591   
592   
593   // Don't bother incorporating aliases, they aren't generally typed well.
594   
595   // Now that we have discovered all of the type equivalences, get a body for
596   // any 'opaque' types in the dest module that are now resolved. 
597   TypeMap.linkDefinedTypeBodies();
598 }
599
600 /// linkAppendingVarProto - If there were any appending global variables, link
601 /// them together now.  Return true on error.
602 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
603                                          GlobalVariable *SrcGV) {
604  
605   if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
606     return emitError("Linking globals named '" + SrcGV->getName() +
607            "': can only link appending global with another appending global!");
608   
609   ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
610   ArrayType *SrcTy =
611     cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
612   Type *EltTy = DstTy->getElementType();
613   
614   // Check to see that they two arrays agree on type.
615   if (EltTy != SrcTy->getElementType())
616     return emitError("Appending variables with different element types!");
617   if (DstGV->isConstant() != SrcGV->isConstant())
618     return emitError("Appending variables linked with different const'ness!");
619   
620   if (DstGV->getAlignment() != SrcGV->getAlignment())
621     return emitError(
622              "Appending variables with different alignment need to be linked!");
623   
624   if (DstGV->getVisibility() != SrcGV->getVisibility())
625     return emitError(
626             "Appending variables with different visibility need to be linked!");
627   
628   if (DstGV->getSection() != SrcGV->getSection())
629     return emitError(
630           "Appending variables with different section name need to be linked!");
631   
632   uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
633   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
634   
635   // Create the new global variable.
636   GlobalVariable *NG =
637     new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
638                        DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV,
639                        DstGV->isThreadLocal(),
640                        DstGV->getType()->getAddressSpace());
641   
642   // Propagate alignment, visibility and section info.
643   CopyGVAttributes(NG, DstGV);
644   
645   AppendingVarInfo AVI;
646   AVI.NewGV = NG;
647   AVI.DstInit = DstGV->getInitializer();
648   AVI.SrcInit = SrcGV->getInitializer();
649   AppendingVars.push_back(AVI);
650
651   // Replace any uses of the two global variables with uses of the new
652   // global.
653   ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
654
655   DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
656   DstGV->eraseFromParent();
657   
658   // Track the source variable so we don't try to link it.
659   DoNotLinkFromSource.insert(SrcGV);
660   
661   return false;
662 }
663
664 /// linkGlobalProto - Loop through the global variables in the src module and
665 /// merge them into the dest module.
666 bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
667   GlobalValue *DGV = getLinkedToGlobal(SGV);
668
669   if (DGV) {
670     // Concatenation of appending linkage variables is magic and handled later.
671     if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage())
672       return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
673     
674     // Determine whether linkage of these two globals follows the source
675     // module's definition or the destination module's definition.
676     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
677     bool LinkFromSrc = false;
678     if (getLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc))
679       return true;
680
681     // If we're not linking from the source, then keep the definition that we
682     // have.
683     if (!LinkFromSrc) {
684       // Special case for const propagation.
685       if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
686         if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
687           DGVar->setConstant(true);
688       
689       // Set calculated linkage.
690       DGV->setLinkage(NewLinkage);
691       
692       // Make sure to remember this mapping.
693       ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
694       
695       // Track the source global so that we don't attempt to copy it over when 
696       // processing global initializers.
697       DoNotLinkFromSource.insert(SGV);
698       
699       return false;
700     }
701   }
702   
703   // No linking to be performed or linking from the source: simply create an
704   // identical version of the symbol over in the dest module... the
705   // initializer will be filled in later by LinkGlobalInits.
706   GlobalVariable *NewDGV =
707     new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
708                        SGV->isConstant(), SGV->getLinkage(), /*init*/0,
709                        SGV->getName(), /*insertbefore*/0,
710                        SGV->isThreadLocal(),
711                        SGV->getType()->getAddressSpace());
712   // Propagate alignment, visibility and section info.
713   CopyGVAttributes(NewDGV, SGV);
714
715   if (DGV) {
716     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
717     DGV->eraseFromParent();
718   }
719   
720   // Make sure to remember this mapping.
721   ValueMap[SGV] = NewDGV;
722   return false;
723 }
724
725 /// linkFunctionProto - Link the function in the source module into the
726 /// destination module if needed, setting up mapping information.
727 bool ModuleLinker::linkFunctionProto(Function *SF) {
728   GlobalValue *DGV = getLinkedToGlobal(SF);
729
730   if (DGV) {
731     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
732     bool LinkFromSrc = false;
733     if (getLinkageResult(DGV, SF, NewLinkage, LinkFromSrc))
734       return true;
735     
736     if (!LinkFromSrc) {
737       // Set calculated linkage
738       DGV->setLinkage(NewLinkage);
739       
740       // Make sure to remember this mapping.
741       ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
742       
743       // Track the function from the source module so we don't attempt to remap 
744       // it.
745       DoNotLinkFromSource.insert(SF);
746       
747       return false;
748     }
749   }
750   
751   // If there is no linkage to be performed or we are linking from the source,
752   // bring SF over.
753   Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
754                                      SF->getLinkage(), SF->getName(), DstM);
755   CopyGVAttributes(NewDF, SF);
756
757   if (DGV) {
758     // Any uses of DF need to change to NewDF, with cast.
759     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
760     DGV->eraseFromParent();
761   } else {
762     // Internal, LO_ODR, or LO linkage - stick in set to ignore and lazily link.
763     if (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
764         SF->hasAvailableExternallyLinkage()) {
765       DoNotLinkFromSource.insert(SF);
766       LazilyLinkFunctions.push_back(SF);
767     }
768   }
769   
770   ValueMap[SF] = NewDF;
771   return false;
772 }
773
774 /// LinkAliasProto - Set up prototypes for any aliases that come over from the
775 /// source module.
776 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
777   GlobalValue *DGV = getLinkedToGlobal(SGA);
778   
779   if (DGV) {
780     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
781     bool LinkFromSrc = false;
782     if (getLinkageResult(DGV, SGA, NewLinkage, LinkFromSrc))
783       return true;
784     
785     if (!LinkFromSrc) {
786       // Set calculated linkage.
787       DGV->setLinkage(NewLinkage);
788       
789       // Make sure to remember this mapping.
790       ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
791       
792       // Track the alias from the source module so we don't attempt to remap it.
793       DoNotLinkFromSource.insert(SGA);
794       
795       return false;
796     }
797   }
798   
799   // If there is no linkage to be performed or we're linking from the source,
800   // bring over SGA.
801   GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()),
802                                        SGA->getLinkage(), SGA->getName(),
803                                        /*aliasee*/0, DstM);
804   CopyGVAttributes(NewDA, SGA);
805
806   if (DGV) {
807     // Any uses of DGV need to change to NewDA, with cast.
808     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
809     DGV->eraseFromParent();
810   }
811   
812   ValueMap[SGA] = NewDA;
813   return false;
814 }
815
816 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
817   // Merge the initializer.
818   SmallVector<Constant*, 16> Elements;
819   if (ConstantArray *I = dyn_cast<ConstantArray>(AVI.DstInit)) {
820     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
821       Elements.push_back(I->getOperand(i));
822   } else {
823     assert(isa<ConstantAggregateZero>(AVI.DstInit));
824     ArrayType *DstAT = cast<ArrayType>(AVI.DstInit->getType());
825     Type *EltTy = DstAT->getElementType();
826     Elements.append(DstAT->getNumElements(), Constant::getNullValue(EltTy));
827   }
828   
829   Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap);
830   if (const ConstantArray *I = dyn_cast<ConstantArray>(SrcInit)) {
831     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
832       Elements.push_back(I->getOperand(i));
833   } else {
834     assert(isa<ConstantAggregateZero>(SrcInit));
835     ArrayType *SrcAT = cast<ArrayType>(SrcInit->getType());
836     Type *EltTy = SrcAT->getElementType();
837     Elements.append(SrcAT->getNumElements(), Constant::getNullValue(EltTy));
838   }
839   ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
840   AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
841 }
842
843
844 // linkGlobalInits - Update the initializers in the Dest module now that all
845 // globals that may be referenced are in Dest.
846 void ModuleLinker::linkGlobalInits() {
847   // Loop over all of the globals in the src module, mapping them over as we go
848   for (Module::const_global_iterator I = SrcM->global_begin(),
849        E = SrcM->global_end(); I != E; ++I) {
850     
851     // Only process initialized GV's or ones not already in dest.
852     if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;          
853     
854     // Grab destination global variable.
855     GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
856     // Figure out what the initializer looks like in the dest module.
857     DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
858                                  RF_None, &TypeMap));
859   }
860 }
861
862 // linkFunctionBody - Copy the source function over into the dest function and
863 // fix up references to values.  At this point we know that Dest is an external
864 // function, and that Src is not.
865 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
866   assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
867
868   // Go through and convert function arguments over, remembering the mapping.
869   Function::arg_iterator DI = Dst->arg_begin();
870   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
871        I != E; ++I, ++DI) {
872     DI->setName(I->getName());  // Copy the name over.
873
874     // Add a mapping to our mapping.
875     ValueMap[I] = DI;
876   }
877
878   if (Mode == Linker::DestroySource) {
879     // Splice the body of the source function into the dest function.
880     Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
881     
882     // At this point, all of the instructions and values of the function are now
883     // copied over.  The only problem is that they are still referencing values in
884     // the Source function as operands.  Loop through all of the operands of the
885     // functions and patch them up to point to the local versions.
886     for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
887       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
888         RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
889     
890   } else {
891     // Clone the body of the function into the dest function.
892     SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
893     CloneFunctionInto(Dst, Src, ValueMap, false, Returns);
894   }
895   
896   // There is no need to map the arguments anymore.
897   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
898        I != E; ++I)
899     ValueMap.erase(I);
900   
901 }
902
903
904 void ModuleLinker::linkAliasBodies() {
905   for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
906        I != E; ++I) {
907     if (DoNotLinkFromSource.count(I))
908       continue;
909     if (Constant *Aliasee = I->getAliasee()) {
910       GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
911       DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap));
912     }
913   }
914 }
915
916 /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest
917 /// module.
918 void ModuleLinker::linkNamedMDNodes() {
919   for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
920        E = SrcM->named_metadata_end(); I != E; ++I) {
921     NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
922     // Add Src elements into Dest node.
923     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
924       DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
925                                    RF_None, &TypeMap));
926   }
927 }
928   
929 bool ModuleLinker::run() {
930   assert(DstM && "Null Destination module");
931   assert(SrcM && "Null Source Module");
932
933   // Inherit the target data from the source module if the destination module
934   // doesn't have one already.
935   if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty())
936     DstM->setDataLayout(SrcM->getDataLayout());
937
938   // Copy the target triple from the source to dest if the dest's is empty.
939   if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
940     DstM->setTargetTriple(SrcM->getTargetTriple());
941
942   if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() &&
943       SrcM->getDataLayout() != DstM->getDataLayout())
944     errs() << "WARNING: Linking two modules of different data layouts!\n";
945   if (!SrcM->getTargetTriple().empty() &&
946       DstM->getTargetTriple() != SrcM->getTargetTriple()) {
947     errs() << "WARNING: Linking two modules of different target triples: ";
948     if (!SrcM->getModuleIdentifier().empty())
949       errs() << SrcM->getModuleIdentifier() << ": ";
950     errs() << "'" << SrcM->getTargetTriple() << "' and '" 
951            << DstM->getTargetTriple() << "'\n";
952   }
953
954   // Append the module inline asm string.
955   if (!SrcM->getModuleInlineAsm().empty()) {
956     if (DstM->getModuleInlineAsm().empty())
957       DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
958     else
959       DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
960                                SrcM->getModuleInlineAsm());
961   }
962
963   // Update the destination module's dependent libraries list with the libraries
964   // from the source module. There's no opportunity for duplicates here as the
965   // Module ensures that duplicate insertions are discarded.
966   for (Module::lib_iterator SI = SrcM->lib_begin(), SE = SrcM->lib_end();
967        SI != SE; ++SI)
968     DstM->addLibrary(*SI);
969   
970   // If the source library's module id is in the dependent library list of the
971   // destination library, remove it since that module is now linked in.
972   StringRef ModuleId = SrcM->getModuleIdentifier();
973   if (!ModuleId.empty())
974     DstM->removeLibrary(sys::path::stem(ModuleId));
975   
976   // Loop over all of the linked values to compute type mappings.
977   computeTypeMapping();
978
979   // Insert all of the globals in src into the DstM module... without linking
980   // initializers (which could refer to functions not yet mapped over).
981   for (Module::global_iterator I = SrcM->global_begin(),
982        E = SrcM->global_end(); I != E; ++I)
983     if (linkGlobalProto(I))
984       return true;
985
986   // Link the functions together between the two modules, without doing function
987   // bodies... this just adds external function prototypes to the DstM
988   // function...  We do this so that when we begin processing function bodies,
989   // all of the global values that may be referenced are available in our
990   // ValueMap.
991   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
992     if (linkFunctionProto(I))
993       return true;
994
995   // If there were any aliases, link them now.
996   for (Module::alias_iterator I = SrcM->alias_begin(),
997        E = SrcM->alias_end(); I != E; ++I)
998     if (linkAliasProto(I))
999       return true;
1000
1001   for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1002     linkAppendingVarInit(AppendingVars[i]);
1003   
1004   // Update the initializers in the DstM module now that all globals that may
1005   // be referenced are in DstM.
1006   linkGlobalInits();
1007
1008   // Link in the function bodies that are defined in the source module into
1009   // DstM.
1010   for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1011     
1012     // Skip if not linking from source.
1013     if (DoNotLinkFromSource.count(SF)) continue;
1014     
1015     // Skip if no body (function is external) or materialize.
1016     if (SF->isDeclaration()) {
1017       if (!SF->isMaterializable())
1018         continue;
1019       if (SF->Materialize(&ErrorMsg))
1020         return true;
1021     }
1022     
1023     linkFunctionBody(cast<Function>(ValueMap[SF]), SF);
1024   }
1025
1026   // Resolve all uses of aliases with aliasees.
1027   linkAliasBodies();
1028
1029   // Remap all of the named mdnoes in Src into the DstM module. We do this
1030   // after linking GlobalValues so that MDNodes that reference GlobalValues
1031   // are properly remapped.
1032   linkNamedMDNodes();
1033
1034   // Process vector of lazily linked in functions.
1035   bool LinkedInAnyFunctions;
1036   do {
1037     LinkedInAnyFunctions = false;
1038     
1039     for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1040         E = LazilyLinkFunctions.end(); I != E; ++I) {
1041       if (!*I)
1042         continue;
1043       
1044       Function *SF = *I;
1045       Function *DF = cast<Function>(ValueMap[SF]);
1046       
1047       if (!DF->use_empty()) {
1048         
1049         // Materialize if necessary.
1050         if (SF->isDeclaration()) {
1051           if (!SF->isMaterializable())
1052             continue;
1053           if (SF->Materialize(&ErrorMsg))
1054             return true;
1055         }
1056         
1057         // Link in function body.
1058         linkFunctionBody(DF, SF);
1059         
1060         // "Remove" from vector by setting the element to 0.
1061         *I = 0;
1062         
1063         // Set flag to indicate we may have more functions to lazily link in
1064         // since we linked in a function.
1065         LinkedInAnyFunctions = true;
1066       }
1067     }
1068   } while (LinkedInAnyFunctions);
1069   
1070   // Remove any prototypes of functions that were not actually linked in.
1071   for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1072       E = LazilyLinkFunctions.end(); I != E; ++I) {
1073     if (!*I)
1074       continue;
1075     
1076     Function *SF = *I;
1077     Function *DF = cast<Function>(ValueMap[SF]);
1078     if (DF->use_empty())
1079       DF->eraseFromParent();
1080   }
1081   
1082   // Now that all of the types from the source are used, resolve any structs
1083   // copied over to the dest that didn't exist there.
1084   TypeMap.linkDefinedTypeBodies();
1085   
1086   return false;
1087 }
1088
1089 //===----------------------------------------------------------------------===//
1090 // LinkModules entrypoint.
1091 //===----------------------------------------------------------------------===//
1092
1093 // LinkModules - This function links two modules together, with the resulting
1094 // left module modified to be the composite of the two input modules.  If an
1095 // error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1096 // the problem.  Upon failure, the Dest module could be in a modified state, and
1097 // shouldn't be relied on to be consistent.
1098 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode, 
1099                          std::string *ErrorMsg) {
1100   ModuleLinker TheLinker(Dest, Src, Mode);
1101   if (TheLinker.run()) {
1102     if (ErrorMsg) *ErrorMsg = TheLinker.ErrorMsg;
1103     return true;
1104   }
1105   
1106   return false;
1107 }