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...
|
2003-04-22 18:02:26 +00:00
|
|
|
Module *New = new Module(M->getModuleIdentifier());
|
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();
|
|
|
|
I != E; ++I)
|
2003-04-16 20:28:45 +00:00
|
|
|
ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
|
|
|
|
GlobalValue::ExternalLinkage, 0,
|
|
|
|
I->getName(), New);
|
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 =
|
2005-05-09 01:04:34 +00:00
|
|
|
new Function(cast<FunctionType>(I->getType()->getElementType()),
|
|
|
|
GlobalValue::ExternalLinkage, I->getName(), New);
|
|
|
|
NF->setCallingConv(I->getCallingConv());
|
2007-11-27 13:23:08 +00:00
|
|
|
NF->setParamAttrs(I->getParamAttrs());
|
2007-12-10 03:18:06 +00:00
|
|
|
if (I->hasCollector())
|
|
|
|
NF->setCollector(I->getCollector());
|
2005-05-09 01:04:34 +00:00
|
|
|
ValueMap[I]= NF;
|
|
|
|
}
|
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(),
|
|
|
|
ValueMap)));
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ReturnInst*> Returns; // Ignore returns cloned...
|
|
|
|
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())
|
|
|
|
GA->setAliasee(cast<Constant>(MapValue(C, ValueMap)));
|
|
|
|
}
|
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
return New;
|
|
|
|
}
|