blob: fbc3407c301fc529a873bacef4b0076780c6678e [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00008//
9// This file defines the MapValue function, which is shared by various parts of
10// the lib/Transforms/Utils library.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohmana2095032010-08-24 18:50:07 +000014#include "llvm/Transforms/Utils/ValueMapper.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000015#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +000017#include "llvm/ADT/DenseSet.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000018#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"
David Blaikie348de692015-04-23 21:36:23 +000024#include "llvm/IR/CallSite.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000025#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Teresa Johnsondf763182018-01-30 20:16:32 +000027#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000028#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Function.h"
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000030#include "llvm/IR/GlobalAlias.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000031#include "llvm/IR/GlobalObject.h"
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000032#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/InlineAsm.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000034#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Instructions.h"
36#include "llvm/IR/Metadata.h"
David Blaikie88208842015-08-21 20:16:51 +000037#include "llvm/IR/Operator.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000038#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
Chris Lattnerdf3c3422004-01-09 06:12:26 +000046using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000047
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000048// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000049void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000050void ValueMaterializer::anchor() {}
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000051
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +000052namespace {
53
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000054/// A basic block used in a BlockAddress whose function body is not yet
55/// materialized.
56struct DelayedBasicBlock {
57 BasicBlock *OldBB;
58 std::unique_ptr<BasicBlock> TempBB;
Duncan P. N. Exon Smitha9978562016-04-03 20:42:21 +000059
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +000060 DelayedBasicBlock(const BlockAddress &Old)
61 : OldBB(Old.getBasicBlock()),
62 TempBB(BasicBlock::Create(Old.getContext())) {}
63};
64
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000065struct 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
97struct 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 : VM(&VM), Materializer(Materializer) {}
105};
106
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000107class Mapper {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000108 friend class MDNodeMapper;
109
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +0000110#ifndef NDEBUG
111 DenseSet<GlobalValue *> AlreadyScheduled;
112#endif
113
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000114 RemapFlags Flags;
115 ValueMapTypeRemapper *TypeMapper;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000116 unsigned CurrentMCID = 0;
117 SmallVector<MappingContext, 2> MCs;
118 SmallVector<WorklistEntry, 4> Worklist;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000119 SmallVector<DelayedBasicBlock, 1> DelayedBBs;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000120 SmallVector<Constant *, 16> AppendingInits;
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000121
122public:
123 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
124 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000125 : Flags(Flags), TypeMapper(TypeMapper),
126 MCs(1, MappingContext(VM, Materializer)) {}
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000127
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000128 /// ValueMapper should explicitly call \a flush() before destruction.
129 ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
130
131 bool hasWorkToDo() const { return !Worklist.empty(); }
132
133 unsigned
134 registerAlternateMappingContext(ValueToValueMapTy &VM,
135 ValueMaterializer *Materializer = nullptr) {
136 MCs.push_back(MappingContext(VM, Materializer));
137 return MCs.size() - 1;
138 }
139
140 void addFlags(RemapFlags Flags);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000141
Evgeniy Stepanov9aff8292017-05-04 23:29:39 +0000142 void remapGlobalObjectMetadata(GlobalObject &GO);
143
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000144 Value *mapValue(const Value *V);
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000145 void remapInstruction(Instruction *I);
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000146 void remapFunction(Function &F);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000147
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000148 Constant *mapConstant(const Constant *C) {
149 return cast_or_null<Constant>(mapValue(C));
150 }
151
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000152 /// 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
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000158 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
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000170private:
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000171 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 ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
179 ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000180
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000181 Value *mapBlockAddress(const BlockAddress &BA);
182
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000183 /// Map metadata that doesn't require visiting operands.
184 Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
185
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000186 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
187 Metadata *mapToSelf(const Metadata *MD);
188};
189
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000190class MDNodeMapper {
191 Mapper &M;
192
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000193 /// Data about a node in \a UniquedGraph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000194 struct Data {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000195 bool HasChanged = false;
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000196 unsigned ID = std::numeric_limits<unsigned>::max();
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000197 TempMDNode Placeholder;
198 };
199
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000200 /// 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;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000221
222public:
223 MDNodeMapper(Mapper &M) : M(M) {}
224
225 /// Map a metadata node (and its transitive operands).
226 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000227 /// 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.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000230 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000231 /// 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.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000234 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000235 /// 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().
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000239 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000240 /// 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);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000244
245private:
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000246 /// 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);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000266
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000267 /// 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);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000276
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000277 /// Map a distinct node.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000278 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000279 /// Return the mapping for the distinct node \c N, saving the result in \a
280 /// DistinctWorklist for later remapping.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000281 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000282 /// \pre \c N is not yet mapped.
283 /// \pre \c N.isDistinct().
284 MDNode *mapDistinctNode(const MDNode &N);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000285
286 /// Get a previously mapped node.
287 Optional<Metadata *> getMappedOp(const Metadata *Op) const;
288
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000289 /// Create a post-order traversal of an unmapped uniqued node subgraph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000290 ///
291 /// This traverses the metadata graph deeply enough to map \c FirstN. It
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000292 /// uses \a tryToMapOperand() (via \a Mapper::mapSimplifiedNode()), so any
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000293 /// metadata that has already been mapped will not be part of the POT.
294 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000295 /// 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);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000304
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000305 /// Visit the operands of a uniqued node in the POT.
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000306 ///
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000307 /// 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);
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000314
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000315 /// Map all the nodes in the given uniqued graph.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000316 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000317 /// 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.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000319 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000320 /// \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);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000326
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000327 /// Remap a node's operands using the given functor.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000328 ///
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000329 /// Iterate through the operands of \c N and update them in place using \c
330 /// mapOperand.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000331 ///
332 /// \pre N.isDistinct() or N.isTemporary().
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000333 template <class OperandMapper>
334 void remapOperands(MDNode &N, OperandMapper mapOperand);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000335};
336
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000337} // end anonymous namespace
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000338
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000339Value *Mapper::mapValue(const Value *V) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000340 ValueToValueMapTy::iterator I = getVM().find(V);
341
Chris Lattner43f8d162011-01-08 08:15:20 +0000342 // If the value already exists in the map, use it.
Duncan P. N. Exon Smith3d555ac2016-04-17 18:53:24 +0000343 if (I != getVM().end()) {
344 assert(I->second && "Unexpected null mapping");
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000345 return I->second;
Duncan P. N. Exon Smith3d555ac2016-04-17 18:53:24 +0000346 }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000347
James Molloyf6f121e2013-05-28 15:17:05 +0000348 // If we have a materializer and it can materialize a value, use that.
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000349 if (auto *Materializer = getMaterializer()) {
Mehdi Aminicc8c1072016-05-25 21:03:21 +0000350 if (Value *NewV = Materializer->materialize(const_cast<Value *>(V))) {
351 getVM()[V] = NewV;
352 return NewV;
Rafael Espindola19b52382015-11-27 20:28:19 +0000353 }
James Molloyf6f121e2013-05-28 15:17:05 +0000354 }
355
Dan Gohmanca26f792010-08-26 15:41:53 +0000356 // Global values do not need to be seeded into the VM if they
357 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000358 if (isa<GlobalValue>(V)) {
Duncan P. N. Exon Smithfdccad92016-04-07 01:22:45 +0000359 if (Flags & RF_NullMapMissingGlobalValues)
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000360 return nullptr;
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000361 return getVM()[V] = const_cast<Value *>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000362 }
363
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000364 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
365 // Inline asm may need *type* remapping.
366 FunctionType *NewTy = IA->getFunctionType();
367 if (TypeMapper) {
368 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
369
370 if (NewTy != IA->getFunctionType())
371 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
372 IA->hasSideEffects(), IA->isAlignStack());
373 }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000374
375 return getVM()[V] = const_cast<Value *>(V);
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000376 }
Chris Lattner6aa34b02003-10-06 15:23:43 +0000377
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000378 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
379 const Metadata *MD = MDV->getMetadata();
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000380
381 if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
382 // Look through to grab the local value.
383 if (Value *LV = mapValue(LAM->getValue())) {
384 if (V == LAM->getValue())
385 return const_cast<Value *>(V);
386 return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV));
387 }
388
389 // FIXME: always return nullptr once Verifier::verifyDominatesUse()
390 // ensures metadata operands only reference defined SSA values.
391 return (Flags & RF_IgnoreMissingLocals)
392 ? nullptr
393 : MetadataAsValue::get(V->getContext(),
394 MDTuple::get(V->getContext(), None));
395 }
396
Chris Lattner43f8d162011-01-08 08:15:20 +0000397 // If this is a module-level metadata and we know that nothing at the module
398 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000399 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000400 return getVM()[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000401
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000402 // Map the metadata and turn it into a value.
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000403 auto *MappedMD = mapMetadata(MD);
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000404 if (MD == MappedMD)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000405 return getVM()[V] = const_cast<Value *>(V);
406 return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +0000407 }
408
Chris Lattner43f8d162011-01-08 08:15:20 +0000409 // Okay, this either must be a constant (which may or may not be mappable) or
410 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000411 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000412 if (!C)
413 return nullptr;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000414
415 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
416 return mapBlockAddress(*BA);
417
Mehdi Aminibcc47412016-05-28 17:26:03 +0000418 auto mapValueOrNull = [this](Value *V) {
419 auto Mapped = mapValue(V);
420 assert((Mapped || (Flags & RF_NullMapMissingGlobalValues)) &&
421 "Unexpected null mapping for constant operand without "
422 "NullMapMissingGlobalValues flag");
423 return Mapped;
424 };
425
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000426 // Otherwise, we have some other constant to remap. Start by checking to see
427 // if all operands have an identity remapping.
428 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000429 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000430 for (; OpNo != NumOperands; ++OpNo) {
431 Value *Op = C->getOperand(OpNo);
Mehdi Aminibcc47412016-05-28 17:26:03 +0000432 Mapped = mapValueOrNull(Op);
433 if (!Mapped)
434 return nullptr;
Mehdi Amini9ee054ae2016-05-27 00:32:12 +0000435 if (Mapped != Op)
436 break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000437 }
Junmo Park95529872016-05-08 23:22:58 +0000438
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000439 // See if the type mapper wants to remap the type as well.
440 Type *NewTy = C->getType();
441 if (TypeMapper)
442 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000443
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000444 // If the result type and all operands match up, then just insert an identity
445 // mapping.
446 if (OpNo == NumOperands && NewTy == C->getType())
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000447 return getVM()[V] = C;
448
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000449 // Okay, we need to create a new constant. We've already processed some or
450 // all of the operands, set them all up now.
451 SmallVector<Constant*, 8> Ops;
452 Ops.reserve(NumOperands);
453 for (unsigned j = 0; j != OpNo; ++j)
454 Ops.push_back(cast<Constant>(C->getOperand(j)));
Junmo Park95529872016-05-08 23:22:58 +0000455
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000456 // If one of the operands mismatch, push it and the other mapped operands.
457 if (OpNo != NumOperands) {
458 Ops.push_back(cast<Constant>(Mapped));
Junmo Park95529872016-05-08 23:22:58 +0000459
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000460 // Map the rest of the operands that aren't processed yet.
Mehdi Aminibcc47412016-05-28 17:26:03 +0000461 for (++OpNo; OpNo != NumOperands; ++OpNo) {
462 Mapped = mapValueOrNull(C->getOperand(OpNo));
463 if (!Mapped)
464 return nullptr;
465 Ops.push_back(cast<Constant>(Mapped));
466 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000467 }
David Blaikie88208842015-08-21 20:16:51 +0000468 Type *NewSrcTy = nullptr;
469 if (TypeMapper)
470 if (auto *GEPO = dyn_cast<GEPOperator>(C))
471 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
472
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000473 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000474 return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000475 if (isa<ConstantArray>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000476 return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000477 if (isa<ConstantStruct>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000478 return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000479 if (isa<ConstantVector>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000480 return getVM()[V] = ConstantVector::get(Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000481 // If this is a no-operand constant, it must be because the type was remapped.
482 if (isa<UndefValue>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000483 return getVM()[V] = UndefValue::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000484 if (isa<ConstantAggregateZero>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000485 return getVM()[V] = ConstantAggregateZero::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000486 assert(isa<ConstantPointerNull>(C));
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000487 return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000488}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000489
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000490Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
491 Function *F = cast<Function>(mapValue(BA.getFunction()));
492
493 // F may not have materialized its initializer. In that case, create a
494 // dummy basic block for now, and replace it once we've materialized all
495 // the initializers.
496 BasicBlock *BB;
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000497 if (F->empty()) {
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000498 DelayedBBs.push_back(DelayedBasicBlock(BA));
499 BB = DelayedBBs.back().TempBB.get();
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000500 } else {
501 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000502 }
503
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000504 return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000505}
506
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000507Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000508 getVM().MD()[Key].reset(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000509 return Val;
510}
511
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000512Metadata *Mapper::mapToSelf(const Metadata *MD) {
513 return mapToMetadata(MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000514}
515
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000516Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000517 if (!Op)
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000518 return nullptr;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000519
520 if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
Simon Atanasyane12bef72016-04-16 11:49:40 +0000521#ifndef NDEBUG
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000522 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");
Simon Atanasyane12bef72016-04-16 11:49:40 +0000529#endif
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000530 return *MappedOp;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000531 }
532
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000533 const MDNode &N = *cast<MDNode>(Op);
534 if (N.isDistinct())
535 return mapDistinctNode(N);
536 return None;
537}
538
Teresa Johnsondf763182018-01-30 20:16:32 +0000539static Metadata *cloneOrBuildODR(const MDNode &N) {
540 auto *CT = dyn_cast<DICompositeType>(&N);
541 // If ODR type uniquing is enabled, we would have uniqued composite types
542 // with identifiers during bitcode reading, so we can just use CT.
543 if (CT && CT->getContext().isODRUniquingDebugTypes() &&
544 CT->getIdentifier() != "")
545 return const_cast<DICompositeType *>(CT);
546 return MDNode::replaceWithDistinct(N.clone());
547}
548
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000549MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
550 assert(N.isDistinct() && "Expected a distinct node");
551 assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
Teresa Johnsondf763182018-01-30 20:16:32 +0000552 DistinctWorklist.push_back(
553 cast<MDNode>((M.Flags & RF_MoveDistinctMDs)
554 ? M.mapToSelf(&N)
555 : M.mapToMetadata(&N, cloneOrBuildODR(N))));
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000556 return DistinctWorklist.back();
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000557}
558
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000559static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
560 Value *MappedV) {
561 if (CMD.getValue() == MappedV)
562 return const_cast<ConstantAsMetadata *>(&CMD);
563 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
564}
565
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000566Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000567 if (!Op)
568 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000569
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000570 if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000571 return *MappedOp;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000572
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000573 if (isa<MDString>(Op))
574 return const_cast<Metadata *>(Op);
575
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000576 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
577 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
578
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000579 return None;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000580}
581
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000582Metadata &MDNodeMapper::UniquedGraph::getFwdReference(MDNode &Op) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000583 auto Where = Info.find(&Op);
584 assert(Where != Info.end() && "Expected a valid reference");
585
586 auto &OpD = Where->second;
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000587 if (!OpD.HasChanged)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000588 return Op;
589
590 // Lazily construct a temporary node.
591 if (!OpD.Placeholder)
592 OpD.Placeholder = Op.clone();
593
594 return *OpD.Placeholder;
595}
596
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000597template <class OperandMapper>
598void MDNodeMapper::remapOperands(MDNode &N, OperandMapper mapOperand) {
599 assert(!N.isUniqued() && "Expected distinct or temporary nodes");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000600 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
601 Metadata *Old = N.getOperand(I);
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000602 Metadata *New = mapOperand(Old);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000603
604 if (Old != New)
605 N.replaceOperandWith(I, New);
606 }
607}
608
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000609namespace {
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000610
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000611/// An entry in the worklist for the post-order traversal.
612struct 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 POTWorklistEntry(MDNode &N) : N(&N), Op(N.op_begin()) {}
621};
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000622
623} // end anonymous namespace
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000624
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000625bool MDNodeMapper::createPOT(UniquedGraph &G, const MDNode &FirstN) {
626 assert(G.Info.empty() && "Expected a fresh traversal");
627 assert(FirstN.isUniqued() && "Expected uniqued node in POT");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000628
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000629 // Construct a post-order traversal of the uniqued subgraph under FirstN.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000630 bool AnyChanges = false;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000631 SmallVector<POTWorklistEntry, 16> Worklist;
632 Worklist.push_back(POTWorklistEntry(const_cast<MDNode &>(FirstN)));
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000633 (void)G.Info[&FirstN];
634 while (!Worklist.empty()) {
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000635 // Start or continue the traversal through the this node's operands.
636 auto &WE = Worklist.back();
637 if (MDNode *N = visitOperands(G, WE.Op, WE.N->op_end(), WE.HasChanged)) {
638 // Push a new node to traverse first.
639 Worklist.push_back(POTWorklistEntry(*N));
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000640 continue;
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000641 }
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000642
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000643 // Push the node onto the POT.
644 assert(WE.N->isUniqued() && "Expected only uniqued nodes");
645 assert(WE.Op == WE.N->op_end() && "Expected to visit all operands");
646 auto &D = G.Info[WE.N];
647 AnyChanges |= D.HasChanged = WE.HasChanged;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000648 D.ID = G.POT.size();
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000649 G.POT.push_back(WE.N);
650
651 // Pop the node off the worklist.
652 Worklist.pop_back();
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000653 }
654 return AnyChanges;
655}
656
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000657MDNode *MDNodeMapper::visitOperands(UniquedGraph &G, MDNode::op_iterator &I,
658 MDNode::op_iterator E, bool &HasChanged) {
659 while (I != E) {
660 Metadata *Op = *I++; // Increment even on early return.
661 if (Optional<Metadata *> MappedOp = tryToMapOperand(Op)) {
662 // Check if the operand changes.
663 HasChanged |= Op != *MappedOp;
664 continue;
665 }
666
667 // A uniqued metadata node.
668 MDNode &OpN = *cast<MDNode>(Op);
669 assert(OpN.isUniqued() &&
670 "Only uniqued operands cannot be mapped immediately");
671 if (G.Info.insert(std::make_pair(&OpN, Data())).second)
672 return &OpN; // This is a new one. Return it.
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000673 }
Duncan P. N. Exon Smith71480bd2016-04-22 02:33:06 +0000674 return nullptr;
Duncan P. N. Exon Smith0ab44db2016-04-21 02:34:36 +0000675}
676
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000677void MDNodeMapper::UniquedGraph::propagateChanges() {
678 bool AnyChanges;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000679 do {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000680 AnyChanges = false;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000681 for (MDNode *N : POT) {
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000682 auto &D = Info[N];
683 if (D.HasChanged)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000684 continue;
685
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000686 if (llvm::none_of(N->operands(), [&](const Metadata *Op) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000687 auto Where = Info.find(Op);
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000688 return Where != Info.end() && Where->second.HasChanged;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000689 }))
690 continue;
691
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000692 AnyChanges = D.HasChanged = true;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000693 }
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000694 } while (AnyChanges);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000695}
696
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000697void MDNodeMapper::mapNodesInPOT(UniquedGraph &G) {
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000698 // Construct uniqued nodes, building forward references as necessary.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000699 SmallVector<MDNode *, 16> CyclicNodes;
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000700 for (auto *N : G.POT) {
701 auto &D = G.Info[N];
702 if (!D.HasChanged) {
703 // The node hasn't changed.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000704 M.mapToSelf(N);
705 continue;
706 }
707
Duncan P. N. Exon Smith0cb5c342016-04-16 21:09:53 +0000708 // Remember whether this node had a placeholder.
709 bool HadPlaceholder(D.Placeholder);
710
711 // Clone the uniqued node and remap the operands.
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000712 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000713 remapOperands(*ClonedN, [this, &D, &G](Metadata *Old) {
714 if (Optional<Metadata *> MappedOp = getMappedOp(Old))
715 return *MappedOp;
David L. Jones41cecba2017-01-13 21:02:41 +0000716 (void)D;
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000717 assert(G.Info[Old].ID > D.ID && "Expected a forward reference");
718 return &G.getFwdReference(*cast<MDNode>(Old));
719 });
720
Duncan P. N. Exon Smith0cb5c342016-04-16 21:09:53 +0000721 auto *NewN = MDNode::replaceWithUniqued(std::move(ClonedN));
722 M.mapToMetadata(N, NewN);
723
724 // Nodes that were referenced out of order in the POT are involved in a
725 // uniquing cycle.
726 if (HadPlaceholder)
727 CyclicNodes.push_back(NewN);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000728 }
729
730 // Resolve cycles.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000731 for (auto *N : CyclicNodes)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000732 if (!N->isResolved())
733 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000734}
735
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000736Metadata *MDNodeMapper::map(const MDNode &N) {
737 assert(DistinctWorklist.empty() && "MDNodeMapper::map is not recursive");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000738 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
739 "MDNodeMapper::map assumes module-level changes");
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000740
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000741 // Require resolved nodes whenever metadata might be remapped.
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000742 assert(N.isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000743
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000744 Metadata *MappedN =
745 N.isUniqued() ? mapTopLevelUniquedNode(N) : mapDistinctNode(N);
746 while (!DistinctWorklist.empty())
747 remapOperands(*DistinctWorklist.pop_back_val(), [this](Metadata *Old) {
748 if (Optional<Metadata *> MappedOp = tryToMapOperand(Old))
749 return *MappedOp;
750 return mapTopLevelUniquedNode(*cast<MDNode>(Old));
751 });
752 return MappedN;
753}
754
755Metadata *MDNodeMapper::mapTopLevelUniquedNode(const MDNode &FirstN) {
756 assert(FirstN.isUniqued() && "Expected uniqued node");
757
758 // Create a post-order traversal of uniqued nodes under FirstN.
759 UniquedGraph G;
760 if (!createPOT(G, FirstN)) {
761 // Return early if no nodes have changed.
762 for (const MDNode *N : G.POT)
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000763 M.mapToSelf(N);
764 return &const_cast<MDNode &>(FirstN);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000765 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000766
Duncan P. N. Exon Smith694ab4e2016-04-16 21:44:08 +0000767 // Update graph with all nodes that have changed.
768 G.propagateChanges();
769
770 // Map all the nodes in the graph.
771 mapNodesInPOT(G);
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000772
773 // Return the original node, remapped.
774 return *getMappedOp(&FirstN);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000775}
776
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000777namespace {
778
779struct MapMetadataDisabler {
780 ValueToValueMapTy &VM;
781
782 MapMetadataDisabler(ValueToValueMapTy &VM) : VM(VM) {
783 VM.disableMapMetadata();
784 }
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000785
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000786 ~MapMetadataDisabler() { VM.enableMapMetadata(); }
787};
788
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000789} // end anonymous namespace
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000790
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 Smith829dc872016-04-03 19:06:24 +0000820Metadata *Mapper::mapMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000821 assert(MD && "Expected valid metadata");
822 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
823
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000824 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
825 return *NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000826
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000827 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000828}
829
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000830void Mapper::flush() {
831 // Flush out the worklist of global values.
832 while (!Worklist.empty()) {
833 WorklistEntry E = Worklist.pop_back_val();
834 CurrentMCID = E.MCID;
835 switch (E.Kind) {
836 case WorklistEntry::MapGlobalInit:
837 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
Evgeniy Stepanov9aff8292017-05-04 23:29:39 +0000838 remapGlobalObjectMetadata(*E.Data.GVInit.GV);
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000839 break;
840 case WorklistEntry::MapAppendingVar: {
841 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
842 mapAppendingVariable(*E.Data.AppendingGV.GV,
843 E.Data.AppendingGV.InitPrefix,
844 E.AppendingGVIsOldCtorDtor,
845 makeArrayRef(AppendingInits).slice(PrefixSize));
846 AppendingInits.resize(PrefixSize);
847 break;
848 }
849 case WorklistEntry::MapGlobalAliasee:
850 E.Data.GlobalAliasee.GA->setAliasee(
851 mapConstant(E.Data.GlobalAliasee.Aliasee));
852 break;
853 case WorklistEntry::RemapFunction:
854 remapFunction(*E.Data.RemapF);
855 break;
856 }
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000857 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000858 CurrentMCID = 0;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000859
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000860 // Finish logic for block addresses now that all global values have been
861 // handled.
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000862 while (!DelayedBBs.empty()) {
863 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
864 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
865 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
866 }
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000867}
868
869void Mapper::remapInstruction(Instruction *I) {
Dan Gohmanca26f792010-08-26 15:41:53 +0000870 // Remap operands.
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000871 for (Use &Op : I->operands()) {
872 Value *V = mapValue(Op);
Chris Lattner43f8d162011-01-08 08:15:20 +0000873 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000874 if (V)
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000875 Op = V;
Chris Lattner43f8d162011-01-08 08:15:20 +0000876 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000877 assert((Flags & RF_IgnoreMissingLocals) &&
Chris Lattner43f8d162011-01-08 08:15:20 +0000878 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000879 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000880
Jay Foad61ea0e42011-06-23 09:09:15 +0000881 // Remap phi nodes' incoming blocks.
882 if (PHINode *PN = dyn_cast<PHINode>(I)) {
883 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Duncan P. N. Exon Smithadcebdf2016-04-08 19:17:13 +0000884 Value *V = mapValue(PN->getIncomingBlock(i));
Jay Foad61ea0e42011-06-23 09:09:15 +0000885 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000886 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000887 PN->setIncomingBlock(i, cast<BasicBlock>(V));
888 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000889 assert((Flags & RF_IgnoreMissingLocals) &&
Jay Foad61ea0e42011-06-23 09:09:15 +0000890 "Referenced block not in value map!");
891 }
892 }
893
Devang Patelc0174042011-08-04 20:02:18 +0000894 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000895 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000896 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000897 for (const auto &MI : MDs) {
898 MDNode *Old = MI.second;
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000899 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
Dan Gohmanca26f792010-08-26 15:41:53 +0000900 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000901 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000902 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000903
David Blaikie348de692015-04-23 21:36:23 +0000904 if (!TypeMapper)
905 return;
906
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000907 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000908 if (auto CS = CallSite(I)) {
909 SmallVector<Type *, 3> Tys;
910 FunctionType *FTy = CS.getFunctionType();
911 Tys.reserve(FTy->getNumParams());
912 for (Type *Ty : FTy->params())
913 Tys.push_back(TypeMapper->remapType(Ty));
914 CS.mutateFunctionType(FunctionType::get(
915 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
Tim Northoverb7141202019-05-30 18:48:23 +0000916
917 LLVMContext &C = CS->getContext();
918 AttributeList Attrs = CS.getAttributes();
919 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
920 if (Attrs.hasAttribute(i, Attribute::ByVal)) {
921 Type *Ty = Attrs.getAttribute(i, Attribute::ByVal).getValueAsType();
922 if (!Ty)
923 continue;
924
925 Attrs = Attrs.removeAttribute(C, i, Attribute::ByVal);
926 Attrs = Attrs.addAttribute(
927 C, i, Attribute::getWithByValType(C, TypeMapper->remapType(Ty)));
928 }
929 }
930 CS.setAttributes(Attrs);
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
Evgeniy Stepanov9aff8292017-05-04 23:29:39 +0000944void Mapper::remapGlobalObjectMetadata(GlobalObject &GO) {
945 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
946 GO.getAllMetadata(MDs);
947 GO.clearMetadata();
948 for (const auto &I : MDs)
949 GO.addMetadata(I.first, *cast<MDNode>(mapMetadata(I.second)));
950}
951
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000952void Mapper::remapFunction(Function &F) {
953 // Remap the operands.
954 for (Use &Op : F.operands())
955 if (Op)
956 Op = mapValue(Op);
957
958 // Remap the metadata attachments.
Evgeniy Stepanov9aff8292017-05-04 23:29:39 +0000959 remapGlobalObjectMetadata(F);
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000960
961 // Remap the argument types.
962 if (TypeMapper)
963 for (Argument &A : F.args())
964 A.mutateType(TypeMapper->remapType(A.getType()));
965
966 // Remap the instructions.
967 for (BasicBlock &BB : F)
968 for (Instruction &I : BB)
969 remapInstruction(&I);
970}
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000971
972void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
973 bool IsOldCtorDtor,
974 ArrayRef<Constant *> NewMembers) {
975 SmallVector<Constant *, 16> Elements;
976 if (InitPrefix) {
977 unsigned NumElements =
978 cast<ArrayType>(InitPrefix->getType())->getNumElements();
979 for (unsigned I = 0; I != NumElements; ++I)
980 Elements.push_back(InitPrefix->getAggregateElement(I));
981 }
982
983 PointerType *VoidPtrTy;
984 Type *EltTy;
985 if (IsOldCtorDtor) {
986 // FIXME: This upgrade is done during linking to support the C API. See
987 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
988 VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
989 auto &ST = *cast<StructType>(NewMembers.front()->getType());
990 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
991 EltTy = StructType::get(GV.getContext(), Tys, false);
992 }
993
994 for (auto *V : NewMembers) {
995 Constant *NewV;
996 if (IsOldCtorDtor) {
997 auto *S = cast<ConstantStruct>(V);
Serge Gueltone38003f2017-05-09 19:31:13 +0000998 auto *E1 = cast<Constant>(mapValue(S->getOperand(0)));
999 auto *E2 = cast<Constant>(mapValue(S->getOperand(1)));
1000 Constant *Null = Constant::getNullValue(VoidPtrTy);
1001 NewV = ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null);
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001002 } else {
1003 NewV = cast_or_null<Constant>(mapValue(V));
1004 }
1005 Elements.push_back(NewV);
1006 }
1007
1008 GV.setInitializer(ConstantArray::get(
1009 cast<ArrayType>(GV.getType()->getElementType()), Elements));
1010}
1011
1012void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
1013 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001014 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001015 assert(MCID < MCs.size() && "Invalid mapping context");
1016
1017 WorklistEntry WE;
1018 WE.Kind = WorklistEntry::MapGlobalInit;
1019 WE.MCID = MCID;
1020 WE.Data.GVInit.GV = &GV;
1021 WE.Data.GVInit.Init = &Init;
1022 Worklist.push_back(WE);
1023}
1024
1025void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1026 Constant *InitPrefix,
1027 bool IsOldCtorDtor,
1028 ArrayRef<Constant *> NewMembers,
1029 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001030 assert(AlreadyScheduled.insert(&GV).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001031 assert(MCID < MCs.size() && "Invalid mapping context");
1032
1033 WorklistEntry WE;
1034 WE.Kind = WorklistEntry::MapAppendingVar;
1035 WE.MCID = MCID;
1036 WE.Data.AppendingGV.GV = &GV;
1037 WE.Data.AppendingGV.InitPrefix = InitPrefix;
1038 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
1039 WE.AppendingGVNumNewMembers = NewMembers.size();
1040 Worklist.push_back(WE);
1041 AppendingInits.append(NewMembers.begin(), NewMembers.end());
1042}
1043
1044void Mapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1045 unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001046 assert(AlreadyScheduled.insert(&GA).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001047 assert(MCID < MCs.size() && "Invalid mapping context");
1048
1049 WorklistEntry WE;
1050 WE.Kind = WorklistEntry::MapGlobalAliasee;
1051 WE.MCID = MCID;
1052 WE.Data.GlobalAliasee.GA = &GA;
1053 WE.Data.GlobalAliasee.Aliasee = &Aliasee;
1054 Worklist.push_back(WE);
1055}
1056
1057void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
Duncan P. N. Exon Smith0fdaf8c2016-04-17 19:40:20 +00001058 assert(AlreadyScheduled.insert(&F).second && "Should not reschedule");
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001059 assert(MCID < MCs.size() && "Invalid mapping context");
1060
1061 WorklistEntry WE;
1062 WE.Kind = WorklistEntry::RemapFunction;
1063 WE.MCID = MCID;
1064 WE.Data.RemapF = &F;
1065 Worklist.push_back(WE);
1066}
1067
1068void Mapper::addFlags(RemapFlags Flags) {
1069 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1070 this->Flags = this->Flags | Flags;
1071}
1072
1073static Mapper *getAsMapper(void *pImpl) {
1074 return reinterpret_cast<Mapper *>(pImpl);
1075}
1076
1077namespace {
1078
1079class FlushingMapper {
1080 Mapper &M;
1081
1082public:
1083 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1084 assert(!M.hasWorkToDo() && "Expected to be flushed");
1085 }
Eugene Zelenko5adb96c2017-10-26 00:55:39 +00001086
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001087 ~FlushingMapper() { M.flush(); }
Eugene Zelenko5adb96c2017-10-26 00:55:39 +00001088
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001089 Mapper *operator->() const { return &M; }
1090};
1091
Eugene Zelenko5adb96c2017-10-26 00:55:39 +00001092} // end anonymous namespace
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +00001093
1094ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1095 ValueMapTypeRemapper *TypeMapper,
1096 ValueMaterializer *Materializer)
1097 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1098
1099ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1100
1101unsigned
1102ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1103 ValueMaterializer *Materializer) {
1104 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1105}
1106
1107void ValueMapper::addFlags(RemapFlags Flags) {
1108 FlushingMapper(pImpl)->addFlags(Flags);
1109}
1110
1111Value *ValueMapper::mapValue(const Value &V) {
1112 return FlushingMapper(pImpl)->mapValue(&V);
1113}
1114
1115Constant *ValueMapper::mapConstant(const Constant &C) {
1116 return cast_or_null<Constant>(mapValue(C));
1117}
1118
1119Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1120 return FlushingMapper(pImpl)->mapMetadata(&MD);
1121}
1122
1123MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1124 return cast_or_null<MDNode>(mapMetadata(N));
1125}
1126
1127void ValueMapper::remapInstruction(Instruction &I) {
1128 FlushingMapper(pImpl)->remapInstruction(&I);
1129}
1130
1131void ValueMapper::remapFunction(Function &F) {
1132 FlushingMapper(pImpl)->remapFunction(F);
1133}
1134
1135void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1136 Constant &Init,
1137 unsigned MCID) {
1138 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1139}
1140
1141void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1142 Constant *InitPrefix,
1143 bool IsOldCtorDtor,
1144 ArrayRef<Constant *> NewMembers,
1145 unsigned MCID) {
1146 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1147 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1148}
1149
1150void ValueMapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1151 unsigned MCID) {
1152 getAsMapper(pImpl)->scheduleMapGlobalAliasee(GA, Aliasee, MCID);
1153}
1154
1155void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1156 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1157}