f0ee1e2ae0d17f32084dcdc443f3aace209263b8
[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 is distributed under the University of Illinois Open Source
6 // 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/TypeSymbolTable.h"
19 #include "llvm/Constant.h"
20 #include "llvm/Transforms/Utils/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   // Create the value map that maps things from the old module over to the new
30   // module.
31   DenseMap<const Value*, Value*> ValueMap;
32   return CloneModule(M, ValueMap);
33 }
34
35 Module *llvm::CloneModule(const Module *M,
36                           DenseMap<const Value*, Value*> &ValueMap) {
37   // First off, we need to create the new module...
38   Module *New = new Module(M->getModuleIdentifier());
39   New->setDataLayout(M->getDataLayout());
40   New->setTargetTriple(M->getTargetTriple());
41   New->setModuleInlineAsm(M->getModuleInlineAsm());
42
43   // Copy all of the type symbol table entries over.
44   const TypeSymbolTable &TST = M->getTypeSymbolTable();
45   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
46        TI != TE; ++TI)
47     New->addTypeName(TI->first, TI->second);
48   
49   // Copy all of the dependent libraries over.
50   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
51     New->addLibrary(*I);
52
53   // Loop over all of the global variables, making corresponding globals in the
54   // new module.  Here we add them to the ValueMap and to the new Module.  We
55   // don't worry about attributes or initializers, they will come later.
56   //
57   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
58        I != E; ++I)
59     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
60                                      GlobalValue::ExternalLinkage, 0,
61                                      I->getName(), New);
62
63   // Loop over the functions in the module, making external functions as before
64   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
65     Function *NF =
66       new Function(cast<FunctionType>(I->getType()->getElementType()),
67                    GlobalValue::ExternalLinkage, I->getName(), New);
68     NF->setCallingConv(I->getCallingConv());
69     NF->setParamAttrs(I->getParamAttrs());
70     if (I->hasCollector())
71       NF->setCollector(I->getCollector());
72     ValueMap[I]= NF;
73   }
74
75   // Loop over the aliases in the module
76   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
77        I != E; ++I)
78     ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
79                                   I->getName(), NULL, New);
80   
81   // Now that all of the things that global variable initializer can refer to
82   // have been created, loop through and copy the global variable referrers
83   // over...  We also set the attributes on the global now.
84   //
85   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
86        I != E; ++I) {
87     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
88     if (I->hasInitializer())
89       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
90                                                  ValueMap)));
91     GV->setLinkage(I->getLinkage());
92     GV->setThreadLocal(I->isThreadLocal());
93     GV->setConstant(I->isConstant());
94   }
95
96   // Similarly, copy over function bodies now...
97   //
98   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
99     Function *F = cast<Function>(ValueMap[I]);
100     if (!I->isDeclaration()) {
101       Function::arg_iterator DestI = F->arg_begin();
102       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
103            ++J) {
104         DestI->setName(J->getName());
105         ValueMap[J] = DestI++;
106       }
107
108       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
109       CloneFunctionInto(F, I, ValueMap, Returns);
110     }
111
112     F->setLinkage(I->getLinkage());
113   }
114
115   // And aliases
116   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
117        I != E; ++I) {
118     GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
119     GA->setLinkage(I->getLinkage());
120     if (const Constant* C = I->getAliasee())
121       GA->setAliasee(cast<Constant>(MapValue(C, ValueMap)));
122   }
123   
124   return New;
125 }