rename method
[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   New->setModuleInlineAsm(M->getModuleInlineAsm());
35
36   // Copy all of the type symbol table entries over.
37   const SymbolTable &SymTab = M->getSymbolTable();
38   SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
39   SymbolTable::type_const_iterator TypeE = SymTab.type_end();
40   for (; TypeI != TypeE; ++TypeI)
41     New->addTypeName(TypeI->first, TypeI->second);
42   
43   // Copy all of the dependent libraries over.
44   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
45     New->addLibrary(*I);
46
47   // Create the value map that maps things from the old module over to the new
48   // module.
49   std::map<const Value*, Value*> ValueMap;
50
51   // Loop over all of the global variables, making corresponding globals in the
52   // new module.  Here we add them to the ValueMap and to the new Module.  We
53   // don't worry about attributes or initializers, they will come later.
54   //
55   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
56        I != E; ++I)
57     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
58                                      GlobalValue::ExternalLinkage, 0,
59                                      I->getName(), New);
60
61   // Loop over the functions in the module, making external functions as before
62   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
63     Function *NF =
64       new Function(cast<FunctionType>(I->getType()->getElementType()),
65                    GlobalValue::ExternalLinkage, I->getName(), New);
66     NF->setCallingConv(I->getCallingConv());
67     ValueMap[I]= NF;
68   }
69
70   // Now that all of the things that global variable initializer can refer to
71   // have been created, loop through and copy the global variable referrers
72   // over...  We also set the attributes on the global now.
73   //
74   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
75        I != E; ++I) {
76     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
77     if (I->hasInitializer())
78       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
79                                                  ValueMap)));
80     GV->setLinkage(I->getLinkage());
81   }
82
83   // Similarly, copy over function bodies now...
84   //
85   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
86     Function *F = cast<Function>(ValueMap[I]);
87     if (!I->isExternal()) {
88       Function::arg_iterator DestI = F->arg_begin();
89       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
90            ++J) {
91         DestI->setName(J->getName());
92         ValueMap[J] = DestI++;
93       }
94
95       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
96       CloneFunctionInto(F, I, ValueMap, Returns);
97     }
98
99     F->setLinkage(I->getLinkage());
100   }
101
102   return New;
103 }
104
105 // vim: sw=2