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