LinkModules.cpp revision d149c053cdbb406b7b5dff24127b4c509420893e
152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===- Linker.cpp - Module Linker Implementation --------------------------===//
252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// This file implements the LLVM module linker.
452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// Specifically, this:
68d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//  * Merges global variables between the two modules
78d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//    * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
8c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner//  * Merges functions between two modules
952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
1052f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//===----------------------------------------------------------------------===//
1152f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
12c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner#include "llvm/Transforms/Utils/Linker.h"
135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/Module.h"
145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/SymbolTable.h"
155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/DerivedTypes.h"
165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner#include "llvm/iOther.h"
1731bcdb822fe9133b1973de51519d34e5813a6184Chris Lattner#include "llvm/Constants.h"
18697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::cerr;
19697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::string;
20697954c15da58bd8b186dbafdedd8b06db770201Chris Lattnerusing std::map;
215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// Error - Simple wrapper function to conditionally assign to E and return true.
235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// This just makes error return conditions a little bit simpler...
245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic inline bool Error(string *E, string Message) {
265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (E) *E = Message;
275c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return true;
285c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
295c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
302c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// LinkTypes - Go through the symbol table of the Src module and see if any
312c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// types are named in the src module that are not named in the Dst module.
322c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner// Make sure there are no type name conflicts.
332c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner//
342c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattnerstatic bool LinkTypes(Module *Dest, const Module *Src, string *Err = 0) {
352c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // No symbol table?  Can't have named types.
362c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (!Src->hasSymbolTable()) return false;
372c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
382c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable       *DestST = Dest->getSymbolTableSure();
392c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable *SrcST  = Src->getSymbolTable();
402c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
412c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Look for a type plane for Type's...
422c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  SymbolTable::const_iterator PI = SrcST->find(Type::TypeTy);
432c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (PI == SrcST->end()) return false;  // No named types, do nothing.
442c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
452c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  const SymbolTable::VarMap &VM = PI->second;
462c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  for (SymbolTable::type_const_iterator I = VM.begin(), E = VM.end();
472c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner       I != E; ++I) {
482c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const string &Name = I->first;
492c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *RHS = cast<Type>(I->second);
502c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
512c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    // Check to see if this type name is already in the dest module...
522c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    const Type *Entry = cast_or_null<Type>(DestST->lookup(Type::TypeTy, Name));
532c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    if (Entry) {     // Yup, the value already exists...
542c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      if (Entry != RHS)            // If it's the same, noop.  Otherwise, error.
552c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner        return Error(Err, "Type named '" + Name +
562c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner                     "' of different shape in modules.\n  Src='" +
579b638019ca8870f418e3b42b39dd34dc7deebb2dChris Lattner                     Entry->getDescription() + "'.\n  Dst='" +
582c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner                     RHS->getDescription() + "'");
592c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    } else {                       // Type not in dest module.  Add it now.
602c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      // TODO: FIXME WHEN TYPES AREN'T CONST
612c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner      DestST->insert(Name, const_cast<Type*>(RHS));
622c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner    }
632c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  }
642c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  return false;
652c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner}
662c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
672d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattnerstatic void PrintMap(const map<const Value*, Value*> &M) {
682d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  for (map<const Value*, Value*>::const_iterator I = M.begin(), E = M.end();
692d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner       I != E; ++I) {
7087182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    cerr << " Fr: " << (void*)I->first << " ";
7187182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->first->dump();
7287182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    cerr << " To: " << (void*)I->second << " ";
7387182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    I->second->dump();
7487182ae6baf28ca6da6ee4bc00cf1857f293acfeChris Lattner    cerr << "\n";
752d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
762d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner}
772d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
782d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// RemapOperand - Use LocalMap and GlobalMap to convert references from one
805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// module to another.  This is somewhat sophisticated in that it can
815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// automatically handle constant references correctly as well...
825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
838d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
84d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner                           map<const Value*, Value*> *GlobalMap = 0) {
855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (I != LocalMap.end()) return I->second;
875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (GlobalMap) {
895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    I = GlobalMap->find(In);
905c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    if (I != GlobalMap->end()) return I->second;
915c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
925c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
938d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Check to see if it's a constant that we are interesting in transforming...
9418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (const Constant *CPV = dyn_cast<Constant>(In)) {
956cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    if (!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV))
9618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
978d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
98e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    Constant *Result = 0;
998d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
10018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
101697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPA->getValues();
102697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
103e306d94782b1c608857738279852dcc050c66004Chris Lattner      for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1048d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
105e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
106e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
10718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
108697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      const std::vector<Use> &Ops = CPS->getValues();
109697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      std::vector<Constant*> Operands(Ops.size());
1108d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      for (unsigned i = 0; i < Ops.size(); ++i)
1118d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        Operands[i] =
112e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner          cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
113e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
114e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner    } else if (isa<ConstantPointerNull>(CPV)) {
11518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Result = const_cast<Constant*>(CPV);
11618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    } else if (const ConstantPointerRef *CPR =
11718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                      dyn_cast<ConstantPointerRef>(CPV)) {
1188d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
119e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Result = ConstantPointerRef::get(cast<GlobalValue>(V));
1206cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
121b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      if (CE->getOpcode() == Instruction::GetElementPtr) {
122b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
123b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        std::vector<Constant*> Indices;
124b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Indices.reserve(CE->getNumOperands()-1);
125b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
126b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
127b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner                                                        LocalMap, GlobalMap)));
128b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner
129b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
130b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner      } else if (CE->getNumOperands() == 1) {
131ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        // Cast instruction
132ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        assert(CE->getOpcode() == Instruction::Cast);
1336cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
134ad333484ea5ae976b83e35ccd3f6cfa6e71290e2Chris Lattner        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
1356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else if (CE->getNumOperands() == 2) {
1366cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        // Binary operator...
1376cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
1386cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
1396cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
1406cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
141e8e4605021141d689493132a9c7c6fce6294937fChris Lattner                                   cast<Constant>(V2));
1426cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      } else {
143b319faff77a5bb12e753c45662f6c532b77f0b91Chris Lattner        assert(0 && "Unknown constant expr type!");
1446cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner      }
1456cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
1468d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    } else {
1478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      assert(0 && "Unknown type of derived type constant value!");
1488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
1498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
1508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    // Cache the mapping in our local map structure...
151d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    if (GlobalMap)
152d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      GlobalMap->insert(std::make_pair(In, Result));
153d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner    else
154d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner      LocalMap.insert(std::make_pair(In, Result));
1558d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    return Result;
1568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
1572d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1582d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  cerr << "XXX LocalMap: \n";
1592d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  PrintMap(LocalMap);
1602d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1612d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  if (GlobalMap) {
1622d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    cerr << "XXX GlobalMap: \n";
1632d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner    PrintMap(*GlobalMap);
1642d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner  }
1652d3e8bba628843bf2dffa21f69d9b45098d3bfc4Chris Lattner
1666cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
1678d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  assert(0 && "Couldn't remap value!");
1688d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return 0;
1695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
1705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// LinkGlobals - Loop through the global variables in the src module and merge
1735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// them into the dest module...
1745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
1755c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattnerstatic bool LinkGlobals(Module *Dest, const Module *Src,
1765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                        map<const Value*, Value*> &ValueMap, string *Err = 0) {
1775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
1785c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
1795c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
1805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1815c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
1825c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
1835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
18418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
1855c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Value *V;
1865c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // If the global variable has a name, and that name is already in use in the
1885c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Dest module, make sure that the name is a compatible global variable...
1895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
190e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner    if (SGV->hasExternalLinkage() && SGV->hasName() &&
191e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	(V = ST->lookup(SGV->getType(), SGV->getName())) &&
192e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	cast<GlobalVariable>(V)->hasExternalLinkage()) {
1935c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // The same named thing is a global variable, because the only two things
19479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
19579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
1965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
1975c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(V);
1985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
1995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Check to see if the two GV's have the same Const'ness...
2008d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      if (SGV->isConstant() != DGV->isConstant())
2015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner        return Error(Err, "Global Variable Collision on '" +
2028d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                     SGV->getType()->getDescription() + "':%" + SGV->getName() +
2035c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner                     " - Global variables differ in const'ness");
2045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2058d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Okay, everything is cool, remember the mapping...
206697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
2075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    } else {
2085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // No linking to be performed, simply create an identical version of the
2098d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // symbol over in the dest module... the initializer will be filled in
2108d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // later by LinkGlobalInits...
2118d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      //
2128d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV =
2137a1767520611d9ff6face702068de858e1cadf2cChris Lattner        new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
214e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner                           SGV->hasInternalLinkage(), 0, SGV->getName());
2155c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2165c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Add the new global to the dest module
2178d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      Dest->getGlobalList().push_back(DGV);
2185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Make sure to remember this mapping...
220697954c15da58bd8b186dbafdedd8b06db770201Chris Lattner      ValueMap.insert(std::make_pair(SGV, DGV));
2215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
2225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
2235c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
2245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
2255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2278d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// LinkGlobalInits - Update the initializers in the Dest module now that all
2288d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner// globals that may be referenced are in Dest.
2298d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner//
2308d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattnerstatic bool LinkGlobalInits(Module *Dest, const Module *Src,
2318d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                            map<const Value*, Value*> &ValueMap,
2328d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                            string *Err = 0) {
2338d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2348d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Loop over all of the globals in the src module, mapping them over as we go
2358d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  //
2368d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
23718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const GlobalVariable *SGV = I;
2388d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2398d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    if (SGV->hasInitializer()) {      // Only process initialized GV's
2408d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      // Figure out what the initializer looks like in the dest module...
241e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner      Constant *DInit =
242e9bb2df410f7a22decad9a883f7139d5857c1520Chris Lattner        cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
2438d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner
2448d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
245e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner      if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
246e4d25aad16bfc2a26595f6255fc2bfdaa6c532a7Chris Lattner	  DGV->hasExternalLinkage()) {
2478d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        if (DGV->getInitializer() != DInit)
2488d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner          return Error(Err, "Global Variable Collision on '" +
2498d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                       SGV->getType()->getDescription() + "':%" +SGV->getName()+
2508d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner                       " - Global variables have different initializers");
2518d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      } else {
2528d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        // Copy the initializer over now...
2538d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner        DGV->setInitializer(DInit);
2548d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner      }
2558d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner    }
2568d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  }
2578d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  return false;
2588d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner}
2595c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
26079df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// LinkFunctionProtos - Link the functions together between the two modules,
261c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// without doing function bodies... this just adds external function prototypes
262c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// to the Dest function...
2635c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
26479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionProtos(Module *Dest, const Module *Src,
26579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner                               map<const Value*, Value*> &ValueMap,
26679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner                               string *Err = 0) {
2675c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // We will need a module level symbol table if the src module has a module
2685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // level symbol table...
2695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  SymbolTable *ST = Src->getSymbolTable() ? Dest->getSymbolTableSure() : 0;
2705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
271c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
272c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
2735c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
2745c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
27518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    const Function *SF = I;   // SrcFunction
2765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    Value *V;
2775c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
278c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // If the function has a name, and that name is already in use in the Dest
279c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // module, make sure that the name is a compatible function...
2805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
28118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (SF->hasExternalLinkage() && SF->hasName() &&
28218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner	(V = ST->lookup(SF->getType(), SF->getName())) &&
28379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner	cast<Function>(V)->hasExternalLinkage()) {
28479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // The same named thing is a Function, because the only two things
28579df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // that may be in a module level symbol table are Global Vars and
28679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner      // Functions, and they both have distinct, nonoverlapping, possible types.
2875c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      //
28818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = cast<Function>(V);   // DestFunction
2895c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
290c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner      // Check to make sure the function is not defined in both modules...
29118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (!SF->isExternal() && !DF->isExternal())
29279df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner        return Error(Err, "Function '" +
29318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                     SF->getFunctionType()->getDescription() + "':\"" +
29418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                     SF->getName() + "\" - Function is already defined!");
2955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
2965c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // Otherwise, just remember this mapping...
29718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
2985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    } else {
299c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner      // Function does not already exist, simply insert an external function
30018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      // signature identical to SF into the dest module...
30118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = new Function(SF->getFunctionType(),
30218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                                  SF->hasInternalLinkage(),
30318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner                                  SF->getName());
3045c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
305c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner      // Add the function signature to the dest module...
30618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Dest->getFunctionList().push_back(DF);
3075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      // ... and remember this mapping...
30918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      ValueMap.insert(std::make_pair(SF, DF));
3105c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
3115c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3125c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3135c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3145c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
315c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBody - Copy the source function over into the dest function and
316c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// fix up references to values.  At this point we know that Dest is an external
317c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function, and that Src is not.
3185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
31979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBody(Function *Dest, const Function *Src,
320d149c053cdbb406b7b5dff24127b4c509420893eChris Lattner                             map<const Value*, Value*> &GlobalMap,
32179df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner                             string *Err = 0) {
3225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
323c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  map<const Value*, Value*> LocalMap;   // Map for function local values
3245c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
325c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Go through and convert function arguments over...
32618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
32718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner       I != E; ++I) {
328c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // Create the new function argument and add to the dest function...
32918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    Argument *DFA = new Argument(I->getType(), I->getName());
33018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    Dest->getArgumentList().push_back(DFA);
3315c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Add a mapping to our local map
33318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    LocalMap.insert(std::make_pair(I, DFA));
3345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3355c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3365c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // Loop over all of the basic blocks, copying the instructions over...
3375c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
33879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
339c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner    // Create new basic block and add to mapping and the Dest function...
34018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
34118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    LocalMap.insert(std::make_pair(I, DBB));
3425c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // Loop over all of the instructions in the src basic block, copying them
3445c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // over.  Note that this is broken in a strict sense because the cloned
3455c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // instructions will still be referencing values in the Src module, not
3465c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // the remapped values.  In our case, however, we will not get caught and
3475c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    // so we can delay patching the values up until later...
3485c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    //
34918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::const_iterator II = I->begin(), IE = I->end();
3505c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner         II != IE; ++II) {
35118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Instruction *DI = II->clone();
35218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      DI->setName(II->getName());
3535c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner      DBB->getInstList().push_back(DI);
35418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      LocalMap.insert(std::make_pair(II, DI));
3555c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner    }
3565c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3575c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
358c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // At this point, all of the instructions and values of the function are now
359c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // copied over.  The only problem is that they are still referencing values in
360c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // the Source function as operands.  Loop through all of the operands of the
361c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // functions and patch them up to point to the local versions...
3625c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
36318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
36418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
36518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
366221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner           OI != OE; ++OI)
367221d688a5ef21a22c2368c9fff0e92d7966c95e5Chris Lattner        *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
3685c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3695c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
3705c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
3715c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
3725c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
373c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// LinkFunctionBodies - Link in the function bodies that are defined in the
374c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// source module into the DestModule.  This consists basically of copying the
375c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner// function over and fixing up references to values.
3765c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner//
37779df7c0aaa18129e55968c8783ef8346807bd4afChris Lattnerstatic bool LinkFunctionBodies(Module *Dest, const Module *Src,
37879df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner                               map<const Value*, Value*> &ValueMap,
37979df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner                               string *Err = 0) {
3805c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
381c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Loop over all of the functions in the src module, mapping them over as we
382c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // go
3835c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
38418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
38518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    if (!SF->isExternal()) {                  // No body if function is external
38618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
387c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner
38818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      // DF not external SF external?
38918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (!DF->isExternal()) {
390c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        if (Err)
39118961504fc2b299578dba817900a0696cf3ccc4dChris Lattner          *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
392c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner                 "' body multiply defined!";
393c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner        return true;
394c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner      }
3955c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
39618961504fc2b299578dba817900a0696cf3ccc4dChris Lattner      if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
397c2d774b6c1d38421c435b6d3cfaa10402c900aebChris Lattner    }
3985c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  }
3995c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  return false;
4005c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner}
4015c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
40252f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
40352f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
40452f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// LinkModules - This function links two modules together, with the resulting
40552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// left module modified to be the composite of the two input modules.  If an
40652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
4075c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// the problem.  Upon failure, the Dest module could be in a modified state, and
4085c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner// shouldn't be relied on to be consistent.
40952f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner//
410b1443b17491163db6a207959d10e6d6d83cd28fdChris Lattnerbool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) {
4112c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
4122c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // LinkTypes - Go through the symbol table of the Src module and see if any
4132c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // types are named in the src module that are not named in the Dst module.
4142c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  // Make sure there are no type name conflicts.
4152c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  //
4162c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner  if (LinkTypes(Dest, Src, ErrorMsg)) return true;
4172c236f3e20a0fb84b5948efa6bb7bb60d759cb32Chris Lattner
4185c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // ValueMap - Mapping of values from what they used to be in Src, to what they
4195c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  // are now in Dest.
4205c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
4215c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  map<const Value*, Value*> ValueMap;
4225c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4238d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // Insert all of the globals in src into the Dest module... without
4248d2de8a82cce67513debee7a3fa5aca0189b4105Chris Lattner  // initializers
4255c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;
4265c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
427c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link the functions together between the two modules, without doing function
428c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // bodies... this just adds external function prototypes to the Dest
429c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // function...  We do this so that when we begin processing function bodies,
430c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // all of the global values that may be referenced are available in our
431c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // ValueMap.
4325c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
43379df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true;
4345c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner
4356cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // Update the initializers in the Dest module now that all globals that may
4366cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  // be referenced are in Dest.
4376cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  //
4386cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner  if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
4396cdf1971bdf88ddd9a7d46b5f5f975497d68c38eChris Lattner
440c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // Link in the function bodies that are defined in the source module into the
441c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // DestModule.  This consists basically of copying the function over and
442c8cc4cb03bd90f89be7fe1649542a2d5ae689632Chris Lattner  // fixing up references to values.
4435c377c524a6929cdaa683f3f034b3fc01526b264Chris Lattner  //
44479df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner  if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
44552f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner
44652f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner  return false;
44752f7e908cb634f9b9b539d5c6670b8a065478915Chris Lattner}
4489466f5113ba859677a28887c3c7143065e702019Vikram S. Adve
449