For PR761:
[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/TypeSymbolTable.h"
20 #include "llvm/Constant.h"
21 #include "ValueMapper.h"
22 using namespace llvm;
23
24 /// CloneModule - Return an exact copy of the specified module.  This is not as
25 /// easy as it might seem because we have to worry about making copies of global
26 /// variables and functions, and making their (initializers and references,
27 /// respectively) refer to the right globals.
28 ///
29 Module *llvm::CloneModule(const Module *M) {
30   // Create the value map that maps things from the old module over to the new
31   // module.
32   std::map<const Value*, Value*> ValueMap;
33
34   return CloneModule(M, ValueMap);
35 }
36
37 Module *llvm::CloneModule(const Module *M, std::map<const Value*, Value*> &ValueMap) {
38   // First off, we need to create the new module...
39   Module *New = new Module(M->getModuleIdentifier());
40   New->setDataLayout(M->getDataLayout());
41   New->setTargetTriple(M->getTargetTriple());
42   New->setModuleInlineAsm(M->getModuleInlineAsm());
43
44   // Copy all of the type symbol table entries over.
45   const TypeSymbolTable &TST = M->getTypeSymbolTable();
46   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
47        TI != TE; ++TI)
48     New->addTypeName(TI->first, TI->second);
49   
50   // Copy all of the dependent libraries over.
51   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
52     New->addLibrary(*I);
53
54   // Loop over all of the global variables, making corresponding globals in the
55   // new module.  Here we add them to the ValueMap and to the new Module.  We
56   // don't worry about attributes or initializers, they will come later.
57   //
58   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
59        I != E; ++I)
60     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
61                                      GlobalValue::ExternalLinkage, 0,
62                                      I->getName(), New);
63
64   // Loop over the functions in the module, making external functions as before
65   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
66     Function *NF =
67       new Function(cast<FunctionType>(I->getType()->getElementType()),
68                    GlobalValue::ExternalLinkage, I->getName(), New);
69     NF->setCallingConv(I->getCallingConv());
70     ValueMap[I]= NF;
71   }
72
73   // Now that all of the things that global variable initializer can refer to
74   // have been created, loop through and copy the global variable referrers
75   // over...  We also set the attributes on the global now.
76   //
77   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
78        I != E; ++I) {
79     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
80     if (I->hasInitializer())
81       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
82                                                  ValueMap)));
83     GV->setLinkage(I->getLinkage());
84   }
85
86   // Similarly, copy over function bodies now...
87   //
88   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
89     Function *F = cast<Function>(ValueMap[I]);
90     if (!I->isExternal()) {
91       Function::arg_iterator DestI = F->arg_begin();
92       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
93            ++J) {
94         DestI->setName(J->getName());
95         ValueMap[J] = DestI++;
96       }
97
98       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
99       CloneFunctionInto(F, I, ValueMap, Returns);
100     }
101
102     F->setLinkage(I->getLinkage());
103   }
104
105   return New;
106 }
107
108 // vim: sw=2