blob: d05d25164cdf12089d70c5ad8bb46910abaad5d5 [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//
Chris Lattnerf3ebc3f2007-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 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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constant.h"
17#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/Module.h"
Dan Gohmana2095032010-08-24 18:50:07 +000019#include "llvm/Transforms/Utils/ValueMapper.h"
Tom Stellard0a4e9a32014-10-01 17:14:57 +000020#include "llvm-c/Core.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 Lattner2e266802006-05-17 18:05:35 +000029 // Create the value map that maps things from the old module over to the new
30 // module.
Devang Pateld8dedee2010-06-24 00:00:42 +000031 ValueToValueMapTy VMap;
Devang Patelb8f11de2010-06-23 23:55:51 +000032 return CloneModule(M, VMap);
Chris Lattner2e266802006-05-17 18:05:35 +000033}
34
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000035Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
36 // First off, we need to create the new module.
Owen Anderson6773d382009-07-01 16:58:40 +000037 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
Reid Spencer3ac38e92007-01-26 08:11:39 +000038 New->setDataLayout(M->getDataLayout());
Chris Lattnerb98282d2006-01-18 21:32:45 +000039 New->setTargetTriple(M->getTargetTriple());
Chris Lattner00fcdfe2006-01-24 04:16:34 +000040 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000041
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000042 // Loop over all of the global variables, making corresponding globals in the
Devang Patelb8f11de2010-06-23 23:55:51 +000043 // new module. Here we add them to the VMap and to the new Module. We
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000044 // don't worry about attributes or initializers, they will come later.
45 //
Chris Lattner21d1dde2005-05-09 01:04:34 +000046 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Nick Lewycky03c5fa12008-10-09 06:27:14 +000047 I != E; ++I) {
Owen Andersonb17f3292009-07-08 19:03:57 +000048 GlobalVariable *GV = new GlobalVariable(*New,
Owen Anderson5948fdf2009-07-08 01:26:06 +000049 I->getType()->getElementType(),
Eli Friedman4d051982011-08-15 21:05:06 +000050 I->isConstant(), I->getLinkage(),
Craig Topperf40110f2014-04-25 05:29:35 +000051 (Constant*) nullptr, I->getName(),
52 (GlobalVariable*) nullptr,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000053 I->getThreadLocalMode(),
Eli Friedman4d051982011-08-15 21:05:06 +000054 I->getType()->getAddressSpace());
55 GV->copyAttributesFrom(I);
Devang Patelb8f11de2010-06-23 23:55:51 +000056 VMap[I] = GV;
Nick Lewycky03c5fa12008-10-09 06:27:14 +000057 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000058
59 // Loop over the functions in the module, making external functions as before
Chris Lattner21d1dde2005-05-09 01:04:34 +000060 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000061 Function *NF =
Gabor Greife9ecc682008-04-06 20:25:17 +000062 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
Eli Friedman4d051982011-08-15 21:05:06 +000063 I->getLinkage(), I->getName(), New);
Duncan Sandsdd7daee2008-05-26 19:58:59 +000064 NF->copyAttributesFrom(I);
Devang Patelb8f11de2010-06-23 23:55:51 +000065 VMap[I] = NF;
Chris Lattner21d1dde2005-05-09 01:04:34 +000066 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000067
Anton Korobeynikov76547342007-07-10 19:07:35 +000068 // Loop over the aliases in the module
69 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
Eli Friedman4d051982011-08-15 21:05:06 +000070 I != E; ++I) {
Rafael Espindola4fe00942014-05-16 13:34:04 +000071 auto *PTy = cast<PointerType>(I->getType());
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +000072 auto *GA =
73 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
74 I->getLinkage(), I->getName(), New);
Eli Friedman4d051982011-08-15 21:05:06 +000075 GA->copyAttributesFrom(I);
76 VMap[I] = GA;
77 }
Anton Korobeynikov76547342007-07-10 19:07:35 +000078
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000079 // Now that all of the things that global variable initializer can refer to
80 // have been created, loop through and copy the global variable referrers
81 // over... We also set the attributes on the global now.
82 //
Chris Lattner21d1dde2005-05-09 01:04:34 +000083 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
84 I != E; ++I) {
Devang Patelb8f11de2010-06-23 23:55:51 +000085 GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000086 if (I->hasInitializer())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000087 GV->setInitializer(MapValue(I->getInitializer(), VMap));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000088 }
89
90 // Similarly, copy over function bodies now...
91 //
92 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Devang Patelb8f11de2010-06-23 23:55:51 +000093 Function *F = cast<Function>(VMap[I]);
Reid Spencer5301e7c2007-01-30 20:08:39 +000094 if (!I->isDeclaration()) {
Chris Lattner531f9e92005-03-15 04:54:21 +000095 Function::arg_iterator DestI = F->arg_begin();
Chris Lattner21d1dde2005-05-09 01:04:34 +000096 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
97 ++J) {
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000098 DestI->setName(J->getName());
Devang Patelb8f11de2010-06-23 23:55:51 +000099 VMap[J] = DestI++;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000100 }
101
Chris Lattnerd84dbb32009-08-27 04:02:30 +0000102 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
Dan Gohmanca26f792010-08-26 15:41:53 +0000103 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000104 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000105 }
106
Anton Korobeynikov76547342007-07-10 19:07:35 +0000107 // And aliases
108 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
109 I != E; ++I) {
Devang Patelb8f11de2010-06-23 23:55:51 +0000110 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
Rafael Espindola64c1e182014-06-03 02:41:57 +0000111 if (const Constant *C = I->getAliasee())
Rafael Espindola6b238632014-05-16 19:35:39 +0000112 GA->setAliasee(cast<GlobalObject>(MapValue(C, VMap)));
Anton Korobeynikov76547342007-07-10 19:07:35 +0000113 }
Devang Patele3fbbd12010-06-22 18:52:38 +0000114
115 // And named metadata....
116 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
117 E = M->named_metadata_end(); I != E; ++I) {
118 const NamedMDNode &NMD = *I;
Dan Gohman2637cc12010-07-21 23:38:33 +0000119 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
Devang Patele3fbbd12010-06-22 18:52:38 +0000120 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000121 NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
Devang Patele3fbbd12010-06-22 18:52:38 +0000122 }
Devang Patele43c6482010-06-22 22:50:42 +0000123
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000124 return New;
125}
Tom Stellard0a4e9a32014-10-01 17:14:57 +0000126
127extern "C" {
128
129LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
130 return wrap(CloneModule(unwrap(M)));
131}
132
133}