LLVM  3.7.0
CloneModule.cpp
Go to the documentation of this file.
1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the CloneModule interface which makes a copy of an
11 // entire module.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/IR/Constant.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Module.h"
20 #include "llvm-c/Core.h"
21 using namespace llvm;
22 
23 /// 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
25 /// variables and functions, and making their (initializers and references,
26 /// respectively) refer to the right globals.
27 ///
29  // Create the value map that maps things from the old module over to the new
30  // module.
31  ValueToValueMapTy VMap;
32  return CloneModule(M, VMap);
33 }
34 
36  // First off, we need to create the new module.
37  Module *New = new Module(M->getModuleIdentifier(), M->getContext());
38  New->setDataLayout(M->getDataLayout());
41 
42  // Loop over all of the global variables, making corresponding globals in the
43  // new module. Here we add them to the VMap and to the new Module. We
44  // don't worry about attributes or initializers, they will come later.
45  //
47  I != E; ++I) {
48  GlobalVariable *GV = new GlobalVariable(*New,
49  I->getType()->getElementType(),
50  I->isConstant(), I->getLinkage(),
51  (Constant*) nullptr, I->getName(),
52  (GlobalVariable*) nullptr,
53  I->getThreadLocalMode(),
54  I->getType()->getAddressSpace());
55  GV->copyAttributesFrom(I);
56  VMap[I] = GV;
57  }
58 
59  // Loop over the functions in the module, making external functions as before
60  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
61  Function *NF =
62  Function::Create(cast<FunctionType>(I->getType()->getElementType()),
63  I->getLinkage(), I->getName(), New);
64  NF->copyAttributesFrom(I);
65  VMap[I] = NF;
66  }
67 
68  // Loop over the aliases in the module
70  I != E; ++I) {
71  auto *PTy = cast<PointerType>(I->getType());
72  auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), New);
73  GA->copyAttributesFrom(I);
74  VMap[I] = GA;
75  }
76 
77  // Now that all of the things that global variable initializer can refer to
78  // have been created, loop through and copy the global variable referrers
79  // over... We also set the attributes on the global now.
80  //
82  I != E; ++I) {
83  GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
84  if (I->hasInitializer())
85  GV->setInitializer(MapValue(I->getInitializer(), VMap));
86  }
87 
88  // Similarly, copy over function bodies now...
89  //
90  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
91  Function *F = cast<Function>(VMap[I]);
92  if (!I->isDeclaration()) {
93  Function::arg_iterator DestI = F->arg_begin();
94  for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
95  ++J) {
96  DestI->setName(J->getName());
97  VMap[J] = DestI++;
98  }
99 
100  SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
101  CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
102 
103  }
104 
105  if (I->hasPersonalityFn())
106  F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap));
107  }
108 
109  // And aliases
110  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
111  I != E; ++I) {
112  GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
113  if (const Constant *C = I->getAliasee())
114  GA->setAliasee(MapValue(C, VMap));
115  }
116 
117  // And named metadata....
119  E = M->named_metadata_end(); I != E; ++I) {
120  const NamedMDNode &NMD = *I;
121  NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
122  for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
123  NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
124  }
125 
126  return New;
127 }
128 
129 extern "C" {
130 
132  return wrap(CloneModule(unwrap(M)));
133 }
134 
135 }
void setPersonalityFn(Constant *C)
Definition: Function.cpp:1001
StringRef getName() const
Definition: Metadata.cpp:986
virtual void copyAttributesFrom(const GlobalValue *Src)
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition: Globals.cpp:57
Module * CloneModule(const Module *M)
CloneModule - Return an exact copy of the specified module.
Definition: CloneModule.cpp:28
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void addOperand(MDNode *M)
Definition: Metadata.cpp:971
named_metadata_iterator named_metadata_end()
Definition: Module.h:614
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition: Module.cpp:262
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:366
F(f)
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:261
A tuple of MDNodes.
Definition: Metadata.h:1127
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:198
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values...
void setModuleInlineAsm(StringRef Asm)
Set the module-scope inline assembly blocks.
Definition: Module.h:298
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:242
global_iterator global_begin()
Definition: Module.h:552
LLVMTargetDataRef wrap(const DataLayout *P)
Definition: DataLayout.h:469
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
alias_iterator alias_end()
Definition: Module.h:593
static GlobalAlias * create(PointerType *Ty, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:243
This is an important base class in LLVM.
Definition: Constant.h:41
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
LLVMModuleRef LLVMCloneModule(LLVMModuleRef M)
Return an exact copy of the specified module.
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Definition: ValueMapper.cpp:28
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:965
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:416
arg_iterator arg_begin()
Definition: Function.h:472
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:269
global_iterator global_end()
Definition: Module.h:554
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
alias_iterator alias_begin()
Definition: Module.h:591
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
iterator end()
Definition: Module.h:571
#define I(x, y, z)
Definition: MD5.cpp:54
iterator begin()
Definition: Module.h:569
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:294
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Core.h:78
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:221
unsigned getNumOperands() const
Definition: Metadata.cpp:961
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:121
void setAliasee(Constant *Aliasee)
These methods retrive and set alias target.
Definition: Globals.cpp:281
named_metadata_iterator named_metadata_begin()
Definition: Module.h:609
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:265