blob: a08fa35065ccc089d84395db4d3770f3730e706e [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
18#include "llvm/Constant.h"
Dan Gohman05ea54e2010-08-24 18:50:07 +000019#include "llvm/Transforms/Utils/ValueMapper.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022/// CloneModule - Return an exact copy of the specified module. This is not as
23/// easy as it might seem because we have to worry about making copies of global
Misha Brukmancf00c4a2003-10-10 17:57:28 +000024/// variables and functions, and making their (initializers and references,
Chris Lattner51cbcbf2002-11-20 20:47:41 +000025/// respectively) refer to the right globals.
26///
Chris Lattnerf7703df2004-01-09 06:12:26 +000027Module *llvm::CloneModule(const Module *M) {
Chris Lattner782e6012006-05-17 18:05:35 +000028 // Create the value map that maps things from the old module over to the new
29 // module.
Devang Patel774cca72010-06-24 00:00:42 +000030 ValueToValueMapTy VMap;
Devang Patel29d3dd82010-06-23 23:55:51 +000031 return CloneModule(M, VMap);
Chris Lattner782e6012006-05-17 18:05:35 +000032}
33
Chris Lattner1afcace2011-07-09 17:41:24 +000034Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
35 // First off, we need to create the new module.
Owen Anderson8b477ed2009-07-01 16:58:40 +000036 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
Reid Spencer26f23852007-01-26 08:11:39 +000037 New->setDataLayout(M->getDataLayout());
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000038 New->setTargetTriple(M->getTargetTriple());
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000039 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattner1afcace2011-07-09 17:41:24 +000040
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000041 // Copy all of the dependent libraries over.
42 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
43 New->addLibrary(*I);
Chris Lattner892310e2003-04-24 17:15:33 +000044
Chris Lattner51cbcbf2002-11-20 20:47:41 +000045 // Loop over all of the global variables, making corresponding globals in the
Devang Patel29d3dd82010-06-23 23:55:51 +000046 // new module. Here we add them to the VMap and to the new Module. We
Chris Lattner51cbcbf2002-11-20 20:47:41 +000047 // don't worry about attributes or initializers, they will come later.
48 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000049 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000050 I != E; ++I) {
Owen Andersone9b11b42009-07-08 19:03:57 +000051 GlobalVariable *GV = new GlobalVariable(*New,
Owen Anderson3d29df32009-07-08 01:26:06 +000052 I->getType()->getElementType(),
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000053 false,
54 GlobalValue::ExternalLinkage, 0,
Owen Andersone9b11b42009-07-08 19:03:57 +000055 I->getName());
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000056 GV->setAlignment(I->getAlignment());
Devang Patel29d3dd82010-06-23 23:55:51 +000057 VMap[I] = GV;
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000058 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000059
60 // Loop over the functions in the module, making external functions as before
Chris Lattnerc154cef2005-05-09 01:04:34 +000061 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen00b168892005-07-27 06:12:32 +000062 Function *NF =
Gabor Greif051a9502008-04-06 20:25:17 +000063 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
64 GlobalValue::ExternalLinkage, I->getName(), New);
Duncan Sands28c3cff2008-05-26 19:58:59 +000065 NF->copyAttributesFrom(I);
Devang Patel29d3dd82010-06-23 23:55:51 +000066 VMap[I] = NF;
Chris Lattnerc154cef2005-05-09 01:04:34 +000067 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000068
Anton Korobeynikova2895112007-07-10 19:07:35 +000069 // Loop over the aliases in the module
70 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
71 I != E; ++I)
Devang Patel29d3dd82010-06-23 23:55:51 +000072 VMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
Anton Korobeynikova2895112007-07-10 19:07:35 +000073 I->getName(), NULL, New);
74
Chris Lattner51cbcbf2002-11-20 20:47:41 +000075 // Now that all of the things that global variable initializer can refer to
76 // have been created, loop through and copy the global variable referrers
77 // over... We also set the attributes on the global now.
78 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000079 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
80 I != E; ++I) {
Devang Patel29d3dd82010-06-23 23:55:51 +000081 GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000082 if (I->hasInitializer())
Chris Lattner1afcace2011-07-09 17:41:24 +000083 GV->setInitializer(MapValue(I->getInitializer(), VMap));
Chris Lattner4ad02e72003-04-16 20:28:45 +000084 GV->setLinkage(I->getLinkage());
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +000085 GV->setThreadLocal(I->isThreadLocal());
86 GV->setConstant(I->isConstant());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000087 }
88
89 // Similarly, copy over function bodies now...
90 //
91 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Devang Patel29d3dd82010-06-23 23:55:51 +000092 Function *F = cast<Function>(VMap[I]);
Reid Spencer5cbf9852007-01-30 20:08:39 +000093 if (!I->isDeclaration()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +000094 Function::arg_iterator DestI = F->arg_begin();
Chris Lattnerc154cef2005-05-09 01:04:34 +000095 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
96 ++J) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000097 DestI->setName(J->getName());
Devang Patel29d3dd82010-06-23 23:55:51 +000098 VMap[J] = DestI++;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000099 }
100
Chris Lattnerec1bea02009-08-27 04:02:30 +0000101 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
Dan Gohman6cb8c232010-08-26 15:41:53 +0000102 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000103 }
104
Chris Lattner4ad02e72003-04-16 20:28:45 +0000105 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000106 }
107
Anton Korobeynikova2895112007-07-10 19:07:35 +0000108 // And aliases
109 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
110 I != E; ++I) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000111 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
Anton Korobeynikova2895112007-07-10 19:07:35 +0000112 GA->setLinkage(I->getLinkage());
Chris Lattner1afcace2011-07-09 17:41:24 +0000113 if (const Constant *C = I->getAliasee())
114 GA->setAliasee(MapValue(C, VMap));
Anton Korobeynikova2895112007-07-10 19:07:35 +0000115 }
Devang Pateld8800e72010-06-22 18:52:38 +0000116
117 // And named metadata....
118 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
119 E = M->named_metadata_end(); I != E; ++I) {
120 const NamedMDNode &NMD = *I;
Dan Gohman17aa92c2010-07-21 23:38:33 +0000121 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
Devang Pateld8800e72010-06-22 18:52:38 +0000122 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
Chris Lattner1afcace2011-07-09 17:41:24 +0000123 NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
Devang Pateld8800e72010-06-22 18:52:38 +0000124 }
Devang Patel3bf329f2010-06-22 22:50:42 +0000125
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000126 return New;
127}