LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 using namespace llvm;
21 
22 /// CloneModule - Return an exact copy of the specified module. This is not as
23 /// easy as it might seem because we have to worry about making copies of global
24 /// variables and functions, and making their (initializers and references,
25 /// respectively) refer to the right globals.
26 ///
28  // Create the value map that maps things from the old module over to the new
29  // module.
30  ValueToValueMapTy VMap;
31  return CloneModule(M, VMap);
32 }
33 
35  // First off, we need to create the new module.
36  Module *New = new Module(M->getModuleIdentifier(), M->getContext());
37  New->setDataLayout(M->getDataLayout());
40 
41  // Loop over all of the global variables, making corresponding globals in the
42  // new module. Here we add them to the VMap and to the new Module. We
43  // don't worry about attributes or initializers, they will come later.
44  //
46  I != E; ++I) {
47  GlobalVariable *GV = new GlobalVariable(*New,
48  I->getType()->getElementType(),
49  I->isConstant(), I->getLinkage(),
50  (Constant*) 0, I->getName(),
51  (GlobalVariable*) 0,
52  I->getThreadLocalMode(),
53  I->getType()->getAddressSpace());
54  GV->copyAttributesFrom(I);
55  VMap[I] = GV;
56  }
57 
58  // Loop over the functions in the module, making external functions as before
59  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
60  Function *NF =
61  Function::Create(cast<FunctionType>(I->getType()->getElementType()),
62  I->getLinkage(), I->getName(), New);
63  NF->copyAttributesFrom(I);
64  VMap[I] = NF;
65  }
66 
67  // Loop over the aliases in the module
69  I != E; ++I) {
70  GlobalAlias *GA = new GlobalAlias(I->getType(), I->getLinkage(),
71  I->getName(), NULL, New);
72  GA->copyAttributesFrom(I);
73  VMap[I] = GA;
74  }
75 
76  // Now that all of the things that global variable initializer can refer to
77  // have been created, loop through and copy the global variable referrers
78  // over... We also set the attributes on the global now.
79  //
81  I != E; ++I) {
82  GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
83  if (I->hasInitializer())
84  GV->setInitializer(MapValue(I->getInitializer(), VMap));
85  }
86 
87  // Similarly, copy over function bodies now...
88  //
89  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
90  Function *F = cast<Function>(VMap[I]);
91  if (!I->isDeclaration()) {
92  Function::arg_iterator DestI = F->arg_begin();
93  for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
94  ++J) {
95  DestI->setName(J->getName());
96  VMap[J] = DestI++;
97  }
98 
99  SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
100  CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
101  }
102  }
103 
104  // And aliases
105  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
106  I != E; ++I) {
107  GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
108  if (const Constant *C = I->getAliasee())
109  GA->setAliasee(MapValue(C, VMap));
110  }
111 
112  // And named metadata....
114  E = M->named_metadata_end(); I != E; ++I) {
115  const NamedMDNode &NMD = *I;
116  NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
117  for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
118  NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
119  }
120 
121  return New;
122 }
StringRef getName() const
getName - Return a constant reference to this named metadata's name.
Definition: Metadata.cpp:569
void setDataLayout(StringRef DL)
Set the data layout.
Definition: Module.h:263
virtual void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:51
Module * CloneModule(const Module *M)
Definition: CloneModule.cpp:27
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
void addOperand(MDNode *M)
addOperand - Add metadata operand.
Definition: Metadata.cpp:551
named_metadata_iterator named_metadata_end()
Definition: Module.h:559
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Definition: Module.cpp:295
F(f)
const std::string & getTargetTriple() const
Definition: Module.h:237
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=0, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
Definition: ValueMapper.cpp:27
void setInitializer(Constant *InitVal)
Definition: Globals.cpp:166
void setModuleInlineAsm(StringRef Asm)
Set the module-scope inline assembly blocks.
Definition: Module.h:269
void copyAttributesFrom(const GlobalValue *Src)
Definition: Function.cpp:347
const std::string & getModuleIdentifier() const
Definition: Module.h:228
global_iterator global_begin()
Definition: Module.h:521
alias_iterator alias_end()
Definition: Module.h:544
LLVM Constant Representation.
Definition: Constant.h:41
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Definition: Metadata.cpp:545
arg_iterator arg_begin()
Definition: Function.h:410
const std::string & getModuleInlineAsm() const
Definition: Module.h:253
global_iterator global_end()
Definition: Module.h:523
See the file comment.
Definition: ValueMap.h:75
const std::string & getDataLayout() const
Definition: Module.h:233
void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:183
alias_iterator alias_begin()
Definition: Module.h:542
void setAliasee(Constant *GV)
set/getAliasee - These methods retrive and set alias target.
Definition: Globals.cpp:225
iterator end()
Definition: Module.h:533
#define I(x, y, z)
Definition: MD5.cpp:54
iterator begin()
Definition: Module.h:531
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:266
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
Definition: Metadata.cpp:540
named_metadata_iterator named_metadata_begin()
Definition: Module.h:554
LLVMContext & getContext() const
Definition: Module.h:249
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
Definition: Function.h:128