blob: d82f513419f9686316ea524359f6242185ce0ede [file] [log] [blame]
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00009//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
Dan Gohmana2095032010-08-24 18:50:07 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +000016#include "llvm/ADT/DenseSet.h"
David Blaikie348de692015-04-23 21:36:23 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +000019#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Function.h"
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000021#include "llvm/IR/GlobalAlias.h"
22#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/InlineAsm.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/Metadata.h"
David Blaikie88208842015-08-21 20:16:51 +000026#include "llvm/IR/Operator.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000027using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000028
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000029// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000030void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000031void ValueMaterializer::anchor() {}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000032
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000033namespace {
34
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000035/// A basic block used in a BlockAddress whose function body is not yet
36/// materialized.
37struct DelayedBasicBlock {
38 BasicBlock *OldBB;
39 std::unique_ptr<BasicBlock> TempBB;
Duncan P. N. Exon Smitha9978562016-04-03 20:42:21 +000040
41 // Explicit move for MSVC.
42 DelayedBasicBlock(DelayedBasicBlock &&X)
43 : OldBB(std::move(X.OldBB)), TempBB(std::move(X.TempBB)) {}
44 DelayedBasicBlock &operator=(DelayedBasicBlock &&X) {
45 OldBB = std::move(X.OldBB);
46 TempBB = std::move(X.TempBB);
47 return *this;
48 }
49
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000050 DelayedBasicBlock(const BlockAddress &Old)
51 : OldBB(Old.getBasicBlock()),
52 TempBB(BasicBlock::Create(Old.getContext())) {}
53};
54
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000055struct WorklistEntry {
56 enum EntryKind {
57 MapGlobalInit,
58 MapAppendingVar,
59 MapGlobalAliasee,
60 RemapFunction
61 };
62 struct GVInitTy {
63 GlobalVariable *GV;
64 Constant *Init;
65 };
66 struct AppendingGVTy {
67 GlobalVariable *GV;
68 Constant *InitPrefix;
69 };
70 struct GlobalAliaseeTy {
71 GlobalAlias *GA;
72 Constant *Aliasee;
73 };
74
75 unsigned Kind : 2;
76 unsigned MCID : 29;
77 unsigned AppendingGVIsOldCtorDtor : 1;
78 unsigned AppendingGVNumNewMembers;
79 union {
80 GVInitTy GVInit;
81 AppendingGVTy AppendingGV;
82 GlobalAliaseeTy GlobalAliasee;
83 Function *RemapF;
84 } Data;
85};
86
87struct MappingContext {
88 ValueToValueMapTy *VM;
89 ValueMaterializer *Materializer = nullptr;
90
91 /// Construct a MappingContext with a value map and materializer.
92 explicit MappingContext(ValueToValueMapTy &VM,
93 ValueMaterializer *Materializer = nullptr)
94 : VM(&VM), Materializer(Materializer) {}
95};
96
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +000097class MDNodeMapper;
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000098class Mapper {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +000099 friend class MDNodeMapper;
100
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +0000101#ifndef NDEBUG
102 DenseSet<GlobalValue *> AlreadyScheduled;
103#endif
104
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000105 RemapFlags Flags;
106 ValueMapTypeRemapper *TypeMapper;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000107 unsigned CurrentMCID = 0;
108 SmallVector<MappingContext, 2> MCs;
109 SmallVector<WorklistEntry, 4> Worklist;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000110 SmallVector<DelayedBasicBlock, 1> DelayedBBs;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000111 SmallVector<Constant *, 16> AppendingInits;
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000112
113public:
114 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
115 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000116 : Flags(Flags), TypeMapper(TypeMapper),
117 MCs(1, MappingContext(VM, Materializer)) {}
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000118
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000119 /// ValueMapper should explicitly call \a flush() before destruction.
120 ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
121
122 bool hasWorkToDo() const { return !Worklist.empty(); }
123
124 unsigned
125 registerAlternateMappingContext(ValueToValueMapTy &VM,
126 ValueMaterializer *Materializer = nullptr) {
127 MCs.push_back(MappingContext(VM, Materializer));
128 return MCs.size() - 1;
129 }
130
131 void addFlags(RemapFlags Flags);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000132
133 Value *mapValue(const Value *V);
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000134 void remapInstruction(Instruction *I);
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000135 void remapFunction(Function &F);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000136
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000137 Constant *mapConstant(const Constant *C) {
138 return cast_or_null<Constant>(mapValue(C));
139 }
140
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000141 /// Map metadata.
142 ///
143 /// Find the mapping for MD. Guarantees that the return will be resolved
144 /// (not an MDNode, or MDNode::isResolved() returns true).
145 Metadata *mapMetadata(const Metadata *MD);
146
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000147 // Map LocalAsMetadata, which never gets memoized.
148 //
149 // If the referenced local is not mapped, the principled return is nullptr.
150 // However, optimization passes sometimes move metadata operands *before* the
151 // SSA values they reference. To prevent crashes in \a RemapInstruction(),
152 // return "!{}" when RF_IgnoreMissingLocals is not set.
153 //
154 // \note Adding a mapping for LocalAsMetadata is unsupported. Add a mapping
155 // to the value map for the SSA value in question instead.
156 //
157 // FIXME: Once we have a verifier check for forward references to SSA values
158 // through metadata operands, always return nullptr on unmapped locals.
159 Metadata *mapLocalAsMetadata(const LocalAsMetadata &LAM);
160
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000161 void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
162 unsigned MCID);
163 void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
164 bool IsOldCtorDtor,
165 ArrayRef<Constant *> NewMembers,
166 unsigned MCID);
167 void scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
168 unsigned MCID);
169 void scheduleRemapFunction(Function &F, unsigned MCID);
170
171 void flush();
172
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000173private:
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000174 void mapGlobalInitializer(GlobalVariable &GV, Constant &Init);
175 void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
176 bool IsOldCtorDtor,
177 ArrayRef<Constant *> NewMembers);
178 void mapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee);
179 void remapFunction(Function &F, ValueToValueMapTy &VM);
180
181 ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
182 ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000183
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000184 Value *mapBlockAddress(const BlockAddress &BA);
185
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000186 /// Map metadata that doesn't require visiting operands.
187 Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
188
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000189 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
190 Metadata *mapToSelf(const Metadata *MD);
191};
192
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000193class MDNodeMapper {
194 Mapper &M;
195
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000196 /// Data about a node in \a UniquedGraph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000197 struct Data {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000198 bool HasChanged = false;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000199 unsigned ID = ~0u;
200 TempMDNode Placeholder;
Duncan P. N. Exon Smithf880d352016-04-05 21:07:01 +0000201
Duncan P. N. Exon Smith818e5f32016-04-05 21:25:33 +0000202 Data() {}
203 Data(Data &&X)
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000204 : HasChanged(std::move(X.HasChanged)), ID(std::move(X.ID)),
205 Placeholder(std::move(X.Placeholder)) {}
Duncan P. N. Exon Smith818e5f32016-04-05 21:25:33 +0000206 Data &operator=(Data &&X) {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000207 HasChanged = std::move(X.HasChanged);
Duncan P. N. Exon Smith818e5f32016-04-05 21:25:33 +0000208 ID = std::move(X.ID);
209 Placeholder = std::move(X.Placeholder);
210 return *this;
211 }
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000212 };
213
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000214 /// A graph of uniqued nodes.
215 struct UniquedGraph {
216 SmallDenseMap<const Metadata *, Data, 32> Info; // Node properties.
217 SmallVector<MDNode *, 16> POT; // Post-order traversal.
218
219 /// Propagate changed operands through the post-order traversal.
220 ///
221 /// Iteratively update \a Data::HasChanged for each node based on \a
222 /// Data::HasChanged of its operands, until fixed point.
223 void propagateChanges();
224
225 /// Get a forward reference to a node to use as an operand.
226 Metadata &getFwdReference(MDNode &Op);
227 };
228
229 /// Worklist of distinct nodes whose operands need to be remapped.
230 SmallVector<MDNode *, 16> DistinctWorklist;
231
232 // Storage for a UniquedGraph.
233 SmallDenseMap<const Metadata *, Data, 32> InfoStorage;
234 SmallVector<MDNode *, 16> POTStorage;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000235
236public:
237 MDNodeMapper(Mapper &M) : M(M) {}
238
239 /// Map a metadata node (and its transitive operands).
240 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000241 /// Map all the (unmapped) nodes in the subgraph under \c N. The iterative
242 /// algorithm handles distinct nodes and uniqued node subgraphs using
243 /// different strategies.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000244 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000245 /// Distinct nodes are immediately mapped and added to \a DistinctWorklist
246 /// using \a mapDistinctNode(). Their mapping can always be computed
247 /// immediately without visiting operands, even if their operands change.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000248 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000249 /// The mapping for uniqued nodes depends on whether their operands change.
250 /// \a mapTopLevelUniquedNode() traverses the transitive uniqued subgraph of
251 /// a node to calculate uniqued node mappings in bulk. Distinct leafs are
252 /// added to \a DistinctWorklist with \a mapDistinctNode().
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000253 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000254 /// After mapping \c N itself, this function remaps the operands of the
255 /// distinct nodes in \a DistinctWorklist until the entire subgraph under \c
256 /// N has been mapped.
257 Metadata *map(const MDNode &N);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000258
259private:
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000260 /// Map a top-level uniqued node and the uniqued subgraph underneath it.
261 ///
262 /// This builds up a post-order traversal of the (unmapped) uniqued subgraph
263 /// underneath \c FirstN and calculates the nodes' mapping. Each node uses
264 /// the identity mapping (\a Mapper::mapToSelf()) as long as all of its
265 /// operands uses the identity mapping.
266 ///
267 /// The algorithm works as follows:
268 ///
269 /// 1. \a createPOT(): traverse the uniqued subgraph under \c FirstN and
270 /// save the post-order traversal in the given \a UniquedGraph, tracking
271 /// nodes' operands change.
272 ///
273 /// 2. \a UniquedGraph::propagateChanges(): propagate changed operands
274 /// through the \a UniquedGraph until fixed point, following the rule
275 /// that if a node changes, any node that references must also change.
276 ///
277 /// 3. \a mapNodesInPOT(): map the uniqued nodes, creating new uniqued nodes
278 /// (referencing new operands) where necessary.
279 Metadata *mapTopLevelUniquedNode(const MDNode &FirstN);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000280
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000281 /// Try to map the operand of an \a MDNode.
282 ///
283 /// If \c Op is already mapped, return the mapping. If it's not an \a
284 /// MDNode, compute and return the mapping. If it's a distinct \a MDNode,
285 /// return the result of \a mapDistinctNode().
286 ///
287 /// \return None if \c Op is an unmapped uniqued \a MDNode.
288 /// \post getMappedOp(Op) only returns None if this returns None.
289 Optional<Metadata *> tryToMapOperand(const Metadata *Op);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000290
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000291 /// Map a distinct node.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000292 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000293 /// Return the mapping for the distinct node \c N, saving the result in \a
294 /// DistinctWorklist for later remapping.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000295 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000296 /// \pre \c N is not yet mapped.
297 /// \pre \c N.isDistinct().
298 MDNode *mapDistinctNode(const MDNode &N);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000299
300 /// Get a previously mapped node.
301 Optional<Metadata *> getMappedOp(const Metadata *Op) const;
302
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000303 /// Create a post-order traversal of an unmapped uniqued node subgraph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000304 ///
305 /// This traverses the metadata graph deeply enough to map \c FirstN. It
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000306 /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000307 /// metadata that has already been mapped will not be part of the POT.
308 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000309 /// Each node that has a changed operand from outside the graph (e.g., a
310 /// distinct node, an already-mapped uniqued node, or \a ConstantAsMetadata)
311 /// is marked with \a Data::HasChanged.
312 ///
313 /// \return \c true if any nodes in \c G have \a Data::HasChanged.
314 /// \post \c G.POT is a post-order traversal ending with \c FirstN.
315 /// \post \a Data::hasChanged in \c G.Info indicates whether any node needs
316 /// to change because of operands outside the graph.
317 bool createPOT(UniquedGraph &G, const MDNode &FirstN);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000318
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000319 /// Visit the operands of a uniqued node in the POT.
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000320 ///
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000321 /// Visit the operands in the range from \c I to \c E, returning the first
322 /// uniqued node we find that isn't yet in \c G. \c I is always advanced to
323 /// where to continue the loop through the operands.
324 ///
325 /// This sets \c HasChanged if any of the visited operands change.
326 MDNode *visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
327 MDNode::op_iterator E, bool &HasChanged);
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000328
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000329 /// Map all the nodes in the given uniqued graph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000330 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000331 /// This visits all the nodes in \c G in post-order, using the identity
332 /// mapping or creating a new node depending on \a Data::HasChanged.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000333 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000334 /// \pre \a getMappedOp() returns None for nodes in \c G, but not for any of
335 /// their operands outside of \c G.
336 /// \pre \a Data::HasChanged is true for a node in \c G iff any of its
337 /// operands have changed.
338 /// \post \a getMappedOp() returns the mapped node for every node in \c G.
339 void mapNodesInPOT(UniquedGraph &G);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000340
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000341 /// Remap a node's operands using the given functor.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000342 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000343 /// Iterate through the operands of \c N and update them in place using \c
344 /// mapOperand.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000345 ///
346 /// \pre N.isDistinct() or N.isTemporary().
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000347 template <class OperandMapper>
348 void remapOperands(MDNode &N, OperandMapper mapOperand);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000349};
350
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000351} // end namespace
352
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000353Value *Mapper::mapValue(const Value *V) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000354 ValueToValueMapTy::iterator I = getVM().find(V);
355
Chris Lattner43f8d162011-01-08 08:15:20 +0000356 // If the value already exists in the map, use it.
Duncan P. N. Exon Smith3d555ac2016-04-17 18:53:24 +0000357 if (I != getVM().end()) {
358 assert(I->second && "Unexpected null mapping");
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000359 return I->second;
Duncan P. N. Exon Smith3d555ac2016-04-17 18:53:24 +0000360 }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000361
James Molloyf6f121e2013-05-28 15:17:05 +0000362 // If we have a materializer and it can materialize a value, use that.
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000363 if (auto *Materializer = getMaterializer()) {
Mehdi Aminicc8c1072016-05-25 21:03:21 +0000364 if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
365 getVM()[V] = NewV;
366 return NewV;
Rafael Espindola19b52382015-11-27 20:28:19 +0000367 }
James Molloyf6f121e2013-05-28 15:17:05 +0000368 }
369
Dan Gohmanca26f792010-08-26 15:41:53 +0000370 // Global values do not need to be seeded into the VM if they
371 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000372 if (isa<GlobalValue>(V)) {
Duncan P. N. Exon Smithfdccad92016-04-07 01:22:45 +0000373 if (Flags & RF_NullMapMissingGlobalValues)
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000374 return nullptr;
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000375 return getVM()[V] = const_cast<Value *>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000376 }
377
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000378 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
379 // Inline asm may need *type* remapping.
380 FunctionType *NewTy = IA->getFunctionType();
381 if (TypeMapper) {
382 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
383
384 if (NewTy != IA->getFunctionType())
385 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
386 IA->hasSideEffects(), IA->isAlignStack());
387 }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000388
389 return getVM()[V] = const_cast<Value *>(V);
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000390 }
Chris Lattner6aa34b02003-10-06 15:23:43 +0000391
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000392 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
393 const Metadata *MD = MDV->getMetadata();
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000394
395 if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
396 // Look through to grab the local value.
397 if (Value *LV = mapValue(LAM->getValue())) {
398 if (V == LAM->getValue())
399 return const_cast<Value *>(V);
400 return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV));
401 }
402
403 // FIXME: always return nullptr once Verifier::verifyDominatesUse()
404 // ensures metadata operands only reference defined SSA values.
405 return (Flags & RF_IgnoreMissingLocals)
406 ? nullptr
407 : MetadataAsValue::get(V->getContext(),
408 MDTuple::get(V->getContext(), None));
409 }
410
Chris Lattner43f8d162011-01-08 08:15:20 +0000411 // If this is a module-level metadata and we know that nothing at the module
412 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000413 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000414 return getVM()[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000415
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000416 // Map the metadata and turn it into a value.
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000417 auto *MappedMD = mapMetadata(MD);
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000418 if (MD == MappedMD)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000419 return getVM()[V] = const_cast<Value *>(V);
420 return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +0000421 }
422
Chris Lattner43f8d162011-01-08 08:15:20 +0000423 // Okay, this either must be a constant (which may or may not be mappable) or
424 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000425 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000426 if (!C)
427 return nullptr;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000428
429 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
430 return mapBlockAddress(*BA);
431
Mehdi Aminibcc47412016-05-28 17:26:03 +0000432 auto mapValueOrNull = [this](Value *V) {
433 auto Mapped = mapValue(V);
434 assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
435 "Unexpected null mapping for constant operand without "
436 "NullMapMissingGlobalValues flag");
437 return Mapped;
438 };
439
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000440 // Otherwise, we have some other constant to remap. Start by checking to see
441 // if all operands have an identity remapping.
442 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000443 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000444 for (; OpNo != NumOperands; ++OpNo) {
445 Value *Op = C->getOperand(OpNo);
Mehdi Aminibcc47412016-05-28 17:26:03 +0000446 Mapped = mapValueOrNull(Op);
447 if (!Mapped)
448 return nullptr;
Mehdi Amini9ee054ae2016-05-27 00:32:12 +0000449 if (Mapped != Op)
450 break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000451 }
Junmo Park95529872016-05-08 23:22:58 +0000452
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000453 // See if the type mapper wants to remap the type as well.
454 Type *NewTy = C->getType();
455 if (TypeMapper)
456 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000457
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000458 // If the result type and all operands match up, then just insert an identity
459 // mapping.
460 if (OpNo == NumOperands && NewTy == C->getType())
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000461 return getVM()[V] = C;
462
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000463 // Okay, we need to create a new constant. We've already processed some or
464 // all of the operands, set them all up now.
465 SmallVector<Constant*, 8> Ops;
466 Ops.reserve(NumOperands);
467 for (unsigned j = 0; j != OpNo; ++j)
468 Ops.push_back(cast<Constant>(C->getOperand(j)));
Junmo Park95529872016-05-08 23:22:58 +0000469
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000470 // If one of the operands mismatch, push it and the other mapped operands.
471 if (OpNo != NumOperands) {
472 Ops.push_back(cast<Constant>(Mapped));
Junmo Park95529872016-05-08 23:22:58 +0000473
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000474 // Map the rest of the operands that aren't processed yet.
Mehdi Aminibcc47412016-05-28 17:26:03 +0000475 for (++OpNo; OpNo != NumOperands; ++OpNo) {
476 Mapped = mapValueOrNull(C->getOperand(OpNo));
477 if (!Mapped)
478 return nullptr;
479 Ops.push_back(cast<Constant>(Mapped));
480 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000481 }
David Blaikie88208842015-08-21 20:16:51 +0000482 Type *NewSrcTy = nullptr;
483 if (TypeMapper)
484 if (auto *GEPO = dyn_cast<GEPOperator>(C))
485 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
486
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000487 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000488 return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000489 if (isa<ConstantArray>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000490 return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000491 if (isa<ConstantStruct>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000492 return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000493 if (isa<ConstantVector>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000494 return getVM()[V] = ConstantVector::get(Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000495 // If this is a no-operand constant, it must be because the type was remapped.
496 if (isa<UndefValue>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000497 return getVM()[V] = UndefValue::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000498 if (isa<ConstantAggregateZero>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000499 return getVM()[V] = ConstantAggregateZero::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000500 assert(isa<ConstantPointerNull>(C));
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000501 return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000502}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000503
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000504Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
505 Function *F = cast<Function>(mapValue(BA.getFunction()));
506
507 // F may not have materialized its initializer. In that case, create a
508 // dummy basic block for now, and replace it once we've materialized all
509 // the initializers.
510 BasicBlock *BB;
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000511 if (F->empty()) {
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000512 DelayedBBs.push_back(DelayedBasicBlock(BA));
513 BB = DelayedBBs.back().TempBB.get();
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000514 } else {
515 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000516 }
517
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000518 return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000519}
520
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000521Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000522 getVM().MD()[Key].reset(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000523 return Val;
524}
525
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000526Metadata *Mapper::mapToSelf(const Metadata *MD) {
527 return mapToMetadata(MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000528}
529
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000530Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000531 if (!Op)
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000532 return nullptr;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000533
534 if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
Simon Atanasyane12bef72016-04-16 11:49:40 +0000535#ifndef NDEBUG
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000536 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
537 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
538 M.getVM().getMappedMD(Op)) &&
539 "Expected Value to be memoized");
540 else
541 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
542 "Expected result to be memoized");
Simon Atanasyane12bef72016-04-16 11:49:40 +0000543#endif
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000544 return *MappedOp;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000545 }
546
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000547 const MDNode &N = *cast<MDNode>(Op);
548 if (N.isDistinct())
549 return mapDistinctNode(N);
550 return None;
551}
552
553MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
554 assert(N.isDistinct() && "Expected a distinct node");
555 assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
556 DistinctWorklist.push_back(cast<MDNode>(
557 (M.Flags & RF_MoveDistinctMDs)
558 ? M.mapToSelf(&N)
559 : M.mapToMetadata(&N, MDNode::replaceWithDistinct(N.clone()))));
560 return DistinctWorklist.back();
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000561}
562
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000563static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
564 Value *MappedV) {
565 if (CMD.getValue() == MappedV)
566 return const_cast<ConstantAsMetadata *>(&CMD);
567 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
568}
569
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000570Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000571 if (!Op)
572 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000573
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000574 if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000575 return *MappedOp;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000576
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000577 if (isa<MDString>(Op))
578 return const_cast<Metadata *>(Op);
579
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000580 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
581 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
582
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000583 return None;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000584}
585
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000586Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000587 auto Where = Info.find(&Op);
588 assert(Where != Info.end() && "Expected a valid reference");
589
590 auto &OpD = Where->second;
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000591 if (!OpD.HasChanged)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000592 return Op;
593
594 // Lazily construct a temporary node.
595 if (!OpD.Placeholder)
596 OpD.Placeholder = Op.clone();
597
598 return *OpD.Placeholder;
599}
600
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000601template <class OperandMapper>
602void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
603 assert(!N.isUniqued() && "Expected distinct or temporary nodes");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000604 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
605 Metadata *Old = N.getOperand(I);
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000606 Metadata *New = mapOperand(Old);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000607
608 if (Old != New)
609 N.replaceOperandWith(I, New);
610 }
611}
612
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000613namespace {
614/// An entry in the worklist for the post-order traversal.
615struct POTWorklistEntry {
616 MDNode *N; ///< Current node.
617 MDNode::op_iterator Op; ///< Current operand of \c N.
618
619 /// Keep a flag of whether operands have changed in the worklist to avoid
620 /// hitting the map in \a UniquedGraph.
621 bool HasChanged = false;
622
623 POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
624};
625} // end namespace
626
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000627bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
628 assert(G.Info.empty() && "Expected a fresh traversal");
629 assert(FirstN.isUniqued() && "Expected uniqued node in POT");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000630
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000631 // Construct a post-order traversal of the uniqued subgraph under FirstN.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000632 bool AnyChanges = false;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000633 SmallVector<POTWorklistEntry, 16> Worklist;
634 Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000635 (void)G.Info[&FirstN];
636 while (!Worklist.empty()) {
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000637 // Start or continue the traversal through the this node's operands.
638 auto &WE = Worklist.back();
639 if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
640 // Push a new node to traverse first.
641 Worklist.push_back(POTWorklistEntry(*N));
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000642 continue;
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000643 }
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000644
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000645 // Push the node onto the POT.
646 assert(WE.N->isUniqued() && "Expected only uniqued nodes");
647 assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
648 auto &D = G.Info[WE.N];
649 AnyChanges |= D.HasChanged = WE.HasChanged;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000650 D.ID = G.POT.size();
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000651 G.POT.push_back(WE.N);
652
653 // Pop the node off the worklist.
654 Worklist.pop_back();
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000655 }
656 return AnyChanges;
657}
658
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000659MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
660 MDNode::op_iterator E, bool &HasChanged) {
661 while (I != E) {
662 Metadata *Op = *I++; // Increment even on early return.
663 if (Optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
664 // Check if the operand changes.
665 HasChanged |= Op != *MappedOp;
666 continue;
667 }
668
669 // A uniqued metadata node.
670 MDNode &OpN = *cast<MDNode>(Op);
671 assert(OpN.isUniqued() &&
672 "Only uniqued operands cannot be mapped immediately");
673 if (G.Info.insert(std::make_pair(&OpN, Data())).second)
674 return &OpN; // This is a new one. Return it.
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000675 }
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000676 return nullptr;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000677}
678
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000679void MDNodeMapper::UniquedGraph::propagateChanges() {
680 bool AnyChanges;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000681 do {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000682 AnyChanges = false;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000683 for (MDNode *N : POT) {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000684 auto &D = Info[N];
685 if (D.HasChanged)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000686 continue;
687
688 if (!llvm::any_of(N->operands(), [&](const Metadata *Op) {
689 auto Where = Info.find(Op);
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000690 return Where != Info.end() && Where->second.HasChanged;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000691 }))
692 continue;
693
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000694 AnyChanges = D.HasChanged = true;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000695 }
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000696 } while (AnyChanges);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000697}
698
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000699void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000700 // Construct uniqued nodes, building forward references as necessary.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000701 SmallVector<MDNode *, 16> CyclicNodes;
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000702 for (auto *N : G.POT) {
703 auto &D = G.Info[N];
704 if (!D.HasChanged) {
705 // The node hasn't changed.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000706 M.mapToSelf(N);
707 continue;
708 }
709
Duncan P. N. Exon Smith0cb5c342016-04-16 21:09:53 +0000710 // Remember whether this node had a placeholder.
711 bool HadPlaceholder(D.Placeholder);
712
713 // Clone the uniqued node and remap the operands.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000714 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000715 remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
716 if (Optional<Metadata *> MappedOp = getMappedOp(Old))
717 return *MappedOp;
718 assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
719 return &G.getFwdReference(*cast<MDNode>(Old));
720 });
721
Duncan P. N. Exon Smith0cb5c342016-04-16 21:09:53 +0000722 auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
723 M.mapToMetadata(N, NewN);
724
725 // Nodes that were referenced out of order in the POT are involved in a
726 // uniquing cycle.
727 if (HadPlaceholder)
728 CyclicNodes.push_back(NewN);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000729 }
730
731 // Resolve cycles.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000732 for (auto *N : CyclicNodes)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000733 if (!N->isResolved())
734 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000735}
736
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000737Metadata *MDNodeMapper::map(const MDNode &N) {
738 assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000739 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
740 "MDNodeMapper::map assumes module-level changes");
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000741
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000742 // Require resolved nodes whenever metadata might be remapped.
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000743 assert(N.isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000744
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000745 Metadata *MappedN =
746 N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
747 while (!DistinctWorklist.empty())
748 remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
749 if (Optional<Metadata *> MappedOp = tryToMapOperand(Old))
750 return *MappedOp;
751 return mapTopLevelUniquedNode(*cast<MDNode>(Old));
752 });
753 return MappedN;
754}
755
756Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
757 assert(FirstN.isUniqued() && "Expected uniqued node");
758
759 // Create a post-order traversal of uniqued nodes under FirstN.
760 UniquedGraph G;
761 if (!createPOT(G, FirstN)) {
762 // Return early if no nodes have changed.
763 for (const MDNode *N : G.POT)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000764 M.mapToSelf(N);
765 return &const_cast<MDNode &>(FirstN);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000766 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000767
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000768 // Update graph with all nodes that have changed.
769 G.propagateChanges();
770
771 // Map all the nodes in the graph.
772 mapNodesInPOT(G);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000773
774 // Return the original node, remapped.
775 return *getMappedOp(&FirstN);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000776}
777
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000778namespace {
779
780struct MapMetadataDisabler {
781 ValueToValueMapTy &VM;
782
783 MapMetadataDisabler(ValueToValueMapTy &VM) : VM(VM) {
784 VM.disableMapMetadata();
785 }
786 ~MapMetadataDisabler() { VM.enableMapMetadata(); }
787};
788
789} // end namespace
790
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000791Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000792 // If the value already exists in the map, use it.
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000793 if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000794 return *NewMD;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000795
796 if (isa<MDString>(MD))
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000797 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000798
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000799 // This is a module-level metadata. If nothing at the module level is
800 // changing, use an identity mapping.
801 if ((Flags & RF_NoModuleLevelChanges))
Duncan P. N. Exon Smith69341e62016-04-08 18:49:36 +0000802 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000803
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000804 if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000805 // Disallow recursion into metadata mapping through mapValue.
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000806 MapMetadataDisabler MMD(getVM());
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000807
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000808 // Don't memoize ConstantAsMetadata. Instead of lasting until the
809 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
810 // reference is destructed. These aren't super common, so the extra
811 // indirection isn't that expensive.
812 return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000813 }
814
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000815 assert(isa<MDNode>(MD) && "Expected a metadata node");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000816
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000817 return None;
818}
819
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000820Metadata *Mapper::mapLocalAsMetadata(const LocalAsMetadata &LAM) {
821 // Lookup the mapping for the value itself, and return the appropriate
822 // metadata.
823 if (Value *V = mapValue(LAM.getValue())) {
824 if (V == LAM.getValue())
825 return const_cast<LocalAsMetadata *>(&LAM);
826 return ValueAsMetadata::get(V);
827 }
828
829 // FIXME: always return nullptr once Verifier::verifyDominatesUse() ensures
830 // metadata operands only reference defined SSA values.
831 return (Flags & RF_IgnoreMissingLocals)
832 ? nullptr
833 : MDTuple::get(LAM.getContext(), None);
834}
835
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000836Metadata *Mapper::mapMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000837 assert(MD && "Expected valid metadata");
838 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
839
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000840 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
841 return *NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000842
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000843 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000844}
845
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000846void Mapper::flush() {
847 // Flush out the worklist of global values.
848 while (!Worklist.empty()) {
849 WorklistEntry E = Worklist.pop_back_val();
850 CurrentMCID = E.MCID;
851 switch (E.Kind) {
852 case WorklistEntry::MapGlobalInit:
853 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
854 break;
855 case WorklistEntry::MapAppendingVar: {
856 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
857 mapAppendingVariable(*E.Data.AppendingGV.GV,
858 E.Data.AppendingGV.InitPrefix,
859 E.AppendingGVIsOldCtorDtor,
860 makeArrayRef(AppendingInits).slice(PrefixSize));
861 AppendingInits.resize(PrefixSize);
862 break;
863 }
864 case WorklistEntry::MapGlobalAliasee:
865 E.Data.GlobalAliasee.GA->setAliasee(
866 mapConstant(E.Data.GlobalAliasee.Aliasee));
867 break;
868 case WorklistEntry::RemapFunction:
869 remapFunction(*E.Data.RemapF);
870 break;
871 }
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000872 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000873 CurrentMCID = 0;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000874
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000875 // Finish logic for block addresses now that all global values have been
876 // handled.
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000877 while (!DelayedBBs.empty()) {
878 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
879 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
880 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
881 }
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000882}
883
884void Mapper::remapInstruction(Instruction *I) {
Dan Gohmanca26f792010-08-26 15:41:53 +0000885 // Remap operands.
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000886 for (Use &Op : I->operands()) {
887 Value *V = mapValue(Op);
Chris Lattner43f8d162011-01-08 08:15:20 +0000888 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000889 if (V)
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000890 Op = V;
Chris Lattner43f8d162011-01-08 08:15:20 +0000891 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000892 assert((Flags & RF_IgnoreMissingLocals) &&
Chris Lattner43f8d162011-01-08 08:15:20 +0000893 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000894 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000895
Jay Foad61ea0e42011-06-23 09:09:15 +0000896 // Remap phi nodes' incoming blocks.
897 if (PHINode *PN = dyn_cast<PHINode>(I)) {
898 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Duncan P. N. Exon Smithadcebdf2016-04-08 19:17:13 +0000899 Value *V = mapValue(PN->getIncomingBlock(i));
Jay Foad61ea0e42011-06-23 09:09:15 +0000900 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000901 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000902 PN->setIncomingBlock(i, cast<BasicBlock>(V));
903 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000904 assert((Flags & RF_IgnoreMissingLocals) &&
Jay Foad61ea0e42011-06-23 09:09:15 +0000905 "Referenced block not in value map!");
906 }
907 }
908
Devang Patelc0174042011-08-04 20:02:18 +0000909 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000910 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000911 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000912 for (const auto &MI : MDs) {
913 MDNode *Old = MI.second;
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000914 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
Dan Gohmanca26f792010-08-26 15:41:53 +0000915 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000916 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000917 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000918
David Blaikie348de692015-04-23 21:36:23 +0000919 if (!TypeMapper)
920 return;
921
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000922 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000923 if (auto CS = CallSite(I)) {
924 SmallVector<Type *, 3> Tys;
925 FunctionType *FTy = CS.getFunctionType();
926 Tys.reserve(FTy->getNumParams());
927 for (Type *Ty : FTy->params())
928 Tys.push_back(TypeMapper->remapType(Ty));
929 CS.mutateFunctionType(FunctionType::get(
930 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000931 return;
932 }
933 if (auto *AI = dyn_cast<AllocaInst>(I))
934 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000935 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000936 GEP->setSourceElementType(
937 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000938 GEP->setResultElementType(
939 TypeMapper->remapType(GEP->getResultElementType()));
940 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000941 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000942}
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000943
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000944void Mapper::remapFunction(Function &F) {
945 // Remap the operands.
946 for (Use &Op : F.operands())
947 if (Op)
948 Op = mapValue(Op);
949
950 // Remap the metadata attachments.
951 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
952 F.getAllMetadata(MDs);
953 for (const auto &I : MDs)
954 F.setMetadata(I.first, cast_or_null<MDNode>(mapMetadata(I.second)));
955
956 // Remap the argument types.
957 if (TypeMapper)
958 for (Argument &A : F.args())
959 A.mutateType(TypeMapper->remapType(A.getType()));
960
961 // Remap the instructions.
962 for (BasicBlock &BB : F)
963 for (Instruction &I : BB)
964 remapInstruction(&I);
965}
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000966
967void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
968 bool IsOldCtorDtor,
969 ArrayRef<Constant *> NewMembers) {
970 SmallVector<Constant *, 16> Elements;
971 if (InitPrefix) {
972 unsigned NumElements =
973 cast<ArrayType>(InitPrefix->getType())->getNumElements();
974 for (unsigned I = 0; I != NumElements; ++I)
975 Elements.push_back(InitPrefix->getAggregateElement(I));
976 }
977
978 PointerType *VoidPtrTy;
979 Type *EltTy;
980 if (IsOldCtorDtor) {
981 // FIXME: This upgrade is done during linking to support the C API. See
982 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
983 VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
984 auto &ST = *cast<StructType>(NewMembers.front()->getType());
985 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
986 EltTy = StructType::get(GV.getContext(), Tys, false);
987 }
988
989 for (auto *V : NewMembers) {
990 Constant *NewV;
991 if (IsOldCtorDtor) {
992 auto *S = cast<ConstantStruct>(V);
993 auto *E1 = mapValue(S->getOperand(0));
994 auto *E2 = mapValue(S->getOperand(1));
995 Value *Null = Constant::getNullValue(VoidPtrTy);
996 NewV =
997 ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
998 } else {
999 NewV = cast_or_null<Constant>(mapValue(V));
1000 }
1001 Elements.push_back(NewV);
1002 }
1003
1004 GV.setInitializer(ConstantArray::get(
1005 cast<ArrayType>(GV.getType()->getElementType()), Elements));
1006}
1007
1008void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1009 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001010 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001011 assert(MCID < MCs.size() && "Invalid mapping context");
1012
1013 WorklistEntry WE;
1014 WE.Kind = WorklistEntry::MapGlobalInit;
1015 WE.MCID = MCID;
1016 WE.Data.GVInit.GV = &GV;
1017 WE.Data.GVInit.Init = &Init;
1018 Worklist.push_back(WE);
1019}
1020
1021void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1022 Constant *InitPrefix,
1023 bool IsOldCtorDtor,
1024 ArrayRef<Constant *> NewMembers,
1025 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001026 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001027 assert(MCID < MCs.size() && "Invalid mapping context");
1028
1029 WorklistEntry WE;
1030 WE.Kind = WorklistEntry::MapAppendingVar;
1031 WE.MCID = MCID;
1032 WE.Data.AppendingGV.GV = &GV;
1033 WE.Data.AppendingGV.InitPrefix = InitPrefix;
1034 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1035 WE.AppendingGVNumNewMembers = NewMembers.size();
1036 Worklist.push_back(WE);
1037 AppendingInits.append(NewMembers.begin(), NewMembers.end());
1038}
1039
1040void Mapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1041 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001042 assert(AlreadyScheduled.insert(&GA).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001043 assert(MCID < MCs.size() && "Invalid mapping context");
1044
1045 WorklistEntry WE;
1046 WE.Kind = WorklistEntry::MapGlobalAliasee;
1047 WE.MCID = MCID;
1048 WE.Data.GlobalAliasee.GA = &GA;
1049 WE.Data.GlobalAliasee.Aliasee = &Aliasee;
1050 Worklist.push_back(WE);
1051}
1052
1053void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001054 assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001055 assert(MCID < MCs.size() && "Invalid mapping context");
1056
1057 WorklistEntry WE;
1058 WE.Kind = WorklistEntry::RemapFunction;
1059 WE.MCID = MCID;
1060 WE.Data.RemapF = &F;
1061 Worklist.push_back(WE);
1062}
1063
1064void Mapper::addFlags(RemapFlags Flags) {
1065 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1066 this->Flags = this->Flags | Flags;
1067}
1068
1069static Mapper *getAsMapper(void *pImpl) {
1070 return reinterpret_cast<Mapper *>(pImpl);
1071}
1072
1073namespace {
1074
1075class FlushingMapper {
1076 Mapper &M;
1077
1078public:
1079 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1080 assert(!M.hasWorkToDo() && "Expected to be flushed");
1081 }
1082 ~FlushingMapper() { M.flush(); }
1083 Mapper *operator->() const { return &M; }
1084};
1085
1086} // end namespace
1087
1088ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1089 ValueMapTypeRemapper *TypeMapper,
1090 ValueMaterializer *Materializer)
1091 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1092
1093ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1094
1095unsigned
1096ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1097 ValueMaterializer *Materializer) {
1098 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1099}
1100
1101void ValueMapper::addFlags(RemapFlags Flags) {
1102 FlushingMapper(pImpl)->addFlags(Flags);
1103}
1104
1105Value *ValueMapper::mapValue(const Value &V) {
1106 return FlushingMapper(pImpl)->mapValue(&V);
1107}
1108
1109Constant *ValueMapper::mapConstant(const Constant &C) {
1110 return cast_or_null<Constant>(mapValue(C));
1111}
1112
1113Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1114 return FlushingMapper(pImpl)->mapMetadata(&MD);
1115}
1116
1117MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1118 return cast_or_null<MDNode>(mapMetadata(N));
1119}
1120
1121void ValueMapper::remapInstruction(Instruction &I) {
1122 FlushingMapper(pImpl)->remapInstruction(&I);
1123}
1124
1125void ValueMapper::remapFunction(Function &F) {
1126 FlushingMapper(pImpl)->remapFunction(F);
1127}
1128
1129void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1130 Constant &Init,
1131 unsigned MCID) {
1132 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1133}
1134
1135void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1136 Constant *InitPrefix,
1137 bool IsOldCtorDtor,
1138 ArrayRef<Constant *> NewMembers,
1139 unsigned MCID) {
1140 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1141 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1142}
1143
1144void ValueMapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1145 unsigned MCID) {
1146 getAsMapper(pImpl)->scheduleMapGlobalAliasee(GA, Aliasee, MCID);
1147}
1148
1149void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1150 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1151}