ValueMapper.cpp 38 KB

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