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