blob: a4460fa69b1b7139675bb1107f241e7ac6cde483 [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"
Reid Spencer78d033e2007-01-06 07:24:44 +000019#include "llvm/TypeSymbolTable.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020#include "llvm/Constant.h"
21#include "ValueMapper.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner51cbcbf2002-11-20 20:47:41 +000024/// 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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000026/// variables and functions, and making their (initializers and references,
Chris Lattner51cbcbf2002-11-20 20:47:41 +000027/// respectively) refer to the right globals.
28///
Chris Lattnerf7703df2004-01-09 06:12:26 +000029Module *llvm::CloneModule(const Module *M) {
Chris Lattner782e6012006-05-17 18:05:35 +000030 // 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
37Module *llvm::CloneModule(const Module *M, std::map<const Value*, Value*> &ValueMap) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000038 // First off, we need to create the new module...
Chris Lattner37f59412003-04-22 18:02:26 +000039 Module *New = new Module(M->getModuleIdentifier());
Reid Spencer26f23852007-01-26 08:11:39 +000040 New->setDataLayout(M->getDataLayout());
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000041 New->setTargetTriple(M->getTargetTriple());
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000042 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000043
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000044 // Copy all of the type symbol table entries over.
Reid Spencer78d033e2007-01-06 07:24:44 +000045 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);
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000049
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);
Chris Lattner892310e2003-04-24 17:15:33 +000053
Chris Lattner51cbcbf2002-11-20 20:47:41 +000054 // 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 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000058 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
59 I != E; ++I)
Chris Lattner4ad02e72003-04-16 20:28:45 +000060 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
61 GlobalValue::ExternalLinkage, 0,
62 I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000063
64 // Loop over the functions in the module, making external functions as before
Chris Lattnerc154cef2005-05-09 01:04:34 +000065 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen00b168892005-07-27 06:12:32 +000066 Function *NF =
Chris Lattnerc154cef2005-05-09 01:04:34 +000067 new Function(cast<FunctionType>(I->getType()->getElementType()),
68 GlobalValue::ExternalLinkage, I->getName(), New);
69 NF->setCallingConv(I->getCallingConv());
70 ValueMap[I]= NF;
71 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000072
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 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000077 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
78 I != E; ++I) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000079 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
80 if (I->hasInitializer())
81 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
82 ValueMap)));
Chris Lattner4ad02e72003-04-16 20:28:45 +000083 GV->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000084 }
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]);
Reid Spencer5cbf9852007-01-30 20:08:39 +000090 if (!I->isDeclaration()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +000091 Function::arg_iterator DestI = F->arg_begin();
Chris Lattnerc154cef2005-05-09 01:04:34 +000092 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
93 ++J) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000094 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
Chris Lattner4ad02e72003-04-16 20:28:45 +0000102 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000103 }
104
105 return New;
106}
Reid Spencer9231ac82004-05-25 08:53:40 +0000107
108// vim: sw=2