blob: a872551eeae364c61a6bd6d6a198f90f634e058d [file] [log] [blame]
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere4dbb1a2002-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 Lattnerb57ed832003-04-24 17:15:33 +000018#include "llvm/SymbolTable.h"
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000019#include "llvm/Constant.h"
20#include "ValueMapper.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattnere4dbb1a2002-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 Brukman8b2bd4e2003-10-10 17:57:28 +000025/// variables and functions, and making their (initializers and references,
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000026/// respectively) refer to the right globals.
27///
Chris Lattnerdf3c3422004-01-09 06:12:26 +000028Module *llvm::CloneModule(const Module *M) {
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000029 // First off, we need to create the new module...
Chris Lattnerd36ad922003-04-22 18:02:26 +000030 Module *New = new Module(M->getModuleIdentifier());
Chris Lattner0aebf8f2003-04-24 15:54:40 +000031 New->setEndianness(M->getEndianness());
32 New->setPointerSize(M->getPointerSize());
Chris Lattnerb98282d2006-01-18 21:32:45 +000033 New->setTargetTriple(M->getTargetTriple());
Chris Lattner00fcdfe2006-01-24 04:16:34 +000034 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000035
Chris Lattnerb98282d2006-01-18 21:32:45 +000036 // Copy all of the type symbol table entries over.
Chris Lattnerb57ed832003-04-24 17:15:33 +000037 const SymbolTable &SymTab = M->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +000038 SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
39 SymbolTable::type_const_iterator TypeE = SymTab.type_end();
Chris Lattnerb98282d2006-01-18 21:32:45 +000040 for (; TypeI != TypeE; ++TypeI)
Reid Spencere7e96712004-05-25 08:53:40 +000041 New->addTypeName(TypeI->first, TypeI->second);
Chris Lattnerb98282d2006-01-18 21:32:45 +000042
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);
Chris Lattnerb57ed832003-04-24 17:15:33 +000046
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000047 // 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 //
Chris Lattner21d1dde2005-05-09 01:04:34 +000055 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
56 I != E; ++I)
Chris Lattner379a8d22003-04-16 20:28:45 +000057 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
58 GlobalValue::ExternalLinkage, 0,
59 I->getName(), New);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000060
61 // Loop over the functions in the module, making external functions as before
Chris Lattner21d1dde2005-05-09 01:04:34 +000062 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000063 Function *NF =
Chris Lattner21d1dde2005-05-09 01:04:34 +000064 new Function(cast<FunctionType>(I->getType()->getElementType()),
65 GlobalValue::ExternalLinkage, I->getName(), New);
66 NF->setCallingConv(I->getCallingConv());
67 ValueMap[I]= NF;
68 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000069
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 //
Chris Lattner21d1dde2005-05-09 01:04:34 +000074 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
75 I != E; ++I) {
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000076 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
77 if (I->hasInitializer())
78 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
79 ValueMap)));
Chris Lattner379a8d22003-04-16 20:28:45 +000080 GV->setLinkage(I->getLinkage());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000081 }
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()) {
Chris Lattner531f9e92005-03-15 04:54:21 +000088 Function::arg_iterator DestI = F->arg_begin();
Chris Lattner21d1dde2005-05-09 01:04:34 +000089 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
90 ++J) {
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000091 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
Chris Lattner379a8d22003-04-16 20:28:45 +000099 F->setLinkage(I->getLinkage());
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000100 }
101
102 return New;
103}
Reid Spencere7e96712004-05-25 08:53:40 +0000104
105// vim: sw=2