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" |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 16 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Function.h" |
| 19 | #include "llvm/IR/InlineAsm.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/Metadata.h" |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Operator.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 24 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 25 | // Out of line method to get vtable etc for class. |
Craig Topper | 2a6a08b | 2012-09-26 06:36:36 +0000 | [diff] [blame] | 26 | void ValueMapTypeRemapper::anchor() {} |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 27 | void ValueMaterializer::anchor() {} |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 28 | void ValueMaterializer::materializeInitFor(GlobalValue *New, GlobalValue *Old) { |
| 29 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 30 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 33 | /// A GlobalValue whose initializer needs to be materialized. |
| 34 | struct DelayedGlobalValueInit { |
| 35 | GlobalValue *Old; |
| 36 | GlobalValue *New; |
| 37 | DelayedGlobalValueInit(const GlobalValue *Old, GlobalValue *New) |
| 38 | : Old(const_cast<GlobalValue *>(Old)), New(New) {} |
| 39 | }; |
| 40 | |
| 41 | /// A basic block used in a BlockAddress whose function body is not yet |
| 42 | /// materialized. |
| 43 | struct DelayedBasicBlock { |
| 44 | BasicBlock *OldBB; |
| 45 | std::unique_ptr<BasicBlock> TempBB; |
Duncan P. N. Exon Smith | a997856 | 2016-04-03 20:42:21 +0000 | [diff] [blame] | 46 | |
| 47 | // Explicit move for MSVC. |
| 48 | DelayedBasicBlock(DelayedBasicBlock &&X) |
| 49 | : OldBB(std::move(X.OldBB)), TempBB(std::move(X.TempBB)) {} |
| 50 | DelayedBasicBlock &operator=(DelayedBasicBlock &&X) { |
| 51 | OldBB = std::move(X.OldBB); |
| 52 | TempBB = std::move(X.TempBB); |
| 53 | return *this; |
| 54 | } |
| 55 | |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 56 | DelayedBasicBlock(const BlockAddress &Old) |
| 57 | : OldBB(Old.getBasicBlock()), |
| 58 | TempBB(BasicBlock::Create(Old.getContext())) {} |
| 59 | }; |
| 60 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 61 | class MDNodeMapper; |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 62 | class Mapper { |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 63 | friend class MDNodeMapper; |
| 64 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 65 | ValueToValueMapTy &VM; |
| 66 | RemapFlags Flags; |
| 67 | ValueMapTypeRemapper *TypeMapper; |
| 68 | ValueMaterializer *Materializer; |
| 69 | |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 70 | SmallVector<DelayedGlobalValueInit, 8> DelayedInits; |
| 71 | SmallVector<DelayedBasicBlock, 1> DelayedBBs; |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 72 | |
| 73 | public: |
| 74 | Mapper(ValueToValueMapTy &VM, RemapFlags Flags, |
| 75 | ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer) |
| 76 | : VM(VM), Flags(Flags), TypeMapper(TypeMapper), |
| 77 | Materializer(Materializer) {} |
| 78 | |
| 79 | ~Mapper(); |
| 80 | |
| 81 | Value *mapValue(const Value *V); |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 82 | void remapInstruction(Instruction *I); |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 83 | |
| 84 | /// Map metadata. |
| 85 | /// |
| 86 | /// Find the mapping for MD. Guarantees that the return will be resolved |
| 87 | /// (not an MDNode, or MDNode::isResolved() returns true). |
| 88 | Metadata *mapMetadata(const Metadata *MD); |
| 89 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 90 | // Map LocalAsMetadata, which never gets memoized. |
| 91 | // |
| 92 | // If the referenced local is not mapped, the principled return is nullptr. |
| 93 | // However, optimization passes sometimes move metadata operands *before* the |
| 94 | // SSA values they reference. To prevent crashes in \a RemapInstruction(), |
| 95 | // return "!{}" when RF_IgnoreMissingLocals is not set. |
| 96 | // |
| 97 | // \note Adding a mapping for LocalAsMetadata is unsupported. Add a mapping |
| 98 | // to the value map for the SSA value in question instead. |
| 99 | // |
| 100 | // FIXME: Once we have a verifier check for forward references to SSA values |
| 101 | // through metadata operands, always return nullptr on unmapped locals. |
| 102 | Metadata *mapLocalAsMetadata(const LocalAsMetadata &LAM); |
| 103 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 104 | private: |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 105 | Value *mapBlockAddress(const BlockAddress &BA); |
| 106 | |
Duncan P. N. Exon Smith | ae8bd4b | 2016-04-03 19:31:01 +0000 | [diff] [blame] | 107 | /// Map metadata that doesn't require visiting operands. |
| 108 | Optional<Metadata *> mapSimpleMetadata(const Metadata *MD); |
| 109 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 110 | Metadata *mapToMetadata(const Metadata *Key, Metadata *Val); |
| 111 | Metadata *mapToSelf(const Metadata *MD); |
| 112 | }; |
| 113 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 114 | class MDNodeMapper { |
| 115 | Mapper &M; |
| 116 | |
| 117 | struct Data { |
| 118 | bool HasChangedOps = false; |
| 119 | bool HasChangedAddress = false; |
| 120 | unsigned ID = ~0u; |
| 121 | TempMDNode Placeholder; |
Duncan P. N. Exon Smith | f880d35 | 2016-04-05 21:07:01 +0000 | [diff] [blame] | 122 | |
Duncan P. N. Exon Smith | 818e5f3 | 2016-04-05 21:25:33 +0000 | [diff] [blame] | 123 | Data() {} |
| 124 | Data(Data &&X) |
| 125 | : HasChangedOps(std::move(X.HasChangedOps)), |
| 126 | HasChangedAddress(std::move(X.HasChangedAddress)), |
| 127 | ID(std::move(X.ID)), Placeholder(std::move(X.Placeholder)) {} |
| 128 | Data &operator=(Data &&X) { |
| 129 | HasChangedOps = std::move(X.HasChangedOps); |
| 130 | HasChangedAddress = std::move(X.HasChangedAddress); |
| 131 | ID = std::move(X.ID); |
| 132 | Placeholder = std::move(X.Placeholder); |
| 133 | return *this; |
| 134 | } |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | SmallDenseMap<const Metadata *, Data, 32> Info; |
| 138 | SmallVector<std::pair<MDNode *, bool>, 16> Worklist; |
| 139 | SmallVector<MDNode *, 16> POT; |
| 140 | |
| 141 | public: |
| 142 | MDNodeMapper(Mapper &M) : M(M) {} |
| 143 | |
| 144 | /// Map a metadata node (and its transitive operands). |
| 145 | /// |
| 146 | /// This is the only entry point into MDNodeMapper. It works as follows: |
| 147 | /// |
| 148 | /// 1. \a createPOT(): use a worklist to perform a post-order traversal of |
| 149 | /// the transitively referenced unmapped nodes. |
| 150 | /// |
| 151 | /// 2. \a propagateChangedOperands(): track which nodes will change |
| 152 | /// operands, and which will have new addresses in the mapped scheme. |
| 153 | /// Propagate the changes through the POT until fixed point, to pick up |
| 154 | /// uniquing cycles that need to change. |
| 155 | /// |
| 156 | /// 3. \a mapDistinctNodes(): map all the distinct nodes without touching |
| 157 | /// their operands. If RF_MoveDistinctMetadata, they get mapped to |
| 158 | /// themselves; otherwise, they get mapped to clones. |
| 159 | /// |
| 160 | /// 4. \a mapUniquedNodes(): map the uniqued nodes (bottom-up), lazily |
| 161 | /// creating temporaries for forward references as needed. |
| 162 | /// |
| 163 | /// 5. \a remapDistinctOperands(): remap the operands of the distinct nodes. |
| 164 | Metadata *map(const MDNode &FirstN); |
| 165 | |
| 166 | private: |
| 167 | /// Return \c true as long as there's work to do. |
| 168 | bool hasWork() const { return !Worklist.empty(); } |
| 169 | |
| 170 | /// Get the current node in the worklist. |
| 171 | MDNode &getCurrentNode() const { return *Worklist.back().first; } |
| 172 | |
| 173 | /// Push a node onto the worklist. |
| 174 | /// |
| 175 | /// Adds \c N to \a Worklist and \a Info, unless it's already inserted. If |
| 176 | /// \c N.isDistinct(), \a Data::HasChangedAddress will be set based on \a |
| 177 | /// RF_MoveDistinctMDs. |
| 178 | /// |
| 179 | /// Returns the data for the node. |
| 180 | /// |
| 181 | /// \post Data::HasChangedAddress iff !RF_MoveDistinctMDs && N.isDistinct(). |
| 182 | /// \post Worklist.back().first == &N. |
| 183 | /// \post Worklist.back().second == false. |
| 184 | Data &push(const MDNode &N); |
| 185 | |
| 186 | /// Map a node operand, and return true if it changes. |
| 187 | /// |
| 188 | /// \post getMappedOp(Op) does not return None. |
| 189 | bool mapOperand(const Metadata *Op); |
| 190 | |
| 191 | /// Get a previously mapped node. |
| 192 | Optional<Metadata *> getMappedOp(const Metadata *Op) const; |
| 193 | |
| 194 | /// Try to pop a node off the worklist and store it in POT. |
| 195 | /// |
| 196 | /// Returns \c true if it popped; \c false if its operands need to be |
| 197 | /// visited. |
| 198 | /// |
| 199 | /// \post If Worklist.back().second == false: Worklist.back().second == true. |
| 200 | /// \post Else: Worklist.back() has been popped off and added to \a POT. |
| 201 | bool tryToPop(); |
| 202 | |
| 203 | /// Get a forward reference to a node to use as an operand. |
| 204 | /// |
| 205 | /// Returns \c Op if it's not changing; otherwise, lazily creates a temporary |
| 206 | /// node and returns it. |
| 207 | Metadata &getFwdReference(const Data &D, MDNode &Op); |
| 208 | |
| 209 | /// Create a post-order traversal from the given node. |
| 210 | /// |
| 211 | /// This traverses the metadata graph deeply enough to map \c FirstN. It |
| 212 | /// uses \a mapOperand() (indirectly, \a Mapper::mapSimplifiedNode()), so any |
| 213 | /// metadata that has already been mapped will not be part of the POT. |
| 214 | /// |
| 215 | /// \post \a POT is a post-order traversal ending with \c FirstN. |
| 216 | bool createPOT(const MDNode &FirstN); |
| 217 | |
| 218 | /// Propagate changed operands through post-order traversal. |
| 219 | /// |
| 220 | /// Until fixed point, iteratively update: |
| 221 | /// |
| 222 | /// - \a Data::HasChangedOps based on \a Data::HasChangedAddress of operands; |
| 223 | /// - \a Data::HasChangedAddress based on Data::HasChangedOps. |
| 224 | /// |
| 225 | /// This algorithm never changes \a Data::HasChangedAddress for distinct |
| 226 | /// nodes. |
| 227 | /// |
| 228 | /// \post \a POT is a post-order traversal ending with \c FirstN. |
| 229 | void propagateChangedOperands(); |
| 230 | |
| 231 | /// Map all distinct nodes in POT. |
| 232 | /// |
| 233 | /// \post \a getMappedOp() returns the correct node for every distinct node. |
| 234 | void mapDistinctNodes(); |
| 235 | |
| 236 | /// Map all uniqued nodes in POT with the correct operands. |
| 237 | /// |
| 238 | /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called). |
| 239 | /// \post \a getMappedOp() returns the correct node for every node. |
| 240 | /// \post \a MDNode::operands() is correct for every uniqued node. |
| 241 | /// \post \a MDNode::isResolved() returns true for every node. |
| 242 | void mapUniquedNodes(); |
| 243 | |
| 244 | /// Re-map the operands for distinct nodes in POT. |
| 245 | /// |
| 246 | /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called). |
| 247 | /// \pre Uniqued nodes are mapped (\a mapUniquedNodes() has been called). |
| 248 | /// \post \a MDNode::operands() is correct for every distinct node. |
| 249 | void remapDistinctOperands(); |
| 250 | |
| 251 | /// Remap a node's operands. |
| 252 | /// |
| 253 | /// Iterate through operands and update them in place using \a getMappedOp() |
| 254 | /// and \a getFwdReference(). |
| 255 | /// |
| 256 | /// \pre N.isDistinct() or N.isTemporary(). |
| 257 | /// \pre Distinct nodes are mapped (\a mapDistinctNodes() has been called). |
| 258 | /// \pre If \c N is distinct, all uniqued nodes are already mapped. |
| 259 | void remapOperands(const Data &D, MDNode &N); |
| 260 | }; |
| 261 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 262 | } // end namespace |
| 263 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 264 | Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 265 | ValueMapTypeRemapper *TypeMapper, |
| 266 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 267 | return Mapper(VM, Flags, TypeMapper, Materializer).mapValue(V); |
| 268 | } |
| 269 | |
| 270 | Value *Mapper::mapValue(const Value *V) { |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 271 | ValueToValueMapTy::iterator I = VM.find(V); |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 273 | // If the value already exists in the map, use it. |
| 274 | if (I != VM.end() && I->second) return I->second; |
| 275 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 276 | // If we have a materializer and it can materialize a value, use that. |
| 277 | if (Materializer) { |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 278 | if (Value *NewV = |
| 279 | Materializer->materializeDeclFor(const_cast<Value *>(V))) { |
| 280 | VM[V] = NewV; |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 281 | if (auto *NewGV = dyn_cast<GlobalValue>(NewV)) |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 282 | DelayedInits.push_back( |
| 283 | DelayedGlobalValueInit(cast<GlobalValue>(V), NewGV)); |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 284 | return NewV; |
| 285 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 288 | // Global values do not need to be seeded into the VM if they |
| 289 | // are using the identity mapping. |
Teresa Johnson | 83d03dd | 2015-11-15 14:50:14 +0000 | [diff] [blame] | 290 | if (isa<GlobalValue>(V)) { |
Duncan P. N. Exon Smith | fdccad9 | 2016-04-07 01:22:45 +0000 | [diff] [blame] | 291 | if (Flags & RF_NullMapMissingGlobalValues) |
Teresa Johnson | 83d03dd | 2015-11-15 14:50:14 +0000 | [diff] [blame] | 292 | return nullptr; |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 293 | return VM[V] = const_cast<Value*>(V); |
Teresa Johnson | 83d03dd | 2015-11-15 14:50:14 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 8b4cf5e | 2011-07-15 23:18:40 +0000 | [diff] [blame] | 296 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
| 297 | // Inline asm may need *type* remapping. |
| 298 | FunctionType *NewTy = IA->getFunctionType(); |
| 299 | if (TypeMapper) { |
| 300 | NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy)); |
| 301 | |
| 302 | if (NewTy != IA->getFunctionType()) |
| 303 | V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(), |
| 304 | IA->hasSideEffects(), IA->isAlignStack()); |
| 305 | } |
| 306 | |
| 307 | return VM[V] = const_cast<Value*>(V); |
| 308 | } |
Chris Lattner | 6aa34b0 | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 309 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 310 | if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) { |
| 311 | const Metadata *MD = MDV->getMetadata(); |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 312 | |
| 313 | if (auto *LAM = dyn_cast<LocalAsMetadata>(MD)) { |
| 314 | // Look through to grab the local value. |
| 315 | if (Value *LV = mapValue(LAM->getValue())) { |
| 316 | if (V == LAM->getValue()) |
| 317 | return const_cast<Value *>(V); |
| 318 | return MetadataAsValue::get(V->getContext(), ValueAsMetadata::get(LV)); |
| 319 | } |
| 320 | |
| 321 | // FIXME: always return nullptr once Verifier::verifyDominatesUse() |
| 322 | // ensures metadata operands only reference defined SSA values. |
| 323 | return (Flags & RF_IgnoreMissingLocals) |
| 324 | ? nullptr |
| 325 | : MetadataAsValue::get(V->getContext(), |
| 326 | MDTuple::get(V->getContext(), None)); |
| 327 | } |
| 328 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 329 | // If this is a module-level metadata and we know that nothing at the module |
| 330 | // level is changing, then use an identity mapping. |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 331 | if (Flags & RF_NoModuleLevelChanges) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 332 | return VM[V] = const_cast<Value *>(V); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 333 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 334 | // Map the metadata and turn it into a value. |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 335 | auto *MappedMD = mapMetadata(MD); |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 336 | if (MD == MappedMD) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 337 | return VM[V] = const_cast<Value *>(V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 338 | return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD); |
Victor Hernandez | 5fa88d4 | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 341 | // Okay, this either must be a constant (which may or may not be mappable) or |
| 342 | // is something that is not in the mapping table. |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 343 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 344 | if (!C) |
| 345 | return nullptr; |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 346 | |
| 347 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 348 | return mapBlockAddress(*BA); |
| 349 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 350 | // Otherwise, we have some other constant to remap. Start by checking to see |
| 351 | // if all operands have an identity remapping. |
| 352 | unsigned OpNo = 0, NumOperands = C->getNumOperands(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 353 | Value *Mapped = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 354 | for (; OpNo != NumOperands; ++OpNo) { |
| 355 | Value *Op = C->getOperand(OpNo); |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 356 | Mapped = mapValue(Op); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 357 | if (Mapped != C) break; |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 359 | |
| 360 | // See if the type mapper wants to remap the type as well. |
| 361 | Type *NewTy = C->getType(); |
| 362 | if (TypeMapper) |
| 363 | NewTy = TypeMapper->remapType(NewTy); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 364 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 365 | // If the result type and all operands match up, then just insert an identity |
| 366 | // mapping. |
| 367 | if (OpNo == NumOperands && NewTy == C->getType()) |
| 368 | return VM[V] = C; |
| 369 | |
| 370 | // Okay, we need to create a new constant. We've already processed some or |
| 371 | // all of the operands, set them all up now. |
| 372 | SmallVector<Constant*, 8> Ops; |
| 373 | Ops.reserve(NumOperands); |
| 374 | for (unsigned j = 0; j != OpNo; ++j) |
| 375 | Ops.push_back(cast<Constant>(C->getOperand(j))); |
| 376 | |
| 377 | // If one of the operands mismatch, push it and the other mapped operands. |
| 378 | if (OpNo != NumOperands) { |
| 379 | Ops.push_back(cast<Constant>(Mapped)); |
| 380 | |
| 381 | // Map the rest of the operands that aren't processed yet. |
| 382 | for (++OpNo; OpNo != NumOperands; ++OpNo) |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 383 | Ops.push_back(cast<Constant>(mapValue(C->getOperand(OpNo)))); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 384 | } |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 385 | Type *NewSrcTy = nullptr; |
| 386 | if (TypeMapper) |
| 387 | if (auto *GEPO = dyn_cast<GEPOperator>(C)) |
| 388 | NewSrcTy = TypeMapper->remapType(GEPO->getSourceElementType()); |
| 389 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 390 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 391 | return VM[V] = CE->getWithOperands(Ops, NewTy, false, NewSrcTy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 392 | if (isa<ConstantArray>(C)) |
| 393 | return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops); |
| 394 | if (isa<ConstantStruct>(C)) |
| 395 | return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops); |
| 396 | if (isa<ConstantVector>(C)) |
| 397 | return VM[V] = ConstantVector::get(Ops); |
| 398 | // If this is a no-operand constant, it must be because the type was remapped. |
| 399 | if (isa<UndefValue>(C)) |
| 400 | return VM[V] = UndefValue::get(NewTy); |
| 401 | if (isa<ConstantAggregateZero>(C)) |
| 402 | return VM[V] = ConstantAggregateZero::get(NewTy); |
| 403 | assert(isa<ConstantPointerNull>(C)); |
| 404 | return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy)); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 405 | } |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 406 | |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 407 | Value *Mapper::mapBlockAddress(const BlockAddress &BA) { |
| 408 | Function *F = cast<Function>(mapValue(BA.getFunction())); |
| 409 | |
| 410 | // F may not have materialized its initializer. In that case, create a |
| 411 | // dummy basic block for now, and replace it once we've materialized all |
| 412 | // the initializers. |
| 413 | BasicBlock *BB; |
Duncan P. N. Exon Smith | 6f2e374 | 2016-04-06 02:25:12 +0000 | [diff] [blame] | 414 | if (F->empty()) { |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 415 | DelayedBBs.push_back(DelayedBasicBlock(BA)); |
| 416 | BB = DelayedBBs.back().TempBB.get(); |
Duncan P. N. Exon Smith | 6f2e374 | 2016-04-06 02:25:12 +0000 | [diff] [blame] | 417 | } else { |
| 418 | BB = cast_or_null<BasicBlock>(mapValue(BA.getBasicBlock())); |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return VM[&BA] = BlockAddress::get(F, BB ? BB : BA.getBasicBlock()); |
| 422 | } |
| 423 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 424 | Metadata *Mapper::mapToMetadata(const Metadata *Key, Metadata *Val) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 425 | VM.MD()[Key].reset(Val); |
| 426 | return Val; |
| 427 | } |
| 428 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 429 | Metadata *Mapper::mapToSelf(const Metadata *MD) { |
| 430 | return mapToMetadata(MD, const_cast<Metadata *>(MD)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 433 | bool MDNodeMapper::mapOperand(const Metadata *Op) { |
| 434 | if (!Op) |
| 435 | return false; |
| 436 | |
| 437 | if (Optional<Metadata *> MappedOp = M.mapSimpleMetadata(Op)) { |
Duncan P. N. Exon Smith | e05ff7c | 2016-04-08 18:47:02 +0000 | [diff] [blame] | 438 | assert((isa<MDString>(Op) || M.VM.getMappedMD(Op)) && |
| 439 | "Expected result to be memoized"); |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 440 | return *MappedOp != Op; |
| 441 | } |
| 442 | |
| 443 | return push(*cast<MDNode>(Op)).HasChangedAddress; |
| 444 | } |
| 445 | |
| 446 | Optional<Metadata *> MDNodeMapper::getMappedOp(const Metadata *Op) const { |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 447 | if (!Op) |
| 448 | return nullptr; |
Teresa Johnson | 0e7c82c | 2015-12-18 17:51:37 +0000 | [diff] [blame] | 449 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 450 | if (Optional<Metadata *> MappedOp = M.VM.getMappedMD(Op)) |
| 451 | return *MappedOp; |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 452 | |
Duncan P. N. Exon Smith | e05ff7c | 2016-04-08 18:47:02 +0000 | [diff] [blame] | 453 | if (isa<MDString>(Op)) |
| 454 | return const_cast<Metadata *>(Op); |
| 455 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 456 | return None; |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 459 | Metadata &MDNodeMapper::getFwdReference(const Data &D, MDNode &Op) { |
| 460 | auto Where = Info.find(&Op); |
| 461 | assert(Where != Info.end() && "Expected a valid reference"); |
| 462 | |
| 463 | auto &OpD = Where->second; |
| 464 | assert(OpD.ID > D.ID && "Expected a forward reference"); |
| 465 | |
| 466 | if (!OpD.HasChangedAddress) |
| 467 | return Op; |
| 468 | |
| 469 | // Lazily construct a temporary node. |
| 470 | if (!OpD.Placeholder) |
| 471 | OpD.Placeholder = Op.clone(); |
| 472 | |
| 473 | return *OpD.Placeholder; |
| 474 | } |
| 475 | |
| 476 | void MDNodeMapper::remapOperands(const Data &D, MDNode &N) { |
| 477 | for (unsigned I = 0, E = N.getNumOperands(); I != E; ++I) { |
| 478 | Metadata *Old = N.getOperand(I); |
| 479 | Metadata *New; |
| 480 | if (Optional<Metadata *> MappedOp = getMappedOp(Old)){ |
| 481 | New = *MappedOp; |
| 482 | } else { |
| 483 | assert(!N.isDistinct() && |
| 484 | "Expected all nodes to be pre-mapped for distinct operands"); |
| 485 | MDNode &OldN = *cast<MDNode>(Old); |
| 486 | assert(!OldN.isDistinct() && "Expected distinct nodes to be pre-mapped"); |
| 487 | New = &getFwdReference(D, OldN); |
| 488 | } |
| 489 | |
| 490 | if (Old != New) |
| 491 | N.replaceOperandWith(I, New); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | MDNodeMapper::Data &MDNodeMapper::push(const MDNode &N) { |
| 496 | auto Insertion = Info.insert(std::make_pair(&N, Data())); |
| 497 | auto &D = Insertion.first->second; |
| 498 | if (!Insertion.second) |
| 499 | return D; |
| 500 | |
| 501 | // Add to the worklist; check for distinct nodes that are required to be |
| 502 | // copied. |
| 503 | Worklist.push_back(std::make_pair(&const_cast<MDNode &>(N), false)); |
| 504 | D.HasChangedAddress = !(M.Flags & RF_MoveDistinctMDs) && N.isDistinct(); |
| 505 | return D; |
| 506 | } |
| 507 | |
| 508 | bool MDNodeMapper::tryToPop() { |
| 509 | if (!Worklist.back().second) { |
| 510 | Worklist.back().second = true; |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | MDNode *N = Worklist.pop_back_val().first; |
| 515 | Info[N].ID = POT.size(); |
| 516 | POT.push_back(N); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | bool MDNodeMapper::createPOT(const MDNode &FirstN) { |
| 521 | bool AnyChanges = false; |
| 522 | |
| 523 | // Do a traversal of the unmapped subgraph, tracking whether operands change. |
| 524 | // In some cases, these changes will propagate naturally, but |
| 525 | // propagateChangedOperands() catches the general case. |
| 526 | AnyChanges |= push(FirstN).HasChangedAddress; |
| 527 | while (hasWork()) { |
| 528 | if (tryToPop()) |
| 529 | continue; |
| 530 | |
| 531 | MDNode &N = getCurrentNode(); |
| 532 | bool LocalChanges = false; |
| 533 | for (const Metadata *Op : N.operands()) |
| 534 | LocalChanges |= mapOperand(Op); |
| 535 | |
| 536 | if (!LocalChanges) |
| 537 | continue; |
| 538 | |
| 539 | AnyChanges = true; |
| 540 | auto &D = Info[&N]; |
| 541 | D.HasChangedOps = true; |
| 542 | |
| 543 | // Uniqued nodes change address when operands change. |
| 544 | if (!N.isDistinct()) |
| 545 | D.HasChangedAddress = true; |
| 546 | } |
| 547 | return AnyChanges; |
| 548 | } |
| 549 | |
| 550 | void MDNodeMapper::propagateChangedOperands() { |
| 551 | bool AnyChangedAddresses; |
| 552 | do { |
| 553 | AnyChangedAddresses = false; |
| 554 | for (MDNode *N : POT) { |
| 555 | auto &NI = Info[N]; |
| 556 | if (NI.HasChangedOps) |
| 557 | continue; |
| 558 | |
| 559 | if (!llvm::any_of(N->operands(), [&](const Metadata *Op) { |
| 560 | auto Where = Info.find(Op); |
| 561 | return Where != Info.end() && Where->second.HasChangedAddress; |
| 562 | })) |
| 563 | continue; |
| 564 | |
| 565 | NI.HasChangedOps = true; |
| 566 | if (!N->isDistinct()) { |
| 567 | NI.HasChangedAddress = true; |
| 568 | AnyChangedAddresses = true; |
| 569 | } |
| 570 | } |
| 571 | } while (AnyChangedAddresses); |
| 572 | } |
| 573 | |
| 574 | void MDNodeMapper::mapDistinctNodes() { |
| 575 | // Map all the distinct nodes in POT. |
| 576 | for (MDNode *N : POT) { |
| 577 | if (!N->isDistinct()) |
| 578 | continue; |
| 579 | |
| 580 | if (M.Flags & RF_MoveDistinctMDs) |
| 581 | M.mapToSelf(N); |
| 582 | else |
| 583 | M.mapToMetadata(N, MDNode::replaceWithDistinct(N->clone())); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | void MDNodeMapper::mapUniquedNodes() { |
| 588 | // Construct uniqued nodes, building forward references as necessary. |
| 589 | for (auto *N : POT) { |
| 590 | if (N->isDistinct()) |
| 591 | continue; |
| 592 | |
| 593 | auto &D = Info[N]; |
| 594 | assert(D.HasChangedAddress == D.HasChangedOps && |
| 595 | "Uniqued nodes should change address iff ops change"); |
| 596 | if (!D.HasChangedAddress) { |
| 597 | M.mapToSelf(N); |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | TempMDNode ClonedN = D.Placeholder ? std::move(D.Placeholder) : N->clone(); |
| 602 | remapOperands(D, *ClonedN); |
| 603 | M.mapToMetadata(N, MDNode::replaceWithUniqued(std::move(ClonedN))); |
| 604 | } |
| 605 | |
| 606 | // Resolve cycles. |
| 607 | for (auto *N : POT) |
Teresa Johnson | b703c77 | 2016-03-29 18:24:19 +0000 | [diff] [blame] | 608 | if (!N->isResolved()) |
| 609 | N->resolveCycles(); |
Duncan P. N. Exon Smith | c9fdbdb | 2015-08-07 00:39:26 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 612 | void MDNodeMapper::remapDistinctOperands() { |
| 613 | for (auto *N : POT) { |
| 614 | if (!N->isDistinct()) |
| 615 | continue; |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 616 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 617 | auto &D = Info[N]; |
| 618 | if (!D.HasChangedOps) |
| 619 | continue; |
Duncan P. N. Exon Smith | 8c9dcac | 2015-08-07 00:44:55 +0000 | [diff] [blame] | 620 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 621 | assert(D.HasChangedAddress == !bool(M.Flags & RF_MoveDistinctMDs) && |
| 622 | "Distinct nodes should change address iff they cannot be moved"); |
| 623 | remapOperands(D, D.HasChangedAddress ? *cast<MDNode>(*getMappedOp(N)) : *N); |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 624 | } |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 627 | Metadata *MDNodeMapper::map(const MDNode &FirstN) { |
| 628 | assert(!(M.Flags & RF_NoModuleLevelChanges) && |
| 629 | "MDNodeMapper::map assumes module-level changes"); |
| 630 | assert(POT.empty() && "MDNodeMapper::map is not re-entrant"); |
Duncan P. N. Exon Smith | 14cc94c | 2015-01-14 01:03:05 +0000 | [diff] [blame] | 631 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 632 | // Require resolved nodes whenever metadata might be remapped. |
| 633 | assert(FirstN.isResolved() && "Unexpected unresolved node"); |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 634 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 635 | // Return early if nothing at all changed. |
| 636 | if (!createPOT(FirstN)) { |
| 637 | for (const MDNode *N : POT) |
| 638 | M.mapToSelf(N); |
| 639 | return &const_cast<MDNode &>(FirstN); |
Duncan P. N. Exon Smith | 706f37e | 2015-08-04 06:42:31 +0000 | [diff] [blame] | 640 | } |
Duncan P. N. Exon Smith | 0dcffe2 | 2015-01-19 22:39:07 +0000 | [diff] [blame] | 641 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 642 | propagateChangedOperands(); |
| 643 | mapDistinctNodes(); |
| 644 | mapUniquedNodes(); |
| 645 | remapDistinctOperands(); |
| 646 | |
| 647 | // Return the original node, remapped. |
| 648 | return *getMappedOp(&FirstN); |
Duncan P. N. Exon Smith | b557989 | 2015-01-14 01:06:21 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Duncan P. N. Exon Smith | ae8bd4b | 2016-04-03 19:31:01 +0000 | [diff] [blame] | 651 | Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 652 | // If the value already exists in the map, use it. |
Duncan P. N. Exon Smith | da4a56d | 2016-04-02 17:04:38 +0000 | [diff] [blame] | 653 | if (Optional<Metadata *> NewMD = VM.getMappedMD(MD)) |
| 654 | return *NewMD; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 655 | |
| 656 | if (isa<MDString>(MD)) |
Duncan P. N. Exon Smith | e05ff7c | 2016-04-08 18:47:02 +0000 | [diff] [blame] | 657 | return const_cast<Metadata *>(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 658 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 659 | // This is a module-level metadata. If nothing at the module level is |
| 660 | // changing, use an identity mapping. |
| 661 | if ((Flags & RF_NoModuleLevelChanges)) |
Duncan P. N. Exon Smith | 69341e6 | 2016-04-08 18:49:36 +0000 | [diff] [blame] | 662 | return const_cast<Metadata *>(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 663 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 664 | if (auto *CMD = dyn_cast<ConstantAsMetadata>(MD)) { |
Duncan P. N. Exon Smith | 756e1c3 | 2016-04-03 20:54:51 +0000 | [diff] [blame] | 665 | // Disallow recursion into metadata mapping through mapValue. |
| 666 | VM.disableMapMetadata(); |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 667 | Value *MappedV = mapValue(CMD->getValue()); |
Duncan P. N. Exon Smith | 756e1c3 | 2016-04-03 20:54:51 +0000 | [diff] [blame] | 668 | VM.enableMapMetadata(); |
| 669 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 670 | if (CMD->getValue() == MappedV) |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 671 | return mapToSelf(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 672 | |
Duncan P. N. Exon Smith | 8e65f8d | 2016-04-04 04:59:56 +0000 | [diff] [blame] | 673 | return mapToMetadata(MD, MappedV ? ValueAsMetadata::get(MappedV) : nullptr); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Duncan P. N. Exon Smith | ae8bd4b | 2016-04-03 19:31:01 +0000 | [diff] [blame] | 676 | assert(isa<MDNode>(MD) && "Expected a metadata node"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 677 | |
Duncan P. N. Exon Smith | ae8bd4b | 2016-04-03 19:31:01 +0000 | [diff] [blame] | 678 | return None; |
| 679 | } |
| 680 | |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 681 | Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, |
| 682 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
| 683 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 684 | return Mapper(VM, Flags, TypeMapper, Materializer).mapMetadata(MD); |
| 685 | } |
| 686 | |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 687 | Metadata *Mapper::mapLocalAsMetadata(const LocalAsMetadata &LAM) { |
| 688 | // Lookup the mapping for the value itself, and return the appropriate |
| 689 | // metadata. |
| 690 | if (Value *V = mapValue(LAM.getValue())) { |
| 691 | if (V == LAM.getValue()) |
| 692 | return const_cast<LocalAsMetadata *>(&LAM); |
| 693 | return ValueAsMetadata::get(V); |
| 694 | } |
| 695 | |
| 696 | // FIXME: always return nullptr once Verifier::verifyDominatesUse() ensures |
| 697 | // metadata operands only reference defined SSA values. |
| 698 | return (Flags & RF_IgnoreMissingLocals) |
| 699 | ? nullptr |
| 700 | : MDTuple::get(LAM.getContext(), None); |
| 701 | } |
| 702 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 703 | Metadata *Mapper::mapMetadata(const Metadata *MD) { |
Duncan P. N. Exon Smith | 4ec55f8 | 2016-04-08 03:13:22 +0000 | [diff] [blame] | 704 | assert(MD && "Expected valid metadata"); |
| 705 | assert(!isa<LocalAsMetadata>(MD) && "Unexpected local metadata"); |
| 706 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 707 | if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD)) |
| 708 | return *NewMD; |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 709 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 710 | return MDNodeMapper(*this).map(*cast<MDNode>(MD)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 713 | Mapper::~Mapper() { |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 714 | // Materialize global initializers. |
| 715 | while (!DelayedInits.empty()) { |
| 716 | auto Init = DelayedInits.pop_back_val(); |
| 717 | Materializer->materializeInitFor(Init.New, Init.Old); |
| 718 | } |
| 719 | |
| 720 | // Process block addresses delayed until global inits. |
| 721 | while (!DelayedBBs.empty()) { |
| 722 | DelayedBasicBlock DBB = DelayedBBs.pop_back_val(); |
| 723 | BasicBlock *BB = cast_or_null<BasicBlock>(mapValue(DBB.OldBB)); |
| 724 | DBB.TempBB->replaceAllUsesWith(BB ? BB : DBB.OldBB); |
| 725 | } |
| 726 | |
Duncan P. N. Exon Smith | ea7df77 | 2016-04-05 20:23:21 +0000 | [diff] [blame] | 727 | // We don't expect these to grow after clearing. |
Duncan P. N. Exon Smith | c6065e3 | 2016-04-03 20:17:45 +0000 | [diff] [blame] | 728 | assert(DelayedInits.empty()); |
| 729 | assert(DelayedBBs.empty()); |
Duncan P. N. Exon Smith | 829dc87 | 2016-04-03 19:06:24 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 732 | MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, |
| 733 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
| 734 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | da4a56d | 2016-04-02 17:04:38 +0000 | [diff] [blame] | 735 | return cast_or_null<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, |
| 736 | Flags, TypeMapper, Materializer)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 739 | void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VM, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 740 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 741 | ValueMaterializer *Materializer) { |
| 742 | Mapper(VM, Flags, TypeMapper, Materializer).remapInstruction(I); |
| 743 | } |
| 744 | |
| 745 | void Mapper::remapInstruction(Instruction *I) { |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 746 | // Remap operands. |
Gabor Greif | 5df4326 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 747 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 748 | Value *V = mapValue(*op); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 749 | // If we aren't ignoring missing entries, assert that something happened. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 750 | if (V) |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 751 | *op = V; |
| 752 | else |
Duncan P. N. Exon Smith | da68cbc | 2016-04-07 00:26:43 +0000 | [diff] [blame] | 753 | assert((Flags & RF_IgnoreMissingLocals) && |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 754 | "Referenced value not in value map!"); |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 755 | } |
Daniel Dunbar | 95fe13c | 2010-08-26 03:48:08 +0000 | [diff] [blame] | 756 | |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 757 | // Remap phi nodes' incoming blocks. |
| 758 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 759 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 760 | // FIXME: Use Mapper::mapValue (but note the missing Materializer flag). |
| 761 | Value *V = MapValue(PN->getIncomingBlock(i), VM, Flags); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 762 | // If we aren't ignoring missing entries, assert that something happened. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 763 | if (V) |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 764 | PN->setIncomingBlock(i, cast<BasicBlock>(V)); |
| 765 | else |
Duncan P. N. Exon Smith | da68cbc | 2016-04-07 00:26:43 +0000 | [diff] [blame] | 766 | assert((Flags & RF_IgnoreMissingLocals) && |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 767 | "Referenced block not in value map!"); |
| 768 | } |
| 769 | } |
| 770 | |
Devang Patel | c017404 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 771 | // Remap attached metadata. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 772 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
Devang Patel | c017404 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 773 | I->getAllMetadata(MDs); |
Duncan P. N. Exon Smith | e08bcbf | 2015-08-03 03:27:12 +0000 | [diff] [blame] | 774 | for (const auto &MI : MDs) { |
| 775 | MDNode *Old = MI.second; |
Duncan P. N. Exon Smith | a574e7a | 2016-04-08 19:09:34 +0000 | [diff] [blame^] | 776 | MDNode *New = cast_or_null<MDNode>(mapMetadata(Old)); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 777 | if (New != Old) |
Duncan P. N. Exon Smith | e08bcbf | 2015-08-03 03:27:12 +0000 | [diff] [blame] | 778 | I->setMetadata(MI.first, New); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 779 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 780 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 781 | if (!TypeMapper) |
| 782 | return; |
| 783 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 784 | // If the instruction's type is being remapped, do so now. |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 785 | if (auto CS = CallSite(I)) { |
| 786 | SmallVector<Type *, 3> Tys; |
| 787 | FunctionType *FTy = CS.getFunctionType(); |
| 788 | Tys.reserve(FTy->getNumParams()); |
| 789 | for (Type *Ty : FTy->params()) |
| 790 | Tys.push_back(TypeMapper->remapType(Ty)); |
| 791 | CS.mutateFunctionType(FunctionType::get( |
| 792 | TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg())); |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | if (auto *AI = dyn_cast<AllocaInst>(I)) |
| 796 | AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType())); |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 797 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 798 | GEP->setSourceElementType( |
| 799 | TypeMapper->remapType(GEP->getSourceElementType())); |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 800 | GEP->setResultElementType( |
| 801 | TypeMapper->remapType(GEP->getResultElementType())); |
| 802 | } |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 803 | I->mutateType(TypeMapper->remapType(I->getType())); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 804 | } |