blob: d12ae926e47dd8406b79ac5935c79d283fd3d75e [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 Smitha77d0732016-04-16 03:39:44 +0000508 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
509 assert((!*MappedOp || M.getVM().count(CMD->getValue()) ||
510 M.getVM().getMappedMD(Op)) &&
511 "Expected Value to be memoized");
512 else
513 assert((isa<MDString>(Op) || M.getVM().getMappedMD(Op)) &&
514 "Expected result to be memoized");
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000515 return *MappedOp != Op;
516 }
517
518 return push(*cast<MDNode>(Op)).HasChangedAddress;
519}
520
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000521static ConstantAsMetadata *wrapConstantAsMetadata(const ConstantAsMetadata &CMD,
522 Value *MappedV) {
523 if (CMD.getValue() == MappedV)
524 return const_cast<ConstantAsMetadata *>(&CMD);
525 return MappedV ? ConstantAsMetadata::getConstant(MappedV) : nullptr;
526}
527
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000528Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const {
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000529 if (!Op)
530 return nullptr;
Teresa Johnson0e7c82c2015-12-18 17:51:37 +0000531
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000532 if (Optional<Metadata *> MappedOp = M.getVM().getMappedMD(Op))
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000533 return *MappedOp;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000534
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000535 if (isa<MDString>(Op))
536 return const_cast<Metadata *>(Op);
537
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000538 if (auto *CMD = dyn_cast<ConstantAsMetadata>(Op))
539 return wrapConstantAsMetadata(*CMD, M.getVM().lookup(CMD->getValue()));
540
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000541 return None;
Duncan P. N. Exon Smith077affd2015-01-14 01:01:19 +0000542}
543
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000544Metadata &MDNodeMapper::getFwdReference(const Data &D, MDNode &Op) {
545 auto Where = Info.find(&Op);
546 assert(Where != Info.end() && "Expected a valid reference");
547
548 auto &OpD = Where->second;
549 assert(OpD.ID > D.ID && "Expected a forward reference");
550
551 if (!OpD.HasChangedAddress)
552 return Op;
553
554 // Lazily construct a temporary node.
555 if (!OpD.Placeholder)
556 OpD.Placeholder = Op.clone();
557
558 return *OpD.Placeholder;
559}
560
561void MDNodeMapper::remapOperands(const Data &D, MDNode &N) {
562 for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) {
563 Metadata *Old = N.getOperand(I);
564 Metadata *New;
565 if (Optional<Metadata *> MappedOp = getMappedOp(Old)){
566 New = *MappedOp;
567 } else {
568 assert(!N.isDistinct() &&
569 "Expected all nodes to be pre-mapped for distinct operands");
570 MDNode &OldN = *cast<MDNode>(Old);
571 assert(!OldN.isDistinct() && "Expected distinct nodes to be pre-mapped");
572 New = &getFwdReference(D, OldN);
573 }
574
575 if (Old != New)
576 N.replaceOperandWith(I, New);
577 }
578}
579
580MDNodeMapper::Data &MDNodeMapper::push(const MDNode &N) {
581 auto Insertion = Info.insert(std::make_pair(&N, Data()));
582 auto &D = Insertion.first->second;
583 if (!Insertion.second)
584 return D;
585
586 // Add to the worklist; check for distinct nodes that are required to be
587 // copied.
588 Worklist.push_back(std::make_pair(&const_cast<MDNode &>(N), false));
589 D.HasChangedAddress = !(M.Flags & RF_MoveDistinctMDs) && N.isDistinct();
590 return D;
591}
592
593bool MDNodeMapper::tryToPop() {
594 if (!Worklist.back().second) {
595 Worklist.back().second = true;
596 return false;
597 }
598
599 MDNode *N = Worklist.pop_back_val().first;
600 Info[N].ID = POT.size();
601 POT.push_back(N);
602 return true;
603}
604
605bool MDNodeMapper::createPOT(const MDNode &FirstN) {
606 bool AnyChanges = false;
607
608 // Do a traversal of the unmapped subgraph, tracking whether operands change.
609 // In some cases, these changes will propagate naturally, but
610 // propagateChangedOperands() catches the general case.
611 AnyChanges |= push(FirstN).HasChangedAddress;
612 while (hasWork()) {
613 if (tryToPop())
614 continue;
615
616 MDNode &N = getCurrentNode();
617 bool LocalChanges = false;
618 for (const Metadata *Op : N.operands())
619 LocalChanges |= mapOperand(Op);
620
621 if (!LocalChanges)
622 continue;
623
624 AnyChanges = true;
625 auto &D = Info[&N];
626 D.HasChangedOps = true;
627
628 // Uniqued nodes change address when operands change.
629 if (!N.isDistinct())
630 D.HasChangedAddress = true;
631 }
632 return AnyChanges;
633}
634
635void MDNodeMapper::propagateChangedOperands() {
636 bool AnyChangedAddresses;
637 do {
638 AnyChangedAddresses = false;
639 for (MDNode *N : POT) {
640 auto &NI = Info[N];
641 if (NI.HasChangedOps)
642 continue;
643
644 if (!llvm::any_of(N->operands(), [&](const Metadata *Op) {
645 auto Where = Info.find(Op);
646 return Where != Info.end() && Where->second.HasChangedAddress;
647 }))
648 continue;
649
650 NI.HasChangedOps = true;
651 if (!N->isDistinct()) {
652 NI.HasChangedAddress = true;
653 AnyChangedAddresses = true;
654 }
655 }
656 } while (AnyChangedAddresses);
657}
658
659void MDNodeMapper::mapDistinctNodes() {
660 // Map all the distinct nodes in POT.
661 for (MDNode *N : POT) {
662 if (!N->isDistinct())
663 continue;
664
665 if (M.Flags & RF_MoveDistinctMDs)
666 M.mapToSelf(N);
667 else
668 M.mapToMetadata(N, MDNode::replaceWithDistinct(N->clone()));
669 }
670}
671
672void MDNodeMapper::mapUniquedNodes() {
673 // Construct uniqued nodes, building forward references as necessary.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000674 SmallVector<MDNode *, 16> CyclicNodes;
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000675 for (auto *N : POT) {
676 if (N->isDistinct())
677 continue;
678
679 auto &D = Info[N];
680 assert(D.HasChangedAddress == D.HasChangedOps &&
681 "Uniqued nodes should change address iff ops change");
682 if (!D.HasChangedAddress) {
683 M.mapToSelf(N);
684 continue;
685 }
686
687 TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone();
688 remapOperands(D, *ClonedN);
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000689 CyclicNodes.push_back(MDNode::replaceWithUniqued(std::move(ClonedN)));
690 M.mapToMetadata(N, CyclicNodes.back());
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000691 }
692
693 // Resolve cycles.
Duncan P. N. Exon Smith11f60fd2016-04-13 22:54:01 +0000694 for (auto *N : CyclicNodes)
Teresa Johnsonb703c772016-03-29 18:24:19 +0000695 if (!N->isResolved())
696 N->resolveCycles();
Duncan P. N. Exon Smithc9fdbdb2015-08-07 00:39:26 +0000697}
698
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000699void MDNodeMapper::remapDistinctOperands() {
700 for (auto *N : POT) {
701 if (!N->isDistinct())
702 continue;
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000703
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000704 auto &D = Info[N];
705 if (!D.HasChangedOps)
706 continue;
Duncan P. N. Exon Smith8c9dcac2015-08-07 00:44:55 +0000707
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000708 assert(D.HasChangedAddress == !bool(M.Flags & RF_MoveDistinctMDs) &&
709 "Distinct nodes should change address iff they cannot be moved");
710 remapOperands(D, D.HasChangedAddress ? *cast<MDNode>(*getMappedOp(N)) : *N);
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000711 }
Duncan P. N. Exon Smith6dc22bf2015-01-19 22:44:32 +0000712}
713
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000714Metadata *MDNodeMapper::map(const MDNode &FirstN) {
715 assert(!(M.Flags & RF_NoModuleLevelChanges) &&
716 "MDNodeMapper::map assumes module-level changes");
717 assert(POT.empty() && "MDNodeMapper::map is not re-entrant");
Duncan P. N. Exon Smith14cc94c2015-01-14 01:03:05 +0000718
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000719 // Require resolved nodes whenever metadata might be remapped.
720 assert(FirstN.isResolved() && "Unexpected unresolved node");
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000721
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000722 // Return early if nothing at all changed.
723 if (!createPOT(FirstN)) {
724 for (const MDNode *N : POT)
725 M.mapToSelf(N);
726 return &const_cast<MDNode &>(FirstN);
Duncan P. N. Exon Smith706f37e2015-08-04 06:42:31 +0000727 }
Duncan P. N. Exon Smith0dcffe22015-01-19 22:39:07 +0000728
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000729 propagateChangedOperands();
730 mapDistinctNodes();
731 mapUniquedNodes();
732 remapDistinctOperands();
733
734 // Return the original node, remapped.
735 return *getMappedOp(&FirstN);
Duncan P. N. Exon Smithb5579892015-01-14 01:06:21 +0000736}
737
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000738namespace {
739
740struct MapMetadataDisabler {
741 ValueToValueMapTy &VM;
742
743 MapMetadataDisabler(ValueToValueMapTy &VM) : VM(VM) {
744 VM.disableMapMetadata();
745 }
746 ~MapMetadataDisabler() { VM.enableMapMetadata(); }
747};
748
749} // end namespace
750
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000751Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000752 // If the value already exists in the map, use it.
Duncan P. N. Exon Smithdb6861e2016-04-15 23:18:43 +0000753 if (Optional<Metadata *> NewMD = getVM().getMappedMD(MD))
Duncan P. N. Exon Smithda4a56d2016-04-02 17:04:38 +0000754 return *NewMD;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000755
756 if (isa<MDString>(MD))
Duncan P. N. Exon Smithe05ff7c2016-04-08 18:47:02 +0000757 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000758
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000759 // This is a module-level metadata. If nothing at the module level is
760 // changing, use an identity mapping.
761 if ((Flags & RF_NoModuleLevelChanges))
Duncan P. N. Exon Smith69341e62016-04-08 18:49:36 +0000762 return const_cast<Metadata *>(MD);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000763
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000764 if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) {
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000765 // Disallow recursion into metadata mapping through mapValue.
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000766 MapMetadataDisabler MMD(getVM());
Duncan P. N. Exon Smith756e1c32016-04-03 20:54:51 +0000767
Duncan P. N. Exon Smitha77d0732016-04-16 03:39:44 +0000768 // Don't memoize ConstantAsMetadata. Instead of lasting until the
769 // LLVMContext is destroyed, they can be deleted when the GlobalValue they
770 // reference is destructed. These aren't super common, so the extra
771 // indirection isn't that expensive.
772 return wrapConstantAsMetadata(*CMD, mapValue(CMD->getValue()));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000773 }
774
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000775 assert(isa<MDNode>(MD) && "Expected a metadata node");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000776
Duncan P. N. Exon Smithae8bd4b2016-04-03 19:31:01 +0000777 return None;
778}
779
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000780Metadata *Mapper::mapLocalAsMetadata(const LocalAsMetadata &LAM) {
781 // Lookup the mapping for the value itself, and return the appropriate
782 // metadata.
783 if (Value *V = mapValue(LAM.getValue())) {
784 if (V == LAM.getValue())
785 return const_cast<LocalAsMetadata *>(&LAM);
786 return ValueAsMetadata::get(V);
787 }
788
789 // FIXME: always return nullptr once Verifier::verifyDominatesUse() ensures
790 // metadata operands only reference defined SSA values.
791 return (Flags & RF_IgnoreMissingLocals)
792 ? nullptr
793 : MDTuple::get(LAM.getContext(), None);
794}
795
Duncan P. N. Exon Smith829dc872016-04-03 19:06:24 +0000796Metadata *Mapper::mapMetadata(const Metadata *MD) {
Duncan P. N. Exon Smith4ec55f82016-04-08 03:13:22 +0000797 assert(MD && "Expected valid metadata");
798 assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata");
799
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000800 if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
801 return *NewMD;
Duncan P. N. Exon Smith920df5c2015-02-04 19:44:34 +0000802
Duncan P. N. Exon Smithea7df772016-04-05 20:23:21 +0000803 return MDNodeMapper(*this).map(*cast<MDNode>(MD));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000804}
805
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000806void Mapper::flush() {
807 // Flush out the worklist of global values.
808 while (!Worklist.empty()) {
809 WorklistEntry E = Worklist.pop_back_val();
810 CurrentMCID = E.MCID;
811 switch (E.Kind) {
812 case WorklistEntry::MapGlobalInit:
813 E.Data.GVInit.GV->setInitializer(mapConstant(E.Data.GVInit.Init));
814 break;
815 case WorklistEntry::MapAppendingVar: {
816 unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers;
817 mapAppendingVariable(*E.Data.AppendingGV.GV,
818 E.Data.AppendingGV.InitPrefix,
819 E.AppendingGVIsOldCtorDtor,
820 makeArrayRef(AppendingInits).slice(PrefixSize));
821 AppendingInits.resize(PrefixSize);
822 break;
823 }
824 case WorklistEntry::MapGlobalAliasee:
825 E.Data.GlobalAliasee.GA->setAliasee(
826 mapConstant(E.Data.GlobalAliasee.Aliasee));
827 break;
828 case WorklistEntry::RemapFunction:
829 remapFunction(*E.Data.RemapF);
830 break;
831 }
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000832 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000833 CurrentMCID = 0;
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000834
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000835 // Finish logic for block addresses now that all global values have been
836 // handled.
Duncan P. N. Exon Smithc6065e32016-04-03 20:17:45 +0000837 while (!DelayedBBs.empty()) {
838 DelayedBasicBlock DBB = DelayedBBs.pop_back_val();
839 BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB));
840 DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB);
841 }
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000842}
843
844void Mapper::remapInstruction(Instruction *I) {
Dan Gohmanca26f792010-08-26 15:41:53 +0000845 // Remap operands.
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000846 for (Use &Op : I->operands()) {
847 Value *V = mapValue(Op);
Chris Lattner43f8d162011-01-08 08:15:20 +0000848 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000849 if (V)
Davide Italiano96d2a1c2016-04-14 18:07:32 +0000850 Op = V;
Chris Lattner43f8d162011-01-08 08:15:20 +0000851 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000852 assert((Flags & RF_IgnoreMissingLocals) &&
Chris Lattner43f8d162011-01-08 08:15:20 +0000853 "Referenced value not in value map!");
Brian Gaeke6182acf2004-05-19 09:08:12 +0000854 }
Daniel Dunbar95fe13c2010-08-26 03:48:08 +0000855
Jay Foad61ea0e42011-06-23 09:09:15 +0000856 // Remap phi nodes' incoming blocks.
857 if (PHINode *PN = dyn_cast<PHINode>(I)) {
858 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Duncan P. N. Exon Smithadcebdf2016-04-08 19:17:13 +0000859 Value *V = mapValue(PN->getIncomingBlock(i));
Jay Foad61ea0e42011-06-23 09:09:15 +0000860 // If we aren't ignoring missing entries, assert that something happened.
Craig Topperf40110f2014-04-25 05:29:35 +0000861 if (V)
Jay Foad61ea0e42011-06-23 09:09:15 +0000862 PN->setIncomingBlock(i, cast<BasicBlock>(V));
863 else
Duncan P. N. Exon Smithda68cbc2016-04-07 00:26:43 +0000864 assert((Flags & RF_IgnoreMissingLocals) &&
Jay Foad61ea0e42011-06-23 09:09:15 +0000865 "Referenced block not in value map!");
866 }
867 }
868
Devang Patelc0174042011-08-04 20:02:18 +0000869 // Remap attached metadata.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000870 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patelc0174042011-08-04 20:02:18 +0000871 I->getAllMetadata(MDs);
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000872 for (const auto &MI : MDs) {
873 MDNode *Old = MI.second;
Duncan P. N. Exon Smitha574e7a2016-04-08 19:09:34 +0000874 MDNode *New = cast_or_null<MDNode>(mapMetadata(Old));
Dan Gohmanca26f792010-08-26 15:41:53 +0000875 if (New != Old)
Duncan P. N. Exon Smithe08bcbf2015-08-03 03:27:12 +0000876 I->setMetadata(MI.first, New);
Dan Gohmanca26f792010-08-26 15:41:53 +0000877 }
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000878
David Blaikie348de692015-04-23 21:36:23 +0000879 if (!TypeMapper)
880 return;
881
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000882 // If the instruction's type is being remapped, do so now.
David Blaikie348de692015-04-23 21:36:23 +0000883 if (auto CS = CallSite(I)) {
884 SmallVector<Type *, 3> Tys;
885 FunctionType *FTy = CS.getFunctionType();
886 Tys.reserve(FTy->getNumParams());
887 for (Type *Ty : FTy->params())
888 Tys.push_back(TypeMapper->remapType(Ty));
889 CS.mutateFunctionType(FunctionType::get(
890 TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg()));
David Blaikiebf0a42a2015-04-29 23:00:35 +0000891 return;
892 }
893 if (auto *AI = dyn_cast<AllocaInst>(I))
894 AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000895 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
David Blaikie73cf8722015-05-05 18:03:48 +0000896 GEP->setSourceElementType(
897 TypeMapper->remapType(GEP->getSourceElementType()));
David Blaikief5147ef2015-06-01 03:09:34 +0000898 GEP->setResultElementType(
899 TypeMapper->remapType(GEP->getResultElementType()));
900 }
David Blaikiebf0a42a2015-04-29 23:00:35 +0000901 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohmanca26f792010-08-26 15:41:53 +0000902}
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000903
Duncan P. N. Exon Smithbb2c3e12016-04-08 19:26:32 +0000904void Mapper::remapFunction(Function &F) {
905 // Remap the operands.
906 for (Use &Op : F.operands())
907 if (Op)
908 Op = mapValue(Op);
909
910 // Remap the metadata attachments.
911 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
912 F.getAllMetadata(MDs);
913 for (const auto &I : MDs)
914 F.setMetadata(I.first, cast_or_null<MDNode>(mapMetadata(I.second)));
915
916 // Remap the argument types.
917 if (TypeMapper)
918 for (Argument &A : F.args())
919 A.mutateType(TypeMapper->remapType(A.getType()));
920
921 // Remap the instructions.
922 for (BasicBlock &BB : F)
923 for (Instruction &I : BB)
924 remapInstruction(&I);
925}
Duncan P. N. Exon Smith39423b02016-04-16 02:29:55 +0000926
927void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,
928 bool IsOldCtorDtor,
929 ArrayRef<Constant *> NewMembers) {
930 SmallVector<Constant *, 16> Elements;
931 if (InitPrefix) {
932 unsigned NumElements =
933 cast<ArrayType>(InitPrefix->getType())->getNumElements();
934 for (unsigned I = 0; I != NumElements; ++I)
935 Elements.push_back(InitPrefix->getAggregateElement(I));
936 }
937
938 PointerType *VoidPtrTy;
939 Type *EltTy;
940 if (IsOldCtorDtor) {
941 // FIXME: This upgrade is done during linking to support the C API. See
942 // also IRLinker::linkAppendingVarProto() in IRMover.cpp.
943 VoidPtrTy = Type::getInt8Ty(GV.getContext())->getPointerTo();
944 auto &ST = *cast<StructType>(NewMembers.front()->getType());
945 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
946 EltTy = StructType::get(GV.getContext(), Tys, false);
947 }
948
949 for (auto *V : NewMembers) {
950 Constant *NewV;
951 if (IsOldCtorDtor) {
952 auto *S = cast<ConstantStruct>(V);
953 auto *E1 = mapValue(S->getOperand(0));
954 auto *E2 = mapValue(S->getOperand(1));
955 Value *Null = Constant::getNullValue(VoidPtrTy);
956 NewV =
957 ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
958 } else {
959 NewV = cast_or_null<Constant>(mapValue(V));
960 }
961 Elements.push_back(NewV);
962 }
963
964 GV.setInitializer(ConstantArray::get(
965 cast<ArrayType>(GV.getType()->getElementType()), Elements));
966}
967
968void Mapper::scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init,
969 unsigned MCID) {
970 assert(MCID < MCs.size() && "Invalid mapping context");
971
972 WorklistEntry WE;
973 WE.Kind = WorklistEntry::MapGlobalInit;
974 WE.MCID = MCID;
975 WE.Data.GVInit.GV = &GV;
976 WE.Data.GVInit.Init = &Init;
977 Worklist.push_back(WE);
978}
979
980void Mapper::scheduleMapAppendingVariable(GlobalVariable &GV,
981 Constant *InitPrefix,
982 bool IsOldCtorDtor,
983 ArrayRef<Constant *> NewMembers,
984 unsigned MCID) {
985 assert(MCID < MCs.size() && "Invalid mapping context");
986
987 WorklistEntry WE;
988 WE.Kind = WorklistEntry::MapAppendingVar;
989 WE.MCID = MCID;
990 WE.Data.AppendingGV.GV = &GV;
991 WE.Data.AppendingGV.InitPrefix = InitPrefix;
992 WE.AppendingGVIsOldCtorDtor = IsOldCtorDtor;
993 WE.AppendingGVNumNewMembers = NewMembers.size();
994 Worklist.push_back(WE);
995 AppendingInits.append(NewMembers.begin(), NewMembers.end());
996}
997
998void Mapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
999 unsigned MCID) {
1000 assert(MCID < MCs.size() && "Invalid mapping context");
1001
1002 WorklistEntry WE;
1003 WE.Kind = WorklistEntry::MapGlobalAliasee;
1004 WE.MCID = MCID;
1005 WE.Data.GlobalAliasee.GA = &GA;
1006 WE.Data.GlobalAliasee.Aliasee = &Aliasee;
1007 Worklist.push_back(WE);
1008}
1009
1010void Mapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1011 assert(MCID < MCs.size() && "Invalid mapping context");
1012
1013 WorklistEntry WE;
1014 WE.Kind = WorklistEntry::RemapFunction;
1015 WE.MCID = MCID;
1016 WE.Data.RemapF = &F;
1017 Worklist.push_back(WE);
1018}
1019
1020void Mapper::addFlags(RemapFlags Flags) {
1021 assert(!hasWorkToDo() && "Expected to have flushed the worklist");
1022 this->Flags = this->Flags | Flags;
1023}
1024
1025static Mapper *getAsMapper(void *pImpl) {
1026 return reinterpret_cast<Mapper *>(pImpl);
1027}
1028
1029namespace {
1030
1031class FlushingMapper {
1032 Mapper &M;
1033
1034public:
1035 explicit FlushingMapper(void *pImpl) : M(*getAsMapper(pImpl)) {
1036 assert(!M.hasWorkToDo() && "Expected to be flushed");
1037 }
1038 ~FlushingMapper() { M.flush(); }
1039 Mapper *operator->() const { return &M; }
1040};
1041
1042} // end namespace
1043
1044ValueMapper::ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags,
1045 ValueMapTypeRemapper *TypeMapper,
1046 ValueMaterializer *Materializer)
1047 : pImpl(new Mapper(VM, Flags, TypeMapper, Materializer)) {}
1048
1049ValueMapper::~ValueMapper() { delete getAsMapper(pImpl); }
1050
1051unsigned
1052ValueMapper::registerAlternateMappingContext(ValueToValueMapTy &VM,
1053 ValueMaterializer *Materializer) {
1054 return getAsMapper(pImpl)->registerAlternateMappingContext(VM, Materializer);
1055}
1056
1057void ValueMapper::addFlags(RemapFlags Flags) {
1058 FlushingMapper(pImpl)->addFlags(Flags);
1059}
1060
1061Value *ValueMapper::mapValue(const Value &V) {
1062 return FlushingMapper(pImpl)->mapValue(&V);
1063}
1064
1065Constant *ValueMapper::mapConstant(const Constant &C) {
1066 return cast_or_null<Constant>(mapValue(C));
1067}
1068
1069Metadata *ValueMapper::mapMetadata(const Metadata &MD) {
1070 return FlushingMapper(pImpl)->mapMetadata(&MD);
1071}
1072
1073MDNode *ValueMapper::mapMDNode(const MDNode &N) {
1074 return cast_or_null<MDNode>(mapMetadata(N));
1075}
1076
1077void ValueMapper::remapInstruction(Instruction &I) {
1078 FlushingMapper(pImpl)->remapInstruction(&I);
1079}
1080
1081void ValueMapper::remapFunction(Function &F) {
1082 FlushingMapper(pImpl)->remapFunction(F);
1083}
1084
1085void ValueMapper::scheduleMapGlobalInitializer(GlobalVariable &GV,
1086 Constant &Init,
1087 unsigned MCID) {
1088 getAsMapper(pImpl)->scheduleMapGlobalInitializer(GV, Init, MCID);
1089}
1090
1091void ValueMapper::scheduleMapAppendingVariable(GlobalVariable &GV,
1092 Constant *InitPrefix,
1093 bool IsOldCtorDtor,
1094 ArrayRef<Constant *> NewMembers,
1095 unsigned MCID) {
1096 getAsMapper(pImpl)->scheduleMapAppendingVariable(
1097 GV, InitPrefix, IsOldCtorDtor, NewMembers, MCID);
1098}
1099
1100void ValueMapper::scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
1101 unsigned MCID) {
1102 getAsMapper(pImpl)->scheduleMapGlobalAliasee(GA, Aliasee, MCID);
1103}
1104
1105void ValueMapper::scheduleRemapFunction(Function &F, unsigned MCID) {
1106 getAsMapper(pImpl)->scheduleRemapFunction(F, MCID);
1107}