2002-11-20 20:47:41 +00:00
|
|
|
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 20:47:41 +00:00
|
|
|
//
|
|
|
|
// This file implements the CloneModule interface which makes a copy of an
|
|
|
|
// entire module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-01-06 07:24:44 +00:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2002-11-20 20:47:41 +00:00
|
|
|
#include "llvm/Constant.h"
|
2010-08-24 18:50:07 +00:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
/// CloneModule - Return an exact copy of the specified module. This is not as
|
|
|
|
/// easy as it might seem because we have to worry about making copies of global
|
2003-10-10 17:57:28 +00:00
|
|
|
/// variables and functions, and making their (initializers and references,
|
2002-11-20 20:47:41 +00:00
|
|
|
/// respectively) refer to the right globals.
|
|
|
|
///
|
2004-01-09 06:12:26 +00:00
|
|
|
Module *llvm::CloneModule(const Module *M) {
|
2006-05-17 18:05:35 +00:00
|
|
|
// Create the value map that maps things from the old module over to the new
|
|
|
|
// module.
|
2010-06-24 00:00:42 +00:00
|
|
|
ValueToValueMapTy VMap;
|
2010-06-23 23:55:51 +00:00
|
|
|
return CloneModule(M, VMap);
|
2006-05-17 18:05:35 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 00:08:31 +00:00
|
|
|
Module *llvm::CloneModule(const Module *M,
|
2010-06-24 00:00:42 +00:00
|
|
|
ValueToValueMapTy &VMap) {
|
2002-11-20 20:47:41 +00:00
|
|
|
// First off, we need to create the new module...
|
2009-07-01 16:58:40 +00:00
|
|
|
Module *New = new Module(M->getModuleIdentifier(), M->getContext());
|
2007-01-26 08:11:39 +00:00
|
|
|
New->setDataLayout(M->getDataLayout());
|
2006-01-18 21:32:45 +00:00
|
|
|
New->setTargetTriple(M->getTargetTriple());
|
2006-01-24 04:16:34 +00:00
|
|
|
New->setModuleInlineAsm(M->getModuleInlineAsm());
|
2002-11-20 20:47:41 +00:00
|
|
|
|
2006-01-18 21:32:45 +00:00
|
|
|
// Copy all of the type symbol table entries over.
|
2007-01-06 07:24:44 +00:00
|
|
|
const TypeSymbolTable &TST = M->getTypeSymbolTable();
|
|
|
|
for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
|
|
|
|
TI != TE; ++TI)
|
|
|
|
New->addTypeName(TI->first, TI->second);
|
2006-01-18 21:32:45 +00:00
|
|
|
|
|
|
|
// Copy all of the dependent libraries over.
|
|
|
|
for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
|
|
|
|
New->addLibrary(*I);
|
2003-04-24 17:15:33 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
// Loop over all of the global variables, making corresponding globals in the
|
2010-06-23 23:55:51 +00:00
|
|
|
// new module. Here we add them to the VMap and to the new Module. We
|
2002-11-20 20:47:41 +00:00
|
|
|
// don't worry about attributes or initializers, they will come later.
|
|
|
|
//
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
2008-10-09 06:27:14 +00:00
|
|
|
I != E; ++I) {
|
2009-07-08 19:03:57 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(*New,
|
2009-07-08 01:26:06 +00:00
|
|
|
I->getType()->getElementType(),
|
2008-10-09 06:27:14 +00:00
|
|
|
false,
|
|
|
|
GlobalValue::ExternalLinkage, 0,
|
2009-07-08 19:03:57 +00:00
|
|
|
I->getName());
|
2008-10-09 06:27:14 +00:00
|
|
|
GV->setAlignment(I->getAlignment());
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[I] = GV;
|
2008-10-09 06:27:14 +00:00
|
|
|
}
|
2002-11-20 20:47:41 +00:00
|
|
|
|
|
|
|
// Loop over the functions in the module, making external functions as before
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2005-07-27 06:12:32 +00:00
|
|
|
Function *NF =
|
2008-04-06 20:25:17 +00:00
|
|
|
Function::Create(cast<FunctionType>(I->getType()->getElementType()),
|
|
|
|
GlobalValue::ExternalLinkage, I->getName(), New);
|
2008-05-26 19:58:59 +00:00
|
|
|
NF->copyAttributesFrom(I);
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[I] = NF;
|
2005-05-09 01:04:34 +00:00
|
|
|
}
|
2002-11-20 20:47:41 +00:00
|
|
|
|
2007-07-10 19:07:35 +00:00
|
|
|
// Loop over the aliases in the module
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I)
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
|
2007-07-10 19:07:35 +00:00
|
|
|
I->getName(), NULL, New);
|
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
// Now that all of the things that global variable initializer can refer to
|
|
|
|
// have been created, loop through and copy the global variable referrers
|
|
|
|
// over... We also set the attributes on the global now.
|
|
|
|
//
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
|
2002-11-20 20:47:41 +00:00
|
|
|
if (I->hasInitializer())
|
|
|
|
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
|
2010-08-26 15:41:53 +00:00
|
|
|
VMap,
|
|
|
|
true)));
|
2003-04-16 20:28:45 +00:00
|
|
|
GV->setLinkage(I->getLinkage());
|
2007-04-12 18:32:50 +00:00
|
|
|
GV->setThreadLocal(I->isThreadLocal());
|
|
|
|
GV->setConstant(I->isConstant());
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, copy over function bodies now...
|
|
|
|
//
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
Function *F = cast<Function>(VMap[I]);
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration()) {
|
2005-03-15 04:54:21 +00:00
|
|
|
Function::arg_iterator DestI = F->arg_begin();
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
|
|
|
|
++J) {
|
2002-11-20 20:47:41 +00:00
|
|
|
DestI->setName(J->getName());
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[J] = DestI++;
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 04:02:30 +00:00
|
|
|
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
|
2010-08-26 15:41:53 +00:00
|
|
|
CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-04-16 20:28:45 +00:00
|
|
|
F->setLinkage(I->getLinkage());
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 19:07:35 +00:00
|
|
|
// And aliases
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
|
2007-07-10 19:07:35 +00:00
|
|
|
GA->setLinkage(I->getLinkage());
|
|
|
|
if (const Constant* C = I->getAliasee())
|
2010-08-26 15:41:53 +00:00
|
|
|
GA->setAliasee(cast<Constant>(MapValue(C, VMap, true)));
|
2007-07-10 19:07:35 +00:00
|
|
|
}
|
2010-06-22 18:52:38 +00:00
|
|
|
|
|
|
|
// And named metadata....
|
|
|
|
for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
|
|
|
|
E = M->named_metadata_end(); I != E; ++I) {
|
|
|
|
const NamedMDNode &NMD = *I;
|
2010-07-21 23:38:33 +00:00
|
|
|
NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
|
2010-06-22 18:52:38 +00:00
|
|
|
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
|
2010-08-26 15:41:53 +00:00
|
|
|
NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap, true)));
|
2010-06-22 18:52:38 +00:00
|
|
|
}
|
2010-06-22 22:50:42 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
return New;
|
|
|
|
}
|