LLVM: lib/Transforms/Utils/CloneModule.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

18using namespace llvm;

19

20namespace llvm {

22}

23

25 const Comdat *SC = Src->getComdat();

26 if (!SC)

27 return;

28 Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());

30 Dst->setComdat(DC);

31}

32

33

34

35

36

37

38

40

41

44}

45

49}

50

54

55 assert(M.isMaterialized() && "Module must be materialized before cloning!");

56

57

58 std::unique_ptr New =

59 std::make_unique(M.getModuleIdentifier(), M.getContext());

60 New->setSourceFileName(M.getSourceFileName());

61 New->setDataLayout(M.getDataLayout());

62 New->setTargetTriple(M.getTargetTriple());

63 New->setModuleInlineAsm(M.getModuleInlineAsm());

64 New->IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;

65

66

67

68

69

72 *New, I.getValueType(), I.isConstant(), I.getLinkage(),

74 I.getThreadLocalMode(), I.getType()->getAddressSpace());

76 VMap[&I] = NewGV;

77 }

78

79

82 Function::Create(cast(I.getValueType()), I.getLinkage(),

83 I.getAddressSpace(), I.getName(), New.get());

85 VMap[&I] = NF;

86 }

87

88

90 if (!ShouldCloneDefinition(&I)) {

91

92

93

94

96 if (I.getValueType()->isFunctionTy())

99 I.getName(), New.get());

100 else

103 I.getName(), nullptr, I.getThreadLocalMode(),

104 I.getType()->getAddressSpace());

105 VMap[&I] = GV;

106

107

108

109 continue;

110 }

112 I.getType()->getPointerAddressSpace(),

113 I.getLinkage(), I.getName(), New.get());

114 GA->copyAttributesFrom(&I);

115 VMap[&I] = GA;

116 }

117

119

120 auto *GI =

122 I.getLinkage(), I.getName(), nullptr, New.get());

123 GI->copyAttributesFrom(&I);

124 VMap[&I] = GI;

125 }

126

127

128

129

130

133

135 G.getAllMetadata(MDs);

136 for (auto MD : MDs)

138

139 if (G.isDeclaration())

140 continue;

141

142 if (!ShouldCloneDefinition(&G)) {

143

145 continue;

146 }

147 if (G.hasInitializer())

149

151 }

152

153

154

156 Function *F = cast(VMap[&I]);

157

158 if (I.isDeclaration()) {

159

160

162 I.getAllMetadata(MDs);

163 for (auto MD : MDs)

164 F->addMetadata(MD.first, *MapMetadata(MD.second, VMap));

165 continue;

166 }

167

168 if (!ShouldCloneDefinition(&I)) {

169

171

172 F->setPersonalityFn(nullptr);

173 continue;

174 }

175

177 for (const Argument &J : I.args()) {

178 DestI->setName(J.getName());

179 VMap[&J] = &*DestI++;

180 }

181

184 Returns);

185

186 if (I.hasPersonalityFn())

187 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));

188

190 }

191

192

194

195 if (!ShouldCloneDefinition(&I))

196 continue;

197 GlobalAlias *GA = cast(VMap[&I]);

198 if (const Constant *C = I.getAliasee())

200 }

201

203 GlobalIFunc *GI = cast(VMap[&I]);

206 }

207

208

209 for (const NamedMDNode &NMD : M.named_metadata()) {

210 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());

211 for (const MDNode *N : NMD.operands())

213 }

214

215 return New;

216}

217

218extern "C" {

219

222}

223

224}

static void copyComdat(GlobalObject *Dst, const GlobalObject *Src)

Module.h This file contains the declarations for the Module class.

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

This class represents an incoming formal argument to a Function.

void setSelectionKind(SelectionKind Val)

This is an important base class in LLVM.

static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)

void copyAttributesFrom(const Function *Src)

copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...

void setAliasee(Constant *Aliasee)

These methods retrieve and set alias target.

static GlobalAlias * create(Type *Ty, unsigned AddressSpace, 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...

static GlobalIFunc * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Resolver, Module *Parent)

If a parent module is specified, the ifunc is automatically inserted into the end of the specified mo...

void setResolver(Constant *Resolver)

These methods retrieve and set ifunc resolver function.

void addMetadata(unsigned KindID, MDNode &MD)

Add a metadata attachment.

void setLinkage(LinkageTypes LT)

@ ExternalLinkage

Externally visible function.

void setInitializer(Constant *InitVal)

setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...

void copyAttributesFrom(const GlobalVariable *Src)

copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...

A Module instance is used to store all the information related to an LLVM module.

void addOperand(MDNode *M)

Interface for looking up the initializer for a variable name, used by Init::resolveReferences.

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

void setName(const Twine &Name)

Change the name of the value.

An efficient, type-erasing, non-owning reference to a callable.

LLVMModuleRef LLVMCloneModule(LLVMModuleRef M)

Return an exact copy of the specified module.

struct LLVMOpaqueModule * LLVMModuleRef

The top-level container for all other LLVM Intermediate Representation (IR) objects.

@ C

The default llvm calling convention, compatible with C.

This is an optimization pass for GlobalISel generic memory operations.

Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)

Lookup or compute a mapping for a piece of metadata.

Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)

Look up or compute a value in the value map.

Attribute unwrap(LLVMAttributeRef Attr)

void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, 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.

LLVMAttributeRef wrap(Attribute Attr)

std::unique_ptr< Module > CloneModule(const Module &M)

Return an exact copy of the specified module.