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"
|
2007-11-09 12:27:04 +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.
|
2007-02-03 00:08:31 +00:00
|
|
|
DenseMap<const Value*, Value*> ValueMap;
|
2006-05-17 18:05:35 +00:00
|
|
|
return CloneModule(M, ValueMap);
|
|
|
|
}
|
|
|
|
|
2007-02-03 00:08:31 +00:00
|
|
|
Module *llvm::CloneModule(const Module *M,
|
|
|
|
DenseMap<const Value*, Value*> &ValueMap) {
|
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
|
|
|
|
// new module. Here we add them to the ValueMap and to the new Module. We
|
|
|
|
// 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());
|
|
|
|
ValueMap[I] = GV;
|
|
|
|
}
|
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);
|
2008-10-09 06:27:14 +00:00
|
|
|
ValueMap[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)
|
|
|
|
ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
|
|
|
|
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) {
|
2002-11-20 20:47:41 +00:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
|
|
|
|
if (I->hasInitializer())
|
|
|
|
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
|
2009-07-05 22:41:43 +00:00
|
|
|
ValueMap,
|
2009-07-22 00:24:57 +00:00
|
|
|
M->getContext())));
|
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) {
|
|
|
|
Function *F = cast<Function>(ValueMap[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());
|
|
|
|
ValueMap[J] = DestI++;
|
|
|
|
}
|
|
|
|
|
2009-08-27 04:02:30 +00:00
|
|
|
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
|
2002-11-20 20:47:41 +00:00
|
|
|
CloneFunctionInto(F, I, ValueMap, Returns);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
|
|
|
|
GA->setLinkage(I->getLinkage());
|
|
|
|
if (const Constant* C = I->getAliasee())
|
2009-07-22 00:24:57 +00:00
|
|
|
GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, M->getContext())));
|
2007-07-10 19:07:35 +00:00
|
|
|
}
|
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
return New;
|
|
|
|
}
|