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