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