blob: 309b280cc02b0899ff3331f941269f348fa87dd0 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner51cbcbf2002-11-20 20:47:41 +00009//
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"
Chris Lattner892310e2003-04-24 17:15:33 +000018#include "llvm/SymbolTable.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000019#include "llvm/Constant.h"
20#include "ValueMapper.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner51cbcbf2002-11-20 20:47:41 +000023/// 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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000025/// variables and functions, and making their (initializers and references,
Chris Lattner51cbcbf2002-11-20 20:47:41 +000026/// respectively) refer to the right globals.
27///
Chris Lattnerf7703df2004-01-09 06:12:26 +000028Module *llvm::CloneModule(const Module *M) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000029 // First off, we need to create the new module...
Chris Lattner37f59412003-04-22 18:02:26 +000030 Module *New = new Module(M->getModuleIdentifier());
Chris Lattner7899b742003-04-24 15:54:40 +000031 New->setEndianness(M->getEndianness());
32 New->setPointerSize(M->getPointerSize());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000033
Chris Lattner892310e2003-04-24 17:15:33 +000034 // Copy all of the type symbol table entries over...
35 const SymbolTable &SymTab = M->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +000036 SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
37 SymbolTable::type_const_iterator TypeE = SymTab.type_end();
38 for ( ; TypeI != TypeE; ++TypeI ) {
39 New->addTypeName(TypeI->first, TypeI->second);
40 }
Chris Lattner892310e2003-04-24 17:15:33 +000041
Chris Lattner51cbcbf2002-11-20 20:47:41 +000042 // Create the value map that maps things from the old module over to the new
43 // module.
44 std::map<const Value*, Value*> ValueMap;
45
46 // Loop over all of the global variables, making corresponding globals in the
47 // new module. Here we add them to the ValueMap and to the new Module. We
48 // don't worry about attributes or initializers, they will come later.
49 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000050 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
51 I != E; ++I)
Chris Lattner4ad02e72003-04-16 20:28:45 +000052 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
53 GlobalValue::ExternalLinkage, 0,
54 I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000055
56 // Loop over the functions in the module, making external functions as before
Chris Lattnerc154cef2005-05-09 01:04:34 +000057 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen00b168892005-07-27 06:12:32 +000058 Function *NF =
Chris Lattnerc154cef2005-05-09 01:04:34 +000059 new Function(cast<FunctionType>(I->getType()->getElementType()),
60 GlobalValue::ExternalLinkage, I->getName(), New);
61 NF->setCallingConv(I->getCallingConv());
62 ValueMap[I]= NF;
63 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000064
65 // Now that all of the things that global variable initializer can refer to
66 // have been created, loop through and copy the global variable referrers
67 // over... We also set the attributes on the global now.
68 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000069 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
70 I != E; ++I) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000071 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
72 if (I->hasInitializer())
73 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
74 ValueMap)));
Chris Lattner4ad02e72003-04-16 20:28:45 +000075 GV->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000076 }
77
78 // Similarly, copy over function bodies now...
79 //
80 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
81 Function *F = cast<Function>(ValueMap[I]);
82 if (!I->isExternal()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +000083 Function::arg_iterator DestI = F->arg_begin();
Chris Lattnerc154cef2005-05-09 01:04:34 +000084 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
85 ++J) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000086 DestI->setName(J->getName());
87 ValueMap[J] = DestI++;
88 }
89
90 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
91 CloneFunctionInto(F, I, ValueMap, Returns);
92 }
93
Chris Lattner4ad02e72003-04-16 20:28:45 +000094 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000095 }
96
97 return New;
98}
Reid Spencer9231ac82004-05-25 08:53:40 +000099
100// vim: sw=2