blob: 0285f8c8d107b052adc5e7daad7a01f15642bc30 [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"
Reid Spencer78d033e2007-01-06 07:24:44 +000018#include "llvm/TypeSymbolTable.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000019#include "llvm/Constant.h"
Anton Korobeynikov344ef192007-11-09 12:27:04 +000020#include "llvm/Transforms/Utils/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 Lattner782e6012006-05-17 18:05:35 +000029 // Create the value map that maps things from the old module over to the new
30 // module.
Chris Lattner5e665f52007-02-03 00:08:31 +000031 DenseMap<const Value*, Value*> ValueMap;
Chris Lattner782e6012006-05-17 18:05:35 +000032 return CloneModule(M, ValueMap);
33}
34
Chris Lattner5e665f52007-02-03 00:08:31 +000035Module *llvm::CloneModule(const Module *M,
36 DenseMap<const Value*, Value*> &ValueMap) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000037 // First off, we need to create the new module...
Owen Anderson8b477ed2009-07-01 16:58:40 +000038 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
Reid Spencer26f23852007-01-26 08:11:39 +000039 New->setDataLayout(M->getDataLayout());
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000040 New->setTargetTriple(M->getTargetTriple());
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000041 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000042
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000043 // Copy all of the type symbol table entries over.
Reid Spencer78d033e2007-01-06 07:24:44 +000044 const TypeSymbolTable &TST = M->getTypeSymbolTable();
45 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
46 TI != TE; ++TI)
47 New->addTypeName(TI->first, TI->second);
Chris Lattnerc4e8c9f2006-01-18 21:32:45 +000048
49 // Copy all of the dependent libraries over.
50 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
51 New->addLibrary(*I);
Chris Lattner892310e2003-04-24 17:15:33 +000052
Chris Lattner51cbcbf2002-11-20 20:47:41 +000053 // Loop over all of the global variables, making corresponding globals in the
54 // new module. Here we add them to the ValueMap and to the new Module. We
55 // don't worry about attributes or initializers, they will come later.
56 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000057 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000058 I != E; ++I) {
Owen Andersone9b11b42009-07-08 19:03:57 +000059 GlobalVariable *GV = new GlobalVariable(*New,
Owen Anderson3d29df32009-07-08 01:26:06 +000060 I->getType()->getElementType(),
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000061 false,
62 GlobalValue::ExternalLinkage, 0,
Owen Andersone9b11b42009-07-08 19:03:57 +000063 I->getName());
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000064 GV->setAlignment(I->getAlignment());
65 ValueMap[I] = GV;
66 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000067
68 // Loop over the functions in the module, making external functions as before
Chris Lattnerc154cef2005-05-09 01:04:34 +000069 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen00b168892005-07-27 06:12:32 +000070 Function *NF =
Gabor Greif051a9502008-04-06 20:25:17 +000071 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
72 GlobalValue::ExternalLinkage, I->getName(), New);
Duncan Sands28c3cff2008-05-26 19:58:59 +000073 NF->copyAttributesFrom(I);
Nick Lewyckya6bf66d2008-10-09 06:27:14 +000074 ValueMap[I] = NF;
Chris Lattnerc154cef2005-05-09 01:04:34 +000075 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000076
Anton Korobeynikova2895112007-07-10 19:07:35 +000077 // Loop over the aliases in the module
78 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
79 I != E; ++I)
80 ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
81 I->getName(), NULL, New);
82
Chris Lattner51cbcbf2002-11-20 20:47:41 +000083 // Now that all of the things that global variable initializer can refer to
84 // have been created, loop through and copy the global variable referrers
85 // over... We also set the attributes on the global now.
86 //
Chris Lattnerc154cef2005-05-09 01:04:34 +000087 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
88 I != E; ++I) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000089 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
90 if (I->hasInitializer())
91 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
Owen Anderson0a205a42009-07-05 22:41:43 +000092 ValueMap,
Owen Andersone922c022009-07-22 00:24:57 +000093 M->getContext())));
Chris Lattner4ad02e72003-04-16 20:28:45 +000094 GV->setLinkage(I->getLinkage());
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +000095 GV->setThreadLocal(I->isThreadLocal());
96 GV->setConstant(I->isConstant());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000097 }
98
99 // Similarly, copy over function bodies now...
100 //
101 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
102 Function *F = cast<Function>(ValueMap[I]);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000103 if (!I->isDeclaration()) {
Chris Lattnere4d5c442005-03-15 04:54:21 +0000104 Function::arg_iterator DestI = F->arg_begin();
Chris Lattnerc154cef2005-05-09 01:04:34 +0000105 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
106 ++J) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000107 DestI->setName(J->getName());
108 ValueMap[J] = DestI++;
109 }
110
Chris Lattnerec1bea02009-08-27 04:02:30 +0000111 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000112 CloneFunctionInto(F, I, ValueMap, Returns);
113 }
114
Chris Lattner4ad02e72003-04-16 20:28:45 +0000115 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000116 }
117
Anton Korobeynikova2895112007-07-10 19:07:35 +0000118 // And aliases
119 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
120 I != E; ++I) {
121 GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
122 GA->setLinkage(I->getLinkage());
123 if (const Constant* C = I->getAliasee())
Owen Andersone922c022009-07-22 00:24:57 +0000124 GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, M->getContext())));
Anton Korobeynikova2895112007-07-10 19:07:35 +0000125 }
126
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000127 return New;
128}