Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/CloneModule.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the CloneModule interface which makes a copy of an
10
// entire module.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/IR/Constant.h"
15
#include "llvm/IR/DerivedTypes.h"
16
#include "llvm/IR/Module.h"
17
#include "llvm/Transforms/Utils/Cloning.h"
18
#include "llvm/Transforms/Utils/ValueMapper.h"
19
using namespace llvm;
20
21
326
static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
22
326
  const Comdat *SC = Src->getComdat();
23
326
  if (!SC)
24
299
    return;
25
27
  Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
26
27
  DC->setSelectionKind(SC->getSelectionKind());
27
27
  Dst->setComdat(DC);
28
27
}
29
30
/// This is not as easy as it might seem because we have to worry about making
31
/// copies of global variables and functions, and making their (initializers and
32
/// references, respectively) refer to the right globals.
33
///
34
69
std::unique_ptr<Module> llvm::CloneModule(const Module &M) {
35
69
  // Create the value map that maps things from the old module over to the new
36
69
  // module.
37
69
  ValueToValueMapTy VMap;
38
69
  return CloneModule(M, VMap);
39
69
}
40
41
std::unique_ptr<Module> llvm::CloneModule(const Module &M,
42
153
                                          ValueToValueMapTy &VMap) {
43
207
  return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
44
153
}
45
46
std::unique_ptr<Module> llvm::CloneModule(
47
    const Module &M, ValueToValueMapTy &VMap,
48
265
    function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
49
265
  // First off, we need to create the new module.
50
265
  std::unique_ptr<Module> New =
51
265
      llvm::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
52
265
  New->setSourceFileName(M.getSourceFileName());
53
265
  New->setDataLayout(M.getDataLayout());
54
265
  New->setTargetTriple(M.getTargetTriple());
55
265
  New->setModuleInlineAsm(M.getModuleInlineAsm());
56
265
57
265
  // Loop over all of the global variables, making corresponding globals in the
58
265
  // new module.  Here we add them to the VMap and to the new Module.  We
59
265
  // don't worry about attributes or initializers, they will come later.
60
265
  //
61
265
  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
62
397
       I != E; 
++I132
) {
63
132
    GlobalVariable *GV = new GlobalVariable(*New,
64
132
                                            I->getValueType(),
65
132
                                            I->isConstant(), I->getLinkage(),
66
132
                                            (Constant*) nullptr, I->getName(),
67
132
                                            (GlobalVariable*) nullptr,
68
132
                                            I->getThreadLocalMode(),
69
132
                                            I->getType()->getAddressSpace());
70
132
    GV->copyAttributesFrom(&*I);
71
132
    VMap[&*I] = GV;
72
132
  }
73
265
74
265
  // Loop over the functions in the module, making external functions as before
75
551
  for (const Function &I : M) {
76
551
    Function *NF =
77
551
        Function::Create(cast<FunctionType>(I.getValueType()), I.getLinkage(),
78
551
                         I.getAddressSpace(), I.getName(), New.get());
79
551
    NF->copyAttributesFrom(&I);
80
551
    VMap[&I] = NF;
81
551
  }
82
265
83
265
  // Loop over the aliases in the module
84
265
  for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
85
318
       I != E; 
++I53
) {
86
53
    if (!ShouldCloneDefinition(&*I)) {
87
26
      // An alias cannot act as an external reference, so we need to create
88
26
      // either a function or a global variable depending on the value type.
89
26
      // FIXME: Once pointee types are gone we can probably pick one or the
90
26
      // other.
91
26
      GlobalValue *GV;
92
26
      if (I->getValueType()->isFunctionTy())
93
18
        GV = Function::Create(cast<FunctionType>(I->getValueType()),
94
18
                              GlobalValue::ExternalLinkage,
95
18
                              I->getAddressSpace(), I->getName(), New.get());
96
8
      else
97
8
        GV = new GlobalVariable(
98
8
            *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
99
8
            nullptr, I->getName(), nullptr,
100
8
            I->getThreadLocalMode(), I->getType()->getAddressSpace());
101
26
      VMap[&*I] = GV;
102
26
      // We do not copy attributes (mainly because copying between different
103
26
      // kinds of globals is forbidden), but this is generally not required for
104
26
      // correctness.
105
26
      continue;
106
26
    }
107
27
    auto *GA = GlobalAlias::create(I->getValueType(),
108
27
                                   I->getType()->getPointerAddressSpace(),
109
27
                                   I->getLinkage(), I->getName(), New.get());
110
27
    GA->copyAttributesFrom(&*I);
111
27
    VMap[&*I] = GA;
112
27
  }
113
265
114
265
  // Now that all of the things that global variable initializer can refer to
115
265
  // have been created, loop through and copy the global variable referrers
116
265
  // over...  We also set the attributes on the global now.
117
265
  //
118
265
  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
119
397
       I != E; 
++I132
) {
120
132
    if (I->isDeclaration())
121
20
      continue;
122
112
123
112
    GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
124
112
    if (!ShouldCloneDefinition(&*I)) {
125
37
      // Skip after setting the correct linkage for an external reference.
126
37
      GV->setLinkage(GlobalValue::ExternalLinkage);
127
37
      continue;
128
37
    }
129
75
    if (I->hasInitializer())
130
75
      GV->setInitializer(MapValue(I->getInitializer(), VMap));
131
75
132
75
    SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
133
75
    I->getAllMetadata(MDs);
134
75
    for (auto MD : MDs)
135
54
      GV->addMetadata(MD.first,
136
54
                      *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
137
75
138
75
    copyComdat(GV, &*I);
139
75
  }
140
265
141
265
  // Similarly, copy over function bodies now...
142
265
  //
143
551
  for (const Function &I : M) {
144
551
    if (I.isDeclaration())
145
162
      continue;
146
389
147
389
    Function *F = cast<Function>(VMap[&I]);
148
389
    if (!ShouldCloneDefinition(&I)) {
149
138
      // Skip after setting the correct linkage for an external reference.
150
138
      F->setLinkage(GlobalValue::ExternalLinkage);
151
138
      // Personality function is not valid on a declaration.
152
138
      F->setPersonalityFn(nullptr);
153
138
      continue;
154
138
    }
155
251
156
251
    Function::arg_iterator DestI = F->arg_begin();
157
388
    for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
158
251
         
++J137
) {
159
137
      DestI->setName(J->getName());
160
137
      VMap[&*J] = &*DestI++;
161
137
    }
162
251
163
251
    SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
164
251
    CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns);
165
251
166
251
    if (I.hasPersonalityFn())
167
8
      F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
168
251
169
251
    copyComdat(F, &I);
170
251
  }
171
265
172
265
  // And aliases
173
265
  for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
174
318
       I != E; 
++I53
) {
175
53
    // We already dealt with undefined aliases above.
176
53
    if (!ShouldCloneDefinition(&*I))
177
26
      continue;
178
27
    GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
179
27
    if (const Constant *C = I->getAliasee())
180
27
      GA->setAliasee(MapValue(C, VMap));
181
27
  }
182
265
183
265
  // And named metadata....
184
265
  for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
185
265
                                             E = M.named_metadata_end();
186
463
       I != E; 
++I198
) {
187
198
    const NamedMDNode &NMD = *I;
188
198
    NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
189
560
    for (unsigned i = 0, e = NMD.getNumOperands(); i != e; 
++i362
)
190
362
      NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
191
198
  }
192
265
193
265
  return New;
194
265
}
195
196
extern "C" {
197
198
0
LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
199
0
  return wrap(CloneModule(*unwrap(M)).release());
200
0
}
201
202
}