2002-11-21 04:47:41 +08:00
|
|
|
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-21 04:47:41 +08: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 15:24:44 +08:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2002-11-21 04:47:41 +08:00
|
|
|
#include "llvm/Constant.h"
|
2007-11-09 20:27:04 +08:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-11-21 04:47:41 +08: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-11 01:57:28 +08:00
|
|
|
/// variables and functions, and making their (initializers and references,
|
2002-11-21 04:47:41 +08:00
|
|
|
/// respectively) refer to the right globals.
|
|
|
|
///
|
2004-01-09 14:12:26 +08:00
|
|
|
Module *llvm::CloneModule(const Module *M) {
|
2006-05-18 02:05:35 +08:00
|
|
|
// Create the value map that maps things from the old module over to the new
|
|
|
|
// module.
|
2007-02-03 08:08:31 +08:00
|
|
|
DenseMap<const Value*, Value*> ValueMap;
|
2006-05-18 02:05:35 +08:00
|
|
|
return CloneModule(M, ValueMap);
|
|
|
|
}
|
|
|
|
|
2007-02-03 08:08:31 +08:00
|
|
|
Module *llvm::CloneModule(const Module *M,
|
|
|
|
DenseMap<const Value*, Value*> &ValueMap) {
|
2002-11-21 04:47:41 +08:00
|
|
|
// First off, we need to create the new module...
|
2009-07-02 00:58:40 +08:00
|
|
|
Module *New = new Module(M->getModuleIdentifier(), M->getContext());
|
2007-01-26 16:11:39 +08:00
|
|
|
New->setDataLayout(M->getDataLayout());
|
2006-01-19 05:32:45 +08:00
|
|
|
New->setTargetTriple(M->getTargetTriple());
|
2006-01-24 12:16:34 +08:00
|
|
|
New->setModuleInlineAsm(M->getModuleInlineAsm());
|
2002-11-21 04:47:41 +08:00
|
|
|
|
2006-01-19 05:32:45 +08:00
|
|
|
// Copy all of the type symbol table entries over.
|
2007-01-06 15:24:44 +08: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-19 05:32:45 +08: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-25 01:15:33 +08:00
|
|
|
|
2002-11-21 04:47:41 +08: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 09:04:34 +08:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
2008-10-09 14:27:14 +08:00
|
|
|
I != E; ++I) {
|
2009-07-09 03:03:57 +08:00
|
|
|
GlobalVariable *GV = new GlobalVariable(*New,
|
2009-07-08 09:26:06 +08:00
|
|
|
I->getType()->getElementType(),
|
2008-10-09 14:27:14 +08:00
|
|
|
false,
|
|
|
|
GlobalValue::ExternalLinkage, 0,
|
2009-07-09 03:03:57 +08:00
|
|
|
I->getName());
|
2008-10-09 14:27:14 +08:00
|
|
|
GV->setAlignment(I->getAlignment());
|
|
|
|
ValueMap[I] = GV;
|
|
|
|
}
|
2002-11-21 04:47:41 +08:00
|
|
|
|
|
|
|
// Loop over the functions in the module, making external functions as before
|
2005-05-09 09:04:34 +08:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2005-07-27 14:12:32 +08:00
|
|
|
Function *NF =
|
2008-04-07 04:25:17 +08:00
|
|
|
Function::Create(cast<FunctionType>(I->getType()->getElementType()),
|
|
|
|
GlobalValue::ExternalLinkage, I->getName(), New);
|
2008-05-27 03:58:59 +08:00
|
|
|
NF->copyAttributesFrom(I);
|
2008-10-09 14:27:14 +08:00
|
|
|
ValueMap[I] = NF;
|
2005-05-09 09:04:34 +08:00
|
|
|
}
|
2002-11-21 04:47:41 +08:00
|
|
|
|
2007-07-11 03:07:35 +08: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-21 04:47:41 +08: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 09:04:34 +08:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I) {
|
2002-11-21 04:47:41 +08:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
|
|
|
|
if (I->hasInitializer())
|
|
|
|
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
|
2009-10-25 07:37:16 +08:00
|
|
|
ValueMap)));
|
2003-04-17 04:28:45 +08:00
|
|
|
GV->setLinkage(I->getLinkage());
|
2007-04-13 02:32:50 +08:00
|
|
|
GV->setThreadLocal(I->isThreadLocal());
|
|
|
|
GV->setConstant(I->isConstant());
|
2002-11-21 04:47:41 +08: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-31 04:08:39 +08:00
|
|
|
if (!I->isDeclaration()) {
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator DestI = F->arg_begin();
|
2005-05-09 09:04:34 +08:00
|
|
|
for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
|
|
|
|
++J) {
|
2002-11-21 04:47:41 +08:00
|
|
|
DestI->setName(J->getName());
|
|
|
|
ValueMap[J] = DestI++;
|
|
|
|
}
|
|
|
|
|
2009-08-27 12:02:30 +08:00
|
|
|
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
|
2002-11-21 04:47:41 +08:00
|
|
|
CloneFunctionInto(F, I, ValueMap, Returns);
|
|
|
|
}
|
|
|
|
|
2003-04-17 04:28:45 +08:00
|
|
|
F->setLinkage(I->getLinkage());
|
2002-11-21 04:47:41 +08:00
|
|
|
}
|
|
|
|
|
2007-07-11 03:07:35 +08: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-10-25 07:37:16 +08:00
|
|
|
GA->setAliasee(cast<Constant>(MapValue(C, ValueMap)));
|
2007-07-11 03:07:35 +08:00
|
|
|
}
|
|
|
|
|
2002-11-21 04:47:41 +08:00
|
|
|
return New;
|
|
|
|
}
|