Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/ValueMapper.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
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 defines the MapValue function, which is shared by various parts of
10
// the lib/Transforms/Utils library.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Utils/ValueMapper.h"
15
#include "llvm/ADT/ArrayRef.h"
16
#include "llvm/ADT/DenseMap.h"
17
#include "llvm/ADT/DenseSet.h"
18
#include "llvm/ADT/None.h"
19
#include "llvm/ADT/Optional.h"
20
#include "llvm/ADT/STLExtras.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/IR/Argument.h"
23
#include "llvm/IR/BasicBlock.h"
24
#include "llvm/IR/CallSite.h"
25
#include "llvm/IR/Constant.h"
26
#include "llvm/IR/Constants.h"
27
#include "llvm/IR/DebugInfoMetadata.h"
28
#include "llvm/IR/DerivedTypes.h"
29
#include "llvm/IR/Function.h"
30
#include "llvm/IR/GlobalAlias.h"
31
#include "llvm/IR/GlobalObject.h"
32
#include "llvm/IR/GlobalVariable.h"
33
#include "llvm/IR/InlineAsm.h"
34
#include "llvm/IR/Instruction.h"
35
#include "llvm/IR/Instructions.h"
36
#include "llvm/IR/Metadata.h"
37
#include "llvm/IR/Operator.h"
38
#include "llvm/IR/Type.h"
39
#include "llvm/IR/Value.h"
40
#include "llvm/Support/Casting.h"
41
#include <cassert>
42
#include <limits>
43
#include <memory>
44
#include <utility>
45
46
using namespace llvm;
47
48
// Out of line method to get vtable etc for class.
49
0
void ValueMapTypeRemapper::anchor() {}
50
0
void ValueMaterializer::anchor() {}
51
52
namespace {
53
54
/// A basic block used in a BlockAddress whose function body is not yet
55
/// materialized.
56
struct DelayedBasicBlock {
57
  BasicBlock *OldBB;
58
  std::unique_ptr<BasicBlock> TempBB;
59
60
  DelayedBasicBlock(const BlockAddress &Old)
61
      : OldBB(Old.getBasicBlock()),
62
2
        TempBB(BasicBlock::Create(Old.getContext())) {}
63
};
64
65
struct WorklistEntry {
66
  enum EntryKind {
67
    MapGlobalInit,
68
    MapAppendingVar,
69
    MapGlobalAliasee,
70
    RemapFunction
71
  };
72
  struct GVInitTy {
73
    GlobalVariable *GV;
74
    Constant *Init;
75
  };
76
  struct AppendingGVTy {
77
    GlobalVariable *GV;
78
    Constant *InitPrefix;
79
  };
80
  struct GlobalAliaseeTy {
81
    GlobalAlias *GA;
82
    Constant *Aliasee;
83
  };
84
85
  unsigned Kind : 2;
86
  unsigned MCID : 29;
87
  unsigned AppendingGVIsOldCtorDtor : 1;
88
  unsigned AppendingGVNumNewMembers;
89
  union {
90
    GVInitTy GVInit;
91
    AppendingGVTy AppendingGV;
92
    GlobalAliaseeTy GlobalAliasee;
93
    Function *RemapF;
94
  } Data;
95
};
96
97
struct MappingContext {
98
  ValueToValueMapTy *VM;
99
  ValueMaterializer *Materializer = nullptr;
100
101
  /// Construct a MappingContext with a value map and materializer.
102
  explicit MappingContext(ValueToValueMapTy &VM,
103
                          ValueMaterializer *Materializer = nullptr)
104
6.79M
      : VM(&VM), Materializer(Materializer) {}
105
};
106
107
class Mapper {
108
  friend class MDNodeMapper;
109
110
#ifndef NDEBUG
111
  DenseSet<GlobalValue *> AlreadyScheduled;
112
#endif
113
114
  RemapFlags Flags;
115
  ValueMapTypeRemapper *TypeMapper;
116
  unsigned CurrentMCID = 0;
117
  SmallVector<MappingContext, 2> MCs;
118
  SmallVector<WorklistEntry, 4> Worklist;
119
  SmallVector<DelayedBasicBlock, 1> DelayedBBs;
120
  SmallVector<Constant *, 16> AppendingInits;
121
122
public:
123
  Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
124
         ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
125
      : Flags(Flags), TypeMapper(TypeMapper),
126
6.79M
        MCs(1, MappingContext(VM, Materializer)) {}
127
128
  /// ValueMapper should explicitly call \a flush() before destruction.
129
6.79M
  ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
130
131
0
  bool hasWorkToDo() const { return !Worklist.empty(); }
132
133
  unsigned
134
  registerAlternateMappingContext(ValueToValueMapTy &VM,
135
1.07k
                                  ValueMaterializer *Materializer = nullptr) {
136
1.07k
    MCs.push_back(MappingContext(VM, Materializer));
137
1.07k
    return MCs.size() - 1;
138
1.07k
  }
139
140
  void addFlags(RemapFlags Flags);
141
142
  void remapGlobalObjectMetadata(GlobalObject &GO);
143
144
  Value *mapValue(const Value *V);
145
  void remapInstruction(Instruction *I);
146
  void remapFunction(Function &F);
147
148
518
  Constant *mapConstant(const Constant *C) {
149
518
    return cast_or_null<Constant>(mapValue(C));
150
518
  }
151
152
  /// Map metadata.
153
  ///
154
  /// Find the mapping for MD.  Guarantees that the return will be resolved
155
  /// (not an MDNode, or MDNode::isResolved() returns true).
156
  Metadata *mapMetadata(const Metadata *MD);
157
158
  void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
159
                                    unsigned MCID);
160
  void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
161
                                    bool IsOldCtorDtor,
162
                                    ArrayRef<Constant *> NewMembers,
163
                                    unsigned MCID);
164
  void scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
165
                                unsigned MCID);
166
  void scheduleRemapFunction(Function &F, unsigned MCID);
167
168
  void flush();
169
170
private:
171
  void mapGlobalInitializer(GlobalVariable &GV, Constant &Init);
172
  void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
173
                            bool IsOldCtorDtor,
174
                            ArrayRef<Constant *> NewMembers);
175
  void mapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee);
176
  void remapFunction(Function &F, ValueToValueMapTy &VM);
177
178
33.9M
  ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
179
2.64M
  ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
180
181
  Value *mapBlockAddress(const BlockAddress &BA);
182
183
  /// Map metadata that doesn't require visiting operands.
184
  Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
185
186
  Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
187
  Metadata *mapToSelf(const Metadata *MD);
188
};
189
190
class MDNodeMapper {
191
  Mapper &M;
192
193
  /// Data about a node in \a UniquedGraph.
194
  struct Data {
195
    bool HasChanged = false;
196
    unsigned ID = std::numeric_limits<unsigned>::max();
197
    TempMDNode Placeholder;
198
  };
199
200
  /// A graph of uniqued nodes.
201
  struct UniquedGraph {
202
    SmallDenseMap<const Metadata *, Data, 32> Info; // Node properties.
203
    SmallVector<MDNode *, 16> POT;                  // Post-order traversal.
204
205
    /// Propagate changed operands through the post-order traversal.
206
    ///
207
    /// Iteratively update \a Data::HasChanged for each node based on \a
208
    /// Data::HasChanged of its operands, until fixed point.
209
    void propagateChanges();
210
211
    /// Get a forward reference to a node to use as an operand.
212
    Metadata &getFwdReference(MDNode &Op);
213
  };
214
215
  /// Worklist of distinct nodes whose operands need to be remapped.
216
  SmallVector<MDNode *, 16> DistinctWorklist;
217
218
  // Storage for a UniquedGraph.
219
  SmallDenseMap<const Metadata *, Data, 32> InfoStorage;
220
  SmallVector<MDNode *, 16> POTStorage;
221
222
public:
223
1.34k
  MDNodeMapper(Mapper &M) : M(M) {}
224
225
  /// Map a metadata node (and its transitive operands).
226
  ///
227
  /// Map all the (unmapped) nodes in the subgraph under \c N.  The iterative
228
  /// algorithm handles distinct nodes and uniqued node subgraphs using
229
  /// different strategies.
230
  ///
231
  /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
232
  /// using \a mapDistinctNode().  Their mapping can always be computed
233
  /// immediately without visiting operands, even if their operands change.
234
  ///
235
  /// The mapping for uniqued nodes depends on whether their operands change.
236
  /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
237
  /// a node to calculate uniqued node mappings in bulk.  Distinct leafs are
238
  /// added to \a DistinctWorklist with \a mapDistinctNode().
239
  ///
240
  /// After mapping \c N itself, this function remaps the operands of the
241
  /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
242
  /// N has been mapped.
243
  Metadata *map(const MDNode &N);
244
245
private:
246
  /// Map a top-level uniqued node and the uniqued subgraph underneath it.
247
  ///
248
  /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
249
  /// underneath \c FirstN and calculates the nodes' mapping.  Each node uses
250
  /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
251
  /// operands uses the identity mapping.
252
  ///
253
  /// The algorithm works as follows:
254
  ///
255
  ///  1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
256
  ///     save the post-order traversal in the given \a UniquedGraph, tracking
257
  ///     nodes' operands change.
258
  ///
259
  ///  2. \a UniquedGraph::propagateChanges(): propagate changed operands
260
  ///     through the \a UniquedGraph until fixed point, following the rule
261
  ///     that if a node changes, any node that references must also change.
262
  ///
263
  ///  3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
264
  ///     (referencing new operands) where necessary.
265
  Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
266
267
  /// Try to map the operand of an \a MDNode.
268
  ///
269
  /// If \c Op is already mapped, return the mapping.  If it's not an \a
270
  /// MDNode, compute and return the mapping.  If it's a distinct \a MDNode,
271
  /// return the result of \a mapDistinctNode().
272
  ///
273
  /// \return None if \c Op is an unmapped uniqued \a MDNode.
274
  /// \post getMappedOp(Op) only returns None if this returns None.
275
  Optional<Metadata *> tryToMapOperand(const Metadata *Op);
276
277
  /// Map a distinct node.
278
  ///
279
  /// Return the mapping for the distinct node \c N, saving the result in \a
280
  /// DistinctWorklist for later remapping.
281
  ///
282
  /// \pre \c N is not yet mapped.
283
  /// \pre \c N.isDistinct().
284
  MDNode *mapDistinctNode(const MDNode &N);
285
286
  /// Get a previously mapped node.
287
  Optional<Metadata *> getMappedOp(const Metadata *Op) const;
288
289
  /// Create a post-order traversal of an unmapped uniqued node subgraph.
290
  ///
291
  /// This traverses the metadata graph deeply enough to map \c FirstN.  It
292
  /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
293
  /// metadata that has already been mapped will not be part of the POT.
294
  ///
295
  /// Each node that has a changed operand from outside the graph (e.g., a
296
  /// distinct node, an already-mapped uniqued node, or \a ConstantAsMetadata)
297
  /// is marked with \a Data::HasChanged.
298
  ///
299
  /// \return \c true if any nodes in \c G have \a Data::HasChanged.
300
  /// \post \c G.POT is a post-order traversal ending with \c FirstN.
301
  /// \post \a Data::hasChanged in \c G.Info indicates whether any node needs
302
  /// to change because of operands outside the graph.
303
  bool createPOT(UniquedGraph &G, const MDNode &FirstN);
304
305
  /// Visit the operands of a uniqued node in the POT.
306
  ///
307
  /// Visit the operands in the range from \c I to \c E, returning the first
308
  /// uniqued node we find that isn't yet in \c G.  \c I is always advanced to
309
  /// where to continue the loop through the operands.
310
  ///
311
  /// This sets \c HasChanged if any of the visited operands change.
312
  MDNode *visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
313
                        MDNode::op_iterator E, bool &HasChanged);
314
315
  /// Map all the nodes in the given uniqued graph.
316
  ///
317
  /// This visits all the nodes in \c G in post-order, using the identity
318
  /// mapping or creating a new node depending on \a Data::HasChanged.
319
  ///
320
  /// \pre \a getMappedOp() returns None for nodes in \c G, but not for any of
321
  /// their operands outside of \c G.
322
  /// \pre \a Data::HasChanged is true for a node in \c G iff any of its
323
  /// operands have changed.
324
  /// \post \a getMappedOp() returns the mapped node for every node in \c G.
325
  void mapNodesInPOT(UniquedGraph &G);
326
327
  /// Remap a node's operands using the given functor.
328
  ///
329
  /// Iterate through the operands of \c N and update them in place using \c
330
  /// mapOperand.
331
  ///
332
  /// \pre N.isDistinct() or N.isTemporary().
333
  template <class OperandMapper>
334
  void remapOperands(MDNode &N, OperandMapper mapOperand);
335
};
336
337
} // end anonymous namespace
338
339
14.0M
Value *Mapper::mapValue(const Value *V) {
340
14.0M
  ValueToValueMapTy::iterator I = getVM().find(V);
341
14.0M
342
14.0M
  // If the value already exists in the map, use it.
343
14.0M
  if (I != getVM().end()) {
344
11.4M
    assert(I->second && "Unexpected null mapping");
345
11.4M
    return I->second;
346
11.4M
  }
347
2.64M
348
2.64M
  // If we have a materializer and it can materialize a value, use that.
349
2.64M
  if (auto *Materializer = getMaterializer()) {
350
4.77k
    if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
351
2.31k
      getVM()[V] = NewV;
352
2.31k
      return NewV;
353
2.31k
    }
354
2.64M
  }
355
2.64M
356
2.64M
  // Global values do not need to be seeded into the VM if they
357
2.64M
  // are using the identity mapping.
358
2.64M
  if (isa<GlobalValue>(V)) {
359
490k
    if (Flags & RF_NullMapMissingGlobalValues)
360
18
      return nullptr;
361
490k
    return getVM()[V] = const_cast<Value *>(V);
362
490k
  }
363
2.15M
364
2.15M
  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
365
6.31k
    // Inline asm may need *type* remapping.
366
6.31k
    FunctionType *NewTy = IA->getFunctionType();
367
6.31k
    if (TypeMapper) {
368
8
      NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
369
8
370
8
      if (NewTy != IA->getFunctionType())
371
0
        V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
372
0
                           IA->hasSideEffects(), IA->isAlignStack());
373
8
    }
374
6.31k
375
6.31k
    return getVM()[V] = const_cast<Value *>(V);
376
6.31k
  }
377
2.14M
378
2.14M
  if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
379
425
    const Metadata *MD = MDV->getMetadata();
380
425
381
425
    if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
382
164
      // Look through to grab the local value.
383
164
      if (Value *LV = mapValue(LAM->getValue())) {
384
116
        if (V == LAM->getValue())
385
0
          return const_cast<Value *>(V);
386
116
        return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV));
387
116
      }
388
48
389
48
      // FIXME: always return nullptr once Verifier::verifyDominatesUse()
390
48
      // ensures metadata operands only reference defined SSA values.
391
48
      return (Flags & RF_IgnoreMissingLocals)
392
48
                 ? 
nullptr45
393
48
                 : MetadataAsValue::get(V->getContext(),
394
3
                                        MDTuple::get(V->getContext(), None));
395
48
    }
396
261
397
261
    // If this is a module-level metadata and we know that nothing at the module
398
261
    // level is changing, then use an identity mapping.
399
261
    if (Flags & RF_NoModuleLevelChanges)
400
131
      return getVM()[V] = const_cast<Value *>(V);
401
130
402
130
    // Map the metadata and turn it into a value.
403
130
    auto *MappedMD = mapMetadata(MD);
404
130
    if (MD == MappedMD)
405
113
      return getVM()[V] = const_cast<Value *>(V);
406
17
    return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
407
17
  }
408
2.14M
409
2.14M
  // Okay, this either must be a constant (which may or may not be mappable) or
410
2.14M
  // is something that is not in the mapping table.
411
2.14M
  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
412
2.14M
  if (!C)
413
211k
    return nullptr;
414
1.93M
415
1.93M
  if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
416
6
    return mapBlockAddress(*BA);
417
1.93M
418
1.93M
  auto mapValueOrNull = [this](Value *V) {
419
343k
    auto Mapped = mapValue(V);
420
343k
    assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
421
343k
           "Unexpected null mapping for constant operand without "
422
343k
           "NullMapMissingGlobalValues flag");
423
343k
    return Mapped;
424
343k
  };
425
1.93M
426
1.93M
  // Otherwise, we have some other constant to remap.  Start by checking to see
427
1.93M
  // if all operands have an identity remapping.
428
1.93M
  unsigned OpNo = 0, NumOperands = C->getNumOperands();
429
1.93M
  Value *Mapped = nullptr;
430
2.27M
  for (; OpNo != NumOperands; 
++OpNo343k
) {
431
343k
    Value *Op = C->getOperand(OpNo);
432
343k
    Mapped = mapValueOrNull(Op);
433
343k
    if (!Mapped)
434
1
      return nullptr;
435
343k
    if (Mapped != Op)
436
332
      break;
437
343k
  }
438
1.93M
439
1.93M
  // See if the type mapper wants to remap the type as well.
440
1.93M
  Type *NewTy = C->getType();
441
1.93M
  if (TypeMapper)
442
1.12k
    NewTy = TypeMapper->remapType(NewTy);
443
1.93M
444
1.93M
  // If the result type and all operands match up, then just insert an identity
445
1.93M
  // mapping.
446
1.93M
  if (OpNo == NumOperands && 
NewTy == C->getType()1.93M
)
447
1.93M
    return getVM()[V] = C;
448
345
449
345
  // Okay, we need to create a new constant.  We've already processed some or
450
345
  // all of the operands, set them all up now.
451
345
  SmallVector<Constant*, 8> Ops;
452
345
  Ops.reserve(NumOperands);
453
461
  for (unsigned j = 0; j != OpNo; 
++j116
)
454
116
    Ops.push_back(cast<Constant>(C->getOperand(j)));
455
345
456
345
  // If one of the operands mismatch, push it and the other mapped operands.
457
345
  if (OpNo != NumOperands) {
458
332
    Ops.push_back(cast<Constant>(Mapped));
459
332
460
332
    // Map the rest of the operands that aren't processed yet.
461
516
    for (++OpNo; OpNo != NumOperands; 
++OpNo184
) {
462
184
      Mapped = mapValueOrNull(C->getOperand(OpNo));
463
184
      if (!Mapped)
464
0
        return nullptr;
465
184
      Ops.push_back(cast<Constant>(Mapped));
466
184
    }
467
332
  }
468
345
  Type *NewSrcTy = nullptr;
469
345
  if (TypeMapper)
470
242
    if (auto *GEPO = dyn_cast<GEPOperator>(C))
471
23
      NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
472
345
473
345
  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
474
187
    return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
475
158
  if (isa<ConstantArray>(C))
476
53
    return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
477
105
  if (isa<ConstantStruct>(C))
478
93
    return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
479
12
  if (isa<ConstantVector>(C))
480
0
    return getVM()[V] = ConstantVector::get(Ops);
481
12
  // If this is a no-operand constant, it must be because the type was remapped.
482
12
  if (isa<UndefValue>(C))
483
0
    return getVM()[V] = UndefValue::get(NewTy);
484
12
  if (isa<ConstantAggregateZero>(C))
485
3
    return getVM()[V] = ConstantAggregateZero::get(NewTy);
486
9
  assert(isa<ConstantPointerNull>(C));
487
9
  return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
488
9
}
489
490
6
Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
491
6
  Function *F = cast<Function>(mapValue(BA.getFunction()));
492
6
493
6
  // F may not have materialized its initializer.  In that case, create a
494
6
  // dummy basic block for now, and replace it once we've materialized all
495
6
  // the initializers.
496
6
  BasicBlock *BB;
497
6
  if (F->empty()) {
498
2
    DelayedBBs.push_back(DelayedBasicBlock(BA));
499
2
    BB = DelayedBBs.back().TempBB.get();
500
4
  } else {
501
4
    BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
502
4
  }
503
6
504
6
  return getVM()[&BA] = BlockAddress::get(F, BB ? 
BB2
:
BA.getBasicBlock()4
);
505
6
}
506
507
2.24k
Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
508
2.24k
  getVM().MD()[Key].reset(Val);
509
2.24k
  return Val;
510
2.24k
}
511
512
1.77k
Metadata *Mapper::mapToSelf(const Metadata *MD) {
513
1.77k
  return mapToMetadata(MD, const_cast<Metadata *>(MD));
514
1.77k
}
515
516
7.76k
Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
517
7.76k
  if (!Op)
518
3.22k
    return nullptr;
519
4.54k
520
4.54k
  if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
521
#ifndef NDEBUG
522
    if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
523
      assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
524
              M.getVM().getMappedMD(Op)) &&
525
             "Expected Value to be memoized");
526
    else
527
      assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
528
             "Expected result to be memoized");
529
#endif
530
    return *MappedOp;
531
3.63k
  }
532
911
533
911
  const MDNode &N = *cast<MDNode>(Op);
534
911
  if (N.isDistinct())
535
228
    return mapDistinctNode(N);
536
683
  return None;
537
683
}
538
539
192
static Metadata *cloneOrBuildODR(const MDNode &N) {
540
192
  auto *CT = dyn_cast<DICompositeType>(&N);
541
192
  // If ODR type uniquing is enabled, we would have uniqued composite types
542
192
  // with identifiers during bitcode reading, so we can just use CT.
543
192
  if (CT && 
CT->getContext().isODRUniquingDebugTypes()3
&&
544
192
      
CT->getIdentifier() != ""2
)
545
2
    return const_cast<DICompositeType *>(CT);
546
190
  return MDNode::replaceWithDistinct(N.clone());
547
190
}
548
549
514
MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
550
514
  assert(N.isDistinct() && "Expected a distinct node");
551
514
  assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
552
514
  DistinctWorklist.push_back(
553
514
      cast<MDNode>((M.Flags & RF_MoveDistinctMDs)
554
514
                       ? 
M.mapToSelf(&N)322
555
514
                       : 
M.mapToMetadata(&N, cloneOrBuildODR(N))192
));
556
514
  return DistinctWorklist.back();
557
514
}
558
559
static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
560
533
                                                  Value *MappedV) {
561
533
  if (CMD.getValue() == MappedV)
562
463
    return const_cast<ConstantAsMetadata *>(&CMD);
563
70
  return MappedV ? 
ConstantAsMetadata::getConstant(MappedV)36
:
nullptr34
;
564
70
}
565
566
672
Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
567
672
  if (!Op)
568
40
    return nullptr;
569
632
570
632
  if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
571
564
    return *MappedOp;
572
68
573
68
  if (isa<MDString>(Op))
574
31
    return const_cast<Metadata *>(Op);
575
37
576
37
  if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
577
36
    return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
578
1
579
1
  return None;
580
1
}
581
582
1
Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
583
1
  auto Where = Info.find(&Op);
584
1
  assert(Where != Info.end() && "Expected a valid reference");
585
1
586
1
  auto &OpD = Where->second;
587
1
  if (!OpD.HasChanged)
588
0
    return Op;
589
1
590
1
  // Lazily construct a temporary node.
591
1
  if (!OpD.Placeholder)
592
1
    OpD.Placeholder = Op.clone();
593
1
594
1
  return *OpD.Placeholder;
595
1
}
596
597
template <class OperandMapper>
598
791
void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
599
791
  assert(!N.isUniqued() && "Expected distinct or temporary nodes");
600
5.16k
  for (unsigned I = 0, E = N.getNumOperands(); I != E; 
++I4.37k
) {
601
4.37k
    Metadata *Old = N.getOperand(I);
602
4.37k
    Metadata *New = mapOperand(Old);
603
4.37k
604
4.37k
    if (Old != New)
605
389
      N.replaceOperandWith(I, New);
606
4.37k
  }
607
791
}
ValueMapper.cpp:void (anonymous namespace)::MDNodeMapper::remapOperands<(anonymous namespace)::MDNodeMapper::mapNodesInPOT((anonymous namespace)::MDNodeMapper::UniquedGraph&)::$_1>(llvm::MDNode&, (anonymous namespace)::MDNodeMapper::mapNodesInPOT((anonymous namespace)::MDNodeMapper::UniquedGraph&)::$_1)
Line
Count
Source
598
277
void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
599
277
  assert(!N.isUniqued() && "Expected distinct or temporary nodes");
600
706
  for (unsigned I = 0, E = N.getNumOperands(); I != E; 
++I429
) {
601
429
    Metadata *Old = N.getOperand(I);
602
429
    Metadata *New = mapOperand(Old);
603
429
604
429
    if (Old != New)
605
291
      N.replaceOperandWith(I, New);
606
429
  }
607
277
}
ValueMapper.cpp:void (anonymous namespace)::MDNodeMapper::remapOperands<(anonymous namespace)::MDNodeMapper::map(llvm::MDNode const&)::$_2>(llvm::MDNode&, (anonymous namespace)::MDNodeMapper::map(llvm::MDNode const&)::$_2)
Line
Count
Source
598
514
void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
599
514
  assert(!N.isUniqued() && "Expected distinct or temporary nodes");
600
4.45k
  for (unsigned I = 0, E = N.getNumOperands(); I != E; 
++I3.94k
) {
601
3.94k
    Metadata *Old = N.getOperand(I);
602
3.94k
    Metadata *New = mapOperand(Old);
603
3.94k
604
3.94k
    if (Old != New)
605
98
      N.replaceOperandWith(I, New);
606
3.94k
  }
607
514
}
608
609
namespace {
610
611
/// An entry in the worklist for the post-order traversal.
612
struct POTWorklistEntry {
613
  MDNode *N;              ///< Current node.
614
  MDNode::op_iterator Op; ///< Current operand of \c N.
615
616
  /// Keep a flag of whether operands have changed in the worklist to avoid
617
  /// hitting the map in \a UniquedGraph.
618
  bool HasChanged = false;
619
620
1.72k
  POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
621
};
622
623
} // end anonymous namespace
624
625
1.42k
bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
626
1.42k
  assert(G.Info.empty() && "Expected a fresh traversal");
627
1.42k
  assert(FirstN.isUniqued() && "Expected uniqued node in POT");
628
1.42k
629
1.42k
  // Construct a post-order traversal of the uniqued subgraph under FirstN.
630
1.42k
  bool AnyChanges = false;
631
1.42k
  SmallVector<POTWorklistEntry, 16> Worklist;
632
1.42k
  Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
633
1.42k
  (void)G.Info[&FirstN];
634
3.45k
  while (!Worklist.empty()) {
635
2.03k
    // Start or continue the traversal through the this node's operands.
636
2.03k
    auto &WE = Worklist.back();
637
2.03k
    if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
638
306
      // Push a new node to traverse first.
639
306
      Worklist.push_back(POTWorklistEntry(*N));
640
306
      continue;
641
306
    }
642
1.72k
643
1.72k
    // Push the node onto the POT.
644
1.72k
    assert(WE.N->isUniqued() && "Expected only uniqued nodes");
645
1.72k
    assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
646
1.72k
    auto &D = G.Info[WE.N];
647
1.72k
    AnyChanges |= D.HasChanged = WE.HasChanged;
648
1.72k
    D.ID = G.POT.size();
649
1.72k
    G.POT.push_back(WE.N);
650
1.72k
651
1.72k
    // Pop the node off the worklist.
652
1.72k
    Worklist.pop_back();
653
1.72k
  }
654
1.42k
  return AnyChanges;
655
1.42k
}
656
657
MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
658
2.03k
                                    MDNode::op_iterator E, bool &HasChanged) {
659
5.54k
  while (I != E) {
660
3.82k
    Metadata *Op = *I++; // Increment even on early return.
661
3.82k
    if (Optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
662
3.50k
      // Check if the operand changes.
663
3.50k
      HasChanged |= Op != *MappedOp;
664
3.50k
      continue;
665
3.50k
    }
666
317
667
317
    // A uniqued metadata node.
668
317
    MDNode &OpN = *cast<MDNode>(Op);
669
317
    assert(OpN.isUniqued() &&
670
317
           "Only uniqued operands cannot be mapped immediately");
671
317
    if (G.Info.insert(std::make_pair(&OpN, Data())).second)
672
306
      return &OpN; // This is a new one.  Return it.
673
317
  }
674
2.03k
  
return nullptr1.72k
;
675
2.03k
}
676
677
243
void MDNodeMapper::UniquedGraph::propagateChanges() {
678
243
  bool AnyChanges;
679
260
  do {
680
260
    AnyChanges = false;
681
356
    for (MDNode *N : POT) {
682
356
      auto &D = Info[N];
683
356
      if (D.HasChanged)
684
302
        continue;
685
54
686
151
      
if (54
llvm::none_of(N->operands(), [&](const Metadata *Op) 54
{
687
151
            auto Where = Info.find(Op);
688
151
            return Where != Info.end() && 
Where->second.HasChanged22
;
689
151
          }))
690
32
        continue;
691
22
692
22
      AnyChanges = D.HasChanged = true;
693
22
    }
694
260
  } while (AnyChanges);
695
243
}
696
697
243
void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
698
243
  // Construct uniqued nodes, building forward references as necessary.
699
243
  SmallVector<MDNode *, 16> CyclicNodes;
700
293
  for (auto *N : G.POT) {
701
293
    auto &D = G.Info[N];
702
293
    if (!D.HasChanged) {
703
16
      // The node hasn't changed.
704
16
      M.mapToSelf(N);
705
16
      continue;
706
16
    }
707
277
708
277
    // Remember whether this node had a placeholder.
709
277
    bool HadPlaceholder(D.Placeholder);
710
277
711
277
    // Clone the uniqued node and remap the operands.
712
277
    TempMDNode ClonedN = D.Placeholder ? 
std::move(D.Placeholder)1
:
N->clone()276
;
713
429
    remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
714
429
      if (Optional<Metadata *> MappedOp = getMappedOp(Old))
715
428
        return *MappedOp;
716
1
      (void)D;
717
1
      assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
718
1
      return &G.getFwdReference(*cast<MDNode>(Old));
719
1
    });
720
277
721
277
    auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
722
277
    M.mapToMetadata(N, NewN);
723
277
724
277
    // Nodes that were referenced out of order in the POT are involved in a
725
277
    // uniquing cycle.
726
277
    if (HadPlaceholder)
727
1
      CyclicNodes.push_back(NewN);
728
277
  }
729
243
730
243
  // Resolve cycles.
731
243
  for (auto *N : CyclicNodes)
732
1
    if (!N->isResolved())
733
1
      N->resolveCycles();
734
243
}
735
736
1.34k
Metadata *MDNodeMapper::map(const MDNode &N) {
737
1.34k
  assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
738
1.34k
  assert(!(M.Flags & RF_NoModuleLevelChanges) &&
739
1.34k
         "MDNodeMapper::map assumes module-level changes");
740
1.34k
741
1.34k
  // Require resolved nodes whenever metadata might be remapped.
742
1.34k
  assert(N.isResolved() && "Unexpected unresolved node");
743
1.34k
744
1.34k
  Metadata *MappedN =
745
1.34k
      N.isUniqued() ? 
mapTopLevelUniquedNode(N)1.05k
:
mapDistinctNode(N)286
;
746
1.85k
  while (!DistinctWorklist.empty())
747
3.94k
    
remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) 514
{
748
3.94k
      if (Optional<Metadata *> MappedOp = tryToMapOperand(Old))
749
3.57k
        return *MappedOp;
750
365
      return mapTopLevelUniquedNode(*cast<MDNode>(Old));
751
365
    });
752
1.34k
  return MappedN;
753
1.34k
}
754
755
1.42k
Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
756
1.42k
  assert(FirstN.isUniqued() && "Expected uniqued node");
757
1.42k
758
1.42k
  // Create a post-order traversal of uniqued nodes under FirstN.
759
1.42k
  UniquedGraph G;
760
1.42k
  if (!createPOT(G, FirstN)) {
761
1.17k
    // Return early if no nodes have changed.
762
1.17k
    for (const MDNode *N : G.POT)
763
1.43k
      M.mapToSelf(N);
764
1.17k
    return &const_cast<MDNode &>(FirstN);
765
1.17k
  }
766
243
767
243
  // Update graph with all nodes that have changed.
768
243
  G.propagateChanges();
769
243
770
243
  // Map all the nodes in the graph.
771
243
  mapNodesInPOT(G);
772
243
773
243
  // Return the original node, remapped.
774
243
  return *getMappedOp(&FirstN);
775
243
}
776
777
namespace {
778
779
struct MapMetadataDisabler {
780
  ValueToValueMapTy &VM;
781
782
497
  MapMetadataDisabler(ValueToValueMapTy &VM) : VM(VM) {
783
497
    VM.disableMapMetadata();
784
497
  }
785
786
497
  ~MapMetadataDisabler() { VM.enableMapMetadata(); }
787
};
788
789
} // end anonymous namespace
790
791
3.32M
Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
792
3.32M
  // If the value already exists in the map, use it.
793
3.32M
  if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
794
2.32k
    return *NewMD;
795
3.32M
796
3.32M
  if (isa<MDString>(MD))
797
1.42k
    return const_cast<Metadata *>(MD);
798
3.32M
799
3.32M
  // This is a module-level metadata.  If nothing at the module level is
800
3.32M
  // changing, use an identity mapping.
801
3.32M
  if ((Flags & RF_NoModuleLevelChanges))
802
3.31M
    return const_cast<Metadata *>(MD);
803
2.74k
804
2.74k
  if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
805
497
    // Disallow recursion into metadata mapping through mapValue.
806
497
    MapMetadataDisabler MMD(getVM());
807
497
808
497
    // Don't memoize ConstantAsMetadata.  Instead of lasting until the
809
497
    // LLVMContext is destroyed, they can be deleted when the GlobalValue they
810
497
    // reference is destructed.  These aren't super common, so the extra
811
497
    // indirection isn't that expensive.
812
497
    return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
813
497
  }
814
2.25k
815
2.25k
  assert(isa<MDNode>(MD) && "Expected a metadata node");
816
2.25k
817
2.25k
  return None;
818
2.25k
}
819
820
3.31M
Metadata *Mapper::mapMetadata(const Metadata *MD) {
821
3.31M
  assert(MD && "Expected valid metadata");
822
3.31M
  assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
823
3.31M
824
3.31M
  if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
825
3.31M
    return *NewMD;
826
1.34k
827
1.34k
  return MDNodeMapper(*this).map(*cast<MDNode>(MD));
828
1.34k
}
829
830
6.79M
void Mapper::flush() {
831
6.79M
  // Flush out the worklist of global values.
832
6.79M
  while (!Worklist.empty()) {
833
1.75k
    WorklistEntry E = Worklist.pop_back_val();
834
1.75k
    CurrentMCID = E.MCID;
835
1.75k
    switch (E.Kind) {
836
1.75k
    case WorklistEntry::MapGlobalInit:
837
437
      E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
838
437
      remapGlobalObjectMetadata(*E.Data.GVInit.GV);
839
437
      break;
840
1.75k
    case WorklistEntry::MapAppendingVar: {
841
64
      unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
842
64
      mapAppendingVariable(*E.Data.AppendingGV.GV,
843
64
                           E.Data.AppendingGV.InitPrefix,
844
64
                           E.AppendingGVIsOldCtorDtor,
845
64
                           makeArrayRef(AppendingInits).slice(PrefixSize));
846
64
      AppendingInits.resize(PrefixSize);
847
64
      break;
848
1.75k
    }
849
1.75k
    case WorklistEntry::MapGlobalAliasee:
850
81
      E.Data.GlobalAliasee.GA->setAliasee(
851
81
          mapConstant(E.Data.GlobalAliasee.Aliasee));
852
81
      break;
853
1.75k
    case WorklistEntry::RemapFunction:
854
1.17k
      remapFunction(*E.Data.RemapF);
855
1.17k
      break;
856
1.75k
    }
857
1.75k
  }
858
6.79M
  CurrentMCID = 0;
859
6.79M
860
6.79M
  // Finish logic for block addresses now that all global values have been
861
6.79M
  // handled.
862
6.79M
  while (!DelayedBBs.empty()) {
863
2
    DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
864
2
    BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
865
2
    DBB.TempBB->replaceAllUsesWith(BB ? 
BB0
: DBB.OldBB);
866
2
  }
867
6.79M
}
868
869
6.37M
void Mapper::remapInstruction(Instruction *I) {
870
6.37M
  // Remap operands.
871
13.2M
  for (Use &Op : I->operands()) {
872
13.2M
    Value *V = mapValue(Op);
873
13.2M
    // If we aren't ignoring missing entries, assert that something happened.
874
13.2M
    if (V)
875
13.0M
      Op = V;
876
13.2M
    else
877
13.2M
      assert((Flags & RF_IgnoreMissingLocals) &&
878
13.2M
             "Referenced value not in value map!");
879
13.2M
  }
880
6.37M
881
6.37M
  // Remap phi nodes' incoming blocks.
882
6.37M
  if (PHINode *PN = dyn_cast<PHINode>(I)) {
883
98.2k
    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; 
++i63.9k
) {
884
63.9k
      Value *V = mapValue(PN->getIncomingBlock(i));
885
63.9k
      // If we aren't ignoring missing entries, assert that something happened.
886
63.9k
      if (V)
887
57.6k
        PN->setIncomingBlock(i, cast<BasicBlock>(V));
888
63.9k
      else
889
63.9k
        assert((Flags & RF_IgnoreMissingLocals) &&
890
63.9k
               "Referenced block not in value map!");
891
63.9k
    }
892
34.3k
  }
893
6.37M
894
6.37M
  // Remap attached metadata.
895
6.37M
  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
896
6.37M
  I->getAllMetadata(MDs);
897
6.37M
  for (const auto &MI : MDs) {
898
3.31M
    MDNode *Old = MI.second;
899
3.31M
    MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
900
3.31M
    if (New != Old)
901
353
      I->setMetadata(MI.first, New);
902
3.31M
  }
903
6.37M
904
6.37M
  if (!TypeMapper)
905
6.37M
    return;
906
2.59k
907
2.59k
  // If the instruction's type is being remapped, do so now.
908
2.59k
  if (auto CS = CallSite(I)) {
909
597
    SmallVector<Type *, 3> Tys;
910
597
    FunctionType *FTy = CS.getFunctionType();
911
597
    Tys.reserve(FTy->getNumParams());
912
597
    for (Type *Ty : FTy->params())
913
373
      Tys.push_back(TypeMapper->remapType(Ty));
914
597
    CS.mutateFunctionType(FunctionType::get(
915
597
        TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
916
597
917
597
    LLVMContext &C = CS->getContext();
918
597
    AttributeList Attrs = CS.getAttributes();
919
646
    for (unsigned i = 0; i < Attrs.getNumAttrSets(); 
++i49
) {
920
49
      if (Attrs.hasAttribute(i, Attribute::ByVal)) {
921
4
        Type *Ty = Attrs.getAttribute(i, Attribute::ByVal).getValueAsType();
922
4
        if (!Ty)
923
2
          continue;
924
2
925
2
        Attrs = Attrs.removeAttribute(C, i, Attribute::ByVal);
926
2
        Attrs = Attrs.addAttribute(
927
2
            C, i, Attribute::getWithByValType(C, TypeMapper->remapType(Ty)));
928
2
      }
929
49
    }
930
597
    CS.setAttributes(Attrs);
931
597
    return;
932
597
  }
933
1.99k
  if (auto *AI = dyn_cast<AllocaInst>(I))
934
115
    AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
935
1.99k
  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
936
93
    GEP->setSourceElementType(
937
93
        TypeMapper->remapType(GEP->getSourceElementType()));
938
93
    GEP->setResultElementType(
939
93
        TypeMapper->remapType(GEP->getResultElementType()));
940
93
  }
941
1.99k
  I->mutateType(TypeMapper->remapType(I->getType()));
942
1.99k
}
943
944
1.61k
void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
945
1.61k
  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
946
1.61k
  GO.getAllMetadata(MDs);
947
1.61k
  GO.clearMetadata();
948
1.61k
  for (const auto &I : MDs)
949
216
    GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
950
1.61k
}
951
952
1.17k
void Mapper::remapFunction(Function &F) {
953
1.17k
  // Remap the operands.
954
1.17k
  for (Use &Op : F.operands())
955
9
    if (Op)
956
9
      Op = mapValue(Op);
957
1.17k
958
1.17k
  // Remap the metadata attachments.
959
1.17k
  remapGlobalObjectMetadata(F);
960
1.17k
961
1.17k
  // Remap the argument types.
962
1.17k
  if (TypeMapper)
963
1.17k
    for (Argument &A : F.args())
964
301
      A.mutateType(TypeMapper->remapType(A.getType()));
965
1.17k
966
1.17k
  // Remap the instructions.
967
1.17k
  for (BasicBlock &BB : F)
968
1.23k
    for (Instruction &I : BB)
969
2.59k
      remapInstruction(&I);
970
1.17k
}
971
972
void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
973
                                  bool IsOldCtorDtor,
974
64
                                  ArrayRef<Constant *> NewMembers) {
975
64
  SmallVector<Constant *, 16> Elements;
976
64
  if (InitPrefix) {
977
16
    unsigned NumElements =
978
16
        cast<ArrayType>(InitPrefix->getType())->getNumElements();
979
33
    for (unsigned I = 0; I != NumElements; 
++I17
)
980
17
      Elements.push_back(InitPrefix->getAggregateElement(I));
981
16
  }
982
64
983
64
  PointerType *VoidPtrTy;
984
64
  Type *EltTy;
985
64
  if (IsOldCtorDtor) {
986
0
    // FIXME: This upgrade is done during linking to support the C API.  See
987
0
    // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
988
0
    VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
989
0
    auto &ST = *cast<StructType>(NewMembers.front()->getType());
990
0
    Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
991
0
    EltTy = StructType::get(GV.getContext(), Tys, false);
992
0
  }
993
64
994
90
  for (auto *V : NewMembers) {
995
90
    Constant *NewV;
996
90
    if (IsOldCtorDtor) {
997
0
      auto *S = cast<ConstantStruct>(V);
998
0
      auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
999
0
      auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
1000
0
      Constant *Null = Constant::getNullValue(VoidPtrTy);
1001
0
      NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
1002
90
    } else {
1003
90
      NewV = cast_or_null<Constant>(mapValue(V));
1004
90
    }
1005
90
    Elements.push_back(NewV);
1006
90
  }
1007
64
1008
64
  GV.setInitializer(ConstantArray::get(
1009
64
      cast<ArrayType>(GV.getType()->getElementType()), Elements));
1010
64
}
1011
1012
void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1013
437
                                          unsigned MCID) {
1014
437
  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1015
437
  assert(MCID < MCs.size() && "Invalid mapping context");
1016
437
1017
437
  WorklistEntry WE;
1018
437
  WE.Kind = WorklistEntry::MapGlobalInit;
1019
437
  WE.MCID = MCID;
1020
437
  WE.Data.GVInit.GV = &GV;
1021
437
  WE.Data.GVInit.Init = &Init;
1022
437
  Worklist.push_back(WE);
1023
437
}
1024
1025
void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1026
                                          Constant *InitPrefix,
1027
                                          bool IsOldCtorDtor,
1028
                                          ArrayRef<Constant *> NewMembers,
1029
64
                                          unsigned MCID) {
1030
64
  assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
1031
64
  assert(MCID < MCs.size() && "Invalid mapping context");
1032
64
1033
64
  WorklistEntry WE;
1034
64
  WE.Kind = WorklistEntry::MapAppendingVar;
1035
64
  WE.MCID = MCID;
1036
64
  WE.Data.AppendingGV.GV = &GV;
1037
64
  WE.Data.AppendingGV.InitPrefix = InitPrefix;
1038
64
  WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1039
64
  WE.AppendingGVNumNewMembers = NewMembers.size();
1040
64
  Worklist.push_back(WE);
1041
64
  AppendingInits.append(NewMembers.begin(), NewMembers.end());
1042
64
}
1043
1044
void Mapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1045
81
                                      unsigned MCID) {
1046
81
  assert(AlreadyScheduled.insert(&GA).second && "Should not reschedule");
1047
81
  assert(MCID < MCs.size() && "Invalid mapping context");
1048
81
1049
81
  WorklistEntry WE;
1050
81
  WE.Kind = WorklistEntry::MapGlobalAliasee;
1051
81
  WE.MCID = MCID;
1052
81
  WE.Data.GlobalAliasee.GA = &GA;
1053
81
  WE.Data.GlobalAliasee.Aliasee = &Aliasee;
1054
81
  Worklist.push_back(WE);
1055
81
}
1056
1057
1.17k
void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1058
1.17k
  assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
1059
1.17k
  assert(MCID < MCs.size() && "Invalid mapping context");
1060
1.17k
1061
1.17k
  WorklistEntry WE;
1062
1.17k
  WE.Kind = WorklistEntry::RemapFunction;
1063
1.17k
  WE.MCID = MCID;
1064
1.17k
  WE.Data.RemapF = &F;
1065
1.17k
  Worklist.push_back(WE);
1066
1.17k
}
1067
1068
1.07k
void Mapper::addFlags(RemapFlags Flags) {
1069
1.07k
  assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1070
1.07k
  this->Flags = this->Flags | Flags;
1071
1.07k
}
1072
1073
13.5M
static Mapper *getAsMapper(void *pImpl) {
1074
13.5M
  return reinterpret_cast<Mapper *>(pImpl);
1075
13.5M
}
1076
1077
namespace {
1078
1079
class FlushingMapper {
1080
  Mapper &M;
1081
1082
public:
1083
6.79M
  explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1084
6.79M
    assert(!M.hasWorkToDo() && "Expected to be flushed");
1085
6.79M
  }
1086
1087
6.79M
  ~FlushingMapper() { M.flush(); }
1088
1089
6.79M
  Mapper *operator->() const { return &M; }
1090
};
1091
1092
} // end anonymous namespace
1093
1094
ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1095
                         ValueMapTypeRemapper *TypeMapper,
1096
                         ValueMaterializer *Materializer)
1097
6.79M
    : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1098
1099
6.79M
ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1100
1101
unsigned
1102
ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1103
1.07k
                                             ValueMaterializer *Materializer) {
1104
1.07k
  return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1105
1.07k
}
1106
1107
1.07k
void ValueMapper::addFlags(RemapFlags Flags) {
1108
1.07k
  FlushingMapper(pImpl)->addFlags(Flags);
1109
1.07k
}
1110
1111
415k
Value *ValueMapper::mapValue(const Value &V) {
1112
415k
  return FlushingMapper(pImpl)->mapValue(&V);
1113
415k
}
1114
1115
151
Constant *ValueMapper::mapConstant(const Constant &C) {
1116
151
  return cast_or_null<Constant>(mapValue(C));
1117
151
}
1118
1119
856
Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1120
856
  return FlushingMapper(pImpl)->mapMetadata(&MD);
1121
856
}
1122
1123
845
MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1124
845
  return cast_or_null<MDNode>(mapMetadata(N));
1125
845
}
1126
1127
6.37M
void ValueMapper::remapInstruction(Instruction &I) {
1128
6.37M
  FlushingMapper(pImpl)->remapInstruction(&I);
1129
6.37M
}
1130
1131
0
void ValueMapper::remapFunction(Function &F) {
1132
0
  FlushingMapper(pImpl)->remapFunction(F);
1133
0
}
1134
1135
void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1136
                                               Constant &Init,
1137
437
                                               unsigned MCID) {
1138
437
  getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1139
437
}
1140
1141
void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1142
                                               Constant *InitPrefix,
1143
                                               bool IsOldCtorDtor,
1144
                                               ArrayRef<Constant *> NewMembers,
1145
64
                                               unsigned MCID) {
1146
64
  getAsMapper(pImpl)->scheduleMapAppendingVariable(
1147
64
      GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1148
64
}
1149
1150
void ValueMapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1151
81
                                           unsigned MCID) {
1152
81
  getAsMapper(pImpl)->scheduleMapGlobalAliasee(GA, Aliasee, MCID);
1153
81
}
1154
1155
1.17k
void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1156
1.17k
  getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1157
1.17k
}