blob: db984eef59c476814cb7c2284fbeed2357222524 [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"
David Blaikie348de692015-04-23 21:36:23 +000016#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/Function.h"
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +000019#include "llvm/IR/GlobalAlias.h"
20#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/InlineAsm.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Metadata.h"
David Blaikie88208842015-08-21 20:16:51 +000024#include "llvm/IR/Operator.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000025using namespace llvm;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000026
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000027// Out of line method to get vtable etc for class.
Craig Topper2a6a08b2012-09-26 06:36:36 +000028void ValueMapTypeRemapper::anchor() {}
James Molloyf6f121e2013-05-28 15:17:05 +000029void ValueMaterializer::anchor() {}
Rafael Espindola19b52382015-11-27 20:28:19 +000030void ValueMaterializer::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
31}
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 Smith829dc872016-04-03 19:06:24 +0000101 RemapFlags Flags;
102 ValueMapTypeRemapper *TypeMapper;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000103 unsigned CurrentMCID = 0;
104 SmallVector<MappingContext, 2> MCs;
105 SmallVector<WorklistEntry, 4> Worklist;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000106 SmallVector<DelayedBasicBlock, 1> DelayedBBs;
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000107 SmallVector<Constant *, 16> AppendingInits;
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000108
109public:
110 Mapper(ValueToValueMapTy &VM, RemapFlags Flags,
111 ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer)
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000112 : Flags(Flags), TypeMapper(TypeMapper),
113 MCs(1, MappingContext(VM, Materializer)) {}
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000114
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000115 /// ValueMapper should explicitly call \a flush() before destruction.
116 ~Mapper() { assert(!hasWorkToDo() && "Expected to be flushed"); }
117
118 bool hasWorkToDo() const { return !Worklist.empty(); }
119
120 unsigned
121 registerAlternateMappingContext(ValueToValueMapTy &VM,
122 ValueMaterializer *Materializer = nullptr) {
123 MCs.push_back(MappingContext(VM, Materializer));
124 return MCs.size() - 1;
125 }
126
127 void addFlags(RemapFlags Flags);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000128
129 Value *mapValue(const Value *V);
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000130 void remapInstruction(Instruction *I);
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000131 void remapFunction(Function &F);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000132
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000133 Constant *mapConstant(const Constant *C) {
134 return cast_or_null<Constant>(mapValue(C));
135 }
136
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000137 /// Map metadata.
138 ///
139 /// Find the mapping for MD. Guarantees that the return will be resolved
140 /// (not an MDNode, or MDNode::isResolved() returns true).
141 Metadata *mapMetadata(const Metadata *MD);
142
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000143 // Map LocalAsMetadata, which never gets memoized.
144 //
145 // If the referenced local is not mapped, the principled return is nullptr.
146 // However, optimization passes sometimes move metadata operands *before* the
147 // SSA values they reference. To prevent crashes in \a RemapInstruction(),
148 // return "!{}" when RF_IgnoreMissingLocals is not set.
149 //
150 // \note Adding a mapping for LocalAsMetadata is unsupported. Add a mapping
151 // to the value map for the SSA value in question instead.
152 //
153 // FIXME: Once we have a verifier check for forward references to SSA values
154 // through metadata operands, always return nullptr on unmapped locals.
155 Metadata *mapLocalAsMetadata(const LocalAsMetadata &LAM);
156
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000157 void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
158 unsigned MCID);
159 void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
160 bool IsOldCtorDtor,
161 ArrayRef<Constant *> NewMembers,
162 unsigned MCID);
163 void scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
164 unsigned MCID);
165 void scheduleRemapFunction(Function &F, unsigned MCID);
166
167 void flush();
168
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000169private:
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000170 void mapGlobalInitializer(GlobalVariable &GV, Constant &Init);
171 void mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
172 bool IsOldCtorDtor,
173 ArrayRef<Constant *> NewMembers);
174 void mapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee);
175 void remapFunction(Function &F, ValueToValueMapTy &VM);
176
177 ValueToValueMapTy &getVM() { return *MCs[CurrentMCID].VM; }
178 ValueMaterializer *getMaterializer() { return MCs[CurrentMCID].Materializer; }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000179
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000180 Value *mapBlockAddress(const BlockAddress &BA);
181
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000182 /// Map metadata that doesn't require visiting operands.
183 Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
184
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000185 Metadata *mapToMetadata(const Metadata *Key, Metadata *Val);
186 Metadata *mapToSelf(const Metadata *MD);
187};
188
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000189class MDNodeMapper {
190 Mapper &M;
191
192 struct Data {
193 bool HasChangedOps = false;
194 bool HasChangedAddress = false;
195 unsigned ID = ~0u;
196 TempMDNode Placeholder;
Duncan P. N. Exon Smithf880d352016-04-05 21:07:01 +0000197
Duncan P. N. Exon Smith818e5f32016-04-05 21:25:33 +0000198 Data() {}
199 Data(Data &&X)
200 : HasChangedOps(std::move(X.HasChangedOps)),
201 HasChangedAddress(std::move(X.HasChangedAddress)),
202 ID(std::move(X.ID)), Placeholder(std::move(X.Placeholder)) {}
203 Data &operator=(Data &&X) {
204 HasChangedOps = std::move(X.HasChangedOps);
205 HasChangedAddress = std::move(X.HasChangedAddress);
206 ID = std::move(X.ID);
207 Placeholder = std::move(X.Placeholder);
208 return *this;
209 }
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000210 };
211
212 SmallDenseMap<const Metadata *, Data, 32> Info;
213 SmallVector<std::pair<MDNode *, bool>, 16> Worklist;
214 SmallVector<MDNode *, 16> POT;
215
216public:
217 MDNodeMapper(Mapper &M) : M(M) {}
218
219 /// Map a metadata node (and its transitive operands).
220 ///
221 /// This is the only entry point into MDNodeMapper. It works as follows:
222 ///
223 /// 1. \a createPOT(): use a worklist to perform a post-order traversal of
224 /// the transitively referenced unmapped nodes.
225 ///
226 /// 2. \a propagateChangedOperands(): track which nodes will change
227 /// operands, and which will have new addresses in the mapped scheme.
228 /// Propagate the changes through the POT until fixed point, to pick up
229 /// uniquing cycles that need to change.
230 ///
231 /// 3. \a mapDistinctNodes(): map all the distinct nodes without touching
232 /// their operands. If RF_MoveDistinctMetadata, they get mapped to
233 /// themselves; otherwise, they get mapped to clones.
234 ///
235 /// 4. \a mapUniquedNodes(): map the uniqued nodes (bottom-up), lazily
236 /// creating temporaries for forward references as needed.
237 ///
238 /// 5. \a remapDistinctOperands(): remap the operands of the distinct nodes.
239 Metadata *map(const MDNode &FirstN);
240
241private:
242 /// Return \c true as long as there's work to do.
243 bool hasWork() const { return !Worklist.empty(); }
244
245 /// Get the current node in the worklist.
246 MDNode &getCurrentNode() const { return *Worklist.back().first; }
247
248 /// Push a node onto the worklist.
249 ///
250 /// Adds \c N to \a Worklist and \a Info, unless it's already inserted. If
251 /// \c N.isDistinct(), \a Data::HasChangedAddress will be set based on \a
252 /// RF_MoveDistinctMDs.
253 ///
254 /// Returns the data for the node.
255 ///
256 /// \post Data::HasChangedAddress iff !RF_MoveDistinctMDs && N.isDistinct().
257 /// \post Worklist.back().first == &N.
258 /// \post Worklist.back().second == false.
259 Data &push(const MDNode &N);
260
261 /// Map a node operand, and return true if it changes.
262 ///
263 /// \post getMappedOp(Op) does not return None.
264 bool mapOperand(const Metadata *Op);
265
266 /// Get a previously mapped node.
267 Optional<Metadata *> getMappedOp(const Metadata *Op) const;
268
269 /// Try to pop a node off the worklist and store it in POT.
270 ///
271 /// Returns \c true if it popped; \c false if its operands need to be
272 /// visited.
273 ///
274 /// \post If Worklist.back().second == false: Worklist.back().second == true.
275 /// \post Else: Worklist.back() has been popped off and added to \a POT.
276 bool tryToPop();
277
278 /// Get a forward reference to a node to use as an operand.
279 ///
280 /// Returns \c Op if it's not changing; otherwise, lazily creates a temporary
281 /// node and returns it.
282 Metadata &getFwdReference(const Data &D, MDNode &Op);
283
284 /// Create a post-order traversal from the given node.
285 ///
286 /// This traverses the metadata graph deeply enough to map \c FirstN. It
287 /// uses \a mapOperand() (indirectly, \a Mapper::mapSimplifiedNode()), so any
288 /// metadata that has already been mapped will not be part of the POT.
289 ///
290 /// \post \a POT is a post-order traversal ending with \c FirstN.
291 bool createPOT(const MDNode &FirstN);
292
293 /// Propagate changed operands through post-order traversal.
294 ///
295 /// Until fixed point, iteratively update:
296 ///
297 /// - \a Data::HasChangedOps based on \a Data::HasChangedAddress of operands;
298 /// - \a Data::HasChangedAddress based on Data::HasChangedOps.
299 ///
300 /// This algorithm never changes \a Data::HasChangedAddress for distinct
301 /// nodes.
302 ///
303 /// \post \a POT is a post-order traversal ending with \c FirstN.
304 void propagateChangedOperands();
305
306 /// Map all distinct nodes in POT.
307 ///
308 /// \post \a getMappedOp() returns the correct node for every distinct node.
309 void mapDistinctNodes();
310
311 /// Map all uniqued nodes in POT with the correct operands.
312 ///
313 /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called).
314 /// \post \a getMappedOp() returns the correct node for every node.
315 /// \post \a MDNode::operands() is correct for every uniqued node.
316 /// \post \a MDNode::isResolved() returns true for every node.
317 void mapUniquedNodes();
318
319 /// Re-map the operands for distinct nodes in POT.
320 ///
321 /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called).
322 /// \pre Uniqued nodes are mapped (\a mapUniquedNodes() has been called).
323 /// \post \a MDNode::operands() is correct for every distinct node.
324 void remapDistinctOperands();
325
326 /// Remap a node's operands.
327 ///
328 /// Iterate through operands and update them in place using \a getMappedOp()
329 /// and \a getFwdReference().
330 ///
331 /// \pre N.isDistinct() or N.isTemporary().
332 /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called).
333 /// \pre If \c N is distinct, all uniqued nodes are already mapped.
334 void remapOperands(const Data &D, MDNode &N);
335};
336
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000337} // end namespace
338
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 Smithdb6861e2016-04-15 23:18:43 +0000343 if (I != getVM().end() && I->second)
344 return I->second;
345
James Molloyf6f121e2013-05-28 15:17:05 +0000346 // If we have a materializer and it can materialize a value, use that.
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000347 if (auto *Materializer = getMaterializer()) {
Rafael Espindola19b52382015-11-27 20:28:19 +0000348 if (Value *NewV =
349 Materializer->materializeDeclFor(const_cast<Value *>(V))) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000350 getVM()[V] = NewV;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000351 if (auto *NewGV = dyn_cast<GlobalValue>(NewV))
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000352 Materializer->materializeInitFor(
353 NewGV, cast<GlobalValue>(const_cast<Value *>(V)));
Rafael Espindola19b52382015-11-27 20:28:19 +0000354 return NewV;
355 }
James Molloyf6f121e2013-05-28 15:17:05 +0000356 }
357
Dan Gohmanca26f792010-08-26 15:41:53 +0000358 // Global values do not need to be seeded into the VM if they
359 // are using the identity mapping.
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000360 if (isa<GlobalValue>(V)) {
Duncan P. N. Exon Smithfdccad92016-04-07 01:22:45 +0000361 if (Flags & RF_NullMapMissingGlobalValues)
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000362 return nullptr;
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000363 return getVM()[V] = const_cast<Value *>(V);
Teresa Johnson83d03dd2015-11-15 14:50:14 +0000364 }
365
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000366 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
367 // Inline asm may need *type* remapping.
368 FunctionType *NewTy = IA->getFunctionType();
369 if (TypeMapper) {
370 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
371
372 if (NewTy != IA->getFunctionType())
373 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
374 IA->hasSideEffects(), IA->isAlignStack());
375 }
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000376
377 return getVM()[V] = const_cast<Value *>(V);
Chris Lattner8b4cf5e2011-07-15 23:18:40 +0000378 }
Chris Lattner6aa34b02003-10-06 15:23:43 +0000379
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000380 if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
381 const Metadata *MD = MDV->getMetadata();
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000382
383 if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) {
384 // Look through to grab the local value.
385 if (Value *LV = mapValue(LAM->getValue())) {
386 if (V == LAM->getValue())
387 return const_cast<Value *>(V);
388 return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV));
389 }
390
391 // FIXME: always return nullptr once Verifier::verifyDominatesUse()
392 // ensures metadata operands only reference defined SSA values.
393 return (Flags & RF_IgnoreMissingLocals)
394 ? nullptr
395 : MetadataAsValue::get(V->getContext(),
396 MDTuple::get(V->getContext(), None));
397 }
398
Chris Lattner43f8d162011-01-08 08:15:20 +0000399 // If this is a module-level metadata and we know that nothing at the module
400 // level is changing, then use an identity mapping.
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000401 if (Flags & RF_NoModuleLevelChanges)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000402 return getVM()[V] = const_cast<Value *>(V);
Dan Gohmanca26f792010-08-26 15:41:53 +0000403
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000404 // Map the metadata and turn it into a value.
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000405 auto *MappedMD = mapMetadata(MD);
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000406 if (MD == MappedMD)
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000407 return getVM()[V] = const_cast<Value *>(V);
408 return getVM()[V] = MetadataAsValue::get(V->getContext(), MappedMD);
Victor Hernandez5fa88d42010-01-20 05:49:59 +0000409 }
410
Chris Lattner43f8d162011-01-08 08:15:20 +0000411 // Okay, this either must be a constant (which may or may not be mappable) or
412 // is something that is not in the mapping table.
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000413 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Craig Topperf40110f2014-04-25 05:29:35 +0000414 if (!C)
415 return nullptr;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000416
417 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
418 return mapBlockAddress(*BA);
419
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000420 // Otherwise, we have some other constant to remap. Start by checking to see
421 // if all operands have an identity remapping.
422 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Craig Topperf40110f2014-04-25 05:29:35 +0000423 Value *Mapped = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000424 for (; OpNo != NumOperands; ++OpNo) {
425 Value *Op = C->getOperand(OpNo);
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000426 Mapped = mapValue(Op);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000427 if (Mapped != C) break;
Chris Lattnercf5a47d2009-10-29 00:28:30 +0000428 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000429
430 // See if the type mapper wants to remap the type as well.
431 Type *NewTy = C->getType();
432 if (TypeMapper)
433 NewTy = TypeMapper->remapType(NewTy);
Chris Lattner43f8d162011-01-08 08:15:20 +0000434
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000435 // If the result type and all operands match up, then just insert an identity
436 // mapping.
437 if (OpNo == NumOperands && NewTy == C->getType())
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000438 return getVM()[V] = C;
439
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000440 // Okay, we need to create a new constant. We've already processed some or
441 // all of the operands, set them all up now.
442 SmallVector<Constant*, 8> Ops;
443 Ops.reserve(NumOperands);
444 for (unsigned j = 0; j != OpNo; ++j)
445 Ops.push_back(cast<Constant>(C->getOperand(j)));
446
447 // If one of the operands mismatch, push it and the other mapped operands.
448 if (OpNo != NumOperands) {
449 Ops.push_back(cast<Constant>(Mapped));
450
451 // Map the rest of the operands that aren't processed yet.
452 for (++OpNo; OpNo != NumOperands; ++OpNo)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000453 Ops.push_back(cast<Constant>(mapValue(C->getOperand(OpNo))));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000454 }
David Blaikie88208842015-08-21 20:16:51 +0000455 Type *NewSrcTy = nullptr;
456 if (TypeMapper)
457 if (auto *GEPO = dyn_cast<GEPOperator>(C))
458 NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType());
459
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000460 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000461 return getVM()[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000462 if (isa<ConstantArray>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000463 return getVM()[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000464 if (isa<ConstantStruct>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000465 return getVM()[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000466 if (isa<ConstantVector>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000467 return getVM()[V] = ConstantVector::get(Ops);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000468 // If this is a no-operand constant, it must be because the type was remapped.
469 if (isa<UndefValue>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000470 return getVM()[V] = UndefValue::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000471 if (isa<ConstantAggregateZero>(C))
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000472 return getVM()[V] = ConstantAggregateZero::get(NewTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000473 assert(isa<ConstantPointerNull>(C));
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000474 return getVM()[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000475}
Brian Gaeke6182acf2004-05-19 09:08:12 +0000476
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000477Value *Mapper::mapBlockAddress(const BlockAddress &BA) {
478 Function *F = cast<Function>(mapValue(BA.getFunction()));
479
480 // F may not have materialized its initializer. In that case, create a
481 // dummy basic block for now, and replace it once we've materialized all
482 // the initializers.
483 BasicBlock *BB;
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000484 if (F->empty()) {
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000485 DelayedBBs.push_back(DelayedBasicBlock(BA));
486 BB = DelayedBBs.back().TempBB.get();
Duncan P. N. Exon Smith6f2e3742016-04-06 02:25:12 +0000487 } else {
488 BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock()));
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000489 }
490
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000491 return getVM()[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock());
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000492}
493
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000494Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000495 getVM().MD()[Key].reset(Val);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000496 return Val;
497}
498
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000499Metadata *Mapper::mapToSelf(const Metadata *MD) {
500 return mapToMetadata(MD, const_cast<Metadata *>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000501}
502
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000503bool MDNodeMapper::mapOperand(const Metadata *Op) {
504 if (!Op)
505 return false;
506
507 if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) {
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000508 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000509 "Expected result to be memoized");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000510 return *MappedOp != Op;
511 }
512
513 return push(*cast<MDNode>(Op)).HasChangedAddress;
514}
515
516Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000517 if (!Op)
518 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000519
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000520 if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000521 return *MappedOp;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000522
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000523 if (isa<MDString>(Op))
524 return const_cast<Metadata *>(Op);
525
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000526 return None;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000527}
528
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000529Metadata &MDNodeMapper::getFwdReference(const Data &D, MDNode &Op) {
530 auto Where = Info.find(&Op);
531 assert(Where != Info.end() && "Expected a valid reference");
532
533 auto &OpD = Where->second;
534 assert(OpD.ID > D.ID && "Expected a forward reference");
535
536 if (!OpD.HasChangedAddress)
537 return Op;
538
539 // Lazily construct a temporary node.
540 if (!OpD.Placeholder)
541 OpD.Placeholder = Op.clone();
542
543 return *OpD.Placeholder;
544}
545
546void MDNodeMapper::remapOperands(const Data &D, MDNode &N) {
547 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
548 Metadata *Old = N.getOperand(I);
549 Metadata *New;
550 if (Optional<Metadata *> MappedOp = getMappedOp(Old)){
551 New = *MappedOp;
552 } else {
553 assert(!N.isDistinct() &&
554 "Expected all nodes to be pre-mapped for distinct operands");
555 MDNode &OldN = *cast<MDNode>(Old);
556 assert(!OldN.isDistinct() && "Expected distinct nodes to be pre-mapped");
557 New = &getFwdReference(D, OldN);
558 }
559
560 if (Old != New)
561 N.replaceOperandWith(I, New);
562 }
563}
564
565MDNodeMapper::Data &MDNodeMapper::push(const MDNode &N) {
566 auto Insertion = Info.insert(std::make_pair(&N, Data()));
567 auto &D = Insertion.first->second;
568 if (!Insertion.second)
569 return D;
570
571 // Add to the worklist; check for distinct nodes that are required to be
572 // copied.
573 Worklist.push_back(std::make_pair(&const_cast<MDNode &>(N), false));
574 D.HasChangedAddress = !(M.Flags & RF_MoveDistinctMDs) && N.isDistinct();
575 return D;
576}
577
578bool MDNodeMapper::tryToPop() {
579 if (!Worklist.back().second) {
580 Worklist.back().second = true;
581 return false;
582 }
583
584 MDNode *N = Worklist.pop_back_val().first;
585 Info[N].ID = POT.size();
586 POT.push_back(N);
587 return true;
588}
589
590bool MDNodeMapper::createPOT(const MDNode &FirstN) {
591 bool AnyChanges = false;
592
593 // Do a traversal of the unmapped subgraph, tracking whether operands change.
594 // In some cases, these changes will propagate naturally, but
595 // propagateChangedOperands() catches the general case.
596 AnyChanges |= push(FirstN).HasChangedAddress;
597 while (hasWork()) {
598 if (tryToPop())
599 continue;
600
601 MDNode &N = getCurrentNode();
602 bool LocalChanges = false;
603 for (const Metadata *Op : N.operands())
604 LocalChanges |= mapOperand(Op);
605
606 if (!LocalChanges)
607 continue;
608
609 AnyChanges = true;
610 auto &D = Info[&N];
611 D.HasChangedOps = true;
612
613 // Uniqued nodes change address when operands change.
614 if (!N.isDistinct())
615 D.HasChangedAddress = true;
616 }
617 return AnyChanges;
618}
619
620void MDNodeMapper::propagateChangedOperands() {
621 bool AnyChangedAddresses;
622 do {
623 AnyChangedAddresses = false;
624 for (MDNode *N : POT) {
625 auto &NI = Info[N];
626 if (NI.HasChangedOps)
627 continue;
628
629 if (!llvm::any_of(N->operands(), [&](const Metadata *Op) {
630 auto Where = Info.find(Op);
631 return Where != Info.end() && Where->second.HasChangedAddress;
632 }))
633 continue;
634
635 NI.HasChangedOps = true;
636 if (!N->isDistinct()) {
637 NI.HasChangedAddress = true;
638 AnyChangedAddresses = true;
639 }
640 }
641 } while (AnyChangedAddresses);
642}
643
644void MDNodeMapper::mapDistinctNodes() {
645 // Map all the distinct nodes in POT.
646 for (MDNode *N : POT) {
647 if (!N->isDistinct())
648 continue;
649
650 if (M.Flags & RF_MoveDistinctMDs)
651 M.mapToSelf(N);
652 else
653 M.mapToMetadata(N, MDNode::replaceWithDistinct(N->clone()));
654 }
655}
656
657void MDNodeMapper::mapUniquedNodes() {
658 // Construct uniqued nodes, building forward references as necessary.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000659 SmallVector<MDNode *, 16> CyclicNodes;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000660 for (auto *N : POT) {
661 if (N->isDistinct())
662 continue;
663
664 auto &D = Info[N];
665 assert(D.HasChangedAddress == D.HasChangedOps &&
666 "Uniqued nodes should change address iff ops change");
667 if (!D.HasChangedAddress) {
668 M.mapToSelf(N);
669 continue;
670 }
671
672 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
673 remapOperands(D, *ClonedN);
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000674 CyclicNodes.push_back(MDNode::replaceWithUniqued(std::move(ClonedN)));
675 M.mapToMetadata(N, CyclicNodes.back());
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000676 }
677
678 // Resolve cycles.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000679 for (auto *N : CyclicNodes)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000680 if (!N->isResolved())
681 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000682}
683
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000684void MDNodeMapper::remapDistinctOperands() {
685 for (auto *N : POT) {
686 if (!N->isDistinct())
687 continue;
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000688
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000689 auto &D = Info[N];
690 if (!D.HasChangedOps)
691 continue;
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000692
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000693 assert(D.HasChangedAddress == !bool(M.Flags & RF_MoveDistinctMDs) &&
694 "Distinct nodes should change address iff they cannot be moved");
695 remapOperands(D, D.HasChangedAddress ? *cast<MDNode>(*getMappedOp(N)) : *N);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000696 }
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000697}
698
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000699Metadata *MDNodeMapper::map(const MDNode &FirstN) {
700 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
701 "MDNodeMapper::map assumes module-level changes");
702 assert(POT.empty() && "MDNodeMapper::map is not re-entrant");
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000703
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000704 // Require resolved nodes whenever metadata might be remapped.
705 assert(FirstN.isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000706
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000707 // Return early if nothing at all changed.
708 if (!createPOT(FirstN)) {
709 for (const MDNode *N : POT)
710 M.mapToSelf(N);
711 return &const_cast<MDNode &>(FirstN);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000712 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000713
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000714 propagateChangedOperands();
715 mapDistinctNodes();
716 mapUniquedNodes();
717 remapDistinctOperands();
718
719 // Return the original node, remapped.
720 return *getMappedOp(&FirstN);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000721}
722
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000723Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000724 // If the value already exists in the map, use it.
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000725 if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000726 return *NewMD;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000727
728 if (isa<MDString>(MD))
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000729 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000730
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000731 // This is a module-level metadata. If nothing at the module level is
732 // changing, use an identity mapping.
733 if ((Flags & RF_NoModuleLevelChanges))
Duncan P. N. Exon Smith69341e62016-04-08 18:49:36 +0000734 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000735
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000736 if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000737 // Disallow recursion into metadata mapping through mapValue.
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000738 getVM().disableMapMetadata();
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000739 Value *MappedV = mapValue(CMD->getValue());
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000740 getVM().enableMapMetadata();
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000741
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000742 if (CMD->getValue() == MappedV)
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000743 return mapToSelf(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000744
Duncan P. N. Exon Smith8e65f8d2016-04-04 04:59:56 +0000745 return mapToMetadata(MD, MappedV ? ValueAsMetadata::get(MappedV) : nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000746 }
747
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000748 assert(isa<MDNode>(MD) && "Expected a metadata node");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000749
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000750 return None;
751}
752
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000753Metadata *Mapper::mapLocalAsMetadata(const LocalAsMetadata &LAM) {
754 // Lookup the mapping for the value itself, and return the appropriate
755 // metadata.
756 if (Value *V = mapValue(LAM.getValue())) {
757 if (V == LAM.getValue())
758 return const_cast<LocalAsMetadata *>(&LAM);
759 return ValueAsMetadata::get(V);
760 }
761
762 // FIXME: always return nullptr once Verifier::verifyDominatesUse() ensures
763 // metadata operands only reference defined SSA values.
764 return (Flags & RF_IgnoreMissingLocals)
765 ? nullptr
766 : MDTuple::get(LAM.getContext(), None);
767}
768
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000769Metadata *Mapper::mapMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000770 assert(MD && "Expected valid metadata");
771 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
772
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000773 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
774 return *NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000775
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000776 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000777}
778
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000779void Mapper::flush() {
780 // Flush out the worklist of global values.
781 while (!Worklist.empty()) {
782 WorklistEntry E = Worklist.pop_back_val();
783 CurrentMCID = E.MCID;
784 switch (E.Kind) {
785 case WorklistEntry::MapGlobalInit:
786 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
787 break;
788 case WorklistEntry::MapAppendingVar: {
789 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
790 mapAppendingVariable(*E.Data.AppendingGV.GV,
791 E.Data.AppendingGV.InitPrefix,
792 E.AppendingGVIsOldCtorDtor,
793 makeArrayRef(AppendingInits).slice(PrefixSize));
794 AppendingInits.resize(PrefixSize);
795 break;
796 }
797 case WorklistEntry::MapGlobalAliasee:
798 E.Data.GlobalAliasee.GA->setAliasee(
799 mapConstant(E.Data.GlobalAliasee.Aliasee));
800 break;
801 case WorklistEntry::RemapFunction:
802 remapFunction(*E.Data.RemapF);
803 break;
804 }
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000805 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000806 CurrentMCID = 0;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000807
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000808 // Finish logic for block addresses now that all global values have been
809 // handled.
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000810 while (!DelayedBBs.empty()) {
811 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
812 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
813 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
814 }
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000815}
816
817void Mapper::remapInstruction(Instruction *I) {
Dan Gohmanca26f792010-08-26 15:41:53 +0000818 // Remap operands.
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000819 for (Use &Op : I->operands()) {
820 Value *V = mapValue(Op);
Chris Lattner43f8d162011-01-08 08:15:20 +0000821 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000822 if (V)
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000823 Op = V;
Chris Lattner43f8d162011-01-08 08:15:20 +0000824 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000825 assert((Flags & RF_IgnoreMissingLocals) &&
Chris Lattner43f8d162011-01-08 08:15:20 +0000826 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000827 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000828
Jay Foad61ea0e42011-06-23 09:09:15 +0000829 // Remap phi nodes' incoming blocks.
830 if (PHINode *PN = dyn_cast<PHINode>(I)) {
831 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Duncan P. N. Exon Smithadcebdf2016-04-08 19:17:13 +0000832 Value *V = mapValue(PN->getIncomingBlock(i));
Jay Foad61ea0e42011-06-23 09:09:15 +0000833 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000834 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000835 PN->setIncomingBlock(i, cast<BasicBlock>(V));
836 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000837 assert((Flags & RF_IgnoreMissingLocals) &&
Jay Foad61ea0e42011-06-23 09:09:15 +0000838 "Referenced block not in value map!");
839 }
840 }
841
Devang Patelc0174042011-08-04 20:02:18 +0000842 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000843 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000844 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000845 for (const auto &MI : MDs) {
846 MDNode *Old = MI.second;
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000847 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
Dan Gohmanca26f792010-08-26 15:41:53 +0000848 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000849 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000850 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000851
David Blaikie348de692015-04-23 21:36:23 +0000852 if (!TypeMapper)
853 return;
854
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000855 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000856 if (auto CS = CallSite(I)) {
857 SmallVector<Type *, 3> Tys;
858 FunctionType *FTy = CS.getFunctionType();
859 Tys.reserve(FTy->getNumParams());
860 for (Type *Ty : FTy->params())
861 Tys.push_back(TypeMapper->remapType(Ty));
862 CS.mutateFunctionType(FunctionType::get(
863 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000864 return;
865 }
866 if (auto *AI = dyn_cast<AllocaInst>(I))
867 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000868 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000869 GEP->setSourceElementType(
870 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000871 GEP->setResultElementType(
872 TypeMapper->remapType(GEP->getResultElementType()));
873 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000874 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000875}
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000876
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000877void Mapper::remapFunction(Function &F) {
878 // Remap the operands.
879 for (Use &Op : F.operands())
880 if (Op)
881 Op = mapValue(Op);
882
883 // Remap the metadata attachments.
884 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
885 F.getAllMetadata(MDs);
886 for (const auto &I : MDs)
887 F.setMetadata(I.first, cast_or_null<MDNode>(mapMetadata(I.second)));
888
889 // Remap the argument types.
890 if (TypeMapper)
891 for (Argument &A : F.args())
892 A.mutateType(TypeMapper->remapType(A.getType()));
893
894 // Remap the instructions.
895 for (BasicBlock &BB : F)
896 for (Instruction &I : BB)
897 remapInstruction(&I);
898}
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000899
900void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
901 bool IsOldCtorDtor,
902 ArrayRef<Constant *> NewMembers) {
903 SmallVector<Constant *, 16> Elements;
904 if (InitPrefix) {
905 unsigned NumElements =
906 cast<ArrayType>(InitPrefix->getType())->getNumElements();
907 for (unsigned I = 0; I != NumElements; ++I)
908 Elements.push_back(InitPrefix->getAggregateElement(I));
909 }
910
911 PointerType *VoidPtrTy;
912 Type *EltTy;
913 if (IsOldCtorDtor) {
914 // FIXME: This upgrade is done during linking to support the C API. See
915 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
916 VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
917 auto &ST = *cast<StructType>(NewMembers.front()->getType());
918 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
919 EltTy = StructType::get(GV.getContext(), Tys, false);
920 }
921
922 for (auto *V : NewMembers) {
923 Constant *NewV;
924 if (IsOldCtorDtor) {
925 auto *S = cast<ConstantStruct>(V);
926 auto *E1 = mapValue(S->getOperand(0));
927 auto *E2 = mapValue(S->getOperand(1));
928 Value *Null = Constant::getNullValue(VoidPtrTy);
929 NewV =
930 ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
931 } else {
932 NewV = cast_or_null<Constant>(mapValue(V));
933 }
934 Elements.push_back(NewV);
935 }
936
937 GV.setInitializer(ConstantArray::get(
938 cast<ArrayType>(GV.getType()->getElementType()), Elements));
939}
940
941void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
942 unsigned MCID) {
943 assert(MCID < MCs.size() && "Invalid mapping context");
944
945 WorklistEntry WE;
946 WE.Kind = WorklistEntry::MapGlobalInit;
947 WE.MCID = MCID;
948 WE.Data.GVInit.GV = &GV;
949 WE.Data.GVInit.Init = &Init;
950 Worklist.push_back(WE);
951}
952
953void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
954 Constant *InitPrefix,
955 bool IsOldCtorDtor,
956 ArrayRef<Constant *> NewMembers,
957 unsigned MCID) {
958 assert(MCID < MCs.size() && "Invalid mapping context");
959
960 WorklistEntry WE;
961 WE.Kind = WorklistEntry::MapAppendingVar;
962 WE.MCID = MCID;
963 WE.Data.AppendingGV.GV = &GV;
964 WE.Data.AppendingGV.InitPrefix = InitPrefix;
965 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
966 WE.AppendingGVNumNewMembers = NewMembers.size();
967 Worklist.push_back(WE);
968 AppendingInits.append(NewMembers.begin(), NewMembers.end());
969}
970
971void Mapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
972 unsigned MCID) {
973 assert(MCID < MCs.size() && "Invalid mapping context");
974
975 WorklistEntry WE;
976 WE.Kind = WorklistEntry::MapGlobalAliasee;
977 WE.MCID = MCID;
978 WE.Data.GlobalAliasee.GA = &GA;
979 WE.Data.GlobalAliasee.Aliasee = &Aliasee;
980 Worklist.push_back(WE);
981}
982
983void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
984 assert(MCID < MCs.size() && "Invalid mapping context");
985
986 WorklistEntry WE;
987 WE.Kind = WorklistEntry::RemapFunction;
988 WE.MCID = MCID;
989 WE.Data.RemapF = &F;
990 Worklist.push_back(WE);
991}
992
993void Mapper::addFlags(RemapFlags Flags) {
994 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
995 this->Flags = this->Flags | Flags;
996}
997
998static Mapper *getAsMapper(void *pImpl) {
999 return reinterpret_cast<Mapper *>(pImpl);
1000}
1001
1002namespace {
1003
1004class FlushingMapper {
1005 Mapper &M;
1006
1007public:
1008 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1009 assert(!M.hasWorkToDo() && "Expected to be flushed");
1010 }
1011 ~FlushingMapper() { M.flush(); }
1012 Mapper *operator->() const { return &M; }
1013};
1014
1015} // end namespace
1016
1017ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1018 ValueMapTypeRemapper *TypeMapper,
1019 ValueMaterializer *Materializer)
1020 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1021
1022ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1023
1024unsigned
1025ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1026 ValueMaterializer *Materializer) {
1027 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1028}
1029
1030void ValueMapper::addFlags(RemapFlags Flags) {
1031 FlushingMapper(pImpl)->addFlags(Flags);
1032}
1033
1034Value *ValueMapper::mapValue(const Value &V) {
1035 return FlushingMapper(pImpl)->mapValue(&V);
1036}
1037
1038Constant *ValueMapper::mapConstant(const Constant &C) {
1039 return cast_or_null<Constant>(mapValue(C));
1040}
1041
1042Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1043 return FlushingMapper(pImpl)->mapMetadata(&MD);
1044}
1045
1046MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1047 return cast_or_null<MDNode>(mapMetadata(N));
1048}
1049
1050void ValueMapper::remapInstruction(Instruction &I) {
1051 FlushingMapper(pImpl)->remapInstruction(&I);
1052}
1053
1054void ValueMapper::remapFunction(Function &F) {
1055 FlushingMapper(pImpl)->remapFunction(F);
1056}
1057
1058void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1059 Constant &Init,
1060 unsigned MCID) {
1061 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1062}
1063
1064void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1065 Constant *InitPrefix,
1066 bool IsOldCtorDtor,
1067 ArrayRef<Constant *> NewMembers,
1068 unsigned MCID) {
1069 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1070 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1071}
1072
1073void ValueMapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1074 unsigned MCID) {
1075 getAsMapper(pImpl)->scheduleMapGlobalAliasee(GA, Aliasee, MCID);
1076}
1077
1078void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1079 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1080}