LinkModules.cpp revision de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6
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
19c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner#include "llvm/Transforms/Utils/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"
245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/iOther.h"
25adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner#include "llvm/Assembly/Writer.h"
26f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
27d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true.
295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler...
305c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
318166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattnerstatic inline bool Error(std::string *E, const std::string &Message) {
325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (E) *E = Message;
335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return true;
345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
36700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
37700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Function: ResolveTypes()
38700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
39700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Description:
40700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  Attempt to link the two specified types together.
41700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
42700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Inputs:
43700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  DestTy - The type to which we wish to resolve.
44700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  SrcTy  - The original type which we want to resolve.
45700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  Name   - The name of the type.
46700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
47700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Outputs:
48700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  DestST - The symbol table in which the new type should be placed.
49700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//
50700867bb69ae1469f2b20005a79fd133e83fd552John Criswell// Return value:
51700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  true  - There is an error and the types cannot yet be linked.
52700867bb69ae1469f2b20005a79fd133e83fd552John Criswell//  false - No errors.
534c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner//
54e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattnerstatic bool ResolveTypes(const Type *DestTy, const Type *SrcTy,
55e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner                         SymbolTable *DestST, const std::string &Name) {
56e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  if (DestTy == SrcTy) return false;       // If already equal, noop
57e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
584c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  // Does the type already exist in the module?
594c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  if (DestTy && !isa<OpaqueType>(DestTy)) {  // Yup, the type already exists...
60e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
61e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
624c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    } else {
634c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      return true;  // Cannot link types... neither is opaque and not-equal
644c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
654c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  } else {                       // Type not in dest module.  Add it now.
664c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (DestTy)                  // Type _is_ in module, just opaque...
67e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      const_cast<OpaqueType*>(cast<OpaqueType>(DestTy))
68e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner                           ->refineAbstractTypeTo(SrcTy);
69fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner    else if (!Name.empty())
70e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      DestST->insert(Name, const_cast<Type*>(SrcTy));
714c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  }
724c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  return false;
734c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner}
744c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
7543f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattnerstatic const FunctionType *getFT(const PATypeHolder &TH) {
7643f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  return cast<FunctionType>(TH.get());
7743f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner}
789732be769e57f1dec03f046ab957053048f60e40Chris Lattnerstatic const StructType *getST(const PATypeHolder &TH) {
7943f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  return cast<StructType>(TH.get());
8043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner}
81e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
82e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// RecursiveResolveTypes - This is just like ResolveTypes, except that it
83e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// recurses down into derived types, merging the used types if the parent types
84e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner// are compatible.
85e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner//
86e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattnerstatic bool RecursiveResolveTypesI(const PATypeHolder &DestTy,
87e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                   const PATypeHolder &SrcTy,
88e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                   SymbolTable *DestST, const std::string &Name,
89e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) {
9043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  const Type *SrcTyT = SrcTy.get();
9143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  const Type *DestTyT = DestTy.get();
9243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  if (DestTyT == SrcTyT) return false;       // If already equal, noop
93e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
94e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // If we found our opaque type, resolve it now!
9543f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT))
9643f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    return ResolveTypes(DestTyT, SrcTyT, DestST, Name);
97e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
98e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // Two types cannot be resolved together if they are of different primitive
99e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // type.  For example, we cannot resolve an int to a float.
10043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  if (DestTyT->getPrimitiveID() != SrcTyT->getPrimitiveID()) return true;
101e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
102e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  // Otherwise, resolve the used type used by this derived type...
10343f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner  switch (DestTyT->getPrimitiveID()) {
104e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::FunctionTyID: {
10543f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    if (cast<FunctionType>(DestTyT)->isVarArg() !=
106841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(SrcTyT)->isVarArg() ||
107841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(DestTyT)->getNumContainedTypes() !=
108841e00b96295a2b66cb7573e961656d28a6cb12bChris Lattner        cast<FunctionType>(SrcTyT)->getNumContainedTypes())
10943f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner      return true;
11043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i)
111e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i),
112e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 getFT(SrcTy)->getContainedType(i), DestST, "",
113e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 Pointers))
114e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        return true;
115e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    return false;
116e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
117e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::StructTyID: {
11843f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    if (getST(DestTy)->getNumContainedTypes() !=
11943f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner        getST(SrcTy)->getNumContainedTypes()) return 1;
12043f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i)
121e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i),
122e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 getST(SrcTy)->getContainedType(i), DestST, "",
123e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                 Pointers))
124e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        return true;
125e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    return false;
126e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
127e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  case Type::ArrayTyID: {
12843f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    const ArrayType *DAT = cast<ArrayType>(DestTy.get());
12943f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner    const ArrayType *SAT = cast<ArrayType>(SrcTy.get());
130e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    if (DAT->getNumElements() != SAT->getNumElements()) return true;
131e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
132e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  DestST, "", Pointers);
133e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  }
134e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  case Type::PointerTyID: {
135e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // If this is a pointer type, check to see if we have already seen it.  If
136e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // so, we are in a recursive branch.  Cut off the search now.  We cannot use
137e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // an associative container for this search, because the type pointers (keys
138e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // in the container) change whenever types get resolved...
139e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    //
140e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    for (unsigned i = 0, e = Pointers.size(); i != e; ++i)
141e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      if (Pointers[i].first == DestTy)
142e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner        return Pointers[i].second != SrcTy;
143e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner
144e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // Otherwise, add the current pointers to the vector to stop recursion on
145e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    // this pair.
146e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    Pointers.push_back(std::make_pair(DestTyT, SrcTyT));
147e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    bool Result =
148e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner      RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(),
149e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                             cast<PointerType>(SrcTy.get())->getElementType(),
150e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                             DestST, "", Pointers);
151e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    Pointers.pop_back();
152e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner    return Result;
153e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
154e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  default: assert(0 && "Unexpected type!"); return true;
155e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner  }
156e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner}
157e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
158e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattnerstatic bool RecursiveResolveTypes(const PATypeHolder &DestTy,
159e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  const PATypeHolder &SrcTy,
160e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner                                  SymbolTable *DestST, const std::string &Name){
161e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes;
162e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner  return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes);
163e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner}
164e3092c94ad2e3af96f37a0a8186149acbbd9700aChris Lattner
165e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
1662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any
1672c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module.
1682c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts.
1692c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner//
1705c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
1716e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  SymbolTable       *DestST = &Dest->getSymbolTable();
1726e6026b46569b01f8f6d4dcdb6c899c3a9c76b3eChris Lattner  const SymbolTable *SrcST  = &Src->getSymbolTable();
1732c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
1742c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Look for a type plane for Type's...
1752c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
1762c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (PI == SrcST->end()) return false;  // No named types, do nothing.
1772c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
178cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // Some types cannot be resolved immediately because they depend on other
179cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // types being resolved to each other first.  This contains a list of types we
180cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman  // are waiting to recheck.
1814c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  std::vector<std::string> DelayedTypesToResolve;
1824c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
1832c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable::VarMap &VM = PI->second;
1842c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
1852c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner       I != E; ++I) {
1865c2d335d86579941b71816196a95a64b277e8963Chris Lattner    const std::string &Name = I->first;
1874c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    Type *RHS = cast<Type>(I->second);
1882c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
1892c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    // Check to see if this type name is already in the dest module...
1904c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
1914c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
1924c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (ResolveTypes(Entry, RHS, DestST, Name)) {
1934c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      // They look different, save the types 'till later to resolve.
1944c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      DelayedTypesToResolve.push_back(Name);
1954c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
1964c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  }
1974c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
1984c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  // Iteratively resolve types while we can...
1994c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner  while (!DelayedTypesToResolve.empty()) {
2004c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    // Loop over all of the types, attempting to resolve them if possible...
2014c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    unsigned OldSize = DelayedTypesToResolve.size();
2024c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
203e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner    // Try direct resolution by name...
2044c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
2054c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      const std::string &Name = DelayedTypesToResolve[i];
2064c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      Type *T1 = cast<Type>(VM.find(Name)->second);
2074c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name));
2084c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      if (!ResolveTypes(T2, T1, DestST, Name)) {
2094c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        // We are making progress!
2104c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
2114c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        --i;
2122f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner      }
2134c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    }
2142f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner
2154c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    // Did we not eliminate any types?
2164c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner    if (DelayedTypesToResolve.size() == OldSize) {
217e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // Attempt to resolve subelements of types.  This allows us to merge these
218e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // two types: { int* } and { opaque* }
2194c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
2204c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner        const std::string &Name = DelayedTypesToResolve[i];
22143f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner        PATypeHolder T1(cast<Type>(VM.find(Name)->second));
22243f4ba8c7d19664bb5de73a7b391b67061f25d22Chris Lattner        PATypeHolder T2(cast<Type>(DestST->lookup(Type::TypeTy, Name)));
223e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
224e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        if (!RecursiveResolveTypes(T2, T1, DestST, Name)) {
225e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // We are making progress!
226e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
227e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
228e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // Go back to the main loop, perhaps we can resolve directly by name
229e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          // now...
230e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner          break;
231e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner        }
232e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      }
233e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner
234e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      // If we STILL cannot resolve the types, then there is something wrong.
235aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner      // Report the warning and delete one of the names.
236e76c57ad467ed57f09f2a3ef51628d4b6eb2b304Chris Lattner      if (DelayedTypesToResolve.size() == OldSize) {
237aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        const std::string &Name = DelayedTypesToResolve.back();
238aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner
239aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        const Type *T1 = cast<Type>(VM.find(Name)->second);
240aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        const Type *T2 = cast<Type>(DestST->lookup(Type::TypeTy, Name));
241aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        std::cerr << "WARNING: Type conflict between types named '" << Name
242adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner                  <<  "'.\n    Src='";
243adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        WriteTypeSymbolic(std::cerr, T1, Src);
244adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        std::cerr << "'.\n   Dest='";
245adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        WriteTypeSymbolic(std::cerr, T2, Dest);
246adbc0b5287bf36893cdcae2440d48b3cb3489e38Chris Lattner        std::cerr << "'\n";
247aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner
248aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        // Remove the symbol name from the destination.
249aeb18ce9c012f8554f48bae9bb32351c6eeaa26cChris Lattner        DelayedTypesToResolve.pop_back();
2504c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner      }
2512c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    }
2522c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  }
2534c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
2544c00e53b81de81ecf4ba0c4e287ea230c79e82aeChris Lattner
2552c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  return false;
2562c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner}
2572c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
2585c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic void PrintMap(const std::map<const Value*, Value*> &M) {
2595c2d335d86579941b71816196a95a64b277e8963Chris Lattner  for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end();
2602d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner       I != E; ++I) {
2615c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " Fr: " << (void*)I->first << " ";
26287182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->first->dump();
2635c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << " To: " << (void*)I->second << " ";
26487182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->second->dump();
2655c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "\n";
2662d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
2672d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner}
2682d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
2692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
2705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one
2715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another.  This is somewhat sophisticated in that it can
2725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well...
2735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
2745c2d335d86579941b71816196a95a64b277e8963Chris Lattnerstatic Value *RemapOperand(const Value *In,
2755c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> &LocalMap,
2765c2d335d86579941b71816196a95a64b277e8963Chris Lattner                           std::map<const Value*, Value*> *GlobalMap) {
2775c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
2785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (I != LocalMap.end()) return I->second;
2795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (GlobalMap) {
2815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    I = GlobalMap->find(In);
2825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    if (I != GlobalMap->end()) return I->second;
2835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
2845c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2858d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Check to see if it's a constant that we are interesting in transforming...
28618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (const Constant *CPV = dyn_cast<Constant>(In)) {
287de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner    if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
288de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        isa<ConstantAggregateZero>(CPV))
28918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
2908d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
291e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    Constant *Result = 0;
2928d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
29318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
294697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPA->getValues();
295697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
296e306d94782b1c608857738279852dcc050c66004Chris Lattner      for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2978d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
298e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
299e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
30018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
301697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPS->getValues();
302697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
3038d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
3048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
305e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
306e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
307e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (isa<ConstantPointerNull>(CPV)) {
30818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Result = const_cast<Constant*>(CPV);
30918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantPointerRef *CPR =
31018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                      dyn_cast<ConstantPointerRef>(CPV)) {
3118d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
312e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantPointerRef::get(cast<GlobalValue>(V));
3136cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
314b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      if (CE->getOpcode() == Instruction::GetElementPtr) {
315b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
316b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        std::vector<Constant*> Indices;
317b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Indices.reserve(CE->getNumOperands()-1);
318b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
319b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
320b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner                                                        LocalMap, GlobalMap)));
321b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner
322b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
323b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      } else if (CE->getNumOperands() == 1) {
324ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        // Cast instruction
325ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        assert(CE->getOpcode() == Instruction::Cast);
3266cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
327ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
3286cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else if (CE->getNumOperands() == 2) {
3296cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        // Binary operator...
3306cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
3316cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
3326cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
3336cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
334d981f8af79be9ea647d593531091740a3a27a85fChris Lattner                                   cast<Constant>(V2));
3356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else {
336b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        assert(0 && "Unknown constant expr type!");
3376cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      }
3386cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
3398d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    } else {
3408d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      assert(0 && "Unknown type of derived type constant value!");
3418d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
3428d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
3438d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    // Cache the mapping in our local map structure...
344d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    if (GlobalMap)
345d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      GlobalMap->insert(std::make_pair(In, Result));
346d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    else
347d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      LocalMap.insert(std::make_pair(In, Result));
3488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    return Result;
3498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
3502d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3515c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "XXX LocalMap: \n";
3522d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  PrintMap(LocalMap);
3532d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3542d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  if (GlobalMap) {
3555c2d335d86579941b71816196a95a64b277e8963Chris Lattner    std::cerr << "XXX GlobalMap: \n";
3562d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    PrintMap(*GlobalMap);
3572d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
3582d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
3595c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
3608d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  assert(0 && "Couldn't remap value!");
3618d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return 0;
3625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3635c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
364fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// FindGlobalNamed - Look in the specified symbol table for a global with the
365fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// specified name and type.  If an exactly matching global does not exist, see
366fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// if there is a global which is "type compatible" with the specified
367fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// name/type.  This allows us to resolve things like '%x = global int*' with
368fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner/// '%x = global opaque*'.
369fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner///
370fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattnerstatic GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty,
371fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                    SymbolTable *ST) {
372fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // See if an exact match exists in the symbol table...
373fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  if (Value *V = ST->lookup(Ty, Name)) return cast<GlobalValue>(V);
374fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner
375fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // It doesn't exist exactly, scan through all of the type planes in the symbol
376fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  // table, checking each of them for a type-compatible version.
377fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  //
378f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner  for (SymbolTable::iterator I = ST->begin(), E = ST->end(); I != E; ++I)
37977c5f733ac51c122ee3f75b8cc247b923d472909Chris Lattner    if (I->first != Type::TypeTy) {
380f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner      SymbolTable::VarMap &VM = I->second;
381700867bb69ae1469f2b20005a79fd133e83fd552John Criswell
382f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner      // Does this type plane contain an entry with the specified name?
383f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner      SymbolTable::type_iterator TI = VM.find(Name);
384f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner      if (TI != VM.end()) {
385700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
386700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        // Ensure that this type if placed correctly into the symbol table.
387700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
388700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        assert(TI->second->getType() == I->first && "Type conflict!");
389700867bb69ae1469f2b20005a79fd133e83fd552John Criswell
390700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
391700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        // Save a reference to the new type.  Resolving the type can modify the
392700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        // symbol table, invalidating the TI variable.
393700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
394700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        Value *ValPtr = TI->second;
395700867bb69ae1469f2b20005a79fd133e83fd552John Criswell
396700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
397f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner        // Determine whether we can fold the two types together, resolving them.
398f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner        // If so, we can use this value.
399700867bb69ae1469f2b20005a79fd133e83fd552John Criswell        //
400f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner        if (!RecursiveResolveTypes(Ty, I->first, ST, ""))
401700867bb69ae1469f2b20005a79fd133e83fd552John Criswell          return cast<GlobalValue>(ValPtr);
402f44c6051e02ddb207d5928b4ef3cde956d2b5025Chris Lattner      }
403fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner    }
404fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner  return 0;  // Otherwise, nothing could be found.
405fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner}
406fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner
4075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge
4098166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// them into the dest module.
4105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
4115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src,
4125c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::map<const Value*, Value*> &ValueMap,
4138166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                    std::multimap<std::string, GlobalVariable *> &AppendingVars,
4145c2d335d86579941b71816196a95a64b277e8963Chris Lattner                        std::string *Err) {
4155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
4165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
417b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
4185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
4205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
42218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
4234ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    GlobalVariable *DGV = 0;
4244ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (SGV->hasName()) {
4254ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // A same named thing is a global variable, because the only two things
42679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
42779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
4285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
429fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner      DGV = cast_or_null<GlobalVariable>(FindGlobalNamed(SGV->getName(),
430fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                                         SGV->getType(), ST));
4314ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    }
4325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4334ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
4344ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner           "Global must either be external or have an initializer!");
4355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4360fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner    bool SGExtern = SGV->isExternal();
4370fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner    bool DGExtern = DGV ? DGV->isExternal() : false;
4380fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner
4394ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
4405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // No linking to be performed, simply create an identical version of the
4418d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // symbol over in the dest module... the initializer will be filled in
4428d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // later by LinkGlobalInits...
4438d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      //
4442719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      GlobalVariable *NewDGV =
4452719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        new GlobalVariable(SGV->getType()->getElementType(),
4462719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                           SGV->isConstant(), SGV->getLinkage(), /*init*/0,
4472719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                           SGV->getName(), Dest);
4482719bac90525985f4ebb97324b4c087d47b54384Chris Lattner
4492719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // If the LLVM runtime renamed the global, but it is an externally visible
4502719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // symbol, DGV must be an existing global with internal linkage.  Rename
4512719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // it.
4522719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){
4532719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        assert(DGV && DGV->getName() == SGV->getName() &&
4542719bac90525985f4ebb97324b4c087d47b54384Chris Lattner               DGV->hasInternalLinkage());
4552719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        DGV->setName("");
4562719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        NewDGV->setName(SGV->getName());  // Force the name back
4572719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        DGV->setName(SGV->getName());     // This will cause a renaming
4582719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        assert(NewDGV->getName() == SGV->getName() &&
4592719bac90525985f4ebb97324b4c087d47b54384Chris Lattner               DGV->getName() != SGV->getName());
4602719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      }
4615c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Make sure to remember this mapping...
4632719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      ValueMap.insert(std::make_pair(SGV, NewDGV));
4648166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (SGV->hasAppendingLinkage())
4658166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        // Keep track that this is an appending variable...
4668166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
4678166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
468c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->isExternal()) {
469c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // If SGV is external or if both SGV & DGV are external..  Just link the
470c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // external globals, we aren't adding anything.
471c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
472c2b97d43a074445c6856d69538a95de09450c26eChris Lattner
473c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (DGV->isExternal()) {   // If DGV is external but SGV is not...
474c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
475c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      DGV->setLinkage(SGV->getLinkage());    // Inherit linkage!
47635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (SGV->hasWeakLinkage() || SGV->hasLinkOnceLinkage()) {
47772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that DGV has LinkOnce, Appending, Weak, or
47872ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // External linkage.  If DGV is Appending, this is an error.
47972ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (DGV->hasAppendingLinkage())
48072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        return Error(Err, "Linking globals named '" + SGV->getName() +
48172ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner                     " ' with 'weak' and 'appending' linkage is not allowed!");
48235956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
48335956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
48435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        return Error(Err, "Global Variable Collision on '" +
48535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
48635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     "' - Global variables differ in const'ness");
48735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
48872ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // Otherwise, just perform the link.
48972ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
49035956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
49135956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      // Linkonce+Weak = Weak
49235956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DGV->hasLinkOnceLinkage() && SGV->hasWeakLinkage())
49335956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        DGV->setLinkage(SGV->getLinkage());
49435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
49535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (DGV->hasWeakLinkage() || DGV->hasLinkOnceLinkage()) {
49672ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that SGV has LinkOnce, Appending, or External
49772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // linkage.  If SGV is Appending, this is an error.
49872ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (SGV->hasAppendingLinkage())
49972ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        return Error(Err, "Linking globals named '" + SGV->getName() +
50072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner                     " ' with 'weak' and 'appending' linkage is not allowed!");
50135956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
50235956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
50335956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        return Error(Err, "Global Variable Collision on '" +
50435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
50535956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner                     "' - Global variables differ in const'ness");
50635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
50772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (!SGV->hasLinkOnceLinkage())
50872ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        DGV->setLinkage(SGV->getLinkage());    // Inherit linkage!
50972ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
51072ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
511c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->getLinkage() != DGV->getLinkage()) {
5124ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      return Error(Err, "Global variables named '" + SGV->getName() +
5134ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                   "' have different linkage specifiers!");
514c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->hasExternalLinkage()) {
515c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // Allow linking two exactly identical external global variables...
516f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
517c2b97d43a074445c6856d69538a95de09450c26eChris Lattner        return Error(Err, "Global Variable Collision on '" +
518c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
519c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                     "' - Global variables differ in const'ness");
520f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner
521f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner      if (SGV->getInitializer() != DGV->getInitializer())
522f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner        return Error(Err, "Global Variable Collision on '" +
523f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner                     SGV->getType()->getDescription() + " %" + SGV->getName() +
524f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner                    "' - External linkage globals have different initializers");
525f85770c9a670dc5c360015125389c389ae9592c7Chris Lattner
526c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
527c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SGV->hasAppendingLinkage()) {
5288166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // No linking is performed yet.  Just insert a new copy of the global, and
5298166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // keep track of the fact that it is an appending variable in the
5308166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // AppendingVars map.  The name is cleared out so that no linkage is
5318166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // performed.
5328166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *NewDGV =
5338166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        new GlobalVariable(SGV->getType()->getElementType(),
5348166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           SGV->isConstant(), SGV->getLinkage(), /*init*/0,
5358166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           "", Dest);
5368166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
5378166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Make sure to remember this mapping...
5388166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      ValueMap.insert(std::make_pair(SGV, NewDGV));
5398166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
5408166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Keep track that this is an appending variable...
5418166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
5424ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    } else {
5434ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      assert(0 && "Unknown linkage!");
5445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
5455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
5465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
5475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
5485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
5495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
5508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all
5518d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest.
5528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//
5538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src,
5545c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::map<const Value*, Value*> &ValueMap,
5555c2d335d86579941b71816196a95a64b277e8963Chris Lattner                            std::string *Err) {
5568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5578d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
5588d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
5598d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
56018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
5618d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5628d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (SGV->hasInitializer()) {      // Only process initialized GV's
5638d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Figure out what the initializer looks like in the dest module...
5644ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      Constant *SInit =
5652f6bb2bce13084dfc4c8acf47f9eae0578267aaaChris Lattner        cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
5668d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
5678d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
5684ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      if (DGV->hasInitializer()) {
5694ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        assert(SGV->getLinkage() == DGV->getLinkage());
5704ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        if (SGV->hasExternalLinkage()) {
5714ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          if (DGV->getInitializer() != SInit)
5724ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner            return Error(Err, "Global Variable Collision on '" +
5734ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         SGV->getType()->getDescription() +"':%"+SGV->getName()+
5744ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner                         " - Global variables have different initializers");
57572ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
5764ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // Nothing is required, mapped values will take the new global
5774ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          // automatically.
5784ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else if (DGV->hasAppendingLinkage()) {
5794ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Appending linkage unimplemented!");
5804ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        } else {
5814ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner          assert(0 && "Unknown linkage!");
5824ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        }
5838d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      } else {
5848d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        // Copy the initializer over now...
5854ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner        DGV->setInitializer(SInit);
5868d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      }
5878d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
5888d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
5898d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return false;
5908d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner}
5915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
59279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// LinkFunctionProtos - Link the functions together between the two modules,
593c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// without doing function bodies... this just adds external function prototypes
594c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// to the Dest function...
5955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
59679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionProtos(Module *Dest, const Module *Src,
5975c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
5985c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
599b91b657e026e86082b665cc875c2881ed075b68dChris Lattner  SymbolTable *ST = (SymbolTable*)&Dest->getSymbolTable();
6005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
601c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
602c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
6035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
6045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
60518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const Function *SF = I;   // SrcFunction
6064ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    Function *DF = 0;
6074ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (SF->hasName())
60879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // The same named thing is a Function, because the only two things
60979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
61079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
6115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
612fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner      DF = cast_or_null<Function>(FindGlobalNamed(SF->getName(), SF->getType(),
613fcd0234de65fc07fb06f3245b60e82bdba78c745Chris Lattner                                                  ST));
6145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
6154ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner    if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
6160fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      // Function does not already exist, simply insert an function signature
6170fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      // identical to SF into the dest module...
6182719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(),
6192719bac90525985f4ebb97324b4c087d47b54384Chris Lattner                                     SF->getName(), Dest);
6202719bac90525985f4ebb97324b4c087d47b54384Chris Lattner
6212719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // If the LLVM runtime renamed the function, but it is an externally
6222719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // visible symbol, DF must be an existing function with internal linkage.
6232719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      // Rename it.
6242719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) {
6252719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage());
6262719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        DF->setName("");
6272719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        NewDF->setName(SF->getName());  // Force the name back
6282719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        DF->setName(SF->getName());     // This will cause a renaming
6292719bac90525985f4ebb97324b4c087d47b54384Chris Lattner        assert(NewDF->getName() == SF->getName() &&
6302719bac90525985f4ebb97324b4c087d47b54384Chris Lattner               DF->getName() != SF->getName());
6312719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      }
6324ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner
6334ad02e726d9b634372b037d4b352d8b63bb9e849Chris Lattner      // ... and remember this mapping...
6342719bac90525985f4ebb97324b4c087d47b54384Chris Lattner      ValueMap.insert(std::make_pair(SF, NewDF));
635c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->isExternal()) {
636c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // If SF is external or if both SF & DF are external..  Just link the
637c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // external functions, we aren't adding anything.
638c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
639c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (DF->isExternal()) {   // If DF is external but SF is not...
640c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // Link the external functions, update linkage qualifiers
641c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
642c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      DF->setLinkage(SF->getLinkage());
643c2b97d43a074445c6856d69538a95de09450c26eChris Lattner
64435956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) {
64572ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that DF has LinkOnce, Weak, or External linkage.
64672ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
64772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
64835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      // Linkonce+Weak = Weak
64935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DF->hasLinkOnceLinkage() && SF->hasWeakLinkage())
65035956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        DF->setLinkage(SF->getLinkage());
65135956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner
65235956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner    } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) {
65372ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      // At this point we know that SF has LinkOnce or External linkage.
65472ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
65572ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner      if (!SF->hasLinkOnceLinkage())   // Don't inherit linkonce linkage
65672ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner        DF->setLinkage(SF->getLinkage());
65772ac148d4964fbff950fe11a0eeeda73973b849bChris Lattner
658c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->getLinkage() != DF->getLinkage()) {
6590fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner      return Error(Err, "Functions named '" + SF->getName() +
6600fec08eb58dd9fffeb72c584aa61a59d71111c8dChris Lattner                   "' have different linkage specifiers!");
661c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else if (SF->hasExternalLinkage()) {
662c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      // The function is defined in both modules!!
663c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      return Error(Err, "Function '" +
664c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                   SF->getFunctionType()->getDescription() + "':\"" +
665c2b97d43a074445c6856d69538a95de09450c26eChris Lattner                   SF->getName() + "\" - Function is already defined!");
666c2b97d43a074445c6856d69538a95de09450c26eChris Lattner    } else {
667c2b97d43a074445c6856d69538a95de09450c26eChris Lattner      assert(0 && "Unknown linkage configuration found!");
6685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
6695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
6705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
6715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
6725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
673c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBody - Copy the source function over into the dest function and
674c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// fix up references to values.  At this point we know that Dest is an external
675c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function, and that Src is not.
6765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
67779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBody(Function *Dest, const Function *Src,
6785c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::map<const Value*, Value*> &GlobalMap,
6795c2d335d86579941b71816196a95a64b277e8963Chris Lattner                             std::string *Err) {
6805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
6815c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> LocalMap;   // Map for function local values
6825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
683c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Go through and convert function arguments over...
68469da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner  Function::aiterator DI = Dest->abegin();
68518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
68669da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner       I != E; ++I, ++DI) {
68769da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    DI->setName(I->getName());  // Copy the name information over...
6885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
6895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Add a mapping to our local map
69069da5cf26143e4542d4bf8c78ffac6d079efe5c9Chris Lattner    LocalMap.insert(std::make_pair(I, DI));
6915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
6925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
6935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the basic blocks, copying the instructions over...
6945c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
69579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
696c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // Create new basic block and add to mapping and the Dest function...
69718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
69818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    LocalMap.insert(std::make_pair(I, DBB));
6995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Loop over all of the instructions in the src basic block, copying them
7015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // over.  Note that this is broken in a strict sense because the cloned
7025c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // instructions will still be referencing values in the Src module, not
7035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // the remapped values.  In our case, however, we will not get caught and
7045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // so we can delay patching the values up until later...
7055c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
70618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
7075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         II != IE; ++II) {
70818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Instruction *DI = II->clone();
70918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      DI->setName(II->getName());
7105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      DBB->getInstList().push_back(DI);
71118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      LocalMap.insert(std::make_pair(II, DI));
7125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
7135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
7145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
715c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // At this point, all of the instructions and values of the function are now
716c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // copied over.  The only problem is that they are still referencing values in
717c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // the Source function as operands.  Loop through all of the operands of the
718c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // functions and patch them up to point to the local versions...
7195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
72018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
72118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
72218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
723221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner           OI != OE; ++OI)
724221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner        *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
7255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
7275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
7285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
730c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBodies - Link in the function bodies that are defined in the
731c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// source module into the DestModule.  This consists basically of copying the
732c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function over and fixing up references to values.
7335c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
73479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBodies(Module *Dest, const Module *Src,
7355c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::map<const Value*, Value*> &ValueMap,
7365c2d335d86579941b71816196a95a64b277e8963Chris Lattner                               std::string *Err) {
7375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
738c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
739c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
7405c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
74118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
74218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (!SF->isExternal()) {                  // No body if function is external
74318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
744c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner
74518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      // DF not external SF external?
74635956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner      if (DF->isExternal()) {
74735956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        // Only provide the function body if there isn't one already.
74835956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner        if (LinkFunctionBody(DF, SF, ValueMap, Err))
74935956558e26e655aaa2e8ad6ee405365271869a4Chris Lattner          return true;
750c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      }
751c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    }
7525c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
7535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
7545c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
7555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
7568166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// LinkAppendingVars - If there were any appending global variables, link them
7578166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner// together now.  Return true on error.
7588166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner//
7598166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattnerstatic bool LinkAppendingVars(Module *M,
7608166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                  std::multimap<std::string, GlobalVariable *> &AppendingVars,
7618166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                              std::string *ErrorMsg) {
7628166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (AppendingVars.empty()) return false; // Nothing to do.
7638166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7648166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Loop over the multimap of appending vars, processing any variables with the
7658166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // same name, forming a new appending global variable with both of the
7668166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // initializers merged together, then rewrite references to the old variables
7678166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // and delete them.
7688166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
7698166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  std::vector<Constant*> Inits;
7708166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  while (AppendingVars.size() > 1) {
7718166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // Get the first two elements in the map...
7728166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    std::multimap<std::string,
7738166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
7748166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7758166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // If the first two elements are for different names, there is no pair...
7768166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    // Otherwise there is a pair, so link them together...
7778166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    if (First->first == Second->first) {
7788166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *G1 = First->second, *G2 = Second->second;
7798166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
7808166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
7818166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7828166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Check to see that they two arrays agree on type...
7838166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (T1->getElementType() != T2->getElementType())
7848166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        return Error(ErrorMsg,
7858166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner         "Appending variables with different element types need to be linked!");
7868166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      if (G1->isConstant() != G2->isConstant())
7878166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        return Error(ErrorMsg,
7888166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                     "Appending variables linked with different const'ness!");
7898166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7908166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      unsigned NewSize = T1->getNumElements() + T2->getNumElements();
7918166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
7928166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7938166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Create the new global variable...
7948166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      GlobalVariable *NG =
7958166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner        new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
7968166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner                           /*init*/0, First->first, M);
7978166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
7988166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Merge the initializer...
7998166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Inits.reserve(NewSize);
800de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
801de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
802de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(cast<Constant>(I->getValues()[i]));
803de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      } else {
804de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        assert(isa<ConstantAggregateZero>(G1->getInitializer()));
805de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        Constant *CV = Constant::getNullValue(T1->getElementType());
806de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
807de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(CV);
808de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      }
809de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
810de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
811de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(cast<Constant>(I->getValues()[i]));
812de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      } else {
813de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        assert(isa<ConstantAggregateZero>(G2->getInitializer()));
814de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        Constant *CV = Constant::getNullValue(T2->getElementType());
815de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner        for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
816de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner          Inits.push_back(CV);
817de512b5b2edebe9c9021a92c7c7a9ae9fbc380d6Chris Lattner      }
8188166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      NG->setInitializer(ConstantArray::get(NewType, Inits));
8198166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Inits.clear();
8208166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8218166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Replace any uses of the two global variables with uses of the new
8228166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // global...
8238166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8248166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // FIXME: This should rewrite simple/straight-forward uses such as
8258166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // getelementptr instructions to not use the Cast!
8268166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      ConstantPointerRef *NGCP = ConstantPointerRef::get(NG);
8278166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      G1->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G1->getType()));
8288166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      G2->replaceAllUsesWith(ConstantExpr::getCast(NGCP, G2->getType()));
8298166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8308166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Remove the two globals from the module now...
8318166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      M->getGlobalList().erase(G1);
8328166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      M->getGlobalList().erase(G2);
8338166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8348166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // Put the new global into the AppendingVars map so that we can handle
8358166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      // linking of more than two vars...
8368166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner      Second->second = NG;
8378166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    }
8388166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner    AppendingVars.erase(First);
8398166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  }
8408166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8418166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  return false;
8428166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner}
84352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
84452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
84552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting
84652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules.  If an
84752f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
8485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem.  Upon failure, the Dest module could be in a modified state, and
8495c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent.
85052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
851f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::LinkModules(Module *Dest, const Module *Src, std::string *ErrorMsg) {
852873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Dest->getEndianness() == Module::AnyEndianness)
853873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner    Dest->setEndianness(Src->getEndianness());
854873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Dest->getPointerSize() == Module::AnyPointerSize)
855873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner    Dest->setPointerSize(Src->getPointerSize());
856873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner
857873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Src->getEndianness() != Module::AnyEndianness &&
858873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner      Dest->getEndianness() != Src->getEndianness())
85943a9994c0164848d44527060da72985c75183a48Chris Lattner    std::cerr << "WARNING: Linking two modules of different endianness!\n";
860873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner  if (Src->getPointerSize() != Module::AnyPointerSize &&
861873c5e7859c1534bf92c9e2747f2b70685059598Chris Lattner      Dest->getPointerSize() != Src->getPointerSize())
86243a9994c0164848d44527060da72985c75183a48Chris Lattner    std::cerr << "WARNING: Linking two modules of different pointer size!\n";
8632c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
8642c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // LinkTypes - Go through the symbol table of the Src module and see if any
8652c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // types are named in the src module that are not named in the Dst module.
8662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Make sure there are no type name conflicts.
8672c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  //
8682c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
8692c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
8705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // ValueMap - Mapping of values from what they used to be in Src, to what they
8715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // are now in Dest.
8725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
8735c2d335d86579941b71816196a95a64b277e8963Chris Lattner  std::map<const Value*, Value*> ValueMap;
8745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
8758166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // AppendingVars - Keep track of global variables in the destination module
8768166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // with appending linkage.  After the module is linked together, they are
8778166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // appended and the module is rewritten.
8788166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
8798166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  std::multimap<std::string, GlobalVariable *> AppendingVars;
8808166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8818166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Add all of the appending globals already in the Dest module to
8828166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // AppendingVars.
8838166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  for (Module::giterator I = Dest->gbegin(), E = Dest->gend(); I != E; ++I)
884f41464618f41b92cf0a0bd14ccc57bce727fb33dChris Lattner    if (I->hasAppendingLinkage())
885f41464618f41b92cf0a0bd14ccc57bce727fb33dChris Lattner      AppendingVars.insert(std::make_pair(I->getName(), I));
8868166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
8878166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // Insert all of the globals in src into the Dest module... without linking
8888166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // initializers (which could refer to functions not yet mapped over).
8898166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
8908166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) return true;
8915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
892c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link the functions together between the two modules, without doing function
893c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // bodies... this just adds external function prototypes to the Dest
894c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // function...  We do this so that when we begin processing function bodies,
895c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // all of the global values that may be referenced are available in our
896c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // ValueMap.
8975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
89879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
8995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
9006cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // Update the initializers in the Dest module now that all globals that may
9016cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // be referenced are in Dest.
9026cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  //
9036cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
9046cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
905c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link in the function bodies that are defined in the source module into the
906c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // DestModule.  This consists basically of copying the function over and
907c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // fixing up references to values.
9085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
90979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
91052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
9118166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  // If there were any appending global variables, link them together now.
9128166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  //
9138166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner  if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
9148166e6eef6518da6f1f805e0d5e2bb22c15dd49cChris Lattner
91552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner  return false;
91652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner}
9179466f5113ba859677a28887c3c7143065e702019Vikram S. Adve
918