Make sure that cloning a module clones its target triple and dependent
[oota-llvm.git] / lib / Transforms / Utils / CloneModule.cpp
1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the CloneModule interface which makes a copy of an
11 // entire module.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/Utils/Cloning.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/Constant.h"
20 #include "ValueMapper.h"
21 using namespace llvm;
22
23 /// CloneModule - Return an exact copy of the specified module.  This is not as
24 /// easy as it might seem because we have to worry about making copies of global
25 /// variables and functions, and making their (initializers and references,
26 /// respectively) refer to the right globals.
27 ///
28 Module *llvm::CloneModule(const Module *M) {
29   // First off, we need to create the new module...
30   Module *New = new Module(M->getModuleIdentifier());
31   New->setEndianness(M->getEndianness());
32   New->setPointerSize(M->getPointerSize());
33   New->setTargetTriple(M->getTargetTriple());
34
35   // Copy all of the type symbol table entries over.
36   const SymbolTable &SymTab = M->getSymbolTable();
37   SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
38   SymbolTable::type_const_iterator TypeE = SymTab.type_end();
39   for (; TypeI != TypeE; ++TypeI)
40     New->addTypeName(TypeI->first, TypeI->second);
41   
42   // Copy all of the dependent libraries over.
43   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
44     New->addLibrary(*I);
45
46   // Create the value map that maps things from the old module over to the new
47   // module.
48   std::map<const Value*, Value*> ValueMap;
49
50   // Loop over all of the global variables, making corresponding globals in the
51   // new module.  Here we add them to the ValueMap and to the new Module.  We
52   // don't worry about attributes or initializers, they will come later.
53   //
54   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
55        I != E; ++I)
56     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
57                                      GlobalValue::ExternalLinkage, 0,
58                                      I->getName(), New);
59
60   // Loop over the functions in the module, making external functions as before
61   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
62     Function *NF =
63       new Function(cast<FunctionType>(I->getType()->getElementType()),
64                    GlobalValue::ExternalLinkage, I->getName(), New);
65     NF->setCallingConv(I->getCallingConv());
66     ValueMap[I]= NF;
67   }
68
69   // Now that all of the things that global variable initializer can refer to
70   // have been created, loop through and copy the global variable referrers
71   // over...  We also set the attributes on the global now.
72   //
73   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
74        I != E; ++I) {
75     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
76     if (I->hasInitializer())
77       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
78                                                  ValueMap)));
79     GV->setLinkage(I->getLinkage());
80   }
81
82   // Similarly, copy over function bodies now...
83   //
84   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
85     Function *F = cast<Function>(ValueMap[I]);
86     if (!I->isExternal()) {
87       Function::arg_iterator DestI = F->arg_begin();
88       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
89            ++J) {
90         DestI->setName(J->getName());
91         ValueMap[J] = DestI++;
92       }
93
94       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
95       CloneFunctionInto(F, I, ValueMap, Returns);
96     }
97
98     F->setLinkage(I->getLinkage());
99   }
100
101   return New;
102 }
103
104 // vim: sw=2