1 //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the MapValue interface which is used by various parts of 10 // the Transforms/Utils library to implement cloning and linking facilities. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H 15 #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/simple_ilist.h" 20 #include "llvm/IR/ValueHandle.h" 21 #include "llvm/IR/ValueMap.h" 22 23 namespace llvm { 24 25 class Constant; 26 class DIBuilder; 27 class DbgRecord; 28 class Function; 29 class GlobalVariable; 30 class Instruction; 31 class MDNode; 32 class Metadata; 33 class Module; 34 class Type; 35 class Value; 36 37 using ValueToValueMapTy = ValueMap<const Value *, WeakTrackingVH>; 38 using DbgRecordIterator = simple_ilist<DbgRecord>::iterator; 39 using MetadataSetTy = SmallPtrSet<const Metadata *, 16>; 40 41 /// This is a class that can be implemented by clients to remap types when 42 /// cloning constants and instructions. 43 class ValueMapTypeRemapper { 44 virtual void anchor(); // Out of line method. 45 46 public: 47 virtual ~ValueMapTypeRemapper() = default; 48 49 /// The client should implement this method if they want to remap types while 50 /// mapping values. 51 virtual Type *remapType(Type *SrcTy) = 0; 52 }; 53 54 /// This is a class that can be implemented by clients to materialize Values on 55 /// demand. 56 class ValueMaterializer { 57 virtual void anchor(); // Out of line method. 58 59 protected: 60 ValueMaterializer() = default; 61 ValueMaterializer(const ValueMaterializer &) = default; 62 ValueMaterializer &operator=(const ValueMaterializer &) = default; 63 ~ValueMaterializer() = default; 64 65 public: 66 /// This method can be implemented to generate a mapped Value on demand. For 67 /// example, if linking lazily. Returns null if the value is not materialized. 68 virtual Value *materialize(Value *V) = 0; 69 }; 70 71 /// These are flags that the value mapping APIs allow. 72 enum RemapFlags { 73 RF_None = 0, 74 75 /// If this flag is set, the remapper knows that only local values within a 76 /// function (such as an instruction or argument) are mapped, not global 77 /// values like functions and global metadata. 78 RF_NoModuleLevelChanges = 1, 79 80 /// If this flag is set, the remapper ignores missing function-local entries 81 /// (Argument, Instruction, BasicBlock) that are not in the value map. If it 82 /// is unset, it aborts if an operand is asked to be remapped which doesn't 83 /// exist in the mapping. 84 /// 85 /// There are no such assertions in MapValue(), whose results are almost 86 /// unchanged by this flag. This flag mainly changes the assertion behaviour 87 /// in RemapInstruction(). 88 /// 89 /// Since an Instruction's metadata operands (even that point to SSA values) 90 /// aren't guaranteed to be dominated by their definitions, MapMetadata will 91 /// return "!{}" instead of "null" for \a LocalAsMetadata instances whose SSA 92 /// values are unmapped when this flag is set. Otherwise, \a MapValue() 93 /// completely ignores this flag. 94 /// 95 /// \a MapMetadata() always ignores this flag. 96 RF_IgnoreMissingLocals = 2, 97 98 /// Instruct the remapper to reuse and mutate distinct metadata (remapping 99 /// them in place) instead of cloning remapped copies. This flag has no 100 /// effect when RF_NoModuleLevelChanges, since that implies an identity 101 /// mapping. 102 RF_ReuseAndMutateDistinctMDs = 4, 103 104 /// Any global values not in value map are mapped to null instead of mapping 105 /// to self. Illegal if RF_IgnoreMissingLocals is also set. 106 RF_NullMapMissingGlobalValues = 8, 107 }; 108 109 inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) { 110 return RemapFlags(unsigned(LHS) | unsigned(RHS)); 111 } 112 113 /// Context for (re-)mapping values (and metadata). 114 /// 115 /// A shared context used for mapping and remapping of Value and Metadata 116 /// instances using \a ValueToValueMapTy, \a RemapFlags, \a 117 /// ValueMapTypeRemapper, \a ValueMaterializer, and \a IdentityMD. 118 /// 119 /// There are a number of top-level entry points: 120 /// - \a mapValue() (and \a mapConstant()); 121 /// - \a mapMetadata() (and \a mapMDNode()); 122 /// - \a remapInstruction(); 123 /// - \a remapFunction(); and 124 /// - \a remapGlobalObjectMetadata(). 125 /// 126 /// The \a ValueMaterializer can be used as a callback, but cannot invoke any 127 /// of these top-level functions recursively. Instead, callbacks should use 128 /// one of the following to schedule work lazily in the \a ValueMapper 129 /// instance: 130 /// - \a scheduleMapGlobalInitializer() 131 /// - \a scheduleMapAppendingVariable() 132 /// - \a scheduleMapGlobalAlias() 133 /// - \a scheduleMapGlobalIFunc() 134 /// - \a scheduleRemapFunction() 135 /// 136 /// Sometimes a callback needs a different mapping context. Such a context can 137 /// be registered using \a registerAlternateMappingContext(), which takes an 138 /// alternate \a ValueToValueMapTy and \a ValueMaterializer and returns a ID to 139 /// pass into the schedule*() functions. 140 /// 141 /// If an \a IdentityMD set is optionally provided, \a Metadata inside this set 142 /// will be mapped onto itself in \a VM on first use. 143 /// 144 /// TODO: lib/Linker really doesn't need the \a ValueHandle in the \a 145 /// ValueToValueMapTy. We should template \a ValueMapper (and its 146 /// implementation classes), and explicitly instantiate on two concrete 147 /// instances of \a ValueMap (one as \a ValueToValueMap, and one with raw \a 148 /// Value pointers). It may be viable to do away with \a TrackingMDRef in the 149 /// \a Metadata side map for the lib/Linker case as well, in which case we'll 150 /// need a new template parameter on \a ValueMap. 151 /// 152 /// TODO: Update callers of \a RemapInstruction() and \a MapValue() (etc.) to 153 /// use \a ValueMapper directly. 154 class ValueMapper { 155 void *pImpl; 156 157 public: 158 ValueMapper(ValueToValueMapTy &VM, RemapFlags Flags = RF_None, 159 ValueMapTypeRemapper *TypeMapper = nullptr, 160 ValueMaterializer *Materializer = nullptr, 161 const MetadataSetTy *IdentityMD = nullptr); 162 ValueMapper(ValueMapper &&) = delete; 163 ValueMapper(const ValueMapper &) = delete; 164 ValueMapper &operator=(ValueMapper &&) = delete; 165 ValueMapper &operator=(const ValueMapper &) = delete; 166 ~ValueMapper(); 167 168 /// Register an alternate mapping context. 169 /// 170 /// Returns a MappingContextID that can be used with the various schedule*() 171 /// API to switch in a different value map on-the-fly. 172 unsigned 173 registerAlternateMappingContext(ValueToValueMapTy &VM, 174 ValueMaterializer *Materializer = nullptr); 175 176 /// Add to the current \a RemapFlags. 177 /// 178 /// \note Like the top-level mapping functions, \a addFlags() must be called 179 /// at the top level, not during a callback in a \a ValueMaterializer. 180 void addFlags(RemapFlags Flags); 181 182 Metadata *mapMetadata(const Metadata &MD); 183 MDNode *mapMDNode(const MDNode &N); 184 185 Value *mapValue(const Value &V); 186 Constant *mapConstant(const Constant &C); 187 188 void remapInstruction(Instruction &I); 189 void remapDbgRecord(Module *M, DbgRecord &V); 190 void remapDbgRecordRange(Module *M, iterator_range<DbgRecordIterator> Range); 191 void remapFunction(Function &F); 192 void remapGlobalObjectMetadata(GlobalObject &GO); 193 194 void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, 195 unsigned MappingContextID = 0); 196 void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, 197 bool IsOldCtorDtor, 198 ArrayRef<Constant *> NewMembers, 199 unsigned MappingContextID = 0); 200 void scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee, 201 unsigned MappingContextID = 0); 202 void scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver, 203 unsigned MappingContextID = 0); 204 void scheduleRemapFunction(Function &F, unsigned MappingContextID = 0); 205 }; 206 207 /// Look up or compute a value in the value map. 208 /// 209 /// Return a mapped value for a function-local value (Argument, Instruction, 210 /// BasicBlock), or compute and memoize a value for a Constant. 211 /// 212 /// 1. If \c V is in VM, return the result. 213 /// 2. Else if \c V can be materialized with \c Materializer, do so, memoize 214 /// it in \c VM, and return it. 215 /// 3. Else if \c V is a function-local value, return nullptr. 216 /// 4. Else if \c V is a \a GlobalValue, return \c nullptr or \c V depending 217 /// on \a RF_NullMapMissingGlobalValues. 218 /// 5. Else if \c V is a \a MetadataAsValue wrapping a LocalAsMetadata, 219 /// recurse on the local SSA value, and return nullptr or "metadata !{}" on 220 /// missing depending on RF_IgnoreMissingValues. 221 /// 6. Else if \c V is a \a MetadataAsValue, rewrap the return of \a 222 /// MapMetadata(). 223 /// 7. Else, compute the equivalent constant, and return it. 224 inline Value *MapValue(const Value *V, ValueToValueMapTy &VM, 225 RemapFlags Flags = RF_None, 226 ValueMapTypeRemapper *TypeMapper = nullptr, 227 ValueMaterializer *Materializer = nullptr, 228 const MetadataSetTy *IdentityMD = nullptr) { 229 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 230 .mapValue(*V); 231 } 232 233 /// Lookup or compute a mapping for a piece of metadata. 234 /// 235 /// Compute and memoize a mapping for \c MD. 236 /// 237 /// 1. If \c MD is mapped, return it. 238 /// 2. Else if \a RF_NoModuleLevelChanges or \c MD is an \a MDString, return 239 /// \c MD. 240 /// 3. Else if \c MD is a \a ConstantAsMetadata, call \a MapValue() and 241 /// re-wrap its return (returning nullptr on nullptr). 242 /// 4. Else if \c MD is in \c IdentityMD then add an identity mapping for it 243 /// and return it. 244 /// 5. Else, \c MD is an \a MDNode. These are remapped, along with their 245 /// transitive operands. Distinct nodes are duplicated or moved depending 246 /// on \a RF_MoveDistinctNodes. Uniqued nodes are remapped like constants. 247 /// 248 /// \note \a LocalAsMetadata is completely unsupported by \a MapMetadata. 249 /// Instead, use \a MapValue() with its wrapping \a MetadataAsValue instance. 250 inline Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, 251 RemapFlags Flags = RF_None, 252 ValueMapTypeRemapper *TypeMapper = nullptr, 253 ValueMaterializer *Materializer = nullptr, 254 const MetadataSetTy *IdentityMD = nullptr) { 255 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 256 .mapMetadata(*MD); 257 } 258 259 /// Version of MapMetadata with type safety for MDNode. 260 inline MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, 261 RemapFlags Flags = RF_None, 262 ValueMapTypeRemapper *TypeMapper = nullptr, 263 ValueMaterializer *Materializer = nullptr, 264 const MetadataSetTy *IdentityMD = nullptr) { 265 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 266 .mapMDNode(*MD); 267 } 268 269 /// Convert the instruction operands from referencing the current values into 270 /// those specified by VM. 271 /// 272 /// If \a RF_IgnoreMissingLocals is set and an operand can't be found via \a 273 /// MapValue(), use the old value. Otherwise assert that this doesn't happen. 274 /// 275 /// Note that \a MapValue() only returns \c nullptr for SSA values missing from 276 /// \c VM. 277 inline void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, 278 RemapFlags Flags = RF_None, 279 ValueMapTypeRemapper *TypeMapper = nullptr, 280 ValueMaterializer *Materializer = nullptr, 281 const MetadataSetTy *IdentityMD = nullptr) { 282 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 283 .remapInstruction(*I); 284 } 285 286 /// Remap the Values used in the DbgRecord \a DR using the value map \a 287 /// VM. 288 inline void RemapDbgRecord(Module *M, DbgRecord *DR, ValueToValueMapTy &VM, 289 RemapFlags Flags = RF_None, 290 ValueMapTypeRemapper *TypeMapper = nullptr, 291 ValueMaterializer *Materializer = nullptr, 292 const MetadataSetTy *IdentityMD = nullptr) { 293 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 294 .remapDbgRecord(M, *DR); 295 } 296 297 /// Remap the Values used in the DbgRecords \a Range using the value map \a 298 /// VM. 299 inline void RemapDbgRecordRange(Module *M, 300 iterator_range<DbgRecordIterator> Range, 301 ValueToValueMapTy &VM, 302 RemapFlags Flags = RF_None, 303 ValueMapTypeRemapper *TypeMapper = nullptr, 304 ValueMaterializer *Materializer = nullptr, 305 const MetadataSetTy *IdentityMD = nullptr) { 306 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 307 .remapDbgRecordRange(M, Range); 308 } 309 310 /// Remap the operands, metadata, arguments, and instructions of a function. 311 /// 312 /// Calls \a MapValue() on prefix data, prologue data, and personality 313 /// function; calls \a MapMetadata() on each attached MDNode; remaps the 314 /// argument types using the provided \c TypeMapper; and calls \a 315 /// RemapInstruction() on every instruction. 316 inline void RemapFunction(Function &F, ValueToValueMapTy &VM, 317 RemapFlags Flags = RF_None, 318 ValueMapTypeRemapper *TypeMapper = nullptr, 319 ValueMaterializer *Materializer = nullptr, 320 const MetadataSetTy *IdentityMD = nullptr) { 321 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD).remapFunction(F); 322 } 323 324 /// Version of MapValue with type safety for Constant. 325 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM, 326 RemapFlags Flags = RF_None, 327 ValueMapTypeRemapper *TypeMapper = nullptr, 328 ValueMaterializer *Materializer = nullptr, 329 const MetadataSetTy *IdentityMD = nullptr) { 330 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD) 331 .mapConstant(*V); 332 } 333 334 } // end namespace llvm 335 336 #endif // LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H 337