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