LinkModules.cpp revision 7b0c84dcf59b6ac3a27843fd8162ab36928fba35
152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===- Linker.cpp - Module Linker Implementation --------------------------===//
2b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
5b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// This file was developed by the LLVM research group and is distributed under
6b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
7b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
1052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// This file implements the LLVM module linker.
1152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
1252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// Specifically, this:
138d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//  * Merges global variables between the two modules
148d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//    * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
15c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner//  * Merges functions between two modules
1652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
1752f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===----------------------------------------------------------------------===//
1852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
19c837dd93cd20b7e37f70d9496ef63e213e4b4713Misha Brukman#include "llvm/Support/Linker.h"
20adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner#include "llvm/Constants.h"
21adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner#include "llvm/DerivedTypes.h"
225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Module.h"
235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/SymbolTable.h"
2447b14a4a6a455c7be169cfd312fcbe796f0ad426Misha Brukman#include "llvm/Instructions.h"
25adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner#include "llvm/Assembly/Writer.h"
26c28a224c68adf0464c275778af4bce762ad25d37Reid Spencer#include <iostream>
27c28a224c68adf0464c275778af4bce762ad25d37Reid Spencer
28f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true.
315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler...
325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
338166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattnerstatic inline bool Error(std::string *E, const std::string &Message) {
345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (E) *E = Message;
355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return true;
365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
38700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
39700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Function: ResolveTypes()
40700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
41700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Description:
42700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  Attempt to link the two specified types together.
43700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
44700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Inputs:
45700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  DestTy - The type to which we wish to resolve.
46700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  SrcTy  - The original type which we want to resolve.
47700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  Name   - The name of the type.
48700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
49700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Outputs:
50700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  DestST - The symbol table in which the new type should be placed.
51700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
52700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Return value:
53700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  true  - There is an error and the types cannot yet be linked.
54700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  false - No errors.
554c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner//
56e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattnerstatic bool ResolveTypes(const Type *DestTy, const Type *SrcTy,
57e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner                         SymbolTable *DestST, const std::string &Name) {
58e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  if (DestTy == SrcTy) return false;       // If already equal, noop
59e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
604c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  // Does the type already exist in the module?
614c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  if (DestTy && !isa<OpaqueType>(DestTy)) {  // Yup, the type already exists...
62e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
63e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
644c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    } else {
654c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      return true;  // Cannot link types... neither is opaque and not-equal
664c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
674c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  } else {                       // Type not in dest module.  Add it now.
684c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (DestTy)                  // Type _is_ in module, just opaque...
69e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      const_cast<OpaqueType*>(cast<OpaqueType>(DestTy))
70e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner                           ->refineAbstractTypeTo(SrcTy);
71fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner    else if (!Name.empty())
72e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      DestST->insert(Name, const_cast<Type*>(SrcTy));
734c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  }
744c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  return false;
754c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner}
764c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
7743f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattnerstatic const FunctionType *getFT(const PATypeHolder &TH) {
7843f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  return cast<FunctionType>(TH.get());
7943f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner}
809732be769e57f1dec03f046ab957053048f60e40Chris Lattnerstatic const StructType *getST(const PATypeHolder &TH) {
8143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  return cast<StructType>(TH.get());
8243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner}
83e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
84e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// RecursiveResolveTypes - This is just like ResolveTypes, except that it
85e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// recurses down into derived types, merging the used types if the parent types
86e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// are compatible.
87e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner//
88e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattnerstatic bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
89e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                   const PATypeHolder &SrcTy,
90e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                   SymbolTable *DestST, const std::string &Name,
91e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) {
9243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  const Type *SrcTyT = SrcTy.get();
9343f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  const Type *DestTyT = DestTy.get();
9443f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  if (DestTyT == SrcTyT) return false;       // If already equal, noop
95e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
96e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // If we found our opaque type, resolve it now!
9743f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT))
9843f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    return ResolveTypes(DestTyT, SrcTyT, DestST, Name);
99e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
100e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // Two types cannot be resolved together if they are of different primitive
101e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // type.  For example, we cannot resolve an int to a float.
102f70c22b019494723d0e706f93d6542dfaa6e73a5Chris Lattner  if (DestTyT->getTypeID() != SrcTyT->getTypeID()) return true;
103e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
104e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // Otherwise, resolve the used type used by this derived type...
105f70c22b019494723d0e706f93d6542dfaa6e73a5Chris Lattner  switch (DestTyT->getTypeID()) {
106e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::FunctionTyID: {
10743f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    if (cast<FunctionType>(DestTyT)->isVarArg() !=
108841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(SrcTyT)->isVarArg() ||
109841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(DestTyT)->getNumContainedTypes() !=
110841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(SrcTyT)->getNumContainedTypes())
11143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner      return true;
11243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i)
113e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i),
114e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 getFT(SrcTy)->getContainedType(i), DestST, "",
115e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 Pointers))
116e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        return true;
117e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    return false;
118e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
119e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::StructTyID: {
12043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    if (getST(DestTy)->getNumContainedTypes() !=
12143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner        getST(SrcTy)->getNumContainedTypes()) return 1;
12243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i)
123e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i),
124e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 getST(SrcTy)->getContainedType(i), DestST, "",
125e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 Pointers))
126e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        return true;
127e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    return false;
128e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
129e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::ArrayTyID: {
13043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    const ArrayType *DAT = cast<ArrayType>(DestTy.get());
13143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    const ArrayType *SAT = cast<ArrayType>(SrcTy.get());
132e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    if (DAT->getNumElements() != SAT->getNumElements()) return true;
133e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
134e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  DestST, "", Pointers);
135e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  }
136e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  case Type::PointerTyID: {
137e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // If this is a pointer type, check to see if we have already seen it.  If
138e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // so, we are in a recursive branch.  Cut off the search now.  We cannot use
139e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // an associative container for this search, because the type pointers (keys
140e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // in the container) change whenever types get resolved...
141e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    //
142e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    for (unsigned i = 0, e = Pointers.size(); i != e; ++i)
143e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (Pointers[i].first == DestTy)
144e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner        return Pointers[i].second != SrcTy;
145e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner
146e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // Otherwise, add the current pointers to the vector to stop recursion on
147e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // this pair.
148e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    Pointers.push_back(std::make_pair(DestTyT, SrcTyT));
149e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    bool Result =
150e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(),
151e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                             cast<PointerType>(SrcTy.get())->getElementType(),
152e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                             DestST, "", Pointers);
153e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    Pointers.pop_back();
154e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    return Result;
155e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
156e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  default: assert(0 && "Unexpected type!"); return true;
157e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
158e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner}
159e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
160e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattnerstatic bool RecursiveResolveTypes(const PATypeHolder &DestTy,
161e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  const PATypeHolder &SrcTy,
162e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  SymbolTable *DestST, const std::string &Name){
163e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes;
164e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes);
165e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner}
166e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner
167e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
1682c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any
1692c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module.
1702c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts.
1712c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner//
1725c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
1736e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  SymbolTable       *DestST = &Dest->getSymbolTable();
1746e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  const SymbolTable *SrcST  = &Src->getSymbolTable();
1752c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
1762c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Look for a type plane for Type's...
177567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer  SymbolTable::type_const_iterator TI = SrcST->type_begin();
178567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer  SymbolTable::type_const_iterator TE = SrcST->type_end();
179567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer  if (TI == TE) return false;  // No named types, do nothing.
1802c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
181cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // Some types cannot be resolved immediately because they depend on other
182cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // types being resolved to each other first.  This contains a list of types we
183cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // are waiting to recheck.
1844c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  std::vector<std::string> DelayedTypesToResolve;
1854c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
186567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer  for ( ; TI != TE; ++TI ) {
187567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer    const std::string &Name = TI->first;
188c28a224c68adf0464c275778af4bce762ad25d37Reid Spencer    const Type *RHS = TI->second;
1892c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
1902c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    // Check to see if this type name is already in the dest module...
191567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer    Type *Entry = DestST->lookupType(Name);
1924c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
1934c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (ResolveTypes(Entry, RHS, DestST, Name)) {
1944c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      // They look different, save the types 'till later to resolve.
1954c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      DelayedTypesToResolve.push_back(Name);
1964c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
1974c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  }
1984c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
1994c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  // Iteratively resolve types while we can...
2004c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  while (!DelayedTypesToResolve.empty()) {
2014c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    // Loop over all of the types, attempting to resolve them if possible...
2024c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    unsigned OldSize = DelayedTypesToResolve.size();
2034c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
204e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    // Try direct resolution by name...
2054c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
2064c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      const std::string &Name = DelayedTypesToResolve[i];
207567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer      Type *T1 = SrcST->lookupType(Name);
208567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer      Type *T2 = DestST->lookupType(Name);
2094c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      if (!ResolveTypes(T2, T1, DestST, Name)) {
2104c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        // We are making progress!
2114c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
2124c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        --i;
2132f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      }
2144c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
2152f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner
2164c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    // Did we not eliminate any types?
2174c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (DelayedTypesToResolve.size() == OldSize) {
218e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // Attempt to resolve subelements of types.  This allows us to merge these
219e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // two types: { int* } and { opaque* }
2204c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
2214c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        const std::string &Name = DelayedTypesToResolve[i];
222567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer        PATypeHolder T1(SrcST->lookupType(Name));
223567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer        PATypeHolder T2(DestST->lookupType(Name));
224e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
225e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        if (!RecursiveResolveTypes(T2, T1, DestST, Name)) {
226e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // We are making progress!
227e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
228e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
229e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // Go back to the main loop, perhaps we can resolve directly by name
230e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // now...
231e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          break;
232e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        }
233e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      }
234e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
235e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // If we STILL cannot resolve the types, then there is something wrong.
236aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner      // Report the warning and delete one of the names.
237e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      if (DelayedTypesToResolve.size() == OldSize) {
238aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        const std::string &Name = DelayedTypesToResolve.back();
239aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner
240567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer        const Type *T1 = SrcST->lookupType(Name);
241567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer        const Type *T2 = DestST->lookupType(Name);
242aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        std::cerr << "WARNING: Type conflict between types named '" << Name
243adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner                  <<  "'.\n    Src='";
244adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        WriteTypeSymbolic(std::cerr, T1, Src);
245adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        std::cerr << "'.\n   Dest='";
246adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        WriteTypeSymbolic(std::cerr, T2, Dest);
247adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        std::cerr << "'\n";
248aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner
249aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        // Remove the symbol name from the destination.
250aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        DelayedTypesToResolve.pop_back();
2514c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      }
2522c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    }
2532c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  }
2544c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
2554c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
2562c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  return false;
2572c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner}
2582c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
2595c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic void PrintMap(const std::map<const Value*, Value*> &M) {
2605c2d335d86579941b71816196a95a64b277e8963Chris Lattner  for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end();
2612d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner       I != E; ++I) {
2625c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " Fr: " << (void*)I->first << " ";
26387182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->first->dump();
2645c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " To: " << (void*)I->second << " ";
26587182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->second->dump();
2665c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "\n";
2672d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
2682d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner}
2692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
2702d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
2715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one
2725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another.  This is somewhat sophisticated in that it can
2735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well...
2745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
2755c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic Value *RemapOperand(const Value *In,
2765c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> &LocalMap,
2775c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> *GlobalMap) {
2785c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
2795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (I != LocalMap.end()) return I->second;
2805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (GlobalMap) {
2825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    I = GlobalMap->find(In);
2835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    if (I != GlobalMap->end()) return I->second;
2845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
2855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2868d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Check to see if it's a constant that we are interesting in transforming...
28718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (const Constant *CPV = dyn_cast<Constant>(In)) {
288de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner    if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
289de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        isa<ConstantAggregateZero>(CPV))
29018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
2918d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
292e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    Constant *Result = 0;
2938d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
29418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
295697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPA->getValues();
296697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
297e306d94782b1c608857738279852dcc050c66004Chris Lattner      for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2988d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
299e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
300e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
30118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
302697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPS->getValues();
303697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
3048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
3058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
306e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
307e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
308e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (isa<ConstantPointerNull>(CPV)) {
30918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Result = const_cast<Constant*>(CPV);
31000dc4796132933e76fe2b834ca087e42621c175aReid Spencer    } else if (isa<GlobalValue>(CPV)) {
31100dc4796132933e76fe2b834ca087e42621c175aReid Spencer      Result = cast<Constant>(RemapOperand(CPV, LocalMap, GlobalMap));
3126cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
313b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      if (CE->getOpcode() == Instruction::GetElementPtr) {
314b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
315b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        std::vector<Constant*> Indices;
316b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Indices.reserve(CE->getNumOperands()-1);
317b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
318b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
319b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner                                                        LocalMap, GlobalMap)));
320b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner
321b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
322b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      } else if (CE->getNumOperands() == 1) {
323ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        // Cast instruction
324ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        assert(CE->getOpcode() == Instruction::Cast);
3256cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
326ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
3271438102576b94d6dac9bb18e8df291c4d632572dChris Lattner      } else if (CE->getNumOperands() == 3) {
3281438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        // Select instruction
3291438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        assert(CE->getOpcode() == Instruction::Select);
3301438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
3311438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
3321438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        Value *V3 = RemapOperand(CE->getOperand(2), LocalMap, GlobalMap);
3331438102576b94d6dac9bb18e8df291c4d632572dChris Lattner        Result = ConstantExpr::getSelect(cast<Constant>(V1), cast<Constant>(V2),
3341438102576b94d6dac9bb18e8df291c4d632572dChris Lattner                                         cast<Constant>(V3));
3356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else if (CE->getNumOperands() == 2) {
3366cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        // Binary operator...
3376cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
3386cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
3396cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
3406cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
341d981f8af79be9ea647d593531091740a3a27a85fChris Lattner                                   cast<Constant>(V2));
3426cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else {
343b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        assert(0 && "Unknown constant expr type!");
3446cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      }
3456cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
3468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    } else {
3478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      assert(0 && "Unknown type of derived type constant value!");
3488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
3498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
3508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    // Cache the mapping in our local map structure...
351d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    if (GlobalMap)
352d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      GlobalMap->insert(std::make_pair(In, Result));
353d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    else
354d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      LocalMap.insert(std::make_pair(In, Result));
3558d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    return Result;
3568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
3572d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3585c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "XXX LocalMap: \n";
3592d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  PrintMap(LocalMap);
3602d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3612d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  if (GlobalMap) {
3625c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "XXX GlobalMap: \n";
3632d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    PrintMap(*GlobalMap);
3642d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
3652d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3665c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
3678d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  assert(0 && "Couldn't remap value!");
3688d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return 0;
3695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
371fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// FindGlobalNamed - Look in the specified symbol table for a global with the
372fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// specified name and type.  If an exactly matching global does not exist, see
373fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// if there is a global which is "type compatible" with the specified
374fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// name/type.  This allows us to resolve things like '%x = global int*' with
375fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// '%x = global opaque*'.
376fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner///
377fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattnerstatic GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty,
378fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                    SymbolTable *ST) {
379fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // See if an exact match exists in the symbol table...
380fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  if (Value *V = ST->lookup(Ty, Name)) return cast<GlobalValue>(V);
381c003628a612d3687fb77088a5894314210a65385Chris Lattner
382fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // It doesn't exist exactly, scan through all of the type planes in the symbol
383fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // table, checking each of them for a type-compatible version.
384fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  //
385fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner  for (SymbolTable::plane_iterator PI = ST->plane_begin(), PE = ST->plane_end();
386567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer       PI != PE; ++PI) {
387567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer    // Does this type plane contain an entry with the specified name?
388fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner    SymbolTable::ValueMap &VM = PI->second;
389567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer    SymbolTable::value_iterator VI = VM.find(Name);
390fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner
391fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner    if (VI != VM.end()) {
392fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      // Ensure that this type if placed correctly into the symbol table.
393fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      GlobalValue *ValPtr = cast<GlobalValue>(VI->second);
394fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      assert(ValPtr->getType() == PI->first && "Type conflict!");
395fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner
396fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      // Determine whether we can fold the two types together, resolving them.
397fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      // If so, we can use this value.
398fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner      if (!ValPtr->hasInternalLinkage() &&
399fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner          !RecursiveResolveTypes(Ty, PI->first, ST, ""))
400fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner        return ValPtr;
401fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner    }
402fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner  }
403fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  return 0;  // Otherwise, nothing could be found.
404fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner}
405fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner
406c003628a612d3687fb77088a5894314210a65385Chris Lattner/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
407c003628a612d3687fb77088a5894314210a65385Chris Lattner/// in the symbol table.  This is good for all clients except for us.  Go
408c003628a612d3687fb77088a5894314210a65385Chris Lattner/// through the trouble to force this back.
409c003628a612d3687fb77088a5894314210a65385Chris Lattnerstatic void ForceRenaming(GlobalValue *GV, const std::string &Name) {
410c003628a612d3687fb77088a5894314210a65385Chris Lattner  assert(GV->getName() != Name && "Can't force rename to self");
411c003628a612d3687fb77088a5894314210a65385Chris Lattner  SymbolTable &ST = GV->getParent()->getSymbolTable();
412c003628a612d3687fb77088a5894314210a65385Chris Lattner
413c003628a612d3687fb77088a5894314210a65385Chris Lattner  // If there is a conflict, rename the conflict.
414c003628a612d3687fb77088a5894314210a65385Chris Lattner  Value *ConflictVal = ST.lookup(GV->getType(), Name);
415c003628a612d3687fb77088a5894314210a65385Chris Lattner  assert(ConflictVal&&"Why do we have to force rename if there is no conflic?");
416c003628a612d3687fb77088a5894314210a65385Chris Lattner  GlobalValue *ConflictGV = cast<GlobalValue>(ConflictVal);
417c003628a612d3687fb77088a5894314210a65385Chris Lattner  assert(ConflictGV->hasInternalLinkage() &&
418c003628a612d3687fb77088a5894314210a65385Chris Lattner         "Not conflicting with a static global, should link instead!");
419c003628a612d3687fb77088a5894314210a65385Chris Lattner
420c003628a612d3687fb77088a5894314210a65385Chris Lattner  ConflictGV->setName("");          // Eliminate the conflict
421c003628a612d3687fb77088a5894314210a65385Chris Lattner  GV->setName(Name);                // Force the name back
422c003628a612d3687fb77088a5894314210a65385Chris Lattner  ConflictGV->setName(Name);        // This will cause ConflictGV to get renamed
4237b0c84dcf59b6ac3a27843fd8162ab36928fba35Chris Lattner  assert(GV->getName() == Name && ConflictGV->getName() != Name &&
424c003628a612d3687fb77088a5894314210a65385Chris Lattner         "ForceRenaming didn't work");
425c003628a612d3687fb77088a5894314210a65385Chris Lattner}
426c003628a612d3687fb77088a5894314210a65385Chris Lattner
4275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge
4298166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// them into the dest module.
4305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
4315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src,
4325c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::map<const Value*, Value*> &ValueMap,
4338166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                    std::multimap<std::string, GlobalVariable *> &AppendingVars,
4345c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::string *Err) {
4355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
4365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
437b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
4385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4395c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
4405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4415c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
44218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
4434ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    GlobalVariable *DGV = 0;
444fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner    if (SGV->hasName() && !SGV->hasInternalLinkage()) {
4454ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // A same named thing is a global variable, because the only two things
44679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
44779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
4485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
449fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner      DGV = cast_or_null<GlobalVariable>(FindGlobalNamed(SGV->getName(),
450fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                                         SGV->getType(), ST));
4514ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    }
4525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4534ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
4544ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner           "Global must either be external or have an initializer!");
4555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4560fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner    bool SGExtern = SGV->isExternal();
4570fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner    bool DGExtern = DGV ? DGV->isExternal() : false;
4580fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner
4594ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
4605c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // No linking to be performed, simply create an identical version of the
4618d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // symbol over in the dest module... the initializer will be filled in
4628d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // later by LinkGlobalInits...
4638d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      //
4642719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      GlobalVariable *NewDGV =
4652719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        new GlobalVariable(SGV->getType()->getElementType(),
4662719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                           SGV->isConstant(), SGV->getLinkage(), /*init*/0,
4672719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                           SGV->getName(), Dest);
4682719bac90525985f4ebb97324b4c087d47b54384Chris Lattner
4692719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // If the LLVM runtime renamed the global, but it is an externally visible
4702719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // symbol, DGV must be an existing global with internal linkage.  Rename
4712719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // it.
472c003628a612d3687fb77088a5894314210a65385Chris Lattner      if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
473c003628a612d3687fb77088a5894314210a65385Chris Lattner        ForceRenaming(NewDGV, SGV->getName());
4745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Make sure to remember this mapping...
4762719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      ValueMap.insert(std::make_pair(SGV, NewDGV));
4778166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (SGV->hasAppendingLinkage())
4788166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        // Keep track that this is an appending variable...
4798166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
4808166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
481c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->isExternal()) {
482c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // If SGV is external or if both SGV & DGV are external..  Just link the
483c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // external globals, we aren't adding anything.
484c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
485c2b97d43a074445c6856d69538a95de09450c26eChris Lattner
486c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (DGV->isExternal()) {   // If DGV is external but SGV is not...
487c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
488c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      DGV->setLinkage(SGV->getLinkage());    // Inherit linkage!
48935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (SGV->hasWeakLinkage() || SGV->hasLinkOnceLinkage()) {
49072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that DGV has LinkOnce, Appending, Weak, or
49172ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // External linkage.  If DGV is Appending, this is an error.
49272ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (DGV->hasAppendingLinkage())
49372ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        return Error(Err, "Linking globals named '" + SGV->getName() +
49472ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner                     " ' with 'weak' and 'appending' linkage is not allowed!");
49535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
49635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
49735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        return Error(Err, "Global Variable Collision on '" +
49835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
49935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     "' - Global variables differ in const'ness");
50035956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
50172ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // Otherwise, just perform the link.
50272ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
50335956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
50435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      // Linkonce+Weak = Weak
50535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DGV->hasLinkOnceLinkage() && SGV->hasWeakLinkage())
50635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        DGV->setLinkage(SGV->getLinkage());
50735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
50835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (DGV->hasWeakLinkage() || DGV->hasLinkOnceLinkage()) {
50972ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that SGV has LinkOnce, Appending, or External
51072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // linkage.  If SGV is Appending, this is an error.
51172ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (SGV->hasAppendingLinkage())
51272ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        return Error(Err, "Linking globals named '" + SGV->getName() +
51372ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner                     " ' with 'weak' and 'appending' linkage is not allowed!");
51435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
51535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
51635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        return Error(Err, "Global Variable Collision on '" +
51735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
51835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     "' - Global variables differ in const'ness");
51935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
52072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (!SGV->hasLinkOnceLinkage())
52172ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        DGV->setLinkage(SGV->getLinkage());    // Inherit linkage!
52272ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
52372ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
524c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->getLinkage() != DGV->getLinkage()) {
5254ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      return Error(Err, "Global variables named '" + SGV->getName() +
5264ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                   "' have different linkage specifiers!");
527c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->hasExternalLinkage()) {
528c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // Allow linking two exactly identical external global variables...
529f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
530c2b97d43a074445c6856d69538a95de09450c26eChris Lattner        return Error(Err, "Global Variable Collision on '" +
531c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
532c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                     "' - Global variables differ in const'ness");
533f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner
534f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner      if (SGV->getInitializer() != DGV->getInitializer())
535f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner        return Error(Err, "Global Variable Collision on '" +
536f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
537f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner                    "' - External linkage globals have different initializers");
538f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner
539c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
540c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->hasAppendingLinkage()) {
5418166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // No linking is performed yet.  Just insert a new copy of the global, and
5428166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // keep track of the fact that it is an appending variable in the
5438166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // AppendingVars map.  The name is cleared out so that no linkage is
5448166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // performed.
5458166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *NewDGV =
5468166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        new GlobalVariable(SGV->getType()->getElementType(),
5478166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           SGV->isConstant(), SGV->getLinkage(), /*init*/0,
5488166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           "", Dest);
5498166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
5508166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Make sure to remember this mapping...
5518166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      ValueMap.insert(std::make_pair(SGV, NewDGV));
5528166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
5538166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Keep track that this is an appending variable...
5548166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
5554ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else {
5564ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      assert(0 && "Unknown linkage!");
5575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
5585c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
5595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
5605c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
5615c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
5625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
5638d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all
5648d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest.
5658d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//
5668d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src,
5675c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::map<const Value*, Value*> &ValueMap,
5685c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::string *Err) {
5698d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5708d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
5718d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
5728d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
57318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
5748d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5758d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (SGV->hasInitializer()) {      // Only process initialized GV's
5768d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Figure out what the initializer looks like in the dest module...
5774ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      Constant *SInit =
5782f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
5798d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5808d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
5814ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      if (DGV->hasInitializer()) {
5824ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        if (SGV->hasExternalLinkage()) {
5834ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          if (DGV->getInitializer() != SInit)
5844ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner            return Error(Err, "Global Variable Collision on '" +
5854ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
5864ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         " - Global variables have different initializers");
58772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
5884ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // Nothing is required, mapped values will take the new global
5894ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // automatically.
59057cb9883aa4c82357594d4186858f175135be3b1Chris Lattner        } else if (SGV->hasLinkOnceLinkage() || SGV->hasWeakLinkage()) {
59157cb9883aa4c82357594d4186858f175135be3b1Chris Lattner          // Nothing is required, mapped values will take the new global
59257cb9883aa4c82357594d4186858f175135be3b1Chris Lattner          // automatically.
5934ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else if (DGV->hasAppendingLinkage()) {
5944ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Appending linkage unimplemented!");
5954ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else {
5964ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Unknown linkage!");
5974ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        }
5988d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      } else {
5998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        // Copy the initializer over now...
6004ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        DGV->setInitializer(SInit);
6018d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      }
6028d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
6038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
6048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return false;
6058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner}
6065c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
60779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// LinkFunctionProtos - Link the functions together between the two modules,
608c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// without doing function bodies... this just adds external function prototypes
609c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// to the Dest function...
6105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
61179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionProtos(Module *Dest, const Module *Src,
6125c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
6135c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
614b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
6155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
616c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
617c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
6185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
6195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
62018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const Function *SF = I;   // SrcFunction
6214ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    Function *DF = 0;
622fe41069070f1a4b2d58a996b1f82ff747d076435Chris Lattner    if (SF->hasName() && !SF->hasInternalLinkage())
62379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // The same named thing is a Function, because the only two things
62479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
62579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
6265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
627fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner      DF = cast_or_null<Function>(FindGlobalNamed(SF->getName(), SF->getType(),
628fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                                  ST));
6295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
6304ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
6310fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      // Function does not already exist, simply insert an function signature
6320fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      // identical to SF into the dest module...
6332719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
6342719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                                     SF->getName(), Dest);
6352719bac90525985f4ebb97324b4c087d47b54384Chris Lattner
6362719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // If the LLVM runtime renamed the function, but it is an externally
6372719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // visible symbol, DF must be an existing function with internal linkage.
6382719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // Rename it.
639c003628a612d3687fb77088a5894314210a65385Chris Lattner      if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
640c003628a612d3687fb77088a5894314210a65385Chris Lattner        ForceRenaming(DF, SF->getName());
6414ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner
6424ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // ... and remember this mapping...
6432719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      ValueMap.insert(std::make_pair(SF, NewDF));
644c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->isExternal()) {
645c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // If SF is external or if both SF & DF are external..  Just link the
646c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // external functions, we aren't adding anything.
647c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
648c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (DF->isExternal()) {   // If DF is external but SF is not...
649c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // Link the external functions, update linkage qualifiers
650c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
651c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      DF->setLinkage(SF->getLinkage());
652c2b97d43a074445c6856d69538a95de09450c26eChris Lattner
65335956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) {
65472ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that DF has LinkOnce, Weak, or External linkage.
65572ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
65672ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
65735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      // Linkonce+Weak = Weak
65835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DF->hasLinkOnceLinkage() && SF->hasWeakLinkage())
65935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        DF->setLinkage(SF->getLinkage());
66035956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
66135956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) {
66272ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that SF has LinkOnce or External linkage.
66372ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
66472ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (!SF->hasLinkOnceLinkage())   // Don't inherit linkonce linkage
66572ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        DF->setLinkage(SF->getLinkage());
66672ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
667c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->getLinkage() != DF->getLinkage()) {
6680fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      return Error(Err, "Functions named '" + SF->getName() +
6690fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner                   "' have different linkage specifiers!");
670c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->hasExternalLinkage()) {
671c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // The function is defined in both modules!!
672c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      return Error(Err, "Function '" +
673c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                   SF->getFunctionType()->getDescription() + "':\"" +
674c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                   SF->getName() + "\" - Function is already defined!");
675c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else {
676c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      assert(0 && "Unknown linkage configuration found!");
6775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
6785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
6795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
6805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
6815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
682c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBody - Copy the source function over into the dest function and
683c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// fix up references to values.  At this point we know that Dest is an external
684c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function, and that Src is not.
6855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
68679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBody(Function *Dest, const Function *Src,
6875c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::map<const Value*, Value*> &GlobalMap,
6885c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::string *Err) {
6895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
6905c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> LocalMap;   // Map for function local values
6915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
692c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Go through and convert function arguments over...
69369da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner  Function::aiterator DI = Dest->abegin();
69418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
69569da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner       I != E; ++I, ++DI) {
69669da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    DI->setName(I->getName());  // Copy the name information over...
6975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
6985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Add a mapping to our local map
69969da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    LocalMap.insert(std::make_pair(I, DI));
7005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
7015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7025c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the basic blocks, copying the instructions over...
7035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
70479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
705c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // Create new basic block and add to mapping and the Dest function...
70618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
70718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    LocalMap.insert(std::make_pair(I, DBB));
7085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7095c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Loop over all of the instructions in the src basic block, copying them
7105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // over.  Note that this is broken in a strict sense because the cloned
7115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // instructions will still be referencing values in the Src module, not
7125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // the remapped values.  In our case, however, we will not get caught and
7135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // so we can delay patching the values up until later...
7145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
71518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
7165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         II != IE; ++II) {
71718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Instruction *DI = II->clone();
71818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      DI->setName(II->getName());
7195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      DBB->getInstList().push_back(DI);
72018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      LocalMap.insert(std::make_pair(II, DI));
7215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
7225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
7235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
724c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // At this point, all of the instructions and values of the function are now
725c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // copied over.  The only problem is that they are still referencing values in
726c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // the Source function as operands.  Loop through all of the operands of the
727c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // functions and patch them up to point to the local versions...
7285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
72918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
73018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
73118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
732221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner           OI != OE; ++OI)
733221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner        *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
7345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
7365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
7375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7385c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
739c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBodies - Link in the function bodies that are defined in the
740c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// source module into the DestModule.  This consists basically of copying the
741c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function over and fixing up references to values.
7425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
74379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBodies(Module *Dest, const Module *Src,
7445c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
7455c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
7465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
747c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
748c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
7495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
75018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
75118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (!SF->isExternal()) {                  // No body if function is external
75218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
753c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner
75418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      // DF not external SF external?
75535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DF->isExternal()) {
75635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        // Only provide the function body if there isn't one already.
75735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        if (LinkFunctionBody(DF, SF, ValueMap, Err))
75835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner          return true;
759c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      }
760c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    }
7615c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
7625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
7635c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
7645c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7658166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// LinkAppendingVars - If there were any appending global variables, link them
7668166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// together now.  Return true on error.
7678166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner//
7688166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattnerstatic bool LinkAppendingVars(Module *M,
7698166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                  std::multimap<std::string, GlobalVariable *> &AppendingVars,
7708166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                              std::string *ErrorMsg) {
7718166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (AppendingVars.empty()) return false; // Nothing to do.
7728166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7738166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Loop over the multimap of appending vars, processing any variables with the
7748166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // same name, forming a new appending global variable with both of the
7758166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // initializers merged together, then rewrite references to the old variables
7768166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // and delete them.
7778166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
7788166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  std::vector<Constant*> Inits;
7798166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  while (AppendingVars.size() > 1) {
7808166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // Get the first two elements in the map...
7818166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    std::multimap<std::string,
7828166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
7838166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7848166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // If the first two elements are for different names, there is no pair...
7858166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // Otherwise there is a pair, so link them together...
7868166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    if (First->first == Second->first) {
7878166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *G1 = First->second, *G2 = Second->second;
7888166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
7898166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
7908166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7918166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Check to see that they two arrays agree on type...
7928166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (T1->getElementType() != T2->getElementType())
7938166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        return Error(ErrorMsg,
7948166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner         "Appending variables with different element types need to be linked!");
7958166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (G1->isConstant() != G2->isConstant())
7968166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        return Error(ErrorMsg,
7978166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                     "Appending variables linked with different const'ness!");
7988166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7998166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      unsigned NewSize = T1->getNumElements() + T2->getNumElements();
8008166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
8018166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8028166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Create the new global variable...
8038166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *NG =
8048166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
8058166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           /*init*/0, First->first, M);
8068166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8078166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Merge the initializer...
8088166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Inits.reserve(NewSize);
809de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
810de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
811de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(cast<Constant>(I->getValues()[i]));
812de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      } else {
813de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        assert(isa<ConstantAggregateZero>(G1->getInitializer()));
814de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        Constant *CV = Constant::getNullValue(T1->getElementType());
815de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
816de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(CV);
817de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      }
818de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
819de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
820de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(cast<Constant>(I->getValues()[i]));
821de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      } else {
822de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        assert(isa<ConstantAggregateZero>(G2->getInitializer()));
823de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        Constant *CV = Constant::getNullValue(T2->getElementType());
824de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
825de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(CV);
826de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      }
8278166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      NG->setInitializer(ConstantArray::get(NewType, Inits));
8288166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Inits.clear();
8298166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8308166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Replace any uses of the two global variables with uses of the new
8318166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // global...
8328166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8338166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // FIXME: This should rewrite simple/straight-forward uses such as
8348166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // getelementptr instructions to not use the Cast!
83500dc4796132933e76fe2b834ca087e42621c175aReid Spencer      G1->replaceAllUsesWith(ConstantExpr::getCast(NG, G1->getType()));
83600dc4796132933e76fe2b834ca087e42621c175aReid Spencer      G2->replaceAllUsesWith(ConstantExpr::getCast(NG, G2->getType()));
8378166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8388166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Remove the two globals from the module now...
8398166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      M->getGlobalList().erase(G1);
8408166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      M->getGlobalList().erase(G2);
8418166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8428166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Put the new global into the AppendingVars map so that we can handle
8438166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // linking of more than two vars...
8448166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Second->second = NG;
8458166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    }
8468166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    AppendingVars.erase(First);
8478166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  }
8488166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8498166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  return false;
8508166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner}
85152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
85252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
85352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting
85452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules.  If an
85552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
8565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem.  Upon failure, the Dest module could be in a modified state, and
8575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent.
85852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
859f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
860873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Dest->getEndianness() == Module::AnyEndianness)
861873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner    Dest->setEndianness(Src->getEndianness());
862873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Dest->getPointerSize() == Module::AnyPointerSize)
863873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner    Dest->setPointerSize(Src->getPointerSize());
864873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner
865873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Src->getEndianness() != Module::AnyEndianness &&
866873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner      Dest->getEndianness() != Src->getEndianness())
86743a9994c0164848d44527060da72985c75183a48Chris Lattner    std::cerr << "WARNING: Linking two modules of different endianness!\n";
868873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Src->getPointerSize() != Module::AnyPointerSize &&
869873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner      Dest->getPointerSize() != Src->getPointerSize())
87043a9994c0164848d44527060da72985c75183a48Chris Lattner    std::cerr << "WARNING: Linking two modules of different pointer size!\n";
8712c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
8722c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // LinkTypes - Go through the symbol table of the Src module and see if any
8732c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // types are named in the src module that are not named in the Dst module.
8742c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Make sure there are no type name conflicts.
8752c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  //
8762c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
8772c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
8785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // ValueMap - Mapping of values from what they used to be in Src, to what they
8795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // are now in Dest.
8805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
8815c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> ValueMap;
8825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
8838166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // AppendingVars - Keep track of global variables in the destination module
8848166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // with appending linkage.  After the module is linked together, they are
8858166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // appended and the module is rewritten.
8868166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
8878166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  std::multimap<std::string, GlobalVariable *> AppendingVars;
8888166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8898166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Add all of the appending globals already in the Dest module to
8908166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // AppendingVars.
8918166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  for (Module::giterator I = Dest->gbegin(), E = Dest->gend(); I != E; ++I)
892f41464618f41b92cf0a0bd14ccc57bce727fb33dChris Lattner    if (I->hasAppendingLinkage())
893f41464618f41b92cf0a0bd14ccc57bce727fb33dChris Lattner      AppendingVars.insert(std::make_pair(I->getName(), I));
8948166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8958166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Insert all of the globals in src into the Dest module... without linking
8968166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // initializers (which could refer to functions not yet mapped over).
8978166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
8988166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) return true;
8995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
900c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link the functions together between the two modules, without doing function
901c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // bodies... this just adds external function prototypes to the Dest
902c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // function...  We do this so that when we begin processing function bodies,
903c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // all of the global values that may be referenced are available in our
904c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // ValueMap.
9055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
90679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
9075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
9086cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // Update the initializers in the Dest module now that all globals that may
9096cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // be referenced are in Dest.
9106cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  //
9116cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
9126cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
913c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link in the function bodies that are defined in the source module into the
914c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // DestModule.  This consists basically of copying the function over and
915c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // fixing up references to values.
9165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
91779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
91852f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
9198166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // If there were any appending global variables, link them together now.
9208166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
9218166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
9228166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
92352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner  return false;
92452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner}
9259466f5113ba859677a28887c3c7143065e702019Vikram S. Adve
926567bc2cc1ed4d610daceecbb4ff7f92ebde1b530Reid Spencer// vim: sw=2
927